From 32ae8f4eaa1dba967357bd66c664ab361f40bcd3 Mon Sep 17 00:00:00 2001 From: Tim Wojtulewicz Date: Mon, 24 Mar 2025 16:16:26 -0700 Subject: [PATCH] Make storage events take a tag for the backend instead of a string --- src/storage/Backend.cc | 14 +++++++++----- src/storage/Backend.h | 13 ++++++++----- src/storage/Component.h | 2 +- src/storage/Manager.cc | 6 +----- src/storage/backend/redis/Redis.cc | 2 +- src/storage/backend/redis/Redis.h | 6 +++--- src/storage/backend/sqlite/SQLite.cc | 2 +- src/storage/backend/sqlite/SQLite.h | 4 ++-- src/storage/storage-events.bif | 10 ++++------ .../plugins/storage-plugin/src/StorageDummy.cc | 2 +- .../plugins/storage-plugin/src/StorageDummy.h | 4 ++-- .../base/frameworks/storage/redis-disconnect.zeek | 4 ++-- .../base/frameworks/storage/redis-sync.zeek | 4 ++-- .../storage/sqlite-basic-sync-in-when.zeek | 2 +- .../base/frameworks/storage/sqlite-basic.zeek | 2 +- 15 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/storage/Backend.cc b/src/storage/Backend.cc index 3580ff4c72..e71b9e2603 100644 --- a/src/storage/Backend.cc +++ b/src/storage/Backend.cc @@ -2,8 +2,10 @@ #include "zeek/storage/Backend.h" +#include "zeek/Desc.h" #include "zeek/Trigger.h" #include "zeek/broker/Data.h" +#include "zeek/storage/Manager.h" #include "zeek/storage/ReturnCode.h" #include "zeek/storage/storage-events.bif.h" @@ -63,6 +65,11 @@ void OpenResultCallback::Complete(OperationResult res) { ResultCallback::Complete(std::move(res)); } +Backend::Backend(uint8_t modes, std::string_view tag_name) : modes(modes) { + tag = storage_mgr->GetComponentTag(std::string{tag_name}); + tag_str = zeek::obj_desc_short(tag.AsVal().get()); +} + OperationResult Backend::Open(OpenResultCallback* cb, RecordValPtr options, TypePtr kt, TypePtr vt) { key_type = std::move(kt); val_type = std::move(vt); @@ -129,13 +136,10 @@ void Backend::CompleteCallback(ResultCallback* cb, const OperationResult& data) } } -void Backend::EnqueueBackendOpened() { - event_mgr.Enqueue(Storage::backend_opened, make_intrusive(Tag()), backend_options); -} +void Backend::EnqueueBackendOpened() { event_mgr.Enqueue(Storage::backend_opened, tag.AsVal(), backend_options); } void Backend::EnqueueBackendLost(std::string_view reason) { - event_mgr.Enqueue(Storage::backend_lost, make_intrusive(Tag()), backend_options, - make_intrusive(reason)); + event_mgr.Enqueue(Storage::backend_lost, tag.AsVal(), backend_options, make_intrusive(reason)); } zeek::OpaqueTypePtr detail::backend_opaque; diff --git a/src/storage/Backend.h b/src/storage/Backend.h index 26ee98e96f..702fde362b 100644 --- a/src/storage/Backend.h +++ b/src/storage/Backend.h @@ -3,6 +3,7 @@ #pragma once #include "zeek/OpaqueVal.h" +#include "zeek/Tag.h" #include "zeek/Val.h" namespace zeek::detail::trigger { @@ -99,7 +100,7 @@ public: /** * Returns a descriptive tag representing the source for debugging. */ - const char* Tag() { return tag.c_str(); } + const char* Tag() { return tag_str.c_str(); } /** * Store a new key/value pair in the backend. @@ -172,10 +173,11 @@ protected: * * @param modes A combination of values from SupportedModes. These modes # define whether a backend only supports sync or async or both. - * @param tag A string representation of the tag for this backend. This - * is passed from the Manager through the component factory. + * @param tag The name of the plugin that this backend is part of. It + * should match the string sent in the ``Plugin`` code for the backend + * plugin. */ - Backend(uint8_t modes, std::string_view tag) : tag(tag), modes(modes) {} + Backend(uint8_t modes, std::string_view tag_name); /** * Called by the manager system to open the backend. @@ -235,7 +237,8 @@ protected: TypePtr val_type; RecordValPtr backend_options; - std::string tag; + zeek::Tag tag; + std::string tag_str; private: /** diff --git a/src/storage/Component.h b/src/storage/Component.h index de8c9899ce..6d7f5d1e6f 100644 --- a/src/storage/Component.h +++ b/src/storage/Component.h @@ -13,7 +13,7 @@ class Backend; */ class Component : public plugin::Component { public: - using factory_callback = IntrusivePtr (*)(std::string_view); + using factory_callback = IntrusivePtr (*)(); /** * Constructor. diff --git a/src/storage/Manager.cc b/src/storage/Manager.cc index 0b0cb601d3..3ca04d5530 100644 --- a/src/storage/Manager.cc +++ b/src/storage/Manager.cc @@ -4,7 +4,6 @@ #include -#include "zeek/Desc.h" #include "zeek/RunState.h" #include "zeek/storage/ReturnCode.h" @@ -61,10 +60,7 @@ zeek::expected Manager::Instantiate(const Tag& type) { util::fmt("Factory invalid for backend %s", GetComponentName(type).c_str())); } - ODesc d; - type.AsVal()->Describe(&d); - - BackendPtr bp = c->Factory()(d.Description()); + BackendPtr bp = c->Factory()(); if ( ! bp ) { return zeek::unexpected( diff --git a/src/storage/backend/redis/Redis.cc b/src/storage/backend/redis/Redis.cc index 95ba8eba23..47666a3c4d 100644 --- a/src/storage/backend/redis/Redis.cc +++ b/src/storage/backend/redis/Redis.cc @@ -137,7 +137,7 @@ std::unique_lock conditionally_lock(bool condition, std::mutex& mute namespace zeek::storage::backend::redis { -storage::BackendPtr Redis::Instantiate(std::string_view tag) { return make_intrusive(tag); } +storage::BackendPtr Redis::Instantiate() { return make_intrusive(); } /** * Called by the manager system to open the backend. diff --git a/src/storage/backend/redis/Redis.h b/src/storage/backend/redis/Redis.h index 64607c2f20..2654ef6c9c 100644 --- a/src/storage/backend/redis/Redis.h +++ b/src/storage/backend/redis/Redis.h @@ -15,10 +15,10 @@ struct redisPollEvents; namespace zeek::storage::backend::redis { class Redis : public Backend, public iosource::IOSource { public: - Redis(std::string_view tag) : Backend(SupportedModes::ASYNC, tag), IOSource(true) {} + Redis() : Backend(SupportedModes::ASYNC, "REDIS"), IOSource(true) {} ~Redis() override = default; - static BackendPtr Instantiate(std::string_view tag); + static BackendPtr Instantiate(); /** * Returns a descriptive tag representing the source for debugging. @@ -26,7 +26,7 @@ public: * * @return The debugging name. */ - const char* Tag() override { return tag.c_str(); } + const char* Tag() override { return tag_str.c_str(); } // IOSource interface double GetNextTimeout() override { return -1; } diff --git a/src/storage/backend/sqlite/SQLite.cc b/src/storage/backend/sqlite/SQLite.cc index 38e528e67a..4758ac071c 100644 --- a/src/storage/backend/sqlite/SQLite.cc +++ b/src/storage/backend/sqlite/SQLite.cc @@ -9,7 +9,7 @@ namespace zeek::storage::backend::sqlite { -storage::BackendPtr SQLite::Instantiate(std::string_view tag) { return make_intrusive(tag); } +storage::BackendPtr SQLite::Instantiate() { return make_intrusive(); } /** * Called by the manager system to open the backend. diff --git a/src/storage/backend/sqlite/SQLite.h b/src/storage/backend/sqlite/SQLite.h index 8fb276f422..5a35651fde 100644 --- a/src/storage/backend/sqlite/SQLite.h +++ b/src/storage/backend/sqlite/SQLite.h @@ -12,10 +12,10 @@ namespace zeek::storage::backend::sqlite { class SQLite : public Backend { public: - SQLite(std::string_view tag) : Backend(SupportedModes::SYNC, tag) {} + SQLite() : Backend(SupportedModes::SYNC, "SQLITE") {} ~SQLite() override = default; - static BackendPtr Instantiate(std::string_view tag); + static BackendPtr Instantiate(); /** * Returns whether the backend is opened. diff --git a/src/storage/storage-events.bif b/src/storage/storage-events.bif index b0aa5c4f72..ab3b7c1dfb 100644 --- a/src/storage/storage-events.bif +++ b/src/storage/storage-events.bif @@ -4,22 +4,20 @@ module Storage; ## Generated automatically when a new backend connection is opened successfully. ## -## tag: A string describing the backend that enqueued this event. This is typically -## generated by the ``Tag()`` method in the backend plugin. +## tag: A tag for one of the storage backends. ## ## options: A copy of the configuration options passed to ## :zeek:see:`Storage::Async::open_backend` or ## :zeek:see:`Storage::Sync::open_backend` when the backend was initially opened. ## ## .. zeek:see:: Storage::backend_lost -event Storage::backend_opened%(tag: string, options: any%); +event Storage::backend_opened%(tag: Storage::Backend, options: any%); ## May be generated when a backend connection is lost, both normally and ## unexpectedly. This event depends on the backends implementing handling for ## it, and is not generated automatically by the storage framework. ## -## tag: A string describing the backend that enqueued this event. This is typically -## generated by the ``Tag()`` method in the backend plugin. +## tag: A tag for one of the storage backends. ## ## options: A copy of the configuration options passed to ## :zeek:see:`Storage::Async::open_backend` or @@ -28,4 +26,4 @@ event Storage::backend_opened%(tag: string, options: any%); ## reason: A string describing why the connection was lost. ## ## .. zeek:see:: Storage::backend_opened -event Storage::backend_lost%(tag: string, options: any, reason: string%); +event Storage::backend_lost%(tag: Storage::Backend, options: any, reason: string%); diff --git a/testing/btest/plugins/storage-plugin/src/StorageDummy.cc b/testing/btest/plugins/storage-plugin/src/StorageDummy.cc index 63739e3a5b..f05f3ed403 100644 --- a/testing/btest/plugins/storage-plugin/src/StorageDummy.cc +++ b/testing/btest/plugins/storage-plugin/src/StorageDummy.cc @@ -11,7 +11,7 @@ using namespace zeek::storage; namespace btest::storage::backend { -BackendPtr StorageDummy::Instantiate(std::string_view tag) { return make_intrusive(tag); } +BackendPtr StorageDummy::Instantiate() { return make_intrusive(); } /** * Called by the manager system to open the backend. diff --git a/testing/btest/plugins/storage-plugin/src/StorageDummy.h b/testing/btest/plugins/storage-plugin/src/StorageDummy.h index a8ffca1176..0fa718fc4c 100644 --- a/testing/btest/plugins/storage-plugin/src/StorageDummy.h +++ b/testing/btest/plugins/storage-plugin/src/StorageDummy.h @@ -13,10 +13,10 @@ namespace btest::storage::backend { */ class StorageDummy : public zeek::storage::Backend { public: - StorageDummy(std::string_view tag) : Backend(zeek::storage::SupportedModes::SYNC, tag) {} + StorageDummy() : Backend(zeek::storage::SupportedModes::SYNC, "StorageDummy") {} ~StorageDummy() override = default; - static zeek::storage::BackendPtr Instantiate(std::string_view tag); + static zeek::storage::BackendPtr Instantiate(); /** * Called by the manager system to open the backend. diff --git a/testing/btest/scripts/base/frameworks/storage/redis-disconnect.zeek b/testing/btest/scripts/base/frameworks/storage/redis-disconnect.zeek index ade46312a9..33f66b1d43 100644 --- a/testing/btest/scripts/base/frameworks/storage/redis-disconnect.zeek +++ b/testing/btest/scripts/base/frameworks/storage/redis-disconnect.zeek @@ -14,11 +14,11 @@ redef exit_only_after_terminate = T; -event Storage::backend_opened(tag: string, config: any) { +event Storage::backend_opened(tag: Storage::Backend, config: any) { print "Storage::backend_opened", tag, config; } -event Storage::backend_lost(tag: string, config: any, reason: string) { +event Storage::backend_lost(tag: Storage::Backend, config: any, reason: string) { print "Storage::backend_lost", tag, config, reason; terminate(); } diff --git a/testing/btest/scripts/base/frameworks/storage/redis-sync.zeek b/testing/btest/scripts/base/frameworks/storage/redis-sync.zeek index 82e9bf41d0..ffaf42f4c8 100644 --- a/testing/btest/scripts/base/frameworks/storage/redis-sync.zeek +++ b/testing/btest/scripts/base/frameworks/storage/redis-sync.zeek @@ -12,11 +12,11 @@ @load base/frameworks/storage/sync @load policy/frameworks/storage/backend/redis -event Storage::backend_opened(tag: string, config: any) { +event Storage::backend_opened(tag: Storage::Backend, config: any) { print "Storage::backend_opened", tag, config; } -event Storage::backend_lost(tag: string, config: any, reason: string) { +event Storage::backend_lost(tag: Storage::Backend, config: any, reason: string) { print "Storage::backend_lost", tag, config, reason; terminate(); } diff --git a/testing/btest/scripts/base/frameworks/storage/sqlite-basic-sync-in-when.zeek b/testing/btest/scripts/base/frameworks/storage/sqlite-basic-sync-in-when.zeek index 389a07bc31..b127c23919 100644 --- a/testing/btest/scripts/base/frameworks/storage/sqlite-basic-sync-in-when.zeek +++ b/testing/btest/scripts/base/frameworks/storage/sqlite-basic-sync-in-when.zeek @@ -8,7 +8,7 @@ redef exit_only_after_terminate = T; -event Storage::backend_opened(tag: string, config: any) { +event Storage::backend_opened(tag: Storage::Backend, config: any) { print "Storage::backend_opened", tag, config; } diff --git a/testing/btest/scripts/base/frameworks/storage/sqlite-basic.zeek b/testing/btest/scripts/base/frameworks/storage/sqlite-basic.zeek index 1bdde525e2..c910353b0e 100644 --- a/testing/btest/scripts/base/frameworks/storage/sqlite-basic.zeek +++ b/testing/btest/scripts/base/frameworks/storage/sqlite-basic.zeek @@ -8,7 +8,7 @@ redef exit_only_after_terminate = T; -event Storage::backend_opened(tag: string, config: any) { +event Storage::backend_opened(tag: Storage::Backend, config: any) { print "Storage::backend_opened", tag, config; }