Skip to main content

openstack_keystone_distributed_storage/proto_impl/
impl_membership.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5//     http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12//
13// SPDX-License-Identifier: Apache-2.0
14use std::collections::BTreeMap;
15use std::collections::BTreeSet;
16
17use openraft::Membership;
18
19use crate::StoreError;
20use crate::TypeConfig;
21use crate::pb;
22
23impl TryFrom<pb::raft::Membership> for Membership<TypeConfig> {
24    type Error = StoreError;
25    fn try_from(value: pb::raft::Membership) -> Result<Self, Self::Error> {
26        let mut configs = vec![];
27        for c in value.configs {
28            let config: BTreeSet<u64> = c.node_ids.keys().copied().collect();
29            configs.push(config);
30        }
31        let nodes = value.nodes;
32        Ok(Membership::new(configs, nodes)?)
33    }
34}
35
36impl From<Membership<TypeConfig>> for pb::raft::Membership {
37    fn from(value: Membership<TypeConfig>) -> Self {
38        let mut configs = vec![];
39        for c in value.get_joint_config() {
40            let mut node_ids = BTreeMap::new();
41            for nid in c.iter() {
42                node_ids.insert(*nid, ());
43            }
44            configs.push(pb::raft::NodeIdSet { node_ids });
45        }
46        let nodes = value.nodes().map(|(nid, n)| (*nid, n.clone())).collect();
47        pb::raft::Membership { configs, nodes }
48    }
49}