Repository navigation
Expand file tree
/
Copy pathwarmup_and_health.py
More file actions
244 lines (185 loc) · 8.07 KB
/
Copy pathwarmup_and_health.py
File metadata and controls
244 lines (185 loc) · 8.07 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
"""Warmup priority groups and the health registry that gates readiness.
This example shows how to:
1. Write `AsyncWarmer`s and order them: lower `priority` runs first, warmers
sharing a priority run in parallel, and the printed start/done offsets prove
both -- peers start together, the next group waits.
2. Rely on fail-fast: one warmer with `raise_on_failure=True` aborts the run
and later priority groups never execute, while a warmer constructed with
`raise_on_failure=False` is allowed to fail without stopping anything.
3. See warmup finish BEFORE readiness flips true -- the Host primes
infrastructure, then binds, then reports ready, so no traffic ever arrives
at a cold pool.
4. Drive readiness from a `HealthRegistry`: it reports healthy only when the
Host's `ready` flag AND every registered check pass, so a degraded
dependency takes the pod out of rotation without killing the process.
Run with: `python examples/warmup_and_health.py`. It needs no infrastructure --
the warmers and checks are in-memory -- and exits 0.
"""
from __future__ import annotations
import asyncio
import contextlib
import logging
import time
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any
from servicewright import (
AppSpec,
AsyncWarmer,
DaemonEntrypoint,
Service,
WarmupError,
perform_warmup,
)
if TYPE_CHECKING:
from collections.abc import AsyncIterator, Mapping
from servicewright import HealthReport, UnitScopeProtocol
SERVICE_NAME = "catalog-service"
WARMUP_DELAY_SECONDS = 0.1
@dataclass(frozen=True)
class Settings:
"""All `BaseServiceSettingsProtocol` asks for; every observability section is off."""
logging: object | None = None
metrics: object | None = None
tracing: object | None = None
error_tracking: object | None = None
def get_app_version(self) -> str:
return "1.0.0"
class Stopwatch:
"""A shared clock: equal start offsets are what parallelism looks like."""
def __init__(self) -> None:
self._start = time.perf_counter()
def restart(self) -> None:
self._start = time.perf_counter()
def ms(self) -> float:
return (time.perf_counter() - self._start) * 1000
class DemoWarmer(AsyncWarmer):
"""A warmer standing in for a real pool/producer/index primer."""
def __init__(
self,
name: str,
priority: int,
stopwatch: Stopwatch,
primed: list[str],
*,
fails: bool = False,
raise_on_failure: bool = True,
) -> None:
super().__init__(raise_on_failure=raise_on_failure)
self._name = name
self._priority = priority
self._stopwatch = stopwatch
self._primed = primed
self._fails = fails
@property
def priority(self) -> int:
return self._priority
async def warmup(self) -> None:
started = self._stopwatch.ms()
await asyncio.sleep(WARMUP_DELAY_SECONDS)
if self._fails:
print(
f"[warmup] p{self._priority:<3} {self._name:<15} start +{started:5.0f}ms "
f"FAILED (raise_on_failure={self.raise_on_failure})"
)
raise WarmupError(f"{self._name} is unreachable")
self._primed.append(self._name)
print(
f"[warmup] p{self._priority:<3} {self._name:<15} start +{started:5.0f}ms "
f"done +{self._stopwatch.ms():5.0f}ms"
)
class ToggleableCheck:
"""Satisfies `HealthCheckerProtocol` structurally: any `async check() -> bool`."""
def __init__(self, *, healthy: bool = True) -> None:
self.healthy = healthy
async def check(self) -> bool:
return self.healthy
async def idle_until_stop(_scope: UnitScopeProtocol, stop: asyncio.Event) -> None:
"""This service has no work of its own; it exists to hold the probes open."""
await stop.wait()
class InMemoryScope:
"""An empty dependency scope -- this service resolves nothing."""
async def get(self, dependency_key: type[Any] | str) -> Any:
raise KeyError(dependency_key)
class InMemoryContainer:
"""The smallest object satisfying `DependencyContainerProtocol`."""
@contextlib.asynccontextmanager
async def app_scope(self) -> AsyncIterator[InMemoryScope]:
yield InMemoryScope()
@contextlib.asynccontextmanager
async def unit_scope(self, context: Mapping[Any, Any] | None = None) -> AsyncIterator[InMemoryScope]:
yield InMemoryScope()
def silence_library_logs() -> None:
"""Keep the narrative readable.
The fail-fast section deliberately fails warmers, and the kernel reports
that at WARNING with a traceback. A NullHandler on the library logger keeps
stdlib logging's last-resort stderr handler out of the output.
"""
library_logger = logging.getLogger("servicewright")
library_logger.addHandler(logging.NullHandler())
library_logger.propagate = False
def show_report(label: str, report: HealthReport) -> None:
"""Print one probe result the way `/system/health/readyz` would answer it."""
print(f"[health] {label:<24} status={report.status.value:<9} checks={report.checks}")
async def demo_fail_fast() -> None:
"""Run a doomed warmup set directly through the orchestrator."""
stopwatch = Stopwatch()
primed: list[str] = []
warmers = [
DemoWarmer("config-cache", 0, stopwatch, primed),
DemoWarmer("postgres-pool", 10, stopwatch, primed),
DemoWarmer("redis-cache", 10, stopwatch, primed, fails=True, raise_on_failure=False),
DemoWarmer("kafka-producer", 10, stopwatch, primed, fails=True),
DemoWarmer("search-index", 20, stopwatch, primed),
]
try:
await perform_warmup(SERVICE_NAME, warmers)
except WarmupError as exc:
print(f"[warmup] perform_warmup raised {type(exc).__name__}: {exc}")
print(f"[warmup] primed before the abort: {primed}")
print("[warmup] 'redis-cache' failed but was optional; 'search-index' (p20) never ran at all")
async def main() -> None:
silence_library_logs()
print("--- warmup: priority groups, parallel peers, fail-fast ---")
await demo_fail_fast()
print("\n--- the real run: warmers are primed before readiness flips ---")
stopwatch = Stopwatch()
primed: list[str] = []
postgres_check = ToggleableCheck()
redis_check = ToggleableCheck()
spec: AppSpec[Settings, InMemoryContainer] = AppSpec(
service_name=SERVICE_NAME,
create_container=lambda _settings: InMemoryContainer(),
)
spec.warmers.extend(
[
DemoWarmer("config-cache", 0, stopwatch, primed),
DemoWarmer("postgres-pool", 10, stopwatch, primed),
DemoWarmer("redis-cache", 10, stopwatch, primed),
DemoWarmer("search-index", 20, stopwatch, primed),
]
)
spec.health.add_check("postgres", postgres_check)
spec.health.add_check("redis", redis_check)
# Both checks already pass, yet readiness is false: the Host has not flipped
# the `ready` flag, so nothing should be routed here yet.
show_report("before startup", await spec.health.readiness())
service = Service(spec, entrypoints=[DaemonEntrypoint(idle_until_stop, kind="idle")])
stop = asyncio.Event()
async def probe_then_stop() -> None:
while not spec.health.ready:
await asyncio.sleep(0.01)
print(f"[host] ready flag flipped true; warmers already primed: {primed}")
show_report("liveness", await spec.health.liveness())
show_report("readiness", await spec.health.readiness())
print("\n--- a dependency degrades ---")
redis_check.healthy = False
show_report("readiness (redis down)", await spec.health.readiness())
print("[host] the `ready` flag is still True: the process serves, the probe says do-not-route")
print("\n--- shutdown ---")
stop.set()
stopwatch.restart()
await asyncio.gather(service.run(Settings(), stop=stop), probe_then_stop())
print(f"[host] readiness flag after drain: {spec.health.ready}")
show_report("after shutdown", await spec.health.readiness())
if __name__ == "__main__":
asyncio.run(main())