chore: add db_slice lock to protect segments from preemptions (#3406)

DastTable::Traverse is error prone when the callback passed preempts because the segment might change. This is problematic and we need atomicity while traversing segments with preemption. The fix is to add Traverse in DbSlice and protect the traversal via ThreadLocalMutex.

* add ConditionFlag to DbSlice
* add Traverse in DbSlice and protect it with the ConditionFlag
* remove condition flag from snapshot
* remove condition flag from streamer

---------

Signed-off-by: kostas <kostas@dragonflydb.io>
This commit is contained in:
Kostas Kyrimis 2024-07-30 15:02:54 +03:00 committed by GitHub
parent f536f8afbd
commit aa02070e3d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 65 additions and 71 deletions

View file

@ -365,45 +365,18 @@ struct ConditionFlag {
};
// Helper class used to guarantee atomicity between serialization of buckets
class ConditionGuard {
class ThreadLocalMutex {
public:
explicit ConditionGuard(ConditionFlag* enclosing) : enclosing_(enclosing) {
util::fb2::NoOpLock noop_lk_;
enclosing_->cond_var.wait(noop_lk_, [this]() { return !enclosing_->flag; });
enclosing_->flag = true;
}
ThreadLocalMutex();
~ThreadLocalMutex();
~ConditionGuard() {
enclosing_->flag = false;
enclosing_->cond_var.notify_one();
}
private:
ConditionFlag* enclosing_;
};
class LocalBlockingCounter {
public:
void lock() {
++mutating_;
}
void unlock() {
DCHECK(mutating_ > 0);
--mutating_;
if (mutating_ == 0) {
cond_var_.notify_one();
}
}
void Wait() {
util::fb2::NoOpLock noop_lk_;
cond_var_.wait(noop_lk_, [this]() { return mutating_ == 0; });
}
void lock();
void unlock();
private:
EngineShard* shard_;
util::fb2::CondVarAny cond_var_;
size_t mutating_ = 0;
bool flag_ = false;
};
} // namespace dfly