[Bugfix] Fix remote replica membership lifecycle races (#5277)
Signed-off-by: hbhflw2000 <417911774@qq.com> Co-authored-by: Gao Han <hgaoaf@connect.ust.hk> Co-authored-by: Samit <285365963@qq.com>
This commit is contained in:
2 files changed
+503
-8
No files matched your search
@@ -49,6 +49,13 @@ class MembershipController:
|
||||
self._stage_pools = stage_pools
|
||||
self._remote_replica_factory = remote_replica_factory
|
||||
self._membership_tasks: set[asyncio.Task[None]] = set()
|
||||
self._attaching_remote_replicas: set[tuple[int, int]] = set()
|
||||
self._attached_remote_replicas: set[tuple[int, int]] = set()
|
||||
self._remote_replica_keys_by_input_addr: dict[str, tuple[int, int]] = {}
|
||||
self._watch_generation = 0
|
||||
self._attach_start_generation: dict[tuple[int, int], int] = {}
|
||||
self._up_generation_by_input_addr: dict[tuple[int, str], int] = {}
|
||||
self._observed_up_attached_addrs: set[tuple[int, str]] = set()
|
||||
self._shutdown_event = asyncio.Event()
|
||||
self._watcher_task: asyncio.Task[None] | None = None
|
||||
self._output_queue: asyncio.Queue[EngineQueueMessage] | None = None
|
||||
@@ -67,8 +74,34 @@ class MembershipController:
|
||||
|
||||
async def handle_register(self, stage_id: int, replica_id: int) -> None:
|
||||
"""Handle a register_remote_replica message (fire-and-forget)."""
|
||||
if self._shutdown_event.is_set():
|
||||
logger.info(
|
||||
"[MembershipController] ignoring register during shutdown stage=%d replica=%d",
|
||||
stage_id,
|
||||
replica_id,
|
||||
)
|
||||
return
|
||||
|
||||
key = (stage_id, replica_id)
|
||||
if key in self._attached_remote_replicas:
|
||||
logger.info(
|
||||
"[MembershipController] ignoring register for attached replica stage=%d replica=%d",
|
||||
stage_id,
|
||||
replica_id,
|
||||
)
|
||||
return
|
||||
if key in self._attaching_remote_replicas:
|
||||
logger.info(
|
||||
"[MembershipController] ignoring register while attach is in flight stage=%d replica=%d",
|
||||
stage_id,
|
||||
replica_id,
|
||||
)
|
||||
return
|
||||
|
||||
self._attaching_remote_replicas.add(key)
|
||||
self._attach_start_generation[key] = self._watch_generation
|
||||
self._spawn_task(
|
||||
self._do_register(stage_id, replica_id),
|
||||
self._register_once(stage_id, replica_id),
|
||||
label=f"register-s{stage_id}-r{replica_id}",
|
||||
)
|
||||
|
||||
@@ -131,8 +164,24 @@ class MembershipController:
|
||||
while not self._shutdown_event.is_set():
|
||||
try:
|
||||
snap = self._hub.get_replica_list()
|
||||
self._watch_generation += 1
|
||||
current = {(rep.stage_id, rep.input_addr) for rep in snap.replicas if rep.status == ReplicaStatus.UP}
|
||||
for stage_id, addr in last_up - current:
|
||||
attached = {
|
||||
(stage_id, input_addr)
|
||||
for input_addr, (stage_id, _replica_id) in self._remote_replica_keys_by_input_addr.items()
|
||||
}
|
||||
self._observed_up_attached_addrs.update(attached & current)
|
||||
attaching_stages = {stage_id for stage_id, _replica_id in self._attaching_remote_replicas}
|
||||
for addr_key in current:
|
||||
if addr_key[0] in attaching_stages:
|
||||
self._up_generation_by_input_addr[addr_key] = self._watch_generation
|
||||
|
||||
# Preserve watcher cleanup for untracked/static clients. A
|
||||
# controller-attached client is eligible only after this
|
||||
# attachment has been observed UP at least once.
|
||||
untracked_disappeared = (last_up - current) - attached
|
||||
confirmed_attached_disappeared = self._observed_up_attached_addrs - current
|
||||
for stage_id, addr in untracked_disappeared | confirmed_attached_disappeared:
|
||||
self._spawn_task(
|
||||
self.handle_unregister(stage_id, addr),
|
||||
label=f"unregister-s{stage_id}",
|
||||
@@ -148,24 +197,75 @@ class MembershipController:
|
||||
except asyncio.CancelledError:
|
||||
raise
|
||||
|
||||
async def _do_register(self, stage_id: int, replica_id: int) -> None:
|
||||
async def _register_once(self, stage_id: int, replica_id: int) -> None:
|
||||
key = (stage_id, replica_id)
|
||||
attach_start_generation = self._attach_start_generation[key]
|
||||
try:
|
||||
input_addr = await self._do_register(stage_id, replica_id)
|
||||
if input_addr is None:
|
||||
return
|
||||
self._attached_remote_replicas.add(key)
|
||||
self._remote_replica_keys_by_input_addr[input_addr] = key
|
||||
addr_key = (stage_id, input_addr)
|
||||
if self._up_generation_by_input_addr.get(addr_key, -1) > attach_start_generation:
|
||||
self._observed_up_attached_addrs.add(addr_key)
|
||||
finally:
|
||||
self._attaching_remote_replicas.discard(key)
|
||||
self._attach_start_generation.pop(key, None)
|
||||
if not any(attaching_stage_id == stage_id for attaching_stage_id, _ in self._attaching_remote_replicas):
|
||||
self._up_generation_by_input_addr = {
|
||||
addr_key: generation
|
||||
for addr_key, generation in self._up_generation_by_input_addr.items()
|
||||
if addr_key[0] != stage_id
|
||||
}
|
||||
|
||||
async def _do_register(self, stage_id: int, replica_id: int) -> str | None:
|
||||
pool = self._pool_for_stage_id(stage_id)
|
||||
if pool is None:
|
||||
logger.warning("[MembershipController] register: stage_id %d out of range", stage_id)
|
||||
return
|
||||
return None
|
||||
client = await asyncio.to_thread(self._remote_replica_factory, stage_id, replica_id)
|
||||
input_addr = StagePool._client_input_addr(client)
|
||||
if input_addr is None:
|
||||
raise RuntimeError(f"remote replica factory for stage {stage_id} produced a client without input address")
|
||||
pool.add_client(input_addr, client, replica_id=replica_id)
|
||||
if self._shutdown_event.is_set():
|
||||
try:
|
||||
client.shutdown()
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"[MembershipController] failed to shutdown late replica client stage=%d replica=%d",
|
||||
stage_id,
|
||||
replica_id,
|
||||
)
|
||||
return None
|
||||
try:
|
||||
input_addr = StagePool._client_input_addr(client)
|
||||
if input_addr is None:
|
||||
raise RuntimeError(
|
||||
f"remote replica factory for stage {stage_id} produced a client without input address"
|
||||
)
|
||||
pool.add_client(input_addr, client, replica_id=replica_id)
|
||||
except Exception:
|
||||
try:
|
||||
client.shutdown()
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"[MembershipController] failed to shutdown rejected replica client stage=%d replica=%d",
|
||||
stage_id,
|
||||
replica_id,
|
||||
)
|
||||
raise
|
||||
logger.info(
|
||||
"[MembershipController] attached remote replica stage=%d replica=%d addr=%s",
|
||||
stage_id,
|
||||
replica_id,
|
||||
input_addr,
|
||||
)
|
||||
return input_addr
|
||||
|
||||
def _detach_replica(self, stage_id: int, input_addr: str) -> None:
|
||||
key = self._remote_replica_keys_by_input_addr.pop(input_addr, None)
|
||||
if key is not None:
|
||||
self._attached_remote_replicas.discard(key)
|
||||
self._attaching_remote_replicas.discard(key)
|
||||
self._observed_up_attached_addrs.discard((stage_id, input_addr))
|
||||
pool = self._pool_for_stage_id(stage_id)
|
||||
if pool is None:
|
||||
return
|
||||
|
||||
Reference in new issue
Block a user