Skip to main content

rustfs_storage_api/
admin.rs

1// Copyright 2024 RustFS Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::fmt::Debug;
16
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
18pub struct DiskSetSelector {
19    pub pool_idx: usize,
20    pub set_idx: usize,
21}
22
23impl DiskSetSelector {
24    pub const fn new(pool_idx: usize, set_idx: usize) -> Self {
25        Self { pool_idx, set_idx }
26    }
27}
28
29#[async_trait::async_trait]
30pub trait StorageAdminApi: Send + Sync + Debug {
31    type BackendInfo: Send + Sync + Debug + 'static;
32    type StorageInfo: Send + Sync + Debug + 'static;
33    type Disk: Send + Sync + Debug + 'static;
34    type Error: std::error::Error + Send + Sync + 'static;
35
36    async fn backend_info(&self) -> Self::BackendInfo;
37    async fn storage_info(&self) -> Self::StorageInfo;
38    async fn local_storage_info(&self) -> Self::StorageInfo;
39    async fn disk_set_inventory(&self, selector: DiskSetSelector) -> Result<Vec<Option<Self::Disk>>, Self::Error>;
40    fn set_drive_counts(&self) -> Vec<usize>;
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46
47    #[derive(Debug)]
48    struct TestError;
49
50    impl std::fmt::Display for TestError {
51        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52            f.write_str("test storage admin error")
53        }
54    }
55
56    impl std::error::Error for TestError {}
57
58    #[derive(Debug, Clone, PartialEq, Eq)]
59    struct TestBackendInfo {
60        drives_per_set: Vec<usize>,
61    }
62
63    #[derive(Debug, Clone, PartialEq, Eq)]
64    struct TestStorageInfo {
65        disk_count: usize,
66    }
67
68    #[derive(Debug, Clone, PartialEq, Eq)]
69    struct TestDisk {
70        uuid: String,
71        disk_index: usize,
72    }
73
74    #[derive(Debug)]
75    struct FakeStorageAdmin;
76
77    #[async_trait::async_trait]
78    impl StorageAdminApi for FakeStorageAdmin {
79        type BackendInfo = TestBackendInfo;
80        type StorageInfo = TestStorageInfo;
81        type Disk = TestDisk;
82        type Error = TestError;
83
84        async fn backend_info(&self) -> Self::BackendInfo {
85            TestBackendInfo { drives_per_set: vec![2] }
86        }
87
88        async fn storage_info(&self) -> Self::StorageInfo {
89            TestStorageInfo { disk_count: 2 }
90        }
91
92        async fn local_storage_info(&self) -> Self::StorageInfo {
93            self.storage_info().await
94        }
95
96        async fn disk_set_inventory(&self, selector: DiskSetSelector) -> Result<Vec<Option<Self::Disk>>, Self::Error> {
97            assert_eq!(selector, DiskSetSelector::new(0, 0));
98            Ok(vec![Some(disk("disk-0", 0)), None])
99        }
100
101        fn set_drive_counts(&self) -> Vec<usize> {
102            vec![2]
103        }
104    }
105
106    fn disk(uuid: &str, disk_index: usize) -> TestDisk {
107        TestDisk {
108            uuid: uuid.to_owned(),
109            disk_index,
110        }
111    }
112
113    #[tokio::test]
114    async fn fake_storage_admin_api_exposes_disk_inventory_contract() {
115        let api = FakeStorageAdmin;
116
117        let disks = api
118            .disk_set_inventory(DiskSetSelector::new(0, 0))
119            .await
120            .expect("fake disk inventory should be available");
121
122        assert_eq!(api.backend_info().await.drives_per_set, vec![2]);
123        assert_eq!(api.storage_info().await.disk_count, 2);
124        assert_eq!(api.set_drive_counts(), vec![2]);
125        assert_eq!(disks.len(), 2);
126        assert_eq!(disks[0].as_ref().map(|disk| disk.uuid.as_str()), Some("disk-0"));
127        assert!(disks[1].is_none());
128    }
129}