Skip to content

Commit

Permalink
refactor(framework) Move all address constants to `flwr.common.consta…
Browse files Browse the repository at this point in the history
…nt` (#4178)
  • Loading branch information
panh99 authored Sep 12, 2024
1 parent 630d787 commit 09aa288
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 35 deletions.
7 changes: 3 additions & 4 deletions src/py/flwr/client/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from flwr.common import GRPC_MAX_MESSAGE_LENGTH, Context, EventType, Message, event
from flwr.common.address import parse_address
from flwr.common.constant import (
CLIENTAPPIO_API_DEFAULT_ADDRESS,
MISSING_EXTRA_REST,
RUN_ID_NUM_BYTES,
TRANSPORT_TYPE_GRPC_ADAPTER,
Expand All @@ -60,8 +61,6 @@
from .node_state import NodeState
from .numpy_client import NumPyClient

ADDRESS_CLIENTAPPIO_API_GRPC_RERE = "0.0.0.0:9094"

ISOLATION_MODE_SUBPROCESS = "subprocess"
ISOLATION_MODE_PROCESS = "process"

Expand Down Expand Up @@ -211,7 +210,7 @@ def start_client_internal(
max_wait_time: Optional[float] = None,
flwr_path: Optional[Path] = None,
isolation: Optional[str] = None,
supernode_address: Optional[str] = ADDRESS_CLIENTAPPIO_API_GRPC_RERE,
supernode_address: Optional[str] = CLIENTAPPIO_API_DEFAULT_ADDRESS,
) -> None:
"""Start a Flower client node which connects to a Flower server.
Expand Down Expand Up @@ -266,7 +265,7 @@ class `flwr.client.Client` (default: None)
by the SueprNode and communicates using gRPC at the address
`supernode_address`. If `process`, the `ClientApp` runs in a separate isolated
process and communicates using gRPC at the address `supernode_address`.
supernode_address : Optional[str] (default: `ADDRESS_CLIENTAPPIO_API_GRPC_RERE`)
supernode_address : Optional[str] (default: `CLIENTAPPIO_API_DEFAULT_ADDRESS`)
The SuperNode gRPC server address.
"""
if insecure is None:
Expand Down
11 changes: 5 additions & 6 deletions src/py/flwr/client/supernode/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from flwr.common import EventType, event
from flwr.common.config import parse_config_args
from flwr.common.constant import (
FLEET_API_GRPC_RERE_DEFAULT_ADDRESS,
TRANSPORT_TYPE_GRPC_ADAPTER,
TRANSPORT_TYPE_GRPC_RERE,
TRANSPORT_TYPE_REST,
Expand All @@ -44,8 +45,6 @@
)
from ..clientapp.utils import get_load_client_app_fn

ADDRESS_FLEET_API_GRPC_RERE = "0.0.0.0:9092"


def run_supernode() -> None:
"""Run Flower SuperNode."""
Expand Down Expand Up @@ -103,11 +102,11 @@ def run_client_app() -> None:

def _warn_deprecated_server_arg(args: argparse.Namespace) -> None:
"""Warn about the deprecated argument `--server`."""
if args.server != ADDRESS_FLEET_API_GRPC_RERE:
if args.server != FLEET_API_GRPC_RERE_DEFAULT_ADDRESS:
warn = "Passing flag --server is deprecated. Use --superlink instead."
warn_deprecated_feature(warn)

if args.superlink != ADDRESS_FLEET_API_GRPC_RERE:
if args.superlink != FLEET_API_GRPC_RERE_DEFAULT_ADDRESS:
# if `--superlink` also passed, then
# warn user that this argument overrides what was passed with `--server`
log(
Expand Down Expand Up @@ -247,12 +246,12 @@ def _parse_args_common(parser: argparse.ArgumentParser) -> None:
)
parser.add_argument(
"--server",
default=ADDRESS_FLEET_API_GRPC_RERE,
default=FLEET_API_GRPC_RERE_DEFAULT_ADDRESS,
help="Server address",
)
parser.add_argument(
"--superlink",
default=ADDRESS_FLEET_API_GRPC_RERE,
default=FLEET_API_GRPC_RERE_DEFAULT_ADDRESS,
help="SuperLink Fleet API (gRPC-rere) address (IPv4, IPv6, or a domain name)",
)
parser.add_argument(
Expand Down
13 changes: 12 additions & 1 deletion src/py/flwr/common/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,18 @@
TRANSPORT_TYPE_VCE,
]

SUPEREXEC_DEFAULT_ADDRESS = "0.0.0.0:9093"
# Addresses
# SuperNode
CLIENTAPPIO_API_DEFAULT_ADDRESS = "0.0.0.0:9094"
# SuperExec
EXEC_API_DEFAULT_ADDRESS = "0.0.0.0:9093"
# SuperLink
DRIVER_API_DEFAULT_ADDRESS = "0.0.0.0:9091"
FLEET_API_GRPC_RERE_DEFAULT_ADDRESS = "0.0.0.0:9092"
FLEET_API_GRPC_BIDI_DEFAULT_ADDRESS = (
"[::]:8080" # IPv6 to keep start_server compatible
)
FLEET_API_REST_DEFAULT_ADDRESS = "0.0.0.0:9093"

# Constants for ping
PING_DEFAULT_INTERVAL = 30
Expand Down
17 changes: 8 additions & 9 deletions src/py/flwr/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
from flwr.common.address import parse_address
from flwr.common.config import get_flwr_dir
from flwr.common.constant import (
DRIVER_API_DEFAULT_ADDRESS,
FLEET_API_GRPC_BIDI_DEFAULT_ADDRESS,
FLEET_API_GRPC_RERE_DEFAULT_ADDRESS,
FLEET_API_REST_DEFAULT_ADDRESS,
MISSING_EXTRA_REST,
TRANSPORT_TYPE_GRPC_ADAPTER,
TRANSPORT_TYPE_GRPC_RERE,
Expand Down Expand Up @@ -68,18 +72,13 @@
from .superlink.fleet.grpc_rere.server_interceptor import AuthenticateServerInterceptor
from .superlink.state import StateFactory

ADDRESS_DRIVER_API = "0.0.0.0:9091"
ADDRESS_FLEET_API_GRPC_RERE = "0.0.0.0:9092"
ADDRESS_FLEET_API_GRPC_BIDI = "[::]:8080" # IPv6 to keep start_server compatible
ADDRESS_FLEET_API_REST = "0.0.0.0:9093"

DATABASE = ":flwr-in-memory-state:"
BASE_DIR = get_flwr_dir() / "superlink" / "ffs"


def start_server( # pylint: disable=too-many-arguments,too-many-locals
*,
server_address: str = ADDRESS_FLEET_API_GRPC_BIDI,
server_address: str = FLEET_API_GRPC_BIDI_DEFAULT_ADDRESS,
server: Optional[Server] = None,
config: Optional[ServerConfig] = None,
strategy: Optional[Strategy] = None,
Expand Down Expand Up @@ -232,9 +231,9 @@ def run_superlink() -> None:
TRANSPORT_TYPE_GRPC_RERE,
TRANSPORT_TYPE_GRPC_ADAPTER,
]:
args.fleet_api_address = ADDRESS_FLEET_API_GRPC_RERE
args.fleet_api_address = FLEET_API_GRPC_RERE_DEFAULT_ADDRESS
elif args.fleet_api_type == TRANSPORT_TYPE_REST:
args.fleet_api_address = ADDRESS_FLEET_API_REST
args.fleet_api_address = FLEET_API_REST_DEFAULT_ADDRESS

fleet_address, host, port = _format_address(args.fleet_api_address)

Expand Down Expand Up @@ -653,7 +652,7 @@ def _add_args_driver_api(parser: argparse.ArgumentParser) -> None:
parser.add_argument(
"--driver-api-address",
help="Driver API (gRPC) server address (IPv4, IPv6, or a domain name).",
default=ADDRESS_DRIVER_API,
default=DRIVER_API_DEFAULT_ADDRESS,
)


Expand Down
5 changes: 2 additions & 3 deletions src/py/flwr/server/driver/grpc_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import grpc

from flwr.common import DEFAULT_TTL, Message, Metadata, RecordSet
from flwr.common.constant import DRIVER_API_DEFAULT_ADDRESS
from flwr.common.grpc import create_channel
from flwr.common.logger import log
from flwr.common.serde import (
Expand All @@ -45,8 +46,6 @@

from .driver import Driver

DEFAULT_SERVER_ADDRESS_DRIVER = "[::]:9091"

ERROR_MESSAGE_DRIVER_NOT_CONNECTED = """
[Driver] Error: Not connected.
Expand All @@ -73,7 +72,7 @@ class GrpcDriver(Driver):
def __init__( # pylint: disable=too-many-arguments
self,
run_id: int,
driver_service_address: str = DEFAULT_SERVER_ADDRESS_DRIVER,
driver_service_address: str = DRIVER_API_DEFAULT_ADDRESS,
root_certificates: Optional[bytes] = None,
) -> None:
self._run_id = run_id
Expand Down
11 changes: 5 additions & 6 deletions src/py/flwr/server/run_serverapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
get_project_config,
get_project_dir,
)
from flwr.common.constant import DRIVER_API_DEFAULT_ADDRESS
from flwr.common.logger import log, update_console_handler, warn_deprecated_feature
from flwr.common.object_ref import load_app
from flwr.common.typing import UserConfig
Expand All @@ -44,8 +45,6 @@
from .driver.grpc_driver import GrpcDriver
from .server_app import LoadServerAppError, ServerApp

ADDRESS_DRIVER_API = "0.0.0.0:9091"


def run(
driver: Driver,
Expand Down Expand Up @@ -112,11 +111,11 @@ def run_server_app() -> None:
"app by executing `flwr new` and following the prompt."
)

if args.server != ADDRESS_DRIVER_API:
if args.server != DRIVER_API_DEFAULT_ADDRESS:
warn = "Passing flag --server is deprecated. Use --superlink instead."
warn_deprecated_feature(warn)

if args.superlink != ADDRESS_DRIVER_API:
if args.superlink != DRIVER_API_DEFAULT_ADDRESS:
# if `--superlink` also passed, then
# warn user that this argument overrides what was passed with `--server`
log(
Expand Down Expand Up @@ -275,12 +274,12 @@ def _parse_args_run_server_app() -> argparse.ArgumentParser:
)
parser.add_argument(
"--server",
default=ADDRESS_DRIVER_API,
default=DRIVER_API_DEFAULT_ADDRESS,
help="Server address",
)
parser.add_argument(
"--superlink",
default=ADDRESS_DRIVER_API,
default=DRIVER_API_DEFAULT_ADDRESS,
help="SuperLink Driver API (gRPC-rere) address (IPv4, IPv6, or a domain name)",
)
parser.add_argument(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import grpc

from flwr.common.constant import FLEET_API_GRPC_RERE_DEFAULT_ADDRESS
from flwr.common.secure_aggregation.crypto.symmetric_encryption import (
compute_hmac,
generate_key_pairs,
Expand All @@ -42,7 +43,7 @@
from flwr.proto.node_pb2 import Node # pylint: disable=E0611
from flwr.proto.run_pb2 import GetRunRequest, GetRunResponse # pylint: disable=E0611
from flwr.proto.task_pb2 import Task, TaskRes # pylint: disable=E0611
from flwr.server.app import ADDRESS_FLEET_API_GRPC_RERE, _run_fleet_api_grpc_rere
from flwr.server.app import _run_fleet_api_grpc_rere
from flwr.server.superlink.ffs.ffs_factory import FfsFactory
from flwr.server.superlink.state.state_factory import StateFactory

Expand Down Expand Up @@ -73,7 +74,7 @@ def setUp(self) -> None:

self._server_interceptor = AuthenticateServerInterceptor(self.state)
self._server: grpc.Server = _run_fleet_api_grpc_rere(
ADDRESS_FLEET_API_GRPC_RERE,
FLEET_API_GRPC_RERE_DEFAULT_ADDRESS,
state_factory,
ffs_factory,
None,
Expand Down
4 changes: 2 additions & 2 deletions src/py/flwr/superexec/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from flwr.common import EventType, event, log
from flwr.common.address import parse_address
from flwr.common.config import parse_config_args
from flwr.common.constant import SUPEREXEC_DEFAULT_ADDRESS
from flwr.common.constant import EXEC_API_DEFAULT_ADDRESS
from flwr.common.exit_handlers import register_exit_handlers
from flwr.common.object_ref import load_app, validate

Expand Down Expand Up @@ -81,7 +81,7 @@ def _parse_args_run_superexec() -> argparse.ArgumentParser:
parser.add_argument(
"--address",
help="SuperExec (gRPC) server address (IPv4, IPv6, or a domain name)",
default=SUPEREXEC_DEFAULT_ADDRESS,
default=EXEC_API_DEFAULT_ADDRESS,
)
parser.add_argument(
"--executor",
Expand Down
4 changes: 2 additions & 2 deletions src/py/flwr/superexec/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
from typing_extensions import override

from flwr.cli.install import install_from_fab
from flwr.common.constant import DRIVER_API_DEFAULT_ADDRESS
from flwr.common.grpc import create_channel
from flwr.common.logger import log
from flwr.common.serde import fab_to_proto, user_config_to_proto
from flwr.common.typing import Fab, UserConfig
from flwr.proto.driver_pb2 import CreateRunRequest # pylint: disable=E0611
from flwr.proto.driver_pb2_grpc import DriverStub
from flwr.server.driver.grpc_driver import DEFAULT_SERVER_ADDRESS_DRIVER

from .executor import Executor, RunTracker

Expand All @@ -50,7 +50,7 @@ class DeploymentEngine(Executor):

def __init__(
self,
superlink: str = DEFAULT_SERVER_ADDRESS_DRIVER,
superlink: str = DRIVER_API_DEFAULT_ADDRESS,
root_certificates: Optional[str] = None,
flwr_dir: Optional[str] = None,
) -> None:
Expand Down

0 comments on commit 09aa288

Please sign in to comment.