zeek/scripts/base/frameworks/cluster/supervisor.zeek
Benjamin Bannier d5fd29edcd Prefer explicit construction to coercion in record initialization
While we support initializing records via coercion from an expression
list, e.g.,

    local x: X = [$x1=1, $x2=2];

this can sometimes obscure the code to readers, e.g., when assigning to
value declared and typed elsewhere. The language runtime has a similar
overhead since instead of just constructing a known type it needs to
check at runtime that the coercion from the expression list is valid;
this can be slower than just writing the readible code in the first
place, see #4559.

With this patch we use explicit construction, e.g.,

    local x = X($x1=1, $x2=2);
2025-07-11 16:28:37 -07:00

55 lines
1.4 KiB
Text

##! Cluster-related functionality specific to running under the Supervisor
##! framework.
@load base/frameworks/supervisor/api
module Cluster::Supervisor;
export {
## Populates the current node's :zeek:id:`Cluster::nodes` table from the
## supervisor's node configuration in :zeek:id:`Supervisor::NodeConfig`.
##
## Returns: true if initialization completed, false otherwise.
global __init_cluster_nodes: function(): bool;
}
function __init_cluster_nodes(): bool
{
local config = Supervisor::node();
if ( |config$cluster| == 0 )
return F;
local rolemap: table[Supervisor::ClusterRole] of Cluster::NodeType = {
[Supervisor::LOGGER] = Cluster::LOGGER,
[Supervisor::MANAGER] = Cluster::MANAGER,
[Supervisor::PROXY] = Cluster::PROXY,
[Supervisor::WORKER] = Cluster::WORKER,
};
local manager_name = "";
local cnode: Cluster::Node;
local typ: Cluster::NodeType = Cluster::NONE;
for ( node_name, endp in config$cluster )
{
if ( endp$role == Supervisor::MANAGER )
manager_name = node_name;
}
for ( node_name, endp in config$cluster )
{
if ( endp$role in rolemap )
typ = rolemap[endp$role];
cnode = Cluster::Node($node_type=typ, $ip=endp$host, $p=endp$p);
if ( |manager_name| > 0 && cnode$node_type != Cluster::MANAGER )
cnode$manager = manager_name;
if ( endp?$metrics_port )
cnode$metrics_port = endp$metrics_port;
Cluster::nodes[node_name] = cnode;
}
return T;
}