1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15+ //! Raft cluster configuration including networking, storage, and performance tuning.
16+
1517use std:: net:: Ipv4Addr ;
1618use std:: path:: Path ;
1719
@@ -24,6 +26,10 @@ use databend_common_meta_types::MetaStartupError;
2426use crate :: ondisk:: DATA_VERSION ;
2527use crate :: raft_log_v004;
2628
29+ /// Configuration for a Raft node including networking, storage paths, and performance tuning.
30+ ///
31+ /// Controls cluster behavior, network endpoints, storage persistence, and operational parameters
32+ /// like heartbeat intervals and snapshot thresholds.
2733#[ derive( Clone , Debug , PartialEq , Eq , serde:: Serialize ) ]
2834pub struct RaftConfig {
2935 /// Identify a config.
@@ -52,49 +58,78 @@ pub struct RaftConfig {
5258 /// You should only use this in a testing environment, unless YOU KNOW WHAT YOU ARE DOING.
5359 pub no_sync : bool ,
5460
55- /// The maximum number of log entries for log entries cache.
61+ /// Maximum log entries to cache in memory.
62+ ///
63+ /// Higher values improve read performance but use more memory.
64+ /// Default: 1,000,000 entries.
5665 pub log_cache_max_items : u64 ,
5766
58- /// The maximum memory in bytes for the log entries cache.
67+ /// Maximum memory for log cache in bytes.
68+ ///
69+ /// Total memory limit for cached log entries.
70+ /// Default: 1GB (1,073,741,824 bytes).
5971 pub log_cache_capacity : u64 ,
6072
61- /// Maximum number of records in a chunk of raft-log WAL.
73+ /// Maximum log records per WAL chunk
6274 pub log_wal_chunk_max_records : u64 ,
6375
64- /// Maximum size in bytes for a chunk of raft-log WAL.
76+ /// Maximum WAL chunk size in bytes
6577 pub log_wal_chunk_max_size : u64 ,
6678
67- /// The number of logs since the last snapshot to trigger next snapshot.
79+ /// Trigger snapshot after this many logs since last snapshot.
80+ ///
81+ /// Lower values create more frequent snapshots but increase I/O.
82+ /// Default: 1024 log entries.
6883 pub snapshot_logs_since_last : u64 ,
6984
70- /// The interval in milli seconds at which a leader send heartbeat message to followers.
71- /// Different value of this setting on leader and followers may cause unexpected behavior.
85+ /// Leader heartbeat interval in milliseconds.
86+ ///
87+ /// Must be > 0. Typical values: 500-2000ms.
88+ /// Affects leader election timeout (2x-3x this value).
89+ /// Default: 1000ms.
7290 pub heartbeat_interval : u64 ,
7391
74- /// The max time in milli seconds that a leader wait for install-snapshot ack from a follower or non-voter.
92+ /// Install snapshot timeout in milliseconds.
93+ ///
94+ /// Time to wait for snapshot installation to complete.
95+ /// Default: 4000ms.
7596 pub install_snapshot_timeout : u64 ,
7697
77- /// The maximum number of applied logs to keep before purging
98+ /// Maximum applied logs to keep before purging.
99+ ///
100+ /// Controls disk usage vs recovery time tradeoff.
101+ /// Default: 1000 log entries.
78102 pub max_applied_log_to_keep : u64 ,
79103
80- /// The size of chunk for transmitting snapshot. The default is 64MB
104+ /// Snapshot transmission chunk size in bytes.
105+ ///
106+ /// Larger chunks reduce network overhead but increase memory usage.
107+ /// Default: 4MB (4,194,304 bytes).
81108 pub snapshot_chunk_size : u64 ,
82109
83110 /// Whether to check keys fed to snapshot are sorted.
111+ ///
112+ /// Enable for debugging snapshot corruption issues.
113+ /// Adds performance overhead in debug builds.
114+ /// Default: true.
84115 pub snapshot_db_debug_check : bool ,
85116
86- /// The maximum number of keys allowed in a block within a snapshot db .
117+ /// Maximum keys per snapshot database block.
87118 ///
88- /// A block serves as the caching unit in a snapshot database .
89- /// Smaller blocks enable more granular cache control but may increase the index size .
119+ /// Higher values improve compression but increase memory usage .
120+ /// Default: 8000 keys per block .
90121 pub snapshot_db_block_keys : u64 ,
91122
92- /// The total block to cache.
123+ /// Number of blocks to cache in snapshot database.
124+ ///
125+ /// Higher values improve read performance but use more memory.
126+ /// Default: 1024 blocks.
93127 pub snapshot_db_block_cache_item : u64 ,
94128
95- /// The total cache size for snapshot blocks.
129+ /// Total cache size for snapshot blocks in bytes .
96130 ///
97- /// By default it is 1GB.
131+ /// Controls memory usage for snapshot block caching.
132+ /// Default: 1GB (1,073,741,824 bytes).
98133 pub snapshot_db_block_cache_size : u64 ,
99134
100135 /// Single node metasrv. It creates a single node cluster if meta data is not initialized.
@@ -234,7 +269,7 @@ impl RaftConfig {
234269 Endpoint :: new ( & self . raft_advertise_host , self . raft_api_port )
235270 }
236271
237- /// Support ip address and hostname
272+ /// Resolves the advertise host to an endpoint, supporting both IP addresses and hostnames.
238273 pub async fn raft_api_addr ( & self ) -> Result < Endpoint > {
239274 let ipv4_addr = self . raft_advertise_host . as_str ( ) . parse :: < Ipv4Addr > ( ) ;
240275 match ipv4_addr {
@@ -260,6 +295,13 @@ impl RaftConfig {
260295 ( self . heartbeat_interval * 2 , self . heartbeat_interval * 3 )
261296 }
262297
298+ /// Validates configuration parameters for consistency and correctness.
299+ ///
300+ /// # Errors
301+ /// Returns `MetaStartupError::InvalidConfig` if:
302+ /// - Neither `single` nor `join` is specified
303+ /// - Both `single` and `join` are specified
304+ /// - Node tries to join itself (self-reference in join addresses)
263305 pub fn check ( & self ) -> std:: result:: Result < ( ) , MetaStartupError > {
264306 // If just leaving, does not need to check other config
265307 if !self . leave_via . is_empty ( ) {
0 commit comments