common_meta/
distributed_time_constants.rs1use std::sync::OnceLock;
16use std::time::Duration;
17
18pub const BASE_HEARTBEAT_INTERVAL: Duration = Duration::from_secs(3);
19
20pub const META_LEASE_SECS: u64 = 5;
22
23pub const POSTGRES_KEEP_ALIVE_SECS: u64 = 30;
25
26pub const META_KEEP_ALIVE_INTERVAL_SECS: u64 = META_LEASE_SECS / 2;
28
29pub const HEARTBEAT_TIMEOUT: Duration = Duration::from_secs(META_KEEP_ALIVE_INTERVAL_SECS + 1);
31
32pub const HEARTBEAT_CHANNEL_KEEP_ALIVE_INTERVAL_SECS: Duration = Duration::from_secs(15);
34
35pub const HEARTBEAT_CHANNEL_KEEP_ALIVE_TIMEOUT_SECS: Duration = Duration::from_secs(5);
37
38pub const MAILBOX_RTT_SECS: u64 = 1;
40
41pub const TOPIC_STATS_REPORT_INTERVAL_SECS: u64 = 15;
43
44pub const TOPIC_STATS_RETENTION_SECS: u64 = TOPIC_STATS_REPORT_INTERVAL_SECS * 100;
46
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48pub struct DistributedTimeConstants {
50 pub heartbeat_interval: Duration,
51 pub frontend_heartbeat_interval: Duration,
52 pub region_lease: Duration,
53 pub datanode_lease: Duration,
54 pub flownode_lease: Duration,
55}
56
57pub fn frontend_heartbeat_interval(base_heartbeat_interval: Duration) -> Duration {
59 base_heartbeat_interval * 6
60}
61
62impl DistributedTimeConstants {
63 pub fn from_heartbeat_interval(heartbeat_interval: Duration) -> Self {
65 let region_lease = heartbeat_interval * 3 + Duration::from_secs(1);
66 let datanode_lease = region_lease;
67 let flownode_lease = datanode_lease;
68 Self {
69 heartbeat_interval,
70 frontend_heartbeat_interval: frontend_heartbeat_interval(heartbeat_interval),
71 region_lease,
72 datanode_lease,
73 flownode_lease,
74 }
75 }
76}
77
78impl Default for DistributedTimeConstants {
79 fn default() -> Self {
80 Self::from_heartbeat_interval(BASE_HEARTBEAT_INTERVAL)
81 }
82}
83
84static DEFAULT_DISTRIBUTED_TIME_CONSTANTS: OnceLock<DistributedTimeConstants> = OnceLock::new();
85
86pub fn default_distributed_time_constants() -> &'static DistributedTimeConstants {
88 DEFAULT_DISTRIBUTED_TIME_CONSTANTS.get_or_init(Default::default)
89}
90
91pub fn init_distributed_time_constants(base_heartbeat_interval: Duration) {
93 let distributed_time_constants =
94 DistributedTimeConstants::from_heartbeat_interval(base_heartbeat_interval);
95 DEFAULT_DISTRIBUTED_TIME_CONSTANTS
96 .set(distributed_time_constants)
97 .expect("Failed to set default distributed time constants");
98 common_telemetry::info!(
99 "Initialized default distributed time constants: {:#?}",
100 distributed_time_constants
101 );
102}