-
Notifications
You must be signed in to change notification settings - Fork 312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add region_statistics
table
#4771
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4e624df
refactor: introduce `region_statistic`
WenyXu c09a97f
refactor: move DatanodeStat related structs to common_meta
WenyXu e0d89b2
chore: add comments
WenyXu 832c25c
feat: implement `list_region_stats` for `ClusterInfo` trait
WenyXu 2f5d561
feat: add `region_statistics` table
WenyXu f7f377b
feat: add table_id and region_number fields
WenyXu f555bce
chore: rename unused snafu
WenyXu f7284aa
chore: udpate sqlness results
WenyXu e0f76f2
chore: avoid to print source in error msg
WenyXu f725d79
chore: move `procedure_info` under `greptime` catalog
WenyXu bffc2ed
chore: apply suggestions from CR
WenyXu fc4b0d7
Update src/common/meta/src/datanode.rs
WenyXu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
257 changes: 257 additions & 0 deletions
257
src/catalog/src/system_schema/information_schema/region_statistics.rs
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,257 @@ | ||
// 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, Weak}; | ||
|
||
use arrow_schema::SchemaRef as ArrowSchemaRef; | ||
use common_catalog::consts::INFORMATION_SCHEMA_REGION_STATISTICS_TABLE_ID; | ||
use common_config::Mode; | ||
use common_error::ext::BoxedError; | ||
use common_meta::cluster::ClusterInfo; | ||
use common_meta::datanode::RegionStat; | ||
use common_recordbatch::adapter::RecordBatchStreamAdapter; | ||
use common_recordbatch::{DfSendableRecordBatchStream, RecordBatch, SendableRecordBatchStream}; | ||
use common_telemetry::tracing::warn; | ||
use datafusion::execution::TaskContext; | ||
use datafusion::physical_plan::stream::RecordBatchStreamAdapter as DfRecordBatchStreamAdapter; | ||
use datafusion::physical_plan::streaming::PartitionStream as DfPartitionStream; | ||
use datatypes::prelude::{ConcreteDataType, ScalarVectorBuilder, VectorRef}; | ||
use datatypes::schema::{ColumnSchema, Schema, SchemaRef}; | ||
use datatypes::value::Value; | ||
use datatypes::vectors::{StringVectorBuilder, UInt32VectorBuilder, UInt64VectorBuilder}; | ||
use snafu::ResultExt; | ||
use store_api::storage::{ScanRequest, TableId}; | ||
|
||
use super::{InformationTable, REGION_STATISTICS}; | ||
use crate::error::{CreateRecordBatchSnafu, InternalSnafu, ListRegionStatsSnafu, Result}; | ||
use crate::information_schema::Predicates; | ||
use crate::system_schema::utils; | ||
use crate::CatalogManager; | ||
|
||
const REGION_ID: &str = "region_id"; | ||
const TABLE_ID: &str = "table_id"; | ||
const REGION_NUMBER: &str = "region_number"; | ||
const MEMTABLE_SIZE: &str = "memtable_size"; | ||
const MANIFEST_SIZE: &str = "manifest_size"; | ||
const SST_SIZE: &str = "sst_size"; | ||
const ENGINE: &str = "engine"; | ||
const REGION_ROLE: &str = "region_role"; | ||
|
||
const INIT_CAPACITY: usize = 42; | ||
|
||
/// The `REGION_STATISTICS` table provides information about the region statistics. Including fields: | ||
/// | ||
/// - `region_id`: The region id. | ||
/// - `table_id`: The table id. | ||
/// - `region_number`: The region number. | ||
/// - `memtable_size`: The memtable size in bytes. | ||
/// - `manifest_size`: The manifest size in bytes. | ||
/// - `sst_size`: The sst size in bytes. | ||
/// - `engine`: The engine type. | ||
/// - `region_role`: The region role. | ||
/// | ||
pub(super) struct InformationSchemaRegionStatistics { | ||
schema: SchemaRef, | ||
catalog_manager: Weak<dyn CatalogManager>, | ||
} | ||
|
||
impl InformationSchemaRegionStatistics { | ||
pub(super) fn new(catalog_manager: Weak<dyn CatalogManager>) -> Self { | ||
Self { | ||
schema: Self::schema(), | ||
catalog_manager, | ||
} | ||
} | ||
|
||
pub(crate) fn schema() -> SchemaRef { | ||
Arc::new(Schema::new(vec![ | ||
ColumnSchema::new(REGION_ID, ConcreteDataType::uint64_datatype(), false), | ||
ColumnSchema::new(TABLE_ID, ConcreteDataType::uint32_datatype(), false), | ||
ColumnSchema::new(REGION_NUMBER, ConcreteDataType::uint32_datatype(), false), | ||
ColumnSchema::new(MEMTABLE_SIZE, ConcreteDataType::uint64_datatype(), true), | ||
ColumnSchema::new(MANIFEST_SIZE, ConcreteDataType::uint64_datatype(), true), | ||
ColumnSchema::new(SST_SIZE, ConcreteDataType::uint64_datatype(), true), | ||
ColumnSchema::new(ENGINE, ConcreteDataType::string_datatype(), true), | ||
ColumnSchema::new(REGION_ROLE, ConcreteDataType::string_datatype(), true), | ||
])) | ||
} | ||
|
||
fn builder(&self) -> InformationSchemaRegionStatisticsBuilder { | ||
InformationSchemaRegionStatisticsBuilder::new( | ||
self.schema.clone(), | ||
self.catalog_manager.clone(), | ||
) | ||
} | ||
} | ||
|
||
impl InformationTable for InformationSchemaRegionStatistics { | ||
fn table_id(&self) -> TableId { | ||
INFORMATION_SCHEMA_REGION_STATISTICS_TABLE_ID | ||
} | ||
|
||
fn table_name(&self) -> &'static str { | ||
REGION_STATISTICS | ||
} | ||
|
||
fn schema(&self) -> SchemaRef { | ||
self.schema.clone() | ||
} | ||
|
||
fn to_stream(&self, request: ScanRequest) -> Result<SendableRecordBatchStream> { | ||
let schema = self.schema.arrow_schema().clone(); | ||
let mut builder = self.builder(); | ||
|
||
let stream = Box::pin(DfRecordBatchStreamAdapter::new( | ||
schema, | ||
futures::stream::once(async move { | ||
builder | ||
.make_region_statistics(Some(request)) | ||
.await | ||
.map(|x| x.into_df_record_batch()) | ||
.map_err(Into::into) | ||
}), | ||
)); | ||
|
||
Ok(Box::pin( | ||
RecordBatchStreamAdapter::try_new(stream) | ||
.map_err(BoxedError::new) | ||
.context(InternalSnafu)?, | ||
)) | ||
} | ||
} | ||
|
||
struct InformationSchemaRegionStatisticsBuilder { | ||
schema: SchemaRef, | ||
catalog_manager: Weak<dyn CatalogManager>, | ||
|
||
region_ids: UInt64VectorBuilder, | ||
table_ids: UInt32VectorBuilder, | ||
region_numbers: UInt32VectorBuilder, | ||
memtable_sizes: UInt64VectorBuilder, | ||
manifest_sizes: UInt64VectorBuilder, | ||
sst_sizes: UInt64VectorBuilder, | ||
engines: StringVectorBuilder, | ||
region_roles: StringVectorBuilder, | ||
} | ||
|
||
impl InformationSchemaRegionStatisticsBuilder { | ||
fn new(schema: SchemaRef, catalog_manager: Weak<dyn CatalogManager>) -> Self { | ||
Self { | ||
schema, | ||
catalog_manager, | ||
region_ids: UInt64VectorBuilder::with_capacity(INIT_CAPACITY), | ||
table_ids: UInt32VectorBuilder::with_capacity(INIT_CAPACITY), | ||
region_numbers: UInt32VectorBuilder::with_capacity(INIT_CAPACITY), | ||
memtable_sizes: UInt64VectorBuilder::with_capacity(INIT_CAPACITY), | ||
manifest_sizes: UInt64VectorBuilder::with_capacity(INIT_CAPACITY), | ||
sst_sizes: UInt64VectorBuilder::with_capacity(INIT_CAPACITY), | ||
engines: StringVectorBuilder::with_capacity(INIT_CAPACITY), | ||
region_roles: StringVectorBuilder::with_capacity(INIT_CAPACITY), | ||
} | ||
} | ||
|
||
/// Construct a new `InformationSchemaRegionStatistics` from the collected data. | ||
async fn make_region_statistics( | ||
&mut self, | ||
request: Option<ScanRequest>, | ||
) -> Result<RecordBatch> { | ||
let predicates = Predicates::from_scan_request(&request); | ||
let mode = utils::running_mode(&self.catalog_manager)?.unwrap_or(Mode::Standalone); | ||
|
||
match mode { | ||
Mode::Standalone => { | ||
// TODO(weny): implement it | ||
} | ||
Mode::Distributed => { | ||
if let Some(meta_client) = utils::meta_client(&self.catalog_manager)? { | ||
let region_stats = meta_client | ||
.list_region_stats() | ||
.await | ||
.map_err(BoxedError::new) | ||
.context(ListRegionStatsSnafu)?; | ||
for region_stat in region_stats { | ||
self.add_region_statistic(&predicates, region_stat); | ||
} | ||
} else { | ||
warn!("Meta client is not available"); | ||
} | ||
} | ||
} | ||
|
||
self.finish() | ||
} | ||
|
||
fn add_region_statistic(&mut self, predicate: &Predicates, region_stat: RegionStat) { | ||
let row = [ | ||
(REGION_ID, &Value::from(region_stat.id.as_u64())), | ||
(TABLE_ID, &Value::from(region_stat.id.table_id())), | ||
(REGION_NUMBER, &Value::from(region_stat.id.region_number())), | ||
(MEMTABLE_SIZE, &Value::from(region_stat.memtable_size)), | ||
(MANIFEST_SIZE, &Value::from(region_stat.manifest_size)), | ||
(SST_SIZE, &Value::from(region_stat.sst_size)), | ||
(ENGINE, &Value::from(region_stat.engine.as_str())), | ||
(REGION_ROLE, &Value::from(region_stat.role.to_string())), | ||
]; | ||
|
||
if !predicate.eval(&row) { | ||
return; | ||
} | ||
|
||
self.region_ids.push(Some(region_stat.id.as_u64())); | ||
self.table_ids.push(Some(region_stat.id.table_id())); | ||
self.region_numbers | ||
.push(Some(region_stat.id.region_number())); | ||
self.memtable_sizes.push(Some(region_stat.memtable_size)); | ||
self.manifest_sizes.push(Some(region_stat.manifest_size)); | ||
self.sst_sizes.push(Some(region_stat.sst_size)); | ||
self.engines.push(Some(®ion_stat.engine)); | ||
self.region_roles.push(Some(®ion_stat.role.to_string())); | ||
} | ||
|
||
fn finish(&mut self) -> Result<RecordBatch> { | ||
let columns: Vec<VectorRef> = vec![ | ||
Arc::new(self.region_ids.finish()), | ||
Arc::new(self.table_ids.finish()), | ||
Arc::new(self.region_numbers.finish()), | ||
Arc::new(self.memtable_sizes.finish()), | ||
Arc::new(self.manifest_sizes.finish()), | ||
Arc::new(self.sst_sizes.finish()), | ||
Arc::new(self.engines.finish()), | ||
Arc::new(self.region_roles.finish()), | ||
]; | ||
|
||
RecordBatch::new(self.schema.clone(), columns).context(CreateRecordBatchSnafu) | ||
} | ||
} | ||
|
||
impl DfPartitionStream for InformationSchemaRegionStatistics { | ||
fn schema(&self) -> &ArrowSchemaRef { | ||
self.schema.arrow_schema() | ||
} | ||
|
||
fn execute(&self, _: Arc<TaskContext>) -> DfSendableRecordBatchStream { | ||
let schema = self.schema.arrow_schema().clone(); | ||
let mut builder = self.builder(); | ||
Box::pin(DfRecordBatchStreamAdapter::new( | ||
schema, | ||
futures::stream::once(async move { | ||
builder | ||
.make_region_statistics(None) | ||
.await | ||
.map(|x| x.into_df_record_batch()) | ||
.map_err(Into::into) | ||
}), | ||
)) | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not implement it? It's very useful for both standalone and distributed clusters to get the statistics info of regions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have implemented it in #4811