diff --git a/scripts/base/frameworks/cluster/main.zeek b/scripts/base/frameworks/cluster/main.zeek index e3e45a0cbc..46b8eed3fa 100644 --- a/scripts/base/frameworks/cluster/main.zeek +++ b/scripts/base/frameworks/cluster/main.zeek @@ -295,6 +295,12 @@ export { ## Returns: a topic string that may used to send a message exclusively to ## a given cluster node. 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. @@ -519,3 +525,13 @@ function log(msg: string) { 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); + } diff --git a/src/broker/messaging.bif b/src/broker/messaging.bif index e647928190..971df738de 100644 --- a/src/broker/messaging.bif +++ b/src/broker/messaging.bif @@ -101,6 +101,12 @@ function Broker::make_event%(...%): Broker::Event 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. ## ## topic: a topic associated with the event message. @@ -122,6 +128,12 @@ function Broker::publish%(topic: string, ...%): bool 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 %{ auto rval = zeek::broker_mgr->FlushLogBuffers(); diff --git a/testing/btest/Baseline/scripts.base.frameworks.cluster.shims/manager-1..stdout b/testing/btest/Baseline/scripts.base.frameworks.cluster.shims/manager-1..stdout new file mode 100644 index 0000000000..bb92b93865 --- /dev/null +++ b/testing/btest/Baseline/scripts.base.frameworks.cluster.shims/manager-1..stdout @@ -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 diff --git a/testing/btest/Baseline/scripts.base.frameworks.cluster.shims/proxy-1..stdout b/testing/btest/Baseline/scripts.base.frameworks.cluster.shims/proxy-1..stdout new file mode 100644 index 0000000000..789d49772c --- /dev/null +++ b/testing/btest/Baseline/scripts.base.frameworks.cluster.shims/proxy-1..stdout @@ -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 diff --git a/testing/btest/Baseline/scripts.base.frameworks.cluster.shims/proxy-2..stdout b/testing/btest/Baseline/scripts.base.frameworks.cluster.shims/proxy-2..stdout new file mode 100644 index 0000000000..789d49772c --- /dev/null +++ b/testing/btest/Baseline/scripts.base.frameworks.cluster.shims/proxy-2..stdout @@ -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 diff --git a/testing/btest/Baseline/scripts.base.frameworks.cluster.shims/worker-1..stdout b/testing/btest/Baseline/scripts.base.frameworks.cluster.shims/worker-1..stdout new file mode 100644 index 0000000000..789d49772c --- /dev/null +++ b/testing/btest/Baseline/scripts.base.frameworks.cluster.shims/worker-1..stdout @@ -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 diff --git a/testing/btest/Baseline/scripts.base.frameworks.cluster.shims/worker-2..stdout b/testing/btest/Baseline/scripts.base.frameworks.cluster.shims/worker-2..stdout new file mode 100644 index 0000000000..789d49772c --- /dev/null +++ b/testing/btest/Baseline/scripts.base.frameworks.cluster.shims/worker-2..stdout @@ -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 diff --git a/testing/btest/scripts/base/frameworks/cluster/shims.zeek b/testing/btest/scripts/base/frameworks/cluster/shims.zeek new file mode 100644 index 0000000000..7a44ce8f13 --- /dev/null +++ b/testing/btest/scripts/base/frameworks/cluster/shims.zeek @@ -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(); + }