forked from apache/horaedb
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add table status check (apache#1418)
## Rationale Refer to this issue apache#1386, currently, if the status of the shard is abnormal, we cannot get any valid exception information from the error message `table not found`. ## Detailed Changes * Add `TableStatus` in `cluster`, you can use it to get the status of the table in the current cluster.. * Add `SchemaWithCluster`, It wraps the schema inside the cluster, through which the state of the cluster and schema can be combined. ## Test Plan Pass CI.
- Loading branch information
1 parent
5377dfd
commit dc10253
Showing
10 changed files
with
202 additions
and
9 deletions.
There are no files selected for viewing
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,115 @@ | ||
// Copyright 2023 The HoraeDB Authors | ||
// | ||
// 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 async_trait::async_trait; | ||
use catalog::{ | ||
schema, | ||
schema::{ | ||
CreateOptions, CreateTableRequest, DropOptions, DropTableRequest, NameRef, Schema, | ||
SchemaRef, TableNotReady, | ||
}, | ||
}; | ||
use cluster::{ClusterRef, TableStatus}; | ||
use generic_error::BoxError; | ||
use snafu::{ResultExt, Snafu}; | ||
use table_engine::table::{SchemaId, TableRef}; | ||
|
||
#[derive(Debug, Snafu)] | ||
#[snafu(visibility(pub))] | ||
pub enum Error { | ||
#[snafu(display("Invalid table status, status:{:?}", status))] | ||
InvalidTableStatus { status: TableStatus }, | ||
} | ||
|
||
/// A cluster-based implementation for [`schema`]. | ||
|
||
/// Schema with cluster. | ||
/// It binds cluster and schema and will detect the health status of the cluster | ||
/// when calling the schema interface. | ||
pub(crate) struct SchemaWithCluster { | ||
internal: SchemaRef, | ||
|
||
cluster: ClusterRef, | ||
} | ||
|
||
impl SchemaWithCluster { | ||
pub(crate) fn new(internal: SchemaRef, cluster: ClusterRef) -> SchemaWithCluster { | ||
SchemaWithCluster { internal, cluster } | ||
} | ||
|
||
// Get table status, return None when table not found in shard. | ||
fn table_status(&self, table_name: NameRef) -> Option<TableStatus> { | ||
self.cluster.get_table_status(self.name(), table_name) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Schema for SchemaWithCluster { | ||
fn name(&self) -> NameRef { | ||
self.internal.name() | ||
} | ||
|
||
fn id(&self) -> SchemaId { | ||
self.internal.id() | ||
} | ||
|
||
fn table_by_name(&self, name: NameRef) -> schema::Result<Option<TableRef>> { | ||
let find_table_result = self.internal.table_by_name(name)?; | ||
|
||
if find_table_result.is_none() { | ||
return match self.table_status(name) { | ||
// Table not found in schema and shard not contains this table. | ||
None => Ok(None), | ||
// Table not found in schema but shard contains this table. | ||
// Check the status of the shard. | ||
Some(table_status) => InvalidTableStatus { | ||
status: table_status, | ||
} | ||
.fail() | ||
.box_err() | ||
.with_context(|| TableNotReady {})?, | ||
}; | ||
} | ||
|
||
Ok(find_table_result) | ||
} | ||
|
||
async fn create_table( | ||
&self, | ||
request: CreateTableRequest, | ||
opts: CreateOptions, | ||
) -> schema::Result<TableRef> { | ||
self.internal.create_table(request, opts).await | ||
} | ||
|
||
async fn drop_table( | ||
&self, | ||
request: DropTableRequest, | ||
opts: DropOptions, | ||
) -> schema::Result<bool> { | ||
self.internal.drop_table(request, opts).await | ||
} | ||
|
||
fn all_tables(&self) -> schema::Result<Vec<TableRef>> { | ||
self.internal.all_tables() | ||
} | ||
|
||
fn register_table(&self, table: TableRef) { | ||
self.internal.register_table(table) | ||
} | ||
|
||
fn unregister_table(&self, table_name: &str) { | ||
self.internal.unregister_table(table_name) | ||
} | ||
} |
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