zeebe_rs/
topology.rs

1use crate::{ClientError, client::Client, proto};
2
3/// Request to obtain the current cluster topology.
4///
5/// This request is used to retrieve detailed information about the Zeebe cluster's topology,
6/// including broker nodes, partition distribution, cluster size, replication factor, and gateway version.
7///
8/// # Example
9///
10/// ```ignore
11/// let topology = client.topology().send().await;
12/// ```
13///
14/// # Errors
15///
16/// Returns a `ClientError` if the request fails.
17#[derive(Debug, Clone)]
18pub struct TopologyRequest(Client);
19
20impl TopologyRequest {
21    pub(crate) fn new(client: Client) -> Self {
22        TopologyRequest(client)
23    }
24
25    /// Sends a request to get the current cluster topology
26    ///
27    /// # Returns
28    ///
29    /// A `Result` containing either:
30    /// - `TopologyResponse` with information about:
31    ///   - Broker nodes in the cluster
32    ///   - Partition distribution
33    ///   - Cluster size and replication factor
34    ///   - Gateway version
35    /// - `ClientError` if the request fails
36    pub async fn send(mut self) -> Result<TopologyResponse, ClientError> {
37        let request = proto::TopologyRequest {};
38
39        let result = self
40            .0
41            .gateway_client
42            .topology(tonic::Request::new(request))
43            .await?;
44
45        Ok(result.into_inner().into())
46    }
47}
48
49/// Information about a partition in the Zeebe cluster
50///
51/// This struct holds details about a specific partition, including its unique identifier,
52/// role, and health status.
53#[derive(Debug, Clone)]
54pub struct Partition {
55    partition_id: i32,
56    role: i32,
57    health: i32,
58}
59
60impl From<proto::Partition> for Partition {
61    fn from(value: proto::Partition) -> Partition {
62        Partition {
63            partition_id: value.partition_id,
64            role: value.role,
65            health: value.health,
66        }
67    }
68}
69
70impl Partition {
71    /// Returns the unique identifier for this partition.
72    ///
73    /// # Returns
74    ///
75    /// An `i32` representing the partition's unique identifier.
76    pub fn partition_id(&self) -> i32 {
77        self.partition_id
78    }
79
80    /// Returns the role of this partition.
81    ///
82    /// # Returns
83    ///
84    /// An `i32` representing the role of the partition:
85    ///
86    /// - `0`: LEADER - Handles all requests.
87    /// - `1`: FOLLOWER - Replicates data.
88    /// - `2`: INACTIVE - Not participating.
89    pub fn role(&self) -> i32 {
90        self.role
91    }
92
93    /// Returns the health status of this partition.
94    ///
95    /// # Returns
96    ///
97    /// An `i32` representing the health status of the partition:
98    ///
99    /// - `0`: HEALTHY - Processing normally.
100    /// - `1`: UNHEALTHY - Processing with issues.
101    /// - `2`: DEAD - Not processing.
102    pub fn health(&self) -> i32 {
103        self.health
104    }
105}
106
107/// Information about a broker node in the Zeebe cluster
108#[derive(Debug, Clone)]
109pub struct BrokerInfo {
110    node_id: i32,
111    host: String,
112    port: i32,
113    partitions: Vec<Partition>,
114    version: String,
115}
116
117impl From<proto::BrokerInfo> for BrokerInfo {
118    fn from(value: proto::BrokerInfo) -> BrokerInfo {
119        BrokerInfo {
120            node_id: value.node_id,
121            host: value.host,
122            port: value.port,
123            partitions: value.partitions.into_iter().map(|p| p.into()).collect(),
124            version: value.version,
125        }
126    }
127}
128
129impl BrokerInfo {
130    /// Returns the unique identifier for this broker within the cluster.
131    ///
132    /// # Returns
133    ///
134    /// An `i32` representing the broker's node ID.
135    pub fn node_id(&self) -> i32 {
136        self.node_id
137    }
138
139    /// Returns the network hostname where this broker can be reached.
140    ///
141    /// # Returns
142    ///
143    /// A string slice (`&str`) representing the broker's hostname.
144    pub fn host(&self) -> &str {
145        &self.host
146    }
147
148    /// Returns the network port where this broker accepts connections.
149    ///
150    /// # Returns
151    ///
152    /// An `i32` representing the broker's port number.
153    pub fn port(&self) -> i32 {
154        self.port
155    }
156
157    /// Returns a reference to the list of partitions managed or replicated by this broker.
158    ///
159    /// # Returns
160    ///
161    /// A slice of `Partition` references representing the partitions managed or replicated by this broker.
162    pub fn partitions(&self) -> &[Partition] {
163        &self.partitions
164    }
165
166    /// Returns the version of the broker software.
167    ///
168    /// # Returns
169    ///
170    /// A string slice (`&str`) representing the version of the broker software.
171    pub fn version(&self) -> &str {
172        &self.version
173    }
174}
175
176/// Response containing current topology of Zeebe cluster
177#[derive(Debug, Clone)]
178pub struct TopologyResponse {
179    brokers: Vec<BrokerInfo>,
180    cluster_size: i32,
181    partitions_count: i32,
182    replication_factor: i32,
183    gateway_version: String,
184}
185
186impl From<proto::TopologyResponse> for TopologyResponse {
187    fn from(value: proto::TopologyResponse) -> TopologyResponse {
188        TopologyResponse {
189            brokers: value.brokers.into_iter().map(|b| b.into()).collect(),
190            cluster_size: value.cluster_size,
191            partitions_count: value.partitions_count,
192            replication_factor: value.replication_factor,
193            gateway_version: value.gateway_version,
194        }
195    }
196}
197
198/// Represents the response containing the topology information of the Zeebe cluster.
199impl TopologyResponse {
200    /// Returns a reference to the list of all broker nodes in the cluster.
201    ///
202    /// # Returns
203    ///
204    /// A slice of `BrokerInfo` references representing all broker nodes in the cluster.
205    pub fn brokers(&self) -> &[BrokerInfo] {
206        &self.brokers
207    }
208
209    /// Returns the total number of nodes in the cluster.
210    ///
211    /// # Returns
212    ///
213    /// An `i32` representing the total number of nodes.
214    pub fn cluster_size(&self) -> i32 {
215        self.cluster_size
216    }
217
218    /// Returns the total number of partitions distributed across the cluster.
219    ///
220    /// # Returns
221    ///
222    /// An `i32` representing the total number of partitions.
223    pub fn partitions_count(&self) -> i32 {
224        self.partitions_count
225    }
226
227    /// Returns the number of copies of each partition that are maintained.
228    ///
229    /// # Returns
230    ///
231    /// An `i32` representing the replication factor.
232    pub fn replication_factor(&self) -> i32 {
233        self.replication_factor
234    }
235
236    /// Returns the version of the gateway software.
237    ///
238    /// # Returns
239    ///
240    /// A string slice (`&str`) representing the version of the gateway software.
241    pub fn gateway_version(&self) -> &str {
242        &self.gateway_version
243    }
244}