Skip to content

Commit

Permalink
feat: vacuum aggregating indexes (databendlabs#12179)
Browse files Browse the repository at this point in the history
* vacuum aggregating indexes

* add dry_run impl
  • Loading branch information
ariesdevil authored Jul 27, 2023
1 parent 484b38e commit 8bd1221
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/meta/api/src/schema_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ use common_meta_app::schema::ListCatalogReq;
use common_meta_app::schema::ListDatabaseReq;
use common_meta_app::schema::ListDroppedTableReq;
use common_meta_app::schema::ListDroppedTableResp;
use common_meta_app::schema::ListIndexesByIdReq;
use common_meta_app::schema::ListIndexesReq;
use common_meta_app::schema::ListTableLockRevReq;
use common_meta_app::schema::ListTableReq;
Expand Down Expand Up @@ -134,6 +135,11 @@ pub trait SchemaApi: Send + Sync {
req: ListIndexesReq,
) -> Result<Vec<(u64, String, IndexMeta)>, KVAppError>;

async fn list_indexes_by_table_id(
&self,
req: ListIndexesByIdReq,
) -> Result<Vec<u64>, KVAppError>;

// virtual column

async fn create_virtual_column(
Expand Down
41 changes: 41 additions & 0 deletions src/meta/api/src/schema_api_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ use common_meta_app::schema::ListCatalogReq;
use common_meta_app::schema::ListDatabaseReq;
use common_meta_app::schema::ListDroppedTableReq;
use common_meta_app::schema::ListDroppedTableResp;
use common_meta_app::schema::ListIndexesByIdReq;
use common_meta_app::schema::ListIndexesReq;
use common_meta_app::schema::ListTableLockRevReq;
use common_meta_app::schema::ListTableReq;
Expand Down Expand Up @@ -1171,6 +1172,46 @@ impl<KV: kvapi::KVApi<Error = MetaError>> SchemaApi for KV {
Ok(index_metas)
}

#[tracing::instrument(level = "debug", ret, err, skip_all)]
async fn list_indexes_by_table_id(
&self,
req: ListIndexesByIdReq,
) -> Result<Vec<u64>, KVAppError> {
debug!(req = debug(&req), "SchemaApi: {}", func_name!());

// Get index id list by `prefix_list` "<prefix>/<tenant>"
let prefix_key = kvapi::KeyBuilder::new_prefixed(IndexNameIdent::PREFIX)
.push_str(&req.tenant)
.done();

let id_list = self.prefix_list_kv(&prefix_key).await?;
let mut id_name_list = Vec::with_capacity(id_list.len());
for (key, seq) in id_list.iter() {
let name_ident = IndexNameIdent::from_str_key(key).map_err(|e| {
KVAppError::MetaError(MetaError::from(InvalidReply::new("list_indexes", &e)))
})?;
let index_id = deserialize_u64(&seq.data)?;
id_name_list.push((index_id.0, name_ident.index_name));
}

debug!(ident = display(&prefix_key), "list_indexes");

if id_name_list.is_empty() {
return Ok(vec![]);
}

let index_ids = {
let index_metas = get_index_metas_by_ids(self, id_name_list).await?;
index_metas
.into_iter()
.filter(|(_, _, meta)| req.table_id == meta.table_id)
.map(|(id, _, _)| id)
.collect::<Vec<_>>()
};

Ok(index_ids)
}

// virtual column

async fn create_virtual_column(
Expand Down
15 changes: 15 additions & 0 deletions src/meta/app/src/schema/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,21 @@ impl ListIndexesReq {
}
}

#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct ListIndexesByIdReq {
pub tenant: String,
pub table_id: MetaId,
}

impl ListIndexesByIdReq {
pub fn new(tenant: impl Into<String>, table_id: MetaId) -> Self {
Self {
tenant: tenant.into(),
table_id,
}
}
}

mod kvapi_key_impl {
use common_meta_kvapi::kvapi;

Expand Down
3 changes: 3 additions & 0 deletions src/query/catalog/src/catalog/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use common_meta_app::schema::GetTableCopiedFileReply;
use common_meta_app::schema::GetTableCopiedFileReq;
use common_meta_app::schema::IndexMeta;
use common_meta_app::schema::ListDroppedTableReq;
use common_meta_app::schema::ListIndexesByIdReq;
use common_meta_app::schema::ListIndexesReq;
use common_meta_app::schema::ListVirtualColumnsReq;
use common_meta_app::schema::RenameDatabaseReply;
Expand Down Expand Up @@ -111,6 +112,8 @@ pub trait Catalog: DynClone + Send + Sync {

async fn list_indexes(&self, req: ListIndexesReq) -> Result<Vec<(u64, String, IndexMeta)>>;

async fn list_indexes_by_table_id(&self, req: ListIndexesByIdReq) -> Result<Vec<u64>>;

async fn create_virtual_column(
&self,
req: CreateVirtualColumnReq,
Expand Down
6 changes: 6 additions & 0 deletions src/query/service/src/catalogs/default/database_catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use common_meta_app::schema::GetTableCopiedFileReply;
use common_meta_app::schema::GetTableCopiedFileReq;
use common_meta_app::schema::IndexMeta;
use common_meta_app::schema::ListDroppedTableReq;
use common_meta_app::schema::ListIndexesByIdReq;
use common_meta_app::schema::ListIndexesReq;
use common_meta_app::schema::ListVirtualColumnsReq;
use common_meta_app::schema::RenameDatabaseReply;
Expand Down Expand Up @@ -512,6 +513,11 @@ impl Catalog for DatabaseCatalog {
self.mutable_catalog.list_indexes(req).await
}

#[async_backtrace::framed]
async fn list_indexes_by_table_id(&self, req: ListIndexesByIdReq) -> Result<Vec<u64>> {
self.mutable_catalog.list_indexes_by_table_id(req).await
}

// Virtual column

#[async_backtrace::framed]
Expand Down
6 changes: 6 additions & 0 deletions src/query/service/src/catalogs/default/immutable_catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use common_meta_app::schema::GetIndexReq;
use common_meta_app::schema::GetTableCopiedFileReply;
use common_meta_app::schema::GetTableCopiedFileReq;
use common_meta_app::schema::IndexMeta;
use common_meta_app::schema::ListIndexesByIdReq;
use common_meta_app::schema::ListIndexesReq;
use common_meta_app::schema::ListVirtualColumnsReq;
use common_meta_app::schema::RenameDatabaseReply;
Expand Down Expand Up @@ -345,6 +346,11 @@ impl Catalog for ImmutableCatalog {
unimplemented!()
}

#[async_backtrace::framed]
async fn list_indexes_by_table_id(&self, _req: ListIndexesByIdReq) -> Result<Vec<u64>> {
unimplemented!()
}

// Virtual column

#[async_backtrace::framed]
Expand Down
6 changes: 6 additions & 0 deletions src/query/service/src/catalogs/default/mutable_catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ use common_meta_app::schema::GetTableCopiedFileReq;
use common_meta_app::schema::IndexMeta;
use common_meta_app::schema::ListDatabaseReq;
use common_meta_app::schema::ListDroppedTableReq;
use common_meta_app::schema::ListIndexesByIdReq;
use common_meta_app::schema::ListIndexesReq;
use common_meta_app::schema::ListTableLockRevReq;
use common_meta_app::schema::ListVirtualColumnsReq;
Expand Down Expand Up @@ -258,6 +259,11 @@ impl Catalog for MutableCatalog {
Ok(self.ctx.meta.list_indexes(req).await?)
}

#[async_backtrace::framed]
async fn list_indexes_by_table_id(&self, req: ListIndexesByIdReq) -> Result<Vec<u64>> {
Ok(self.ctx.meta.list_indexes_by_table_id(req).await?)
}

// Virtual column

#[async_backtrace::framed]
Expand Down
6 changes: 6 additions & 0 deletions src/query/service/tests/it/storages/fuse/operations/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ use common_meta_app::schema::GetIndexReq;
use common_meta_app::schema::GetTableCopiedFileReply;
use common_meta_app::schema::GetTableCopiedFileReq;
use common_meta_app::schema::IndexMeta;
use common_meta_app::schema::ListIndexesByIdReq;
use common_meta_app::schema::ListIndexesReq;
use common_meta_app::schema::ListVirtualColumnsReq;
use common_meta_app::schema::RenameDatabaseReply;
Expand Down Expand Up @@ -706,6 +707,11 @@ impl Catalog for FakedCatalog {
unimplemented!()
}

#[async_backtrace::framed]
async fn list_indexes_by_table_id(&self, _req: ListIndexesByIdReq) -> Result<Vec<u64>> {
unimplemented!()
}

#[async_backtrace::framed]
async fn create_virtual_column(
&self,
Expand Down
Loading

0 comments on commit 8bd1221

Please sign in to comment.