Skip to content

Commit 7e58856

Browse files
fregataaclaude
andcommitted
refactor(BA-4910): Use set comprehension for container_ids deduplication
Change container_ids collection from list to set comprehension in collect_container_stat() and collect_process_stat() to automatically deduplicate container IDs and prevent redundant stat collection. Benefits: - Automatic deduplication of container IDs - More efficient (no duplicate processing) - Semantically clearer (expresses intent of unique IDs) - Defensive against edge cases in kernel registry Changes: - src/ai/backend/agent/agent.py:1391: collect_container_stat() - src/ai/backend/agent/agent.py:1403: collect_process_stat() Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 87e336a commit 7e58856

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/ai/backend/agent/agent.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,25 +1388,25 @@ async def collect_node_stat(self, resource_scaling_factors: Mapping[SlotName, De
13881388
async def collect_container_stat(self, interval: float) -> None:
13891389
if self.local_config.debug.log_stats:
13901390
log.debug("collecting container statistics")
1391-
container_ids: list[ContainerId] = []
1392-
for kernel_obj in [*self.kernel_registry.values()]:
1393-
if not kernel_obj.stats_enabled or kernel_obj.container_id is None:
1394-
continue
1395-
container_ids.append(ContainerId(kernel_obj.container_id))
1391+
container_ids = {
1392+
ContainerId(kernel_obj.container_id)
1393+
for kernel_obj in self.kernel_registry.values()
1394+
if kernel_obj.stats_enabled and kernel_obj.container_id is not None
1395+
}
13961396
async with asyncio.timeout(STAT_COLLECTION_TIMEOUT):
1397-
await self.stat_ctx.collect_container_stat(container_ids)
1397+
await self.stat_ctx.collect_container_stat(list(container_ids))
13981398

13991399
@_observe_stat_task(stat_scope=StatScope.PROCESS)
14001400
async def collect_process_stat(self, interval: float) -> None:
14011401
if self.local_config.debug.log_stats:
14021402
log.debug("collecting process statistics in container")
1403-
container_ids = []
1404-
for kernel_obj in [*self.kernel_registry.values()]:
1405-
if not kernel_obj.stats_enabled or kernel_obj.container_id is None:
1406-
continue
1407-
container_ids.append(ContainerId(kernel_obj.container_id))
1403+
container_ids = {
1404+
ContainerId(kernel_obj.container_id)
1405+
for kernel_obj in self.kernel_registry.values()
1406+
if kernel_obj.stats_enabled and kernel_obj.container_id is not None
1407+
}
14081408
async with asyncio.timeout(STAT_COLLECTION_TIMEOUT):
1409-
await self.stat_ctx.collect_per_container_process_stat(container_ids)
1409+
await self.stat_ctx.collect_per_container_process_stat(list(container_ids))
14101410

14111411
def _get_public_host(self) -> str:
14121412
agent_config = self.local_config.agent

0 commit comments

Comments
 (0)