Skip to content

Commit

Permalink
sanitize block size (#4920)
Browse files Browse the repository at this point in the history
Signed-off-by: zhangjinpeng1987 <zhangjinpeng@pingcap.com>
  • Loading branch information
zhangjinpeng87 committed Jun 19, 2019
1 parent 1924a32 commit c95175d
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const LOCKCF_MAX_MEM: usize = GB as usize;
const RAFT_MIN_MEM: usize = 256 * MB as usize;
const RAFT_MAX_MEM: usize = 2 * GB as usize;
const LAST_CONFIG_FILE: &str = "last_tikv.toml";
const MAX_BLOCK_SIZE: usize = 32 * MB as usize;

fn memory_mb_for_cf(is_raft_db: bool, cf: &str) -> usize {
let total_mem = sys_info::mem_info().unwrap().total * KB;
Expand Down Expand Up @@ -158,6 +159,21 @@ macro_rules! cf_config {
pub prop_keys_index_distance: u64,
pub titan: TitanCfConfig,
}

impl $name {
fn validate(&self) -> Result<(), Box<dyn Error>> {
if self.block_size.0 as usize > MAX_BLOCK_SIZE {
return Err(format!(
"invalid block-size {} for {}, exceed max size {}",
self.block_size.0,
stringify!($name),
MAX_BLOCK_SIZE
)
.into());
}
Ok(())
}
}
};
}

Expand Down Expand Up @@ -599,6 +615,10 @@ impl TitanDBConfig {
opts.set_max_background_gc(self.max_background_gc);
opts
}

fn validate(&self) -> Result<(), Box<dyn Error>> {
Ok(())
}
}

#[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
Expand Down Expand Up @@ -750,6 +770,11 @@ impl DbConfig {
}

fn validate(&mut self) -> Result<(), Box<dyn Error>> {
self.defaultcf.validate()?;
self.lockcf.validate()?;
self.writecf.validate()?;
self.raftcf.validate()?;
self.titan.validate()?;
Ok(())
}

Expand Down Expand Up @@ -934,6 +959,11 @@ impl RaftDbConfig {
pub fn build_cf_opts(&self, cache: &Option<Cache>) -> Vec<CFOptions<'_>> {
vec![CFOptions::new(CF_DEFAULT, self.defaultcf.build_opt(cache))]
}

fn validate(&mut self) -> Result<(), Box<dyn Error>> {
self.defaultcf.validate()?;
Ok(())
}
}

#[derive(Clone, Serialize, Deserialize, PartialEq, Debug)]
Expand Down Expand Up @@ -1260,6 +1290,7 @@ impl TiKvConfig {
}

self.rocksdb.validate()?;
self.raftdb.validate()?;
self.server.validate()?;
self.raft_store.validate()?;
self.pd.validate()?;
Expand Down Expand Up @@ -1554,6 +1585,24 @@ mod tests {
tikv_cfg.validate().unwrap();
}

#[test]
fn test_block_size() {
let mut tikv_cfg = TiKvConfig::default();
tikv_cfg.pd.endpoints = vec!["".to_owned()];
tikv_cfg.rocksdb.defaultcf.block_size = ReadableSize::gb(10);
tikv_cfg.rocksdb.lockcf.block_size = ReadableSize::gb(10);
tikv_cfg.rocksdb.writecf.block_size = ReadableSize::gb(10);
tikv_cfg.rocksdb.raftcf.block_size = ReadableSize::gb(10);
tikv_cfg.raftdb.defaultcf.block_size = ReadableSize::gb(10);
assert!(tikv_cfg.validate().is_err());
tikv_cfg.rocksdb.defaultcf.block_size = ReadableSize::kb(10);
tikv_cfg.rocksdb.lockcf.block_size = ReadableSize::kb(10);
tikv_cfg.rocksdb.writecf.block_size = ReadableSize::kb(10);
tikv_cfg.rocksdb.raftcf.block_size = ReadableSize::kb(10);
tikv_cfg.raftdb.defaultcf.block_size = ReadableSize::kb(10);
tikv_cfg.validate().unwrap();
}

#[test]
fn test_parse_log_level() {
#[derive(Serialize, Deserialize, Debug)]
Expand Down

0 comments on commit c95175d

Please sign in to comment.