Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete iterator in replication_test #295

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cloud/replication_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1315,13 +1315,15 @@ TEST_F(ReplicationTest, SuperSnapshot) {
ASSERT_OK(follower->Get(ro, "k1", &val));
EXPECT_EQ(val, "v1");

auto iter = follower->NewIterator(ro, follower->DefaultColumnFamily());
auto iter = std::unique_ptr<rocksdb::Iterator>(
follower->NewIterator(ro, follower->DefaultColumnFamily()));
iter->SeekToFirst();
EXPECT_TRUE(iter->Valid());
EXPECT_EQ(iter->key(), "k1");
EXPECT_EQ(iter->value(), "v1");
iter->Next();
EXPECT_FALSE(iter->Valid());
iter.reset();

ro.snapshot = nullptr;
ASSERT_OK(follower->Get(ro, followerCF("cf1"), "cf1k1", &val));
Expand Down
5 changes: 4 additions & 1 deletion db/db_impl/db_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4077,6 +4077,8 @@ Status DBImpl::GetSuperSnapshots(
return Status::InvalidArgument(
"GetSuperSnapshots only supported in RocksDB compiled with USE_RTTI=1");
#endif
InstrumentedMutexLock l(&mutex_);

if (!is_snapshot_supported_) {
return Status::InvalidArgument("Snapshot not supported");
}
Expand All @@ -4092,7 +4094,8 @@ Status DBImpl::GetSuperSnapshots(
for (auto& cf : column_families) {
auto cfh = static_cast_with_check<ColumnFamilyHandleImpl>(cf);
auto cfd = cfh->cfd();
auto sv = cfd->GetReferencedSuperVersion(this);
auto sv = cfd->GetSuperVersion();
sv->Ref();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what the difference is here could you give me some context?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are equivalent, but GetReferencedSuperVersion() is meant to (and optimized to) be called outside of the lock. By adding a mutex to GetSuperSnapshots() (oops) it caused a deadlock :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's using quite an interesting technique:

// The SuperVersion is cached in thread local storage to avoid acquiring
// mutex when SuperVersion does not change since the last use. When a new
// SuperVersion is installed, the compaction or flush thread cleans up
// cached SuperVersion in all existing thread local storage. To avoid
// acquiring mutex for this operation, we use atomic Swap() on the thread
// local pointer to guarantee exclusive access. If the thread local pointer
// is being used while a new SuperVersion is installed, the cached
// SuperVersion can become stale. In that case, the background thread would
// have swapped in kSVObsolete. We re-check the value at when returning
// SuperVersion back to thread local, with an atomic compare and swap.
// The superversion will need to be released if detected to be stale.
void* ptr = local_sv_->Swap(SuperVersion::kSVInUse);
// Invariant:
// (1) Scrape (always) installs kSVObsolete in ThreadLocal storage
// (2) the Swap above (always) installs kSVInUse, ThreadLocal storage
// should only keep kSVInUse before ReturnThreadLocalSuperVersion call
// (if no Scrape happens).
assert(ptr != SuperVersion::kSVInUse);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will have to read more outside the function but definitely interesting. Not quite clear to me how thread local super versions get cleaned after a swap since local_sv becomes obsolete (dump thread does it? but then wouldn't that mess up the currently used sv for the thread?)

auto ss = new SuperSnapshotImpl(cfd, sv);
snapshots_.New(ss, snapshot_seq, unix_time,
/*is_write_conflict_boundary=*/false);
Expand Down
Loading