fleetfs_raft/
status.rs

1// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
2
3// Copyright 2015 The etcd Authors
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9//     http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17use crate::eraftpb::HardState;
18
19use crate::raft::{Raft, SoftState, StateRole};
20use crate::storage::Storage;
21use crate::ProgressTracker;
22
23/// Represents the current status of the raft
24#[derive(Default)]
25pub struct Status<'a> {
26    /// The ID of the current node.
27    pub id: u64,
28    /// The hardstate of the raft, representing voted state.
29    pub hs: HardState,
30    /// The softstate of the raft, representing proposed state.
31    pub ss: SoftState,
32    /// The index of the last entry to have been applied.
33    pub applied: u64,
34    /// The progress towards catching up and applying logs.
35    pub progress: Option<&'a ProgressTracker>,
36}
37
38impl<'a> Status<'a> {
39    /// Gets a copy of the current raft status.
40    pub fn new<T: Storage>(raft: &'a Raft<T>) -> Status<'a> {
41        let mut s = Status {
42            id: raft.id,
43            ..Default::default()
44        };
45        s.hs = raft.hard_state();
46        s.ss = raft.soft_state();
47        s.applied = raft.raft_log.applied;
48        if s.ss.raft_state == StateRole::Leader {
49            s.progress = Some(raft.prs());
50        }
51        s
52    }
53}