Skip to content

Commit

Permalink
use table_id instead of metric_name
Browse files Browse the repository at this point in the history
Signed-off-by: Ruihang Xia <[email protected]>
  • Loading branch information
waynexia committed Nov 16, 2023
1 parent c9fd09e commit 4344c1d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/metric-engine/src/data_region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ mod test {
let expected = vec![
"greptime_timestamp",
"greptime_value",
"__metric",
"__table_id",
"__tsid",
"job",
"tag2",
Expand Down
43 changes: 21 additions & 22 deletions src/metric-engine/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use store_api::region_request::{
AlterKind, RegionAlterRequest, RegionCreateRequest, RegionPutRequest, RegionRequest,
};
use store_api::storage::consts::ReservedColumnId;
use store_api::storage::{RegionGroup, RegionId, ScanRequest};
use store_api::storage::{RegionGroup, RegionId, ScanRequest, TableId};
use tokio::sync::RwLock;

use crate::data_region::DataRegion;
Expand Down Expand Up @@ -68,7 +68,7 @@ pub const METADATA_SCHEMA_KEY_COLUMN_INDEX: usize = 1;
pub const METADATA_SCHEMA_VALUE_COLUMN_INDEX: usize = 2;

/// Column name of internal column `__metric` that stores the original metric name
pub const DATA_SCHEMA_METRIC_NAME_COLUMN_NAME: &str = "__metric";
pub const DATA_SCHEMA_TABLE_ID_COLUMN_NAME: &str = "__table_id";
pub const DATA_SCHEMA_TSID_COLUMN_NAME: &str = "__tsid";

pub const METADATA_REGION_SUBDIR: &str = "metadata";
Expand Down Expand Up @@ -477,9 +477,9 @@ impl MetricEngineInner {

// check if internal columns are not occupied
ensure!(
!name_to_index.contains_key(DATA_SCHEMA_METRIC_NAME_COLUMN_NAME),
!name_to_index.contains_key(DATA_SCHEMA_TABLE_ID_COLUMN_NAME),
InternalColumnOccupiedSnafu {
column: DATA_SCHEMA_METRIC_NAME_COLUMN_NAME,
column: DATA_SCHEMA_TABLE_ID_COLUMN_NAME,
}
);
ensure!(
Expand Down Expand Up @@ -599,21 +599,21 @@ impl MetricEngineInner {
data_region_request.column_metadatas.push(metric_name_col);
data_region_request.column_metadatas.push(tsid_col);
data_region_request.primary_key =
vec![ReservedColumnId::metric_name(), ReservedColumnId::tsid()];
vec![ReservedColumnId::table_id(), ReservedColumnId::tsid()];

data_region_request
}

/// Generate internal column metadata.
///
/// Return `[metric_name_col, tsid_col]`
/// Return `[table_id_col, tsid_col]`
fn internal_column_metadata() -> [ColumnMetadata; 2] {
let metric_name_col = ColumnMetadata {
column_id: ReservedColumnId::metric_name(),
column_id: ReservedColumnId::table_id(),
semantic_type: SemanticType::Tag,
column_schema: ColumnSchema::new(
DATA_SCHEMA_METRIC_NAME_COLUMN_NAME,
ConcreteDataType::string_datatype(),
DATA_SCHEMA_TABLE_ID_COLUMN_NAME,
ConcreteDataType::uint32_datatype(),
false,
),
};
Expand Down Expand Up @@ -770,7 +770,7 @@ impl MetricEngineInner {

// write to data region
// TODO: retrieve table name
self.modify_rows("test".to_string(), &mut request.rows)?;
self.modify_rows(logical_region_id.table_id(), &mut request.rows)?;
self.data_region.write_data(data_region_id, request).await
}

Expand Down Expand Up @@ -820,9 +820,9 @@ impl MetricEngineInner {

/// Perform metric engine specific logic to incomming rows.

Check warning on line 821 in src/metric-engine/src/engine.rs

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"incomming" should be "incoming".
/// - Change the semantic type of tag columns to field
/// - Add table_name column
/// - Add table_id column
/// - Generate tsid
fn modify_rows(&self, table_name: String, rows: &mut Rows) -> Result<()> {
fn modify_rows(&self, table_id: TableId, rows: &mut Rows) -> Result<()> {
// gather tag column indices
let mut tag_col_indices = rows
.schema
Expand Down Expand Up @@ -851,8 +851,8 @@ impl MetricEngineInner {
.collect::<Vec<_>>();
// add table_name column
rows.schema.push(PbColumnSchema {
column_name: DATA_SCHEMA_METRIC_NAME_COLUMN_NAME.to_string(),
datatype: to_column_data_type(&ConcreteDataType::string_datatype())
column_name: DATA_SCHEMA_TABLE_ID_COLUMN_NAME.to_string(),
datatype: to_column_data_type(&ConcreteDataType::uint32_datatype())
.unwrap()
.into(),
semantic_type: SemanticType::Tag as _,
Expand All @@ -869,7 +869,7 @@ impl MetricEngineInner {
// fill internal columns
let mut random_state = ahash::RandomState::with_seeds(1, 2, 3, 4);
for row in &mut rows.rows {
Self::fill_internal_columns(&mut random_state, &table_name, &tag_col_indices, row);
Self::fill_internal_columns(&mut random_state, table_id, &tag_col_indices, row);
}

Ok(())
Expand All @@ -878,7 +878,7 @@ impl MetricEngineInner {
/// Fills internal columns of a row with table name and a hash of tag values.
fn fill_internal_columns(
random_state: &mut RandomState,
table_name: &str,
table_id: TableId,
tag_col_indices: &[(usize, String)],
row: &mut Row,
) {
Expand All @@ -894,8 +894,7 @@ impl MetricEngineInner {
let hash = hasher.finish();

// fill table name and tsid
row.values
.push(ValueData::StringValue(table_name.to_string()).into());
row.values.push(ValueData::U32Value(table_id).into());
row.values.push(ValueData::U64Value(hash).into());
}
}
Expand Down Expand Up @@ -928,8 +927,8 @@ mod tests {
column_id: 1,
semantic_type: SemanticType::Tag,
column_schema: ColumnSchema::new(
DATA_SCHEMA_METRIC_NAME_COLUMN_NAME,
ConcreteDataType::string_datatype(),
DATA_SCHEMA_TABLE_ID_COLUMN_NAME,
ConcreteDataType::uint32_datatype(),
false,
),
},
Expand All @@ -943,7 +942,7 @@ mod tests {
assert!(result.is_err());
assert_eq!(
result.unwrap_err().to_string(),
"Internal column __metric is reserved".to_string()
"Internal column __table_id is reserved".to_string()
);

// valid request
Expand Down Expand Up @@ -1049,7 +1048,7 @@ mod tests {
assert_eq!(data_region_request.column_metadatas.len(), 4);
assert_eq!(
data_region_request.primary_key,
vec![ReservedColumnId::metric_name(), ReservedColumnId::tsid()]
vec![ReservedColumnId::table_id(), ReservedColumnId::tsid()]
);
}

Expand Down
8 changes: 4 additions & 4 deletions src/store-api/src/storage/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ enum ReservedColumnType {
Sequence,
OpType,
Tsid,
MetricName,
TableId,
}

/// Column id reserved by the engine.
Expand Down Expand Up @@ -76,11 +76,11 @@ impl ReservedColumnId {
Self::BASE | ReservedColumnType::Tsid as ColumnId
}

/// Id for storing metric name column.
/// Id for storing logical table id column.
///
/// Used by: metric engine
pub const fn metric_name() -> ColumnId {
Self::BASE | ReservedColumnType::MetricName as ColumnId
pub const fn table_id() -> ColumnId {
Self::BASE | ReservedColumnType::TableId as ColumnId
}
}

Expand Down

0 comments on commit 4344c1d

Please sign in to comment.