diff --git a/src/daemons/StorageDaemon.cpp b/src/daemons/StorageDaemon.cpp index 97faf10ec75..00d5ba3b173 100644 --- a/src/daemons/StorageDaemon.cpp +++ b/src/daemons/StorageDaemon.cpp @@ -180,7 +180,7 @@ void signalHandler(int sig) { case SIGTERM: FLOG_INFO("Signal %d(%s) received, stopping this server", sig, ::strsignal(sig)); if (gStorageServer) { - gStorageServer->stop(); + gStorageServer->notifyStop(); } break; default: diff --git a/src/storage/StorageServer.cpp b/src/storage/StorageServer.cpp index 44e864928b3..c03ec525dcc 100644 --- a/src/storage/StorageServer.cpp +++ b/src/storage/StorageServer.cpp @@ -305,15 +305,40 @@ bool StorageServer::start() { internalStorageSvcStatus_.load() != STATUS_RUNNING) { return false; } + { + std::lock_guard lkStop(muStop_); + if (serverStatus_ != STATUS_UNINITIALIZED) { + // stop() called during start() + return false; + } + serverStatus_ = STATUS_RUNNING; + } return true; } void StorageServer::waitUntilStop() { + { + std::unique_lock lkStop(muStop_); + while (serverStatus_ == STATUS_RUNNING) { + cvStop_.wait(lkStop); + } + } + + this->stop(); + adminThread_->join(); storageThread_->join(); internalStorageThread_->join(); } +void StorageServer::notifyStop() { + std::unique_lock lkStop(muStop_); + if (serverStatus_ == STATUS_RUNNING) { + serverStatus_ = STATUS_STOPPED; + cvStop_.notify_one(); + } +} + void StorageServer::stop() { if (adminSvcStatus_.load() == ServiceStatus::STATUS_STOPPED && storageSvcStatus_.load() == ServiceStatus::STATUS_STOPPED && diff --git a/src/storage/StorageServer.h b/src/storage/StorageServer.h index ba4936c060d..27795c5eaf5 100644 --- a/src/storage/StorageServer.h +++ b/src/storage/StorageServer.h @@ -39,6 +39,9 @@ class StorageServer final { void stop(); + // used for signal handler to set an internal stop flag + void notifyStop(); + void waitUntilStop(); private: @@ -88,6 +91,10 @@ class StorageServer final { std::unique_ptr txnMan_{nullptr}; // used for communicate between one storaged to another std::unique_ptr interClient_; + + ServiceStatus serverStatus_{STATUS_UNINITIALIZED}; + std::mutex muStop_; + std::condition_variable cvStop_; }; } // namespace storage diff --git a/src/storage/transaction/TransactionManager.cpp b/src/storage/transaction/TransactionManager.cpp index 3c073d6105c..3c42694adc1 100644 --- a/src/storage/transaction/TransactionManager.cpp +++ b/src/storage/transaction/TransactionManager.cpp @@ -98,6 +98,7 @@ bool TransactionManager::start() { } void TransactionManager::stop() { + exec_->stop(); resumeThread_->stop(); resumeThread_->wait(); }