-
Notifications
You must be signed in to change notification settings - Fork 875
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create new REST API for client nodes (#1594)
Co-authored-by: Pedro Porto Buarque de Gusmão <pedropgusmao@gmail.com> Co-authored-by: Taner Topal <taner@adap.com>
- Loading branch information
1 parent
09510f1
commit 1757d5a
Showing
12 changed files
with
690 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Copyright 2023 Adap GmbH. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================== | ||
"""Task handling.""" | ||
|
||
|
||
from typing import Optional, Tuple | ||
|
||
from flwr.proto.fleet_pb2 import PullTaskInsResponse | ||
from flwr.proto.task_pb2 import TaskIns | ||
from flwr.proto.transport_pb2 import ServerMessage | ||
|
||
|
||
def get_server_message( | ||
pull_task_ins_response: PullTaskInsResponse, | ||
) -> Optional[Tuple[TaskIns, ServerMessage]]: | ||
"""Get the first ServerMessage, if available.""" | ||
|
||
# Extract a single ServerMessage from the response, if possible | ||
if len(pull_task_ins_response.task_ins_list) == 0: | ||
return None | ||
|
||
# Only evaluate the first message | ||
task_ins: TaskIns = pull_task_ins_response.task_ins_list[0] | ||
|
||
# Discard the message if it is not in | ||
# {GetPropertiesIns, GetParametersIns, FitIns, EvaluateIns} | ||
if ( | ||
not task_ins.HasField("task") | ||
or not task_ins.task.HasField("legacy_server_message") | ||
or task_ins.task.legacy_server_message.WhichOneof("msg") == "reconnect_ins" | ||
): | ||
return None | ||
|
||
return task_ins, task_ins.task.legacy_server_message |
128 changes: 128 additions & 0 deletions
128
src/py/flwr/client/message_handler/task_handler_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
# Copyright 2023 Adap GmbH. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================== | ||
"""Tests for module task_handler.""" | ||
|
||
|
||
from flwr.client.message_handler.task_handler import get_server_message | ||
from flwr.proto.fleet_pb2 import PullTaskInsResponse, Reconnect | ||
from flwr.proto.task_pb2 import Task, TaskIns | ||
from flwr.proto.transport_pb2 import ServerMessage | ||
|
||
|
||
def test_get_server_message_empty() -> None: | ||
"""Test get_server_message.""" | ||
|
||
# Prepare | ||
res = PullTaskInsResponse(reconnect=None, task_ins_list=[]) | ||
|
||
# Execute | ||
actual = get_server_message(res) | ||
|
||
# Assert | ||
assert actual is None | ||
|
||
|
||
def test_get_server_message_reconnect() -> None: | ||
"""Test get_server_message.""" | ||
|
||
# Prepare | ||
res = PullTaskInsResponse(reconnect=Reconnect(reconnect=42), task_ins_list=[]) | ||
|
||
# Execute | ||
actual = get_server_message(res) | ||
|
||
# Assert | ||
assert actual is None | ||
|
||
|
||
def test_get_server_message_none_task() -> None: | ||
"""Test get_server_message.""" | ||
|
||
# Prepare | ||
res = PullTaskInsResponse(reconnect=None, task_ins_list=[TaskIns(task=None)]) | ||
|
||
# Execute | ||
actual = get_server_message(res) | ||
|
||
# Assert | ||
assert actual is None | ||
|
||
|
||
def test_get_server_message_none_legacy() -> None: | ||
"""Test get_server_message.""" | ||
|
||
# Prepare | ||
res = PullTaskInsResponse( | ||
reconnect=None, task_ins_list=[TaskIns(task=Task(legacy_server_message=None))] | ||
) | ||
|
||
# Execute | ||
actual = get_server_message(res) | ||
|
||
# Assert | ||
assert actual is None | ||
|
||
|
||
def test_get_server_message_legacy_reconnect() -> None: | ||
"""Test get_server_message.""" | ||
|
||
# Prepare | ||
res = PullTaskInsResponse( | ||
reconnect=None, | ||
task_ins_list=[ | ||
TaskIns( | ||
task=Task( | ||
legacy_server_message=ServerMessage( | ||
reconnect_ins=ServerMessage.ReconnectIns(seconds=3) | ||
) | ||
) | ||
) | ||
], | ||
) | ||
|
||
# Execute | ||
actual = get_server_message(res) | ||
|
||
# Assert | ||
assert actual is None | ||
|
||
|
||
def test_get_server_message_legacy_valid() -> None: | ||
"""Test get_server_message.""" | ||
|
||
# Prepare | ||
expected = TaskIns( | ||
task=Task( | ||
legacy_server_message=ServerMessage( | ||
get_properties_ins=ServerMessage.GetPropertiesIns() | ||
) | ||
) | ||
) | ||
res = PullTaskInsResponse( | ||
reconnect=None, | ||
task_ins_list=[expected], | ||
) | ||
|
||
# Execute | ||
actual = get_server_message(res) | ||
|
||
# Assert | ||
assert actual is not None | ||
actual_task_ins, actual_server_message = actual | ||
assert actual_task_ins == expected | ||
|
||
# pylint: disable=no-member | ||
assert actual_server_message == expected.task.legacy_server_message | ||
# pylint: enable=no-member |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Copyright 2020 Adap GmbH. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================== | ||
"""Client-side part of the REST transport layer.""" |
Oops, something went wrong.