Skip to content
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: replace ahash with murmur3 on generating tsid #3007

Merged
merged 2 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/metric-engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ edition.workspace = true
license.workspace = true

[dependencies]
ahash.workspace = true
api.workspace = true
aquamarine.workspace = true
async-trait.workspace = true
Expand All @@ -20,6 +19,7 @@ datafusion.workspace = true
datatypes.workspace = true
lazy_static = "1.4"
mito2.workspace = true
mur3 = "0.1"
object-store.workspace = true
prometheus.workspace = true
serde_json.workspace = true
Expand Down
3 changes: 0 additions & 3 deletions src/metric-engine/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ use self::state::MetricEngineState;
use crate::data_region::DataRegion;
use crate::metadata_region::MetadataRegion;

/// Fixed random state for generating tsid
pub(crate) const RANDOM_STATE: ahash::RandomState = ahash::RandomState::with_seeds(1, 2, 3, 4);

#[cfg_attr(doc, aquamarine::aquamarine)]
/// # Metric Engine
///
Expand Down
33 changes: 17 additions & 16 deletions src/metric-engine/src/engine/put.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

use std::hash::{BuildHasher, Hash, Hasher};

use ahash::RandomState;
use api::v1::value::ValueData;
use api::v1::{ColumnDataType, ColumnSchema, Row, Rows, SemanticType};
use common_telemetry::{error, info};
Expand All @@ -25,13 +24,16 @@ use store_api::metric_engine_consts::{
use store_api::region_request::{AffectedRows, RegionPutRequest};
use store_api::storage::{RegionId, TableId};

use crate::engine::{MetricEngineInner, RANDOM_STATE};
use crate::engine::MetricEngineInner;
use crate::error::{
ColumnNotFoundSnafu, ForbiddenPhysicalAlterSnafu, LogicalRegionNotFoundSnafu, Result,
};
use crate::metrics::FORBIDDEN_OPERATION_COUNT;
use crate::utils::{to_data_region_id, to_metadata_region_id};

// A random number
const TSID_HASH_SEED: u32 = 846793005;

impl MetricEngineInner {
/// Dispatch region put request
pub async fn put_region(
Expand Down Expand Up @@ -174,22 +176,20 @@ impl MetricEngineInner {
});

// fill internal columns
let mut random_state = RANDOM_STATE.clone();
for row in &mut rows.rows {
Self::fill_internal_columns(&mut random_state, table_id, &tag_col_indices, row);
Self::fill_internal_columns(table_id, &tag_col_indices, row);
}

Ok(())
}

/// Fills internal columns of a row with table name and a hash of tag values.
fn fill_internal_columns(
random_state: &mut RandomState,
table_id: TableId,
tag_col_indices: &[(usize, String)],
row: &mut Row,
) {
let mut hasher = random_state.build_hasher();
let mut hasher = mur3::Hasher128::with_seed(TSID_HASH_SEED);
for (idx, name) in tag_col_indices {
let tag = row.values[*idx].clone();
name.hash(&mut hasher);
Expand All @@ -198,7 +198,8 @@ impl MetricEngineInner {
string.hash(&mut hasher);
}
}
let hash = hasher.finish();
// TSID is 64 bits, simply truncate the 128 bits hash
let (hash, _) = hasher.finish128();

// fill table id and tsid
row.values.push(ValueData::U32Value(table_id).into());
Expand Down Expand Up @@ -247,15 +248,15 @@ mod tests {
.unwrap();
let batches = RecordBatches::try_collect(stream).await.unwrap();
let expected = "\
+-------------------------+----------------+------------+---------------------+-------+
| greptime_timestamp | greptime_value | __table_id | __tsid | job |
+-------------------------+----------------+------------+---------------------+-------+
| 1970-01-01T00:00:00 | 0.0 | 3 | 4844750677434873907 | tag_0 |
| 1970-01-01T00:00:00.001 | 1.0 | 3 | 4844750677434873907 | tag_0 |
| 1970-01-01T00:00:00.002 | 2.0 | 3 | 4844750677434873907 | tag_0 |
| 1970-01-01T00:00:00.003 | 3.0 | 3 | 4844750677434873907 | tag_0 |
| 1970-01-01T00:00:00.004 | 4.0 | 3 | 4844750677434873907 | tag_0 |
+-------------------------+----------------+------------+---------------------+-------+";
+-------------------------+----------------+------------+----------------------+-------+
| greptime_timestamp | greptime_value | __table_id | __tsid | job |
+-------------------------+----------------+------------+----------------------+-------+
| 1970-01-01T00:00:00 | 0.0 | 3 | 12881218023286672757 | tag_0 |
| 1970-01-01T00:00:00.001 | 1.0 | 3 | 12881218023286672757 | tag_0 |
| 1970-01-01T00:00:00.002 | 2.0 | 3 | 12881218023286672757 | tag_0 |
| 1970-01-01T00:00:00.003 | 3.0 | 3 | 12881218023286672757 | tag_0 |
| 1970-01-01T00:00:00.004 | 4.0 | 3 | 12881218023286672757 | tag_0 |
+-------------------------+----------------+------------+----------------------+-------+";
assert_eq!(expected, batches.pretty_print().unwrap(), "physical region");

// read data from logical region
Expand Down
Loading