Skip to content

Commit 2b8bc84

Browse files
committed
Change: Add default value to declare_raft_types
Types used in `declare_raft_types` can be omitted, in which case the default type will be used. The default values for each type are: - `D`: `String` - `R`: `String` - `NodeId`: `u64` - `Node`: `::openraft::BasicNode` - `Entry`: `::openraft::Entry<Self>` - `SnapshotData`: `Cursor<Vec<u8>>` - `AsyncRuntime`: `::openraft::TokioRuntime` Note that **The types must be specified in the exact order**: `D`, `R`, `NodeId`, `Node`, `Entry`, `SnapshotData`, `AsyncRuntime`. For example, to declare with only `D`, `R` and `Entry` types: ```rust,ignore openraft::declare_raft_types!( pub TypeConfig: D = ClientRequest, R = ClientResponse, Entry = MyEntry, ); ``` Type `NodeId`, `Node`, `SnapshotData` and `AsyncRuntime` will be filled with default values mentioned above. Or one can just use the default types for all: ```rust,ignore openraft::declare_raft_types!(pub TypeConfig); ``` Upgrade tip: Ensures types declared in `declare_raft_types` are in the correct order
1 parent 56118c1 commit 2b8bc84

File tree

15 files changed

+186
-64
lines changed

15 files changed

+186
-64
lines changed

cluster_benchmark/tests/benchmark/store.rs

-4
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ openraft::declare_raft_types!(
4343
pub TypeConfig:
4444
D = ClientRequest,
4545
R = ClientResponse,
46-
NodeId = NodeId,
4746
Node = (),
48-
Entry = Entry<TypeConfig>,
49-
SnapshotData = Cursor<Vec<u8>>,
50-
AsyncRuntime = TokioRuntime
5147
);
5248

5349
#[derive(Debug)]

examples/raft-kv-memstore-generic-snapshot-data/src/lib.rs

-6
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33

44
use std::sync::Arc;
55

6-
use openraft::BasicNode;
76
use openraft::Config;
8-
use openraft::TokioRuntime;
97

108
use crate::app::App;
119
use crate::router::Router;
@@ -27,13 +25,9 @@ openraft::declare_raft_types!(
2725
pub TypeConfig:
2826
D = Request,
2927
R = Response,
30-
NodeId = NodeId,
31-
Node = BasicNode,
32-
Entry = openraft::Entry<TypeConfig>,
3328
// In this example, snapshot is just a copy of the state machine.
3429
// And it can be any type.
3530
SnapshotData = StateMachineData,
36-
AsyncRuntime = TokioRuntime
3731
);
3832

3933
pub type LogStore = store::LogStore;

examples/raft-kv-memstore-generic-snapshot-data/src/network.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use openraft::raft::AppendEntriesResponse;
88
use openraft::raft::SnapshotResponse;
99
use openraft::raft::VoteRequest;
1010
use openraft::raft::VoteResponse;
11+
use openraft::BasicNode;
1112
use openraft::OptionalSend;
1213
use openraft::RaftNetwork;
1314
use openraft::RaftNetworkFactory;
@@ -16,7 +17,6 @@ use openraft::Vote;
1617

1718
use crate::router::Router;
1819
use crate::typ;
19-
use crate::BasicNode;
2020
use crate::NodeId;
2121
use crate::TypeConfig;
2222

examples/raft-kv-memstore-opendal-snapshot-data/src/lib.rs

-6
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
use std::sync::Arc;
55

66
use opendal::Operator;
7-
use openraft::BasicNode;
87
use openraft::Config;
9-
use openraft::TokioRuntime;
108

119
use crate::app::App;
1210
use crate::router::Router;
@@ -27,12 +25,8 @@ openraft::declare_raft_types!(
2725
pub TypeConfig:
2826
D = Request,
2927
R = Response,
30-
NodeId = NodeId,
31-
Node = BasicNode,
32-
Entry = openraft::Entry<TypeConfig>,
3328
// In this example, snapshot is a path pointing to a file stored in shared storage.
3429
SnapshotData = String,
35-
AsyncRuntime = TokioRuntime
3630
);
3731

3832
pub type LogStore = store::LogStore;

examples/raft-kv-memstore-opendal-snapshot-data/src/network.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use openraft::raft::AppendEntriesResponse;
88
use openraft::raft::SnapshotResponse;
99
use openraft::raft::VoteRequest;
1010
use openraft::raft::VoteResponse;
11+
use openraft::BasicNode;
1112
use openraft::OptionalSend;
1213
use openraft::RaftNetwork;
1314
use openraft::RaftNetworkFactory;
@@ -16,7 +17,6 @@ use openraft::Vote;
1617

1718
use crate::router::Router;
1819
use crate::typ;
19-
use crate::BasicNode;
2020
use crate::NodeId;
2121
use crate::TypeConfig;
2222

examples/raft-kv-memstore-singlethreaded/src/lib.rs

-6
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ use std::marker::PhantomData;
66
use std::rc::Rc;
77
use std::sync::Arc;
88

9-
use openraft::BasicNode;
109
use openraft::Config;
11-
use openraft::TokioRuntime;
1210

1311
use crate::app::App;
1412
use crate::router::Router;
@@ -51,10 +49,6 @@ openraft::declare_raft_types!(
5149
D = Request,
5250
R = Response,
5351
NodeId = NodeId,
54-
Node = BasicNode,
55-
Entry = openraft::Entry<TypeConfig>,
56-
SnapshotData = Cursor<Vec<u8>>,
57-
AsyncRuntime = TokioRuntime
5852
);
5953

6054
pub type LogStore = store::LogStore;

examples/raft-kv-memstore-singlethreaded/src/network.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ use openraft::raft::InstallSnapshotRequest;
77
use openraft::raft::InstallSnapshotResponse;
88
use openraft::raft::VoteRequest;
99
use openraft::raft::VoteResponse;
10+
use openraft::BasicNode;
1011
use openraft::RaftNetwork;
1112
use openraft::RaftNetworkFactory;
1213

1314
use crate::router::Router;
1415
use crate::typ;
15-
use crate::BasicNode;
1616
use crate::NodeId;
1717
use crate::TypeConfig;
1818

examples/raft-kv-memstore/src/lib.rs

-7
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ use actix_web::middleware;
88
use actix_web::middleware::Logger;
99
use actix_web::web::Data;
1010
use actix_web::HttpServer;
11-
use openraft::BasicNode;
1211
use openraft::Config;
13-
use openraft::TokioRuntime;
1412

1513
use crate::app::App;
1614
use crate::network::api;
@@ -32,11 +30,6 @@ openraft::declare_raft_types!(
3230
pub TypeConfig:
3331
D = Request,
3432
R = Response,
35-
NodeId = NodeId,
36-
Node = BasicNode,
37-
Entry = openraft::Entry<TypeConfig>,
38-
SnapshotData = Cursor<Vec<u8>>,
39-
AsyncRuntime = TokioRuntime,
4033
);
4134

4235
pub type LogStore = store::LogStore;

examples/raft-kv-rocksdb/src/lib.rs

-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::path::Path;
77
use std::sync::Arc;
88

99
use openraft::Config;
10-
use openraft::TokioRuntime;
1110
use tokio::net::TcpListener;
1211
use tokio::task;
1312

@@ -44,11 +43,7 @@ openraft::declare_raft_types!(
4443
pub TypeConfig:
4544
D = Request,
4645
R = Response,
47-
NodeId = NodeId,
4846
Node = Node,
49-
Entry = openraft::Entry<TypeConfig>,
50-
SnapshotData = SnapshotData,
51-
AsyncRuntime = TokioRuntime
5247
);
5348

5449
pub mod typ {

memstore/src/lib.rs

-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use openraft::SnapshotMeta;
2626
use openraft::StorageError;
2727
use openraft::StorageIOError;
2828
use openraft::StoredMembership;
29-
use openraft::TokioRuntime;
3029
use openraft::Vote;
3130
use serde::Deserialize;
3231
use serde::Serialize;
@@ -77,11 +76,7 @@ openraft::declare_raft_types!(
7776
pub TypeConfig:
7877
D = ClientRequest,
7978
R = ClientResponse,
80-
NodeId = MemNodeId,
8179
Node = (),
82-
Entry = Entry<TypeConfig>,
83-
SnapshotData = Cursor<Vec<u8>>,
84-
AsyncRuntime = TokioRuntime
8580
);
8681

8782
/// The application snapshot type which the `MemStore` works with.

openraft/src/engine/command.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ where C: RaftTypeConfig
8989
DeleteConflictLog { since: LogId<C::NodeId> },
9090

9191
// TODO(1): current it is only used to replace BuildSnapshot, InstallSnapshot, CancelSnapshot.
92-
/// A command send to state machine worker [`worker::Worker`].
92+
/// A command send to state machine worker [`sm::worker::Worker`].
9393
///
94-
/// The runtime(`RaftCore`) will just forward this command to [`worker::Worker`].
94+
/// The runtime(`RaftCore`) will just forward this command to [`sm::worker::Worker`].
9595
/// The response will be sent back in a `RaftMsg::StateMachine` message to `RaftCore`.
9696
StateMachine { command: sm::Command<C> },
9797

@@ -212,7 +212,7 @@ where NID: NodeId
212212
#[allow(dead_code)]
213213
Applied { log_id: Option<LogId<NID>> },
214214

215-
/// Wait until a [`worker::Worker`] command is finished.
215+
/// Wait until a [`sm::worker::Worker`] command is finished.
216216
#[allow(dead_code)]
217217
StateMachineCommand { command_seq: sm::CommandSeq },
218218
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//! Test the `declare_raft_types` macro with default values
2+
3+
use std::io::Cursor;
4+
5+
use crate::declare_raft_types;
6+
use crate::TokioRuntime;
7+
8+
declare_raft_types!(
9+
All:
10+
D = (),
11+
R = (),
12+
NodeId = u64,
13+
Node = (),
14+
Entry = crate::Entry<Self>,
15+
SnapshotData = Cursor<Vec<u8>>,
16+
AsyncRuntime = TokioRuntime,
17+
);
18+
19+
declare_raft_types!(
20+
WithoutD:
21+
R = (),
22+
NodeId = u64,
23+
Node = (),
24+
Entry = crate::Entry<Self>,
25+
SnapshotData = Cursor<Vec<u8>>,
26+
AsyncRuntime = TokioRuntime,
27+
);
28+
29+
declare_raft_types!(
30+
WithoutR:
31+
D = (),
32+
NodeId = u64,
33+
Node = (),
34+
Entry = crate::Entry<Self>,
35+
SnapshotData = Cursor<Vec<u8>>,
36+
AsyncRuntime = TokioRuntime,
37+
);
38+
39+
// This raise an compile error:
40+
// > error: Type not in its expected position : NodeId = u64, D = (), types must present
41+
// > in this order : D, R, NodeId, Node, Entry, SnapshotData, AsyncRuntime
42+
// declare_raft_types!(
43+
// Foo:
44+
// Node = (),
45+
// NodeId = u64,
46+
// D = (),
47+
// );
48+
49+
declare_raft_types!(EmptyWithColon:);
50+
51+
declare_raft_types!(Empty);

0 commit comments

Comments
 (0)