flwr-nightly 1.8.0.dev20240315__py3-none-any.whl → 1.15.0.dev20250114__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- flwr/cli/app.py +16 -2
- flwr/cli/build.py +181 -0
- flwr/cli/cli_user_auth_interceptor.py +90 -0
- flwr/cli/config_utils.py +343 -0
- flwr/cli/example.py +4 -1
- flwr/cli/install.py +253 -0
- flwr/cli/log.py +182 -0
- flwr/{server/superlink/state → cli/login}/__init__.py +4 -10
- flwr/cli/login/login.py +88 -0
- flwr/cli/ls.py +327 -0
- flwr/cli/new/__init__.py +1 -0
- flwr/cli/new/new.py +210 -66
- flwr/cli/new/templates/app/.gitignore.tpl +163 -0
- flwr/cli/new/templates/app/LICENSE.tpl +202 -0
- flwr/cli/new/templates/app/README.baseline.md.tpl +127 -0
- flwr/cli/new/templates/app/README.flowertune.md.tpl +66 -0
- flwr/cli/new/templates/app/README.md.tpl +16 -32
- flwr/cli/new/templates/app/code/__init__.baseline.py.tpl +1 -0
- flwr/cli/new/templates/app/code/__init__.py.tpl +1 -1
- flwr/cli/new/templates/app/code/client.baseline.py.tpl +58 -0
- flwr/cli/new/templates/app/code/client.huggingface.py.tpl +55 -0
- flwr/cli/new/templates/app/code/client.jax.py.tpl +50 -0
- flwr/cli/new/templates/app/code/client.mlx.py.tpl +73 -0
- flwr/cli/new/templates/app/code/client.numpy.py.tpl +7 -7
- flwr/cli/new/templates/app/code/client.pytorch.py.tpl +30 -21
- flwr/cli/new/templates/app/code/client.sklearn.py.tpl +63 -0
- flwr/cli/new/templates/app/code/client.tensorflow.py.tpl +57 -1
- flwr/cli/new/templates/app/code/dataset.baseline.py.tpl +36 -0
- flwr/cli/new/templates/app/code/flwr_tune/__init__.py +15 -0
- flwr/cli/new/templates/app/code/flwr_tune/client_app.py.tpl +126 -0
- flwr/cli/new/templates/app/code/flwr_tune/dataset.py.tpl +87 -0
- flwr/cli/new/templates/app/code/flwr_tune/models.py.tpl +78 -0
- flwr/cli/new/templates/app/code/flwr_tune/server_app.py.tpl +94 -0
- flwr/cli/new/templates/app/code/flwr_tune/strategy.py.tpl +83 -0
- flwr/cli/new/templates/app/code/model.baseline.py.tpl +80 -0
- flwr/cli/new/templates/app/code/server.baseline.py.tpl +46 -0
- flwr/cli/new/templates/app/code/server.huggingface.py.tpl +38 -0
- flwr/cli/new/templates/app/code/server.jax.py.tpl +26 -0
- flwr/cli/new/templates/app/code/server.mlx.py.tpl +31 -0
- flwr/cli/new/templates/app/code/server.numpy.py.tpl +22 -9
- flwr/cli/new/templates/app/code/server.pytorch.py.tpl +21 -18
- flwr/cli/new/templates/app/code/server.sklearn.py.tpl +36 -0
- flwr/cli/new/templates/app/code/server.tensorflow.py.tpl +29 -1
- flwr/cli/new/templates/app/code/strategy.baseline.py.tpl +1 -0
- flwr/cli/new/templates/app/code/task.huggingface.py.tpl +102 -0
- flwr/cli/new/templates/app/code/task.jax.py.tpl +57 -0
- flwr/cli/new/templates/app/code/task.mlx.py.tpl +102 -0
- flwr/cli/new/templates/app/code/task.numpy.py.tpl +7 -0
- flwr/cli/new/templates/app/code/task.pytorch.py.tpl +29 -24
- flwr/cli/new/templates/app/code/task.sklearn.py.tpl +67 -0
- flwr/cli/new/templates/app/code/task.tensorflow.py.tpl +53 -0
- flwr/cli/new/templates/app/code/utils.baseline.py.tpl +1 -0
- flwr/cli/new/templates/app/pyproject.baseline.toml.tpl +138 -0
- flwr/cli/new/templates/app/pyproject.flowertune.toml.tpl +68 -0
- flwr/cli/new/templates/app/pyproject.huggingface.toml.tpl +46 -0
- flwr/cli/new/templates/app/pyproject.jax.toml.tpl +35 -0
- flwr/cli/new/templates/app/pyproject.mlx.toml.tpl +39 -0
- flwr/cli/new/templates/app/pyproject.numpy.toml.tpl +25 -12
- flwr/cli/new/templates/app/pyproject.pytorch.toml.tpl +29 -14
- flwr/cli/new/templates/app/pyproject.sklearn.toml.tpl +35 -0
- flwr/cli/new/templates/app/pyproject.tensorflow.toml.tpl +29 -14
- flwr/cli/run/__init__.py +1 -0
- flwr/cli/run/run.py +212 -34
- flwr/cli/stop.py +130 -0
- flwr/cli/utils.py +240 -5
- flwr/client/__init__.py +3 -2
- flwr/client/app.py +432 -255
- flwr/client/client.py +1 -11
- flwr/client/client_app.py +74 -13
- flwr/client/clientapp/__init__.py +22 -0
- flwr/client/clientapp/app.py +259 -0
- flwr/client/clientapp/clientappio_servicer.py +244 -0
- flwr/client/clientapp/utils.py +115 -0
- flwr/client/dpfedavg_numpy_client.py +7 -8
- flwr/client/grpc_adapter_client/__init__.py +15 -0
- flwr/client/grpc_adapter_client/connection.py +98 -0
- flwr/client/grpc_client/connection.py +21 -7
- flwr/client/grpc_rere_client/__init__.py +1 -1
- flwr/client/grpc_rere_client/client_interceptor.py +176 -0
- flwr/client/grpc_rere_client/connection.py +163 -56
- flwr/client/grpc_rere_client/grpc_adapter.py +167 -0
- flwr/client/heartbeat.py +74 -0
- flwr/client/message_handler/__init__.py +1 -1
- flwr/client/message_handler/message_handler.py +10 -11
- flwr/client/mod/__init__.py +5 -5
- flwr/client/mod/centraldp_mods.py +4 -2
- flwr/client/mod/comms_mods.py +5 -4
- flwr/client/mod/localdp_mod.py +10 -5
- flwr/client/mod/secure_aggregation/__init__.py +1 -1
- flwr/client/mod/secure_aggregation/secaggplus_mod.py +26 -26
- flwr/client/mod/utils.py +2 -4
- flwr/client/nodestate/__init__.py +26 -0
- flwr/client/nodestate/in_memory_nodestate.py +38 -0
- flwr/client/nodestate/nodestate.py +31 -0
- flwr/client/nodestate/nodestate_factory.py +38 -0
- flwr/client/numpy_client.py +8 -31
- flwr/client/rest_client/__init__.py +1 -1
- flwr/client/rest_client/connection.py +199 -176
- flwr/client/run_info_store.py +112 -0
- flwr/client/supernode/__init__.py +24 -0
- flwr/client/supernode/app.py +321 -0
- flwr/client/typing.py +1 -0
- flwr/common/__init__.py +17 -11
- flwr/common/address.py +47 -3
- flwr/common/args.py +153 -0
- flwr/common/auth_plugin/__init__.py +24 -0
- flwr/common/auth_plugin/auth_plugin.py +121 -0
- flwr/common/config.py +243 -0
- flwr/common/constant.py +132 -1
- flwr/common/context.py +32 -2
- flwr/common/date.py +22 -4
- flwr/common/differential_privacy.py +2 -2
- flwr/common/dp.py +2 -4
- flwr/common/exit_handlers.py +3 -3
- flwr/common/grpc.py +164 -5
- flwr/common/logger.py +230 -12
- flwr/common/message.py +191 -106
- flwr/common/object_ref.py +179 -44
- flwr/common/pyproject.py +1 -0
- flwr/common/record/__init__.py +2 -1
- flwr/common/record/configsrecord.py +58 -18
- flwr/common/record/metricsrecord.py +57 -17
- flwr/common/record/parametersrecord.py +88 -20
- flwr/common/record/recordset.py +153 -30
- flwr/common/record/typeddict.py +30 -55
- flwr/common/recordset_compat.py +31 -12
- flwr/common/retry_invoker.py +123 -30
- flwr/common/secure_aggregation/__init__.py +1 -1
- flwr/common/secure_aggregation/crypto/__init__.py +1 -1
- flwr/common/secure_aggregation/crypto/shamir.py +11 -11
- flwr/common/secure_aggregation/crypto/symmetric_encryption.py +68 -4
- flwr/common/secure_aggregation/ndarrays_arithmetic.py +17 -17
- flwr/common/secure_aggregation/quantization.py +8 -8
- flwr/common/secure_aggregation/secaggplus_constants.py +1 -1
- flwr/common/secure_aggregation/secaggplus_utils.py +10 -12
- flwr/common/serde.py +298 -19
- flwr/common/telemetry.py +65 -29
- flwr/common/typing.py +120 -19
- flwr/common/version.py +17 -3
- flwr/proto/clientappio_pb2.py +45 -0
- flwr/proto/clientappio_pb2.pyi +132 -0
- flwr/proto/clientappio_pb2_grpc.py +135 -0
- flwr/proto/clientappio_pb2_grpc.pyi +53 -0
- flwr/proto/exec_pb2.py +62 -0
- flwr/proto/exec_pb2.pyi +212 -0
- flwr/proto/exec_pb2_grpc.py +237 -0
- flwr/proto/exec_pb2_grpc.pyi +93 -0
- flwr/proto/fab_pb2.py +31 -0
- flwr/proto/fab_pb2.pyi +65 -0
- flwr/proto/fab_pb2_grpc.py +4 -0
- flwr/proto/fab_pb2_grpc.pyi +4 -0
- flwr/proto/fleet_pb2.py +42 -23
- flwr/proto/fleet_pb2.pyi +123 -1
- flwr/proto/fleet_pb2_grpc.py +170 -0
- flwr/proto/fleet_pb2_grpc.pyi +61 -0
- flwr/proto/grpcadapter_pb2.py +32 -0
- flwr/proto/grpcadapter_pb2.pyi +43 -0
- flwr/proto/grpcadapter_pb2_grpc.py +66 -0
- flwr/proto/grpcadapter_pb2_grpc.pyi +24 -0
- flwr/proto/log_pb2.py +29 -0
- flwr/proto/log_pb2.pyi +39 -0
- flwr/proto/log_pb2_grpc.py +4 -0
- flwr/proto/log_pb2_grpc.pyi +4 -0
- flwr/proto/message_pb2.py +41 -0
- flwr/proto/message_pb2.pyi +128 -0
- flwr/proto/message_pb2_grpc.py +4 -0
- flwr/proto/message_pb2_grpc.pyi +4 -0
- flwr/proto/node_pb2.py +1 -1
- flwr/proto/recordset_pb2.py +35 -33
- flwr/proto/recordset_pb2.pyi +40 -14
- flwr/proto/run_pb2.py +64 -0
- flwr/proto/run_pb2.pyi +268 -0
- flwr/proto/run_pb2_grpc.py +4 -0
- flwr/proto/run_pb2_grpc.pyi +4 -0
- flwr/proto/serverappio_pb2.py +52 -0
- flwr/proto/{driver_pb2.pyi → serverappio_pb2.pyi} +62 -20
- flwr/proto/serverappio_pb2_grpc.py +410 -0
- flwr/proto/serverappio_pb2_grpc.pyi +160 -0
- flwr/proto/simulationio_pb2.py +38 -0
- flwr/proto/simulationio_pb2.pyi +65 -0
- flwr/proto/simulationio_pb2_grpc.py +239 -0
- flwr/proto/simulationio_pb2_grpc.pyi +94 -0
- flwr/proto/task_pb2.py +7 -8
- flwr/proto/task_pb2.pyi +8 -5
- flwr/proto/transport_pb2.py +8 -8
- flwr/proto/transport_pb2.pyi +9 -6
- flwr/server/__init__.py +2 -10
- flwr/server/app.py +579 -402
- flwr/server/client_manager.py +8 -6
- flwr/server/compat/app.py +6 -62
- flwr/server/compat/app_utils.py +14 -8
- flwr/server/compat/driver_client_proxy.py +25 -58
- flwr/server/compat/legacy_context.py +5 -4
- flwr/server/driver/__init__.py +2 -0
- flwr/server/driver/driver.py +36 -131
- flwr/server/driver/grpc_driver.py +217 -81
- flwr/server/driver/inmemory_driver.py +182 -0
- flwr/server/history.py +28 -29
- flwr/server/run_serverapp.py +15 -126
- flwr/server/server.py +50 -44
- flwr/server/server_app.py +59 -10
- flwr/server/serverapp/__init__.py +22 -0
- flwr/server/serverapp/app.py +256 -0
- flwr/server/serverapp_components.py +52 -0
- flwr/server/strategy/__init__.py +2 -2
- flwr/server/strategy/aggregate.py +37 -23
- flwr/server/strategy/bulyan.py +9 -9
- flwr/server/strategy/dp_adaptive_clipping.py +25 -25
- flwr/server/strategy/dp_fixed_clipping.py +23 -22
- flwr/server/strategy/dpfedavg_adaptive.py +8 -8
- flwr/server/strategy/dpfedavg_fixed.py +13 -12
- flwr/server/strategy/fault_tolerant_fedavg.py +11 -11
- flwr/server/strategy/fedadagrad.py +9 -9
- flwr/server/strategy/fedadam.py +20 -10
- flwr/server/strategy/fedavg.py +16 -16
- flwr/server/strategy/fedavg_android.py +17 -17
- flwr/server/strategy/fedavgm.py +9 -9
- flwr/server/strategy/fedmedian.py +5 -5
- flwr/server/strategy/fedopt.py +6 -6
- flwr/server/strategy/fedprox.py +7 -7
- flwr/server/strategy/fedtrimmedavg.py +8 -8
- flwr/server/strategy/fedxgb_bagging.py +12 -12
- flwr/server/strategy/fedxgb_cyclic.py +10 -10
- flwr/server/strategy/fedxgb_nn_avg.py +6 -6
- flwr/server/strategy/fedyogi.py +9 -9
- flwr/server/strategy/krum.py +9 -9
- flwr/server/strategy/qfedavg.py +16 -16
- flwr/server/strategy/strategy.py +10 -10
- flwr/server/superlink/driver/__init__.py +2 -2
- flwr/server/superlink/driver/serverappio_grpc.py +61 -0
- flwr/server/superlink/driver/serverappio_servicer.py +363 -0
- flwr/server/superlink/ffs/__init__.py +24 -0
- flwr/server/superlink/ffs/disk_ffs.py +108 -0
- flwr/server/superlink/ffs/ffs.py +79 -0
- flwr/server/superlink/ffs/ffs_factory.py +47 -0
- flwr/server/superlink/fleet/__init__.py +1 -1
- flwr/server/superlink/fleet/grpc_adapter/__init__.py +15 -0
- flwr/server/superlink/fleet/grpc_adapter/grpc_adapter_servicer.py +162 -0
- flwr/server/superlink/fleet/grpc_bidi/__init__.py +1 -1
- flwr/server/superlink/fleet/grpc_bidi/flower_service_servicer.py +4 -2
- flwr/server/superlink/fleet/grpc_bidi/grpc_bridge.py +3 -2
- flwr/server/superlink/fleet/grpc_bidi/grpc_client_proxy.py +1 -1
- flwr/server/superlink/fleet/grpc_bidi/grpc_server.py +5 -154
- flwr/server/superlink/fleet/grpc_rere/__init__.py +1 -1
- flwr/server/superlink/fleet/grpc_rere/fleet_servicer.py +120 -13
- flwr/server/superlink/fleet/grpc_rere/server_interceptor.py +228 -0
- flwr/server/superlink/fleet/message_handler/__init__.py +1 -1
- flwr/server/superlink/fleet/message_handler/message_handler.py +153 -9
- flwr/server/superlink/fleet/rest_rere/__init__.py +1 -1
- flwr/server/superlink/fleet/rest_rere/rest_api.py +119 -81
- flwr/server/superlink/fleet/vce/__init__.py +1 -0
- flwr/server/superlink/fleet/vce/backend/__init__.py +4 -4
- flwr/server/superlink/fleet/vce/backend/backend.py +8 -9
- flwr/server/superlink/fleet/vce/backend/raybackend.py +87 -68
- flwr/server/superlink/fleet/vce/vce_api.py +208 -146
- flwr/server/superlink/linkstate/__init__.py +28 -0
- flwr/server/superlink/linkstate/in_memory_linkstate.py +581 -0
- flwr/server/superlink/linkstate/linkstate.py +389 -0
- flwr/server/superlink/{state/state_factory.py → linkstate/linkstate_factory.py} +19 -10
- flwr/server/superlink/linkstate/sqlite_linkstate.py +1236 -0
- flwr/server/superlink/linkstate/utils.py +389 -0
- flwr/server/superlink/simulation/__init__.py +15 -0
- flwr/server/superlink/simulation/simulationio_grpc.py +65 -0
- flwr/server/superlink/simulation/simulationio_servicer.py +186 -0
- flwr/server/superlink/utils.py +65 -0
- flwr/server/typing.py +2 -0
- flwr/server/utils/__init__.py +1 -1
- flwr/server/utils/tensorboard.py +5 -5
- flwr/server/utils/validator.py +31 -11
- flwr/server/workflow/default_workflows.py +70 -26
- flwr/server/workflow/secure_aggregation/secagg_workflow.py +1 -0
- flwr/server/workflow/secure_aggregation/secaggplus_workflow.py +40 -27
- flwr/simulation/__init__.py +12 -5
- flwr/simulation/app.py +247 -315
- flwr/simulation/legacy_app.py +402 -0
- flwr/simulation/ray_transport/__init__.py +1 -1
- flwr/simulation/ray_transport/ray_actor.py +42 -67
- flwr/simulation/ray_transport/ray_client_proxy.py +37 -17
- flwr/simulation/ray_transport/utils.py +1 -0
- flwr/simulation/run_simulation.py +306 -163
- flwr/simulation/simulationio_connection.py +89 -0
- flwr/superexec/__init__.py +15 -0
- flwr/superexec/app.py +59 -0
- flwr/superexec/deployment.py +188 -0
- flwr/superexec/exec_grpc.py +80 -0
- flwr/superexec/exec_servicer.py +231 -0
- flwr/superexec/exec_user_auth_interceptor.py +101 -0
- flwr/superexec/executor.py +96 -0
- flwr/superexec/simulation.py +124 -0
- {flwr_nightly-1.8.0.dev20240315.dist-info → flwr_nightly-1.15.0.dev20250114.dist-info}/METADATA +33 -26
- flwr_nightly-1.15.0.dev20250114.dist-info/RECORD +328 -0
- flwr_nightly-1.15.0.dev20250114.dist-info/entry_points.txt +12 -0
- flwr/cli/flower_toml.py +0 -140
- flwr/cli/new/templates/app/flower.toml.tpl +0 -13
- flwr/cli/new/templates/app/requirements.numpy.txt.tpl +0 -2
- flwr/cli/new/templates/app/requirements.pytorch.txt.tpl +0 -4
- flwr/cli/new/templates/app/requirements.tensorflow.txt.tpl +0 -4
- flwr/client/node_state.py +0 -48
- flwr/client/node_state_tests.py +0 -65
- flwr/proto/driver_pb2.py +0 -44
- flwr/proto/driver_pb2_grpc.py +0 -169
- flwr/proto/driver_pb2_grpc.pyi +0 -66
- flwr/server/superlink/driver/driver_grpc.py +0 -54
- flwr/server/superlink/driver/driver_servicer.py +0 -129
- flwr/server/superlink/state/in_memory_state.py +0 -230
- flwr/server/superlink/state/sqlite_state.py +0 -630
- flwr/server/superlink/state/state.py +0 -154
- flwr_nightly-1.8.0.dev20240315.dist-info/RECORD +0 -211
- flwr_nightly-1.8.0.dev20240315.dist-info/entry_points.txt +0 -9
- {flwr_nightly-1.8.0.dev20240315.dist-info → flwr_nightly-1.15.0.dev20250114.dist-info}/LICENSE +0 -0
- {flwr_nightly-1.8.0.dev20240315.dist-info → flwr_nightly-1.15.0.dev20250114.dist-info}/WHEEL +0 -0
flwr/common/message.py
CHANGED
@@ -14,14 +14,20 @@
|
|
14
14
|
# ==============================================================================
|
15
15
|
"""Message."""
|
16
16
|
|
17
|
+
|
17
18
|
from __future__ import annotations
|
18
19
|
|
19
|
-
|
20
|
+
import time
|
21
|
+
from logging import WARNING
|
22
|
+
from typing import Optional, cast
|
20
23
|
|
24
|
+
from .constant import MESSAGE_TTL_TOLERANCE
|
25
|
+
from .logger import log
|
21
26
|
from .record import RecordSet
|
22
27
|
|
28
|
+
DEFAULT_TTL = 3600
|
29
|
+
|
23
30
|
|
24
|
-
@dataclass
|
25
31
|
class Metadata: # pylint: disable=too-many-instance-attributes
|
26
32
|
"""A dataclass holding metadata associated with the current message.
|
27
33
|
|
@@ -40,28 +46,14 @@ class Metadata: # pylint: disable=too-many-instance-attributes
|
|
40
46
|
group_id : str
|
41
47
|
An identifier for grouping messages. In some settings,
|
42
48
|
this is used as the FL round.
|
43
|
-
ttl :
|
44
|
-
Time-to-live for this message.
|
49
|
+
ttl : float
|
50
|
+
Time-to-live for this message in seconds.
|
45
51
|
message_type : str
|
46
52
|
A string that encodes the action to be executed on
|
47
53
|
the receiving end.
|
48
|
-
partition_id : Optional[int]
|
49
|
-
An identifier that can be used when loading a particular
|
50
|
-
data partition for a ClientApp. Making use of this identifier
|
51
|
-
is more relevant when conducting simulations.
|
52
54
|
"""
|
53
55
|
|
54
|
-
|
55
|
-
_message_id: str
|
56
|
-
_src_node_id: int
|
57
|
-
_dst_node_id: int
|
58
|
-
_reply_to_message: str
|
59
|
-
_group_id: str
|
60
|
-
_ttl: str
|
61
|
-
_message_type: str
|
62
|
-
_partition_id: int | None
|
63
|
-
|
64
|
-
def __init__( # pylint: disable=too-many-arguments
|
56
|
+
def __init__( # pylint: disable=too-many-arguments,too-many-positional-arguments
|
65
57
|
self,
|
66
58
|
run_id: int,
|
67
59
|
message_id: str,
|
@@ -69,92 +61,103 @@ class Metadata: # pylint: disable=too-many-instance-attributes
|
|
69
61
|
dst_node_id: int,
|
70
62
|
reply_to_message: str,
|
71
63
|
group_id: str,
|
72
|
-
ttl:
|
64
|
+
ttl: float,
|
73
65
|
message_type: str,
|
74
|
-
partition_id: int | None = None,
|
75
66
|
) -> None:
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
67
|
+
var_dict = {
|
68
|
+
"_run_id": run_id,
|
69
|
+
"_message_id": message_id,
|
70
|
+
"_src_node_id": src_node_id,
|
71
|
+
"_dst_node_id": dst_node_id,
|
72
|
+
"_reply_to_message": reply_to_message,
|
73
|
+
"_group_id": group_id,
|
74
|
+
"_ttl": ttl,
|
75
|
+
"_message_type": message_type,
|
76
|
+
}
|
77
|
+
self.__dict__.update(var_dict)
|
85
78
|
|
86
79
|
@property
|
87
80
|
def run_id(self) -> int:
|
88
81
|
"""An identifier for the current run."""
|
89
|
-
return self._run_id
|
82
|
+
return cast(int, self.__dict__["_run_id"])
|
90
83
|
|
91
84
|
@property
|
92
85
|
def message_id(self) -> str:
|
93
86
|
"""An identifier for the current message."""
|
94
|
-
return self._message_id
|
87
|
+
return cast(str, self.__dict__["_message_id"])
|
95
88
|
|
96
89
|
@property
|
97
90
|
def src_node_id(self) -> int:
|
98
91
|
"""An identifier for the node sending this message."""
|
99
|
-
return self._src_node_id
|
92
|
+
return cast(int, self.__dict__["_src_node_id"])
|
100
93
|
|
101
94
|
@property
|
102
95
|
def reply_to_message(self) -> str:
|
103
96
|
"""An identifier for the message this message replies to."""
|
104
|
-
return self._reply_to_message
|
97
|
+
return cast(str, self.__dict__["_reply_to_message"])
|
105
98
|
|
106
99
|
@property
|
107
100
|
def dst_node_id(self) -> int:
|
108
101
|
"""An identifier for the node receiving this message."""
|
109
|
-
return self._dst_node_id
|
102
|
+
return cast(int, self.__dict__["_dst_node_id"])
|
110
103
|
|
111
104
|
@dst_node_id.setter
|
112
105
|
def dst_node_id(self, value: int) -> None:
|
113
106
|
"""Set dst_node_id."""
|
114
|
-
self._dst_node_id = value
|
107
|
+
self.__dict__["_dst_node_id"] = value
|
115
108
|
|
116
109
|
@property
|
117
110
|
def group_id(self) -> str:
|
118
111
|
"""An identifier for grouping messages."""
|
119
|
-
return self._group_id
|
112
|
+
return cast(str, self.__dict__["_group_id"])
|
120
113
|
|
121
114
|
@group_id.setter
|
122
115
|
def group_id(self, value: str) -> None:
|
123
116
|
"""Set group_id."""
|
124
|
-
self._group_id = value
|
117
|
+
self.__dict__["_group_id"] = value
|
118
|
+
|
119
|
+
@property
|
120
|
+
def created_at(self) -> float:
|
121
|
+
"""Unix timestamp when the message was created."""
|
122
|
+
return cast(float, self.__dict__["_created_at"])
|
123
|
+
|
124
|
+
@created_at.setter
|
125
|
+
def created_at(self, value: float) -> None:
|
126
|
+
"""Set creation timestamp for this message."""
|
127
|
+
self.__dict__["_created_at"] = value
|
125
128
|
|
126
129
|
@property
|
127
|
-
def ttl(self) ->
|
130
|
+
def ttl(self) -> float:
|
128
131
|
"""Time-to-live for this message."""
|
129
|
-
return self._ttl
|
132
|
+
return cast(float, self.__dict__["_ttl"])
|
130
133
|
|
131
134
|
@ttl.setter
|
132
|
-
def ttl(self, value:
|
135
|
+
def ttl(self, value: float) -> None:
|
133
136
|
"""Set ttl."""
|
134
|
-
self._ttl = value
|
137
|
+
self.__dict__["_ttl"] = value
|
135
138
|
|
136
139
|
@property
|
137
140
|
def message_type(self) -> str:
|
138
141
|
"""A string that encodes the action to be executed on the receiving end."""
|
139
|
-
return self._message_type
|
142
|
+
return cast(str, self.__dict__["_message_type"])
|
140
143
|
|
141
144
|
@message_type.setter
|
142
145
|
def message_type(self, value: str) -> None:
|
143
146
|
"""Set message_type."""
|
144
|
-
self._message_type = value
|
147
|
+
self.__dict__["_message_type"] = value
|
145
148
|
|
146
|
-
|
147
|
-
|
148
|
-
"""
|
149
|
-
return self.
|
149
|
+
def __repr__(self) -> str:
|
150
|
+
"""Return a string representation of this instance."""
|
151
|
+
view = ", ".join([f"{k.lstrip('_')}={v!r}" for k, v in self.__dict__.items()])
|
152
|
+
return f"{self.__class__.__qualname__}({view})"
|
150
153
|
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
154
|
+
def __eq__(self, other: object) -> bool:
|
155
|
+
"""Compare two instances of the class."""
|
156
|
+
if not isinstance(other, self.__class__):
|
157
|
+
raise NotImplementedError
|
158
|
+
return self.__dict__ == other.__dict__
|
155
159
|
|
156
160
|
|
157
|
-
@dataclass
|
158
161
|
class Error:
|
159
162
|
"""A dataclass that stores information about an error that occurred.
|
160
163
|
|
@@ -166,25 +169,35 @@ class Error:
|
|
166
169
|
A reason for why the error arose (e.g. an exception stack-trace)
|
167
170
|
"""
|
168
171
|
|
169
|
-
_code: int
|
170
|
-
_reason: str | None = None
|
171
|
-
|
172
172
|
def __init__(self, code: int, reason: str | None = None) -> None:
|
173
|
-
|
174
|
-
|
173
|
+
var_dict = {
|
174
|
+
"_code": code,
|
175
|
+
"_reason": reason,
|
176
|
+
}
|
177
|
+
self.__dict__.update(var_dict)
|
175
178
|
|
176
179
|
@property
|
177
180
|
def code(self) -> int:
|
178
181
|
"""Error code."""
|
179
|
-
return self._code
|
182
|
+
return cast(int, self.__dict__["_code"])
|
180
183
|
|
181
184
|
@property
|
182
185
|
def reason(self) -> str | None:
|
183
186
|
"""Reason reported about the error."""
|
184
|
-
return self._reason
|
187
|
+
return cast(Optional[str], self.__dict__["_reason"])
|
188
|
+
|
189
|
+
def __repr__(self) -> str:
|
190
|
+
"""Return a string representation of this instance."""
|
191
|
+
view = ", ".join([f"{k.lstrip('_')}={v!r}" for k, v in self.__dict__.items()])
|
192
|
+
return f"{self.__class__.__qualname__}({view})"
|
193
|
+
|
194
|
+
def __eq__(self, other: object) -> bool:
|
195
|
+
"""Compare two instances of the class."""
|
196
|
+
if not isinstance(other, self.__class__):
|
197
|
+
raise NotImplementedError
|
198
|
+
return self.__dict__ == other.__dict__
|
185
199
|
|
186
200
|
|
187
|
-
@dataclass
|
188
201
|
class Message:
|
189
202
|
"""State of your application from the viewpoint of the entity using it.
|
190
203
|
|
@@ -200,105 +213,108 @@ class Message:
|
|
200
213
|
when processing another message.
|
201
214
|
"""
|
202
215
|
|
203
|
-
_metadata: Metadata
|
204
|
-
_content: RecordSet | None = None
|
205
|
-
_error: Error | None = None
|
206
|
-
|
207
216
|
def __init__(
|
208
217
|
self,
|
209
218
|
metadata: Metadata,
|
210
219
|
content: RecordSet | None = None,
|
211
220
|
error: Error | None = None,
|
212
221
|
) -> None:
|
213
|
-
self._metadata = metadata
|
214
|
-
|
215
222
|
if not (content is None) ^ (error is None):
|
216
223
|
raise ValueError("Either `content` or `error` must be set, but not both.")
|
217
224
|
|
218
|
-
|
219
|
-
|
225
|
+
metadata.created_at = time.time() # Set the message creation timestamp
|
226
|
+
var_dict = {
|
227
|
+
"_metadata": metadata,
|
228
|
+
"_content": content,
|
229
|
+
"_error": error,
|
230
|
+
}
|
231
|
+
self.__dict__.update(var_dict)
|
220
232
|
|
221
233
|
@property
|
222
234
|
def metadata(self) -> Metadata:
|
223
235
|
"""A dataclass including information about the message to be executed."""
|
224
|
-
return self._metadata
|
236
|
+
return cast(Metadata, self.__dict__["_metadata"])
|
225
237
|
|
226
238
|
@property
|
227
239
|
def content(self) -> RecordSet:
|
228
240
|
"""The content of this message."""
|
229
|
-
if self._content is None:
|
241
|
+
if self.__dict__["_content"] is None:
|
230
242
|
raise ValueError(
|
231
243
|
"Message content is None. Use <message>.has_content() "
|
232
244
|
"to check if a message has content."
|
233
245
|
)
|
234
|
-
return self._content
|
246
|
+
return cast(RecordSet, self.__dict__["_content"])
|
235
247
|
|
236
248
|
@content.setter
|
237
249
|
def content(self, value: RecordSet) -> None:
|
238
250
|
"""Set content."""
|
239
|
-
if self._error is None:
|
240
|
-
self._content = value
|
251
|
+
if self.__dict__["_error"] is None:
|
252
|
+
self.__dict__["_content"] = value
|
241
253
|
else:
|
242
254
|
raise ValueError("A message with an error set cannot have content.")
|
243
255
|
|
244
256
|
@property
|
245
257
|
def error(self) -> Error:
|
246
258
|
"""Error captured by this message."""
|
247
|
-
if self._error is None:
|
259
|
+
if self.__dict__["_error"] is None:
|
248
260
|
raise ValueError(
|
249
261
|
"Message error is None. Use <message>.has_error() "
|
250
262
|
"to check first if a message carries an error."
|
251
263
|
)
|
252
|
-
return self._error
|
264
|
+
return cast(Error, self.__dict__["_error"])
|
253
265
|
|
254
266
|
@error.setter
|
255
267
|
def error(self, value: Error) -> None:
|
256
268
|
"""Set error."""
|
257
269
|
if self.has_content():
|
258
270
|
raise ValueError("A message with content set cannot carry an error.")
|
259
|
-
self._error = value
|
271
|
+
self.__dict__["_error"] = value
|
260
272
|
|
261
273
|
def has_content(self) -> bool:
|
262
274
|
"""Return True if message has content, else False."""
|
263
|
-
return self._content is not None
|
275
|
+
return self.__dict__["_content"] is not None
|
264
276
|
|
265
277
|
def has_error(self) -> bool:
|
266
278
|
"""Return True if message has an error, else False."""
|
267
|
-
return self._error is not None
|
268
|
-
|
269
|
-
def _create_reply_metadata(self, ttl: str) -> Metadata:
|
270
|
-
"""Construct metadata for a reply message."""
|
271
|
-
return Metadata(
|
272
|
-
run_id=self.metadata.run_id,
|
273
|
-
message_id="",
|
274
|
-
src_node_id=self.metadata.dst_node_id,
|
275
|
-
dst_node_id=self.metadata.src_node_id,
|
276
|
-
reply_to_message=self.metadata.message_id,
|
277
|
-
group_id=self.metadata.group_id,
|
278
|
-
ttl=ttl,
|
279
|
-
message_type=self.metadata.message_type,
|
280
|
-
partition_id=self.metadata.partition_id,
|
281
|
-
)
|
279
|
+
return self.__dict__["_error"] is not None
|
282
280
|
|
283
|
-
def create_error_reply(
|
284
|
-
self,
|
285
|
-
error: Error,
|
286
|
-
ttl: str,
|
287
|
-
) -> Message:
|
281
|
+
def create_error_reply(self, error: Error, ttl: float | None = None) -> Message:
|
288
282
|
"""Construct a reply message indicating an error happened.
|
289
283
|
|
290
284
|
Parameters
|
291
285
|
----------
|
292
286
|
error : Error
|
293
287
|
The error that was encountered.
|
294
|
-
ttl :
|
295
|
-
Time-to-live for this message.
|
288
|
+
ttl : Optional[float] (default: None)
|
289
|
+
Time-to-live for this message in seconds. If unset, it will be set based
|
290
|
+
on the remaining time for the received message before it expires. This
|
291
|
+
follows the equation:
|
292
|
+
|
293
|
+
ttl = msg.meta.ttl - (reply.meta.created_at - msg.meta.created_at)
|
294
|
+
|
295
|
+
Returns
|
296
|
+
-------
|
297
|
+
message : Message
|
298
|
+
A Message containing only the relevant error and metadata.
|
296
299
|
"""
|
300
|
+
# If no TTL passed, use default for message creation (will update after
|
301
|
+
# message creation)
|
302
|
+
ttl_ = DEFAULT_TTL if ttl is None else ttl
|
297
303
|
# Create reply with error
|
298
|
-
message = Message(metadata=
|
304
|
+
message = Message(metadata=_create_reply_metadata(self, ttl_), error=error)
|
305
|
+
|
306
|
+
if ttl is None:
|
307
|
+
# Set TTL equal to the remaining time for the received message to expire
|
308
|
+
ttl = self.metadata.ttl - (
|
309
|
+
message.metadata.created_at - self.metadata.created_at
|
310
|
+
)
|
311
|
+
message.metadata.ttl = ttl
|
312
|
+
|
313
|
+
self._limit_task_res_ttl(message)
|
314
|
+
|
299
315
|
return message
|
300
316
|
|
301
|
-
def create_reply(self, content: RecordSet, ttl:
|
317
|
+
def create_reply(self, content: RecordSet, ttl: float | None = None) -> Message:
|
302
318
|
"""Create a reply to this message with specified content and TTL.
|
303
319
|
|
304
320
|
The method generates a new `Message` as a reply to this message.
|
@@ -309,15 +325,84 @@ class Message:
|
|
309
325
|
----------
|
310
326
|
content : RecordSet
|
311
327
|
The content for the reply message.
|
312
|
-
ttl :
|
313
|
-
Time-to-live for this message.
|
328
|
+
ttl : Optional[float] (default: None)
|
329
|
+
Time-to-live for this message in seconds. If unset, it will be set based
|
330
|
+
on the remaining time for the received message before it expires. This
|
331
|
+
follows the equation:
|
332
|
+
|
333
|
+
ttl = msg.meta.ttl - (reply.meta.created_at - msg.meta.created_at)
|
314
334
|
|
315
335
|
Returns
|
316
336
|
-------
|
317
337
|
Message
|
318
338
|
A new `Message` instance representing the reply.
|
319
339
|
"""
|
320
|
-
|
321
|
-
|
340
|
+
# If no TTL passed, use default for message creation (will update after
|
341
|
+
# message creation)
|
342
|
+
ttl_ = DEFAULT_TTL if ttl is None else ttl
|
343
|
+
|
344
|
+
message = Message(
|
345
|
+
metadata=_create_reply_metadata(self, ttl_),
|
322
346
|
content=content,
|
323
347
|
)
|
348
|
+
|
349
|
+
if ttl is None:
|
350
|
+
# Set TTL equal to the remaining time for the received message to expire
|
351
|
+
ttl = self.metadata.ttl - (
|
352
|
+
message.metadata.created_at - self.metadata.created_at
|
353
|
+
)
|
354
|
+
message.metadata.ttl = ttl
|
355
|
+
|
356
|
+
self._limit_task_res_ttl(message)
|
357
|
+
|
358
|
+
return message
|
359
|
+
|
360
|
+
def __repr__(self) -> str:
|
361
|
+
"""Return a string representation of this instance."""
|
362
|
+
view = ", ".join(
|
363
|
+
[
|
364
|
+
f"{k.lstrip('_')}={v!r}"
|
365
|
+
for k, v in self.__dict__.items()
|
366
|
+
if v is not None
|
367
|
+
]
|
368
|
+
)
|
369
|
+
return f"{self.__class__.__qualname__}({view})"
|
370
|
+
|
371
|
+
def _limit_task_res_ttl(self, message: Message) -> None:
|
372
|
+
"""Limit the TaskRes TTL to not exceed the expiration time of the TaskIns it
|
373
|
+
replies to.
|
374
|
+
|
375
|
+
Parameters
|
376
|
+
----------
|
377
|
+
message : Message
|
378
|
+
The message to which the TaskRes is replying.
|
379
|
+
"""
|
380
|
+
# Calculate the maximum allowed TTL
|
381
|
+
max_allowed_ttl = (
|
382
|
+
self.metadata.created_at + self.metadata.ttl - message.metadata.created_at
|
383
|
+
)
|
384
|
+
|
385
|
+
if message.metadata.ttl - max_allowed_ttl > MESSAGE_TTL_TOLERANCE:
|
386
|
+
log(
|
387
|
+
WARNING,
|
388
|
+
"The reply TTL of %.2f seconds exceeded the "
|
389
|
+
"allowed maximum of %.2f seconds. "
|
390
|
+
"The TTL has been updated to the allowed maximum.",
|
391
|
+
message.metadata.ttl,
|
392
|
+
max_allowed_ttl,
|
393
|
+
)
|
394
|
+
message.metadata.ttl = max_allowed_ttl
|
395
|
+
|
396
|
+
|
397
|
+
def _create_reply_metadata(msg: Message, ttl: float) -> Metadata:
|
398
|
+
"""Construct metadata for a reply message."""
|
399
|
+
return Metadata(
|
400
|
+
run_id=msg.metadata.run_id,
|
401
|
+
message_id="",
|
402
|
+
src_node_id=msg.metadata.dst_node_id,
|
403
|
+
dst_node_id=msg.metadata.src_node_id,
|
404
|
+
reply_to_message=msg.metadata.message_id,
|
405
|
+
group_id=msg.metadata.group_id,
|
406
|
+
ttl=ttl,
|
407
|
+
message_type=msg.metadata.message_type,
|
408
|
+
)
|