Repository: tikv/tikv
File: components/engine_rocks/src/properties.rs (Rust)

Code before:
    },
    info,
    smoother::Smoother,
};
use txn_types::{Key, Write, WriteType};

use crate::{
    decode_properties::{DecodeProperties, IndexHandle, IndexHandles},
    mvcc_properties::*,
};

const PROP_TOTAL_SIZE: &str = "tikv.total_size";
const PROP_SIZE_INDEX: &str = "tikv.size_index";
const PROP_RANGE_INDEX: &str = "tikv.range_index";
pub const DEFAULT_PROP_SIZE_INDEX_DISTANCE: u64 = 4 * 1024 * 1024;
pub const DEFAULT_PROP_KEYS_INDEX_DISTANCE: u64 = 40 * 1024;

/// Caps the compaction factor to avoid unrealistic estimates.
pub const TITAN_MAX_COMPACTION_FACTOR: f64 = 5000.0;
const FIVE_MINS_IN_SECONDS: u64 = 5 * 60;

lazy_static! {
    pub static ref TITAN_COMPRESSION_FACTOR_SMOOTHER: Mutex<Smoother<f64, 30, FIVE_MINS_IN_SECONDS, 0>> =
        Mutex::new(Smoother::<f64, 30, FIVE_MINS_IN_SECONDS, 0>::default());
    pub static ref TITAN_COMPRESSION_FACTOR: AtomicU64 = AtomicU64::new(0);
    pub static ref TITAN_MAX_BLOB_SIZE_SEEN: AtomicU64 = AtomicU64::new(0);
}

fn get_entry_size(
    value: &[u8],
    entry_type: DBEntryType,
    titan_compression_factor: f64,
    titan_max_blob_size: u64,
) -> std::result::Result<u64, ()> {
    match entry_type {
        DBEntryType::Put => Ok(value.len() as u64),
        DBEntryType::BlobIndex => match TitanBlobIndex::decode(value) {
            Ok(index) => {
                // Estimate the raw blob size using the Titan compression factor.
                let estimation = (index.blob_size as f64 * titan_compression_factor) as u64;
                let blob_raw_size = estimation.min(titan_max_blob_size).max(index.blob_size);
                Ok(blob_raw_size + value.len() as u64)
            }
            Err(_) => Err(()),
        },
        _ => Err(()),
    }
}

// Deprecated. Only for compatible issue from v2.0 or older version.
#[derive(Debug, Default)]

Review comment (suggestion):
Same here, `u64::MAX` feels better. Imagine compression factor has been instantiated, while max_blob_size has not. 

Code after:
    },
    info,
    smoother::Smoother,
};
use txn_types::{Key, Write, WriteType};

use crate::{
    decode_properties::{DecodeProperties, IndexHandle, IndexHandles},
    mvcc_properties::*,
};

const PROP_TOTAL_SIZE: &str = "tikv.total_size";
const PROP_SIZE_INDEX: &str = "tikv.size_index";
const PROP_RANGE_INDEX: &str = "tikv.range_index";
pub const DEFAULT_PROP_SIZE_INDEX_DISTANCE: u64 = 4 * 1024 * 1024;
pub const DEFAULT_PROP_KEYS_INDEX_DISTANCE: u64 = 40 * 1024;

/// Caps the compaction factor to avoid unrealistic estimates.
pub const TITAN_MAX_COMPACTION_FACTOR: f64 = 5000.0;
const FIVE_MINS_IN_SECONDS: u64 = 5 * 60;

lazy_static! {
    // A global smoother used to estimate the Titan blob compression factor over
    // the last 5 minutes. The window size is 30, roughly matching the number of
    // data points collected in 5 minutes assuming one data point every 10 secs.
    pub static ref TITAN_COMPRESSION_FACTOR_SMOOTHER: Mutex<Smoother<f64, 30, FIVE_MINS_IN_SECONDS, 0>> =
        Mutex::new(Smoother::<f64, 30, FIVE_MINS_IN_SECONDS, 0>::default());
    pub static ref TITAN_COMPRESSION_FACTOR: AtomicU64 = AtomicU64::new(f64::to_bits(1.0));
    pub static ref TITAN_MAX_BLOB_SIZE_SEEN: AtomicU64 = AtomicU64::new(u64::MAX);
}

fn get_entry_size(
    value: &[u8],
    entry_type: DBEntryType,
    titan_compression_factor: f64,
    titan_max_blob_size: u64,
) -> std::result::Result<u64, ()> {
    match entry_type {
        DBEntryType::Put => Ok(value.len() as u64),
        DBEntryType::BlobIndex => match TitanBlobIndex::decode(value) {
            Ok(index) => {
                // Estimate the raw blob size using the Titan compression factor.
                let estimation = (index.blob_size as f64 * titan_compression_factor) as u64;
                let blob_raw_size = estimation.min(titan_max_blob_size).max(index.blob_size);
                Ok(blob_raw_size + value.len() as u64)
            }
            Err(_) => Err(()),
        },
        _ => Err(()),
    }
}