Skip to content

Commit

Permalink
config: try updating soft limit before failing (tikv#1308)
Browse files Browse the repository at this point in the history
  • Loading branch information
BusyJay authored Nov 13, 2016
1 parent 1050931 commit 505f1ec
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 24 deletions.
10 changes: 4 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,18 @@ static_release:
ROCKSDB_SYS_STATIC=1 ROCKSDB_SYS_PORTABLE=1 make release

test:
# Default Mac OSX `ulimit -n` is 256, too small. When SIP is enabled, DYLD_LIBRARY_PATH will not work
# in subshell, so we have to set it again here. LOCAL_DIR is defined in .travis.yml.
# When SIP is enabled, DYLD_LIBRARY_PATH will not work in subshell, so we have to set it
# again here. LOCAL_DIR is defined in .travis.yml.
export DYLD_LIBRARY_PATH="${DYLD_LIBRARY_PATH}:${LOCAL_DIR}/lib" && \
export LOG_LEVEL=DEBUG && \
export RUST_BACKTRACE=1 && \
ulimit -n 2000 && \
cargo test --features "${ENABLE_FEATURES}" -- --nocapture && \
cargo test --features "${ENABLE_FEATURES}" --bench benches -- --nocapture
# TODO: remove above target once https://github.com/rust-lang/cargo/issues/2984 is resolved.

bench:
# Default Mac OSX `ulimit -n` is 256, too small.
ulimit -n 4096 && LOG_LEVEL=ERROR RUST_BACKTRACE=1 cargo bench --features "${ENABLE_FEATURES}" -- --nocapture
ulimit -n 4096 && RUST_BACKTRACE=1 cargo run --release --bin bench-tikv --features "${ENABLE_FEATURES}"
LOG_LEVEL=ERROR RUST_BACKTRACE=1 cargo bench --features "${ENABLE_FEATURES}" -- --nocapture && \
RUST_BACKTRACE=1 cargo run --release --bin bench-tikv --features "${ENABLE_FEATURES}"

format:
@cargo fmt -- --write-mode diff | grep -E "Diff .*at line" > /dev/null && cargo fmt -- --write-mode overwrite || exit 0
Expand Down
9 changes: 9 additions & 0 deletions benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ use test::Bencher;

use util::KvGenerator;

#[bench]
fn _bench_check_requirement(_: &mut test::Bencher) {
if let Err(e) = tikv::util::config::check_max_open_fds(4096) {
panic!("To run bench, please make sure the maximum number of open file descriptors not \
less than 4096: {:?}",
e);
}
}

#[bench]
fn bench_kv_iter(b: &mut Bencher) {
let mut g = KvGenerator::new(100, 1000);
Expand Down
5 changes: 5 additions & 0 deletions benches/bin/bench-tikv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ fn print_result(smp: BenchSamples) {
}

fn main() {
if let Err(e) = tikv::util::config::check_max_open_fds(4096) {
panic!("To run bench, please make sure the maximum number of open file descriptors not \
less than 4096: {:?}",
e);
}
// TODO allow user to specify flag to just bench some cases.
raftstore::bench_raftstore();
mvcc::bench_engine();
Expand Down
36 changes: 22 additions & 14 deletions src/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// limitations under the License.

use std::str::FromStr;
use std::mem;
use std::net::{SocketAddrV4, SocketAddrV6};

use url;
Expand Down Expand Up @@ -133,24 +134,31 @@ pub fn parse_readable_int(size: &str) -> Result<i64, ConfigError> {
pub fn check_max_open_fds(expect: u64) -> Result<(), ConfigError> {
use libc;

let fd_limit = unsafe {
let mut fd_limit = ::std::mem::zeroed();
if 0 != libc::getrlimit(libc::RLIMIT_NOFILE, &mut fd_limit) {
unsafe {
let mut fd_limit = mem::zeroed();
let mut err = libc::getrlimit(libc::RLIMIT_NOFILE, &mut fd_limit);
if err != 0 {
return Err(ConfigError::Limit("check_max_open_fds failed".to_owned()));
}
if fd_limit.rlim_cur >= expect {
return Ok(());
}

fd_limit
};

if fd_limit.rlim_cur < expect {
return Err(ConfigError::Limit(format!("the maximum number of open file descriptors is \
too small, got {}, expect greater or equal to \
{}",
fd_limit.rlim_cur,
expect)));
let prev_limit = fd_limit.rlim_cur;
fd_limit.rlim_cur = expect;
if fd_limit.rlim_max < expect {
// If the process is not started by privileged user, this will fail.
fd_limit.rlim_max = expect;
}
err = libc::setrlimit(libc::RLIMIT_NOFILE, &fd_limit);
if err == 0 {
return Ok(());
}
Err(ConfigError::Limit(format!("the maximum number of open file descriptors is too \
small, got {}, expect greater or equal to {}",
prev_limit,
expect)))
}

Ok(())
}

#[cfg(not(unix))]
Expand Down
15 changes: 12 additions & 3 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ extern crate time;
extern crate rustc_serialize;
extern crate test;

use std::env;

mod test_raft;
mod test_raft_snap;
mod test_raft_paper;
Expand All @@ -50,11 +48,22 @@ mod storage;
mod util;
mod pd;

use std::env;

#[test]
fn _travis_setup() {
fn _0_travis_setup() {
// Set up travis test fail case log.
// The prefix "_" here is to guarantee running this case first.
if env::var("TRAVIS").is_ok() && env::var("LOG_FILE").is_ok() {
self::util::init_log();
}
}

#[test]
fn _1_check_system_requirement() {
if let Err(e) = tikv::util::config::check_max_open_fds(2000) {
panic!("To run test, please make sure the maximum number of open file descriptors not \
less than 2000: {:?}",
e);
}
}
2 changes: 1 addition & 1 deletion travis-build/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ prepare_brew:
prepare_osx: ${LOCAL_DIR}/lib/librocksdb.dylib ${LOCAL_DIR}/bin/pd-server prepare-rustfmt | prepare_brew prepare_rust

test_linux test_osx:
ulimit -n 2000 && ./travis-build/test.sh
./travis-build/test.sh

cover_linux:
export LOG_LEVEL=DEBUG && \
Expand Down

0 comments on commit 505f1ec

Please sign in to comment.