feat: manual compact api (#1912)

* merge develop

* chore: merge develop

* fix: some cr commentx

* fix: cr comments
This commit is contained in:
Lei, HUANG
2023-07-11 12:00:39 +08:00
committed by GitHub
parent fc850c9988
commit a7ea3bbc16
22 changed files with 327 additions and 85 deletions

View File

@@ -346,13 +346,6 @@ pub enum Error {
source: catalog::error::Error,
},
#[snafu(display("Failed to find table {} from catalog, source: {}", table_name, source))]
FindTable {
table_name: String,
location: Location,
source: catalog::error::Error,
},
#[snafu(display("Failed to initialize meta client, source: {}", source))]
MetaClientInit {
location: Location,
@@ -499,7 +492,6 @@ impl ErrorExt for Error {
DecodeLogicalPlan { source, .. } => source.status_code(),
NewCatalog { source, .. } | RegisterSchema { source, .. } => source.status_code(),
FindTable { source, .. } => source.status_code(),
CreateTable { source, .. } => source.status_code(),
DropTable { source, .. } => source.status_code(),
FlushTable { source, .. } => source.status_code(),

View File

@@ -198,9 +198,7 @@ impl Instance {
DdlExpr::CreateDatabase(expr) => self.handle_create_database(expr, query_ctx).await,
DdlExpr::DropTable(expr) => self.handle_drop_table(expr).await,
DdlExpr::FlushTable(expr) => self.handle_flush_table(expr).await,
Expr::CompactTable(_) => {
unreachable!("https://github.com/GreptimeTeam/greptimedb/pull/1912")
}
Expr::CompactTable(expr) => self.handle_compact_table(expr).await,
}
}
}

View File

@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use api::v1::{AlterExpr, CreateTableExpr, DropTableExpr, FlushTableExpr};
use api::v1::{AlterExpr, CompactTableExpr, CreateTableExpr, DropTableExpr, FlushTableExpr};
use common_catalog::consts::IMMUTABLE_FILE_ENGINE;
use common_catalog::format_full_table_name;
use common_grpc_expr::{alter_expr_to_request, create_expr_to_request};
@@ -20,7 +20,7 @@ use common_query::Output;
use common_telemetry::info;
use session::context::QueryContext;
use snafu::prelude::*;
use table::requests::{DropTableRequest, FlushTableRequest};
use table::requests::{CompactTableRequest, DropTableRequest, FlushTableRequest};
use crate::error::{
AlterExprToRequestSnafu, BumpTableIdSnafu, CatalogSnafu, CreateExprToRequestSnafu,
@@ -133,6 +133,25 @@ impl Instance {
.execute(SqlRequest::FlushTable(req), QueryContext::arc())
.await
}
pub(crate) async fn handle_compact_table(&self, expr: CompactTableExpr) -> Result<Output> {
let table_name = if expr.table_name.trim().is_empty() {
None
} else {
Some(expr.table_name)
};
let req = CompactTableRequest {
catalog_name: expr.catalog_name,
schema_name: expr.schema_name,
table_name,
region_number: expr.region_number,
wait: None,
};
self.sql_handler()
.execute(SqlRequest::CompactTable(req), QueryContext::arc())
.await
}
}
#[cfg(test)]

View File

@@ -31,6 +31,7 @@ use crate::error::{
use crate::instance::sql::table_idents_to_full_name;
mod alter;
mod compact_table;
mod create;
mod create_external;
mod drop_table;
@@ -44,6 +45,7 @@ pub enum SqlRequest {
Alter(AlterTableRequest),
DropTable(DropTableRequest),
FlushTable(FlushTableRequest),
CompactTable(CompactTableRequest),
}
// Handler to execute SQL except query
@@ -74,6 +76,7 @@ impl SqlHandler {
SqlRequest::Alter(req) => self.alter_table(req).await,
SqlRequest::DropTable(req) => self.drop_table(req).await,
SqlRequest::FlushTable(req) => self.flush_table(req).await,
SqlRequest::CompactTable(req) => self.compact_table(req).await,
};
if let Err(e) = &result {
error!(e; "{query_ctx}");

View File

@@ -0,0 +1,77 @@
// Copyright 2023 Greptime Team
//
// 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 catalog::CatalogManagerRef;
use common_query::Output;
use snafu::{OptionExt, ResultExt};
use table::requests::CompactTableRequest;
use crate::error;
use crate::error::{CatalogSnafu, Result};
use crate::sql::SqlHandler;
impl SqlHandler {
pub(crate) async fn compact_table(&self, req: CompactTableRequest) -> Result<Output> {
if let Some(table) = &req.table_name {
self.compact_table_inner(
&self.catalog_manager,
&req.catalog_name,
&req.schema_name,
table,
req.region_number,
req.wait,
)
.await?;
} else {
let all_table_names = self
.catalog_manager
.table_names(&req.catalog_name, &req.schema_name)
.await
.context(CatalogSnafu)?;
let _ = futures::future::join_all(all_table_names.iter().map(|table| {
self.compact_table_inner(
&self.catalog_manager,
&req.catalog_name,
&req.schema_name,
table,
req.region_number,
req.wait,
)
}))
.await
.into_iter()
.collect::<Result<Vec<_>>>()?;
}
Ok(Output::AffectedRows(0))
}
async fn compact_table_inner(
&self,
catalog_manager: &CatalogManagerRef,
catalog_name: &str,
schema_name: &str,
table_name: &str,
region: Option<u32>,
wait: Option<bool>,
) -> Result<()> {
catalog_manager
.table(catalog_name, schema_name, table_name)
.await
.context(CatalogSnafu)?
.context(error::TableNotFoundSnafu { table_name })?
.compact(region, wait)
.await
.context(error::FlushTableSnafu { table_name })
}
}

View File

@@ -67,7 +67,7 @@ impl SqlHandler {
catalog_manager
.table(catalog_name, schema_name, table_name)
.await
.context(error::FindTableSnafu { table_name })?
.context(CatalogSnafu)?
.context(error::TableNotFoundSnafu { table_name })?
.flush(region, wait)
.await