Skip to content

Commit

Permalink
add basic test
Browse files Browse the repository at this point in the history
Signed-off-by: Ruihang Xia <[email protected]>
  • Loading branch information
waynexia committed Nov 2, 2023
1 parent 722d683 commit a2bc099
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 16 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

8 changes: 7 additions & 1 deletion src/metric-engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ common-error.workspace = true
common-macro.workspace = true
common-query.workspace = true
common-recordbatch.workspace = true
common-telemetry.workspace = true
common-time.workspace = true
datatypes.workspace = true
mito2 = { workspace = true, features = ["test"] }
mito2.workspace = true
object-store.workspace = true
serde_json.workspace = true
snafu.workspace = true
store-api.workspace = true
tokio.workspace = true

[dev-dependencies]
common-test-util.workspace = true
mito2 = { workspace = true, features = ["test"] }
90 changes: 75 additions & 15 deletions src/metric-engine/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const METADATA_SCHEMA_VALUE_COLUMN_NAME: &str = "val";
const METADATA_REGION_SUBDIR: &str = "metadata";
const DATA_REGION_SUBDIR: &str = "data";

pub const METRIC_ENGINE_NAME: &str = "metric";

pub struct MetricEngine {
inner: Arc<MetricEngineInner>,
}
Expand All @@ -56,7 +58,7 @@ pub struct MetricEngine {
impl RegionEngine for MetricEngine {
/// Name of this engine
fn name(&self) -> &str {
"metric"
METRIC_ENGINE_NAME
}

/// Handles request to the region.
Expand All @@ -67,19 +69,24 @@ impl RegionEngine for MetricEngine {
region_id: RegionId,
request: RegionRequest,
) -> std::result::Result<Output, BoxedError> {
match request {
let result = match request {
RegionRequest::Put(_) => todo!(),
RegionRequest::Delete(_) => todo!(),
RegionRequest::Create(create) => todo!(),
RegionRequest::Create(create) => self
.inner
.create_region(region_id, create)
.await
.map(|_| Output::AffectedRows(0)),
RegionRequest::Drop(_) => todo!(),
RegionRequest::Open(_) => todo!(),
RegionRequest::Close(_) => todo!(),
RegionRequest::Alter(_) => todo!(),
RegionRequest::Flush(_) => todo!(),
RegionRequest::Compact(_) => todo!(),
RegionRequest::Truncate(_) => todo!(),
}
todo!()
};

result.map_err(BoxedError::new)
}

/// Handles substrait query and return a stream of record batches
Expand Down Expand Up @@ -139,15 +146,15 @@ impl MetricEngineInner {
let create_metadata_region_request =
self.create_request_for_metadata_region(&request.region_dir);

self.mito
.handle_request(
data_region_id,
RegionRequest::Create(create_data_region_request),
)
.await
.with_context(|_| CreateMitoRegionSnafu {
region_type: DATA_REGION_SUBDIR,
})?;
// self.mito
// .handle_request(
// data_region_id,
// RegionRequest::Create(create_data_region_request),
// )
// .await
// .with_context(|_| CreateMitoRegionSnafu {
// region_type: DATA_REGION_SUBDIR,
// })?;
self.mito
.handle_request(
metadata_region_id,
Expand Down Expand Up @@ -194,7 +201,7 @@ impl MetricEngineInner {
semantic_type: SemanticType::Timestamp,
column_schema: ColumnSchema::new(
METADATA_SCHEMA_TIMESTAMP_COLUMN_NAME,
ConcreteDataType::time_millisecond_datatype(),
ConcreteDataType::timestamp_millisecond_datatype(),
false,
)
.with_default_constraint(Some(datatypes::schema::ColumnDefaultConstraint::Value(
Expand Down Expand Up @@ -255,3 +262,56 @@ impl MetricEngineInner {
data_region_request
}
}

#[cfg(test)]
mod test {
use std::time::Duration;

use common_telemetry::info;

use super::*;
use crate::test_util::TestEnv;

#[tokio::test]
async fn create_metadata_region() {
common_telemetry::init_default_ut_logging();

let env = TestEnv::new().await;
let mito = env.mito();
let engine = MetricEngine {
inner: Arc::new(MetricEngineInner { mito }),
};
let engine_dir = env.data_home();
info!("[DEBUG] {engine_dir}");
let region_dir = join_dir(&engine_dir, "test_metric_region");

let region_id = RegionId::new(1, 2);
let region_create_request = RegionCreateRequest {
engine: METRIC_ENGINE_NAME.to_string(),
column_metadatas: vec![],
primary_key: vec![],
options: HashMap::new(),
region_dir: "test_metric_region".to_string(),
};

// create the region
engine
.handle_request(region_id, RegionRequest::Create(region_create_request))
.await
.unwrap();

let mut ls_result = tokio::fs::read_dir(&engine_dir).await.unwrap();
while let Some(dir) = ls_result.next_entry().await.unwrap() {
info!("[DEBUG] {dir:?}");
}

// assert metadata region's dir
let metadata_region_dir = join_dir(&region_dir, METADATA_REGION_SUBDIR);
let exist = tokio::fs::try_exists(region_dir).await.unwrap();
assert!(exist);

// check mito engine
let metadata_region_id = utils::to_metadata_region_id(region_id);
let result = env.mito().get_metadata(metadata_region_id).await.unwrap();
}
}
2 changes: 2 additions & 0 deletions src/metric-engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ pub mod engine;
pub mod error;
#[allow(unused)]
mod metadata_region;
#[cfg(test)]
mod test_util;
mod utils;
50 changes: 50 additions & 0 deletions src/metric-engine/src/test_util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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.

//! Utilities for testing.
use mito2::config::MitoConfig;
use mito2::engine::MitoEngine;
use mito2::test_util::TestEnv as MitoTestEnv;
use object_store::util::join_dir;

/// Env to test metric engine.
pub struct TestEnv {
mito_env: MitoTestEnv,
mito: MitoEngine,
}

impl TestEnv {
/// Returns a new env with empty prefix for test.
pub async fn new() -> Self {
Self::with_prefix("").await
}

/// Returns a new env with specific `prefix` for test.
pub async fn with_prefix(prefix: &str) -> Self {
let mut mito_env = MitoTestEnv::with_prefix(prefix);
let mito = mito_env.create_engine(MitoConfig::default()).await;
Self { mito_env, mito }
}

pub fn data_home(&self) -> String {
let env_root = self.mito_env.data_home().to_string_lossy().to_string();
join_dir(&env_root, "data")
}

/// Returns a reference to the engine.
pub fn mito(&self) -> MitoEngine {
self.mito.clone()
}
}
5 changes: 5 additions & 0 deletions src/mito2/src/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod scheduler_util;
pub mod version_util;

use std::collections::HashMap;
use std::path::Path;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Arc;

Expand Down Expand Up @@ -119,6 +120,10 @@ impl TestEnv {
.map(|manager| manager.default_object_store().clone())
}

pub fn data_home(&self) -> &Path {
self.data_home.path()
}

/// Creates a new engine with specific config under this env.
pub async fn create_engine(&mut self, config: MitoConfig) -> MitoEngine {
let (log_store, object_store_manager) = self.create_log_and_object_store_manager().await;
Expand Down

0 comments on commit a2bc099

Please sign in to comment.