common_meta/
distributed_time_constants.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::sync::OnceLock;
16use std::time::Duration;
17
18pub const BASE_HEARTBEAT_INTERVAL: Duration = Duration::from_secs(3);
19
20/// The lease seconds of metasrv leader.
21pub const META_LEASE_SECS: u64 = 5;
22
23/// The keep-alive interval of the Postgres connection.
24pub const POSTGRES_KEEP_ALIVE_SECS: u64 = 30;
25
26/// In a lease, there are two opportunities for renewal.
27pub const META_KEEP_ALIVE_INTERVAL_SECS: u64 = META_LEASE_SECS / 2;
28
29/// The timeout of the heartbeat request.
30pub const HEARTBEAT_TIMEOUT: Duration = Duration::from_secs(META_KEEP_ALIVE_INTERVAL_SECS + 1);
31
32/// The keep-alive interval of the heartbeat channel.
33pub const HEARTBEAT_CHANNEL_KEEP_ALIVE_INTERVAL_SECS: Duration = Duration::from_secs(15);
34
35/// The keep-alive timeout of the heartbeat channel.
36pub const HEARTBEAT_CHANNEL_KEEP_ALIVE_TIMEOUT_SECS: Duration = Duration::from_secs(5);
37
38/// The default mailbox round-trip timeout.
39pub const MAILBOX_RTT_SECS: u64 = 1;
40
41/// The interval of reporting topic stats.
42pub const TOPIC_STATS_REPORT_INTERVAL_SECS: u64 = 15;
43
44/// The retention seconds of topic stats.
45pub const TOPIC_STATS_RETENTION_SECS: u64 = TOPIC_STATS_REPORT_INTERVAL_SECS * 100;
46
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
48/// The distributed time constants.
49pub 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
57/// The frontend heartbeat interval is 6 times of the base heartbeat interval.
58pub fn frontend_heartbeat_interval(base_heartbeat_interval: Duration) -> Duration {
59    base_heartbeat_interval * 6
60}
61
62impl DistributedTimeConstants {
63    /// Create a new DistributedTimeConstants from the heartbeat interval.
64    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
86/// Get the default distributed time constants.
87pub fn default_distributed_time_constants() -> &'static DistributedTimeConstants {
88    DEFAULT_DISTRIBUTED_TIME_CONSTANTS.get_or_init(Default::default)
89}
90
91/// Initialize the default distributed time constants.
92pub 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}