forked from bentoml/BentoML
/
start.py
282 lines (248 loc) · 9.1 KB
/
start.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
from __future__ import annotations
import os
import sys
import json
import math
import shutil
import typing as t
import logging
import tempfile
import contextlib
from pathlib import Path
from simple_di import inject
from simple_di import Provide
from bentoml import load
from ._internal.utils import reserve_free_port
from ._internal.resource import CpuResource
from ._internal.utils.circus import create_standalone_arbiter
from ._internal.utils.analytics import track_serve
from ._internal.configuration.containers import BentoMLContainer
logger = logging.getLogger(__name__)
SCRIPT_RUNNER = "bentoml_cli.worker.runner"
SCRIPT_API_SERVER = "bentoml_cli.worker.http_api_server"
SCRIPT_DEV_API_SERVER = "bentoml_cli.worker.http_dev_api_server"
API_SERVER = "api_server"
RUNNER = "runner"
@inject
def ensure_prometheus_dir(
directory: str = Provide[BentoMLContainer.prometheus_multiproc_dir],
clean: bool = True,
use_alternative: bool = True,
) -> str:
try:
path = Path(directory)
if path.exists():
if not path.is_dir() or any(path.iterdir()):
if clean:
shutil.rmtree(str(path))
path.mkdir()
return str(path.absolute())
else:
raise RuntimeError(
"Prometheus multiproc directory {} is not empty".format(path)
)
else:
return str(path.absolute())
else:
path.mkdir(parents=True)
return str(path.absolute())
except shutil.Error as e:
if not use_alternative:
raise RuntimeError(
f"Failed to clean the prometheus multiproc directory {directory}: {e}"
)
except OSError as e:
if not use_alternative:
raise RuntimeError(
f"Failed to create the prometheus multiproc directory {directory}: {e}"
)
assert use_alternative
alternative = tempfile.mkdtemp()
logger.warning(
f"Failed to ensure the prometheus multiproc directory {directory}, "
f"using alternative: {alternative}",
)
BentoMLContainer.prometheus_multiproc_dir.set(alternative)
return alternative
MAX_AF_UNIX_PATH_LENGTH = 103
@inject
def start_runner_server(
bento_identifier: str,
working_dir: str,
runner_name: str,
port: int | None = None,
host: str | None = None,
backlog: int = Provide[BentoMLContainer.api_server_config.backlog],
) -> None:
"""
Experimental API for serving a BentoML runner.
"""
working_dir = os.path.realpath(os.path.expanduser(working_dir))
svc = load(bento_identifier, working_dir=working_dir, standalone_load=True)
from circus.sockets import CircusSocket # type: ignore
from circus.watcher import Watcher # type: ignore
watchers: t.List[Watcher] = []
circus_socket_map: t.Dict[str, CircusSocket] = {}
uds_path = None
ensure_prometheus_dir()
with contextlib.ExitStack() as port_stack:
for runner in svc.runners:
if runner.name == runner_name:
if port is None:
port = port_stack.enter_context(reserve_free_port())
if host is None:
host = "127.0.0.1"
circus_socket_map[runner.name] = CircusSocket(
name=runner.name,
host=host,
port=port,
backlog=backlog,
)
watchers.append(
Watcher(
name=f"{RUNNER}_{runner.name}",
cmd=sys.executable,
args=[
"-m",
SCRIPT_RUNNER,
bento_identifier,
"--runner-name",
runner.name,
"--fd",
f"$(circus.sockets.{runner.name})",
"--working-dir",
working_dir,
"--no-access-log",
"--worker-id",
"$(circus.wid)",
],
copy_env=True,
stop_children=True,
use_sockets=True,
working_dir=working_dir,
numprocesses=runner.scheduled_worker_count,
)
)
break
else:
raise ValueError(
f"Runner {runner_name} not found in the service: `{bento_identifier}`, "
f"available runners: {[r.name for r in svc.runners]}"
)
arbiter = create_standalone_arbiter(
watchers=watchers,
sockets=list(circus_socket_map.values()),
)
with track_serve(svc, production=True, component=RUNNER):
try:
arbiter.start(
cb=lambda _: logger.info( # type: ignore
'Starting RunnerServer from "%s"\n running on http://%s:%s (Press CTRL+C to quit)',
bento_identifier,
host,
port,
),
)
finally:
if uds_path is not None:
shutil.rmtree(uds_path)
@inject
def start_http_server(
bento_identifier: str,
runner_map: t.Dict[str, str],
working_dir: str,
port: int = Provide[BentoMLContainer.api_server_config.port],
host: str = Provide[BentoMLContainer.api_server_config.host],
backlog: int = Provide[BentoMLContainer.api_server_config.backlog],
api_workers: int | None = None,
ssl_certfile: str | None = Provide[BentoMLContainer.api_server_config.ssl.certfile],
ssl_keyfile: str | None = Provide[BentoMLContainer.api_server_config.ssl.keyfile],
ssl_keyfile_password: str
| None = Provide[BentoMLContainer.api_server_config.ssl.keyfile_password],
ssl_version: int | None = Provide[BentoMLContainer.api_server_config.ssl.version],
ssl_cert_reqs: int
| None = Provide[BentoMLContainer.api_server_config.ssl.cert_reqs],
ssl_ca_certs: str | None = Provide[BentoMLContainer.api_server_config.ssl.ca_certs],
ssl_ciphers: str | None = Provide[BentoMLContainer.api_server_config.ssl.ciphers],
) -> None:
working_dir = os.path.realpath(os.path.expanduser(working_dir))
svc = load(bento_identifier, working_dir=working_dir, standalone_load=True)
runner_requirements = {runner.name for runner in svc.runners}
if not runner_requirements.issubset(set(runner_map)):
raise ValueError(
f"{bento_identifier} requires runners {runner_requirements}, but only "
f"{set(runner_map)} are provided"
)
from circus.sockets import CircusSocket # type: ignore
from circus.watcher import Watcher # type: ignore
watchers: t.List[Watcher] = []
circus_socket_map: t.Dict[str, CircusSocket] = {}
uds_path = None
prometheus_dir = ensure_prometheus_dir()
logger.debug("Runner map: %s", runner_map)
circus_socket_map["_bento_api_server"] = CircusSocket(
name="_bento_api_server",
host=host,
port=port,
backlog=backlog,
)
args: list[str | int] = [
"-m",
SCRIPT_API_SERVER,
bento_identifier,
"--fd",
"$(circus.sockets._bento_api_server)",
"--runner-map",
json.dumps(runner_map),
"--working-dir",
working_dir,
"--backlog",
f"{backlog}",
"--worker-id",
"$(CIRCUS.WID)",
"--prometheus-dir",
prometheus_dir,
]
# Add optional SSL args if they exist
if ssl_certfile:
args.extend(["--ssl-certfile", str(ssl_certfile)])
if ssl_keyfile:
args.extend(["--ssl-keyfile", str(ssl_keyfile)])
if ssl_keyfile_password:
args.extend(["--ssl-keyfile-password", ssl_keyfile_password])
if ssl_ca_certs:
args.extend(["--ssl-ca-certs", str(ssl_ca_certs)])
# match with default uvicorn values.
if ssl_version:
args.extend(["--ssl-version", int(ssl_version)])
if ssl_cert_reqs:
args.extend(["--ssl-cert-reqs", int(ssl_cert_reqs)])
if ssl_ciphers:
args.extend(["--ssl-ciphers", ssl_ciphers])
watchers.append(
Watcher(
name=API_SERVER,
cmd=sys.executable,
args=args,
copy_env=True,
numprocesses=api_workers or math.ceil(CpuResource.from_system()),
stop_children=True,
use_sockets=True,
working_dir=working_dir,
)
)
arbiter = create_standalone_arbiter(
watchers=watchers,
sockets=list(circus_socket_map.values()),
)
with track_serve(svc, production=True, component=API_SERVER):
try:
arbiter.start(
cb=lambda _: logger.info( # type: ignore
f'Starting bare Bento API server from "{bento_identifier}" '
f"running on http://{host}:{port} (Press CTRL+C to quit)"
),
)
finally:
if uds_path is not None:
shutil.rmtree(uds_path)