Avoid starting up storage expiration thread with zero backends

This commit is contained in:
Tim Wojtulewicz 2025-09-15 14:18:03 -07:00
parent 8bc849d425
commit 50581bfd3c
3 changed files with 10 additions and 2 deletions

View file

@ -22,7 +22,7 @@ void detail::ExpirationTimer::Dispatch(double t, bool is_expire) {
// start another one running. If this causes us to skip a cycle, that's not // start another one running. If this causes us to skip a cycle, that's not
// a big deal as the next cycle will catch anything that should be expired // a big deal as the next cycle will catch anything that should be expired
// in the interim. // in the interim.
if ( ! expire_running.test_and_set() ) { if ( ! expire_running.test_and_set() && storage_mgr->BackendCount() > 0 ) {
DBG_LOG(DBG_STORAGE, "Starting new expiration thread"); DBG_LOG(DBG_STORAGE, "Starting new expiration thread");
storage_mgr->expiration_thread = zeek::jthread([t]() { storage_mgr->Expire(t); }); storage_mgr->expiration_thread = zeek::jthread([t]() { storage_mgr->Expire(t); });
} }
@ -146,4 +146,11 @@ void Manager::RegisterBackend(BackendPtr backend) {
DBG_LOG(DBG_STORAGE, "Registered backends: %zu", backends.size()); DBG_LOG(DBG_STORAGE, "Registered backends: %zu", backends.size());
} }
size_t Manager::BackendCount() {
// Expiration runs on a separate thread and loops over the vector of backends. The mutex
// here ensures exclusive access.
std::unique_lock<std::mutex> lk(backends_mtx);
return backends.size();
}
} // namespace zeek::storage } // namespace zeek::storage

View file

@ -113,6 +113,7 @@ protected:
friend class storage::detail::ExpirationTimer; friend class storage::detail::ExpirationTimer;
void RunExpireThread(); void RunExpireThread();
void StartExpirationTimer(); void StartExpirationTimer();
size_t BackendCount();
zeek::jthread expiration_thread; zeek::jthread expiration_thread;
friend class storage::OpenResultCallback; friend class storage::OpenResultCallback;

View file

@ -4,7 +4,7 @@
# @TEST-PORT: REDIS_PORT # @TEST-PORT: REDIS_PORT
# @TEST-EXEC: btest-bg-run redis-server run-redis-server ${REDIS_PORT%/tcp} # @TEST-EXEC: btest-bg-run redis-server run-redis-server ${REDIS_PORT%/tcp}
# @TEST-EXEC: zcat <$TRACES/echo-connections.pcap.gz | zeek -B storage -b -r - %INPUT > out # @TEST-EXEC: zeek -B storage -b %INPUT > out
# @TEST-EXEC: btest-bg-wait -k 1 # @TEST-EXEC: btest-bg-wait -k 1
# @TEST-EXEC: btest-diff out # @TEST-EXEC: btest-diff out