Skip to content

Commit

Permalink
Merge pull request #8693 from zhang2014/exchange_backtrace
Browse files Browse the repository at this point in the history
chore(cluster): exchange error backtrace
  • Loading branch information
BohuTANG authored Nov 8, 2022
2 parents 062d6e0 + db0e840 commit 5ef66df
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
52 changes: 48 additions & 4 deletions src/common/exception/src/exception_flight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,43 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::mem::size_of;
use std::sync::Arc;

use common_arrow::arrow_format::flight::data::FlightData;

use crate::exception::ErrorCodeBacktrace;
use crate::ErrorCode;
use crate::Result;

impl From<ErrorCode> for FlightData {
fn from(error: ErrorCode) -> Self {
// TODO: has stack trace
let error_message = error.message();
let error_backtrace = error.backtrace_str();

let message = error_message.as_bytes();
let backtrace = error_backtrace.as_bytes();

let mut data_body = Vec::with_capacity(16 + message.len() + backtrace.len());

data_body.extend((message.len() as u64).to_be_bytes());
data_body.extend_from_slice(message);
data_body.extend((backtrace.len() as u64).to_be_bytes());
data_body.extend_from_slice(backtrace);

FlightData {
data_body,
app_metadata: vec![0x02],
data_body: error.message().into_bytes(),
data_header: error.code().to_be_bytes().to_vec(),
flight_descriptor: None,
}
}
}

fn read_be_u64(bytes: &[u8]) -> u64 {
u64::from_be_bytes(bytes[0..size_of::<u64>()].try_into().unwrap())
}

impl TryFrom<FlightData> for ErrorCode {
type Error = ErrorCode;

Expand All @@ -37,8 +57,32 @@ impl TryFrom<FlightData> for ErrorCode {
Err(_) => Err(ErrorCode::BadBytes("Cannot parse inf usize.")),
Ok(slice) => {
let code = u16::from_be_bytes(slice);
let message = String::from_utf8(flight_data.data_body)?;
Ok(ErrorCode::create(code, message, None, None))
let data_body = flight_data.data_body;

let mut data_offset = 0;
let message_len = read_be_u64(&data_body) as usize;
data_offset += size_of::<u64>();
let message = &data_body[data_offset..data_offset + message_len];
data_offset += message_len;
data_offset += size_of::<u64>();
let backtrace = &data_body[data_offset..];

match backtrace.is_empty() {
true => Ok(ErrorCode::create(
code,
String::from_utf8(message.to_vec())?,
None,
None,
)),
false => Ok(ErrorCode::create(
code,
String::from_utf8(message.to_vec())?,
None,
Some(ErrorCodeBacktrace::Serialized(Arc::new(String::from_utf8(
backtrace.to_vec(),
)?))),
)),
}
}
}
}
Expand Down
37 changes: 37 additions & 0 deletions src/common/exception/tests/it/exception_flight.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2022 Datafuse Labs.
//
// 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.

use std::backtrace::Backtrace;
use std::sync::Arc;

use common_arrow::arrow_format::flight::data::FlightData;
use common_exception::exception::ErrorCodeBacktrace;
use common_exception::ErrorCode;
use common_exception::Result;

#[test]
fn test_serialize() -> Result<()> {
let error_code = ErrorCode::create(
1,
String::from("test_message"),
None,
Some(ErrorCodeBacktrace::Origin(Arc::new(Backtrace::capture()))),
);
let backtrace_str = error_code.backtrace_str();
let error_code = ErrorCode::try_from(FlightData::from(error_code))?;
assert_eq!(1, error_code.code());
assert_eq!(String::from("test_message"), error_code.message());
assert_eq!(backtrace_str, error_code.backtrace_str());
Ok(())
}
1 change: 1 addition & 0 deletions src/common/exception/tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
// limitations under the License.

mod exception;
mod exception_flight;
mod prelude;

1 comment on commit 5ef66df

@vercel
Copy link

@vercel vercel bot commented on 5ef66df Nov 8, 2022

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

databend – ./

databend-databend.vercel.app
databend.vercel.app
databend.rs
databend-git-main-databend.vercel.app

Please sign in to comment.