AdjustOpenFilesLimit only try best to raise the max open files according to the max clients and RocksDB open file configuration. It also reserves a number of file descriptors(128) for extra operations of persistence, listening sockets, log files and so forth.
| 1989 | // of file descriptors(128) for extra operations of persistence, listening sockets, |
| 1990 | // log files and so forth. |
| 1991 | void Server::AdjustOpenFilesLimit() { |
| 1992 | const int min_reserved_fds = 128; |
| 1993 | auto rocksdb_max_open_file = static_cast<rlim_t>(config_->rocks_db.max_open_files); |
| 1994 | auto max_clients = static_cast<rlim_t>(config_->maxclients); |
| 1995 | auto max_files = max_clients + rocksdb_max_open_file + min_reserved_fds; |
| 1996 | |
| 1997 | rlimit limit; |
| 1998 | if (getrlimit(RLIMIT_NOFILE, &limit) == -1) { |
| 1999 | return; |
| 2000 | } |
| 2001 | |
| 2002 | rlim_t old_limit = limit.rlim_cur; |
| 2003 | // Set the max number of files only if the current limit is not enough |
| 2004 | if (old_limit >= max_files) { |
| 2005 | return; |
| 2006 | } |
| 2007 | |
| 2008 | int setrlimit_error = 0; |
| 2009 | rlim_t best_limit = max_files; |
| 2010 | |
| 2011 | while (best_limit > old_limit) { |
| 2012 | limit.rlim_cur = best_limit; |
| 2013 | limit.rlim_max = best_limit; |
| 2014 | if (setrlimit(RLIMIT_NOFILE, &limit) != -1) break; |
| 2015 | |
| 2016 | setrlimit_error = errno; |
| 2017 | |
| 2018 | rlim_t decr_step = 16; |
| 2019 | if (best_limit < decr_step) { |
| 2020 | best_limit = old_limit; |
| 2021 | break; |
| 2022 | } |
| 2023 | |
| 2024 | best_limit -= decr_step; |
| 2025 | } |
| 2026 | |
| 2027 | if (best_limit < old_limit) best_limit = old_limit; |
| 2028 | |
| 2029 | if (best_limit < max_files) { |
| 2030 | if (best_limit <= static_cast<int>(min_reserved_fds)) { |
| 2031 | WARN( |
| 2032 | "[server] Your current 'ulimit -n' of {} is not enough for the server to start. " |
| 2033 | "Please increase your open file limit to at least {}. Exiting.", |
| 2034 | old_limit, max_files); |
| 2035 | exit(1); |
| 2036 | } |
| 2037 | |
| 2038 | WARN( |
| 2039 | "[server] You requested max clients of {} and RocksDB max open files of {} " |
| 2040 | "requiring at least {} max file descriptors.", |
| 2041 | max_clients, rocksdb_max_open_file, max_files); |
| 2042 | |
| 2043 | WARN( |
| 2044 | "[server] Server can't set maximum open files to {} " |
| 2045 | "because of OS error: {}", |
| 2046 | max_files, strerror(setrlimit_error)); |
| 2047 | } else { |
| 2048 | WARN("[server] Increased maximum number of open files to {} (it's originally set to {})", max_files, old_limit); |