Skip to content

Commit

Permalink
Switching to the new version of the prost and related libraries.
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitry-huba committed Apr 11, 2024
1 parent 93c40a2 commit 5b2275b
Show file tree
Hide file tree
Showing 16 changed files with 86 additions and 37 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ jobs:
- name: check clippy
if: ${{ matrix.rust == 'stable' && matrix.os == 'ubuntu-latest' }}
run: cargo clippy --all --all-targets -- -D clippy::all && cargo clippy --no-default-features --features prost-codec --features std -- -D clippy::all
- name: check no_std compatibility
- name: install targets
run: rustup target add x86_64-unknown-none
- name: run build for no-std target
# No-std implementation uses feature that can only be compiled by nightly version
if: ${{ matrix.rust == 'nightly' }}
run: cargo build --no-default-features --features prost-codec --target x86_64-unknown-none
- name: run tests with no-std enabled
# No-std implementation uses feature that can only be compiled by nightly version
if: ${{ matrix.rust == 'nightly' }}
run: cargo test --all --no-default-features --features=prost-codec --features=default-logger --no-fail-fast -- --nocapture
- run: cargo test --all --no-fail-fast -- --nocapture
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ ahash = { version = "0.8.3", default-features = false }
fail = { version = "0.4", optional = true }
getset = "0.1.1"
protobuf = { version = "2", optional = true }
prost = { version = "0.11", optional = true, default-features = false, features = ["prost-derive"] }
prost = { version = "0.12.4", optional = true, default-features = false, features = ["prost-derive"] }
thiserror = { version = "1.0", optional = true }
raft-proto = { path = "proto", version = "0.7.0", default-features = false }
rand = { version = "0.8", default-features = false, features = ["alloc", "small_rng", "getrandom"] }
Expand Down
1 change: 0 additions & 1 deletion examples/single_mem_node/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::sync::mpsc::{self, RecvTimeoutError};
use std::thread;
use std::time::{Duration, Instant};

use raft::eraftpb::ConfState;
use raft::prelude::*;
use raft::storage::MemStorage;

Expand Down
2 changes: 1 addition & 1 deletion harness/tests/integration_cases/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.

#[allow(clippy::all)]
mod test_raft;
mod test_raft_flow_control;
mod test_raft_paper;
Expand Down
2 changes: 1 addition & 1 deletion harness/tests/integration_cases/test_raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use harness::*;
#[cfg(feature = "protobuf-codec")]
use protobuf::Message as PbMessage;
use raft::eraftpb::*;
use raft::storage::{GetEntriesContext, MemStorage};
use raft::storage::MemStorage;
use raft::*;
use raft_proto::*;
use slog::Logger;
Expand Down
21 changes: 16 additions & 5 deletions harness/tests/integration_cases/test_raw_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use harness::Network;
#[cfg(feature = "protobuf-codec")]
use protobuf::{Message as PbMessage, ProtobufEnum as _};
use raft::eraftpb::*;
use raft::storage::{GetEntriesContext, MemStorage};
use raft::storage::MemStorage;
use raft::*;
use raft_proto::*;
use slog::Logger;
Expand Down Expand Up @@ -174,6 +174,11 @@ fn test_raw_node_read_index_to_old_leader() {
);
}

fn sort_conf_state_collections(cs: &mut ConfState) {
cs.voters.sort();
cs.learners.sort();
}

/// Tests the configuration change mechanism. Each test case sends a configuration
/// change which is either simple or joint, verifies that it applies and that the
/// resulting ConfState matches expectations, and for joint configurations makes
Expand Down Expand Up @@ -251,7 +256,7 @@ fn test_raw_node_propose_and_conf_change() {
let cs = conf_state_v2(vec![2], vec![3], vec![1], vec![1], true);
test_cases.push((Box::new(cc), cs, Some(conf_state(vec![2], vec![1, 3]))));

for (cc, exp, exp2) in test_cases {
for (cc, mut exp, exp2) in test_cases {
let s = new_storage();
let mut raw_node = new_raw_node(1, vec![1], 10, 1, s.clone(), &l);
raw_node.campaign().unwrap();
Expand Down Expand Up @@ -321,7 +326,10 @@ fn test_raw_node_propose_and_conf_change() {
assert_eq!(entries[1].get_entry_type(), EntryType::EntryConfChangeV2);
}
assert_eq!(ccdata, entries[1].get_data());
assert_eq!(exp, cs.unwrap());
let mut cs = cs.unwrap();
sort_conf_state_collections(&mut cs);
sort_conf_state_collections(&mut exp);
assert_eq!(exp, cs);

let conf_index = if cc.as_v2().enter_joint() == Some(true) {
// If this is an auto-leaving joint conf change, it will have
Expand Down Expand Up @@ -366,8 +374,11 @@ fn test_raw_node_propose_and_conf_change() {
assert_eq!(context, leave_cc.get_context(), "{:?}", cc.as_v2());
// Lie and pretend the ConfChange applied. It won't do so because now
// we require the joint quorum and we're only running one node.
let cs = raw_node.apply_conf_change(&leave_cc).unwrap();
assert_eq!(cs, exp2.unwrap());
let mut cs = raw_node.apply_conf_change(&leave_cc).unwrap();
let mut exp2 = exp2.unwrap();
sort_conf_state_collections(&mut cs);
sort_conf_state_collections(&mut exp2);
assert_eq!(cs, exp2);
}
}

Expand Down
2 changes: 1 addition & 1 deletion proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ protobuf-build = { version = "0.15.1", default-features = false }
[dependencies]
bytes = { version = "1", optional = true, default-features = false }
lazy_static = { version = "1", optional = true, default-features = false, features = ["spin_no_std"] }
prost = { version = "0.11", optional = true, default-features = false, features = ["prost-derive"] }
prost = { version = "0.12.4", optional = true, default-features = false, features = ["prost-derive"] }
protobuf = { version = "2", optional = true }
7 changes: 1 addition & 6 deletions proto/src/confchange.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0.

#![allow(unused_imports)]
extern crate alloc;

use crate::eraftpb::{
Expand All @@ -11,11 +11,6 @@ use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;
use core::fmt::Write;
use core::iter::Iterator;
use core::option::Option;
use core::option::Option::{None, Some};
use core::result::Result;
use core::result::Result::{Err, Ok};
use core::write;

/// Creates a `ConfChangeSingle`.
Expand Down
2 changes: 0 additions & 2 deletions proto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ pub mod prelude {

pub mod util {
use crate::eraftpb::ConfState;
use core::convert::From;
use core::iter::IntoIterator;

impl<Iter1, Iter2> From<(Iter1, Iter2)> for ConfState
where
Expand Down
5 changes: 5 additions & 0 deletions proto/src/wrappers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Generated file, modified and added to support no_std

#[allow(deprecated)]
impl Entry {
#[inline]
pub fn default_ref() -> &'static Self {
Expand Down Expand Up @@ -220,6 +221,7 @@ impl Snapshot {
.unwrap_or_else(SnapshotMetadata::default)
}
}
#[allow(deprecated)]
impl Message {
pub fn new() -> Self {
Self::default()
Expand Down Expand Up @@ -604,6 +606,7 @@ impl ConfState {
self.auto_leave
}
}
#[allow(deprecated)]
impl ConfChange {
pub fn write_to_bytes(
&self,
Expand Down Expand Up @@ -683,6 +686,7 @@ impl ConfChange {
self.id
}
}
#[allow(deprecated)]
impl ConfChangeSingle {
#[inline]
pub fn default_ref() -> &'static Self {
Expand Down Expand Up @@ -715,6 +719,7 @@ impl ConfChangeSingle {
self.node_id
}
}
#[allow(deprecated)]
impl ConfChangeV2 {
pub fn write_to_bytes(
&self,
Expand Down
42 changes: 42 additions & 0 deletions scripts/ci.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2024 The Google raft-rs authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http:#www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#!/usr/bin/env bash

# Exit when any command fails
#set -e

printf "\n// cargo fmt --all -- --check\n\n"
env cargo fmt --all -- --check

printf "\n// cargo clippy --all --all-targets -- -D clippy::all && cargo clippy --no-default-features --features prost-codec --features std -- -D clippy::all\n\n"
env cargo clippy --all --all-targets -- -D clippy::all && cargo clippy --no-default-features --features prost-codec --features std -- -D clippy::all

printf "\n// cargo build --no-default-features --features prost-codec --target x86_64-unknown-none\n\n"
env cargo build --no-default-features --features prost-codec --target x86_64-unknown-none

printf "\n// cargo test --all --no-default-features --features=prost-codec --features=default-logger --no-fail-fast -- --nocapture\n\n"
env cargo test --all --no-default-features --features=prost-codec --features=default-logger --no-fail-fast -- --nocapture

printf "\n// cargo test --all --no-fail-fast -- --nocapture\n\n"
env cargo test --all --no-fail-fast -- --nocapture

printf "\n// cargo bench --all -- --test\n\n"
env cargo bench --all -- --test

printf "\n// cargo test --tests --features failpoints --package harness -- --nocapture\n\n"
env cargo test --tests --features failpoints --package harness -- --nocapture

printf "\n// cargo test --no-default-features --features prost-codec --features std -- --nocapture\n\n"
env cargo test --no-default-features --features prost-codec --features std -- --nocapture
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use alloc::borrow::ToOwned;
use alloc::format;

pub use super::read_only::{ReadOnlyOption, ReadState};
pub use super::read_only::ReadOnlyOption;
use super::util::NO_LIMIT;
use super::{
errors::{Error, Result},
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ before taking old, removed peers offline.
// We use `default` method a lot to be support prost and rust-protobuf at the
// same time. And reassignment can be optimized by compiler.
#![allow(clippy::field_reassign_with_default)]
#![allow(unused_imports)]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature = "std"), feature(error_in_core))]

Expand Down
16 changes: 7 additions & 9 deletions src/quorum/majority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ pub struct Configuration {

impl core::fmt::Display for Configuration {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(
f,
"({})",
self.voters
.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>()
.join(" ")
)
let mut voters = self
.voters
.iter()
.map(|x| x.to_string())
.collect::<Vec<String>>();
voters.sort();
write!(f, "({})", voters.join(" "))
}
}

Expand Down
9 changes: 2 additions & 7 deletions src/raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

use alloc::vec::Vec;
use core::cmp;
use core::convert::TryFrom;
use core::ops::{Deref, DerefMut};
use rand::rngs::SmallRng;

Expand All @@ -27,8 +26,8 @@ use crate::eraftpb::{
#[cfg(feature = "protobuf-codec")]
use protobuf::Message as _;
use raft_proto::ConfChangeI;
use rand::{self, Rng, SeedableRng};
use slog::{self, Logger};
use rand::{Rng, SeedableRng};
use slog::Logger;

#[cfg(feature = "failpoints")]
use fail::fail_point;
Expand Down Expand Up @@ -290,10 +289,6 @@ impl<T: Storage> DerefMut for Raft<T> {
}
}

trait AssertSend: Send {}

impl<T: Storage + Send> AssertSend for Raft<T> {}

fn new_message(to: u64, field_type: MessageType, from: Option<u64>) -> Message {
let mut m = Message::default();
m.to = to;
Expand Down
2 changes: 2 additions & 0 deletions src/raft_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,8 @@ impl<T: Storage> RaftLog<T> {

#[cfg(test)]
mod test {
#![allow(clippy::useless_vec)]

use alloc::vec;

use crate::default_logger;
Expand Down

0 comments on commit 5b2275b

Please sign in to comment.