1use std::ops::Deref;
16use std::path::PathBuf;
17use std::sync::Arc;
18use std::sync::atomic::AtomicU32;
19
20use anyhow::Context;
21use risingwave_common::config::{
22 CompactionConfig, DefaultParallelism, ObjectStoreConfig, RpcClientConfig,
23};
24use risingwave_common::session_config::SessionConfig;
25use risingwave_common::system_param::reader::SystemParamsReader;
26use risingwave_common::{bail, system_param};
27use risingwave_meta_model::prelude::Cluster;
28use risingwave_pb::meta::SystemParams;
29use risingwave_rpc_client::{
30 FrontendClientPool, FrontendClientPoolRef, StreamClientPool, StreamClientPoolRef,
31};
32use risingwave_sqlparser::ast::RedactSqlOptionKeywordsRef;
33use sea_orm::EntityTrait;
34
35use crate::MetaResult;
36use crate::barrier::SharedActorInfos;
37use crate::controller::SqlMetaStore;
38use crate::controller::id::{
39 IdGeneratorManager as SqlIdGeneratorManager, IdGeneratorManagerRef as SqlIdGeneratorManagerRef,
40};
41use crate::controller::session_params::{SessionParamsController, SessionParamsControllerRef};
42use crate::controller::system_param::{SystemParamsController, SystemParamsControllerRef};
43use crate::hummock::sequence::SequenceGenerator;
44use crate::manager::event_log::{EventLogManagerRef, start_event_log_manager};
45use crate::manager::{IdleManager, IdleManagerRef, NotificationManager, NotificationManagerRef};
46use crate::model::ClusterId;
47
48#[derive(Clone)]
51pub struct MetaSrvEnv {
52 id_gen_manager_impl: SqlIdGeneratorManagerRef,
54
55 system_param_manager_impl: SystemParamsControllerRef,
57
58 session_param_manager_impl: SessionParamsControllerRef,
60
61 meta_store_impl: SqlMetaStore,
63
64 notification_manager: NotificationManagerRef,
66
67 pub shared_actor_info: SharedActorInfos,
68
69 stream_client_pool: StreamClientPoolRef,
71
72 frontend_client_pool: FrontendClientPoolRef,
74
75 idle_manager: IdleManagerRef,
77
78 event_log_manager: EventLogManagerRef,
79
80 cluster_id: ClusterId,
82
83 pub hummock_seq: Arc<SequenceGenerator>,
84
85 await_tree_reg: await_tree::Registry,
87
88 pub opts: Arc<MetaOpts>,
90
91 actor_id_generator: Arc<AtomicU32>,
92}
93
94#[derive(Clone, serde::Serialize)]
96pub struct MetaOpts {
97 pub enable_recovery: bool,
100 pub disable_automatic_parallelism_control: bool,
102 pub parallelism_control_batch_size: usize,
104 pub parallelism_control_trigger_period_sec: u64,
106 pub parallelism_control_trigger_first_delay_sec: u64,
108 pub in_flight_barrier_nums: usize,
110 pub max_idle_ms: u64,
113 pub compaction_deterministic_test: bool,
115 pub default_parallelism: DefaultParallelism,
117
118 pub vacuum_interval_sec: u64,
121 pub vacuum_spin_interval_ms: u64,
124 pub iceberg_gc_interval_sec: u64,
126 pub time_travel_vacuum_interval_sec: u64,
127 pub time_travel_vacuum_max_version_count: Option<u32>,
128 pub hummock_version_checkpoint_interval_sec: u64,
130 pub enable_hummock_data_archive: bool,
131 pub checkpoint_compression_algorithm: risingwave_common::config::CheckpointCompression,
133 pub checkpoint_read_chunk_size: usize,
135 pub checkpoint_read_max_in_flight_chunks: usize,
137 pub hummock_time_travel_snapshot_interval: u64,
138 pub hummock_time_travel_sst_info_fetch_batch_size: usize,
139 pub hummock_time_travel_sst_info_insert_batch_size: usize,
140 pub hummock_time_travel_epoch_version_insert_batch_size: usize,
141 pub hummock_gc_history_insert_batch_size: usize,
142 pub hummock_time_travel_filter_out_objects_batch_size: usize,
143 pub hummock_time_travel_filter_out_objects_v1: bool,
144 pub hummock_time_travel_filter_out_objects_list_version_batch_size: usize,
145 pub hummock_time_travel_filter_out_objects_list_delta_batch_size: usize,
146 pub min_delta_log_num_for_hummock_version_checkpoint: u64,
151 pub min_sst_retention_time_sec: u64,
154 pub full_gc_interval_sec: u64,
156 pub full_gc_object_limit: u64,
158 pub gc_history_retention_time_sec: u64,
160 pub max_inflight_time_travel_query: u64,
162 pub enable_committed_sst_sanity_check: bool,
164 pub periodic_compaction_interval_sec: u64,
166 pub node_num_monitor_interval_sec: u64,
168 pub protect_drop_table_with_incoming_sink: bool,
170 pub prometheus_endpoint: Option<String>,
176
177 pub prometheus_selector: Option<String>,
179
180 pub vpc_id: Option<String>,
182
183 pub security_group_id: Option<String>,
185
186 pub privatelink_endpoint_default_tags: Option<Vec<(String, String)>>,
190
191 pub periodic_space_reclaim_compaction_interval_sec: u64,
193
194 pub telemetry_enabled: bool,
196 pub periodic_ttl_reclaim_compaction_interval_sec: u64,
198
199 pub periodic_tombstone_reclaim_compaction_interval_sec: u64,
201
202 pub periodic_scheduling_compaction_group_split_interval_sec: u64,
204 pub enable_compaction_group_normalize: bool,
206 pub max_normalize_splits_per_round: u64,
208
209 pub do_not_config_object_storage_lifecycle: bool,
211
212 pub partition_vnode_count: u32,
213
214 pub table_high_write_throughput_threshold: u64,
216 pub table_low_write_throughput_threshold: u64,
218
219 pub compaction_task_max_heartbeat_interval_secs: u64,
220 pub compaction_task_max_progress_interval_secs: u64,
221 pub compaction_task_id_refill_capacity: u32,
222 pub compaction_config: Option<CompactionConfig>,
223
224 pub hybrid_partition_node_count: u32,
232
233 pub event_log_enabled: bool,
234 pub event_log_channel_max_size: u32,
235 pub advertise_addr: String,
236 pub cached_traces_num: u32,
239 pub cached_traces_memory_limit_bytes: usize,
242
243 pub enable_trivial_move: bool,
245
246 pub enable_check_task_level_overlap: bool,
248 pub enable_dropped_column_reclaim: bool,
249
250 pub split_group_size_ratio: f64,
252
253 pub refresh_scheduler_interval_sec: u64,
255
256 pub table_stat_high_write_throughput_ratio_for_split: f64,
258
259 pub table_stat_low_write_throughput_ratio_for_merge: f64,
261
262 pub table_stat_throuput_window_seconds_for_split: usize,
264
265 pub table_stat_throuput_window_seconds_for_merge: usize,
267
268 pub object_store_config: ObjectStoreConfig,
270
271 pub max_trivial_move_task_count_per_loop: usize,
273
274 pub max_get_task_probe_times: usize,
276
277 pub compact_task_table_size_partition_threshold_low: u64,
278 pub compact_task_table_size_partition_threshold_high: u64,
279
280 pub periodic_scheduling_compaction_group_merge_interval_sec: u64,
281
282 pub compaction_group_merge_dimension_threshold: f64,
283
284 pub secret_store_private_key: Option<Vec<u8>>,
286 pub temp_secret_file_dir: String,
288
289 pub actor_cnt_per_worker_parallelism_hard_limit: usize,
291 pub actor_cnt_per_worker_parallelism_soft_limit: usize,
292
293 pub table_change_log_insert_batch_size: u64,
294 pub table_change_log_delete_batch_size: u64,
295
296 pub license_key_path: Option<PathBuf>,
297
298 pub compute_client_config: RpcClientConfig,
299 pub stream_client_config: RpcClientConfig,
300 pub frontend_client_config: RpcClientConfig,
301 pub redact_sql_option_keywords: RedactSqlOptionKeywordsRef,
302
303 pub cdc_table_split_init_sleep_interval_splits: u64,
304 pub cdc_table_split_init_sleep_duration_millis: u64,
305 pub cdc_table_split_init_insert_batch_size: u64,
306
307 pub enable_legacy_table_migration: bool,
308 pub pause_on_next_bootstrap_offline: bool,
309}
310
311impl MetaOpts {
312 pub fn test(enable_recovery: bool) -> Self {
314 Self {
315 enable_recovery,
316 disable_automatic_parallelism_control: false,
317 parallelism_control_batch_size: 1,
318 parallelism_control_trigger_period_sec: 10,
319 parallelism_control_trigger_first_delay_sec: 30,
320 in_flight_barrier_nums: 40,
321 max_idle_ms: 0,
322 compaction_deterministic_test: false,
323 default_parallelism: DefaultParallelism::Full,
324 vacuum_interval_sec: 30,
325 time_travel_vacuum_interval_sec: 30,
326 time_travel_vacuum_max_version_count: None,
327 vacuum_spin_interval_ms: 0,
328 iceberg_gc_interval_sec: 3600,
329 hummock_version_checkpoint_interval_sec: 30,
330 enable_hummock_data_archive: false,
331 checkpoint_compression_algorithm:
332 risingwave_common::config::CheckpointCompression::Zstd,
333 checkpoint_read_chunk_size: 128 * 1024 * 1024,
334 checkpoint_read_max_in_flight_chunks: 4,
335 hummock_time_travel_snapshot_interval: 0,
336 hummock_time_travel_sst_info_fetch_batch_size: 10_000,
337 hummock_time_travel_sst_info_insert_batch_size: 10,
338 hummock_time_travel_epoch_version_insert_batch_size: 1000,
339 hummock_gc_history_insert_batch_size: 1000,
340 hummock_time_travel_filter_out_objects_batch_size: 1000,
341 hummock_time_travel_filter_out_objects_v1: false,
342 hummock_time_travel_filter_out_objects_list_version_batch_size: 10,
343 hummock_time_travel_filter_out_objects_list_delta_batch_size: 1000,
344 min_delta_log_num_for_hummock_version_checkpoint: 1,
345 min_sst_retention_time_sec: 3600 * 24 * 7,
346 full_gc_interval_sec: 3600 * 24 * 7,
347 full_gc_object_limit: 100_000,
348 gc_history_retention_time_sec: 3600 * 24 * 7,
349 max_inflight_time_travel_query: 1000,
350 enable_committed_sst_sanity_check: false,
351 periodic_compaction_interval_sec: 300,
352 node_num_monitor_interval_sec: 10,
353 protect_drop_table_with_incoming_sink: false,
354 prometheus_endpoint: None,
355 prometheus_selector: None,
356 vpc_id: None,
357 security_group_id: None,
358 privatelink_endpoint_default_tags: None,
359 periodic_space_reclaim_compaction_interval_sec: 60,
360 telemetry_enabled: false,
361 periodic_ttl_reclaim_compaction_interval_sec: 60,
362 periodic_tombstone_reclaim_compaction_interval_sec: 60,
363 periodic_scheduling_compaction_group_split_interval_sec: 60,
364 enable_compaction_group_normalize: false,
365 max_normalize_splits_per_round: 4,
366 compact_task_table_size_partition_threshold_low: 128 * 1024 * 1024,
367 compact_task_table_size_partition_threshold_high: 512 * 1024 * 1024,
368 table_high_write_throughput_threshold: 128 * 1024 * 1024,
369 table_low_write_throughput_threshold: 64 * 1024 * 1024,
370 do_not_config_object_storage_lifecycle: true,
371 partition_vnode_count: 32,
372 compaction_task_max_heartbeat_interval_secs: 0,
373 compaction_task_max_progress_interval_secs: 1,
374 compaction_task_id_refill_capacity: 64,
375 compaction_config: None,
376 hybrid_partition_node_count: 4,
377 event_log_enabled: false,
378 event_log_channel_max_size: 1,
379 advertise_addr: "".to_owned(),
380 cached_traces_num: 1,
381 cached_traces_memory_limit_bytes: usize::MAX,
382 enable_trivial_move: true,
383 enable_check_task_level_overlap: true,
384 enable_dropped_column_reclaim: false,
385 object_store_config: ObjectStoreConfig::default(),
386 max_trivial_move_task_count_per_loop: 256,
387 max_get_task_probe_times: 5,
388 secret_store_private_key: Some(
389 hex::decode("0123456789abcdef0123456789abcdef").unwrap(),
390 ),
391 temp_secret_file_dir: "./secrets".to_owned(),
392 actor_cnt_per_worker_parallelism_hard_limit: usize::MAX,
393 actor_cnt_per_worker_parallelism_soft_limit: usize::MAX,
394 split_group_size_ratio: 0.9,
395 table_stat_high_write_throughput_ratio_for_split: 0.5,
396 table_stat_low_write_throughput_ratio_for_merge: 0.7,
397 table_stat_throuput_window_seconds_for_split: 60,
398 table_stat_throuput_window_seconds_for_merge: 240,
399 periodic_scheduling_compaction_group_merge_interval_sec: 60 * 10,
400 compaction_group_merge_dimension_threshold: 1.2,
401 license_key_path: None,
402 compute_client_config: RpcClientConfig::default(),
403 stream_client_config: RpcClientConfig::default(),
404 frontend_client_config: RpcClientConfig::default(),
405 redact_sql_option_keywords: Arc::new(Default::default()),
406 cdc_table_split_init_sleep_interval_splits: 1000,
407 cdc_table_split_init_sleep_duration_millis: 10,
408 cdc_table_split_init_insert_batch_size: 1000,
409 enable_legacy_table_migration: true,
410 refresh_scheduler_interval_sec: 60,
411 pause_on_next_bootstrap_offline: false,
412 table_change_log_insert_batch_size: 1000,
413 table_change_log_delete_batch_size: 1000,
414 }
415 }
416}
417
418impl MetaSrvEnv {
419 pub async fn new(
420 opts: MetaOpts,
421 mut init_system_params: SystemParams,
422 init_session_config: SessionConfig,
423 meta_store_impl: SqlMetaStore,
424 ) -> MetaResult<Self> {
425 let idle_manager = Arc::new(IdleManager::new(opts.max_idle_ms));
426 let stream_client_pool =
427 Arc::new(StreamClientPool::new(1, opts.stream_client_config.clone())); let frontend_client_pool = Arc::new(FrontendClientPool::new(
429 1,
430 opts.frontend_client_config.clone(),
431 ));
432 let event_log_manager = Arc::new(start_event_log_manager(
433 opts.event_log_enabled,
434 opts.event_log_channel_max_size,
435 ));
436
437 if opts.license_key_path.is_some()
440 && init_system_params.license_key
441 != system_param::default::license_key_opt().map(Into::into)
442 {
443 bail!(
444 "argument `--license-key-path` (or env var `RW_LICENSE_KEY_PATH`) and \
445 system parameter `license_key` (or env var `RW_LICENSE_KEY`) may not \
446 be set at the same time"
447 );
448 }
449
450 let cluster_first_launch = meta_store_impl.up().await.context(
451 "Failed to initialize the meta store, \
452 this may happen if there's existing metadata incompatible with the current version of RisingWave, \
453 e.g., downgrading from a newer release or a nightly build to an older one. \
454 For a single-node deployment, you may want to reset all data by deleting the data directory, \
455 typically located at `~/.risingwave`.",
456 )?;
457
458 let notification_manager =
459 Arc::new(NotificationManager::new(meta_store_impl.clone()).await);
460 let cluster_id = Cluster::find()
461 .one(&meta_store_impl.conn)
462 .await?
463 .map(|c| c.cluster_id.to_string().into())
464 .unwrap();
465
466 init_system_params.use_new_object_prefix_strategy = Some(cluster_first_launch);
472
473 let system_param_controller = Arc::new(
474 SystemParamsController::new(
475 meta_store_impl.clone(),
476 notification_manager.clone(),
477 init_system_params,
478 )
479 .await?,
480 );
481 let session_param_controller = Arc::new(
482 SessionParamsController::new(
483 meta_store_impl.clone(),
484 notification_manager.clone(),
485 init_session_config,
486 )
487 .await?,
488 );
489 Ok(Self {
490 id_gen_manager_impl: Arc::new(SqlIdGeneratorManager::new(&meta_store_impl.conn).await?),
491 system_param_manager_impl: system_param_controller,
492 session_param_manager_impl: session_param_controller,
493 meta_store_impl: meta_store_impl.clone(),
494 shared_actor_info: SharedActorInfos::new(notification_manager.clone()),
495 notification_manager,
496 stream_client_pool,
497 frontend_client_pool,
498 idle_manager,
499 event_log_manager,
500 cluster_id,
501 hummock_seq: Arc::new(SequenceGenerator::new(meta_store_impl.conn.clone())),
502 opts: opts.into(),
503 await_tree_reg: await_tree::Registry::new(Default::default()),
505 actor_id_generator: Arc::new(AtomicU32::new(0)),
506 })
507 }
508
509 pub fn meta_store(&self) -> SqlMetaStore {
510 self.meta_store_impl.clone()
511 }
512
513 pub fn meta_store_ref(&self) -> &SqlMetaStore {
514 &self.meta_store_impl
515 }
516
517 pub fn id_gen_manager(&self) -> &SqlIdGeneratorManagerRef {
518 &self.id_gen_manager_impl
519 }
520
521 pub fn notification_manager_ref(&self) -> NotificationManagerRef {
522 self.notification_manager.clone()
523 }
524
525 pub fn notification_manager(&self) -> &NotificationManager {
526 self.notification_manager.deref()
527 }
528
529 pub fn idle_manager_ref(&self) -> IdleManagerRef {
530 self.idle_manager.clone()
531 }
532
533 pub fn idle_manager(&self) -> &IdleManager {
534 self.idle_manager.deref()
535 }
536
537 pub fn actor_id_generator(&self) -> &AtomicU32 {
538 self.actor_id_generator.deref()
539 }
540
541 pub async fn system_params_reader(&self) -> SystemParamsReader {
542 self.system_param_manager_impl.get_params().await
543 }
544
545 pub fn system_params_manager_impl_ref(&self) -> SystemParamsControllerRef {
546 self.system_param_manager_impl.clone()
547 }
548
549 pub fn session_params_manager_impl_ref(&self) -> SessionParamsControllerRef {
550 self.session_param_manager_impl.clone()
551 }
552
553 pub fn stream_client_pool_ref(&self) -> StreamClientPoolRef {
554 self.stream_client_pool.clone()
555 }
556
557 pub fn stream_client_pool(&self) -> &StreamClientPool {
558 self.stream_client_pool.deref()
559 }
560
561 pub fn frontend_client_pool(&self) -> &FrontendClientPool {
562 self.frontend_client_pool.deref()
563 }
564
565 pub fn cluster_id(&self) -> &ClusterId {
566 &self.cluster_id
567 }
568
569 pub fn event_log_manager_ref(&self) -> EventLogManagerRef {
570 self.event_log_manager.clone()
571 }
572
573 pub fn await_tree_reg(&self) -> &await_tree::Registry {
574 &self.await_tree_reg
575 }
576
577 pub fn shared_actor_infos(&self) -> &SharedActorInfos {
578 &self.shared_actor_info
579 }
580}
581
582#[cfg(any(test, feature = "test"))]
583impl MetaSrvEnv {
584 pub async fn for_test() -> Self {
586 Self::for_test_opts(MetaOpts::test(false), |_| ()).await
587 }
588
589 pub async fn for_test_opts(
590 opts: MetaOpts,
591 on_test_system_params: impl FnOnce(&mut risingwave_pb::meta::PbSystemParams),
592 ) -> Self {
593 let mut system_params = risingwave_common::system_param::system_params_for_test();
594 on_test_system_params(&mut system_params);
595 Self::new(
596 opts,
597 system_params,
598 Default::default(),
599 SqlMetaStore::for_test().await,
600 )
601 .await
602 .unwrap()
603 }
604}