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 Aug 17, 2022
1 parent 1d00244 commit ea69647
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 17 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,56 @@ jobs:
args: --all-targets -- -D warnings -A clippy::bool-assert-comparison


- name: Upload artifact
uses: actions/upload-artifact@v2
if: failure()
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()
Expand Down
2 changes: 1 addition & 1 deletion memstore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ repository = "https://github.com/datafuselabs/openraft"
readme = "README.md"

[dependencies]
anyerror = { version = "0.1.1", features=["anyhow"]}
anyerror = { version = "0.1.6", features = ["anyhow"]}
anyhow = "1.0.32"
openraft = { version="0.7.0", path= "../openraft" }
async-trait = "0.1.36"
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 @@ -11,13 +9,13 @@ 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::storage::HardState;
use openraft::storage::LogState;
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,7 +17,7 @@ readme = "../README.md"

[dependencies]
anyhow = "1.0.32"
anyerror = { version = "0.1.1", features=["anyhow"]}
anyerror = { version = "0.1.6", features = ["anyhow"]}
async-trait = "0.1.36"
byte-unit = "4.0.12"
bytes = "1.0"
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 @@ -491,7 +491,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); // This will always succeed.
// This will always succeed.
let _ = chan_tx.send(snapshot.meta.last_log_id);
}
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 @@ -198,7 +198,7 @@ pub enum ReplicationError {

#[error(transparent)]
IO {
#[backtrace]
#[cfg_attr(feature = "bt", backtrace)]
#[from]
source: std::io::Error,
},
Expand All @@ -212,7 +212,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`.

mod config;
mod core;
Expand All @@ -24,6 +30,8 @@ pub mod types;
mod metrics_wait_test;
mod storage_helper;

pub use anyerror;
pub use anyerror::AnyError;
pub use async_trait;

pub use crate::config::Config;
Expand Down
6 changes: 2 additions & 4 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 @@ -59,8 +58,7 @@ impl StorageIOError {
subject,
verb,
source,
// TODO: use crate backtrace instead of std::backtrace.
backtrace: format!("{:?}", Backtrace::capture()),
backtrace: anyerror::backtrace_str(),
}
}
}
8 changes: 4 additions & 4 deletions openraft/src/types/v070/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 @@ -116,15 +116,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 @@ -135,5 +135,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 ea69647

Please sign in to comment.