Skip to main content

openstack_keystone_distributed_storage/proto_impl/
impl_entry.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::fmt;
15
16use openraft::EntryPayload;
17use openraft::Membership;
18use openraft::alias::LogIdOf;
19use openraft::entry::RaftEntry;
20use openraft::entry::RaftPayload;
21
22use crate::TypeConfig;
23use crate::protobuf as pb;
24
25impl fmt::Display for pb::raft::Entry {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        write!(f, "Entry{{term={},index={}}}", self.term, self.index)
28    }
29}
30
31impl RaftPayload<TypeConfig> for pb::raft::Entry {
32    fn get_membership(&self) -> Option<Membership<TypeConfig>> {
33        // NOTE: Converting the membership is fallible. This interface does not allow us
34        // to handle it properly, so the conversion error is treated as `None`.
35        self.membership
36            .clone()
37            .map(TryInto::try_into)
38            .transpose()
39            .unwrap_or(None)
40    }
41}
42
43impl RaftEntry<TypeConfig> for pb::raft::Entry {
44    fn new(log_id: LogIdOf<TypeConfig>, payload: EntryPayload<TypeConfig>) -> Self {
45        let mut app_data = None;
46        let mut membership = None;
47        match payload {
48            EntryPayload::Blank => {}
49            EntryPayload::Normal(data) => app_data = Some(data),
50            EntryPayload::Membership(m) => membership = Some(m.into()),
51        }
52
53        Self {
54            term: log_id.leader_id,
55            index: log_id.index,
56            app_data,
57            membership,
58        }
59    }
60
61    fn log_id_parts(&self) -> (&u64, u64) {
62        (&self.term, self.index)
63    }
64
65    fn set_log_id(&mut self, new: LogIdOf<TypeConfig>) {
66        self.term = new.leader_id;
67        self.index = new.index;
68    }
69}