Pass network time down to Expire()

This commit is contained in:
Tim Wojtulewicz 2025-03-09 21:04:21 -07:00
parent c7503654e8
commit ebefb21c53
6 changed files with 14 additions and 11 deletions

View file

@ -160,8 +160,11 @@ protected:
/** /**
* Removes any entries in the backend that have expired. Can be overridden by * Removes any entries in the backend that have expired. Can be overridden by
* derived classes. * derived classes.
*
* @param current_network_time The network time as of the start of the
* expiration operation.
*/ */
void Expire() { DoExpire(); } void Expire(double current_network_time) { DoExpire(current_network_time); }
/** /**
* Enqueues the Storage::backend_opened event. This is called automatically * Enqueues the Storage::backend_opened event. This is called automatically
@ -192,7 +195,7 @@ private:
virtual OperationResult DoGet(ValPtr key, OperationResultCallback* cb = nullptr) = 0; virtual OperationResult DoGet(ValPtr key, OperationResultCallback* cb = nullptr) = 0;
virtual OperationResult DoErase(ValPtr key, OperationResultCallback* cb = nullptr) = 0; virtual OperationResult DoErase(ValPtr key, OperationResultCallback* cb = nullptr) = 0;
virtual void DoPoll() {} virtual void DoPoll() {}
virtual void DoExpire() {} virtual void DoExpire(double current_network_time) {}
uint8_t modes; uint8_t modes;
}; };

View file

@ -113,9 +113,10 @@ void Manager::Expire() {
DBG_LOG(DBG_STORAGE, "Expiration running, have %zu backends to check", backends.size()); DBG_LOG(DBG_STORAGE, "Expiration running, have %zu backends to check", backends.size());
double current_network_time = run_state::network_time;
for ( auto it = backends.begin(); it != backends.end() && ! run_state::terminating; ++it ) { for ( auto it = backends.begin(); it != backends.end() && ! run_state::terminating; ++it ) {
if ( (*it)->IsOpen() ) if ( (*it)->IsOpen() )
(*it)->Expire(); (*it)->Expire(current_network_time);
} }
expire_running.clear(); expire_running.clear();

View file

@ -351,7 +351,7 @@ OperationResult Redis::DoErase(ValPtr key, OperationResultCallback* cb) {
return {ReturnCode::IN_PROGRESS}; return {ReturnCode::IN_PROGRESS};
} }
void Redis::DoExpire() { void Redis::DoExpire(double current_network_time) {
// Expiration is handled natively by Redis if not reading traces. // Expiration is handled natively by Redis if not reading traces.
if ( ! connected || ! zeek::run_state::reading_traces ) if ( ! connected || ! zeek::run_state::reading_traces )
return; return;
@ -361,7 +361,7 @@ void Redis::DoExpire() {
during_expire = true; during_expire = true;
int status = redisAsyncCommand(async_ctx, redisGeneric, NULL, "ZRANGEBYSCORE %s_expire -inf %f", key_prefix.data(), int status = redisAsyncCommand(async_ctx, redisGeneric, NULL, "ZRANGEBYSCORE %s_expire -inf %f", key_prefix.data(),
run_state::network_time); current_network_time);
if ( status == REDIS_ERR ) { if ( status == REDIS_ERR ) {
// TODO: do something with the error? // TODO: do something with the error?
@ -407,7 +407,7 @@ void Redis::DoExpire() {
// Remove all of the elements from the range-set that match the time range. // Remove all of the elements from the range-set that match the time range.
redisAsyncCommand(async_ctx, redisGeneric, NULL, "ZREMRANGEBYSCORE %s_expire -inf %f", key_prefix.data(), redisAsyncCommand(async_ctx, redisGeneric, NULL, "ZREMRANGEBYSCORE %s_expire -inf %f", key_prefix.data(),
run_state::network_time); current_network_time);
++active_ops; ++active_ops;
Poll(); Poll();

View file

@ -54,7 +54,7 @@ private:
OperationResultCallback* cb = nullptr) override; OperationResultCallback* cb = nullptr) override;
OperationResult DoGet(ValPtr key, OperationResultCallback* cb = nullptr) override; OperationResult DoGet(ValPtr key, OperationResultCallback* cb = nullptr) override;
OperationResult DoErase(ValPtr key, OperationResultCallback* cb = nullptr) override; OperationResult DoErase(ValPtr key, OperationResultCallback* cb = nullptr) override;
void DoExpire() override; void DoExpire(double current_network_time) override;
void DoPoll() override; void DoPoll() override;
OperationResult ParseGetReply(redisReply* reply) const; OperationResult ParseGetReply(redisReply* reply) const;

View file

@ -234,11 +234,10 @@ OperationResult SQLite::DoErase(ValPtr key, OperationResultCallback* cb) {
* Removes any entries in the backend that have expired. Can be overridden by * Removes any entries in the backend that have expired. Can be overridden by
* derived classes. * derived classes.
*/ */
void SQLite::DoExpire() { void SQLite::DoExpire(double current_network_time) {
auto stmt = expire_stmt.get(); auto stmt = expire_stmt.get();
if ( auto res = CheckError(sqlite3_bind_double(stmt, 1, run_state::network_time)); if ( auto res = CheckError(sqlite3_bind_double(stmt, 1, current_network_time)); res.code != ReturnCode::SUCCESS ) {
res.code != ReturnCode::SUCCESS ) {
sqlite3_reset(stmt); sqlite3_reset(stmt);
// TODO: do something with the error here? // TODO: do something with the error here?
} }

View file

@ -29,7 +29,7 @@ private:
OperationResultCallback* cb = nullptr) override; OperationResultCallback* cb = nullptr) override;
OperationResult DoGet(ValPtr key, OperationResultCallback* cb = nullptr) override; OperationResult DoGet(ValPtr key, OperationResultCallback* cb = nullptr) override;
OperationResult DoErase(ValPtr key, OperationResultCallback* cb = nullptr) override; OperationResult DoErase(ValPtr key, OperationResultCallback* cb = nullptr) override;
void DoExpire() override; void DoExpire(double current_network_time) override;
/** /**
* Checks whether a status code returned by an sqlite call is a success. * Checks whether a status code returned by an sqlite call is a success.