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 hummock_time_travel_snapshot_interval: u64,
132 pub hummock_time_travel_sst_info_fetch_batch_size: usize,
133 pub hummock_time_travel_sst_info_insert_batch_size: usize,
134 pub hummock_time_travel_epoch_version_insert_batch_size: usize,
135 pub hummock_gc_history_insert_batch_size: usize,
136 pub hummock_time_travel_filter_out_objects_batch_size: usize,
137 pub hummock_time_travel_filter_out_objects_v1: bool,
138 pub hummock_time_travel_filter_out_objects_list_version_batch_size: usize,
139 pub hummock_time_travel_filter_out_objects_list_delta_batch_size: usize,
140 pub min_delta_log_num_for_hummock_version_checkpoint: u64,
145 pub min_sst_retention_time_sec: u64,
148 pub full_gc_interval_sec: u64,
150 pub full_gc_object_limit: u64,
152 pub gc_history_retention_time_sec: u64,
154 pub max_inflight_time_travel_query: u64,
156 pub enable_committed_sst_sanity_check: bool,
158 pub periodic_compaction_interval_sec: u64,
160 pub node_num_monitor_interval_sec: u64,
162 pub protect_drop_table_with_incoming_sink: bool,
164 pub prometheus_endpoint: Option<String>,
170
171 pub prometheus_selector: Option<String>,
173
174 pub vpc_id: Option<String>,
176
177 pub security_group_id: Option<String>,
179
180 pub privatelink_endpoint_default_tags: Option<Vec<(String, String)>>,
184
185 pub periodic_space_reclaim_compaction_interval_sec: u64,
187
188 pub telemetry_enabled: bool,
190 pub periodic_ttl_reclaim_compaction_interval_sec: u64,
192
193 pub periodic_tombstone_reclaim_compaction_interval_sec: u64,
195
196 pub periodic_scheduling_compaction_group_split_interval_sec: u64,
198
199 pub do_not_config_object_storage_lifecycle: bool,
201
202 pub partition_vnode_count: u32,
203
204 pub table_high_write_throughput_threshold: u64,
206 pub table_low_write_throughput_threshold: u64,
208
209 pub compaction_task_max_heartbeat_interval_secs: u64,
210 pub compaction_task_max_progress_interval_secs: u64,
211 pub compaction_config: Option<CompactionConfig>,
212
213 pub hybrid_partition_node_count: u32,
221
222 pub event_log_enabled: bool,
223 pub event_log_channel_max_size: u32,
224 pub advertise_addr: String,
225 pub cached_traces_num: u32,
228 pub cached_traces_memory_limit_bytes: usize,
231
232 pub enable_trivial_move: bool,
234
235 pub enable_check_task_level_overlap: bool,
237 pub enable_dropped_column_reclaim: bool,
238
239 pub split_group_size_ratio: f64,
241
242 pub refresh_scheduler_interval_sec: u64,
244
245 pub table_stat_high_write_throughput_ratio_for_split: f64,
247
248 pub table_stat_low_write_throughput_ratio_for_merge: f64,
250
251 pub table_stat_throuput_window_seconds_for_split: usize,
253
254 pub table_stat_throuput_window_seconds_for_merge: usize,
256
257 pub object_store_config: ObjectStoreConfig,
259
260 pub max_trivial_move_task_count_per_loop: usize,
262
263 pub max_get_task_probe_times: usize,
265
266 pub compact_task_table_size_partition_threshold_low: u64,
267 pub compact_task_table_size_partition_threshold_high: u64,
268
269 pub periodic_scheduling_compaction_group_merge_interval_sec: u64,
270
271 pub compaction_group_merge_dimension_threshold: f64,
272
273 pub secret_store_private_key: Option<Vec<u8>>,
275 pub temp_secret_file_dir: String,
277
278 pub actor_cnt_per_worker_parallelism_hard_limit: usize,
280 pub actor_cnt_per_worker_parallelism_soft_limit: usize,
281
282 pub license_key_path: Option<PathBuf>,
283
284 pub compute_client_config: RpcClientConfig,
285 pub stream_client_config: RpcClientConfig,
286 pub frontend_client_config: RpcClientConfig,
287 pub redact_sql_option_keywords: RedactSqlOptionKeywordsRef,
288
289 pub cdc_table_split_init_sleep_interval_splits: u64,
290 pub cdc_table_split_init_sleep_duration_millis: u64,
291 pub cdc_table_split_init_insert_batch_size: u64,
292
293 pub enable_legacy_table_migration: bool,
294 pub pause_on_next_bootstrap_offline: bool,
295}
296
297impl MetaOpts {
298 pub fn test(enable_recovery: bool) -> Self {
300 Self {
301 enable_recovery,
302 disable_automatic_parallelism_control: false,
303 parallelism_control_batch_size: 1,
304 parallelism_control_trigger_period_sec: 10,
305 parallelism_control_trigger_first_delay_sec: 30,
306 in_flight_barrier_nums: 40,
307 max_idle_ms: 0,
308 compaction_deterministic_test: false,
309 default_parallelism: DefaultParallelism::Full,
310 vacuum_interval_sec: 30,
311 time_travel_vacuum_interval_sec: 30,
312 time_travel_vacuum_max_version_count: None,
313 vacuum_spin_interval_ms: 0,
314 iceberg_gc_interval_sec: 3600,
315 hummock_version_checkpoint_interval_sec: 30,
316 enable_hummock_data_archive: false,
317 hummock_time_travel_snapshot_interval: 0,
318 hummock_time_travel_sst_info_fetch_batch_size: 10_000,
319 hummock_time_travel_sst_info_insert_batch_size: 10,
320 hummock_time_travel_epoch_version_insert_batch_size: 1000,
321 hummock_gc_history_insert_batch_size: 1000,
322 hummock_time_travel_filter_out_objects_batch_size: 1000,
323 hummock_time_travel_filter_out_objects_v1: false,
324 hummock_time_travel_filter_out_objects_list_version_batch_size: 10,
325 hummock_time_travel_filter_out_objects_list_delta_batch_size: 1000,
326 min_delta_log_num_for_hummock_version_checkpoint: 1,
327 min_sst_retention_time_sec: 3600 * 24 * 7,
328 full_gc_interval_sec: 3600 * 24 * 7,
329 full_gc_object_limit: 100_000,
330 gc_history_retention_time_sec: 3600 * 24 * 7,
331 max_inflight_time_travel_query: 1000,
332 enable_committed_sst_sanity_check: false,
333 periodic_compaction_interval_sec: 60,
334 node_num_monitor_interval_sec: 10,
335 protect_drop_table_with_incoming_sink: false,
336 prometheus_endpoint: None,
337 prometheus_selector: None,
338 vpc_id: None,
339 security_group_id: None,
340 privatelink_endpoint_default_tags: None,
341 periodic_space_reclaim_compaction_interval_sec: 60,
342 telemetry_enabled: false,
343 periodic_ttl_reclaim_compaction_interval_sec: 60,
344 periodic_tombstone_reclaim_compaction_interval_sec: 60,
345 periodic_scheduling_compaction_group_split_interval_sec: 60,
346 compact_task_table_size_partition_threshold_low: 128 * 1024 * 1024,
347 compact_task_table_size_partition_threshold_high: 512 * 1024 * 1024,
348 table_high_write_throughput_threshold: 128 * 1024 * 1024,
349 table_low_write_throughput_threshold: 64 * 1024 * 1024,
350 do_not_config_object_storage_lifecycle: true,
351 partition_vnode_count: 32,
352 compaction_task_max_heartbeat_interval_secs: 0,
353 compaction_task_max_progress_interval_secs: 1,
354 compaction_config: None,
355 hybrid_partition_node_count: 4,
356 event_log_enabled: false,
357 event_log_channel_max_size: 1,
358 advertise_addr: "".to_owned(),
359 cached_traces_num: 1,
360 cached_traces_memory_limit_bytes: usize::MAX,
361 enable_trivial_move: true,
362 enable_check_task_level_overlap: true,
363 enable_dropped_column_reclaim: false,
364 object_store_config: ObjectStoreConfig::default(),
365 max_trivial_move_task_count_per_loop: 256,
366 max_get_task_probe_times: 5,
367 secret_store_private_key: Some(
368 hex::decode("0123456789abcdef0123456789abcdef").unwrap(),
369 ),
370 temp_secret_file_dir: "./secrets".to_owned(),
371 actor_cnt_per_worker_parallelism_hard_limit: usize::MAX,
372 actor_cnt_per_worker_parallelism_soft_limit: usize::MAX,
373 split_group_size_ratio: 0.9,
374 table_stat_high_write_throughput_ratio_for_split: 0.5,
375 table_stat_low_write_throughput_ratio_for_merge: 0.7,
376 table_stat_throuput_window_seconds_for_split: 60,
377 table_stat_throuput_window_seconds_for_merge: 240,
378 periodic_scheduling_compaction_group_merge_interval_sec: 60 * 10,
379 compaction_group_merge_dimension_threshold: 1.2,
380 license_key_path: None,
381 compute_client_config: RpcClientConfig::default(),
382 stream_client_config: RpcClientConfig::default(),
383 frontend_client_config: RpcClientConfig::default(),
384 redact_sql_option_keywords: Arc::new(Default::default()),
385 cdc_table_split_init_sleep_interval_splits: 1000,
386 cdc_table_split_init_sleep_duration_millis: 10,
387 cdc_table_split_init_insert_batch_size: 1000,
388 enable_legacy_table_migration: true,
389 refresh_scheduler_interval_sec: 60,
390 pause_on_next_bootstrap_offline: false,
391 }
392 }
393}
394
395impl MetaSrvEnv {
396 pub async fn new(
397 opts: MetaOpts,
398 mut init_system_params: SystemParams,
399 init_session_config: SessionConfig,
400 meta_store_impl: SqlMetaStore,
401 ) -> MetaResult<Self> {
402 let idle_manager = Arc::new(IdleManager::new(opts.max_idle_ms));
403 let stream_client_pool =
404 Arc::new(StreamClientPool::new(1, opts.stream_client_config.clone())); let frontend_client_pool = Arc::new(FrontendClientPool::new(
406 1,
407 opts.frontend_client_config.clone(),
408 ));
409 let event_log_manager = Arc::new(start_event_log_manager(
410 opts.event_log_enabled,
411 opts.event_log_channel_max_size,
412 ));
413
414 if opts.license_key_path.is_some()
417 && init_system_params.license_key
418 != system_param::default::license_key_opt().map(Into::into)
419 {
420 bail!(
421 "argument `--license-key-path` (or env var `RW_LICENSE_KEY_PATH`) and \
422 system parameter `license_key` (or env var `RW_LICENSE_KEY`) may not \
423 be set at the same time"
424 );
425 }
426
427 let cluster_first_launch = meta_store_impl.up().await.context(
428 "Failed to initialize the meta store, \
429 this may happen if there's existing metadata incompatible with the current version of RisingWave, \
430 e.g., downgrading from a newer release or a nightly build to an older one. \
431 For a single-node deployment, you may want to reset all data by deleting the data directory, \
432 typically located at `~/.risingwave`.",
433 )?;
434
435 let notification_manager =
436 Arc::new(NotificationManager::new(meta_store_impl.clone()).await);
437 let cluster_id = Cluster::find()
438 .one(&meta_store_impl.conn)
439 .await?
440 .map(|c| c.cluster_id.to_string().into())
441 .unwrap();
442
443 init_system_params.use_new_object_prefix_strategy = Some(cluster_first_launch);
449
450 let system_param_controller = Arc::new(
451 SystemParamsController::new(
452 meta_store_impl.clone(),
453 notification_manager.clone(),
454 init_system_params,
455 )
456 .await?,
457 );
458 let session_param_controller = Arc::new(
459 SessionParamsController::new(
460 meta_store_impl.clone(),
461 notification_manager.clone(),
462 init_session_config,
463 )
464 .await?,
465 );
466 Ok(Self {
467 id_gen_manager_impl: Arc::new(SqlIdGeneratorManager::new(&meta_store_impl.conn).await?),
468 system_param_manager_impl: system_param_controller,
469 session_param_manager_impl: session_param_controller,
470 meta_store_impl: meta_store_impl.clone(),
471 shared_actor_info: SharedActorInfos::new(notification_manager.clone()),
472 notification_manager,
473 stream_client_pool,
474 frontend_client_pool,
475 idle_manager,
476 event_log_manager,
477 cluster_id,
478 hummock_seq: Arc::new(SequenceGenerator::new(meta_store_impl.conn.clone())),
479 opts: opts.into(),
480 await_tree_reg: await_tree::Registry::new(Default::default()),
482 actor_id_generator: Arc::new(AtomicU32::new(0)),
483 })
484 }
485
486 pub fn meta_store(&self) -> SqlMetaStore {
487 self.meta_store_impl.clone()
488 }
489
490 pub fn meta_store_ref(&self) -> &SqlMetaStore {
491 &self.meta_store_impl
492 }
493
494 pub fn id_gen_manager(&self) -> &SqlIdGeneratorManagerRef {
495 &self.id_gen_manager_impl
496 }
497
498 pub fn notification_manager_ref(&self) -> NotificationManagerRef {
499 self.notification_manager.clone()
500 }
501
502 pub fn notification_manager(&self) -> &NotificationManager {
503 self.notification_manager.deref()
504 }
505
506 pub fn idle_manager_ref(&self) -> IdleManagerRef {
507 self.idle_manager.clone()
508 }
509
510 pub fn idle_manager(&self) -> &IdleManager {
511 self.idle_manager.deref()
512 }
513
514 pub fn actor_id_generator(&self) -> &AtomicU32 {
515 self.actor_id_generator.deref()
516 }
517
518 pub async fn system_params_reader(&self) -> SystemParamsReader {
519 self.system_param_manager_impl.get_params().await
520 }
521
522 pub fn system_params_manager_impl_ref(&self) -> SystemParamsControllerRef {
523 self.system_param_manager_impl.clone()
524 }
525
526 pub fn session_params_manager_impl_ref(&self) -> SessionParamsControllerRef {
527 self.session_param_manager_impl.clone()
528 }
529
530 pub fn stream_client_pool_ref(&self) -> StreamClientPoolRef {
531 self.stream_client_pool.clone()
532 }
533
534 pub fn stream_client_pool(&self) -> &StreamClientPool {
535 self.stream_client_pool.deref()
536 }
537
538 pub fn frontend_client_pool(&self) -> &FrontendClientPool {
539 self.frontend_client_pool.deref()
540 }
541
542 pub fn cluster_id(&self) -> &ClusterId {
543 &self.cluster_id
544 }
545
546 pub fn event_log_manager_ref(&self) -> EventLogManagerRef {
547 self.event_log_manager.clone()
548 }
549
550 pub fn await_tree_reg(&self) -> &await_tree::Registry {
551 &self.await_tree_reg
552 }
553
554 pub fn shared_actor_infos(&self) -> &SharedActorInfos {
555 &self.shared_actor_info
556 }
557}
558
559#[cfg(any(test, feature = "test"))]
560impl MetaSrvEnv {
561 pub async fn for_test() -> Self {
563 Self::for_test_opts(MetaOpts::test(false), |_| ()).await
564 }
565
566 pub async fn for_test_opts(
567 opts: MetaOpts,
568 on_test_system_params: impl FnOnce(&mut risingwave_pb::meta::PbSystemParams),
569 ) -> Self {
570 let mut system_params = risingwave_common::system_param::system_params_for_test();
571 on_test_system_params(&mut system_params);
572 Self::new(
573 opts,
574 system_params,
575 Default::default(),
576 SqlMetaStore::for_test().await,
577 )
578 .await
579 .unwrap()
580 }
581}