Broker::publish: Warn on using Broker::publish() when inactive

This is mostly for transitioning base scripts to Cluster::publish() and
avoid silent surprises why certain things don't work when using ZeroMQ.
This commit is contained in:
Arne Welzel 2024-12-11 15:34:07 +01:00
parent f674fc2634
commit 0ad3210177
6 changed files with 77 additions and 1 deletions

View file

@ -109,7 +109,7 @@ public:
/** /**
* Returns true if any Broker communication is currently active. * Returns true if any Broker communication is currently active.
*/ */
[[deprecated("Remove with v8.1 - unused")]] bool Active(); bool Active();
/** /**
* Advances time. Broker data store expiration is driven by this * Advances time. Broker data store expiration is driven by this

View file

@ -57,8 +57,14 @@ static bool publish_event_args(ArgsSpan args, const zeek::String* topic,
zeek::detail::Frame* frame) zeek::detail::Frame* frame)
{ {
zeek::Broker::Manager::ScriptScopeGuard ssg; zeek::Broker::Manager::ScriptScopeGuard ssg;
zeek::ScriptLocationScope scope{frame};
auto rval = false; auto rval = false;
if ( zeek::broker_mgr != zeek::cluster::backend && ! zeek::broker_mgr->Active() )
zeek::reporter->Warning("Non-broker cluster backend configured and Broker manager inactive. "
"Did you mean to use Cluster::publish() instead of Broker::publish()?");
if ( args[0]->GetType()->Tag() == zeek::TYPE_RECORD ) if ( args[0]->GetType()->Tag() == zeek::TYPE_RECORD )
rval = zeek::broker_mgr->PublishEvent(topic->CheckString(), rval = zeek::broker_mgr->PublishEvent(topic->CheckString(),
args[0]->AsRecordVal()); args[0]->AsRecordVal());

View file

@ -0,0 +1,4 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
warning in ../manager.zeek, line 8: Non-broker cluster backend configured and Broker manager inactive. Did you mean to use Cluster::publish() instead of Broker::publish()?
warning in ../manager.zeek, line 16: Non-broker cluster backend configured and Broker manager inactive. Did you mean to use Cluster::publish() instead of Broker::publish()?
received termination signal

View file

@ -0,0 +1,3 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
node_up, worker-1
node_down, worker-1

View file

@ -0,0 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
node_up, manager

View file

@ -0,0 +1,61 @@
# @TEST-DOC: When using ZeroMQ, Broker::publish() produces a warning.
#
# @TEST-REQUIRES: have-zeromq
#
# @TEST-GROUP: cluster-zeromq
#
# @TEST-PORT: XPUB_PORT
# @TEST-PORT: XSUB_PORT
# @TEST-PORT: LOG_PULL_PORT
#
# @TEST-EXEC: cp $FILES/zeromq/cluster-layout-simple.zeek cluster-layout.zeek
# @TEST-EXEC: cp $FILES/zeromq/test-bootstrap.zeek zeromq-test-bootstrap.zeek
#
# @TEST-EXEC: btest-bg-run manager "ZEEKPATH=$ZEEKPATH:.. && CLUSTER_NODE=manager zeek -b ../manager.zeek >out"
# @TEST-EXEC: btest-bg-run worker "ZEEKPATH=$ZEEKPATH:.. && CLUSTER_NODE=worker-1 zeek -b ../worker.zeek >out"
#
# @TEST-EXEC: btest-bg-wait 30
# @TEST-EXEC: btest-diff ./manager/out
# @TEST-EXEC: btest-diff ./manager/.stderr
# @TEST-EXEC: btest-diff ./worker/out
# @TEST-START-FILE common.zeek
@load ./zeromq-test-bootstrap
global finish: event(name: string);
# @TEST-END-FILE
# @TEST-START-FILE manager.zeek
@load ./common.zeek
# If a node comes up that isn't us, send it a finish event.
event Cluster::node_up(name: string, id: string) {
print "node_up", name;
Cluster::publish(Cluster::nodeid_topic(id), finish, Cluster::node);
# Also via broker, but this produces a warning which we test for.
Broker::publish(Cluster::nodeid_topic(id), finish, Cluster::node);
}
# If the worker vanishes, finish the test.
event Cluster::node_down(name: string, id: string) {
print "node_down", name;
# Do another Broker::publish(), just for the kicks.
Broker::publish(Cluster::nodeid_topic(id), finish, Cluster::node);
terminate();
}
# @TEST-END-FILE
# @TEST-START-FILE worker.zeek
@load ./common.zeek
event Cluster::node_up(name: string, id: string) {
print "node_up", name;
}
event finish(name: string) &is_used {
terminate();
}
# @TEST-END-FILE