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

Make backtrace as a cargo feature #7527

Merged
merged 6 commits into from
Sep 15, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ jobs:
with:
rust-version: stable
- name: Run tests (excluding doctests)
run: cargo test --lib --tests --bins --features avro,json,dictionary_expressions
run: cargo test --lib --tests --bins --features avro,json,dictionary_expressions,backtrace
- name: Verify Working Directory Clean
run: git diff --exit-code

Expand Down Expand Up @@ -302,12 +302,13 @@ jobs:
shell: bash
run: |
export PATH=$PATH:$HOME/d/protoc/bin
cargo test --lib --tests --bins --features avro,json,dictionary_expressions
cargo test --lib --tests --bins --features avro,json,dictionary_expressions,backtrace
cd datafusion-cli
cargo test --lib --tests --bins --all-features
env:
# do not produce debug symbols to keep memory usage down
RUSTFLAGS: "-C debuginfo=0"
RUST_BACKTRACE: "1"

macos:
name: cargo test (mac)
Expand Down Expand Up @@ -337,12 +338,13 @@ jobs:
- name: Run tests (excluding doctests)
shell: bash
run: |
cargo test --lib --tests --bins --features avro,json,dictionary_expressions
cargo test --lib --tests --bins --features avro,json,dictionary_expressions,backtrace
cd datafusion-cli
cargo test --lib --tests --bins --all-features
env:
# do not produce debug symbols to keep memory usage down
RUSTFLAGS: "-C debuginfo=0"
RUST_BACKTRACE: "1"

test-datafusion-pyarrow:
name: cargo test pyarrow (amd64)
Expand Down
1 change: 1 addition & 0 deletions datafusion/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ path = "src/lib.rs"

[features]
avro = ["apache-avro"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As written, to be enabled, a user would have to use datafusion_common directly (they couldn't activate it through datafusion)

To enable it via datafusion we also need to add it to core/Cargo.toml, like this:

https://github.com/apache/arrow-datafusion/blob/cde74016e930ffd9c55eed403b84bcd026f38d0f/datafusion/core/Cargo.toml#L44

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks I was little bit struggling on how it make on the datafusion level

backtrace = []
compression = ["xz2", "bzip2", "flate2", "zstd", "async-compression"]
default = ["compression", "parquet"]
pyarrow = ["pyo3", "arrow/pyarrow"]
Expand Down
64 changes: 58 additions & 6 deletions datafusion/common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
// under the License.

//! DataFusion error types

#[cfg(feature = "backtrace")]
use std::backtrace::{Backtrace, BacktraceStatus};

use std::error::Error;
use std::fmt::{Display, Formatter};
use std::io;
Expand Down Expand Up @@ -368,6 +369,8 @@ impl From<DataFusionError> for io::Error {
}

impl DataFusionError {
const BACK_TRACE_SEP: &str = "\n\nbacktrace: ";

/// Get deepest underlying [`DataFusionError`]
///
/// [`DataFusionError`]s sometimes form a chain, such as `DataFusionError::ArrowError()` in order to conform
Expand Down Expand Up @@ -412,20 +415,34 @@ impl DataFusionError {

pub fn strip_backtrace(&self) -> String {
self.to_string()
.split("\n\nbacktrace: ")
.split(Self::BACK_TRACE_SEP)
.collect::<Vec<&str>>()
.first()
.unwrap_or(&"")
.to_string()
}

/// To enable optional rust backtrace in DataFusion:
/// - [`Setup Env Variables`]<https://doc.rust-lang.org/std/backtrace/index.html#environment-variables>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

/// - Enable `backtrace` cargo feature
///
/// Example:
/// cargo build --features 'backtrace'
/// RUST_BACKTRACE=1 ./app
#[inline(always)]
pub fn get_back_trace() -> String {
let back_trace = Backtrace::capture();
if back_trace.status() == BacktraceStatus::Captured {
return format!("\n\nbacktrace: {}", back_trace);
#[cfg(feature = "backtrace")]
{
let back_trace = Backtrace::capture();
if back_trace.status() == BacktraceStatus::Captured {
return format!("{}{}", Self::BACK_TRACE_SEP, back_trace);
}

"".to_owned()
}

"".to_string()
#[cfg(not(feature = "backtrace"))]
"".to_owned()
}
}

Expand Down Expand Up @@ -522,6 +539,41 @@ mod test {
assert_eq!(res.strip_backtrace(), "Arrow error: Schema error: bar");
}

// RUST_BACKTRACE=1 cargo test --features backtrace --package datafusion-common --lib -- error::test::test_backtrace
#[cfg(feature = "backtrace")]
#[test]
#[allow(clippy::unnecessary_literal_unwrap)]
fn test_enabled_backtrace() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let res: Result<(), DataFusionError> = plan_err!("Err");
let err = res.unwrap_err().to_string();
assert!(err.contains(DataFusionError::BACK_TRACE_SEP));
assert_eq!(
err.split(DataFusionError::BACK_TRACE_SEP)
.collect::<Vec<&str>>()
.get(0)
.unwrap(),
&"Error during planning: Err"
);
assert!(
err.split(DataFusionError::BACK_TRACE_SEP)
.collect::<Vec<&str>>()
.get(1)
.unwrap()
.len()
> 0
);
}

#[cfg(not(feature = "backtrace"))]
#[test]
#[allow(clippy::unnecessary_literal_unwrap)]
fn test_disabled_backtrace() {
let res: Result<(), DataFusionError> = plan_err!("Err");
let res = res.unwrap_err().to_string();
assert!(!res.contains(DataFusionError::BACK_TRACE_SEP));
assert_eq!(res, "Error during planning: Err");
}

#[test]
fn test_find_root_error() {
do_root_test(
Expand Down
1 change: 1 addition & 0 deletions datafusion/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ path = "src/lib.rs"
[features]
# Used to enable the avro format
avro = ["apache-avro", "num-traits", "datafusion-common/avro"]
backtrace = ["datafusion-common/backtrace"]
compression = ["xz2", "bzip2", "flate2", "zstd", "async-compression"]
crypto_expressions = ["datafusion-physical-expr/crypto_expressions", "datafusion-optimizer/crypto_expressions"]
default = ["crypto_expressions", "encoding__expressions", "regex_expressions", "unicode_expressions", "compression"]
Expand Down