2
2
3
3
mod message;
4
4
mod raft_inner;
5
+ mod trigger;
6
+
7
+ pub ( in crate :: raft) mod core_state;
5
8
6
9
use std:: fmt:: Debug ;
7
10
use std:: marker:: PhantomData ;
8
11
use std:: sync:: atomic:: Ordering ;
9
12
use std:: sync:: Arc ;
10
13
use std:: time:: Duration ;
11
14
15
+ use core_state:: CoreState ;
12
16
use maplit:: btreemap;
13
17
pub use message:: AppendEntriesRequest ;
14
18
pub use message:: AppendEntriesResponse ;
@@ -17,9 +21,6 @@ pub use message::InstallSnapshotRequest;
17
21
pub use message:: InstallSnapshotResponse ;
18
22
pub use message:: VoteRequest ;
19
23
pub use message:: VoteResponse ;
20
- use tokio:: io:: AsyncRead ;
21
- use tokio:: io:: AsyncSeek ;
22
- use tokio:: io:: AsyncWrite ;
23
24
use tokio:: sync:: mpsc;
24
25
use tokio:: sync:: oneshot;
25
26
use tokio:: sync:: watch;
@@ -31,16 +32,13 @@ use tracing::Level;
31
32
use crate :: config:: Config ;
32
33
use crate :: config:: RuntimeConfig ;
33
34
use crate :: core:: command_state:: CommandState ;
34
- use crate :: core:: raft_msg:: external_command:: ExternalCommand ;
35
35
use crate :: core:: raft_msg:: RaftMsg ;
36
36
use crate :: core:: replication_lag;
37
37
use crate :: core:: sm;
38
38
use crate :: core:: RaftCore ;
39
39
use crate :: core:: Tick ;
40
40
use crate :: engine:: Engine ;
41
41
use crate :: engine:: EngineConfig ;
42
- use crate :: entry:: FromAppData ;
43
- use crate :: entry:: RaftEntry ;
44
42
use crate :: error:: CheckIsLeaderError ;
45
43
use crate :: error:: ClientWriteError ;
46
44
use crate :: error:: Fatal ;
@@ -51,69 +49,19 @@ use crate::membership::IntoNodes;
51
49
use crate :: metrics:: RaftMetrics ;
52
50
use crate :: metrics:: Wait ;
53
51
use crate :: network:: RaftNetworkFactory ;
54
- use crate :: node:: Node ;
55
52
use crate :: raft:: raft_inner:: RaftInner ;
53
+ use crate :: raft:: trigger:: Trigger ;
56
54
use crate :: storage:: RaftLogStorage ;
57
55
use crate :: storage:: RaftStateMachine ;
58
- use crate :: AppData ;
59
- use crate :: AppDataResponse ;
60
56
use crate :: AsyncRuntime ;
61
57
use crate :: ChangeMembers ;
62
58
use crate :: LogId ;
63
59
use crate :: LogIdOptionExt ;
64
60
use crate :: MessageSummary ;
65
- use crate :: NodeId ;
66
- use crate :: OptionalSend ;
67
61
use crate :: RaftState ;
62
+ pub use crate :: RaftTypeConfig ;
68
63
use crate :: StorageHelper ;
69
64
70
- /// Configuration of types used by the [`Raft`] core engine.
71
- ///
72
- /// The (empty) implementation structure defines request/response types, node ID type
73
- /// and the like. Refer to the documentation of associated types for more information.
74
- ///
75
- /// ## Note
76
- ///
77
- /// Since Rust cannot automatically infer traits for various inner types using this config
78
- /// type as a parameter, this trait simply uses all the traits required for various types
79
- /// as its supertraits as a workaround. To ease the declaration, the macro
80
- /// `declare_raft_types` is provided, which can be used to declare the type easily.
81
- ///
82
- /// Example:
83
- /// ```ignore
84
- /// openraft::declare_raft_types!(
85
- /// /// Declare the type configuration for `MemStore`.
86
- /// pub Config: D = ClientRequest, R = ClientResponse, NodeId = MemNodeId
87
- /// );
88
- /// ```
89
- pub trait RaftTypeConfig :
90
- Sized + Send + Sync + Debug + Clone + Copy + Default + Eq + PartialEq + Ord + PartialOrd + ' static
91
- {
92
- /// Application-specific request data passed to the state machine.
93
- type D : AppData ;
94
-
95
- /// Application-specific response data returned by the state machine.
96
- type R : AppDataResponse ;
97
-
98
- /// A Raft node's ID.
99
- type NodeId : NodeId ;
100
-
101
- /// Raft application level node data
102
- type Node : Node ;
103
-
104
- /// Raft log entry, which can be built from an AppData.
105
- type Entry : RaftEntry < Self :: NodeId , Self :: Node > + FromAppData < Self :: D > ;
106
-
107
- /// Snapshot data for exposing a snapshot for reading & writing.
108
- ///
109
- /// See the [storage chapter of the guide](https://datafuselabs.github.io/openraft/getting-started.html#implement-raftstorage)
110
- /// for details on where and how this is used.
111
- type SnapshotData : AsyncRead + AsyncWrite + AsyncSeek + OptionalSend + Sync + Unpin + ' static ;
112
-
113
- /// Asynchronous runtime type.
114
- type AsyncRuntime : AsyncRuntime ;
115
- }
116
-
117
65
/// Define types for a Raft type configuration.
118
66
///
119
67
/// Since Rust has some limitations when deriving traits for types with generic arguments
@@ -125,8 +73,14 @@ pub trait RaftTypeConfig:
125
73
/// Example:
126
74
/// ```ignore
127
75
/// openraft::declare_raft_types!(
128
- /// /// Declare the type configuration for `MemStore`.
129
- /// pub Config: D = ClientRequest, R = ClientResponse, NodeId = MemNodeId
76
+ /// pub Config:
77
+ /// D = ClientRequest,
78
+ /// R = ClientResponse,
79
+ /// NodeId = u64,
80
+ /// Node = openraft::BasicNode,
81
+ /// Entry = openraft::Entry<TypeConfig>,
82
+ /// SnapshotData = Cursor<Vec<u8>>,
83
+ /// AsyncRuntime = openraft::TokioRuntime,
130
84
/// );
131
85
/// ```
132
86
#[ macro_export]
@@ -146,19 +100,6 @@ macro_rules! declare_raft_types {
146
100
} ;
147
101
}
148
102
149
- /// The running state of RaftCore
150
- enum CoreState < NID , A >
151
- where
152
- NID : NodeId ,
153
- A : AsyncRuntime ,
154
- {
155
- /// The RaftCore task is still running.
156
- Running ( A :: JoinHandle < Result < ( ) , Fatal < NID > > > ) ,
157
-
158
- /// The RaftCore task has finished. The return value of the task is stored.
159
- Done ( Result < ( ) , Fatal < NID > > ) ,
160
- }
161
-
162
103
/// The Raft API.
163
104
///
164
105
/// This type implements the full Raft spec, and is the interface to a running Raft node.
@@ -337,46 +278,39 @@ where
337
278
self . inner . runtime_config . enable_elect . store ( enabled, Ordering :: Relaxed ) ;
338
279
}
339
280
340
- /// Trigger election at once and return at once .
281
+ /// Return a handle to manually trigger raft actions, such as elect or build snapshot .
341
282
///
342
- /// Returns error when RaftCore has [`Fatal`] error, e.g. shut down or having storage error.
343
- /// It is not affected by `Raft::enable_elect(false)`.
283
+ /// Example:
284
+ /// ```ignore
285
+ /// let raft = Raft::new(...).await?;
286
+ /// raft.trigger().elect().await?;
287
+ /// ```
288
+ pub fn trigger ( & self ) -> Trigger < C , N , LS > {
289
+ Trigger :: new ( self . inner . as_ref ( ) )
290
+ }
291
+
292
+ /// Trigger election at once and return at once.
293
+ #[ deprecated( note = "use `Raft::trigger().elect()` instead" ) ]
344
294
pub async fn trigger_elect ( & self ) -> Result < ( ) , Fatal < C :: NodeId > > {
345
- self . inner . send_external_command ( ExternalCommand :: Elect , "trigger_elect" ) . await
295
+ self . trigger ( ) . elect ( ) . await
346
296
}
347
297
348
298
/// Trigger a heartbeat at once and return at once.
349
- ///
350
- /// Returns error when RaftCore has [`Fatal`] error, e.g. shut down or having storage error.
351
- /// It is not affected by `Raft::enable_heartbeat(false)`.
299
+ #[ deprecated( note = "use `Raft::trigger().heartbeat()` instead" ) ]
352
300
pub async fn trigger_heartbeat ( & self ) -> Result < ( ) , Fatal < C :: NodeId > > {
353
- self . inner . send_external_command ( ExternalCommand :: Heartbeat , "trigger_heartbeat" ) . await
301
+ self . trigger ( ) . heartbeat ( ) . await
354
302
}
355
303
356
304
/// Trigger to build a snapshot at once and return at once.
357
- ///
358
- /// Returns error when RaftCore has [`Fatal`] error, e.g. shut down or having storage error.
305
+ #[ deprecated( note = "use `Raft::trigger().snapshot()` instead" ) ]
359
306
pub async fn trigger_snapshot ( & self ) -> Result < ( ) , Fatal < C :: NodeId > > {
360
- self . inner . send_external_command ( ExternalCommand :: Snapshot , "trigger_snapshot" ) . await
307
+ self . trigger ( ) . snapshot ( ) . await
361
308
}
362
309
363
310
/// Initiate the log purge up to and including the given `upto` log index.
364
- ///
365
- /// Logs that are not included in a snapshot will **NOT** be purged.
366
- /// In such scenario it will delete as many log as possible.
367
- /// The [`max_in_snapshot_log_to_keep`] config is not taken into account
368
- /// when purging logs.
369
- ///
370
- /// It returns error only when RaftCore has [`Fatal`] error, e.g. shut down or having storage
371
- /// error.
372
- ///
373
- /// Openraft won't purge logs at once, e.g. it may be delayed by several seconds, because if it
374
- /// is a leader and a replication task has been replicating the logs to a follower, the logs
375
- /// can't be purged until the replication task is finished.
376
- ///
377
- /// [`max_in_snapshot_log_to_keep`]: `crate::Config::max_in_snapshot_log_to_keep`
311
+ #[ deprecated( note = "use `Raft::trigger().purge_log()` instead" ) ]
378
312
pub async fn purge_log ( & self , upto : u64 ) -> Result < ( ) , Fatal < C :: NodeId > > {
379
- self . inner . send_external_command ( ExternalCommand :: PurgeLog { upto } , "purge_log" ) . await
313
+ self . trigger ( ) . purge_log ( upto) . await
380
314
}
381
315
382
316
/// Submit an AppendEntries RPC to this Raft node.
0 commit comments