-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: schema cache invalidation (#5067)
* fix: use SchemaCache to locate database metadata * main: Refactor SchemaMetadataManager to use TableInfoCacheRef - Replace TableInfoManagerRef with TableInfoCacheRef in SchemaMetadataManager - Update DatanodeBuilder to pass TableInfoCacheRef to SchemaMetadataManager - Rename error MissingCacheRegistrySnafu to MissingCacheSnafu in datanode module - Adjust tests to use new mock_schema_metadata_manager with TableInfoCacheRef * fix/schema-cache-invalidation: Add cache module and integrate cache registry into datanode • Implement build_datanode_cache_registry function to create cache registry for datanode • Integrate cache registry into datanode by modifying DatanodeBuilder and HeartbeatTask • Refactor InvalidateTableCacheHandler to InvalidateCacheHandler and move to common-meta crate • Update Cargo.toml to include cache as a dev-dependency for datanode • Adjust related modules (flownode, frontend, tests-integration, standalone) to use new cache handler and registry • Remove obsolete handler module from frontend crate * fix: fuzz imports * chore: add some doc for cahce builder functions * refactor: change table info cache to table schema cache * fix: remove unused variants * fix fuzz * chore: apply suggestion Co-authored-by: Weny Xu <wenymedia@gmail.com> * chore: apply suggestion Co-authored-by: Weny Xu <wenymedia@gmail.com> * fix: compile --------- Co-authored-by: dennis zhuang <killme2008@gmail.com> Co-authored-by: Weny Xu <wenymedia@gmail.com>
- Loading branch information
1 parent
51c6eaf
commit a518538
Showing
27 changed files
with
377 additions
and
290 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// 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 std::sync::Arc; | ||
|
||
use futures_util::future::BoxFuture; | ||
use moka::future::Cache; | ||
use snafu::OptionExt; | ||
|
||
use crate::cache::{CacheContainer, Initializer}; | ||
use crate::error::ValueNotExistSnafu; | ||
use crate::instruction::CacheIdent; | ||
use crate::key::schema_name::{SchemaManager, SchemaName, SchemaNameKey, SchemaNameValue}; | ||
use crate::kv_backend::KvBackendRef; | ||
|
||
pub type SchemaCache = CacheContainer<SchemaName, Arc<SchemaNameValue>, CacheIdent>; | ||
pub type SchemaCacheRef = Arc<SchemaCache>; | ||
|
||
/// Constructs a [SchemaCache]. | ||
pub fn new_schema_cache( | ||
name: String, | ||
cache: Cache<SchemaName, Arc<SchemaNameValue>>, | ||
kv_backend: KvBackendRef, | ||
) -> SchemaCache { | ||
let schema_manager = SchemaManager::new(kv_backend.clone()); | ||
let init = init_factory(schema_manager); | ||
|
||
CacheContainer::new(name, cache, Box::new(invalidator), init, Box::new(filter)) | ||
} | ||
|
||
fn init_factory(schema_manager: SchemaManager) -> Initializer<SchemaName, Arc<SchemaNameValue>> { | ||
Arc::new(move |schema_name| { | ||
let manager = schema_manager.clone(); | ||
Box::pin(async move { | ||
let schema_value = manager | ||
.get(SchemaNameKey { | ||
catalog: &schema_name.catalog_name, | ||
schema: &schema_name.schema_name, | ||
}) | ||
.await? | ||
.context(ValueNotExistSnafu)? | ||
.into_inner(); | ||
Ok(Some(Arc::new(schema_value))) | ||
}) | ||
}) | ||
} | ||
|
||
fn invalidator<'a>( | ||
cache: &'a Cache<SchemaName, Arc<SchemaNameValue>>, | ||
ident: &'a CacheIdent, | ||
) -> BoxFuture<'a, crate::error::Result<()>> { | ||
Box::pin(async move { | ||
if let CacheIdent::SchemaName(schema_name) = ident { | ||
cache.invalidate(schema_name).await | ||
} | ||
Ok(()) | ||
}) | ||
} | ||
|
||
fn filter(ident: &CacheIdent) -> bool { | ||
matches!(ident, CacheIdent::SchemaName(_)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// 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. | ||
|
||
//! Cache for table id to schema name mapping. | ||
|
||
use std::sync::Arc; | ||
|
||
use futures_util::future::BoxFuture; | ||
use moka::future::Cache; | ||
use snafu::OptionExt; | ||
use store_api::storage::TableId; | ||
|
||
use crate::cache::{CacheContainer, Initializer}; | ||
use crate::error; | ||
use crate::instruction::CacheIdent; | ||
use crate::key::schema_name::SchemaName; | ||
use crate::key::table_info::TableInfoManager; | ||
use crate::kv_backend::KvBackendRef; | ||
|
||
pub type TableSchemaCache = CacheContainer<TableId, Arc<SchemaName>, CacheIdent>; | ||
pub type TableSchemaCacheRef = Arc<TableSchemaCache>; | ||
|
||
/// Constructs a [TableSchemaCache]. | ||
pub fn new_table_schema_cache( | ||
name: String, | ||
cache: Cache<TableId, Arc<SchemaName>>, | ||
kv_backend: KvBackendRef, | ||
) -> TableSchemaCache { | ||
let table_info_manager = TableInfoManager::new(kv_backend); | ||
let init = init_factory(table_info_manager); | ||
|
||
CacheContainer::new(name, cache, Box::new(invalidator), init, Box::new(filter)) | ||
} | ||
|
||
fn init_factory(table_info_manager: TableInfoManager) -> Initializer<TableId, Arc<SchemaName>> { | ||
Arc::new(move |table_id| { | ||
let table_info_manager = table_info_manager.clone(); | ||
Box::pin(async move { | ||
let raw_table_info = table_info_manager | ||
.get(*table_id) | ||
.await? | ||
.context(error::ValueNotExistSnafu)? | ||
.into_inner() | ||
.table_info; | ||
|
||
Ok(Some(Arc::new(SchemaName { | ||
catalog_name: raw_table_info.catalog_name, | ||
schema_name: raw_table_info.schema_name, | ||
}))) | ||
}) | ||
}) | ||
} | ||
|
||
/// Never invalidates table id schema cache. | ||
fn invalidator<'a>( | ||
_cache: &'a Cache<TableId, Arc<SchemaName>>, | ||
_ident: &'a CacheIdent, | ||
) -> BoxFuture<'a, error::Result<()>> { | ||
Box::pin(std::future::ready(Ok(()))) | ||
} | ||
|
||
/// Never invalidates table id schema cache. | ||
fn filter(_ident: &CacheIdent) -> bool { | ||
false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.