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