|
| 1 | +// Copyright 2023 Greptime Team |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use std::sync::{Arc, Weak}; |
| 16 | + |
| 17 | +use arrow_schema::SchemaRef as ArrowSchemaRef; |
| 18 | +use common_catalog::consts::INFORMATION_SCHEMA_REGION_STATISTICS_TABLE_ID; |
| 19 | +use common_config::Mode; |
| 20 | +use common_error::ext::BoxedError; |
| 21 | +use common_meta::cluster::ClusterInfo; |
| 22 | +use common_meta::datanode::RegionStat; |
| 23 | +use common_recordbatch::adapter::RecordBatchStreamAdapter; |
| 24 | +use common_recordbatch::{DfSendableRecordBatchStream, RecordBatch, SendableRecordBatchStream}; |
| 25 | +use common_telemetry::tracing::warn; |
| 26 | +use datafusion::execution::TaskContext; |
| 27 | +use datafusion::physical_plan::stream::RecordBatchStreamAdapter as DfRecordBatchStreamAdapter; |
| 28 | +use datafusion::physical_plan::streaming::PartitionStream as DfPartitionStream; |
| 29 | +use datatypes::prelude::{ConcreteDataType, ScalarVectorBuilder, VectorRef}; |
| 30 | +use datatypes::schema::{ColumnSchema, Schema, SchemaRef}; |
| 31 | +use datatypes::value::Value; |
| 32 | +use datatypes::vectors::{StringVectorBuilder, UInt32VectorBuilder, UInt64VectorBuilder}; |
| 33 | +use snafu::ResultExt; |
| 34 | +use store_api::storage::{ScanRequest, TableId}; |
| 35 | + |
| 36 | +use super::{InformationTable, REGION_STATISTICS}; |
| 37 | +use crate::error::{CreateRecordBatchSnafu, InternalSnafu, ListRegionStatsSnafu, Result}; |
| 38 | +use crate::information_schema::Predicates; |
| 39 | +use crate::system_schema::utils; |
| 40 | +use crate::CatalogManager; |
| 41 | + |
| 42 | +const REGION_ID: &str = "region_id"; |
| 43 | +const TABLE_ID: &str = "table_id"; |
| 44 | +const REGION_NUMBER: &str = "region_number"; |
| 45 | +const MEMTABLE_SIZE: &str = "memtable_size"; |
| 46 | +const MANIFEST_SIZE: &str = "manifest_size"; |
| 47 | +const SST_SIZE: &str = "sst_size"; |
| 48 | +const ENGINE: &str = "engine"; |
| 49 | +const REGION_ROLE: &str = "region_role"; |
| 50 | + |
| 51 | +const INIT_CAPACITY: usize = 42; |
| 52 | + |
| 53 | +/// The `REGION_STATISTICS` table provides information about the region statistics. Including fields: |
| 54 | +/// |
| 55 | +/// - `region_id`: The region id. |
| 56 | +/// - `table_id`: The table id. |
| 57 | +/// - `region_number`: The region number. |
| 58 | +/// - `memtable_size`: The memtable size in bytes. |
| 59 | +/// - `manifest_size`: The manifest size in bytes. |
| 60 | +/// - `sst_size`: The sst size in bytes. |
| 61 | +/// - `engine`: The engine type. |
| 62 | +/// - `region_role`: The region role. |
| 63 | +/// |
| 64 | +pub(super) struct InformationSchemaRegionStatistics { |
| 65 | + schema: SchemaRef, |
| 66 | + catalog_manager: Weak<dyn CatalogManager>, |
| 67 | +} |
| 68 | + |
| 69 | +impl InformationSchemaRegionStatistics { |
| 70 | + pub(super) fn new(catalog_manager: Weak<dyn CatalogManager>) -> Self { |
| 71 | + Self { |
| 72 | + schema: Self::schema(), |
| 73 | + catalog_manager, |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + pub(crate) fn schema() -> SchemaRef { |
| 78 | + Arc::new(Schema::new(vec![ |
| 79 | + ColumnSchema::new(REGION_ID, ConcreteDataType::uint64_datatype(), false), |
| 80 | + ColumnSchema::new(TABLE_ID, ConcreteDataType::uint32_datatype(), false), |
| 81 | + ColumnSchema::new(REGION_NUMBER, ConcreteDataType::uint32_datatype(), false), |
| 82 | + ColumnSchema::new(MEMTABLE_SIZE, ConcreteDataType::uint64_datatype(), true), |
| 83 | + ColumnSchema::new(MANIFEST_SIZE, ConcreteDataType::uint64_datatype(), true), |
| 84 | + ColumnSchema::new(SST_SIZE, ConcreteDataType::uint64_datatype(), true), |
| 85 | + ColumnSchema::new(ENGINE, ConcreteDataType::string_datatype(), true), |
| 86 | + ColumnSchema::new(REGION_ROLE, ConcreteDataType::string_datatype(), true), |
| 87 | + ])) |
| 88 | + } |
| 89 | + |
| 90 | + fn builder(&self) -> InformationSchemaRegionStatisticsBuilder { |
| 91 | + InformationSchemaRegionStatisticsBuilder::new( |
| 92 | + self.schema.clone(), |
| 93 | + self.catalog_manager.clone(), |
| 94 | + ) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +impl InformationTable for InformationSchemaRegionStatistics { |
| 99 | + fn table_id(&self) -> TableId { |
| 100 | + INFORMATION_SCHEMA_REGION_STATISTICS_TABLE_ID |
| 101 | + } |
| 102 | + |
| 103 | + fn table_name(&self) -> &'static str { |
| 104 | + REGION_STATISTICS |
| 105 | + } |
| 106 | + |
| 107 | + fn schema(&self) -> SchemaRef { |
| 108 | + self.schema.clone() |
| 109 | + } |
| 110 | + |
| 111 | + fn to_stream(&self, request: ScanRequest) -> Result<SendableRecordBatchStream> { |
| 112 | + let schema = self.schema.arrow_schema().clone(); |
| 113 | + let mut builder = self.builder(); |
| 114 | + |
| 115 | + let stream = Box::pin(DfRecordBatchStreamAdapter::new( |
| 116 | + schema, |
| 117 | + futures::stream::once(async move { |
| 118 | + builder |
| 119 | + .make_region_statistics(Some(request)) |
| 120 | + .await |
| 121 | + .map(|x| x.into_df_record_batch()) |
| 122 | + .map_err(Into::into) |
| 123 | + }), |
| 124 | + )); |
| 125 | + |
| 126 | + Ok(Box::pin( |
| 127 | + RecordBatchStreamAdapter::try_new(stream) |
| 128 | + .map_err(BoxedError::new) |
| 129 | + .context(InternalSnafu)?, |
| 130 | + )) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +struct InformationSchemaRegionStatisticsBuilder { |
| 135 | + schema: SchemaRef, |
| 136 | + catalog_manager: Weak<dyn CatalogManager>, |
| 137 | + |
| 138 | + region_ids: UInt64VectorBuilder, |
| 139 | + table_ids: UInt32VectorBuilder, |
| 140 | + region_numbers: UInt32VectorBuilder, |
| 141 | + memtable_sizes: UInt64VectorBuilder, |
| 142 | + manifest_sizes: UInt64VectorBuilder, |
| 143 | + sst_sizes: UInt64VectorBuilder, |
| 144 | + engines: StringVectorBuilder, |
| 145 | + region_roles: StringVectorBuilder, |
| 146 | +} |
| 147 | + |
| 148 | +impl InformationSchemaRegionStatisticsBuilder { |
| 149 | + fn new(schema: SchemaRef, catalog_manager: Weak<dyn CatalogManager>) -> Self { |
| 150 | + Self { |
| 151 | + schema, |
| 152 | + catalog_manager, |
| 153 | + region_ids: UInt64VectorBuilder::with_capacity(INIT_CAPACITY), |
| 154 | + table_ids: UInt32VectorBuilder::with_capacity(INIT_CAPACITY), |
| 155 | + region_numbers: UInt32VectorBuilder::with_capacity(INIT_CAPACITY), |
| 156 | + memtable_sizes: UInt64VectorBuilder::with_capacity(INIT_CAPACITY), |
| 157 | + manifest_sizes: UInt64VectorBuilder::with_capacity(INIT_CAPACITY), |
| 158 | + sst_sizes: UInt64VectorBuilder::with_capacity(INIT_CAPACITY), |
| 159 | + engines: StringVectorBuilder::with_capacity(INIT_CAPACITY), |
| 160 | + region_roles: StringVectorBuilder::with_capacity(INIT_CAPACITY), |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + /// Construct a new `InformationSchemaRegionStatistics` from the collected data. |
| 165 | + async fn make_region_statistics( |
| 166 | + &mut self, |
| 167 | + request: Option<ScanRequest>, |
| 168 | + ) -> Result<RecordBatch> { |
| 169 | + let predicates = Predicates::from_scan_request(&request); |
| 170 | + let mode = utils::running_mode(&self.catalog_manager)?.unwrap_or(Mode::Standalone); |
| 171 | + |
| 172 | + match mode { |
| 173 | + Mode::Standalone => { |
| 174 | + // TODO(weny): implement it |
| 175 | + } |
| 176 | + Mode::Distributed => { |
| 177 | + if let Some(meta_client) = utils::meta_client(&self.catalog_manager)? { |
| 178 | + let region_stats = meta_client |
| 179 | + .list_region_stats() |
| 180 | + .await |
| 181 | + .map_err(BoxedError::new) |
| 182 | + .context(ListRegionStatsSnafu)?; |
| 183 | + for region_stat in region_stats { |
| 184 | + self.add_region_statistic(&predicates, region_stat); |
| 185 | + } |
| 186 | + } else { |
| 187 | + warn!("Meta client is not available"); |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + self.finish() |
| 193 | + } |
| 194 | + |
| 195 | + fn add_region_statistic(&mut self, predicate: &Predicates, region_stat: RegionStat) { |
| 196 | + let row = [ |
| 197 | + (REGION_ID, &Value::from(region_stat.id.as_u64())), |
| 198 | + (TABLE_ID, &Value::from(region_stat.id.table_id())), |
| 199 | + (REGION_NUMBER, &Value::from(region_stat.id.region_number())), |
| 200 | + (MEMTABLE_SIZE, &Value::from(region_stat.memtable_size)), |
| 201 | + (MANIFEST_SIZE, &Value::from(region_stat.manifest_size)), |
| 202 | + (SST_SIZE, &Value::from(region_stat.sst_size)), |
| 203 | + (ENGINE, &Value::from(region_stat.engine.as_str())), |
| 204 | + (REGION_ROLE, &Value::from(region_stat.role.to_string())), |
| 205 | + ]; |
| 206 | + |
| 207 | + if !predicate.eval(&row) { |
| 208 | + return; |
| 209 | + } |
| 210 | + |
| 211 | + self.region_ids.push(Some(region_stat.id.as_u64())); |
| 212 | + self.table_ids.push(Some(region_stat.id.table_id())); |
| 213 | + self.region_numbers |
| 214 | + .push(Some(region_stat.id.region_number())); |
| 215 | + self.memtable_sizes.push(Some(region_stat.memtable_size)); |
| 216 | + self.manifest_sizes.push(Some(region_stat.manifest_size)); |
| 217 | + self.sst_sizes.push(Some(region_stat.sst_size)); |
| 218 | + self.engines.push(Some(®ion_stat.engine)); |
| 219 | + self.region_roles.push(Some(®ion_stat.role.to_string())); |
| 220 | + } |
| 221 | + |
| 222 | + fn finish(&mut self) -> Result<RecordBatch> { |
| 223 | + let columns: Vec<VectorRef> = vec![ |
| 224 | + Arc::new(self.region_ids.finish()), |
| 225 | + Arc::new(self.table_ids.finish()), |
| 226 | + Arc::new(self.region_numbers.finish()), |
| 227 | + Arc::new(self.memtable_sizes.finish()), |
| 228 | + Arc::new(self.manifest_sizes.finish()), |
| 229 | + Arc::new(self.sst_sizes.finish()), |
| 230 | + Arc::new(self.engines.finish()), |
| 231 | + Arc::new(self.region_roles.finish()), |
| 232 | + ]; |
| 233 | + |
| 234 | + RecordBatch::new(self.schema.clone(), columns).context(CreateRecordBatchSnafu) |
| 235 | + } |
| 236 | +} |
| 237 | + |
| 238 | +impl DfPartitionStream for InformationSchemaRegionStatistics { |
| 239 | + fn schema(&self) -> &ArrowSchemaRef { |
| 240 | + self.schema.arrow_schema() |
| 241 | + } |
| 242 | + |
| 243 | + fn execute(&self, _: Arc<TaskContext>) -> DfSendableRecordBatchStream { |
| 244 | + let schema = self.schema.arrow_schema().clone(); |
| 245 | + let mut builder = self.builder(); |
| 246 | + Box::pin(DfRecordBatchStreamAdapter::new( |
| 247 | + schema, |
| 248 | + futures::stream::once(async move { |
| 249 | + builder |
| 250 | + .make_region_statistics(None) |
| 251 | + .await |
| 252 | + .map(|x| x.into_df_record_batch()) |
| 253 | + .map_err(Into::into) |
| 254 | + }), |
| 255 | + )) |
| 256 | + } |
| 257 | +} |
0 commit comments