Skip to content

Commit ea69647

Browse files
committed
Feature: add feature-flag: bt enables backtrace
`--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 ```
1 parent 1d00244 commit ea69647

9 files changed

Lines changed: 76 additions & 17 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,56 @@ jobs:
5555
args: --all-targets -- -D warnings -A clippy::bool-assert-comparison
5656

5757

58+
- name: Upload artifact
59+
uses: actions/upload-artifact@v2
60+
if: failure()
61+
with:
62+
path: |
63+
openraft/_log/
64+
65+
ut-on-stable-rust:
66+
name: unittest on stable rust
67+
runs-on: ubuntu-latest
68+
69+
steps:
70+
- name: Setup | Checkout
71+
uses: actions/checkout@v2
72+
73+
- name: Setup | Toolchain
74+
uses: actions-rs/toolchain@v1.0.6
75+
with:
76+
toolchain: "stable"
77+
override: true
78+
components: rustfmt, clippy
79+
80+
- name: Unit Tests
81+
uses: actions-rs/cargo@v1
82+
with:
83+
command: test
84+
env:
85+
# Parallel tests block each other and result in timeout.
86+
RUST_TEST_THREADS: 2
87+
RUST_LOG: debug
88+
RUST_BACKTRACE: full
89+
90+
91+
- name: Build | Release Mode | No features
92+
uses: actions-rs/cargo@v1
93+
with:
94+
command: build
95+
args: --release
96+
97+
98+
- name: Build | Release Mode | No features
99+
uses: actions-rs/cargo@v1
100+
with:
101+
command: build
102+
args: --release --all-features
103+
env:
104+
# Enable unstable feature on stalbe rust.
105+
RUSTC_BOOTSTRAP: 1
106+
107+
58108
- name: Upload artifact
59109
uses: actions/upload-artifact@v2
60110
if: failure()

memstore/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ repository = "https://github.com/datafuselabs/openraft"
1616
readme = "README.md"
1717

1818
[dependencies]
19-
anyerror = { version = "0.1.1", features=["anyhow"]}
19+
anyerror = { version = "0.1.6", features = ["anyhow"]}
2020
anyhow = "1.0.32"
2121
openraft = { version="0.7.0", path= "../openraft" }
2222
async-trait = "0.1.36"

memstore/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![feature(backtrace)]
2-
31
#[cfg(test)]
42
mod test;
53

@@ -11,13 +9,13 @@ use std::ops::RangeBounds;
119
use std::sync::Arc;
1210
use std::sync::Mutex;
1311

14-
use anyerror::AnyError;
1512
use openraft::async_trait::async_trait;
1613
use openraft::raft::Entry;
1714
use openraft::raft::EntryPayload;
1815
use openraft::storage::HardState;
1916
use openraft::storage::LogState;
2017
use openraft::storage::Snapshot;
18+
use openraft::AnyError;
2119
use openraft::AppData;
2220
use openraft::AppDataResponse;
2321
use openraft::EffectiveMembership;

openraft/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ readme = "../README.md"
1717

1818
[dependencies]
1919
anyhow = "1.0.32"
20-
anyerror = { version = "0.1.1", features=["anyhow"]}
20+
anyerror = { version = "0.1.6", features = ["anyhow"]}
2121
async-trait = "0.1.36"
2222
byte-unit = "4.0.12"
2323
bytes = "1.0"
@@ -44,5 +44,9 @@ tracing-subscriber = { version = "0.3.3", features=["env-filter"] }
4444
[features]
4545
docinclude = [] # Used only for activating `doc(include="...")` on nightly.
4646

47+
# Enable backtrace when generating an error.
48+
# Stable rust does not support backtrace.
49+
bt = ["anyerror/backtrace", "anyhow/backtrace"]
50+
4751
[package.metadata.docs.rs]
4852
features = ["docinclude"] # Activate `docinclude` during docs.rs build.

openraft/src/core/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,8 @@ impl<D: AppData, R: AppDataResponse, N: RaftNetwork<D>, S: RaftStorage<D, R>> Ra
491491
Ok(res) => match res {
492492
Ok(snapshot) => {
493493
let _ = tx_compaction.try_send(SnapshotUpdate::SnapshotComplete(snapshot.meta.last_log_id));
494-
let _ = chan_tx.send(snapshot.meta.last_log_id); // This will always succeed.
494+
// This will always succeed.
495+
let _ = chan_tx.send(snapshot.meta.last_log_id);
495496
}
496497
Err(err) => {
497498
tracing::error!({error=%err}, "error while generating snapshot");

openraft/src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ pub enum ReplicationError {
198198

199199
#[error(transparent)]
200200
IO {
201-
#[backtrace]
201+
#[cfg_attr(feature = "bt", backtrace)]
202202
#[from]
203203
source: std::io::Error,
204204
},
@@ -212,7 +212,7 @@ pub enum ReplicationError {
212212

213213
#[error(transparent)]
214214
Network {
215-
#[backtrace]
215+
#[cfg_attr(feature = "bt", backtrace)]
216216
source: anyhow::Error,
217217
},
218218
}

openraft/src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#![doc = include_str!("../README.md")]
2-
#![feature(backtrace)]
2+
#![cfg_attr(feature = "bt", feature(backtrace))]
3+
4+
//! # Feature flags
5+
//!
6+
//! - `bt`: Enable backtrace: generate backtrace for errors. This requires a unstable feature `backtrace` thus it can
7+
//! not be used with stable rust, unless explicity allowing using unstable features in stable rust with
8+
//! `RUSTC_BOOTSTRAP=1`.
39
410
mod config;
511
mod core;
@@ -24,6 +30,8 @@ pub mod types;
2430
mod metrics_wait_test;
2531
mod storage_helper;
2632

33+
pub use anyerror;
34+
pub use anyerror::AnyError;
2735
pub use async_trait;
2836

2937
pub use crate::config::Config;

openraft/src/storage_error.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::backtrace::Backtrace;
21
use std::fmt::Formatter;
32

43
use anyerror::AnyError;
@@ -15,7 +14,7 @@ impl DefensiveError {
1514
DefensiveError {
1615
subject,
1716
violation,
18-
backtrace: format!("{:?}", Backtrace::capture()),
17+
backtrace: anyerror::backtrace_str(),
1918
}
2019
}
2120
}
@@ -59,8 +58,7 @@ impl StorageIOError {
5958
subject,
6059
verb,
6160
source,
62-
// TODO: use crate backtrace instead of std::backtrace.
63-
backtrace: format!("{:?}", Backtrace::capture()),
61+
backtrace: anyerror::backtrace_str(),
6462
}
6563
}
6664
}

openraft/src/types/v070/storage_error.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub struct DefensiveError {
1818
/// The description of the violation.
1919
pub violation: Violation,
2020

21-
pub backtrace: String,
21+
pub backtrace: Option<String>,
2222
}
2323

2424
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
@@ -116,15 +116,15 @@ pub enum StorageError {
116116
#[error(transparent)]
117117
Defensive {
118118
#[from]
119-
#[backtrace]
119+
#[cfg_attr(feature = "bt", backtrace)]
120120
source: DefensiveError,
121121
},
122122

123123
/// An error raised by io operation.
124124
#[error(transparent)]
125125
IO {
126126
#[from]
127-
#[backtrace]
127+
#[cfg_attr(feature = "bt", backtrace)]
128128
source: StorageIOError,
129129
},
130130
}
@@ -135,5 +135,5 @@ pub struct StorageIOError {
135135
pub(crate) subject: ErrorSubject,
136136
pub(crate) verb: ErrorVerb,
137137
pub(crate) source: AnyError,
138-
pub(crate) backtrace: String,
138+
pub(crate) backtrace: Option<String>,
139139
}

0 commit comments

Comments
 (0)