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

raftstore: tiny refactor for peer_storage #4668

Merged
merged 6 commits into from May 10, 2019
@@ -376,9 +376,9 @@ pub fn recover_from_applying_state(
Ok(())
}

pub fn init_raft_state(raft_engine: &DB, region: &Region) -> Result<RaftLocalState> {
pub fn init_raft_state(engines: &Engines, region: &Region) -> Result<RaftLocalState> {
let state_key = keys::raft_state_key(region.get_id());
Ok(match raft_engine.get_msg(&state_key)? {
Ok(match engines.raft.get_msg(&state_key)? {
Some(s) => s,
None => {
let mut raft_state = RaftLocalState::new();
@@ -387,16 +387,19 @@ pub fn init_raft_state(raft_engine: &DB, region: &Region) -> Result<RaftLocalSta
raft_state.set_last_index(RAFT_INIT_LOG_INDEX);
raft_state.mut_hard_state().set_term(RAFT_INIT_LOG_TERM);
raft_state.mut_hard_state().set_commit(RAFT_INIT_LOG_INDEX);
raft_engine.put_msg(&state_key, &raft_state)?;
engines.raft.put_msg(&state_key, &raft_state)?;
}
raft_state
}
})
}

pub fn init_apply_state(kv_engine: &DB, region: &Region) -> Result<RaftApplyState> {
pub fn init_apply_state(engines: &Engines, region: &Region) -> Result<RaftApplyState> {
Ok(
match kv_engine.get_msg_cf(CF_RAFT, &keys::apply_state_key(region.get_id()))? {
match engines
.kv
.get_msg_cf(CF_RAFT, &keys::apply_state_key(region.get_id()))?
{
Some(s) => s,
None => {
let mut apply_state = RaftApplyState::new();
@@ -413,7 +416,7 @@ pub fn init_apply_state(kv_engine: &DB, region: &Region) -> Result<RaftApplyStat
}

fn init_last_term(
raft_engine: &DB,
engines: &Engines,
region: &Region,
raft_state: &RaftLocalState,
apply_state: &RaftApplyState,
@@ -429,16 +432,41 @@ fn init_last_term(
assert!(last_idx > RAFT_INIT_LOG_INDEX);
}
let last_log_key = keys::raft_log_key(region.get_id(), last_idx);
Ok(match raft_engine.get_msg::<Entry>(&last_log_key)? {
None => {
return Err(box_err!(
"[region {}] entry at {} doesn't exist, may lose data.",
region.get_id(),
last_idx
));
}
Some(e) => e.get_term(),
})
let entry = engines.raft.get_msg::<Entry>(&last_log_key)?;
match entry {
None => Err(box_err!(
"[region {}] entry at {} doesn't exist, may lose data.",
region.get_id(),
last_idx
)),
Some(e) => Ok(e.get_term()),
}
}

impl Storage for PeerStorage {

This comment has been minimized.

Copy link
@Connor1996

Connor1996 May 9, 2019

Member

by the way, can you move struct PeerStorage right above it?

fn initial_state(&self) -> raft::Result<RaftState> {
self.initial_state()
}

fn entries(&self, low: u64, high: u64, max_size: u64) -> raft::Result<Vec<Entry>> {
self.entries(low, high, max_size)
}

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

fn first_index(&self) -> raft::Result<u64> {
Ok(self.first_index())
}

fn last_index(&self) -> raft::Result<u64> {
Ok(self.last_index())
}

fn snapshot(&self) -> raft::Result<Snapshot> {
self.snapshot()
}
}

impl PeerStorage {
@@ -455,8 +483,8 @@ impl PeerStorage {
"peer_id" => peer_id,
"path" => ?engines.kv.path(),
);
let raft_state = init_raft_state(&engines.raft, region)?;
let apply_state = init_apply_state(&engines.kv, region)?;
let raft_state = init_raft_state(&engines, region)?;
let apply_state = init_apply_state(&engines, region)?;
if raft_state.get_last_index() < apply_state.get_applied_index() {
panic!(
"{} unexpected raft log index: last_index {} < applied_index {}",
@@ -465,7 +493,7 @@ impl PeerStorage {
apply_state.get_applied_index()
);
}
let last_term = init_last_term(&engines.raft, region, &raft_state, &apply_state)?;
let last_term = init_last_term(&engines, region, &raft_state, &apply_state)?;

Ok(PeerStorage {
engines,
@@ -1442,32 +1470,6 @@ pub fn write_peer_state<T: Mutable>(
Ok(())
}

impl Storage for PeerStorage {
fn initial_state(&self) -> raft::Result<RaftState> {
self.initial_state()
}

fn entries(&self, low: u64, high: u64, max_size: u64) -> raft::Result<Vec<Entry>> {
self.entries(low, high, max_size)
}

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

fn first_index(&self) -> raft::Result<u64> {
Ok(self.first_index())
}

fn last_index(&self) -> raft::Result<u64> {
Ok(self.last_index())
}

fn snapshot(&self) -> raft::Result<Snapshot> {
self.snapshot()
}
}

/// Upgreade from v2.x to v3.x
///
/// For backward compatibility, it needs to check whether there are any
@@ -457,8 +457,8 @@ impl Debugger {
Error::Other("RegionLocalState doesn't contains peer itself".into())
})?;

let raft_state = box_try!(init_raft_state(&self.engines.raft, region));
let apply_state = box_try!(init_apply_state(&self.engines.kv, region));
let raft_state = box_try!(init_raft_state(&self.engines, region));
let apply_state = box_try!(init_apply_state(&self.engines, region));
if raft_state.get_last_index() < apply_state.get_applied_index() {
return Err(Error::Other("last index < applied index".into()));
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.