Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
zenus committed Jun 15, 2024
2 parents d7fe74e + f30b9e0 commit d812a8c
Show file tree
Hide file tree
Showing 31 changed files with 391 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 13 additions & 3 deletions src/common/exception/src/exception_backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,19 @@ use std::sync::Arc;

use crate::exception::ErrorCodeBacktrace;

// 0: not specified 1: disable 2: enable
pub static USER_SET_ENABLE_BACKTRACE: AtomicUsize = AtomicUsize::new(0);

pub fn set_backtrace(switch: bool) {
if switch {
USER_SET_ENABLE_BACKTRACE.store(2, Ordering::Relaxed);
} else {
USER_SET_ENABLE_BACKTRACE.store(1, Ordering::Relaxed);
}
}

fn enable_rust_backtrace() -> bool {
static ENABLED: AtomicUsize = AtomicUsize::new(0);
match ENABLED.load(Ordering::Relaxed) {
match USER_SET_ENABLE_BACKTRACE.load(Ordering::Relaxed) {
0 => {}
1 => return false,
_ => return true,
Expand All @@ -35,7 +45,7 @@ fn enable_rust_backtrace() -> bool {
},
};

ENABLED.store(enabled as usize + 1, Ordering::Relaxed);
USER_SET_ENABLE_BACKTRACE.store(enabled as usize + 1, Ordering::Relaxed);
enabled
}

Expand Down
2 changes: 2 additions & 0 deletions src/common/exception/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ mod with_context;
pub use exception::ErrorCode;
pub use exception::Result;
pub use exception::ToErrorCode;
pub use exception_backtrace::set_backtrace;
pub use exception_backtrace::USER_SET_ENABLE_BACKTRACE;
pub use exception_into::SerializedError;
pub use with_context::ErrorWithContext;
pub use with_context::WithContext;
1 change: 1 addition & 0 deletions src/common/tracing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ test = true

[dependencies]
databend-common-base = { workspace = true }
databend-common-exception = { workspace = true }
defer = "0.2"
fern = "0.6.2"
humantime = "2.1.0"
Expand Down
21 changes: 18 additions & 3 deletions src/common/tracing/src/panic_hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@

use std::backtrace::Backtrace;
use std::panic::PanicInfo;
use std::sync::atomic::Ordering;

use databend_common_base::runtime::LimitMemGuard;
use databend_common_exception::USER_SET_ENABLE_BACKTRACE;
use log::error;

pub fn set_panic_hook() {
Expand All @@ -31,12 +33,25 @@ pub fn set_panic_hook() {
}));
}

fn should_backtrace() -> bool {
// if user not specify or user set to enable, we should backtrace
match USER_SET_ENABLE_BACKTRACE.load(Ordering::Relaxed) {
0 => true,
1 => false,
_ => true,
}
}

pub fn log_panic(panic: &PanicInfo) {
let backtrace = Backtrace::force_capture();
let backtrace_str = format!("{:?}", backtrace);
let backtrace_str = if should_backtrace() {
let backtrace = Backtrace::force_capture();
format!("{:?}", backtrace)
} else {
String::new()
};

eprintln!("{}", panic);
eprintln!("{}", backtrace);
eprintln!("{}", backtrace_str);

if let Some(location) = panic.location() {
error!(
Expand Down
2 changes: 2 additions & 0 deletions src/query/ast/src/ast/statements/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ mod show;
mod stage;
mod statement;
mod stream;
mod system_action;
mod table;
mod task;
mod udf;
Expand Down Expand Up @@ -87,6 +88,7 @@ pub use show::*;
pub use stage::*;
pub use statement::*;
pub use stream::*;
pub use system_action::*;
pub use table::*;
pub use task::*;
pub use udf::*;
Expand Down
4 changes: 4 additions & 0 deletions src/query/ast/src/ast/statements/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,9 @@ pub enum Statement {
priority: Priority,
object_id: String,
},

// System actions
System(SystemStmt),
}

impl Statement {
Expand Down Expand Up @@ -721,6 +724,7 @@ impl Display for Statement {
write!(f, " {priority}")?;
write!(f, " '{object_id}'")?;
}
Statement::System(stmt) => write!(f, "{stmt}")?,
}
Ok(())
}
Expand Down
46 changes: 46 additions & 0 deletions src/query/ast/src/ast/statements/system_action.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2021 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::fmt::Display;
use std::fmt::Formatter;

use derive_visitor::Drive;
use derive_visitor::DriveMut;

#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
pub struct SystemStmt {
pub action: SystemAction,
}

impl Display for SystemStmt {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "SYSTEM {}", self.action)
}
}

#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
pub enum SystemAction {
Backtrace(bool),
}

impl Display for SystemAction {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
match self {
SystemAction::Backtrace(switch) => match switch {
true => write!(f, "ENABLE EXCEPTION_BACKTRACE"),
false => write!(f, "DISABLE EXCEPTION_BACKTRACE"),
},
}
}
}
2 changes: 2 additions & 0 deletions src/query/ast/src/ast/visitors/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,4 +844,6 @@ pub trait Visitor<'ast>: Sized {
fn visit_drop_sequence(&mut self, _stmt: &'ast DropSequenceStmt) {}
fn visit_set_priority(&mut self, _priority: &'ast Priority, _object_id: &'ast str) {}
fn visit_multi_table_insert(&mut self, insert: &'ast InsertMultiTableStmt);

fn visit_system(&mut self, _stmt: &'ast SystemStmt) {}
}
1 change: 1 addition & 0 deletions src/query/ast/src/ast/visitors/visitor_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,4 +854,5 @@ pub trait VisitorMut: Sized {
fn visit_create_sequence(&mut self, _stmt: &mut CreateSequenceStmt) {}
fn visit_drop_sequence(&mut self, _stmt: &mut DropSequenceStmt) {}
fn visit_set_priority(&mut self, _priority: &mut Priority, _object_id: &mut String) {}
fn visit_system(&mut self, _stmt: &mut SystemStmt) {}
}
1 change: 1 addition & 0 deletions src/query/ast/src/ast/visitors/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,5 +597,6 @@ pub fn walk_statement<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Statem
priority,
object_id,
} => visitor.visit_set_priority(priority, object_id),
Statement::System(stmt) => visitor.visit_system(stmt),
}
}
1 change: 1 addition & 0 deletions src/query/ast/src/ast/visitors/walk_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,5 +596,6 @@ pub fn walk_statement_mut<V: VisitorMut>(visitor: &mut V, statement: &mut Statem
priority,
object_id,
} => visitor.visit_set_priority(priority, object_id),
Statement::System(stmt) => visitor.visit_system(stmt),
}
}
30 changes: 29 additions & 1 deletion src/query/ast/src/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2047,6 +2047,13 @@ pub fn statement_body(i: Input) -> IResult<Statement> {
|(_, _, script)| Statement::ExecuteImmediate(ExecuteImmediateStmt { script }),
);

let system_action = map(
rule! {
SYSTEM ~ #action
},
|(_, action)| Statement::System(SystemStmt { action }),
);

alt((
// query, explain,show
rule!(
Expand All @@ -2063,7 +2070,8 @@ pub fn statement_body(i: Input) -> IResult<Statement> {
| #show_locks : "`SHOW LOCKS [IN ACCOUNT] [WHERE ...]`"
| #kill_stmt : "`KILL (QUERY | CONNECTION) <object_id>`"
| #vacuum_temp_files : "VACUUM TEMPORARY FILES [RETAIN number SECONDS|DAYS] [LIMIT number]"
| #set_priority: "SET PRIORITY (HIGH | MEDIUM | LOW) <object_id>"
| #set_priority: "`SET PRIORITY (HIGH | MEDIUM | LOW) <object_id>`"
| #system_action: "`SYSTEM (ENABLE | DISABLE) EXCEPTION_BACKTRACE`"
),
// database
rule!(
Expand Down Expand Up @@ -3744,6 +3752,26 @@ pub fn priority(i: Input) -> IResult<Priority> {
))(i)
}

pub fn action(i: Input) -> IResult<SystemAction> {
let mut backtrace = map(
rule! {
#switch ~ EXCEPTION_BACKTRACE
},
|(switch, _)| SystemAction::Backtrace(switch),
);
// add other system action type here
rule!(
#backtrace
)(i)
}

pub fn switch(i: Input) -> IResult<bool> {
alt((
value(true, rule! { ENABLE }),
value(false, rule! { DISABLE }),
))(i)
}

pub fn limit_where(i: Input) -> IResult<ShowLimit> {
map(
rule! {
Expand Down
8 changes: 8 additions & 0 deletions src/query/ast/src/parser/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,8 @@ pub enum TokenKind {
DETAILED_OUTPUT,
#[token("DESCRIBE", ignore(ascii_case))]
DESCRIBE,
#[token("DISABLE", ignore(ascii_case))]
DISABLE,
#[token("DISABLE_VARIANT_CHECK", ignore(ascii_case))]
DISABLE_VARIANT_CHECK,
#[token("DISTINCT", ignore(ascii_case))]
Expand Down Expand Up @@ -540,6 +542,8 @@ pub enum TokenKind {
ELSE,
#[token("EMPTY_FIELD_AS", ignore(ascii_case))]
EMPTY_FIELD_AS,
#[token("ENABLE", ignore(ascii_case))]
ENABLE,
#[token("ENABLE_VIRTUAL_HOST_STYLE", ignore(ascii_case))]
ENABLE_VIRTUAL_HOST_STYLE,
#[token("END", ignore(ascii_case))]
Expand All @@ -556,6 +560,8 @@ pub enum TokenKind {
ERROR_ON_COLUMN_COUNT_MISMATCH,
#[token("ESCAPE", ignore(ascii_case))]
ESCAPE,
#[token("EXCEPTION_BACKTRACE", ignore(ascii_case))]
EXCEPTION_BACKTRACE,
#[token("EXISTS", ignore(ascii_case))]
EXISTS,
#[token("EXPLAIN", ignore(ascii_case))]
Expand Down Expand Up @@ -1055,6 +1061,8 @@ pub enum TokenKind {
SOUNDS,
#[token("SYNC", ignore(ascii_case))]
SYNC,
#[token("SYSTEM", ignore(ascii_case))]
SYSTEM,
#[token("STORAGE_TYPE", ignore(ascii_case))]
STORAGE_TYPE,
#[token("TABLE", ignore(ascii_case))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,7 @@ impl AccessChecker for PrivilegeAccess {
self.validate_access(&GrantObject::Global, UserPrivilegeType::Grant,false)
.await?;
}
Plan::SetVariable(_) | Plan::UnSetVariable(_) | Plan::Kill(_) | Plan::SetPriority(_) => {
Plan::SetVariable(_) | Plan::UnSetVariable(_) | Plan::Kill(_) | Plan::SetPriority(_) | Plan::System(_) => {
self.validate_access(&GrantObject::Global, UserPrivilegeType::Super, false)
.await?;
}
Expand Down
5 changes: 5 additions & 0 deletions src/query/service/src/interpreters/interpreter_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use crate::interpreters::interpreter_notification_drop::DropNotificationInterpre
use crate::interpreters::interpreter_presign::PresignInterpreter;
use crate::interpreters::interpreter_role_show::ShowRolesInterpreter;
use crate::interpreters::interpreter_set_priority::SetPriorityInterpreter;
use crate::interpreters::interpreter_system_action::SystemActionInterpreter;
use crate::interpreters::interpreter_table_create::CreateTableInterpreter;
use crate::interpreters::interpreter_table_revert::RevertTableInterpreter;
use crate::interpreters::interpreter_task_alter::AlterTaskInterpreter;
Expand Down Expand Up @@ -598,6 +599,10 @@ impl InterpreterFactory {
ctx,
*p.clone(),
)?)),
Plan::System(p) => Ok(Arc::new(SystemActionInterpreter::try_create(
ctx,
*p.clone(),
)?)),
}
}
}
Loading

0 comments on commit d812a8c

Please sign in to comment.