chore: change Namespaces to be a global pointer (#4032)

* chore: change Namespaces to be a global pointer

Before the namespaces object was defined globally.
However it has non-trivial d'tor that is being called after main exits.
It's quite dangerous to have global non-POD objects being defined globally.
For example, if we used LOG(INFO) inside the Clear function , that would crash dragonfly on exit.

Ths PR changes it to be a global pointer.

---------

Signed-off-by: Roman Gershman <roman@dragonflydb.io>
This commit is contained in:
Roman Gershman 2024-11-10 12:45:53 +02:00 committed by GitHub
parent 9366c67464
commit be96e6cf99
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
22 changed files with 76 additions and 71 deletions

View file

@ -103,7 +103,10 @@ EngineShardSet* shard_set = nullptr;
void EngineShardSet::Init(uint32_t sz, std::function<void()> shard_handler) {
CHECK_EQ(0u, size());
CHECK(namespaces == nullptr);
shards_.reset(new EngineShard*[sz]);
size_ = sz;
size_t max_shard_file_size = GetTieredFileLimit(sz);
pp_->AwaitFiberOnAll([this](uint32_t index, ProactorBase* pb) {
@ -112,7 +115,8 @@ void EngineShardSet::Init(uint32_t sz, std::function<void()> shard_handler) {
}
});
namespaces.Init();
// The order is important here. We must initialize namespaces after shards_.
namespaces = new Namespaces();
pp_->AwaitFiberOnAll([&](uint32_t index, ProactorBase* pb) {
if (index < size_) {
@ -139,7 +143,13 @@ void EngineShardSet::PreShutdown() {
}
void EngineShardSet::Shutdown() {
// Calling Namespaces::Clear before destroying engine shards, because it accesses them
// internally.
namespaces->Clear();
RunBlockingInParallel([](EngineShard*) { EngineShard::DestroyThreadLocal(); });
delete namespaces;
namespaces = nullptr;
}
void EngineShardSet::InitThreadLocal(ProactorBase* pb) {
@ -150,7 +160,7 @@ void EngineShardSet::InitThreadLocal(ProactorBase* pb) {
void EngineShardSet::TEST_EnableCacheMode() {
RunBlockingInParallel([](EngineShard* shard) {
namespaces.GetDefaultNamespace().GetCurrentDbSlice().TEST_EnableCacheMode();
namespaces->GetDefaultNamespace().GetCurrentDbSlice().TEST_EnableCacheMode();
});
}