Skip to content

Commit 506bc10

Browse files
jameseh96daverigby
authored andcommitted
RocksDB: Ensure Snapshots are released when no longer needed
Change-Id: I4e9722be036390adc52fa2f365e8215f9fb6126b Reviewed-on: http://review.couchbase.org/82916 Reviewed-by: Dave Rigby <[email protected]> Tested-by: Build Bot <[email protected]>
1 parent f98c74e commit 506bc10

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

engines/ep/src/rocksdb-kvstore/rocksdb-kvstore.cc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,7 @@ ScanContext* RocksDBKVStore::initScanContext(
670670
DocumentFilter options,
671671
ValueFilter valOptions) {
672672
size_t scanId = scanCounter++;
673+
scanSnapshots.emplace(scanId, SnapshotPtr(db->GetSnapshot(), *db));
673674
// As we cannot efficiently determine how many documents this scan will
674675
// find, we approximate this value with the seqno difference + 1
675676
// as scan is supposed to be inclusive at both ends,
@@ -707,7 +708,7 @@ scan_error_t RocksDBKVStore::scan(ScanContext* ctx) {
707708
: GetMetaOnly::No;
708709

709710
rocksdb::ReadOptions snapshotOpts{rocksdb::ReadOptions()};
710-
snapshotOpts.snapshot = db->GetSnapshot();
711+
snapshotOpts.snapshot = scanSnapshots.at(ctx->scanId).get();
711712

712713
std::unique_ptr<rocksdb::Iterator> it(
713714
db->NewIterator(snapshotOpts, seqnoFamilyHandle.get()));
@@ -799,3 +800,13 @@ scan_error_t RocksDBKVStore::scan(ScanContext* ctx) {
799800

800801
return scan_success;
801802
}
803+
804+
void RocksDBKVStore::destroyScanContext(ScanContext* ctx) {
805+
// TODO RDB: Might be nice to have the snapshot in the ctx and
806+
// release it on destruction
807+
auto it = scanSnapshots.find(ctx->scanId);
808+
if (it != scanSnapshots.end()) {
809+
scanSnapshots.erase(it);
810+
}
811+
delete ctx;
812+
}

engines/ep/src/rocksdb-kvstore/rocksdb-kvstore.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,7 @@ class RocksDBKVStore : public KVStore {
262262

263263
scan_error_t scan(ScanContext* sctx) override;
264264

265-
void destroyScanContext(ScanContext* ctx) override {
266-
// TODO vmx 2016-10-29: implement
267-
delete ctx;
268-
}
265+
void destroyScanContext(ScanContext* ctx) override;
269266

270267
bool persistCollectionsManifestItem(uint16_t vbid,
271268
const Item& manifestItem) override {
@@ -356,5 +353,17 @@ class RocksDBKVStore : public KVStore {
356353

357354
std::atomic<size_t> scanCounter; // atomic counter for generating scan id
358355

356+
struct SnapshotDeleter {
357+
SnapshotDeleter(rocksdb::DB& db) : db(db) {
358+
}
359+
void operator()(const rocksdb::Snapshot* s) {
360+
db.ReleaseSnapshot(s);
361+
}
362+
rocksdb::DB& db;
363+
};
364+
using SnapshotPtr =
365+
std::unique_ptr<const rocksdb::Snapshot, SnapshotDeleter>;
366+
std::map<size_t, SnapshotPtr> scanSnapshots;
367+
359368
Logger& logger;
360369
};

0 commit comments

Comments
 (0)