SQLite: Fix some issues with expiration, including in the btest

This commit is contained in:
Tim Wojtulewicz 2024-12-20 15:20:12 -07:00
parent 6bc5f70236
commit 6289eb8e15
3 changed files with 19 additions and 9 deletions

View file

@ -155,6 +155,9 @@ ErrorResult SQLite::DoPut(ValPtr key, ValPtr value, bool overwrite, double expir
return res; return res;
} }
if ( expiration_time != 0 )
expiration_time += run_state::network_time;
if ( auto res = checkError(sqlite3_bind_double(stmt, 3, expiration_time)); res.has_value() ) { if ( auto res = checkError(sqlite3_bind_double(stmt, 3, expiration_time)); res.has_value() ) {
sqlite3_reset(stmt); sqlite3_reset(stmt);
return res; return res;
@ -244,7 +247,7 @@ ErrorResult SQLite::DoErase(ValPtr key, ErrorResultCallback* cb) {
void SQLite::Expire() { void SQLite::Expire() {
auto stmt = prepared_stmts["expire"]; auto stmt = prepared_stmts["expire"];
if ( auto res = checkError(sqlite3_bind_double(stmt, 1, util::current_time())); res.has_value() ) { if ( auto res = checkError(sqlite3_bind_double(stmt, 1, run_state::network_time)); res.has_value() ) {
sqlite3_reset(stmt); sqlite3_reset(stmt);
// TODO: do something with the error here? // TODO: do something with the error here?
} }

View file

@ -1,3 +1,3 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. ### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in /Users/tim/Desktop/projects/storage-framework/testing/btest/.tmp/scripts.base.frameworks.storage.expiration/expiration.zeek, line 20: Failed to retrieve data: Failed to find row for key: no more rows available (Storage::get(backend, to_any_coerce key, F)) 1627225025.686472 error in <...>/expiration.zeek, line 20: Failed to retrieve data: Failed to find row for key: no more rows available (Storage::get(backend, to_any_coerce key, F))
received termination signal 1627225025.686472 received termination signal

View file

@ -1,12 +1,12 @@
# @TEST-DOC: Automatic expiration of stored data # @TEST-DOC: Automatic expiration of stored data
# @TEST-EXEC: zcat <$TRACES/echo-connections.pcap.gz | zeek -b %INPUT > out # @TEST-EXEC: zcat <$TRACES/echo-connections.pcap.gz | zeek -b -Cr - %INPUT > out
# @TEST-EXEC: btest-diff out # @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff out
# @TEST-EXEC: btest-diff .stderr # @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff .stderr
@load base/frameworks/storage @load base/frameworks/storage
@load policy/frameworks/storage/backend/sqlite @load policy/frameworks/storage/backend/sqlite
redef Storage::expire_interval = 5 secs; redef Storage::expire_interval = 2 secs;
redef exit_only_after_terminate = T; redef exit_only_after_terminate = T;
# Create a typename here that can be passed down into get(). # Create a typename here that can be passed down into get().
@ -24,14 +24,14 @@ event check_removed() {
terminate(); terminate();
} }
event zeek_init() { event setup_test() {
local opts : Storage::Backend::SQLite::Options; local opts : Storage::Backend::SQLite::Options;
opts$database_path = "storage-test.sqlite"; opts$database_path = "storage-test.sqlite";
opts$table_name = "testing"; opts$table_name = "testing";
backend = Storage::open_backend(Storage::SQLITE, opts, str, str); backend = Storage::open_backend(Storage::SQLITE, opts, str, str);
local res = Storage::put(backend, [$key=key, $value=value, $overwrite=T, $expire_time=2 secs, $async_mode=F]); local res = Storage::put(backend, [$key=key, $value=value, $expire_time=2 secs, $async_mode=F]);
print "put result", res; print "put result", res;
local res2 = Storage::get(backend, key, F); local res2 = Storage::get(backend, key, F);
@ -40,3 +40,10 @@ event zeek_init() {
schedule 5 secs { check_removed() }; schedule 5 secs { check_removed() };
} }
event zeek_init() {
# We need network time to be set to something other than zero for the
# expiration time to be set correctly. Schedule an event on a short
# timer so packets start getting read and do the setup there.
schedule 100 msecs { setup_test() };
}