d_engine_client/
cluster.rs

1use std::fmt::Debug;
2use std::sync::Arc;
3
4use arc_swap::ArcSwap;
5use d_engine_proto::server::cluster::NodeMeta;
6
7use super::ClientInner;
8use crate::ClientApiError;
9
10/// Cluster administration interface
11///
12/// Currently supports member discovery. Node management operations
13/// will be added in future releases.
14#[derive(Clone)]
15pub struct ClusterClient {
16    client_inner: Arc<ArcSwap<ClientInner>>,
17}
18
19impl Debug for ClusterClient {
20    fn fmt(
21        &self,
22        f: &mut std::fmt::Formatter<'_>,
23    ) -> std::fmt::Result {
24        f.debug_struct("ClusterClient").finish()
25    }
26}
27
28impl ClusterClient {
29    pub(crate) fn new(client_inner: Arc<ArcSwap<ClientInner>>) -> Self {
30        Self { client_inner }
31    }
32
33    /// Lists all cluster members with metadata
34    ///
35    /// Returns node information including:
36    /// - IP address
37    /// - Port
38    /// - Role (Leader/Follower)
39    pub async fn list_members(&self) -> std::result::Result<Vec<NodeMeta>, ClientApiError> {
40        let client_inner = self.client_inner.load();
41
42        Ok(client_inner.pool.get_all_members())
43    }
44
45    /// Get the current leader ID
46    ///
47    /// Returns the leader node ID if known, or None if no leader is currently elected.
48    pub async fn get_leader_id(&self) -> std::result::Result<Option<u32>, ClientApiError> {
49        let client_inner = self.client_inner.load();
50
51        Ok(client_inner.pool.get_leader_id())
52    }
53}