-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathapp.py
More file actions
284 lines (246 loc) · 9.15 KB
/
app.py
File metadata and controls
284 lines (246 loc) · 9.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# Copyright 2025 Flower Labs 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.
# ==============================================================================
"""Flower ServerApp process."""
import argparse
from logging import DEBUG, ERROR, INFO
from pathlib import Path
from queue import Queue
from typing import Optional
from flwr.cli.config_utils import get_fab_metadata
from flwr.cli.install import install_from_fab
from flwr.cli.utils import get_sha256_hash
from flwr.common.args import add_args_flwr_app_common
from flwr.common.config import (
get_flwr_dir,
get_fused_config_from_dir,
get_project_config,
get_project_dir,
)
from flwr.common.constant import (
SERVERAPPIO_API_DEFAULT_CLIENT_ADDRESS,
ExecPluginType,
Status,
SubStatus,
)
from flwr.common.exit import ExitCode, flwr_exit
from flwr.common.heartbeat import HeartbeatSender, get_grpc_app_heartbeat_fn
from flwr.common.logger import (
log,
mirror_output_to_queue,
restore_output,
start_log_uploader,
stop_log_uploader,
)
from flwr.common.serde import (
context_from_proto,
context_to_proto,
fab_from_proto,
run_from_proto,
run_status_to_proto,
)
from flwr.common.telemetry import EventType, event
from flwr.common.typing import RunNotRunningException, RunStatus
from flwr.proto.appio_pb2 import ( # pylint: disable=E0611
PullAppInputsRequest,
PullAppInputsResponse,
PushAppOutputsRequest,
)
from flwr.proto.run_pb2 import UpdateRunStatusRequest # pylint: disable=E0611
from flwr.proto.serverappio_pb2_grpc import ServerAppIoStub
from flwr.server.grid.grpc_grid import GrpcGrid
from flwr.server.run_serverapp import run as run_
from flwr.supercore.app_utils import start_parent_process_monitor
from flwr.supercore.superexec.plugin import ServerAppExecPlugin
from flwr.supercore.superexec.run_superexec import run_with_deprecation_warning
def flwr_serverapp() -> None:
"""Run process-isolated Flower ServerApp."""
# Capture stdout/stderr
log_queue: Queue[Optional[str]] = Queue()
mirror_output_to_queue(log_queue)
args = _parse_args_run_flwr_serverapp().parse_args()
if not args.insecure:
flwr_exit(
ExitCode.COMMON_TLS_NOT_SUPPORTED,
"`flwr-serverapp` does not support TLS yet.",
)
# Disallow long-running `flwr-serverapp` processes
if args.token is None:
run_with_deprecation_warning(
cmd="flwr-serverapp",
plugin_type=ExecPluginType.SERVER_APP,
plugin_class=ServerAppExecPlugin,
stub_class=ServerAppIoStub,
appio_api_address=args.serverappio_api_address,
flwr_dir=args.flwr_dir,
parent_pid=args.parent_pid,
warn_run_once=args.run_once,
)
return
log(INFO, "Start `flwr-serverapp` process")
log(
DEBUG,
"`flwr-serverapp` will attempt to connect to SuperLink's "
"ServerAppIo API at %s",
args.serverappio_api_address,
)
run_serverapp(
serverappio_api_address=args.serverappio_api_address,
log_queue=log_queue,
token=args.token,
flwr_dir=args.flwr_dir,
certificates=None,
parent_pid=args.parent_pid,
)
# Restore stdout/stderr
restore_output()
def run_serverapp( # pylint: disable=R0913, R0914, R0915, R0917, W0212
serverappio_api_address: str,
log_queue: Queue[Optional[str]],
token: str,
flwr_dir: Optional[str] = None,
certificates: Optional[bytes] = None,
parent_pid: Optional[int] = None,
) -> None:
"""Run Flower ServerApp process."""
# Monitor the main process in case of SIGKILL
if parent_pid is not None:
start_parent_process_monitor(parent_pid)
# Resolve directory where FABs are installed
flwr_dir_ = get_flwr_dir(flwr_dir)
log_uploader = None
success = True
hash_run_id = None
run_status = None
heartbeat_sender = None
grid = None
context = None
try:
# Initialize the GrpcGrid
grid = GrpcGrid(
serverappio_service_address=serverappio_api_address,
root_certificates=certificates,
)
# Pull ServerAppInputs from LinkState
req = PullAppInputsRequest(token=token)
log(DEBUG, "[flwr-serverapp] Pull ServerAppInputs")
res: PullAppInputsResponse = grid._stub.PullAppInputs(req)
context = context_from_proto(res.context)
run = run_from_proto(res.run)
fab = fab_from_proto(res.fab)
hash_run_id = get_sha256_hash(run.run_id)
grid.set_run(run.run_id)
# Start log uploader for this run
log_uploader = start_log_uploader(
log_queue=log_queue,
node_id=0,
run_id=run.run_id,
stub=grid._stub,
)
log(DEBUG, "[flwr-serverapp] Start FAB installation.")
install_from_fab(fab.content, flwr_dir=flwr_dir_, skip_prompt=True)
fab_id, fab_version = get_fab_metadata(fab.content)
app_path = str(get_project_dir(fab_id, fab_version, fab.hash_str, flwr_dir_))
config = get_project_config(app_path)
# Obtain server app reference and the run config
server_app_attr = config["tool"]["flwr"]["app"]["components"]["serverapp"]
server_app_run_config = get_fused_config_from_dir(
Path(app_path), run.override_config
)
# Update run_config in context
context.run_config = server_app_run_config
log(
DEBUG,
"[flwr-serverapp] Will load ServerApp `%s` in %s",
server_app_attr,
app_path,
)
# Change status to Running
run_status_proto = run_status_to_proto(RunStatus(Status.RUNNING, "", ""))
grid._stub.UpdateRunStatus(
UpdateRunStatusRequest(run_id=run.run_id, run_status=run_status_proto)
)
event(
EventType.FLWR_SERVERAPP_RUN_ENTER,
event_details={"run-id-hash": hash_run_id},
)
# Set up heartbeat sender
heartbeat_fn = get_grpc_app_heartbeat_fn(
grid._stub,
run.run_id,
failure_message="Heartbeat failed unexpectedly. The SuperLink could "
"not find the provided run ID, or the run status is invalid.",
)
heartbeat_sender = HeartbeatSender(heartbeat_fn)
heartbeat_sender.start()
# Load and run the ServerApp with the Grid
updated_context = run_(
grid=grid,
server_app_dir=app_path,
server_app_attr=server_app_attr,
context=context,
)
# Send resulting context
context_proto = context_to_proto(updated_context)
log(DEBUG, "[flwr-serverapp] Will push ServerAppOutputs")
out_req = PushAppOutputsRequest(
token=token, run_id=run.run_id, context=context_proto
)
_ = grid._stub.PushAppOutputs(out_req)
run_status = RunStatus(Status.FINISHED, SubStatus.COMPLETED, "")
except RunNotRunningException:
log(INFO, "")
log(INFO, "Run ID %s stopped.", run.run_id)
log(INFO, "")
run_status = None
success = False
except Exception as ex: # pylint: disable=broad-exception-caught
exc_entity = "ServerApp"
log(ERROR, "%s raised an exception", exc_entity, exc_info=ex)
run_status = RunStatus(Status.FINISHED, SubStatus.FAILED, str(ex))
success = False
finally:
# Stop heartbeat sender
if heartbeat_sender:
heartbeat_sender.stop()
# Stop log uploader for this run and upload final logs
if log_uploader:
stop_log_uploader(log_queue, log_uploader)
# Update run status
if run_status and grid:
run_status_proto = run_status_to_proto(run_status)
grid._stub.UpdateRunStatus(
UpdateRunStatusRequest(run_id=run.run_id, run_status=run_status_proto)
)
# Close the Grpc connection
if grid:
grid.close()
event(
EventType.FLWR_SERVERAPP_RUN_LEAVE,
event_details={"run-id-hash": hash_run_id, "success": success},
)
def _parse_args_run_flwr_serverapp() -> argparse.ArgumentParser:
"""Parse flwr-serverapp command line arguments."""
parser = argparse.ArgumentParser(
description="Run a Flower ServerApp",
)
parser.add_argument(
"--serverappio-api-address",
default=SERVERAPPIO_API_DEFAULT_CLIENT_ADDRESS,
type=str,
help="Address of SuperLink's ServerAppIo API (IPv4, IPv6, or a domain name)."
f"By default, it is set to {SERVERAPPIO_API_DEFAULT_CLIENT_ADDRESS}.",
)
add_args_flwr_app_common(parser=parser)
return parser