Revise network communication backend and message-parsing logic.
Summary
This MR introduces some important backend changes to the communication and messaging APIs of DecLearn, resulting in more robust code (that is also easier to test and maintain), more efficient message parsing (that delays possibly-costly de-serialization to a time posterior to validity verification, both in the message-handling backend and application code) and the free extensibility of application messages, enabling to easily define and use custom message structures in downstream applications.
This MR follows the new version policy introduced for the upcoming v2.4
release: end-user code should remain workable, however some API details
have changed in a breaking manner (namely, network communication endpoints
now return SerializedMessage
instances rather than Message
ones; and
metadata sharing as part of the FL process is now optional, delayed and
more parsimonious).
API changes
-
Introduce a new
declearn.communication.api.backend
submodule:- Introduce a new
ActionMessage
minimal API under itsactions
submodule, that defines hard-coded, lightweight and easy-to-parse data structures designed to convey information and content across network communications agnostic to the content's nature. - Revise and expose the
MessagesHandler
util, that now builds on theActionMessage
API to model remote calls and answer them. - Move the
declearn.communication.messaging.flags
submodule todeclearn.communication.api.backend.flags
.
- Introduce a new
-
Introduce a new
declearn.messaging
submodule:- Revise the
Message
API to make it extendable, with automated type-registration of subclasses by default. - Introduce
SerializedMessage
as a wrapper for received messages, that parses the exactLessage
subtype (enabling logic tests and message filtering) but delays actual content de-serialization andMessage
object recovery (enabling to prevent undue resources use for unwanted messages that end up being discarded). - Move most existing
Message
subclasses to the new submodule, for retro-compatibility purposes. In DecLearn 3.0 these will probably be re-dispatched to make it clear that concrete messages only make sense in the context of specific multi-agent processes. - Drop backend-oriented
Message
subclasses that are replaced with the newActionMessage
backbone structures. - Deprecate the
declearn.communication.messaging
submodule, that is temporarily maintained, re-exporting moved contents as well as deprecated message types (which are bound to be rejected if sent).
- Revise the
-
Revise
NetworkClient
andNetworkServer
:- Have message-receiving methods return
SerializedMessage
instances rather than finalized de-serializedMessage
ones. - Quit sending and expecting 'data_info' with registration requests.
- Rename
NetworkClient.check_message
intorecv_message
(keep the former as an alias, with aDeprecationWarning
). - Improve the use of (optional) timeouts when sending or expecting
messages and overall exceptions handling:
-
NetworkClient.recv_message
may either raise aTimeoutError
(in case of timeout) orRuntimeError
(in case of rejection). -
NeworkServer.send_messages
andbroadcast_message
quietly stop waiting for clients to collect messages after the (opt.) timeout delay has passed. Messages may still be collected. -
NetworkServer.wait_for_messages
no longer accepts a timeout. -
NetworkServer.wait_for_messages_with_timeout
implements the possibility to setup a timeout. It returns both received client replies and a list of clients that failed to answer. - All timeouts can now be specified as float values (which is mostly useful for testing purposes or simulated environments).
-
- Add a
heartbeat
instantiation parameter, with a default value of 1 second, that is passed to the underlyingMessagesHandler
. In simulated contexts (including tests), setting a low heartbeat can cut runtime down significantly.
- Have message-receiving methods return
Notes
- Some
declearn.communication.api.backend.actions
structures were implemented merely for the sake of ensuring that requests received from older DecLearn clients would be detected as legacy and answered to accordingly (rejected with an instruction to update the version). - Thanks to the introduction of
heartbeat
, unit and functional tests can now run more quickly when communications are involved. - On the side, functional tests for the quickrun mode were fixed. They had been running without actually executing for some time.