Skip to content

Commit

Permalink
Feature: add feature-flag: bt enables backtrace
Browse files Browse the repository at this point in the history
`--features bt` enables backtrace when generating errors.
By default errors does not contain backtrace info.

Thus openraft can be built on stable rust by default.

To use on stable rust with backtrace, set `RUSTC_BOOTSTRAP=1`, e.g.:
```
RUSTUP_TOOLCHAIN=stable RUSTC_BOOTSTRAP=1 make test
```
  • Loading branch information
drmingdrmer committed Jul 25, 2022
1 parent bf49cc3 commit e9d772b
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 16 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,53 @@ jobs:
with:
path: |
openraft/_log/
ut-on-stable-rust:
name: unittest on stable rust
runs-on: ubuntu-latest

steps:
- name: Setup | Checkout
uses: actions/checkout@v2

- name: Setup | Toolchain
uses: actions-rs/[email protected]
with:
toolchain: "stable"
override: true
components: rustfmt, clippy

- name: Unit Tests
uses: actions-rs/cargo@v1
with:
command: test
env:
# Parallel tests block each other and result in timeout.
RUST_TEST_THREADS: 2
RUST_LOG: debug
RUST_BACKTRACE: full


- name: Build | Release Mode | No features
uses: actions-rs/cargo@v1
with:
command: build
args: --release


- name: Build | Release Mode | No features
uses: actions-rs/cargo@v1
with:
command: build
args: --release --all-features
env:
# Enable unstable feature on stalbe rust.
RUSTC_BOOTSTRAP: 1


- name: Upload artifact
uses: actions/upload-artifact@v2
if: failure()
with:
path: |
openraft/_log/
2 changes: 1 addition & 1 deletion memstore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ readme = "README.md"
[dependencies]
anyhow = "1.0.32"
anyerror = "0.1.6"
openraft = { version="0.6.6", path= "../openraft" }
openraft = { version="0.6", path= "../openraft" }
async-trait = "0.1.36"
serde = { version="1.0.114", features=["derive"] }
serde_json = "1.0.57"
Expand Down
4 changes: 1 addition & 3 deletions memstore/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![feature(backtrace)]

#[cfg(test)]
mod test;

Expand All @@ -12,14 +10,14 @@ use std::ops::RangeBounds;
use std::sync::Arc;
use std::sync::Mutex;

use anyerror::AnyError;
use openraft::async_trait::async_trait;
use openraft::raft::Entry;
use openraft::raft::EntryPayload;
use openraft::raft::Membership;
use openraft::storage::HardState;
use openraft::storage::InitialState;
use openraft::storage::Snapshot;
use openraft::AnyError;
use openraft::AppData;
use openraft::AppDataResponse;
use openraft::EffectiveMembership;
Expand Down
6 changes: 5 additions & 1 deletion openraft/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ readme = "../README.md"

[dependencies]
anyhow = "1.0.32"
anyerror = { version = "0.1.6", features = ["anyhow"]}
async-trait = "0.1.36"
anyerror = "0.1.6"
byte-unit = "4.0.12"
bytes = "1.0"
derive_more = { version="0.99.9" }
Expand All @@ -44,5 +44,9 @@ tracing-subscriber = { version = "0.3.3", features=["env-filter"] }
[features]
docinclude = [] # Used only for activating `doc(include="...")` on nightly.

# Enable backtrace when generating an error.
# Stable rust does not support backtrace.
bt = ["anyerror/backtrace", "anyhow/backtrace"]

[package.metadata.docs.rs]
features = ["docinclude"] # Activate `docinclude` during docs.rs build.
3 changes: 2 additions & 1 deletion openraft/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,8 @@ impl<D: AppData, R: AppDataResponse, N: RaftNetwork<D>, S: RaftStorage<D, R>> Ra
Ok(res) => match res {
Ok(snapshot) => {
let _ = tx_compaction.try_send(SnapshotUpdate::SnapshotComplete(snapshot.meta.last_log_id));
let _ = chan_tx.send(snapshot.meta.last_log_id.index); // This will always succeed.
// This will always succeed.
let _ = chan_tx.send(snapshot.meta.last_log_id.index);
}
Err(err) => {
tracing::error!({error=%err}, "error while generating snapshot");
Expand Down
4 changes: 2 additions & 2 deletions openraft/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub enum ReplicationError {

#[error(transparent)]
IO {
#[backtrace]
#[cfg_attr(feature = "bt", backtrace)]
#[from]
source: std::io::Error,
},
Expand All @@ -75,7 +75,7 @@ pub enum ReplicationError {

#[error(transparent)]
Network {
#[backtrace]
#[cfg_attr(feature = "bt", backtrace)]
source: anyhow::Error,
},
}
Expand Down
10 changes: 9 additions & 1 deletion openraft/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#![doc = include_str!("../README.md")]
#![feature(backtrace)]
#![cfg_attr(feature = "bt", feature(backtrace))]

//! # Feature flags
//!
//! - `bt`: Enable backtrace: generate backtrace for errors. This requires a unstable feature `backtrace` thus it can
//! not be used with stable rust, unless explicity allowing using unstable features in stable rust with
//! `RUSTC_BOOTSTRAP=1`.
pub mod config;
mod core;
Expand All @@ -24,6 +30,8 @@ mod store_wrapper;

pub mod types;

pub use anyerror;
pub use anyerror::AnyError;
pub use async_trait;
pub use store_ext::StoreExt;
pub use store_wrapper::Wrapper;
Expand Down
5 changes: 2 additions & 3 deletions openraft/src/storage_error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::backtrace::Backtrace;
use std::fmt::Formatter;

use anyerror::AnyError;
Expand All @@ -15,7 +14,7 @@ impl DefensiveError {
DefensiveError {
subject,
violation,
backtrace: format!("{:?}", Backtrace::capture()),
backtrace: anyerror::backtrace_str(),
}
}
}
Expand Down Expand Up @@ -54,7 +53,7 @@ impl StorageIOError {
subject,
verb,
source,
backtrace: format!("{:?}", Backtrace::capture()),
backtrace: anyerror::backtrace_str(),
}
}
}
8 changes: 4 additions & 4 deletions openraft/src/types/v065/storage_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct DefensiveError {
/// The description of the violation.
pub violation: Violation,

pub backtrace: String,
pub backtrace: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
Expand Down Expand Up @@ -103,15 +103,15 @@ pub enum StorageError {
#[error(transparent)]
Defensive {
#[from]
#[backtrace]
#[cfg_attr(feature = "bt", backtrace)]
source: DefensiveError,
},

/// An error raised by io operation.
#[error(transparent)]
IO {
#[from]
#[backtrace]
#[cfg_attr(feature = "bt", backtrace)]
source: StorageIOError,
},
}
Expand All @@ -122,5 +122,5 @@ pub struct StorageIOError {
pub(crate) subject: ErrorSubject,
pub(crate) verb: ErrorVerb,
pub(crate) source: AnyError,
pub(crate) backtrace: String,
pub(crate) backtrace: Option<String>,
}

0 comments on commit e9d772b

Please sign in to comment.