-
Notifications
You must be signed in to change notification settings - Fork 178
refactor(BA-3160): Separate loader and writer of Kernel registry recovery #6958
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
HyeockJinKim
merged 4 commits into
main
from
refactor/divide-loader-writer-in-kernel-registry
Nov 26, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Refactor Agent's kernel registry recovery by separating loader and writer |
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import logging | ||
| import os | ||
| import pickle | ||
| import shutil | ||
| from pathlib import Path | ||
| from typing import override | ||
|
|
||
| from ai.backend.common.types import KernelId | ||
| from ai.backend.logging import BraceStyleAdapter | ||
|
|
||
| from ....agent.kernel import AbstractKernel | ||
| from ...exception import KernelRegistryLoadError, KernelRegistryNotFound | ||
| from .abc import AbstractKernelRegistryLoader | ||
|
|
||
| log = BraceStyleAdapter(logging.getLogger(__spec__.name)) | ||
|
|
||
|
|
||
| class PickleBasedKernelRegistryLoader(AbstractKernelRegistryLoader): | ||
| def __init__( | ||
| self, | ||
| last_registry_file_path: Path, | ||
| fallback_registry_file_path: Path, | ||
| legacy_registry_file_path: Path, | ||
| ) -> None: | ||
| self._last_registry_file_path = last_registry_file_path | ||
| self._fallback_registry_file_path = fallback_registry_file_path | ||
| self._legacy_registry_file_path = legacy_registry_file_path | ||
|
|
||
| @override | ||
| async def load_kernel_registry(self) -> dict[KernelId, AbstractKernel]: | ||
| legacy_registry_file = self._legacy_registry_file_path | ||
| fallback_registry_file = self._fallback_registry_file_path | ||
| final_file_path = self._last_registry_file_path | ||
| if not final_file_path.is_file(): | ||
| log.warning( | ||
| "Registry file with name {} not found. " | ||
| "Falling back to path with local instance id: {}", | ||
| final_file_path, | ||
| fallback_registry_file, | ||
| ) | ||
| final_file_path = fallback_registry_file | ||
| try: | ||
| if os.path.isfile(legacy_registry_file): | ||
| shutil.move(legacy_registry_file, final_file_path) | ||
| except Exception as e: | ||
| log.warning( | ||
| "Failed to move legacy kernel registry file {} to {} (err: {})", | ||
| str(legacy_registry_file), | ||
| str(final_file_path), | ||
| str(e), | ||
| ) | ||
| try: | ||
| with open(final_file_path, "rb") as f: | ||
| return pickle.load(f) | ||
Check noticeCode scanning / devskim Deserializing attacker-supplied data using `pickle` or `cPickle` can result in code execution. Note
Do not deserialize untrusted data.
|
||
| except EOFError as e: | ||
| log.warning("Failed to load the last kernel registry: {}", str(final_file_path)) | ||
| raise KernelRegistryLoadError from e | ||
| except FileNotFoundError as e: | ||
| raise KernelRegistryNotFound from e | ||
89 changes: 0 additions & 89 deletions
89
src/ai/backend/agent/kernel_registry/loader/pickle_based.py
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import logging | ||
| from collections.abc import Mapping | ||
| from dataclasses import dataclass | ||
| from pathlib import Path | ||
| from typing import Self | ||
|
|
||
| from ai.backend.common.types import AgentId, KernelId | ||
| from ai.backend.logging import BraceStyleAdapter | ||
|
|
||
| from ....agent.kernel import AbstractKernel | ||
| from ..loader.abc import AbstractKernelRegistryLoader | ||
| from ..loader.pickle import PickleBasedKernelRegistryLoader | ||
| from ..writer.abc import AbstractKernelRegistryWriter | ||
| from ..writer.pickle import PickleBasedKernelRegistryWriter | ||
| from ..writer.types import KernelRegistrySaveMetadata | ||
|
|
||
| log = BraceStyleAdapter(logging.getLogger(__spec__.name)) | ||
|
|
||
|
|
||
| @dataclass | ||
| class KernelRegistryRecoveryArgs: | ||
| ipc_base_path: Path | ||
| var_base_path: Path | ||
| agent_id: AgentId | ||
| local_instance_id: str | ||
|
|
||
|
|
||
| class KernelRegistryRecovery: | ||
| def __init__( | ||
| self, | ||
| loaders: list[AbstractKernelRegistryLoader], | ||
| writer: AbstractKernelRegistryWriter, | ||
| ) -> None: | ||
| self._loaders = loaders | ||
| self._writer = writer | ||
|
|
||
| @classmethod | ||
| def create(cls, args: KernelRegistryRecoveryArgs) -> Self: | ||
| registry_file_name = f"kernel_registry.{args.agent_id}.dat" | ||
| fallback_registry_file_name = f"kernel_registry.{args.local_instance_id}.dat" | ||
| legacy_registry_file_path = args.ipc_base_path / registry_file_name | ||
| fallback_registry_file_path = args.var_base_path / fallback_registry_file_name | ||
| last_registry_file_path = args.var_base_path / registry_file_name | ||
|
|
||
| return cls( | ||
| loaders=[ | ||
| PickleBasedKernelRegistryLoader( | ||
| last_registry_file_path, | ||
| fallback_registry_file_path, | ||
| legacy_registry_file_path, | ||
| ) | ||
| ], | ||
| writer=PickleBasedKernelRegistryWriter(last_registry_file_path), | ||
| ) | ||
|
|
||
| async def save_kernel_registry( | ||
| self, registry: Mapping[KernelId, AbstractKernel], metadata: KernelRegistrySaveMetadata | ||
| ) -> None: | ||
| await self._writer.save_kernel_registry(registry, metadata) | ||
|
|
||
| async def load_kernel_registry(self) -> dict[KernelId, AbstractKernel]: | ||
| result: dict[KernelId, AbstractKernel] = {} | ||
| for loader in self._loaders: | ||
| try: | ||
| loaded = await loader.load_kernel_registry() | ||
| for kernel_id, kernel in loaded.items(): | ||
| result[kernel_id] = kernel | ||
| except Exception as e: | ||
| log.warning( | ||
| "Failed to load kernel registry using loader {}, skip (err: {})", | ||
| loader.__class__.__name__, | ||
| str(e), | ||
| ) | ||
| continue | ||
| return result |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import logging | ||
| from abc import ABC, abstractmethod | ||
| from collections.abc import Mapping | ||
|
|
||
| from ai.backend.common.types import KernelId | ||
| from ai.backend.logging import BraceStyleAdapter | ||
|
|
||
| from ....agent.kernel import AbstractKernel | ||
| from .types import KernelRegistrySaveMetadata | ||
|
|
||
| log = BraceStyleAdapter(logging.getLogger(__spec__.name)) | ||
|
|
||
|
|
||
| class AbstractKernelRegistryWriter(ABC): | ||
| """ | ||
| Writer interface for saving kernel registry | ||
| """ | ||
|
|
||
| @abstractmethod | ||
| async def save_kernel_registry( | ||
| self, registry: Mapping[KernelId, AbstractKernel], metadata: KernelRegistrySaveMetadata | ||
| ) -> None: | ||
| """ | ||
| Save the kernel registry to persistent storage. | ||
| args: | ||
|
fregataa marked this conversation as resolved.
|
||
| registry: The kernel registry to save. | ||
| metadata: Additional metadata for saving. | ||
| Returns: None | ||
| """ | ||
| pass | ||
|
fregataa marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import logging | ||
| from collections.abc import Mapping | ||
| from typing import override | ||
|
|
||
| from ai.backend.common.types import KernelId | ||
| from ai.backend.logging import BraceStyleAdapter | ||
|
|
||
| from ....agent.kernel import AbstractKernel | ||
| from .abc import AbstractKernelRegistryWriter | ||
| from .types import KernelRegistrySaveMetadata | ||
|
|
||
| log = BraceStyleAdapter(logging.getLogger(__spec__.name)) | ||
|
|
||
|
|
||
| class NoopKernelRegistryWriter(AbstractKernelRegistryWriter): | ||
| def __init__(self) -> None: | ||
| pass | ||
|
|
||
| @override | ||
| async def save_kernel_registry( | ||
| self, registry: Mapping[KernelId, AbstractKernel], metadata: KernelRegistrySaveMetadata | ||
| ) -> None: | ||
| log.debug("NoopKernelRegistryWriter: skipping save_kernel_registry") | ||
| return | ||
|
fregataa marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import logging | ||
| import os | ||
| import pickle | ||
| import time | ||
| from collections.abc import Mapping | ||
| from pathlib import Path | ||
| from typing import override | ||
|
|
||
| from ai.backend.common.types import KernelId | ||
| from ai.backend.logging import BraceStyleAdapter | ||
|
|
||
| from ....agent.kernel import AbstractKernel | ||
| from .abc import AbstractKernelRegistryWriter | ||
| from .types import KernelRegistrySaveMetadata | ||
|
|
||
| log = BraceStyleAdapter(logging.getLogger(__spec__.name)) | ||
|
|
||
|
|
||
| SAVE_COOL_DOWN_SECONDS = 60 | ||
|
|
||
|
|
||
| class PickleBasedKernelRegistryWriter(AbstractKernelRegistryWriter): | ||
| def __init__(self, last_registry_file_path: Path) -> None: | ||
| self._last_registry_file_path = last_registry_file_path | ||
| self._last_saved_time = time.monotonic() | ||
|
|
||
| @override | ||
| async def save_kernel_registry( | ||
| self, registry: Mapping[KernelId, AbstractKernel], metadata: KernelRegistrySaveMetadata | ||
| ) -> None: | ||
| now = time.monotonic() | ||
| if (not metadata.force) and (now <= self._last_saved_time + SAVE_COOL_DOWN_SECONDS): | ||
| return # don't save too frequently | ||
| last_registry_file = self._last_registry_file_path | ||
| try: | ||
| with open(last_registry_file, "wb") as f: | ||
| pickle.dump(dict(registry), f) | ||
| self._last_saved_time = now | ||
| log.debug("Saved kernel registry to {}", str(last_registry_file)) | ||
| except Exception as e: | ||
| log.exception( | ||
| "Failed to save kernel registry to {} (registry: {})", | ||
| str(last_registry_file), | ||
| str(registry), | ||
| exc_info=e, | ||
| ) | ||
| try: | ||
| os.remove(last_registry_file) | ||
| except FileNotFoundError: | ||
|
fregataa marked this conversation as resolved.
|
||
| pass | ||
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.