Skip to content

Commit deeb67b

Browse files
committed
Refactor: move engine::Command to standalone file
1 parent 4911180 commit deeb67b

File tree

3 files changed

+103
-90
lines changed

3 files changed

+103
-90
lines changed

openraft/src/engine/command.rs

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
use std::ops::Range;
2+
3+
use crate::raft::VoteRequest;
4+
use crate::LogId;
5+
use crate::Membership;
6+
use crate::MetricsChangeFlags;
7+
use crate::NodeId;
8+
use crate::ServerState;
9+
use crate::Vote;
10+
11+
/// Commands to send to `RaftRuntime` to execute, to update the application state.
12+
#[derive(Debug, Clone, PartialEq, Eq)]
13+
pub(crate) enum Command<NID: NodeId> {
14+
// Update server state, e.g., Leader, Follower etc.
15+
// TODO: consider remove this variant. A runtime does not need to know about this. It is only meant for metrics
16+
// report.
17+
UpdateServerState {
18+
server_state: ServerState,
19+
},
20+
21+
// Append a `range` of entries in the input buffer.
22+
AppendInputEntries {
23+
range: Range<usize>,
24+
},
25+
26+
// Commit entries that are already in the store, upto `upto`, inclusive.
27+
// And send applied result to the client that proposed the entry.
28+
Commit {
29+
upto: LogId<NID>,
30+
},
31+
32+
// Replicate a `range` of entries in the input buffer.
33+
ReplicateInputEntries {
34+
range: Range<usize>,
35+
},
36+
37+
// Membership config changed, need to update replication stream etc.
38+
UpdateMembership {
39+
membership: Membership<NID>,
40+
},
41+
42+
// Move the cursor pointing to an entry in the input buffer.
43+
MoveInputCursorBy {
44+
n: usize,
45+
},
46+
47+
// Save vote to storage
48+
SaveVote {
49+
vote: Vote<NID>,
50+
},
51+
52+
// Send vote to all other members
53+
SendVote {
54+
vote_req: VoteRequest<NID>,
55+
},
56+
57+
// Install a timer to trigger an election after some `timeout` which is decided by the runtime.
58+
// An already installed timer should be cleared.
59+
InstallElectionTimer {},
60+
61+
//
62+
// --- Draft unimplemented commands:
63+
//
64+
65+
// TODO:
66+
#[allow(dead_code)]
67+
PurgeAppliedLog {
68+
upto: LogId<NID>,
69+
},
70+
// TODO:
71+
#[allow(dead_code)]
72+
DeleteConflictLog {
73+
since: LogId<NID>,
74+
},
75+
76+
// TODO:
77+
#[allow(dead_code)]
78+
BuildSnapshot {},
79+
}
80+
81+
impl<NID: NodeId> Command<NID> {
82+
/// Update the flag of the metrics that needs to be updated when this command is executed.
83+
pub(crate) fn update_metrics_flags(&self, flags: &mut MetricsChangeFlags) {
84+
match &self {
85+
Command::UpdateServerState { .. } => flags.set_cluster_changed(),
86+
Command::AppendInputEntries { .. } => flags.set_data_changed(),
87+
Command::Commit { .. } => flags.set_data_changed(),
88+
Command::ReplicateInputEntries { .. } => {}
89+
Command::UpdateMembership { .. } => flags.set_cluster_changed(),
90+
Command::MoveInputCursorBy { .. } => {}
91+
Command::SaveVote { .. } => flags.set_data_changed(),
92+
Command::SendVote { .. } => {}
93+
Command::InstallElectionTimer { .. } => {}
94+
Command::PurgeAppliedLog { .. } => flags.set_data_changed(),
95+
Command::DeleteConflictLog { .. } => flags.set_data_changed(),
96+
Command::BuildSnapshot { .. } => flags.set_data_changed(),
97+
}
98+
}
99+
}

openraft/src/engine/engine.rs

+2-89
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::collections::BTreeSet;
2-
use std::ops::Range;
32
use std::sync::Arc;
43

54
use maplit::btreeset;
65

76
use crate::core::ServerState;
7+
use crate::engine::Command;
88
use crate::entry::RaftEntry;
99
use crate::error::InitializeError;
1010
use crate::error::NotAMembershipEntry;
@@ -52,76 +52,6 @@ pub(crate) struct Engine<NID: NodeId> {
5252
pub(crate) commands: Vec<Command<NID>>,
5353
}
5454

55-
/// Commands to send to `RaftRuntime` to execute, to update the application state.
56-
#[derive(Debug, Clone, PartialEq, Eq)]
57-
pub(crate) enum Command<NID: NodeId> {
58-
// Update server state, e.g., Leader, Follower etc.
59-
// TODO: consider remove this variant. A runtime does not need to know about this. It is only meant for metrics
60-
// report.
61-
UpdateServerState {
62-
server_state: ServerState,
63-
},
64-
65-
// Append a `range` of entries in the input buffer.
66-
AppendInputEntries {
67-
range: Range<usize>,
68-
},
69-
70-
// Commit entries that are already in the store, upto `upto`, inclusive.
71-
// And send applied result to the client that proposed the entry.
72-
Commit {
73-
upto: LogId<NID>,
74-
},
75-
76-
// Replicate a `range` of entries in the input buffer.
77-
ReplicateInputEntries {
78-
range: Range<usize>,
79-
},
80-
81-
// Membership config changed, need to update replication stream etc.
82-
UpdateMembership {
83-
membership: Membership<NID>,
84-
},
85-
86-
// Move the cursor pointing to an entry in the input buffer.
87-
MoveInputCursorBy {
88-
n: usize,
89-
},
90-
91-
// Save vote to storage
92-
SaveVote {
93-
vote: Vote<NID>,
94-
},
95-
96-
// Send vote to all other members
97-
SendVote {
98-
vote_req: VoteRequest<NID>,
99-
},
100-
101-
// Install a timer to trigger an election after some `timeout` which is decided by the runtime.
102-
// An already installed timer should be cleared.
103-
InstallElectionTimer {},
104-
105-
//
106-
// --- Draft unimplemented commands:
107-
//
108-
109-
// TODO:
110-
#[allow(dead_code)]
111-
PurgeAppliedLog {
112-
upto: LogId<NID>,
113-
},
114-
// TODO:
115-
#[allow(dead_code)]
116-
DeleteConflictLog {
117-
since: LogId<NID>,
118-
},
119-
120-
// TODO:
121-
#[allow(dead_code)]
122-
BuildSnapshot {},
123-
}
124-
12555
impl<NID: NodeId> Engine<NID> {
12656
pub(crate) fn new(id: NID, init_state: &RaftState<NID>) -> Self {
12757
Self {
@@ -347,7 +277,6 @@ impl<NID: NodeId> Engine<NID> {
347277
// // --- raft protocol API ---
348278
//
349279
// //
350-
// pub(crate) fn handle_vote() {}
351280
// pub(crate) fn handle_append_entries() {}
352281
// pub(crate) fn handle_install_snapshot() {}
353282
//
@@ -430,23 +359,7 @@ impl<NID: NodeId> Engine<NID> {
430359
}
431360

432361
fn push_command(&mut self, cmd: Command<NID>) {
433-
// Update flags for metrics that need to update, according to the queued commands.
434-
let f = &mut self.metrics_flags;
435-
match &cmd {
436-
Command::UpdateServerState { .. } => f.set_cluster_changed(),
437-
Command::AppendInputEntries { .. } => f.set_data_changed(),
438-
Command::Commit { .. } => f.set_data_changed(),
439-
Command::ReplicateInputEntries { .. } => {}
440-
Command::UpdateMembership { .. } => f.set_cluster_changed(),
441-
Command::MoveInputCursorBy { .. } => {}
442-
Command::SaveVote { .. } => f.set_data_changed(),
443-
Command::SendVote { .. } => {}
444-
Command::InstallElectionTimer { .. } => {}
445-
Command::PurgeAppliedLog { .. } => f.set_data_changed(),
446-
Command::DeleteConflictLog { .. } => f.set_data_changed(),
447-
Command::BuildSnapshot { .. } => f.set_data_changed(),
448-
}
449-
362+
cmd.update_metrics_flags(&mut self.metrics_flags);
450363
self.commands.push(cmd)
451364
}
452365
}

openraft/src/engine/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#[allow(clippy::module_inception)]
22
mod engine;
33

4+
mod command;
45
#[cfg(test)]
56
mod elect_test;
67
#[cfg(test)]
@@ -15,6 +16,6 @@ mod log_id_list_test;
1516
#[cfg(test)]
1617
mod testing;
1718

18-
pub(crate) use engine::Command;
19+
pub(crate) use command::Command;
1920
pub(crate) use engine::Engine;
2021
pub use log_id_list::LogIdList;

0 commit comments

Comments
 (0)