Skip to content

Commit

Permalink
chore: add metrics for truncate table
Browse files Browse the repository at this point in the history
  • Loading branch information
DevilExileSu committed Sep 16, 2023
1 parent 1d607f0 commit a0decb6
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ datafusion-substrait = { git = "https://github.com/waynexia/arrow-datafusion.git
derive_builder = "0.12"
futures = "0.3"
futures-util = "0.3"
# greptime-proto = { git = "https://github.com/GreptimeTeam/greptime-proto.git", rev = "1969b5d610209ad1c2ad8e4cd3e9c3c24e40f1c2" }
greptime-proto = {git = "https://github.com/DevilExileSu/greptime-proto.git", rev = "637f06ca7fe097ab22277f473d33840d92f4190c"}
greptime-proto = {git = "https://github.com/GreptimeTeam/greptime-proto.git", rev = "637f06ca7fe097ab22277f473d33840d92f4190c"}
humantime-serde = "1.1"
itertools = "0.10"
lazy_static = "1.4"
Expand Down
42 changes: 23 additions & 19 deletions src/common/meta/src/ddl/truncate_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,24 @@ use common_error::ext::ErrorExt;
use common_error::status_code::StatusCode;
use common_procedure::error::{FromJsonSnafu, ToJsonSnafu};
use common_procedure::{
Context as ProcedureContext, Error as ProcedureError, LockKey, Procedure,
Result as ProcedureResult, Status,
Context as ProcedureContext, LockKey, Procedure, Result as ProcedureResult, Status,
};
use common_telemetry::debug;
use futures::future::join_all;
use serde::{Deserialize, Serialize};
use snafu::{ensure, ResultExt};
use store_api::storage::RegionId;
use strum::AsRefStr;
use table::engine::TableReference;
use table::metadata::{RawTableInfo, TableId};

use super::utils::handle_retry_error;
use crate::ddl::utils::handle_operate_region_error;
use crate::ddl::DdlContext;
use crate::error::{self, Result, TableNotFoundSnafu};
use crate::error::{Result, TableNotFoundSnafu};
use crate::key::table_info::TableInfoValue;
use crate::key::table_name::TableNameKey;
use crate::metrics;
use crate::rpc::ddl::TruncateTableTask;
use crate::rpc::router::{find_leader_regions, find_leaders, RegionRoute};
use crate::table_name::TableName;
Expand All @@ -52,18 +54,20 @@ impl Procedure for TruncateTableProcedure {
}

async fn execute(&mut self, _ctx: &ProcedureContext) -> ProcedureResult<Status> {
let error_handler = |e| {
if matches!(e, error::Error::RetryLater { .. }) {
ProcedureError::retry_later(e)
} else {
ProcedureError::external(e)
}
};
let state = &self.data.state;

let _timer = common_telemetry::timer!(
metrics::METRIC_META_PROCEDURE_TRUNCATE_TABLE,
&[("step", state.as_ref().to_string())]
);

match self.data.state {
TruncateTableState::Prepare => self.on_prepare().await,
TruncateTableState::DatanodeTruncateTable => self.on_datanode_truncate_table().await,
TruncateTableState::DatanodeTruncateRegions => {
self.on_datanode_truncate_regions().await
}
}
.map_err(error_handler)
.map_err(handle_retry_error)
}

fn dump(&self) -> ProcedureResult<String> {
Expand All @@ -83,7 +87,7 @@ impl Procedure for TruncateTableProcedure {
}

impl TruncateTableProcedure {
pub(crate) const TYPE_NAME: &'static str = "metasrv-procedure::TruncateTableProcedure";
pub(crate) const TYPE_NAME: &'static str = "metasrv-procedure::TruncateTable";

pub(crate) fn new(
cluster_id: u64,
Expand Down Expand Up @@ -125,12 +129,12 @@ impl TruncateTableProcedure {
}
);

self.data.state = TruncateTableState::DatanodeTruncateTable;
self.data.state = TruncateTableState::DatanodeTruncateRegions;

Ok(Status::executing(true))
}

async fn on_datanode_truncate_table(&mut self) -> Result<Status> {
async fn on_datanode_truncate_regions(&mut self) -> Result<Status> {
let table_id = self.data.table_id();

let region_routes = &self.data.region_routes;
Expand Down Expand Up @@ -195,7 +199,7 @@ impl TruncateTableData {
region_routes: Vec<RegionRoute>,
) -> Self {
Self {
state: TruncateTableState::DatanodeTruncateTable,
state: TruncateTableState::Prepare,
cluster_id,
task,
table_info_value,
Expand All @@ -220,10 +224,10 @@ impl TruncateTableData {
}
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, AsRefStr)]
enum TruncateTableState {
/// Prepares to truncate the table
Prepare,
/// Datanode truncates the table
DatanodeTruncateTable,
/// Truncates regions on Datanode
DatanodeTruncateRegions,
}
1 change: 1 addition & 0 deletions src/common/meta/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ pub(crate) const METRIC_META_CREATE_SCHEMA: &str = "meta.create_schema";
pub(crate) const METRIC_META_PROCEDURE_CREATE_TABLE: &str = "meta.procedure.create_table";
pub(crate) const METRIC_META_PROCEDURE_DROP_TABLE: &str = "meta.procedure.drop_table";
pub(crate) const METRIC_META_PROCEDURE_ALTER_TABLE: &str = "meta.procedure.alter_table";
pub(crate) const METRIC_META_PROCEDURE_TRUNCATE_TABLE: &str = "meta.procedure.truncate_table";
1 change: 1 addition & 0 deletions src/store-api/src/region_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ pub struct RegionFlushRequest {}
#[derive(Debug)]
pub struct RegionCompactRequest {}

/// Truncate region request.
#[derive(Debug)]
pub struct RegionTruncateRequest {}

Expand Down

0 comments on commit a0decb6

Please sign in to comment.