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

raft: reduce allocation #2434

Merged
merged 1 commit into from Oct 30, 2017
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
28 changes: 16 additions & 12 deletions src/raft/raft.rs
Expand Up @@ -32,12 +32,11 @@ use rand::{self, Rng};
use kvproto::eraftpb::{Entry, EntryType, HardState, Message, MessageType, Snapshot};
use protobuf::repeated::RepeatedField;

use raft::storage::Storage;
use raft::progress::{Inflights, Progress, ProgressState};
use raft::errors::{Error, Result, StorageError};
use raft::raft_log::{self, RaftLog};
use raft::read_only::{ReadOnly, ReadOnlyOption, ReadState};

use super::storage::Storage;
use super::progress::{Inflights, Progress, ProgressState};
use super::errors::{Error, Result, StorageError};
use super::raft_log::{self, RaftLog};
use super::read_only::{ReadOnly, ReadOnlyOption, ReadState};
use super::FlatMap;

// CAMPAIGN_PRE_ELECTION represents the first phase of a normal election when
Expand Down Expand Up @@ -600,16 +599,21 @@ impl<T: Storage> Raft<T> {
// the commit index changed (in which case the caller should call
// r.bcast_append).
pub fn maybe_commit(&mut self) -> bool {
// TODO: optimize
let mut mis = Vec::with_capacity(self.prs.len());
for p in self.prs.values() {
mis.push(p.matched);
let mut mis_arr = [0; 5];
let mut mis_vec;
let mis = if self.prs.len() <= 5 {
&mut mis_arr[..self.prs.len()]
} else {
mis_vec = vec![0; self.prs.len()];
mis_vec.as_mut_slice()
};
for (i, pr) in self.prs.values().enumerate() {
mis[i] = pr.matched;
}
// reverse sort
mis.sort_by(|a, b| b.cmp(a));
let mci = mis[self.quorum() - 1];
let term = self.term;
self.raft_log.maybe_commit(mci, term)
self.raft_log.maybe_commit(mci, self.term)
}

pub fn reset(&mut self, term: u64) {
Expand Down
12 changes: 8 additions & 4 deletions src/raft/raw_node.rs
Expand Up @@ -26,14 +26,16 @@
// limitations under the License.


use std::mem;

use raft::errors::{Error, Result};
use raft::Storage;
use protobuf::{self, RepeatedField};
use kvproto::eraftpb::{ConfChange, ConfChangeType, ConfState, Entry, EntryType, HardState,
Message, MessageType, Snapshot};
use raft::raft::{Config, Raft, SoftState, INVALID_ID};
use raft::Status;
use raft::read_only::ReadState;
use super::raft::{Config, Raft, SoftState, INVALID_ID};
use super::Status;
use super::read_only::ReadState;

#[derive(Debug, Default)]
pub struct Peer {
Expand Down Expand Up @@ -126,9 +128,11 @@ impl Ready {
) -> Ready {
let mut rd = Ready {
entries: raft.raft_log.unstable_entries().unwrap_or(&[]).to_vec(),
messages: raft.msgs.drain(..).collect(),
..Default::default()
};
if !raft.msgs.is_empty() {
mem::swap(&mut raft.msgs, &mut rd.messages);
}
rd.committed_entries = Some(
(match since_idx {
None => raft.raft_log.next_entries(),
Expand Down