-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_manager.py
153 lines (122 loc) · 4.72 KB
/
client_manager.py
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
# 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.
# ==============================================================================
"""Flower ClientManager."""
import random
import threading
from abc import ABC, abstractmethod
from logging import INFO
from typing import Dict, List, Optional
from flwr.common.logger import log
from flwr.server.client_proxy import ClientProxy
from flwr.server.criterion import Criterion
class ClientManager(ABC):
"""Abstract base class for managing Flower clients."""
@abstractmethod
def num_available(self) -> int:
"""Return the number of available clients."""
@abstractmethod
def register(self, client: ClientProxy) -> bool:
"""Register Flower ClientProxy instance.
Returns:
bool: Indicating if registration was successful
"""
@abstractmethod
def unregister(self, client: ClientProxy) -> None:
"""Unregister Flower ClientProxy instance."""
@abstractmethod
def all(self) -> Dict[str, ClientProxy]:
"""Return all available clients."""
@abstractmethod
def wait_for(self, num_clients: int, timeout: int) -> bool:
"""Wait until at least `num_clients` are available."""
@abstractmethod
def sample(
self,
num_clients: int,
min_num_clients: Optional[int] = None,
criterion: Optional[Criterion] = None,
) -> List[ClientProxy]:
"""Sample a number of Flower ClientProxy instances."""
class SimpleClientManager(ClientManager):
"""Provides a pool of available clients."""
def __init__(self) -> None:
self.clients: Dict[str, ClientProxy] = {}
self._cv = threading.Condition()
random.seed(2022)
def __len__(self) -> int:
return len(self.clients)
def wait_for(self, num_clients: int, timeout: int = 86400) -> bool:
"""Block until at least `num_clients` are available or until a timeout
is reached.
Current timeout default: 1 day.
"""
with self._cv:
return self._cv.wait_for(
lambda: len(self.clients) >= num_clients, timeout=timeout
)
def num_available(self) -> int:
"""Return the number of available clients."""
return len(self)
def register(self, client: ClientProxy) -> bool:
"""Register Flower ClientProxy instance.
Returns:
bool: Indicating if registration was successful. False if ClientProxy is
already registered or can not be registered for any reason
"""
if client.cid in self.clients:
return False
self.clients[client.cid] = client
with self._cv:
self._cv.notify_all()
return True
def unregister(self, client: ClientProxy) -> None:
"""Unregister Flower ClientProxy instance.
This method is idempotent.
"""
if client.cid in self.clients:
del self.clients[client.cid]
with self._cv:
self._cv.notify_all()
def all(self) -> Dict[str, ClientProxy]:
"""Return all available clients."""
return self.clients
def sample(
self,
num_clients: int,
min_num_clients: Optional[int] = None,
criterion: Optional[Criterion] = None,
) -> List[ClientProxy]:
"""Sample a number of Flower ClientProxy instances."""
# Block until at least num_clients are connected.
if min_num_clients is None:
min_num_clients = num_clients
self.wait_for(num_clients)
# Sample clients which meet the criterion
available_cids = list(self.clients)
if criterion is not None:
available_cids = [
cid for cid in available_cids if criterion.select(self.clients[cid])
]
if num_clients > len(available_cids):
log(
INFO,
"Sampling failed: number of available clients"
" (%s) is less than number of requested clients (%s).",
len(available_cids),
num_clients,
)
return []
sampled_cids = random.sample(available_cids, 30)
return [self.clients[cid] for cid in sampled_cids]