Add broadcasting system for the cluster #13124
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR adds a new broadcasting system for the master node of the Wazuh cluster, as requested at #12912.
To do this, an asynchronous queue is created for each AbstractServerHandler (there is one for each connected client), to which the functions to be executed are added. Thus, the functions will be executed as soon as the AbstractServerHandler is free, in the most resource-efficient way possible.
Broadcast methods
This broadcasting system adds three new methods to make use of it. They are defined inside the AbstractServer class. Therefore, they can be used there or in any inheriting class (such as
Master
orLocalServer
):broadcast_id
). When this function is used, the result of each server handler is stored and it can be queried with itsbroadcast_id
.broadcast_id
as a parameter. It returns False if the request has not yet been run in all expected server handlers. Otherwise, a dict with the response for each one is returned (or True if thebroadcast_id
is unknown).Usage example
An example of use could be this development of agent-groups (#10771). Until now, due to the lack of a broadcasting method, a function was called from which each MasterHandler obtained the info:
wazuh/framework/wazuh/core/cluster/master.py
Lines 1194 to 1206 in 813c85f
Now, it could just call
broadcast()
, specifying a reference to theMasterHandler.send_agent_groups_information
method and the info as a keyword:There is no way to verify if the function has been run in all MasterHandlers. If that is required,
broadcast_add()
andbroadcast_pop()
could be used instead.broadcast_pop
would return False until all handlers run the function:Tests results
As it can be seen below, the coverage for the
server.py
module is still 100% after this development: