-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathservice_discovery.py
368 lines (294 loc) · 10.8 KB
/
service_discovery.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import abc
import enum
import threading
import time
from dataclasses import dataclass
from typing import Dict, List, Optional
import requests
from kubernetes import client, config, watch
from vllm_router.log import init_logger
logger = init_logger(__name__)
_global_service_discovery: "Optional[ServiceDiscovery]" = None
class ServiceDiscoveryType(enum.Enum):
STATIC = "static"
K8S = "k8s"
@dataclass
class EndpointInfo:
# Endpoint's url
url: str
# Model name
model_name: str
# Added timestamp
added_timestamp: float
class ServiceDiscovery(metaclass=abc.ABCMeta):
@abc.abstractmethod
def get_endpoint_info(self) -> List[EndpointInfo]:
"""
Get the URLs of the serving engines that are available for
querying.
Returns:
a list of engine URLs
"""
pass
def get_health(self) -> bool:
"""
Check if the service discovery module is healthy.
Returns:
True if the service discovery module is healthy, False otherwise
"""
return True
def close(self) -> None:
"""
Close the service discovery module.
"""
pass
class StaticServiceDiscovery(ServiceDiscovery):
def __init__(self, urls: List[str], models: List[str]):
assert len(urls) == len(models), "URLs and models should have the same length"
self.urls = urls
self.models = models
self.added_timestamp = int(time.time())
def get_endpoint_info(self) -> List[EndpointInfo]:
"""
Get the URLs of the serving engines that are available for
querying.
Returns:
a list of engine URLs
"""
return [
EndpointInfo(url, model, self.added_timestamp)
for url, model in zip(self.urls, self.models)
]
class K8sServiceDiscovery(ServiceDiscovery):
def __init__(self, namespace: str, port: str, label_selector=None):
"""
Initialize the Kubernetes service discovery module. This module
assumes all serving engine pods are in the same namespace, listening
on the same port, and have the same label selector.
It will start a daemon thread to watch the engine pods and update
the url of the available engines.
Args:
namespace: the namespace of the engine pods
port: the port of the engines
label_selector: the label selector of the engines
"""
self.namespace = namespace
self.port = port
self.available_engines: Dict[str, EndpointInfo] = {}
self.available_engines_lock = threading.Lock()
self.label_selector = label_selector
# Init kubernetes watcher
try:
config.load_incluster_config()
except:
config.load_kube_config()
self.k8s_api = client.CoreV1Api()
self.k8s_watcher = watch.Watch()
# Start watching engines
self.running = True
self.watcher_thread = threading.Thread(target=self._watch_engines, daemon=True)
self.watcher_thread.start()
@staticmethod
def _check_pod_ready(container_statuses):
"""
Check if all containers in the pod are ready by reading the
k8s container statuses.
"""
if not container_statuses:
return False
ready_count = sum(1 for status in container_statuses if status.ready)
return ready_count == len(container_statuses)
def _get_model_name(self, pod_ip) -> Optional[str]:
"""
Get the model name of the serving engine pod by querying the pod's
'/v1/models' endpoint.
Args:
pod_ip: the IP address of the pod
Returns:
the model name of the serving engine
"""
url = f"http://{pod_ip}:{self.port}/v1/models"
try:
response = requests.get(url)
response.raise_for_status()
model_name = response.json()["data"][0]["id"]
except Exception as e:
logger.error(f"Failed to get model name from {url}: {e}")
return None
return model_name
def _watch_engines(self):
# TODO (ApostaC): remove the hard-coded timeouts
while self.running:
try:
for event in self.k8s_watcher.stream(
self.k8s_api.list_namespaced_pod,
namespace=self.namespace,
label_selector=self.label_selector,
timeout_seconds=30,
):
pod = event["object"]
event_type = event["type"]
pod_name = pod.metadata.name
pod_ip = pod.status.pod_ip
is_pod_ready = self._check_pod_ready(pod.status.container_statuses)
if is_pod_ready:
model_name = self._get_model_name(pod_ip)
else:
model_name = None
self._on_engine_update(
pod_name, pod_ip, event_type, is_pod_ready, model_name
)
except Exception as e:
logger.error(f"K8s watcher error: {e}")
time.sleep(0.5)
def _add_engine(self, engine_name: str, engine_ip: str, model_name: str):
logger.info(
f"Discovered new serving engine {engine_name} at "
f"{engine_ip}, running model: {model_name}"
)
with self.available_engines_lock:
self.available_engines[engine_name] = EndpointInfo(
url=f"http://{engine_ip}:{self.port}",
model_name=model_name,
added_timestamp=int(time.time()),
)
def _delete_engine(self, engine_name: str):
logger.info(f"Serving engine {engine_name} is deleted")
with self.available_engines_lock:
del self.available_engines[engine_name]
def _on_engine_update(
self,
engine_name: str,
engine_ip: Optional[str],
event: str,
is_pod_ready: bool,
model_name: Optional[str],
) -> None:
if event == "ADDED":
if engine_ip is None:
return
if not is_pod_ready:
return
if model_name is None:
return
self._add_engine(engine_name, engine_ip, model_name)
elif event == "DELETED":
if engine_name not in self.available_engines:
return
self._delete_engine(engine_name)
elif event == "MODIFIED":
if engine_ip is None:
return
if is_pod_ready and model_name is not None:
self._add_engine(engine_name, engine_ip, model_name)
return
if (
not is_pod_ready or model_name is None
) and engine_name in self.available_engines:
self._delete_engine(engine_name)
return
def get_endpoint_info(self) -> List[EndpointInfo]:
"""
Get the URLs of the serving engines that are available for
querying.
Returns:
a list of engine URLs
"""
with self.available_engines_lock:
return list(self.available_engines.values())
def get_health(self) -> bool:
"""
Check if the service discovery module is healthy.
Returns:
True if the service discovery module is healthy, False otherwise
"""
return self.watcher_thread.is_alive()
def close(self):
"""
Close the service discovery module.
"""
self.running = False
self.k8s_watcher.stop()
self.watcher_thread.join()
def _create_service_discovery(
service_discovery_type: ServiceDiscoveryType, *args, **kwargs
) -> ServiceDiscovery:
"""
Create a service discovery module with the given type and arguments.
Args:
service_discovery_type: the type of service discovery module
*args: positional arguments for the service discovery module
**kwargs: keyword arguments for the service discovery module
Returns:
the created service discovery module
"""
if service_discovery_type == ServiceDiscoveryType.STATIC:
return StaticServiceDiscovery(*args, **kwargs)
elif service_discovery_type == ServiceDiscoveryType.K8S:
return K8sServiceDiscovery(*args, **kwargs)
else:
raise ValueError("Invalid service discovery type")
def initialize_service_discovery(
service_discovery_type: ServiceDiscoveryType, *args, **kwargs
) -> ServiceDiscovery:
"""
Initialize the service discovery module with the given type and arguments.
Args:
service_discovery_type: the type of service discovery module
*args: positional arguments for the service discovery module
**kwargs: keyword arguments for the service discovery module
Returns:
the initialized service discovery module
Raises:
ValueError: if the service discovery module is already initialized
ValueError: if the service discovery type is invalid
"""
global _global_service_discovery
if _global_service_discovery is not None:
raise ValueError("Service discovery module already initialized")
_global_service_discovery = _create_service_discovery(
service_discovery_type, *args, **kwargs
)
return _global_service_discovery
def reconfigure_service_discovery(
service_discovery_type: ServiceDiscoveryType, *args, **kwargs
) -> ServiceDiscovery:
"""
Reconfigure the service discovery module with the given type and arguments.
"""
global _global_service_discovery
if _global_service_discovery is None:
raise ValueError("Service discovery module not initialized")
new_service_discovery = _create_service_discovery(
service_discovery_type, *args, **kwargs
)
_global_service_discovery.close()
_global_service_discovery = new_service_discovery
return _global_service_discovery
def get_service_discovery() -> ServiceDiscovery:
"""
Get the initialized service discovery module.
Returns:
the initialized service discovery module
Raises:
ValueError: if the service discovery module is not initialized
"""
global _global_service_discovery
if _global_service_discovery is None:
raise ValueError("Service discovery module not initialized")
return _global_service_discovery
if __name__ == "__main__":
# Test the service discovery
# k8s_sd = K8sServiceDiscovery("default", 8000, "release=test")
initialize_service_discovery(
ServiceDiscoveryType.K8S,
namespace="default",
port=8000,
label_selector="release=test",
)
k8s_sd = get_service_discovery()
time.sleep(1)
while True:
urls = k8s_sd.get_endpoint_info()
print(urls)
time.sleep(2)