Skip to content
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

fix(BA-932): Unload purged docker images #3923

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/3923.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unload images that have been purged from the agents in the manager.
1 change: 0 additions & 1 deletion src/ai/backend/agent/docker/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,6 @@ class DockerAgent(AbstractAgent[DockerKernel, DockerKernelCreationContext]):
monitor_docker_task: asyncio.Task
agent_sockpath: Path
agent_sock_task: asyncio.Task
scan_images_timer: asyncio.Task
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The timer is created in the line below, but this variable is not used.

self.timer_tasks.append(aiotools.create_timer(self._scan_images_wrapper, 20.0))

metadata_server: MetadataServer
docker_ptask_group: aiotools.PersistentTaskGroup
gwbridge_subnet: Optional[str]
Expand Down
18 changes: 11 additions & 7 deletions src/ai/backend/manager/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ class AgentRegistry:
pending_waits: set[asyncio.Task[None]]
database_ptask_group: aiotools.PersistentTaskGroup
webhook_ptask_group: aiotools.PersistentTaskGroup
agent_installed_images: dict[AgentId, set[str]]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are caching image info to redis, should we cache locally again?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is necessary to detect images that need to be removed.


def __init__(
self,
Expand Down Expand Up @@ -287,6 +288,7 @@ def __init__(
hook_plugin_ctx,
self,
)
self.agent_installed_images = {}

async def init(self) -> None:
self.heartbeat_lock = asyncio.Lock()
Expand Down Expand Up @@ -3149,19 +3151,21 @@ async def _update() -> None:
)

# Update the mapping of kernel images to agents.
loaded_images = msgpack.unpackb(zlib.decompress(agent_info["images"]))
images = msgpack.unpackb(zlib.decompress(agent_info["images"]))
image_canonicals = set(img_info[0] for img_info in images)
Comment on lines +3154 to +3155
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume the 0th value means canonical, but that's not going to change, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, FYI,
Tuple's 0th value: image canonical.
Tuple's 1st value: image digest.

prev_image_canonicals = self.agent_installed_images.get(agent_id, set())

async def _pipe_builder(r: Redis):
pipe = r.pipeline()
for image, _ in loaded_images:
try:
await pipe.sadd(ImageRef.parse_image_str(image, "*").canonical, agent_id)
except ValueError:
# Skip opaque (non-Backend.AI) image.
continue
Comment on lines -3156 to -3161
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intentionally omitted the logic to skip image caching when a ValueError occurs while parsing the image string since I think excluding the loaded images sent from the agent from only redis caching is meaningless.

for image in image_canonicals:
await pipe.sadd(image, agent_id)

for image in prev_image_canonicals - image_canonicals:
await pipe.srem(image, agent_id)
return pipe

await redis_helper.execute(self.redis_image, _pipe_builder)
self.agent_installed_images[agent_id] = image_canonicals

await self.hook_plugin_ctx.notify(
"POST_AGENT_HEARTBEAT",
Expand Down
Loading