Skip to content

Commit 9e0e6aa

Browse files
committed
[RocksDB] make sure KSVObsolete does not get accessed as a valid pointer.
Summary: KSVObsolete is no longer nullptr and needs to be checked explicitly. Also did some minor code cleanup and added a stat counter to track superversion cleanups incurred in the foreground. Test Plan: make check Reviewers: ljin Reviewed By: ljin CC: leveldb Differential Revision: https://reviews.facebook.net/D16701
1 parent cff908d commit 9e0e6aa

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

db/db_impl.cc

+10-6
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,7 @@ bool DBImpl::SuperVersion::Unref() {
541541
}
542542

543543
void DBImpl::SuperVersion::Cleanup() {
544+
db->mutex_.AssertHeld();
544545
assert(refs.load(std::memory_order_relaxed) == 0);
545546
imm->Unref(&to_delete);
546547
MemTable* m = mem->Unref();
@@ -552,6 +553,7 @@ void DBImpl::SuperVersion::Cleanup() {
552553

553554
void DBImpl::SuperVersion::Init(MemTable* new_mem, MemTableListVersion* new_imm,
554555
Version* new_current) {
556+
db->mutex_.AssertHeld();
555557
mem = new_mem;
556558
imm = new_imm;
557559
current = new_current;
@@ -2960,8 +2962,10 @@ Status DBImpl::GetImpl(const ReadOptions& options,
29602962
// acquiring mutex for this operation, we use atomic Swap() on the thread
29612963
// local pointer to guarantee exclusive access. If the thread local pointer
29622964
// is being used while a new SuperVersion is installed, the cached
2963-
// SuperVersion can become stale. It will eventually get refreshed either
2964-
// on the next GetImpl() call or next SuperVersion installation.
2965+
// SuperVersion can become stale. In that case, the background thread would
2966+
// have swapped in kSVObsolete. We re-check the value at the end of
2967+
// Get, with an atomic compare and swap. The superversion will be released
2968+
// if detected to be stale.
29652969
void* ptr = local_sv_->Swap(SuperVersion::kSVInUse);
29662970
// Invariant:
29672971
// (1) Scrape (always) installs kSVObsolete in ThreadLocal storage
@@ -2976,7 +2980,10 @@ Status DBImpl::GetImpl(const ReadOptions& options,
29762980
SuperVersion* sv_to_delete = nullptr;
29772981

29782982
if (sv && sv->Unref()) {
2983+
RecordTick(options_.statistics.get(), NUMBER_SUPERVERSION_CLEANUPS);
29792984
mutex_.Lock();
2985+
// TODO underlying resources held by superversion (sst files) might
2986+
// not be released until the next background job.
29802987
sv->Cleanup();
29812988
sv_to_delete = sv;
29822989
} else {
@@ -3051,15 +3058,12 @@ Status DBImpl::GetImpl(const ReadOptions& options,
30513058

30523059
if (unref_sv) {
30533060
// Release SuperVersion
3054-
bool delete_sv = false;
30553061
if (sv->Unref()) {
30563062
mutex_.Lock();
30573063
sv->Cleanup();
30583064
mutex_.Unlock();
3059-
delete_sv = true;
3060-
}
3061-
if (delete_sv) {
30623065
delete sv;
3066+
RecordTick(options_.statistics.get(), NUMBER_SUPERVERSION_CLEANUPS);
30633067
}
30643068
RecordTick(options_.statistics.get(), NUMBER_SUPERVERSION_RELEASES);
30653069
}

db/db_impl.h

+1
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ class DBImpl : public DB {
291291
private:
292292
friend class DB;
293293
friend class TailingIterator;
294+
friend struct SuperVersion;
294295
struct CompactionState;
295296
struct Writer;
296297

include/rocksdb/statistics.h

+2
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ enum Tickers {
124124
NUMBER_DIRECT_LOAD_TABLE_PROPERTIES,
125125
NUMBER_SUPERVERSION_ACQUIRES,
126126
NUMBER_SUPERVERSION_RELEASES,
127+
NUMBER_SUPERVERSION_CLEANUPS,
127128
TICKER_ENUM_MAX
128129
};
129130

@@ -181,6 +182,7 @@ const std::vector<std::pair<Tickers, std::string>> TickersNameMap = {
181182
"rocksdb.number.direct.load.table.properties"},
182183
{NUMBER_SUPERVERSION_ACQUIRES, "rocksdb.number.superversion_acquires"},
183184
{NUMBER_SUPERVERSION_RELEASES, "rocksdb.number.superversion_releases"},
185+
{NUMBER_SUPERVERSION_CLEANUPS, "rocksdb.number.superversion_cleanups"},
184186
};
185187

186188
/**

0 commit comments

Comments
 (0)