Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raftstorev2: add basic layout #12843

Merged
merged 2 commits into from
Jun 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ members = [
"components/panic_hook",
"components/pd_client",
"components/raftstore",
"components/raftstore-v2",
"components/resolved_ts",
"components/resource_metering",
"components/server",
Expand Down
39 changes: 39 additions & 0 deletions components/raftstore-v2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[package]
name = "raftstore-v2"
version = "0.1.0"
edition = "2021"

[features]
default = ["test-engine-kv-rocksdb", "test-engine-raft-raft-engine"]
failpoints = ["raftstore/failpoints"]
testexport = ["raftstore/testexport"]
test-engine-kv-rocksdb = [
"raftstore/test-engine-kv-rocksdb"
]
test-engine-raft-raft-engine = [
"raftstore/test-engine-raft-raft-engine"
]
test-engines-rocksdb = [
"raftstore/test-engines-rocksdb",
]
test-engines-panic = [
"raftstore/test-engines-panic",
]

cloud-aws = ["raftstore/cloud-aws"]
cloud-gcp = ["raftstore/cloud-gcp"]
cloud-azure = ["raftstore/cloud-azure"]

[dependencies]
collections = { path = "../collections" }
crossbeam = "0.8"
engine_traits = { path = "../engine_traits" }
error_code = { path = "../error_code" }
kvproto = { git = "https://github.com/pingcap/kvproto.git" }
pd_client = { path = "../pd_client" }
raft = { version = "0.7.0", default-features = false, features = ["protobuf-codec"] }
raft-proto = { version = "0.7.0" }
raftstore = { path = "../raftstore" }
slog = "2.3"
smallvec = "1.4"
tikv_util = { path = "../tikv_util", default-features = false }
1 change: 1 addition & 0 deletions components/raftstore-v2/src/fsm/apply.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Copyright 2022 TiKV Project Authors. Licensed under Apache-2.0.
5 changes: 5 additions & 0 deletions components/raftstore-v2/src/fsm/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright 2022 TiKV Project Authors. Licensed under Apache-2.0.

mod apply;
mod peer;
mod store;
22 changes: 22 additions & 0 deletions components/raftstore-v2/src/fsm/peer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2022 TiKV Project Authors. Licensed under Apache-2.0.

use engine_traits::RaftEngine;
use kvproto::metapb;
use raftstore::store::Config;
use slog::Logger;

use crate::{raft::Peer, Result};

pub struct PeerFsm<ER: RaftEngine> {
peer: Peer<ER>,
}

impl<ER: RaftEngine> PeerFsm<ER> {
pub fn new(peer: Peer<ER>) -> Result<Self> {
Ok(PeerFsm { peer })
}

pub fn logger(&self) -> &Logger {
self.peer.logger()
}
}
1 change: 1 addition & 0 deletions components/raftstore-v2/src/fsm/store.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Copyright 2022 TiKV Project Authors. Licensed under Apache-2.0.
19 changes: 19 additions & 0 deletions components/raftstore-v2/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2022 TiKV Project Authors. Licensed under Apache-2.0.

//! Raftstore is the place where we implement multi-raft.
//!
//! The thread module of raftstore is batch-system, more check components/batch-system.
//! All state machines are defined in [`fsm`] module. Everything that wrapping raft is
//! implemented in [`raft`] module. And the commands are implemented in [`operation`] module.
//! All state machines are expected to communicate with messages. They are defined in
//! [`router`] module.

#![allow(unused)]

mod fsm;
mod operation;
mod raft;
mod router;

pub use raftstore::{Error, Result};
pub use router::{PeerMsg, PeerTick, StoreMsg, StoreTick};
1 change: 1 addition & 0 deletions components/raftstore-v2/src/operation/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Copyright 2022 TiKV Project Authors. Licensed under Apache-2.0.
7 changes: 7 additions & 0 deletions components/raftstore-v2/src/raft/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright 2022 TiKV Project Authors. Licensed under Apache-2.0.

mod peer;
mod storage;

pub use peer::Peer;
pub use storage::Storage;
70 changes: 70 additions & 0 deletions components/raftstore-v2/src/raft/peer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2022 TiKV Project Authors. Licensed under Apache-2.0.

use engine_traits::RaftEngine;
use kvproto::metapb;
use raft::RawNode;
use raftstore::store::Config;
use slog::{o, Logger};
use tikv_util::{box_err, config::ReadableSize};

use super::storage::Storage;
use crate::Result;

/// A peer that delegates commands between state machine and raft.
pub struct Peer<ER: RaftEngine> {
region_id: u64,
peer: metapb::Peer,
raft_group: RawNode<Storage<ER>>,
logger: Logger,
}

impl<ER: RaftEngine> Peer<ER> {
pub fn new(
cfg: &Config,
store_id: u64,
region: metapb::Region,
engine: ER,
logger: Logger,
) -> Result<Self> {
let peer = region
.get_peers()
.iter()
.find(|p| p.get_store_id() == store_id && p.get_id() != raft::INVALID_ID);
let peer = match peer {
Some(p) => p,
None => return Err(box_err!("no valid peer found in {:?}", region.get_peers())),
};
let l = logger.new(o!("peer_id" => peer.id));

let ps = Storage::new(engine, l.clone());

let applied_index = ps.applied_index();

let raft_cfg = raft::Config {
id: peer.get_id(),
election_tick: cfg.raft_election_timeout_ticks,
heartbeat_tick: cfg.raft_heartbeat_ticks,
min_election_tick: cfg.raft_min_election_timeout_ticks,
max_election_tick: cfg.raft_max_election_timeout_ticks,
max_size_per_msg: cfg.raft_max_size_per_msg.0,
max_inflight_msgs: cfg.raft_max_inflight_msgs,
applied: applied_index,
check_quorum: true,
skip_bcast_commit: true,
pre_vote: cfg.prevote,
max_committed_size_per_ready: ReadableSize::mb(16).0,
..Default::default()
};

Ok(Peer {
region_id: region.get_id(),
peer: peer.clone(),
raft_group: RawNode::new(&raft_cfg, ps, &logger)?,
logger: l,
})
}

pub fn logger(&self) -> &Logger {
&self.logger
}
}
56 changes: 56 additions & 0 deletions components/raftstore-v2/src/raft/storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2022 TiKV Project Authors. Licensed under Apache-2.0.

use engine_traits::RaftEngine;
use raft::{
eraftpb::{Entry, Snapshot},
GetEntriesContext, RaftState,
};
use slog::Logger;

/// A storage for raft.
pub struct Storage<ER> {
engine: ER,
logger: Logger,
}

impl<ER> Storage<ER> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for Raft Storage, what's the expecting difference between v2 and v1?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initialization is different.

pub fn new(engine: ER, logger: Logger) -> Storage<ER> {
Storage { engine, logger }
}

pub fn applied_index(&self) -> u64 {
unimplemented!()
}
}

impl<ER: RaftEngine> raft::Storage for Storage<ER> {
fn initial_state(&self) -> raft::Result<RaftState> {
unimplemented!()
}

fn entries(
&self,
low: u64,
high: u64,
max_size: impl Into<Option<u64>>,
context: GetEntriesContext,
) -> raft::Result<Vec<Entry>> {
unimplemented!()
}

fn term(&self, idx: u64) -> raft::Result<u64> {
unimplemented!()
}

fn first_index(&self) -> raft::Result<u64> {
unimplemented!()
}

fn last_index(&self) -> raft::Result<u64> {
unimplemented!()
}

fn snapshot(&self, request_index: u64, to: u64) -> raft::Result<Snapshot> {
unimplemented!()
}
}