Skip to main content

tests_integration/
test_util.rs

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
15use std::env;
16use std::fmt::Display;
17use std::net::SocketAddr;
18use std::sync::Arc;
19use std::time::Duration;
20
21use api::v1::Basic;
22use auth::{
23    DefaultPermissionChecker, PermissionCheckerRef, UserProviderRef, user_provider_from_option,
24};
25use axum::Router;
26use catalog::kvbackend::KvBackendCatalogManager;
27use client::{Client, Database};
28use common_base::Plugins;
29use common_config::Configurable;
30use common_meta::key::TableMetadataManager;
31use common_meta::key::catalog_name::CatalogNameKey;
32use common_meta::key::schema_name::SchemaNameKey;
33use common_meta::kv_backend::KvBackendRef;
34use common_query::Output;
35use common_runtime::runtime::BuilderBuild;
36use common_runtime::{Builder as RuntimeBuilder, Runtime};
37use common_test_util::ports;
38use common_test_util::temp_dir::{TempDir, create_temp_dir};
39use common_wal::config::DatanodeWalConfig;
40use datanode::config::{DatanodeOptions, StorageConfig};
41use frontend::instance::Instance;
42use frontend::service_config::{MysqlOptions, PostgresOptions};
43use mito2::gc::GcConfig;
44use object_store::config::{
45    AzblobConfig, FileConfig, GcsConfig, ObjectStoreConfig, OssConfig, S3Config,
46};
47use object_store::services::{Azblob, Gcs, Oss, S3};
48use object_store::test_util::TempFolder;
49use object_store::{AzblobConnection, GcsConnection, ObjectStore, OssConnection, S3Connection};
50use servers::grpc::builder::GrpcServerBuilder;
51use servers::grpc::greptime_handler::GreptimeRequestHandler;
52use servers::grpc::{FlightCompression, GrpcOptions, GrpcServer, GrpcServerConfig};
53use servers::http::{HttpOptions, HttpServerBuilder};
54use servers::metrics_handler::MetricsHandler;
55use servers::mysql::server::{MysqlServer, MysqlSpawnConfig, MysqlSpawnRef};
56use servers::otel_arrow::OtelArrowServiceHandler;
57use servers::pending_rows_batcher::PendingRowsBatcher;
58use servers::postgres::PostgresServer;
59use servers::prom_remote_write::validation::PromValidationMode;
60use servers::query_handler::sql::SqlQueryHandler;
61use servers::request_memory_limiter::ServerMemoryLimiter;
62use servers::server::Server;
63use servers::tls::ReloadableTlsServerConfig;
64use session::context::QueryContext;
65
66use crate::standalone::{GreptimeDbStandalone, GreptimeDbStandaloneBuilder};
67
68pub const PEER_PLACEHOLDER_ADDR: &str = "127.0.0.1:3001";
69
70#[derive(Debug, Clone, Copy, Eq, PartialEq)]
71pub enum StorageType {
72    S3,
73    S3WithCache,
74    File,
75    Oss,
76    Azblob,
77    Gcs,
78}
79
80impl Display for StorageType {
81    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
82        match self {
83            StorageType::S3 => write!(f, "S3"),
84            StorageType::S3WithCache => write!(f, "S3"),
85            StorageType::File => write!(f, "File"),
86            StorageType::Oss => write!(f, "Oss"),
87            StorageType::Azblob => write!(f, "Azblob"),
88            StorageType::Gcs => write!(f, "Gcs"),
89        }
90    }
91}
92
93impl StorageType {
94    pub fn build_storage_types_based_on_env() -> Vec<StorageType> {
95        let mut storage_types = Vec::with_capacity(4);
96        storage_types.push(StorageType::File);
97        if let Ok(bucket) = env::var("GT_S3_BUCKET")
98            && !bucket.is_empty()
99        {
100            storage_types.push(StorageType::S3);
101        }
102        if env::var("GT_OSS_BUCKET").is_ok() {
103            storage_types.push(StorageType::Oss);
104        }
105        if env::var("GT_AZBLOB_CONTAINER").is_ok() {
106            storage_types.push(StorageType::Azblob);
107        }
108        if env::var("GT_GCS_BUCKET").is_ok() {
109            storage_types.push(StorageType::Gcs);
110        }
111        storage_types
112    }
113
114    pub fn test_on(&self) -> bool {
115        let _ = dotenv::dotenv();
116
117        match self {
118            StorageType::File => true, // always test file
119            StorageType::S3 | StorageType::S3WithCache => {
120                if let Ok(b) = env::var("GT_S3_BUCKET") {
121                    !b.is_empty()
122                } else {
123                    false
124                }
125            }
126            StorageType::Oss => {
127                if let Ok(b) = env::var("GT_OSS_BUCKET") {
128                    !b.is_empty()
129                } else {
130                    false
131                }
132            }
133            StorageType::Azblob => {
134                if let Ok(b) = env::var("GT_AZBLOB_CONTAINER") {
135                    !b.is_empty()
136                } else {
137                    false
138                }
139            }
140            StorageType::Gcs => {
141                if let Ok(b) = env::var("GT_GCS_BUCKET") {
142                    !b.is_empty()
143                } else {
144                    false
145                }
146            }
147        }
148    }
149}
150
151fn s3_test_config() -> S3Config {
152    S3Config {
153        connection: S3Connection {
154            root: uuid::Uuid::new_v4().to_string(),
155            access_key_id: env::var("GT_S3_ACCESS_KEY_ID").unwrap().into(),
156            secret_access_key: env::var("GT_S3_ACCESS_KEY").unwrap().into(),
157            bucket: env::var("GT_S3_BUCKET").unwrap(),
158            region: Some(env::var("GT_S3_REGION").unwrap()),
159            endpoint: env::var("GT_S3_ENDPOINT_URL").ok(),
160            ..Default::default()
161        },
162        ..Default::default()
163    }
164}
165
166pub fn get_test_store_config(store_type: &StorageType) -> (ObjectStoreConfig, TempDirGuard) {
167    let _ = dotenv::dotenv();
168
169    match store_type {
170        StorageType::Gcs => {
171            let gcs_config = GcsConfig {
172                connection: GcsConnection {
173                    root: uuid::Uuid::new_v4().to_string(),
174                    bucket: env::var("GT_GCS_BUCKET").unwrap(),
175                    scope: env::var("GT_GCS_SCOPE").unwrap(),
176                    credential_path: env::var("GT_GCS_CREDENTIAL_PATH").unwrap().into(),
177                    credential: env::var("GT_GCS_CREDENTIAL").unwrap().into(),
178                    endpoint: env::var("GT_GCS_ENDPOINT").unwrap_or_default(),
179                },
180                ..Default::default()
181            };
182
183            let builder = Gcs::from(&gcs_config.connection);
184            let config = ObjectStoreConfig::Gcs(gcs_config);
185            let store = ObjectStore::new(builder).unwrap().finish();
186            (config, TempDirGuard::Gcs(TempFolder::new(&store, "/")))
187        }
188        StorageType::Azblob => {
189            let azblob_config = AzblobConfig {
190                connection: AzblobConnection {
191                    root: uuid::Uuid::new_v4().to_string(),
192                    container: env::var("GT_AZBLOB_CONTAINER").unwrap(),
193                    account_name: env::var("GT_AZBLOB_ACCOUNT_NAME").unwrap().into(),
194                    account_key: env::var("GT_AZBLOB_ACCOUNT_KEY").unwrap().into(),
195                    endpoint: env::var("GT_AZBLOB_ENDPOINT").unwrap(),
196                    ..Default::default()
197                },
198                ..Default::default()
199            };
200
201            let builder = Azblob::from(&azblob_config.connection);
202            let config = ObjectStoreConfig::Azblob(azblob_config);
203            let store = ObjectStore::new(builder).unwrap().finish();
204            (config, TempDirGuard::Azblob(TempFolder::new(&store, "/")))
205        }
206        StorageType::Oss => {
207            let oss_config = OssConfig {
208                connection: OssConnection {
209                    root: uuid::Uuid::new_v4().to_string(),
210                    access_key_id: env::var("GT_OSS_ACCESS_KEY_ID").unwrap().into(),
211                    access_key_secret: env::var("GT_OSS_ACCESS_KEY").unwrap().into(),
212                    bucket: env::var("GT_OSS_BUCKET").unwrap(),
213                    endpoint: env::var("GT_OSS_ENDPOINT").unwrap(),
214                },
215                ..Default::default()
216            };
217
218            let builder = Oss::from(&oss_config.connection);
219            let config = ObjectStoreConfig::Oss(oss_config);
220            let store = ObjectStore::new(builder).unwrap().finish();
221            (config, TempDirGuard::Oss(TempFolder::new(&store, "/")))
222        }
223        StorageType::S3 | StorageType::S3WithCache => {
224            let mut s3_config = s3_test_config();
225
226            if *store_type == StorageType::S3WithCache {
227                s3_config.cache.cache_path = "/tmp/greptimedb_cache".to_string();
228            } else {
229                s3_config.cache.enable_read_cache = false;
230            }
231
232            let builder = S3::from(&s3_config.connection);
233            let config = ObjectStoreConfig::S3(s3_config);
234            let store = ObjectStore::new(builder).unwrap().finish();
235            (config, TempDirGuard::S3(TempFolder::new(&store, "/")))
236        }
237        StorageType::File => (ObjectStoreConfig::File(FileConfig {}), TempDirGuard::None),
238    }
239}
240
241pub enum TempDirGuard {
242    None,
243    S3(TempFolder),
244    Oss(TempFolder),
245    Azblob(TempFolder),
246    Gcs(TempFolder),
247}
248
249pub struct TestGuard {
250    pub home_guard: FileDirGuard,
251    pub storage_guards: Vec<StorageGuard>,
252}
253
254pub struct FileDirGuard {
255    pub temp_dir: TempDir,
256}
257
258impl FileDirGuard {
259    pub fn new(temp_dir: TempDir) -> Self {
260        Self { temp_dir }
261    }
262}
263
264pub struct StorageGuard(pub TempDirGuard);
265
266impl TestGuard {
267    pub async fn remove_all(&mut self) {
268        for storage_guard in self.storage_guards.iter_mut() {
269            if let TempDirGuard::S3(guard)
270            | TempDirGuard::Oss(guard)
271            | TempDirGuard::Azblob(guard)
272            | TempDirGuard::Gcs(guard) = &mut storage_guard.0
273            {
274                guard.remove_all().await.unwrap()
275            }
276        }
277    }
278}
279
280impl Drop for TestGuard {
281    fn drop(&mut self) {
282        let (tx, rx) = std::sync::mpsc::channel();
283
284        let guards = std::mem::take(&mut self.storage_guards);
285        common_runtime::spawn_global(async move {
286            let mut errors = vec![];
287            for guard in guards {
288                if let TempDirGuard::S3(guard)
289                | TempDirGuard::Oss(guard)
290                | TempDirGuard::Azblob(guard)
291                | TempDirGuard::Gcs(guard) = guard.0
292                    && let Err(e) = guard.remove_all().await
293                {
294                    errors.push(e);
295                }
296            }
297            if errors.is_empty() {
298                tx.send(Ok(())).unwrap();
299            } else {
300                tx.send(Err(errors)).unwrap();
301            }
302        });
303        rx.recv().unwrap().unwrap_or_else(|e| panic!("{:?}", e));
304    }
305}
306
307pub fn create_tmp_dir_and_datanode_opts(
308    default_store_type: StorageType,
309    store_provider_types: Vec<StorageType>,
310    name: &str,
311    wal_config: DatanodeWalConfig,
312    gc_config: GcConfig,
313) -> (DatanodeOptions, TestGuard) {
314    let home_tmp_dir = create_temp_dir(&format!("gt_data_{name}"));
315    let home_dir = home_tmp_dir.path().to_str().unwrap().to_string();
316
317    // Excludes the default object store.
318    let mut store_providers = Vec::with_capacity(store_provider_types.len());
319    // Includes the default object store.
320    let mut storage_guards = Vec::with_capacity(store_provider_types.len() + 1);
321
322    let (default_store, data_tmp_dir) = get_test_store_config(&default_store_type);
323    storage_guards.push(StorageGuard(data_tmp_dir));
324
325    for store_type in store_provider_types {
326        let (store, data_tmp_dir) = get_test_store_config(&store_type);
327        store_providers.push(store);
328        storage_guards.push(StorageGuard(data_tmp_dir))
329    }
330    let opts = create_datanode_opts(
331        default_store,
332        store_providers,
333        home_dir,
334        wal_config,
335        gc_config,
336    );
337
338    (
339        opts,
340        TestGuard {
341            home_guard: FileDirGuard::new(home_tmp_dir),
342            storage_guards,
343        },
344    )
345}
346
347pub(crate) fn create_datanode_opts(
348    default_store: ObjectStoreConfig,
349    providers: Vec<ObjectStoreConfig>,
350    home_dir: String,
351    wal_config: DatanodeWalConfig,
352    gc_config: GcConfig,
353) -> DatanodeOptions {
354    let region_engine = DatanodeOptions::default()
355        .region_engine
356        .into_iter()
357        .map(|mut v| {
358            if let datanode::config::RegionEngineConfig::Mito(mito_config) = &mut v {
359                mito_config.gc = gc_config.clone();
360            }
361            v
362        })
363        .collect();
364    DatanodeOptions {
365        node_id: Some(0),
366        require_lease_before_startup: true,
367        storage: StorageConfig {
368            data_home: home_dir,
369            copy_root: None,
370            providers,
371            store: default_store,
372        },
373        grpc: GrpcOptions::default()
374            .with_bind_addr(PEER_PLACEHOLDER_ADDR)
375            .with_server_addr(PEER_PLACEHOLDER_ADDR),
376        wal: wal_config,
377        region_engine,
378        ..Default::default()
379    }
380}
381
382pub(crate) async fn create_test_table(instance: &Instance, table_name: &str) {
383    let sql = format!(
384        r#"
385CREATE TABLE IF NOT EXISTS {table_name} (
386    host String NOT NULL PRIMARY KEY,
387    cpu DOUBLE NULL,
388    memory DOUBLE NULL,
389    ts TIMESTAMP NOT NULL TIME INDEX,
390)
391"#
392    );
393
394    let result = instance.do_query(&sql, QueryContext::arc()).await;
395    let _ = result.first().unwrap().as_ref().unwrap();
396}
397
398async fn setup_standalone_instance(
399    test_name: &str,
400    store_type: StorageType,
401) -> GreptimeDbStandalone {
402    GreptimeDbStandaloneBuilder::new(test_name)
403        .with_default_store_type(store_type)
404        .build()
405        .await
406}
407
408async fn setup_standalone_instance_with_slow_query_threshold(
409    test_name: &str,
410    store_type: StorageType,
411    slow_query_threshold: std::time::Duration,
412) -> GreptimeDbStandalone {
413    GreptimeDbStandaloneBuilder::new(test_name)
414        .with_default_store_type(store_type)
415        .with_slow_query_threshold(slow_query_threshold)
416        .build()
417        .await
418}
419
420async fn setup_standalone_instance_with_plugins(
421    test_name: &str,
422    store_type: StorageType,
423    plugins: Plugins,
424) -> GreptimeDbStandalone {
425    GreptimeDbStandaloneBuilder::new(test_name)
426        .with_default_store_type(store_type)
427        .with_plugin(plugins)
428        .build()
429        .await
430}
431
432async fn setup_standalone_instance_with_plugins_and_slow_query_threshold(
433    test_name: &str,
434    store_type: StorageType,
435    plugins: Plugins,
436    slow_query_threshold: std::time::Duration,
437) -> GreptimeDbStandalone {
438    GreptimeDbStandaloneBuilder::new(test_name)
439        .with_default_store_type(store_type)
440        .with_plugin(plugins)
441        .with_slow_query_threshold(slow_query_threshold)
442        .build()
443        .await
444}
445
446pub async fn setup_test_http_app(store_type: StorageType, name: &str) -> (Router, TestGuard) {
447    let instance = setup_standalone_instance(name, store_type).await;
448
449    let http_opts = HttpOptions {
450        addr: format!("127.0.0.1:{}", ports::get_port()),
451        ..Default::default()
452    };
453    let http_server = HttpServerBuilder::new(http_opts)
454        .with_sql_handler(instance.fe_instance().clone())
455        .with_logs_handler(instance.fe_instance().clone())
456        .with_metrics_handler(MetricsHandler)
457        .with_greptime_config_options(instance.opts.datanode_options().to_toml().unwrap())
458        .build();
459    (
460        http_server.build(http_server.make_app()).unwrap(),
461        instance.guard,
462    )
463}
464
465pub async fn setup_test_http_app_with_frontend(
466    store_type: StorageType,
467    name: &str,
468) -> (Router, TestGuard) {
469    setup_test_http_app_with_frontend_and_user_provider(store_type, name, None).await
470}
471
472pub async fn setup_test_http_app_with_frontend_and_slow_query_threshold(
473    store_type: StorageType,
474    name: &str,
475    slow_query_threshold: std::time::Duration,
476) -> (Router, TestGuard) {
477    let instance =
478        setup_standalone_instance_with_slow_query_threshold(name, store_type, slow_query_threshold)
479            .await;
480
481    create_test_table(instance.fe_instance(), "demo").await;
482
483    let http_opts = HttpOptions {
484        addr: format!("127.0.0.1:{}", ports::get_port()),
485        ..Default::default()
486    };
487
488    let http_server = HttpServerBuilder::new(http_opts)
489        .with_sql_handler(instance.fe_instance().clone())
490        .with_log_ingest_handler(instance.fe_instance().clone(), None, None)
491        .with_logs_handler(instance.fe_instance().clone())
492        .with_influxdb_handler(instance.fe_instance().clone())
493        .with_otlp_handler(instance.fe_instance().clone(), true, false)
494        .with_jaeger_handler(instance.fe_instance().clone())
495        .with_greptime_config_options(instance.opts.to_toml().unwrap())
496        .build();
497
498    let app = http_server.build(http_server.make_app()).unwrap();
499    (app, instance.guard)
500}
501
502pub async fn setup_test_http_app_with_frontend_and_user_provider(
503    store_type: StorageType,
504    name: &str,
505    user_provider: Option<UserProviderRef>,
506) -> (Router, TestGuard) {
507    setup_test_http_app_with_frontend_and_custom_options(
508        store_type,
509        name,
510        user_provider,
511        None,
512        None,
513        false,
514    )
515    .await
516}
517
518pub async fn setup_test_http_app_with_otlp_exponential_histogram(
519    store_type: StorageType,
520    name: &str,
521    enabled: bool,
522) -> (Router, TestGuard) {
523    setup_test_http_app_with_frontend_and_custom_options(
524        store_type, name, None, None, None, enabled,
525    )
526    .await
527}
528
529pub async fn setup_test_http_app_with_frontend_and_custom_options(
530    store_type: StorageType,
531    name: &str,
532    user_provider: Option<UserProviderRef>,
533    http_opts: Option<HttpOptions>,
534    memory_limiter: Option<ServerMemoryLimiter>,
535    experimental_enable_exponential_histogram: bool,
536) -> (Router, TestGuard) {
537    let plugins = Plugins::new();
538    if let Some(user_provider) = user_provider.clone() {
539        plugins.insert::<UserProviderRef>(user_provider.clone());
540        plugins.insert::<PermissionCheckerRef>(DefaultPermissionChecker::arc());
541    }
542
543    let instance = setup_standalone_instance_with_plugins(name, store_type, plugins).await;
544
545    create_test_table(instance.fe_instance(), "demo").await;
546
547    let http_opts = http_opts.unwrap_or_else(|| HttpOptions {
548        addr: format!("127.0.0.1:{}", ports::get_port()),
549        ..Default::default()
550    });
551
552    let mut http_server = HttpServerBuilder::new(http_opts)
553        .with_sql_handler(instance.fe_instance().clone())
554        .with_log_ingest_handler(instance.fe_instance().clone(), None, None)
555        .with_logs_handler(instance.fe_instance().clone())
556        .with_influxdb_handler(instance.fe_instance().clone())
557        .with_otlp_handler(
558            instance.fe_instance().clone(),
559            true,
560            experimental_enable_exponential_histogram,
561        )
562        .with_prometheus_handler(instance.fe_instance().clone())
563        .with_jaeger_handler(instance.fe_instance().clone())
564        .with_dashboard_handler(instance.fe_instance().clone())
565        .with_greptime_config_options(instance.opts.to_toml().unwrap());
566
567    if let Some(user_provider) = user_provider {
568        http_server = http_server.with_user_provider(user_provider);
569    }
570
571    if let Some(limiter) = memory_limiter {
572        http_server = http_server.with_memory_limiter(limiter);
573    }
574
575    let http_server = http_server.build();
576
577    let app = http_server.build(http_server.make_app()).unwrap();
578    (app, instance.guard)
579}
580
581async fn run_sql(sql: &str, instance: &GreptimeDbStandalone) {
582    let result = instance
583        .fe_instance()
584        .do_query(sql, QueryContext::arc())
585        .await;
586    let _ = result.first().unwrap().as_ref().unwrap();
587}
588
589pub async fn setup_test_prom_app_with_frontend(
590    store_type: StorageType,
591    name: &str,
592) -> (Router, TestGuard) {
593    setup_test_prom_app_with_frontend_inner(store_type, name, false, false).await
594}
595
596pub async fn setup_test_prom_app_with_frontend_native_histogram(
597    store_type: StorageType,
598    name: &str,
599) -> (Router, TestGuard) {
600    setup_test_prom_app_with_frontend_inner(store_type, name, false, true).await
601}
602
603/// Like [`setup_test_prom_app_with_frontend`] but enables the pending-rows batcher,
604/// so Prometheus remote write goes through the batched (metric-engine) path instead
605/// of the direct `PromStoreProtocolHandler::write` path.
606pub async fn setup_test_prom_app_with_frontend_batched(
607    store_type: StorageType,
608    name: &str,
609) -> (Router, TestGuard) {
610    setup_test_prom_app_with_frontend_inner(store_type, name, true, false).await
611}
612
613async fn setup_test_prom_app_with_frontend_inner(
614    store_type: StorageType,
615    name: &str,
616    enable_batcher: bool,
617    experimental_enable_prometheus_native_histogram: bool,
618) -> (Router, TestGuard) {
619    unsafe {
620        std::env::set_var("TZ", "UTC");
621    }
622
623    let instance = setup_standalone_instance(name, store_type).await;
624
625    // build physical table
626    let sql = "CREATE TABLE phy (ts timestamp time index, val double, host string primary key) engine=metric with ('physical_metric_table' = '')";
627    run_sql(sql, &instance).await;
628    let sql = "CREATE TABLE phy_ns (ts timestamp(0) time index, val double, host string primary key) engine=metric with ('physical_metric_table' = '')";
629    run_sql(sql, &instance).await;
630    // build metric tables
631    let sql = "CREATE TABLE demo (ts timestamp time index, val double, host string primary key) engine=metric with ('on_physical_table' = 'phy')";
632    run_sql(sql, &instance).await;
633    let sql = "CREATE TABLE demo_metrics (ts timestamp time index, val double, idc string primary key) engine=metric with ('on_physical_table' = 'phy')";
634    run_sql(sql, &instance).await;
635    let sql = "CREATE TABLE multi_labels (ts timestamp(0) time index, val double, idc string, env string, host string, primary key (idc, env, host)) engine=metric with ('on_physical_table' = 'phy_ns')";
636    run_sql(sql, &instance).await;
637
638    // insert rows
639    let sql = "INSERT INTO demo(host, val, ts) VALUES ('host1', 1.1, 0), ('host2', 2.1, 600000)";
640    run_sql(sql, &instance).await;
641    let sql =
642        "INSERT INTO demo_metrics(idc, val, ts) VALUES ('idc1', 1.1, 0), ('idc2', 2.1, 600000)";
643    run_sql(sql, &instance).await;
644    // insert a row with empty label
645    let sql = "INSERT INTO demo_metrics(val, ts) VALUES (1.1, 0)";
646    run_sql(sql, &instance).await;
647
648    // insert rows to multi_labels
649    let sql = "INSERT INTO multi_labels(idc, env, host, val, ts) VALUES ('idc1', 'dev', 'host1', 1.1, 0), ('idc1', 'dev', 'host2', 2.1, 0), ('idc2', 'dev', 'host1', 1.1, 0), ('idc2', 'test', 'host3', 2.1, 0)";
650    run_sql(sql, &instance).await;
651
652    // build physical table
653    let sql = "CREATE TABLE phy2 (ts timestamp(9) time index, val double, host string primary key) engine=metric with ('physical_metric_table' = '')";
654    run_sql(sql, &instance).await;
655    let sql = "CREATE TABLE demo_metrics_with_nanos(ts timestamp(9) time index, val double, idc string primary key) engine=metric with ('on_physical_table' = 'phy2')";
656    run_sql(sql, &instance).await;
657    let sql = "INSERT INTO demo_metrics_with_nanos(idc, val, ts) VALUES ('idc1', 1.1, 0)";
658    run_sql(sql, &instance).await;
659
660    // a mito table with non-prometheus compatible values
661    let sql = "CREATE TABLE mito (ts timestamp(9) time index, val double, host bigint primary key) engine=mito";
662    run_sql(sql, &instance).await;
663    let sql = "INSERT INTO mito(host, val, ts) VALUES (1, 1.1, 0)";
664    run_sql(sql, &instance).await;
665
666    let http_opts = HttpOptions {
667        addr: format!("127.0.0.1:{}", ports::get_port()),
668        ..Default::default()
669    };
670    let frontend_ref = instance.fe_instance().clone();
671    // Mirror the production wiring at `frontend::server`: build the batcher from the
672    // instance's managers. A short flush interval keeps the test responsive.
673    let pending_rows_batcher = if enable_batcher {
674        PendingRowsBatcher::try_new(
675            frontend_ref.partition_manager().clone(),
676            frontend_ref.node_manager().clone(),
677            frontend_ref.catalog_manager().clone(),
678            frontend_ref.table_flownode_set_cache().clone(),
679            true,
680            frontend_ref.clone(),
681            Duration::from_millis(50),
682            1000,
683            4,
684            64,
685            64,
686            std::num::NonZeroUsize::new(1024).unwrap(),
687        )
688    } else {
689        None
690    };
691    let http_server = HttpServerBuilder::new(http_opts)
692        .with_sql_handler(frontend_ref.clone())
693        .with_logs_handler(instance.fe_instance().clone())
694        .with_prom_handler(
695            frontend_ref.clone(),
696            Some(frontend_ref.clone()),
697            true,
698            PromValidationMode::Strict,
699            experimental_enable_prometheus_native_histogram,
700            pending_rows_batcher,
701        )
702        .with_prometheus_handler(frontend_ref)
703        .with_greptime_config_options(instance.opts.datanode_options().to_toml().unwrap())
704        .build();
705    let app = http_server.build(http_server.make_app()).unwrap();
706    (app, instance.guard)
707}
708
709pub async fn setup_grpc_server(
710    store_type: StorageType,
711    name: &str,
712) -> (GreptimeDbStandalone, Arc<GrpcServer>) {
713    setup_grpc_server_with(store_type, name, None, None, None).await
714}
715
716pub async fn setup_grpc_server_with_user_provider(
717    store_type: StorageType,
718    name: &str,
719    user_provider: Option<UserProviderRef>,
720) -> (GreptimeDbStandalone, Arc<GrpcServer>) {
721    setup_grpc_server_with(store_type, name, user_provider, None, None).await
722}
723
724/// Sets up a gRPC server backed by a standalone instance whose frontend has auto
725/// table creation disabled, for testing the server-side global switch.
726pub async fn setup_grpc_server_with_auto_create_table_disabled(
727    store_type: StorageType,
728    name: &str,
729) -> (GreptimeDbStandalone, Arc<GrpcServer>) {
730    let instance = GreptimeDbStandaloneBuilder::new(name)
731        .with_default_store_type(store_type)
732        .with_auto_create_table(false)
733        .build()
734        .await;
735    setup_grpc_server_for_instance(instance, None, None, None).await
736}
737
738pub async fn setup_grpc_server_with(
739    store_type: StorageType,
740    name: &str,
741    user_provider: Option<UserProviderRef>,
742    grpc_config: Option<GrpcServerConfig>,
743    memory_limiter: Option<servers::request_memory_limiter::ServerMemoryLimiter>,
744) -> (GreptimeDbStandalone, Arc<GrpcServer>) {
745    let instance = setup_standalone_instance(name, store_type).await;
746    setup_grpc_server_for_instance(instance, user_provider, grpc_config, memory_limiter).await
747}
748
749/// Builds and starts a gRPC server on top of an already-constructed standalone
750/// instance. This is the shared core behind the `setup_grpc_server_*` helpers.
751async fn setup_grpc_server_for_instance(
752    instance: GreptimeDbStandalone,
753    user_provider: Option<UserProviderRef>,
754    grpc_config: Option<GrpcServerConfig>,
755    memory_limiter: Option<servers::request_memory_limiter::ServerMemoryLimiter>,
756) -> (GreptimeDbStandalone, Arc<GrpcServer>) {
757    let grpc_server = setup_grpc_server_for_frontend_instance_with(
758        instance.fe_instance().clone(),
759        user_provider,
760        grpc_config,
761        memory_limiter,
762    )
763    .await;
764    (instance, grpc_server)
765}
766
767/// Builds and starts an authenticated gRPC server on an existing frontend instance.
768pub async fn setup_grpc_server_for_frontend_instance(
769    instance: Arc<Instance>,
770    user_provider: Option<UserProviderRef>,
771) -> Arc<GrpcServer> {
772    setup_grpc_server_for_frontend_instance_with(instance, user_provider, None, None).await
773}
774
775/// Starts a gRPC server backed by `instance` and returns a client authenticated
776/// as the supplied test user. The server handle keeps the listener alive.
777pub async fn setup_authenticated_grpc_database(
778    instance: Arc<Instance>,
779    username: &str,
780    password: &str,
781) -> (Database, Arc<GrpcServer>) {
782    let user_provider =
783        user_provider_from_option(&format!("static_user_provider:cmd:{username}={password}"))
784            .unwrap();
785    let grpc_server = setup_grpc_server_for_frontend_instance(instance, Some(user_provider)).await;
786    let grpc_addr = grpc_server.bind_addr().unwrap().to_string();
787    let mut database =
788        Database::new_with_dbname("greptime-public", Client::with_urls(vec![grpc_addr]));
789    database.set_auth(api::v1::auth_header::AuthScheme::Basic(Basic {
790        username: username.to_string(),
791        password: password.to_string(),
792    }));
793    (database, grpc_server)
794}
795
796async fn setup_grpc_server_for_frontend_instance_with(
797    fe_instance_ref: Arc<Instance>,
798    user_provider: Option<UserProviderRef>,
799    grpc_config: Option<GrpcServerConfig>,
800    memory_limiter: Option<servers::request_memory_limiter::ServerMemoryLimiter>,
801) -> Arc<GrpcServer> {
802    let runtime: Runtime = RuntimeBuilder::default()
803        .worker_threads(2)
804        .thread_name("grpc-handlers")
805        .build()
806        .unwrap();
807
808    let greptime_request_handler = GreptimeRequestHandler::new(
809        fe_instance_ref.clone(),
810        user_provider.clone(),
811        Some(runtime.clone()),
812        FlightCompression::default(),
813    );
814
815    let flight_handler = Arc::new(greptime_request_handler.clone());
816
817    let grpc_config = grpc_config.unwrap_or_default();
818    let mut grpc_builder = GrpcServerBuilder::new(grpc_config.clone(), runtime);
819
820    if let Some(limiter) = memory_limiter {
821        grpc_builder = grpc_builder.with_memory_limiter(limiter);
822    }
823
824    let grpc_builder = grpc_builder
825        .database_handler(greptime_request_handler)
826        .flight_handler(flight_handler)
827        .prometheus_handler(fe_instance_ref.clone(), user_provider.clone())
828        .otel_arrow_handler(OtelArrowServiceHandler::new(fe_instance_ref, user_provider))
829        .with_tls_config(grpc_config.tls)
830        .unwrap();
831
832    let mut grpc_server = grpc_builder.build();
833
834    let fe_grpc_addr = "127.0.0.1:0".parse::<SocketAddr>().unwrap();
835    grpc_server.start(fe_grpc_addr).await.unwrap();
836
837    Arc::new(grpc_server)
838}
839
840pub async fn setup_mysql_server(
841    store_type: StorageType,
842    name: &str,
843) -> (TestGuard, Arc<Box<dyn Server>>) {
844    setup_mysql_server_with_user_provider(store_type, name, None).await
845}
846
847pub async fn setup_mysql_server_with_slow_query_threshold(
848    store_type: StorageType,
849    name: &str,
850    slow_query_threshold: std::time::Duration,
851) -> (TestGuard, Arc<Box<dyn Server>>) {
852    let plugins = Plugins::new();
853    let instance = setup_standalone_instance_with_plugins_and_slow_query_threshold(
854        name,
855        store_type,
856        plugins,
857        slow_query_threshold,
858    )
859    .await;
860
861    let runtime = RuntimeBuilder::default()
862        .worker_threads(2)
863        .thread_name("mysql-runtime")
864        .build()
865        .unwrap();
866
867    let fe_mysql_addr = format!("127.0.0.1:{}", ports::get_port());
868
869    let fe_instance_ref = instance.fe_instance().clone();
870    let opts = MysqlOptions {
871        addr: fe_mysql_addr.clone(),
872        ..Default::default()
873    };
874    let mut mysql_server = MysqlServer::create_server(
875        runtime,
876        Arc::new(MysqlSpawnRef::new(fe_instance_ref, None)),
877        Arc::new(MysqlSpawnConfig::new(
878            false,
879            Arc::new(
880                ReloadableTlsServerConfig::try_new(opts.tls.clone())
881                    .expect("Failed to load certificates and keys"),
882            ),
883            0,
884            opts.reject_no_database.unwrap_or(false),
885            opts.prepared_stmt_cache_size,
886        )),
887        None,
888    );
889
890    mysql_server
891        .start(fe_mysql_addr.parse::<SocketAddr>().unwrap())
892        .await
893        .unwrap();
894
895    (instance.guard, Arc::new(mysql_server))
896}
897
898pub async fn setup_mysql_server_with_user_provider(
899    store_type: StorageType,
900    name: &str,
901    user_provider: Option<UserProviderRef>,
902) -> (TestGuard, Arc<Box<dyn Server>>) {
903    let plugins = Plugins::new();
904    if let Some(user_provider) = user_provider.clone() {
905        plugins.insert::<UserProviderRef>(user_provider.clone());
906        plugins.insert::<PermissionCheckerRef>(DefaultPermissionChecker::arc());
907    }
908
909    let instance = setup_standalone_instance_with_plugins(name, store_type, plugins).await;
910
911    let runtime = RuntimeBuilder::default()
912        .worker_threads(2)
913        .thread_name("mysql-runtime")
914        .build()
915        .unwrap();
916
917    let fe_mysql_addr = format!("127.0.0.1:{}", ports::get_port());
918
919    let fe_instance_ref = instance.fe_instance().clone();
920    let opts = MysqlOptions {
921        addr: fe_mysql_addr.clone(),
922        ..Default::default()
923    };
924    let mut mysql_server = MysqlServer::create_server(
925        runtime,
926        Arc::new(MysqlSpawnRef::new(fe_instance_ref, user_provider)),
927        Arc::new(MysqlSpawnConfig::new(
928            false,
929            Arc::new(
930                ReloadableTlsServerConfig::try_new(opts.tls.clone())
931                    .expect("Failed to load certificates and keys"),
932            ),
933            0,
934            opts.reject_no_database.unwrap_or(false),
935            opts.prepared_stmt_cache_size,
936        )),
937        None,
938    );
939
940    mysql_server
941        .start(fe_mysql_addr.parse::<SocketAddr>().unwrap())
942        .await
943        .unwrap();
944
945    (instance.guard, Arc::new(mysql_server))
946}
947
948pub async fn setup_pg_server(
949    store_type: StorageType,
950    name: &str,
951) -> (TestGuard, Arc<Box<dyn Server>>) {
952    setup_pg_server_with_user_provider(store_type, name, None).await
953}
954
955pub async fn setup_pg_server_with_slow_query_threshold(
956    store_type: StorageType,
957    name: &str,
958    slow_query_threshold: std::time::Duration,
959) -> (TestGuard, Arc<Box<dyn Server>>) {
960    let instance =
961        setup_standalone_instance_with_slow_query_threshold(name, store_type, slow_query_threshold)
962            .await;
963
964    let runtime = RuntimeBuilder::default()
965        .worker_threads(2)
966        .thread_name("pg-runtime")
967        .build()
968        .unwrap();
969
970    let fe_pg_addr = format!("127.0.0.1:{}", ports::get_port());
971
972    let fe_instance_ref = instance.fe_instance().clone();
973    let opts = PostgresOptions {
974        addr: fe_pg_addr.clone(),
975        ..Default::default()
976    };
977    let tls_server_config = Arc::new(
978        ReloadableTlsServerConfig::try_new(opts.tls.clone())
979            .expect("Failed to load certificates and keys"),
980    );
981
982    let mut pg_server = Box::new(PostgresServer::new(
983        fe_instance_ref,
984        opts.tls.should_force_tls(),
985        tls_server_config,
986        0,
987        runtime,
988        None,
989        None,
990    ));
991
992    pg_server
993        .start(fe_pg_addr.parse::<SocketAddr>().unwrap())
994        .await
995        .unwrap();
996
997    (instance.guard, Arc::new(pg_server))
998}
999
1000pub async fn setup_pg_server_with_user_provider(
1001    store_type: StorageType,
1002    name: &str,
1003    user_provider: Option<UserProviderRef>,
1004) -> (TestGuard, Arc<Box<dyn Server>>) {
1005    let instance = setup_standalone_instance(name, store_type).await;
1006
1007    let runtime = RuntimeBuilder::default()
1008        .worker_threads(2)
1009        .thread_name("pg-runtime")
1010        .build()
1011        .unwrap();
1012
1013    let fe_pg_addr = format!("127.0.0.1:{}", ports::get_port());
1014
1015    let fe_instance_ref = instance.fe_instance().clone();
1016    let opts = PostgresOptions {
1017        addr: fe_pg_addr.clone(),
1018        ..Default::default()
1019    };
1020    let tls_server_config = Arc::new(
1021        ReloadableTlsServerConfig::try_new(opts.tls.clone())
1022            .expect("Failed to load certificates and keys"),
1023    );
1024
1025    let mut pg_server = Box::new(PostgresServer::new(
1026        fe_instance_ref,
1027        opts.tls.should_force_tls(),
1028        tls_server_config,
1029        0,
1030        runtime,
1031        user_provider,
1032        None,
1033    ));
1034
1035    pg_server
1036        .start(fe_pg_addr.parse::<SocketAddr>().unwrap())
1037        .await
1038        .unwrap();
1039
1040    (instance.guard, Arc::new(pg_server))
1041}
1042
1043pub(crate) async fn prepare_another_catalog_and_schema(instance: &Instance) {
1044    let catalog_manager = instance
1045        .catalog_manager()
1046        .as_any()
1047        .downcast_ref::<KvBackendCatalogManager>()
1048        .unwrap();
1049
1050    let table_metadata_manager = catalog_manager.table_metadata_manager_ref();
1051    prepare_another_catalog_and_schema_with_manager(table_metadata_manager).await;
1052}
1053
1054pub(crate) async fn prepare_another_catalog_and_schema_with_kv_backend(kv_backend: KvBackendRef) {
1055    let table_metadata_manager = TableMetadataManager::new(kv_backend);
1056    prepare_another_catalog_and_schema_with_manager(&table_metadata_manager).await;
1057}
1058
1059async fn prepare_another_catalog_and_schema_with_manager(
1060    table_metadata_manager: &TableMetadataManager,
1061) {
1062    table_metadata_manager
1063        .catalog_manager()
1064        .create(CatalogNameKey::new("another_catalog"), true)
1065        .await
1066        .unwrap();
1067    table_metadata_manager
1068        .schema_manager()
1069        .create(
1070            SchemaNameKey::new("another_catalog", "another_schema"),
1071            None,
1072            true,
1073        )
1074        .await
1075        .unwrap();
1076}
1077
1078pub async fn execute_sql(instance: &Arc<Instance>, sql: &str) -> Output {
1079    SqlQueryHandler::do_query(instance.as_ref(), sql, QueryContext::arc())
1080        .await
1081        .remove(0)
1082        .unwrap()
1083}
1084
1085pub async fn try_execute_sql(
1086    instance: &Arc<Instance>,
1087    sql: &str,
1088) -> servers::error::Result<Output> {
1089    SqlQueryHandler::do_query(instance.as_ref(), sql, QueryContext::arc())
1090        .await
1091        .remove(0)
1092}
1093
1094pub async fn execute_sql_and_expect(instance: &Arc<Instance>, sql: &str, expected: &str) {
1095    let output = execute_sql(instance, sql).await;
1096    let output = output.data.pretty_print().await;
1097    assert_eq!(output, expected.trim());
1098}