Skip to main content

rc_core/admin/
cluster.rs

1//! Cluster management type definitions
2//!
3//! This module contains data structures for cluster management operations
4//! including server information, disk status, and heal operations.
5
6use serde::{Deserialize, Serialize};
7use std::collections::{BTreeMap, HashMap};
8
9/// Server information representing a RustFS node
10#[derive(Debug, Clone, Serialize, Deserialize, Default)]
11#[serde(rename_all = "camelCase")]
12pub struct ServerInfo {
13    /// Server state (online, offline, initializing)
14    #[serde(default)]
15    pub state: String,
16
17    /// Server endpoint URL
18    #[serde(default)]
19    pub endpoint: String,
20
21    /// Connection scheme (http/https)
22    #[serde(default)]
23    pub scheme: String,
24
25    /// Uptime in seconds
26    #[serde(default)]
27    pub uptime: u64,
28
29    /// Server version
30    #[serde(default)]
31    pub version: String,
32
33    /// Git commit ID
34    #[serde(default, rename = "commitID")]
35    pub commit_id: String,
36
37    /// Network interfaces
38    #[serde(default)]
39    pub network: HashMap<String, String>,
40
41    /// Attached drives
42    #[serde(default, rename = "drives")]
43    pub disks: Vec<DiskInfo>,
44
45    /// Pool number
46    #[serde(default, rename = "poolNumber")]
47    pub pool_number: i32,
48
49    /// Memory statistics
50    #[serde(default, rename = "mem_stats")]
51    pub mem_stats: MemStats,
52}
53
54/// Disk information
55#[derive(Debug, Clone, Serialize, Deserialize, Default)]
56#[serde(rename_all = "camelCase")]
57pub struct DiskInfo {
58    /// Disk endpoint
59    #[serde(default)]
60    pub endpoint: String,
61
62    /// Whether this is a root disk
63    #[serde(default, rename = "rootDisk")]
64    pub root_disk: bool,
65
66    /// Drive path
67    #[serde(default, rename = "path")]
68    pub drive_path: String,
69
70    /// Whether healing is in progress
71    #[serde(default)]
72    pub healing: bool,
73
74    /// Whether scanning is in progress
75    #[serde(default)]
76    pub scanning: bool,
77
78    /// Disk state (online, offline)
79    #[serde(default)]
80    pub state: String,
81
82    /// Disk UUID
83    #[serde(default)]
84    pub uuid: String,
85
86    /// Total space in bytes
87    #[serde(default, rename = "totalspace")]
88    pub total_space: u64,
89
90    /// Used space in bytes
91    #[serde(default, rename = "usedspace")]
92    pub used_space: u64,
93
94    /// Available space in bytes
95    #[serde(default, rename = "availspace")]
96    pub available_space: u64,
97
98    /// Pool index
99    #[serde(default, alias = "pool_index")]
100    pub pool_index: i32,
101
102    /// Set index
103    #[serde(default, alias = "set_index")]
104    pub set_index: i32,
105
106    /// Disk index within set
107    #[serde(default, alias = "disk_index")]
108    pub disk_index: i32,
109
110    /// Healing info if disk is being healed
111    #[serde(default, skip_serializing_if = "Option::is_none")]
112    pub heal_info: Option<HealingDiskInfo>,
113}
114
115/// Healing disk information
116#[derive(Debug, Clone, Serialize, Deserialize, Default)]
117#[serde(rename_all = "camelCase")]
118pub struct HealingDiskInfo {
119    /// Heal ID
120    #[serde(default)]
121    pub id: String,
122
123    /// Heal session ID
124    #[serde(default)]
125    pub heal_id: String,
126
127    /// Pool index
128    #[serde(default)]
129    pub pool_index: Option<usize>,
130
131    /// Set index
132    #[serde(default)]
133    pub set_index: Option<usize>,
134
135    /// Disk index
136    #[serde(default)]
137    pub disk_index: Option<usize>,
138
139    /// Endpoint being healed
140    #[serde(default)]
141    pub endpoint: String,
142
143    /// Path being healed
144    #[serde(default)]
145    pub path: String,
146
147    /// Objects total count
148    #[serde(default)]
149    pub objects_total_count: u64,
150
151    /// Objects total size
152    #[serde(default)]
153    pub objects_total_size: u64,
154
155    /// Items healed count
156    #[serde(default)]
157    pub items_healed: u64,
158
159    /// Items failed count
160    #[serde(default)]
161    pub items_failed: u64,
162
163    /// Bytes done
164    #[serde(default)]
165    pub bytes_done: u64,
166
167    /// Whether healing is finished
168    #[serde(default)]
169    pub finished: bool,
170
171    /// Current bucket being healed
172    #[serde(default)]
173    pub bucket: String,
174
175    /// Current object being healed
176    #[serde(default)]
177    pub object: String,
178}
179
180/// Memory statistics
181#[derive(Debug, Clone, Serialize, Deserialize, Default)]
182pub struct MemStats {
183    /// Current allocated memory
184    #[serde(default)]
185    pub alloc: u64,
186
187    /// Total allocated memory over lifetime
188    #[serde(default)]
189    pub total_alloc: u64,
190
191    /// Heap allocated memory
192    #[serde(default)]
193    pub heap_alloc: u64,
194}
195
196/// Storage backend type
197#[derive(Debug, Clone, Serialize, Deserialize, Default)]
198#[serde(rename_all = "lowercase")]
199pub enum BackendType {
200    /// Filesystem backend (single drive)
201    #[default]
202    #[serde(rename = "FS")]
203    Fs,
204    /// Erasure coding backend (distributed)
205    #[serde(rename = "Erasure")]
206    Erasure,
207}
208
209impl std::fmt::Display for BackendType {
210    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
211        match self {
212            BackendType::Fs => write!(f, "FS"),
213            BackendType::Erasure => write!(f, "Erasure"),
214        }
215    }
216}
217
218/// Backend information
219#[derive(Debug, Clone, Serialize, Deserialize, Default)]
220#[serde(rename_all = "camelCase")]
221pub struct BackendInfo {
222    /// Backend type
223    #[serde(default, rename = "backendType")]
224    pub backend_type: BackendType,
225
226    /// Number of online disks
227    #[serde(default, rename = "onlineDisks")]
228    pub online_disks: usize,
229
230    /// Number of offline disks
231    #[serde(default, rename = "offlineDisks")]
232    pub offline_disks: usize,
233
234    /// Standard storage class parity
235    #[serde(default, rename = "standardSCParity")]
236    pub standard_sc_parity: Option<usize>,
237
238    /// Reduced redundancy storage class parity
239    #[serde(default, rename = "rrSCParity")]
240    pub rr_sc_parity: Option<usize>,
241
242    /// Total erasure sets
243    #[serde(default, rename = "totalSets")]
244    pub total_sets: Vec<usize>,
245
246    /// Drives per erasure set
247    #[serde(default, rename = "totalDrivesPerSet")]
248    pub drives_per_set: Vec<usize>,
249}
250
251/// Cluster usage statistics
252#[derive(Debug, Clone, Serialize, Deserialize, Default)]
253pub struct UsageInfo {
254    /// Total storage size in bytes
255    #[serde(default)]
256    pub size: u64,
257
258    /// Error message if any
259    #[serde(default, skip_serializing_if = "Option::is_none")]
260    pub error: Option<String>,
261}
262
263/// Bucket count information
264#[derive(Debug, Clone, Serialize, Deserialize, Default)]
265pub struct BucketsInfo {
266    /// Number of buckets
267    #[serde(default)]
268    pub count: u64,
269
270    /// Error message if any
271    #[serde(default, skip_serializing_if = "Option::is_none")]
272    pub error: Option<String>,
273}
274
275/// Object count information
276#[derive(Debug, Clone, Serialize, Deserialize, Default)]
277pub struct ObjectsInfo {
278    /// Number of objects
279    #[serde(default)]
280    pub count: u64,
281
282    /// Error message if any
283    #[serde(default, skip_serializing_if = "Option::is_none")]
284    pub error: Option<String>,
285}
286
287/// Pool erasure set metrics returned by cluster information.
288#[derive(Debug, Clone, Serialize, Deserialize, Default)]
289#[serde(rename_all = "camelCase")]
290pub struct PoolErasureSetInfo {
291    /// Erasure set ID within the pool.
292    #[serde(default)]
293    pub id: i32,
294
295    /// Raw used capacity in bytes.
296    #[serde(default, rename = "rawUsage")]
297    pub raw_usage: u64,
298
299    /// Raw total capacity in bytes.
300    #[serde(default, rename = "rawCapacity")]
301    pub raw_capacity: u64,
302
303    /// Object data usage in bytes.
304    #[serde(default)]
305    pub usage: u64,
306
307    /// Number of objects in the set.
308    #[serde(default, rename = "objectsCount")]
309    pub objects_count: u64,
310
311    /// Number of versions in the set.
312    #[serde(default, rename = "versionsCount")]
313    pub versions_count: u64,
314
315    /// Number of delete markers in the set.
316    #[serde(default, rename = "deleteMarkersCount")]
317    pub delete_markers_count: u64,
318
319    /// Number of healing disks in the set.
320    #[serde(default, rename = "healDisks")]
321    pub heal_disks: i32,
322}
323
324/// Complete cluster information response
325#[derive(Debug, Clone, Serialize, Deserialize, Default)]
326#[serde(rename_all = "camelCase")]
327pub struct ClusterInfo {
328    /// Deployment mode (distributed, standalone)
329    #[serde(default)]
330    pub mode: Option<String>,
331
332    /// Domain names
333    #[serde(default)]
334    pub domain: Option<Vec<String>>,
335
336    /// Region
337    #[serde(default)]
338    pub region: Option<String>,
339
340    /// Deployment ID
341    #[serde(default, rename = "deploymentID")]
342    pub deployment_id: Option<String>,
343
344    /// Bucket information
345    #[serde(default)]
346    pub buckets: Option<BucketsInfo>,
347
348    /// Object information
349    #[serde(default)]
350    pub objects: Option<ObjectsInfo>,
351
352    /// Storage usage
353    #[serde(default)]
354    pub usage: Option<UsageInfo>,
355
356    /// Backend information
357    #[serde(default)]
358    pub backend: Option<BackendInfo>,
359
360    /// Server information
361    #[serde(default)]
362    pub servers: Option<Vec<ServerInfo>>,
363
364    /// Pool metrics keyed by pool and erasure set index.
365    #[serde(default)]
366    pub pools: Option<BTreeMap<i32, BTreeMap<i32, PoolErasureSetInfo>>>,
367}
368
369impl ClusterInfo {
370    /// Get the total number of online disks across all servers
371    pub fn online_disks(&self) -> usize {
372        self.servers
373            .as_ref()
374            .map(|servers| {
375                servers
376                    .iter()
377                    .flat_map(|s| &s.disks)
378                    .filter(|d| d.state == "online" || d.state == "ok")
379                    .count()
380            })
381            .unwrap_or(0)
382    }
383
384    /// Get the total number of offline disks across all servers
385    pub fn offline_disks(&self) -> usize {
386        self.servers
387            .as_ref()
388            .map(|servers| {
389                servers
390                    .iter()
391                    .flat_map(|s| &s.disks)
392                    .filter(|d| d.state == "offline")
393                    .count()
394            })
395            .unwrap_or(0)
396    }
397
398    /// Get total storage capacity in bytes
399    pub fn total_capacity(&self) -> u64 {
400        self.servers
401            .as_ref()
402            .map(|servers| {
403                servers
404                    .iter()
405                    .flat_map(|s| &s.disks)
406                    .map(|d| d.total_space)
407                    .sum()
408            })
409            .unwrap_or(0)
410    }
411
412    /// Get used storage in bytes
413    pub fn used_capacity(&self) -> u64 {
414        self.servers
415            .as_ref()
416            .map(|servers| {
417                servers
418                    .iter()
419                    .flat_map(|s| &s.disks)
420                    .map(|d| d.used_space)
421                    .sum()
422            })
423            .unwrap_or(0)
424    }
425}
426
427/// Heal operation mode
428#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
429#[serde(rename_all = "lowercase")]
430pub enum HealScanMode {
431    /// Normal scan (default)
432    #[default]
433    Normal,
434    /// Deep scan (slower but more thorough)
435    Deep,
436}
437
438impl std::fmt::Display for HealScanMode {
439    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
440        match self {
441            HealScanMode::Normal => write!(f, "normal"),
442            HealScanMode::Deep => write!(f, "deep"),
443        }
444    }
445}
446
447impl std::str::FromStr for HealScanMode {
448    type Err = String;
449
450    fn from_str(s: &str) -> Result<Self, Self::Err> {
451        match s.to_lowercase().as_str() {
452            "normal" => Ok(HealScanMode::Normal),
453            "deep" => Ok(HealScanMode::Deep),
454            _ => Err(format!("Invalid heal scan mode: {s}")),
455        }
456    }
457}
458
459/// Request to start a heal operation
460#[derive(Debug, Clone, Serialize, Deserialize, Default)]
461#[serde(rename_all = "camelCase")]
462pub struct HealStartRequest {
463    /// Bucket to heal (empty for all buckets)
464    #[serde(default, skip_serializing_if = "Option::is_none")]
465    pub bucket: Option<String>,
466
467    /// Object prefix to heal
468    #[serde(default, skip_serializing_if = "Option::is_none")]
469    pub prefix: Option<String>,
470
471    /// Scan mode
472    #[serde(default)]
473    pub scan_mode: HealScanMode,
474
475    /// Whether to remove dangling objects
476    #[serde(default)]
477    pub remove: bool,
478
479    /// Whether to recreate missing data
480    #[serde(default)]
481    pub recreate: bool,
482
483    /// Dry run mode (don't actually heal)
484    #[serde(default)]
485    pub dry_run: bool,
486}
487
488/// Request to inspect or stop a token-scoped heal task
489#[derive(Debug, Clone, Serialize, Deserialize)]
490#[serde(rename_all = "camelCase")]
491pub struct HealTaskRequest {
492    /// Bucket being healed
493    pub bucket: String,
494
495    /// Object prefix being healed
496    #[serde(default, skip_serializing_if = "Option::is_none")]
497    pub prefix: Option<String>,
498
499    /// Client token returned by the heal start request
500    pub client_token: String,
501}
502
503/// Information about a single heal drive
504#[derive(Debug, Clone, Serialize, Deserialize, Default)]
505pub struct HealDriveInfo {
506    /// Drive UUID
507    #[serde(default)]
508    pub uuid: String,
509
510    /// Drive endpoint
511    #[serde(default)]
512    pub endpoint: String,
513
514    /// Drive state
515    #[serde(default)]
516    pub state: String,
517}
518
519/// Result of a heal operation on a single item
520#[derive(Debug, Clone, Serialize, Deserialize, Default)]
521#[serde(rename_all = "camelCase")]
522pub struct HealResultItem {
523    /// Result index
524    #[serde(default, rename = "resultId")]
525    pub result_index: usize,
526
527    /// Type of item healed (bucket, object, metadata)
528    #[serde(default, rename = "type")]
529    pub item_type: String,
530
531    /// Bucket name
532    #[serde(default)]
533    pub bucket: String,
534
535    /// Object key
536    #[serde(default)]
537    pub object: String,
538
539    /// Version ID
540    #[serde(default, rename = "versionId")]
541    pub version_id: String,
542
543    /// Detail message
544    #[serde(default)]
545    pub detail: String,
546
547    /// Number of parity blocks
548    #[serde(default, rename = "parityBlocks")]
549    pub parity_blocks: usize,
550
551    /// Number of data blocks
552    #[serde(default, rename = "dataBlocks")]
553    pub data_blocks: usize,
554
555    /// Object size
556    #[serde(default, rename = "objectSize")]
557    pub object_size: u64,
558
559    /// Drive info before healing
560    #[serde(default)]
561    pub before: HealDriveInfos,
562
563    /// Drive info after healing
564    #[serde(default)]
565    pub after: HealDriveInfos,
566}
567
568/// Collection of heal drive infos
569#[derive(Debug, Clone, Serialize, Deserialize, Default)]
570pub struct HealDriveInfos {
571    /// Drive information
572    #[serde(default)]
573    pub drives: Vec<HealDriveInfo>,
574}
575
576/// Status of a heal operation
577#[derive(Debug, Clone, Serialize, Deserialize, Default)]
578#[serde(rename_all = "camelCase")]
579pub struct HealStatus {
580    /// Heal ID
581    #[serde(default)]
582    pub heal_id: String,
583
584    /// Whether healing is in progress
585    #[serde(default)]
586    pub healing: bool,
587
588    /// Task summary for token-scoped manual heal status
589    #[serde(default, skip_serializing_if = "Option::is_none")]
590    pub summary: Option<String>,
591
592    /// Task detail for token-scoped manual heal status
593    #[serde(default, skip_serializing_if = "Option::is_none")]
594    pub detail: Option<String>,
595
596    /// Current bucket being healed
597    #[serde(default)]
598    pub bucket: String,
599
600    /// Current object being healed
601    #[serde(default)]
602    pub object: String,
603
604    /// Current scan mode reported by background healing
605    #[serde(default, skip_serializing_if = "Option::is_none")]
606    pub scan_mode: Option<HealScanMode>,
607
608    /// Background heal scan cycle
609    #[serde(default)]
610    pub scan_cycle: u64,
611
612    /// Number of queued heal tasks
613    #[serde(default)]
614    pub heal_queue_length: u64,
615
616    /// Number of active heal tasks
617    #[serde(default)]
618    pub heal_active_tasks: u64,
619
620    /// Number of items scanned
621    #[serde(default)]
622    pub items_scanned: u64,
623
624    /// Number of items healed
625    #[serde(default)]
626    pub items_healed: u64,
627
628    /// Number of items failed
629    #[serde(default)]
630    pub items_failed: u64,
631
632    /// Bytes scanned
633    #[serde(default)]
634    pub bytes_scanned: u64,
635
636    /// Bytes healed
637    #[serde(default)]
638    pub bytes_healed: u64,
639
640    /// Start time
641    #[serde(default)]
642    pub started: Option<String>,
643
644    /// Last update time
645    #[serde(default)]
646    pub last_update: Option<String>,
647}
648
649/// Request targeting a storage pool by command line or numeric ID.
650#[derive(Debug, Clone, Serialize, Deserialize, Default)]
651#[serde(rename_all = "camelCase")]
652pub struct PoolTarget {
653    /// Pool command line, or zero-based pool ID when `by_id` is true.
654    pub pool: String,
655
656    /// Interpret `pool` as a zero-based pool ID.
657    #[serde(default)]
658    pub by_id: bool,
659}
660
661/// Status of a server pool.
662#[derive(Debug, Clone, Serialize, Deserialize, Default)]
663pub struct PoolStatus {
664    /// Zero-based pool ID.
665    #[serde(default)]
666    pub id: usize,
667
668    /// Pool command line used by the server process.
669    #[serde(default, rename = "cmdline")]
670    pub cmd_line: String,
671
672    /// Last pool metadata update timestamp.
673    #[serde(default, rename = "lastUpdate")]
674    pub last_update: String,
675
676    /// Pool lifecycle status.
677    #[serde(default)]
678    pub status: String,
679
680    /// Decommission operation status for this pool.
681    #[serde(default, rename = "decommissionStatus")]
682    pub decommission_status: String,
683
684    /// Rebalance operation status for this pool.
685    #[serde(default, rename = "rebalanceStatus")]
686    pub rebalance_status: String,
687
688    /// Total pool size in bytes.
689    #[serde(default, rename = "totalSize")]
690    pub total_size: u64,
691
692    /// Current free size in bytes.
693    #[serde(default, rename = "currentSize")]
694    pub current_size: u64,
695
696    /// Used pool size in bytes.
697    #[serde(default, rename = "usedSize")]
698    pub used_size: u64,
699
700    /// Used capacity ratio in the range 0.0..=1.0.
701    #[serde(default)]
702    pub used: f64,
703
704    /// Decommission status and progress for this pool.
705    #[serde(default, rename = "decommissionInfo")]
706    pub decommission: Option<PoolDecommissionInfo>,
707}
708
709/// Decommission status response.
710#[derive(Debug, Clone, Serialize, Deserialize, Default)]
711pub struct DecommissionStatus {
712    /// Per-pool decommission status.
713    #[serde(default)]
714    pub pools: Vec<DecommissionPoolStatus>,
715}
716
717/// Decommission operation status for a single pool.
718#[derive(Debug, Clone, Serialize, Deserialize, Default)]
719pub struct DecommissionPoolStatus {
720    /// Zero-based pool ID.
721    #[serde(default)]
722    pub id: usize,
723
724    /// Pool command line used by the server process.
725    #[serde(default, rename = "cmdline")]
726    pub cmd_line: String,
727
728    /// Decommission operation status for this pool.
729    #[serde(default)]
730    pub status: String,
731
732    /// Pool lifecycle status.
733    #[serde(default, rename = "poolStatus")]
734    pub pool_status: String,
735
736    /// Decommission state and progress for this pool.
737    #[serde(default, rename = "decommissionInfo")]
738    pub decommission: Option<PoolDecommissionInfo>,
739}
740
741/// Decommission state and progress for a server pool.
742#[derive(Debug, Clone, Serialize, Deserialize, Default)]
743pub struct PoolDecommissionInfo {
744    /// Decommission start timestamp.
745    #[serde(default, rename = "startTime")]
746    pub start_time: Option<String>,
747
748    /// Free bytes when decommission started.
749    #[serde(default, rename = "startSize")]
750    pub start_size: u64,
751
752    /// Total pool size in bytes.
753    #[serde(default, rename = "totalSize")]
754    pub total_size: u64,
755
756    /// Current free size in bytes.
757    #[serde(default, rename = "currentSize")]
758    pub current_size: u64,
759
760    /// Whether decommission completed.
761    #[serde(default)]
762    pub complete: bool,
763
764    /// Whether decommission failed.
765    #[serde(default)]
766    pub failed: bool,
767
768    /// Whether decommission was canceled.
769    #[serde(default)]
770    pub canceled: bool,
771
772    /// Whether decommission is queued.
773    #[serde(default)]
774    pub queued: bool,
775
776    /// Buckets waiting to be decommissioned.
777    #[serde(default, rename = "queuedBuckets")]
778    pub queued_buckets: Vec<String>,
779
780    /// Buckets already decommissioned.
781    #[serde(default, rename = "decommissionedBuckets")]
782    pub decommissioned_buckets: Vec<String>,
783
784    /// Current bucket.
785    #[serde(default)]
786    pub bucket: String,
787
788    /// Current prefix.
789    #[serde(default)]
790    pub prefix: String,
791
792    /// Current object.
793    #[serde(default)]
794    pub object: String,
795
796    /// Current decommission stage.
797    #[serde(default)]
798    pub stage: String,
799
800    /// Number of successfully decommissioned objects.
801    #[serde(default, rename = "objectsDecommissioned")]
802    pub objects_decommissioned: u64,
803
804    /// Number of objects that failed to decommission.
805    #[serde(default, rename = "objectsDecommissionedFailed")]
806    pub objects_decommissioned_failed: u64,
807
808    /// Bytes successfully moved off the pool.
809    #[serde(default, rename = "bytesDecommissioned")]
810    pub bytes_decommissioned: u64,
811
812    /// Bytes that failed to move off the pool.
813    #[serde(default, rename = "bytesDecommissionedFailed")]
814    pub bytes_decommissioned_failed: u64,
815
816    /// Reason why decommission is waiting.
817    #[serde(default, rename = "waitingReason")]
818    pub waiting_reason: Option<String>,
819}
820
821/// Response from starting a rebalance operation.
822#[derive(Debug, Clone, Serialize, Deserialize, Default)]
823pub struct RebalanceStartResult {
824    /// Rebalance operation ID.
825    #[serde(default)]
826    pub id: String,
827}
828
829/// Cluster-wide rebalance status.
830#[derive(Debug, Clone, Serialize, Deserialize, Default)]
831pub struct RebalanceStatus {
832    /// Rebalance operation ID.
833    #[serde(default)]
834    pub id: String,
835
836    /// Per-pool rebalance status.
837    #[serde(default)]
838    pub pools: Vec<RebalancePoolStatus>,
839
840    /// Timestamp when rebalance was stopped.
841    #[serde(default, rename = "stoppedAt")]
842    pub stopped_at: Option<String>,
843}
844
845/// Rebalance status for a single pool.
846#[derive(Debug, Clone, Serialize, Deserialize, Default)]
847pub struct RebalancePoolStatus {
848    /// Zero-based pool ID.
849    #[serde(default)]
850    pub id: usize,
851
852    /// Rebalance status for this pool.
853    #[serde(default)]
854    pub status: String,
855
856    /// Used capacity ratio in the range 0.0..=1.0.
857    #[serde(default)]
858    pub used: f64,
859
860    /// Last rebalance error, if any.
861    #[serde(default, rename = "lastError")]
862    pub last_error: Option<String>,
863
864    /// Cleanup warnings observed after this pool finishes rebalance.
865    #[serde(default, rename = "cleanupWarnings")]
866    pub cleanup_warnings: RebalanceCleanupWarnings,
867
868    /// Rebalance progress, if this pool is active.
869    #[serde(default)]
870    pub progress: Option<RebalancePoolProgress>,
871}
872
873/// Cleanup warnings recorded for a rebalanced pool.
874#[derive(Debug, Clone, Serialize, Deserialize, Default)]
875pub struct RebalanceCleanupWarnings {
876    /// Number of cleanup warnings observed.
877    #[serde(default)]
878    pub count: u64,
879
880    /// Last cleanup warning message.
881    #[serde(default, rename = "lastMsg")]
882    pub last_message: Option<String>,
883
884    /// Bucket associated with the last cleanup warning.
885    #[serde(default, rename = "lastBucket")]
886    pub last_bucket: Option<String>,
887
888    /// Object associated with the last cleanup warning.
889    #[serde(default, rename = "lastObject")]
890    pub last_object: Option<String>,
891
892    /// Timestamp of the last cleanup warning.
893    #[serde(default, rename = "lastAt")]
894    pub last_at: Option<String>,
895}
896
897/// Rebalance progress for a single pool.
898#[derive(Debug, Clone, Serialize, Deserialize, Default)]
899pub struct RebalancePoolProgress {
900    /// Number of objects moved.
901    #[serde(default, rename = "objects")]
902    pub num_objects: u64,
903
904    /// Number of object versions moved.
905    #[serde(default, rename = "versions")]
906    pub num_versions: u64,
907
908    /// Number of bytes moved.
909    #[serde(default)]
910    pub bytes: u64,
911
912    /// Number of buckets remaining.
913    #[serde(default, rename = "remainingBuckets")]
914    pub remaining_buckets: usize,
915
916    /// Current bucket.
917    #[serde(default)]
918    pub bucket: String,
919
920    /// Current object.
921    #[serde(default)]
922    pub object: String,
923
924    /// Elapsed seconds.
925    #[serde(default)]
926    pub elapsed: u64,
927
928    /// Estimated seconds remaining.
929    #[serde(default)]
930    pub eta: u64,
931}
932
933#[cfg(test)]
934mod tests {
935    use super::*;
936
937    #[test]
938    fn test_backend_type_display() {
939        assert_eq!(BackendType::Fs.to_string(), "FS");
940        assert_eq!(BackendType::Erasure.to_string(), "Erasure");
941    }
942
943    #[test]
944    fn test_heal_scan_mode_display() {
945        assert_eq!(HealScanMode::Normal.to_string(), "normal");
946        assert_eq!(HealScanMode::Deep.to_string(), "deep");
947    }
948
949    #[test]
950    fn test_heal_scan_mode_from_str() {
951        assert_eq!(
952            "normal".parse::<HealScanMode>().unwrap(),
953            HealScanMode::Normal
954        );
955        assert_eq!("deep".parse::<HealScanMode>().unwrap(), HealScanMode::Deep);
956        assert!("invalid".parse::<HealScanMode>().is_err());
957    }
958
959    #[test]
960    fn test_cluster_info_default() {
961        let info = ClusterInfo::default();
962        assert!(info.mode.is_none());
963        assert!(info.servers.is_none());
964        assert_eq!(info.online_disks(), 0);
965        assert_eq!(info.offline_disks(), 0);
966    }
967
968    #[test]
969    fn test_cluster_info_disk_counts() {
970        let info = ClusterInfo {
971            servers: Some(vec![ServerInfo {
972                disks: vec![
973                    DiskInfo {
974                        state: "online".to_string(),
975                        ..Default::default()
976                    },
977                    DiskInfo {
978                        state: "online".to_string(),
979                        ..Default::default()
980                    },
981                    DiskInfo {
982                        state: "offline".to_string(),
983                        ..Default::default()
984                    },
985                ],
986                ..Default::default()
987            }]),
988            ..Default::default()
989        };
990
991        assert_eq!(info.online_disks(), 2);
992        assert_eq!(info.offline_disks(), 1);
993    }
994
995    #[test]
996    fn test_cluster_info_capacity() {
997        let info = ClusterInfo {
998            servers: Some(vec![ServerInfo {
999                disks: vec![
1000                    DiskInfo {
1001                        total_space: 1000,
1002                        used_space: 300,
1003                        ..Default::default()
1004                    },
1005                    DiskInfo {
1006                        total_space: 2000,
1007                        used_space: 500,
1008                        ..Default::default()
1009                    },
1010                ],
1011                ..Default::default()
1012            }]),
1013            ..Default::default()
1014        };
1015
1016        assert_eq!(info.total_capacity(), 3000);
1017        assert_eq!(info.used_capacity(), 800);
1018    }
1019
1020    #[test]
1021    fn test_disk_info_default() {
1022        let disk = DiskInfo::default();
1023        assert!(disk.endpoint.is_empty());
1024        assert!(!disk.healing);
1025        assert!(!disk.scanning);
1026        assert_eq!(disk.total_space, 0);
1027    }
1028
1029    #[test]
1030    fn test_disk_info_deserializes_snake_case_location_indexes() {
1031        let json = r#"{"pool_index":1,"set_index":2,"disk_index":3}"#;
1032
1033        let disk: DiskInfo = serde_json::from_str(json).unwrap();
1034
1035        assert_eq!(disk.pool_index, 1);
1036        assert_eq!(disk.set_index, 2);
1037        assert_eq!(disk.disk_index, 3);
1038    }
1039
1040    #[test]
1041    fn test_server_info_default() {
1042        let server = ServerInfo::default();
1043        assert!(server.state.is_empty());
1044        assert!(server.endpoint.is_empty());
1045        assert_eq!(server.uptime, 0);
1046    }
1047
1048    #[test]
1049    fn test_heal_start_request_default() {
1050        let req = HealStartRequest::default();
1051        assert!(req.bucket.is_none());
1052        assert!(req.prefix.is_none());
1053        assert_eq!(req.scan_mode, HealScanMode::Normal);
1054        assert!(!req.remove);
1055        assert!(!req.dry_run);
1056    }
1057
1058    #[test]
1059    fn test_heal_status_default() {
1060        let status = HealStatus::default();
1061        assert!(status.heal_id.is_empty());
1062        assert!(!status.healing);
1063        assert!(status.scan_mode.is_none());
1064        assert_eq!(status.scan_cycle, 0);
1065        assert_eq!(status.heal_queue_length, 0);
1066        assert_eq!(status.heal_active_tasks, 0);
1067        assert_eq!(status.items_scanned, 0);
1068    }
1069
1070    #[test]
1071    fn test_pool_status_deserialization() {
1072        let json = r#"{"id":1,"cmdline":"/data/pool1/disk{1...4}","lastUpdate":"2026-05-06T00:00:00Z","status":"decommissioning","decommissionStatus":"running","rebalanceStatus":"none","totalSize":1000,"currentSize":600,"usedSize":400,"used":0.4,"decommissionInfo":{"startTime":"2026-05-06T00:00:01Z","startSize":100,"totalSize":1000,"currentSize":600,"complete":false,"failed":false,"canceled":false,"queued":true,"queuedBuckets":["bucket-a"],"decommissionedBuckets":["bucket-b"],"bucket":"bucket-a","prefix":"","object":"object.txt","stage":"migrate_object","objectsDecommissioned":2,"objectsDecommissionedFailed":1,"bytesDecommissioned":128,"bytesDecommissionedFailed":64,"waitingReason":"queued"}}"#;
1073
1074        let status: PoolStatus = serde_json::from_str(json).unwrap();
1075
1076        assert_eq!(status.id, 1);
1077        assert_eq!(status.cmd_line, "/data/pool1/disk{1...4}");
1078        assert_eq!(status.status, "decommissioning");
1079        assert_eq!(status.decommission_status, "running");
1080        assert_eq!(status.rebalance_status, "none");
1081        assert_eq!(status.used_size, 400);
1082        let info = status.decommission.expect("decommission info exists");
1083        assert!(info.queued);
1084        assert_eq!(info.queued_buckets, vec!["bucket-a"]);
1085        assert_eq!(info.bucket, "bucket-a");
1086        assert_eq!(info.object, "object.txt");
1087        assert_eq!(info.waiting_reason.as_deref(), Some("queued"));
1088        assert_eq!(info.objects_decommissioned, 2);
1089        assert_eq!(info.bytes_decommissioned_failed, 64);
1090    }
1091
1092    #[test]
1093    fn test_decommission_status_deserialization() {
1094        let json = r#"{"pools":[{"id":2,"cmdline":"/data/pool2/disk{1...4}","status":"failed","poolStatus":"blocked","decommissionInfo":{"failed":true,"totalSize":1000,"currentSize":900}}]}"#;
1095
1096        let status: DecommissionStatus = serde_json::from_str(json).unwrap();
1097
1098        assert_eq!(status.pools.len(), 1);
1099        assert_eq!(status.pools[0].id, 2);
1100        assert_eq!(status.pools[0].status, "failed");
1101        assert_eq!(status.pools[0].pool_status, "blocked");
1102        assert!(
1103            status.pools[0]
1104                .decommission
1105                .as_ref()
1106                .is_some_and(|info| info.failed)
1107        );
1108    }
1109
1110    #[test]
1111    fn test_rebalance_status_deserialization() {
1112        let json = r#"{"id":"rebalance-1","pools":[{"id":0,"status":"Started","used":0.5,"lastError":null,"cleanupWarnings":{"count":1,"lastMsg":"cleanup warning","lastBucket":"bucket","lastObject":"object","lastAt":"2026-06-12T00:00:00Z"},"progress":{"objects":3,"versions":4,"bytes":1024,"remainingBuckets":2,"bucket":"bucket","object":"object","elapsed":10,"eta":20}}],"stoppedAt":null}"#;
1113
1114        let status: RebalanceStatus = serde_json::from_str(json).unwrap();
1115
1116        assert_eq!(status.id, "rebalance-1");
1117        assert_eq!(status.pools.len(), 1);
1118        assert_eq!(status.pools[0].used, 0.5);
1119        assert_eq!(status.pools[0].cleanup_warnings.count, 1);
1120        assert_eq!(
1121            status.pools[0].cleanup_warnings.last_message.as_deref(),
1122            Some("cleanup warning")
1123        );
1124        let progress = status.pools[0]
1125            .progress
1126            .as_ref()
1127            .expect("progress should exist");
1128        assert_eq!(progress.num_objects, 3);
1129        assert_eq!(progress.remaining_buckets, 2);
1130    }
1131
1132    #[test]
1133    fn test_rebalance_status_defaults_cleanup_warnings() {
1134        let json = r#"{"id":"rebalance-1","pools":[{"id":0,"status":"Completed","used":0.5,"lastError":null,"progress":null}],"stoppedAt":null}"#;
1135
1136        let status: RebalanceStatus = serde_json::from_str(json).unwrap();
1137
1138        assert_eq!(status.pools[0].cleanup_warnings.count, 0);
1139        assert_eq!(status.pools[0].cleanup_warnings.last_message, None);
1140    }
1141
1142    #[test]
1143    fn test_serialization() {
1144        let info = ClusterInfo {
1145            mode: Some("distributed".to_string()),
1146            deployment_id: Some("test-123".to_string()),
1147            ..Default::default()
1148        };
1149
1150        let json = serde_json::to_string(&info).unwrap();
1151        assert!(json.contains("distributed"));
1152        assert!(json.contains("test-123"));
1153
1154        let deserialized: ClusterInfo = serde_json::from_str(&json).unwrap();
1155        assert_eq!(deserialized.mode, Some("distributed".to_string()));
1156    }
1157}