Merge remote-tracking branch 'origin/topic/timw/pass-tags-for-storage-events'

* origin/topic/timw/pass-tags-for-storage-events:
  Make storage events take a tag for the backend instead of a string
  Add move constructor to Tag class
This commit is contained in:
Tim Wojtulewicz 2025-03-27 16:12:44 -07:00
commit edbe8f0831
19 changed files with 58 additions and 39 deletions

View file

@ -1,3 +1,9 @@
7.2.0-dev.439 | 2025-03-27 16:12:44 -0700
* Make storage events take a tag for the backend instead of a string (Tim Wojtulewicz, Corelight)
* Add move constructor to Tag class (Tim Wojtulewicz, Corelight)
7.2.0-dev.436 | 2025-03-27 14:07:11 -0700
* Fix handling of timeout conditions from storage backends (Tim Wojtulewicz, Corelight)

View file

@ -1 +1 @@
7.2.0-dev.436
7.2.0-dev.439

View file

@ -37,6 +37,13 @@ Tag::Tag(const Tag& other) {
etype = other.etype;
}
Tag::Tag(Tag&& other) noexcept {
type = other.type;
subtype = other.subtype;
val = std::move(other.val);
etype = std::move(other.etype);
}
Tag::Tag() {
val = nullptr;
etype = nullptr;

View file

@ -100,6 +100,11 @@ public:
*/
Tag(const Tag& other);
/**
* Move constructor.
*/
Tag(Tag&& other) noexcept;
/**
* Destructor.
*/

View file

@ -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<StringVal>(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<StringVal>(Tag()), backend_options,
make_intrusive<StringVal>(reason));
event_mgr.Enqueue(Storage::backend_lost, tag.AsVal(), backend_options, make_intrusive<StringVal>(reason));
}
zeek::OpaqueTypePtr detail::backend_opaque;

View file

@ -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:
/**

View file

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

View file

@ -4,7 +4,6 @@
#include <atomic>
#include "zeek/Desc.h"
#include "zeek/RunState.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()));
}
ODesc d;
type.AsVal()->Describe(&d);
BackendPtr bp = c->Factory()(d.Description());
BackendPtr bp = c->Factory()();
if ( ! bp ) {
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 {
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.

View file

@ -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; }

View file

@ -9,7 +9,7 @@
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.

View file

@ -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.

View file

@ -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%);

View file

@ -11,7 +11,7 @@ using namespace zeek::storage;
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.

View file

@ -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.

View file

@ -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();
}

View file

@ -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();
}

View file

@ -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;
}

View file

@ -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;
}