Make storage events take a tag for the backend instead of a string

This commit is contained in:
Tim Wojtulewicz 2025-03-24 16:16:26 -07:00
parent 989e4adf90
commit 32ae8f4eaa
15 changed files with 39 additions and 38 deletions

View file

@ -2,8 +2,10 @@
#include "zeek/storage/Backend.h" #include "zeek/storage/Backend.h"
#include "zeek/Desc.h"
#include "zeek/Trigger.h" #include "zeek/Trigger.h"
#include "zeek/broker/Data.h" #include "zeek/broker/Data.h"
#include "zeek/storage/Manager.h"
#include "zeek/storage/ReturnCode.h" #include "zeek/storage/ReturnCode.h"
#include "zeek/storage/storage-events.bif.h" #include "zeek/storage/storage-events.bif.h"
@ -63,6 +65,11 @@ void OpenResultCallback::Complete(OperationResult res) {
ResultCallback::Complete(std::move(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) { OperationResult Backend::Open(OpenResultCallback* cb, RecordValPtr options, TypePtr kt, TypePtr vt) {
key_type = std::move(kt); key_type = std::move(kt);
val_type = std::move(vt); val_type = std::move(vt);
@ -129,13 +136,10 @@ void Backend::CompleteCallback(ResultCallback* cb, const OperationResult& data)
} }
} }
void Backend::EnqueueBackendOpened() { void Backend::EnqueueBackendOpened() { event_mgr.Enqueue(Storage::backend_opened, tag.AsVal(), backend_options); }
event_mgr.Enqueue(Storage::backend_opened, make_intrusive<StringVal>(Tag()), backend_options);
}
void Backend::EnqueueBackendLost(std::string_view reason) { void Backend::EnqueueBackendLost(std::string_view reason) {
event_mgr.Enqueue(Storage::backend_lost, make_intrusive<StringVal>(Tag()), backend_options, event_mgr.Enqueue(Storage::backend_lost, tag.AsVal(), backend_options, make_intrusive<StringVal>(reason));
make_intrusive<StringVal>(reason));
} }
zeek::OpaqueTypePtr detail::backend_opaque; zeek::OpaqueTypePtr detail::backend_opaque;

View file

@ -3,6 +3,7 @@
#pragma once #pragma once
#include "zeek/OpaqueVal.h" #include "zeek/OpaqueVal.h"
#include "zeek/Tag.h"
#include "zeek/Val.h" #include "zeek/Val.h"
namespace zeek::detail::trigger { namespace zeek::detail::trigger {
@ -99,7 +100,7 @@ public:
/** /**
* Returns a descriptive tag representing the source for debugging. * 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. * Store a new key/value pair in the backend.
@ -172,10 +173,11 @@ protected:
* *
* @param modes A combination of values from SupportedModes. These modes * @param modes A combination of values from SupportedModes. These modes
# define whether a backend only supports sync or async or both. # define whether a backend only supports sync or async or both.
* @param tag A string representation of the tag for this backend. This * @param tag The name of the plugin that this backend is part of. It
* is passed from the Manager through the component factory. * 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. * Called by the manager system to open the backend.
@ -235,7 +237,8 @@ protected:
TypePtr val_type; TypePtr val_type;
RecordValPtr backend_options; RecordValPtr backend_options;
std::string tag; zeek::Tag tag;
std::string tag_str;
private: private:
/** /**

View file

@ -13,7 +13,7 @@ class Backend;
*/ */
class Component : public plugin::Component { class Component : public plugin::Component {
public: public:
using factory_callback = IntrusivePtr<Backend> (*)(std::string_view); using factory_callback = IntrusivePtr<Backend> (*)();
/** /**
* Constructor. * Constructor.

View file

@ -4,7 +4,6 @@
#include <atomic> #include <atomic>
#include "zeek/Desc.h"
#include "zeek/RunState.h" #include "zeek/RunState.h"
#include "zeek/storage/ReturnCode.h" #include "zeek/storage/ReturnCode.h"
@ -61,10 +60,7 @@ zeek::expected<BackendPtr, std::string> Manager::Instantiate(const Tag& type) {
util::fmt("Factory invalid for backend %s", GetComponentName(type).c_str())); util::fmt("Factory invalid for backend %s", GetComponentName(type).c_str()));
} }
ODesc d; BackendPtr bp = c->Factory()();
type.AsVal()->Describe(&d);
BackendPtr bp = c->Factory()(d.Description());
if ( ! bp ) { if ( ! bp ) {
return zeek::unexpected<std::string>( return zeek::unexpected<std::string>(

View file

@ -137,7 +137,7 @@ std::unique_lock<std::mutex> conditionally_lock(bool condition, std::mutex& mute
namespace zeek::storage::backend::redis { namespace zeek::storage::backend::redis {
storage::BackendPtr Redis::Instantiate(std::string_view tag) { return make_intrusive<Redis>(tag); } storage::BackendPtr Redis::Instantiate() { return make_intrusive<Redis>(); }
/** /**
* Called by the manager system to open the backend. * Called by the manager system to open the backend.

View file

@ -15,10 +15,10 @@ struct redisPollEvents;
namespace zeek::storage::backend::redis { namespace zeek::storage::backend::redis {
class Redis : public Backend, public iosource::IOSource { class Redis : public Backend, public iosource::IOSource {
public: public:
Redis(std::string_view tag) : Backend(SupportedModes::ASYNC, tag), IOSource(true) {} Redis() : Backend(SupportedModes::ASYNC, "REDIS"), IOSource(true) {}
~Redis() override = default; ~Redis() override = default;
static BackendPtr Instantiate(std::string_view tag); static BackendPtr Instantiate();
/** /**
* Returns a descriptive tag representing the source for debugging. * Returns a descriptive tag representing the source for debugging.
@ -26,7 +26,7 @@ public:
* *
* @return The debugging name. * @return The debugging name.
*/ */
const char* Tag() override { return tag.c_str(); } const char* Tag() override { return tag_str.c_str(); }
// IOSource interface // IOSource interface
double GetNextTimeout() override { return -1; } double GetNextTimeout() override { return -1; }

View file

@ -9,7 +9,7 @@
namespace zeek::storage::backend::sqlite { namespace zeek::storage::backend::sqlite {
storage::BackendPtr SQLite::Instantiate(std::string_view tag) { return make_intrusive<SQLite>(tag); } storage::BackendPtr SQLite::Instantiate() { return make_intrusive<SQLite>(); }
/** /**
* Called by the manager system to open the backend. * Called by the manager system to open the backend.

View file

@ -12,10 +12,10 @@ namespace zeek::storage::backend::sqlite {
class SQLite : public Backend { class SQLite : public Backend {
public: public:
SQLite(std::string_view tag) : Backend(SupportedModes::SYNC, tag) {} SQLite() : Backend(SupportedModes::SYNC, "SQLITE") {}
~SQLite() override = default; ~SQLite() override = default;
static BackendPtr Instantiate(std::string_view tag); static BackendPtr Instantiate();
/** /**
* Returns whether the backend is opened. * Returns whether the backend is opened.

View file

@ -4,22 +4,20 @@ module Storage;
## Generated automatically when a new backend connection is opened successfully. ## Generated automatically when a new backend connection is opened successfully.
## ##
## tag: A string describing the backend that enqueued this event. This is typically ## tag: A tag for one of the storage backends.
## generated by the ``Tag()`` method in the backend plugin.
## ##
## options: A copy of the configuration options passed to ## options: A copy of the configuration options passed to
## :zeek:see:`Storage::Async::open_backend` or ## :zeek:see:`Storage::Async::open_backend` or
## :zeek:see:`Storage::Sync::open_backend` when the backend was initially opened. ## :zeek:see:`Storage::Sync::open_backend` when the backend was initially opened.
## ##
## .. zeek:see:: Storage::backend_lost ## .. 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 ## May be generated when a backend connection is lost, both normally and
## unexpectedly. This event depends on the backends implementing handling for ## unexpectedly. This event depends on the backends implementing handling for
## it, and is not generated automatically by the storage framework. ## it, and is not generated automatically by the storage framework.
## ##
## tag: A string describing the backend that enqueued this event. This is typically ## tag: A tag for one of the storage backends.
## generated by the ``Tag()`` method in the backend plugin.
## ##
## options: A copy of the configuration options passed to ## options: A copy of the configuration options passed to
## :zeek:see:`Storage::Async::open_backend` or ## :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. ## reason: A string describing why the connection was lost.
## ##
## .. zeek:see:: Storage::backend_opened ## .. 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%);

View file

@ -11,7 +11,7 @@ using namespace zeek::storage;
namespace btest::storage::backend { namespace btest::storage::backend {
BackendPtr StorageDummy::Instantiate(std::string_view tag) { return make_intrusive<StorageDummy>(tag); } BackendPtr StorageDummy::Instantiate() { return make_intrusive<StorageDummy>(); }
/** /**
* Called by the manager system to open the backend. * Called by the manager system to open the backend.

View file

@ -13,10 +13,10 @@ namespace btest::storage::backend {
*/ */
class StorageDummy : public zeek::storage::Backend { class StorageDummy : public zeek::storage::Backend {
public: public:
StorageDummy(std::string_view tag) : Backend(zeek::storage::SupportedModes::SYNC, tag) {} StorageDummy() : Backend(zeek::storage::SupportedModes::SYNC, "StorageDummy") {}
~StorageDummy() override = default; ~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. * Called by the manager system to open the backend.

View file

@ -14,11 +14,11 @@
redef exit_only_after_terminate = T; 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; 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; print "Storage::backend_lost", tag, config, reason;
terminate(); terminate();
} }

View file

@ -12,11 +12,11 @@
@load base/frameworks/storage/sync @load base/frameworks/storage/sync
@load policy/frameworks/storage/backend/redis @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; 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; print "Storage::backend_lost", tag, config, reason;
terminate(); terminate();
} }

View file

@ -8,7 +8,7 @@
redef exit_only_after_terminate = T; 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; print "Storage::backend_opened", tag, config;
} }

View file

@ -8,7 +8,7 @@
redef exit_only_after_terminate = T; 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; print "Storage::backend_opened", tag, config;
} }