dynamo_runtime/
instances.rs

1// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Instance management functions for the distributed runtime.
5//!
6//! This module provides functionality to list and manage instances across
7//! the entire distributed system, complementing the component-specific
8//! instance listing in `component.rs`.
9
10use std::sync::Arc;
11
12use crate::component::{INSTANCE_ROOT_PATH, Instance};
13use crate::storage::key_value_store::{KeyValueStore, KeyValueStoreManager};
14use crate::transports::etcd::Client as EtcdClient;
15
16pub async fn list_all_instances(client: &KeyValueStoreManager) -> anyhow::Result<Vec<Instance>> {
17    let Some(bucket) = client.get_bucket(INSTANCE_ROOT_PATH).await? else {
18        return Ok(vec![]);
19    };
20
21    let entries = bucket.entries().await?;
22    let mut instances = Vec::with_capacity(entries.len());
23    for (name, bytes) in entries.into_iter() {
24        match serde_json::from_slice::<Instance>(&bytes) {
25            Ok(instance) => instances.push(instance),
26            Err(err) => {
27                tracing::warn!(%err, key = name, "Failed to parse instance from storage");
28            }
29        }
30    }
31    instances.sort();
32
33    Ok(instances)
34}