broker/cluster: Add cluster shims for 7.0

For making Zeek 7.0 compatible with the new Cluster::* methods coming
with Zeek 7.1/7.2.
This commit is contained in:
Arne Welzel 2025-06-06 16:37:43 +02:00
parent 4021a0c654
commit 8600cfa882
8 changed files with 143 additions and 0 deletions

View file

@ -295,6 +295,12 @@ export {
## Returns: a topic string that may used to send a message exclusively to ## Returns: a topic string that may used to send a message exclusively to
## a given cluster node. ## a given cluster node.
global nodeid_topic: function(id: string): string; global nodeid_topic: function(id: string): string;
## Delegate to Broker::__subscribe()
global subscribe: function(topic_prefix: string): bool;
## Delegate to Broker::__unsubscribe()
global unsubscribe: function(topic_prefix: string): bool;
} }
# Track active nodes per type. # Track active nodes per type.
@ -519,3 +525,13 @@ function log(msg: string)
{ {
Log::write(Cluster::LOG, [$ts = network_time(), $node = node, $message = msg]); Log::write(Cluster::LOG, [$ts = network_time(), $node = node, $message = msg]);
} }
function subscribe(topic_prefix: string): bool
{
return Broker::__subscribe(topic_prefix);
}
function unsubscribe(topic_prefix: string): bool
{
return Broker::__unsubscribe(topic_prefix);
}

View file

@ -101,6 +101,12 @@ function Broker::make_event%(...%): Broker::Event
return RecordValPtr{zeek::AdoptRef{}, zeek::broker_mgr->MakeEvent(&args, frame)}; return RecordValPtr{zeek::AdoptRef{}, zeek::broker_mgr->MakeEvent(&args, frame)};
%} %}
## Delegate to Broker::make_event()
function Cluster::make_event%(...%): Broker::Event
%{
return zeek::BifFunc::Broker::make_event_bif(frame, BiF_ARGS);
%}
## Publishes an event at a given topic. ## Publishes an event at a given topic.
## ##
## topic: a topic associated with the event message. ## topic: a topic associated with the event message.
@ -122,6 +128,12 @@ function Broker::publish%(topic: string, ...%): bool
return zeek::val_mgr->Bool(rval); return zeek::val_mgr->Bool(rval);
%} %}
## Delegate to Broker::make_event()
function Cluster::publish%(topic: string, ...%): bool
%{
return zeek::BifFunc::Broker::publish_bif(frame, BiF_ARGS);
%}
function Broker::__flush_logs%(%): count function Broker::__flush_logs%(%): count
%{ %{
auto rval = zeek::broker_mgr->FlushLogBuffers(); auto rval = zeek::broker_mgr->FlushLogBuffers();

View file

@ -0,0 +1,11 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
### NOTE: This file has been sorted with diff-sort.
node_down, 1
node_down, 2
node_down, 3
node_down, 4
ping, reply
ping, reply
ping, reply
ping, reply
started

View file

@ -0,0 +1,5 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
started
ping, hello
ping, end
terminate

View file

@ -0,0 +1,5 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
started
ping, hello
ping, end
terminate

View file

@ -0,0 +1,5 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
started
ping, hello
ping, end
terminate

View file

@ -0,0 +1,5 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
started
ping, hello
ping, end
terminate

View file

@ -0,0 +1,84 @@
# @TEST-PORT: BROKER_PORT1
# @TEST-PORT: BROKER_PORT2
# @TEST-PORT: BROKER_PORT3
# @TEST-PORT: BROKER_PORT4
# @TEST-PORT: BROKER_PORT5
#
# @TEST-EXEC: zeek --parse-only %INPUT
#
# @TEST-EXEC: btest-bg-run manager-1 ZEEKPATH=$ZEEKPATH:.. CLUSTER_NODE=manager-1 zeek -b %INPUT
# @TEST-EXEC: btest-bg-run proxy-1 ZEEKPATH=$ZEEKPATH:.. CLUSTER_NODE=proxy-1 zeek -b %INPUT
# @TEST-EXEC: btest-bg-run proxy-2 ZEEKPATH=$ZEEKPATH:.. CLUSTER_NODE=proxy-2 zeek -b %INPUT
# @TEST-EXEC: btest-bg-run worker-1 ZEEKPATH=$ZEEKPATH:.. CLUSTER_NODE=worker-1 zeek -b %INPUT
# @TEST-EXEC: btest-bg-run worker-2 ZEEKPATH=$ZEEKPATH:.. CLUSTER_NODE=worker-2 zeek -b %INPUT
# @TEST-EXEC: btest-bg-wait 5
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-sort btest-diff manager-1/.stdout
# @TEST-EXEC: btest-diff proxy-1/.stdout
# @TEST-EXEC: btest-diff proxy-2/.stdout
# @TEST-EXEC: btest-diff worker-1/.stdout
# @TEST-EXEC: btest-diff worker-2/.stdout
@load base/frameworks/cluster
@load policy/frameworks/cluster/experimental
@TEST-START-FILE cluster-layout.zeek
redef Cluster::nodes = {
["manager-1"] = [$node_type=Cluster::MANAGER, $ip=127.0.0.1, $p=to_port(getenv("BROKER_PORT1"))],
["proxy-1"] = [$node_type=Cluster::PROXY, $ip=127.0.0.1, $p=to_port(getenv("BROKER_PORT2")), $manager="manager-1"],
["proxy-2"] = [$node_type=Cluster::PROXY, $ip=127.0.0.1, $p=to_port(getenv("BROKER_PORT3")), $manager="manager-1"],
["worker-1"] = [$node_type=Cluster::WORKER, $ip=127.0.0.1, $p=to_port(getenv("BROKER_PORT4")), $manager="manager-1"],
["worker-2"] = [$node_type=Cluster::WORKER, $ip=127.0.0.1, $p=to_port(getenv("BROKER_PORT5")), $manager="manager-1"],
};
@TEST-END-FILE
global got_pings = 0;
event zeek_init() {
if ( Cluster::local_node_type() == Cluster::MANAGER )
Cluster::subscribe("/manager/ping");
else
Cluster::subscribe("/other/ping");
}
event ping(msg: string) {
print "ping", msg;
++got_pings;
if ( Cluster::local_node_type() == Cluster::MANAGER )
{
if ( got_pings == 4 )
Cluster::publish("/other/ping", ping, "end");
}
else
{
if ( msg != "end")
Cluster::publish("/manager/ping", ping, "reply");
else
{
print "terminate";
terminate();
}
}
}
event Cluster::Experimental::cluster_started()
{
print "started";
if ( Cluster::local_node_type() == Cluster::MANAGER )
Cluster::publish("/other/ping", ping, "hello");
}
global node_downs = 0;
event Cluster::node_down(name: string, id: string)
{
if ( Cluster::local_node_type() != Cluster::MANAGER )
return;
++node_downs;
print "node_down", node_downs;
if ( node_downs == 4 )
terminate();
}