Skip to content

Architecture: Magic numbers in configuration defaults #28

@drmingdrmer

Description

@drmingdrmer

Summary

Configuration defaults use magic numbers with inconsistent formatting and no named constants.

Location

crates/raft-store/src/config.rs:182-216

impl Default for RaftConfig {
    fn default() -> Self {
        Self {
            log_cache_max_items: 1_000_000,
            log_cache_capacity: 1024 * 1024 * 1024,         // 1GB - expression
            log_wal_chunk_max_records: 100_000,
            log_wal_chunk_max_size: 256 * 1024 * 1024,      // 256MB - expression
            snapshot_chunk_size: 4194304,                    // 4MB - raw number
            snapshot_db_block_cache_size: 1073741824,        // 1GB - raw number
            // ...
        }
    }
}

Problem

  1. Inconsistent formats: some use 1024 * 1024 * 1024, others use 1073741824
  2. Some have comments explaining units, others do not
  3. No named constants - hard to understand intent
  4. Difficult to tune or document configuration options

Suggested Fix

Define named constants:

// In a constants module
const KB: u64 = 1024;
const MB: u64 = 1024 * KB;
const GB: u64 = 1024 * MB;

const DEFAULT_LOG_CACHE_MAX_ITEMS: u64 = 1_000_000;
const DEFAULT_LOG_CACHE_CAPACITY: u64 = 1 * GB;
const DEFAULT_WAL_CHUNK_MAX_RECORDS: u64 = 100_000;
const DEFAULT_WAL_CHUNK_MAX_SIZE: u64 = 256 * MB;
const DEFAULT_SNAPSHOT_CHUNK_SIZE: u64 = 4 * MB;
const DEFAULT_BLOCK_CACHE_SIZE: u64 = 1 * GB;

impl Default for RaftConfig {
    fn default() -> Self {
        Self {
            log_cache_max_items: DEFAULT_LOG_CACHE_MAX_ITEMS,
            log_cache_capacity: DEFAULT_LOG_CACHE_CAPACITY,
            // ...
        }
    }
}

Priority

P2 - Code maintainability

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions