Skip to content
Permalink
Browse files

Add option to decide panic or not when key exceed bound (tikv#4254)

  • Loading branch information...
zhangjinpeng1987 authored and Connor1996 committed Feb 23, 2019
1 parent 24c94b4 commit 0b46d3f99baaf87f8cf3bf96d9290ea362c1469e
Showing with 32 additions and 20 deletions.
  1. +13 −4 src/bin/tikv-server.rs
  2. +2 −0 src/config.rs
  3. +4 −16 src/raftstore/store/region_snapshot.rs
  4. +10 −0 src/util/mod.rs
  5. +1 −0 tests/config/mod.rs
  6. +2 −0 tests/config/test-custom.toml
@@ -98,6 +98,17 @@ fn check_system_config(config: &TiKvConfig) {
}
}

fn pre_start(cfg: &TiKvConfig) {
// Before any startup, check system configuration and environment variables.
check_system_config(&cfg);
check_environment_variables();

if cfg.panic_when_key_exceed_bound {
info!("panic-when-key-exceed-bound is on");
tikv_util::set_panic_when_key_exceed_bound(true);
}
}

fn run_raft_server(pd_client: RpcClient, cfg: &TiKvConfig, security_mgr: Arc<SecurityManager>) {
let store_path = Path::new(&cfg.storage.data_dir);
let lock_path = store_path.join(Path::new("LOCK"));
@@ -434,10 +445,8 @@ fn main() {
serde_json::to_string_pretty(&config).unwrap()
);

// Before any startup, check system configuration.
check_system_config(&config);

check_environment_variables();
// Do some prepare works before start.
pre_start(&config);

let security_mgr = Arc::new(
SecurityManager::new(&config.security)
@@ -910,6 +910,7 @@ pub struct TiKvConfig {
pub log_level: slog::Level,
pub log_file: String,
pub log_rotation_timespan: ReadableDuration,
pub panic_when_key_exceed_bound: bool,
pub readpool: ReadPoolConfig,
pub server: ServerConfig,
pub storage: StorageConfig,
@@ -930,6 +931,7 @@ impl Default for TiKvConfig {
log_level: slog::Level::Info,
log_file: "".to_owned(),
log_rotation_timespan: ReadableDuration::hours(24),
panic_when_key_exceed_bound: false,
readpool: ReadPoolConfig::default(),
server: ServerConfig::default(),
metric: MetricConfig::default(),
@@ -19,7 +19,7 @@ use std::sync::Arc;
use raftstore::store::engine::{IterOption, Peekable, Snapshot, SyncSnapshot};
use raftstore::store::{keys, util, PeerStorage};
use raftstore::Result;
use util::set_panic_mark;
use util::{panic_when_key_exceed_bound, set_panic_mark};

/// Snapshot of a region.
///
@@ -154,7 +154,6 @@ pub struct RegionIterator {
region: Arc<Region>,
start_key: Vec<u8>,
end_key: Vec<u8>,
panic_when_exceed_bound: bool,
}

fn set_lower_bound(iter_opt: &mut IterOption, region: &Region) {
@@ -195,7 +194,6 @@ impl RegionIterator {
start_key,
end_key,
region,
panic_when_exceed_bound: true,
}
}

@@ -216,15 +214,9 @@ impl RegionIterator {
start_key,
end_key,
region,
panic_when_exceed_bound: true,
}
}

// Set false only in test.
pub fn panic_when_exceed_bound(&mut self, panic: bool) {
self.panic_when_exceed_bound = panic;
}

pub fn seek_to_first(&mut self) -> bool {
self.valid = self.iter.seek(self.start_key.as_slice().into());

@@ -316,7 +308,7 @@ impl RegionIterator {
#[inline]
pub fn should_seekable(&self, key: &[u8]) -> Result<()> {
if let Err(e) = util::check_key_in_region_inclusive(key, &self.region) {
if self.panic_when_exceed_bound {
if panic_when_key_exceed_bound() {
set_panic_mark();
panic!("key exceed bound: {:?}", e);
} else {
@@ -453,7 +445,6 @@ mod tests {
for upper_bound in upper_bounds {
let iter_opt = IterOption::new(None, upper_bound.map(|v| v.to_vec()), true);
let mut iter = snap.iter(iter_opt);
iter.panic_when_exceed_bound(false);
for (seek_key, in_range, seek_exp, prev_exp) in seek_table.clone() {
let check_res =
|iter: &RegionIterator, res: Result<bool>, exp: Option<(&[u8], &[u8])>| {
@@ -547,8 +538,7 @@ mod tests {

let snap = RegionSnapshot::new(&store);
let mut statistics = CFStatistics::default();
let mut it = snap.iter(IterOption::default());
it.panic_when_exceed_bound(false);
let it = snap.iter(IterOption::default());
let mut iter = Cursor::new(it, ScanMode::Mixed);
assert!(
!iter
@@ -607,8 +597,7 @@ mod tests {
region.mut_peers().push(Peer::new());
let store = new_peer_storage(engines, &region);
let snap = RegionSnapshot::new(&store);
let mut it = snap.iter(IterOption::default());
it.panic_when_exceed_bound(false);
let it = snap.iter(IterOption::default());
let mut iter = Cursor::new(it, ScanMode::Mixed);
assert!(
!iter
@@ -664,7 +653,6 @@ mod tests {
let mut iter_opt = IterOption::default();
iter_opt.set_lower_bound(b"a3".to_vec());
let mut iter = snap.iter(iter_opt);
iter.panic_when_exceed_bound(false);
assert!(iter.seek_to_last());
let mut res = vec![];
loop {
@@ -53,6 +53,16 @@ pub mod worker;
pub use self::rocksdb::properties;
pub use self::rocksdb::stats as rocksdb_stats;

static PANIC_WHEN_KEY_EXCEED_BOUND: AtomicBool = AtomicBool::new(false);

pub fn panic_when_key_exceed_bound() -> bool {
PANIC_WHEN_KEY_EXCEED_BOUND.load(Ordering::SeqCst)
}

pub fn set_panic_when_key_exceed_bound(flag: bool) {
PANIC_WHEN_KEY_EXCEED_BOUND.store(flag, Ordering::SeqCst);
}

static PANIC_MARK: AtomicBool = AtomicBool::new(false);

pub fn set_panic_mark() {
@@ -435,6 +435,7 @@ fn test_serde_custom_tikv_config() {
stream_channel_window: 123,
max_open_engines: 2,
};
value.panic_when_key_exceed_bound = true;

let custom = read_file_in_project_dir("tests/config/test-custom.toml");
let load = toml::from_str(&custom).unwrap();
@@ -1,6 +1,8 @@
log-level = "debug"
log-file = "foo"
log-rotation-timespan = "24h"
panic-when-key-exceed-bound = true

[readpool.storage]
high-concurrency = 1
normal-concurrency = 3

0 comments on commit 0b46d3f

Please sign in to comment.
You can’t perform that action at this time.