2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2013-10-16 21:59:46 +00:00
|
|
|
//
|
2011-03-18 22:37:00 +00:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
2017-01-12 00:42:07 +00:00
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors
|
2020-09-23 19:54:29 +00:00
|
|
|
|
|
|
|
#if !defined(OS_WIN)
|
|
|
|
|
2015-10-23 16:16:46 +00:00
|
|
|
#include <dirent.h>
|
2019-06-04 05:59:54 +00:00
|
|
|
#ifndef ROCKSDB_NO_DYNAMIC_EXTENSION
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
2015-10-23 16:16:46 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2019-06-04 05:59:54 +00:00
|
|
|
|
2015-10-23 16:16:46 +00:00
|
|
|
#if defined(OS_LINUX)
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#endif
|
2019-12-08 04:54:27 +00:00
|
|
|
#if defined(ROCKSDB_IOURING_PRESENT)
|
|
|
|
#include <liburing.h>
|
|
|
|
#endif
|
2015-10-23 16:16:46 +00:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
2018-08-08 15:02:43 +00:00
|
|
|
#if defined(OS_LINUX) || defined(OS_SOLARIS) || defined(OS_ANDROID)
|
2015-10-23 16:16:46 +00:00
|
|
|
#include <sys/statfs.h>
|
|
|
|
#include <sys/syscall.h>
|
2017-09-22 19:37:59 +00:00
|
|
|
#include <sys/sysmacros.h>
|
2015-10-23 16:16:46 +00:00
|
|
|
#endif
|
Auto recovery from out of space errors (#4164)
Summary:
This commit implements automatic recovery from a Status::NoSpace() error
during background operations such as write callback, flush and
compaction. The broad design is as follows -
1. Compaction errors are treated as soft errors and don't put the
database in read-only mode. A compaction is delayed until enough free
disk space is available to accomodate the compaction outputs, which is
estimated based on the input size. This means that users can continue to
write, and we rely on the WriteController to delay or stop writes if the
compaction debt becomes too high due to persistent low disk space
condition
2. Errors during write callback and flush are treated as hard errors,
i.e the database is put in read-only mode and goes back to read-write
only fater certain recovery actions are taken.
3. Both types of recovery rely on the SstFileManagerImpl to poll for
sufficient disk space. We assume that there is a 1-1 mapping between an
SFM and the underlying OS storage container. For cases where multiple
DBs are hosted on a single storage container, the user is expected to
allocate a single SFM instance and use the same one for all the DBs. If
no SFM is specified by the user, DBImpl::Open() will allocate one, but
this will be one per DB and each DB will recover independently. The
recovery implemented by SFM is as follows -
a) On the first occurance of an out of space error during compaction,
subsequent
compactions will be delayed until the disk free space check indicates
enough available space. The required space is computed as the sum of
input sizes.
b) The free space check requirement will be removed once the amount of
free space is greater than the size reserved by in progress
compactions when the first error occured
c) If the out of space error is a hard error, a background thread in
SFM will poll for sufficient headroom before triggering the recovery
of the database and putting it in write-only mode. The headroom is
calculated as the sum of the write_buffer_size of all the DB instances
associated with the SFM
4. EventListener callbacks will be called at the start and completion of
automatic recovery. Users can disable the auto recov ery in the start
callback, and later initiate it manually by calling DB::Resume()
Todo:
1. More extensive testing
2. Add disk full condition to db_stress (follow-on PR)
Pull Request resolved: https://github.com/facebook/rocksdb/pull/4164
Differential Revision: D9846378
Pulled By: anand1976
fbshipit-source-id: 80ea875dbd7f00205e19c82215ff6e37da10da4a
2018-09-15 20:36:19 +00:00
|
|
|
#include <sys/statvfs.h>
|
2015-10-23 16:16:46 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
2019-12-08 04:54:27 +00:00
|
|
|
#if defined(ROCKSDB_IOURING_PRESENT)
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#endif
|
2015-10-23 16:16:46 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <algorithm>
|
2015-03-18 18:26:10 +00:00
|
|
|
// Get nano time includes
|
2020-06-18 16:50:05 +00:00
|
|
|
#if defined(OS_LINUX) || defined(OS_FREEBSD) || defined(OS_GNU_KFREEBSD)
|
2015-03-18 18:26:10 +00:00
|
|
|
#elif defined(__MACH__)
|
2019-09-11 21:04:54 +00:00
|
|
|
#include <Availability.h>
|
2013-11-17 07:44:39 +00:00
|
|
|
#include <mach/clock.h>
|
|
|
|
#include <mach/mach.h>
|
2015-03-18 18:26:10 +00:00
|
|
|
#else
|
|
|
|
#include <chrono>
|
2013-11-17 07:44:39 +00:00
|
|
|
#endif
|
2015-10-23 16:16:46 +00:00
|
|
|
#include <deque>
|
|
|
|
#include <set>
|
2016-12-22 20:51:29 +00:00
|
|
|
#include <vector>
|
2017-02-06 22:43:55 +00:00
|
|
|
|
Introduce a new storage specific Env API (#5761)
Summary:
The current Env API encompasses both storage/file operations, as well as OS related operations. Most of the APIs return a Status, which does not have enough metadata about an error, such as whether its retry-able or not, scope (i.e fault domain) of the error etc., that may be required in order to properly handle a storage error. The file APIs also do not provide enough control over the IO SLA, such as timeout, prioritization, hinting about placement and redundancy etc.
This PR separates out the file/storage APIs from Env into a new FileSystem class. The APIs are updated to return an IOStatus with metadata about the error, as well as to take an IOOptions structure as input in order to allow more control over the IO.
The user can set both ```options.env``` and ```options.file_system``` to specify that RocksDB should use the former for OS related operations and the latter for storage operations. Internally, a ```CompositeEnvWrapper``` has been introduced that inherits from ```Env``` and redirects individual methods to either an ```Env``` implementation or the ```FileSystem``` as appropriate. When options are sanitized during ```DB::Open```, ```options.env``` is replaced with a newly allocated ```CompositeEnvWrapper``` instance if both env and file_system have been specified. This way, the rest of the RocksDB code can continue to function as before.
This PR also ports PosixEnv to the new API by splitting it into two - PosixEnv and PosixFileSystem. PosixEnv is defined as a sub-class of CompositeEnvWrapper, and threading/time functions are overridden with Posix specific implementations in order to avoid an extra level of indirection.
The ```CompositeEnvWrapper``` translates ```IOStatus``` return code to ```Status```, and sets the severity to ```kSoftError``` if the io_status is retryable. The error handling code in RocksDB can then recover the DB automatically.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5761
Differential Revision: D18868376
Pulled By: anand1976
fbshipit-source-id: 39efe18a162ea746fabac6360ff529baba48486f
2019-12-13 22:47:08 +00:00
|
|
|
#include "env/composite_env_wrapper.h"
|
2017-04-06 02:02:00 +00:00
|
|
|
#include "env/io_posix.h"
|
2019-06-01 00:19:43 +00:00
|
|
|
#include "logging/logging.h"
|
|
|
|
#include "logging/posix_logger.h"
|
2017-04-06 02:02:00 +00:00
|
|
|
#include "monitoring/iostats_context_imp.h"
|
|
|
|
#include "monitoring/thread_status_updater.h"
|
2015-10-23 16:16:46 +00:00
|
|
|
#include "port/port.h"
|
2017-04-06 02:02:00 +00:00
|
|
|
#include "rocksdb/options.h"
|
2015-10-23 16:16:46 +00:00
|
|
|
#include "rocksdb/slice.h"
|
2019-05-31 00:39:43 +00:00
|
|
|
#include "test_util/sync_point.h"
|
2015-10-23 16:16:46 +00:00
|
|
|
#include "util/coding.h"
|
2018-06-05 19:51:05 +00:00
|
|
|
#include "util/compression_context_cache.h"
|
2016-01-26 00:26:53 +00:00
|
|
|
#include "util/random.h"
|
2015-10-23 16:16:46 +00:00
|
|
|
#include "util/string_util.h"
|
Ensure the destruction order of PosixEnv and ThreadLocalPtr
Summary:
By default, RocksDB initializes the singletons of ThreadLocalPtr first, then initializes PosixEnv
via static initializer. Destructor terminates objects in reverse order, so terminating PosixEnv
(calling pthread_mutex_lock), then ThreadLocal (calling pthread_mutex_destroy).
However, in certain case, application might initialize PosixEnv first, then ThreadLocalPtr.
This will cause core dump at the end of the program (eg. https://github.com/facebook/mysql-5.6/issues/122)
This patch fix this issue by ensuring the destruction order by moving the global static singletons
to function static singletons. Since function static singletons are initialized when the function is first
called, this property allows us invoke to enforce the construction of the static PosixEnv and the
singletons of ThreadLocalPtr by calling the function where the ThreadLocalPtr singletons belongs
right before we initialize the static PosixEnv.
Test Plan: Verified in the MyRocks.
Reviewers: yoshinorim, IslamAbdelRahman, rven, kradhakrishnan, anthony, sdong, MarkCallaghan
Reviewed By: anthony
Subscribers: dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D51789
2015-12-11 08:21:58 +00:00
|
|
|
#include "util/thread_local.h"
|
2016-08-26 17:41:35 +00:00
|
|
|
#include "util/threadpool_imp.h"
|
2013-11-17 07:44:39 +00:00
|
|
|
|
2013-03-13 20:50:26 +00:00
|
|
|
#if !defined(TMPFS_MAGIC)
|
|
|
|
#define TMPFS_MAGIC 0x01021994
|
|
|
|
#endif
|
|
|
|
#if !defined(XFS_SUPER_MAGIC)
|
|
|
|
#define XFS_SUPER_MAGIC 0x58465342
|
|
|
|
#endif
|
|
|
|
#if !defined(EXT4_SUPER_MAGIC)
|
|
|
|
#define EXT4_SUPER_MAGIC 0xEF53
|
|
|
|
#endif
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2019-06-04 05:59:54 +00:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
static const std::string kSharedLibExt = ".dll";
|
|
|
|
static const char kPathSeparator = ';';
|
|
|
|
#else
|
|
|
|
static const char kPathSeparator = ':';
|
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
static const std::string kSharedLibExt = ".dylib";
|
|
|
|
#else
|
|
|
|
static const std::string kSharedLibExt = ".so";
|
|
|
|
#endif
|
|
|
|
#endif
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2014-12-22 20:20:17 +00:00
|
|
|
ThreadStatusUpdater* CreateThreadStatusUpdater() {
|
|
|
|
return new ThreadStatusUpdater();
|
|
|
|
}
|
|
|
|
|
2019-06-04 05:59:54 +00:00
|
|
|
#ifndef ROCKSDB_NO_DYNAMIC_EXTENSION
|
|
|
|
class PosixDynamicLibrary : public DynamicLibrary {
|
|
|
|
public:
|
|
|
|
PosixDynamicLibrary(const std::string& name, void* handle)
|
|
|
|
: name_(name), handle_(handle) {}
|
|
|
|
~PosixDynamicLibrary() override { dlclose(handle_); }
|
|
|
|
|
2019-06-05 22:16:43 +00:00
|
|
|
Status LoadSymbol(const std::string& sym_name, void** func) override {
|
|
|
|
assert(nullptr != func);
|
|
|
|
dlerror(); // Clear any old error
|
|
|
|
*func = dlsym(handle_, sym_name.c_str());
|
2019-06-04 05:59:54 +00:00
|
|
|
if (*func != nullptr) {
|
|
|
|
return Status::OK();
|
|
|
|
} else {
|
2019-06-05 22:16:43 +00:00
|
|
|
char* err = dlerror();
|
2019-06-04 05:59:54 +00:00
|
|
|
return Status::NotFound("Error finding symbol: " + sym_name, err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* Name() const override { return name_.c_str(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string name_;
|
|
|
|
void* handle_;
|
|
|
|
};
|
|
|
|
#endif // !ROCKSDB_NO_DYNAMIC_EXTENSION
|
|
|
|
|
Introduce a new storage specific Env API (#5761)
Summary:
The current Env API encompasses both storage/file operations, as well as OS related operations. Most of the APIs return a Status, which does not have enough metadata about an error, such as whether its retry-able or not, scope (i.e fault domain) of the error etc., that may be required in order to properly handle a storage error. The file APIs also do not provide enough control over the IO SLA, such as timeout, prioritization, hinting about placement and redundancy etc.
This PR separates out the file/storage APIs from Env into a new FileSystem class. The APIs are updated to return an IOStatus with metadata about the error, as well as to take an IOOptions structure as input in order to allow more control over the IO.
The user can set both ```options.env``` and ```options.file_system``` to specify that RocksDB should use the former for OS related operations and the latter for storage operations. Internally, a ```CompositeEnvWrapper``` has been introduced that inherits from ```Env``` and redirects individual methods to either an ```Env``` implementation or the ```FileSystem``` as appropriate. When options are sanitized during ```DB::Open```, ```options.env``` is replaced with a newly allocated ```CompositeEnvWrapper``` instance if both env and file_system have been specified. This way, the rest of the RocksDB code can continue to function as before.
This PR also ports PosixEnv to the new API by splitting it into two - PosixEnv and PosixFileSystem. PosixEnv is defined as a sub-class of CompositeEnvWrapper, and threading/time functions are overridden with Posix specific implementations in order to avoid an extra level of indirection.
The ```CompositeEnvWrapper``` translates ```IOStatus``` return code to ```Status```, and sets the severity to ```kSoftError``` if the io_status is retryable. The error handling code in RocksDB can then recover the DB automatically.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5761
Differential Revision: D18868376
Pulled By: anand1976
fbshipit-source-id: 39efe18a162ea746fabac6360ff529baba48486f
2019-12-13 22:47:08 +00:00
|
|
|
class PosixEnv : public CompositeEnvWrapper {
|
2011-03-18 22:37:00 +00:00
|
|
|
public:
|
2020-03-24 04:50:42 +00:00
|
|
|
// This constructor is for constructing non-default Envs, mainly by
|
|
|
|
// NewCompositeEnv(). It allows new instances to share the same
|
|
|
|
// threadpool and other resources as the default Env, while allowing
|
|
|
|
// a non-default FileSystem implementation
|
|
|
|
PosixEnv(const PosixEnv* default_env, std::shared_ptr<FileSystem> fs);
|
2013-03-19 21:39:28 +00:00
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
~PosixEnv() override {
|
2020-03-24 04:50:42 +00:00
|
|
|
if (this == Env::Default()) {
|
|
|
|
for (const auto tid : threads_to_join_) {
|
|
|
|
pthread_join(tid, nullptr);
|
|
|
|
}
|
|
|
|
for (int pool_id = 0; pool_id < Env::Priority::TOTAL; ++pool_id) {
|
|
|
|
thread_pools_[pool_id].JoinAllThreads();
|
|
|
|
}
|
|
|
|
// Do not delete the thread_status_updater_ in order to avoid the
|
|
|
|
// free after use when Env::Default() is destructed while some other
|
|
|
|
// child threads are still trying to update thread status. All
|
|
|
|
// PosixEnv instances use the same thread_status_updater_, so never
|
|
|
|
// explicitly delete it.
|
2016-08-15 16:04:55 +00:00
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 02:42:07 +00:00
|
|
|
void SetFD_CLOEXEC(int fd, const EnvOptions* options) {
|
2013-06-07 22:35:17 +00:00
|
|
|
if ((options == nullptr || options->set_fd_cloexec) && fd > 0) {
|
2013-04-10 02:42:07 +00:00
|
|
|
fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-04 05:59:54 +00:00
|
|
|
#ifndef ROCKSDB_NO_DYNAMIC_EXTENSION
|
2019-06-05 22:16:43 +00:00
|
|
|
// Loads the named library into the result.
|
|
|
|
// If the input name is empty, the current executable is loaded
|
|
|
|
// On *nix systems, a "lib" prefix is added to the name if one is not supplied
|
|
|
|
// Comparably, the appropriate shared library extension is added to the name
|
|
|
|
// if not supplied. If search_path is not specified, the shared library will
|
|
|
|
// be loaded using the default path (LD_LIBRARY_PATH) If search_path is
|
|
|
|
// specified, the shared library will be searched for in the directories
|
|
|
|
// provided by the search path
|
2019-06-04 05:59:54 +00:00
|
|
|
Status LoadLibrary(const std::string& name, const std::string& path,
|
|
|
|
std::shared_ptr<DynamicLibrary>* result) override {
|
|
|
|
assert(result != nullptr);
|
|
|
|
if (name.empty()) {
|
|
|
|
void* hndl = dlopen(NULL, RTLD_NOW);
|
|
|
|
if (hndl != nullptr) {
|
|
|
|
result->reset(new PosixDynamicLibrary(name, hndl));
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
std::string library_name = name;
|
|
|
|
if (library_name.find(kSharedLibExt) == std::string::npos) {
|
|
|
|
library_name = library_name + kSharedLibExt;
|
|
|
|
}
|
|
|
|
#if !defined(OS_WIN)
|
|
|
|
if (library_name.find('/') == std::string::npos &&
|
|
|
|
library_name.compare(0, 3, "lib") != 0) {
|
|
|
|
library_name = "lib" + library_name;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (path.empty()) {
|
|
|
|
void* hndl = dlopen(library_name.c_str(), RTLD_NOW);
|
|
|
|
if (hndl != nullptr) {
|
|
|
|
result->reset(new PosixDynamicLibrary(library_name, hndl));
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
std::string local_path;
|
|
|
|
std::stringstream ss(path);
|
|
|
|
while (getline(ss, local_path, kPathSeparator)) {
|
|
|
|
if (!path.empty()) {
|
|
|
|
std::string full_name = local_path + "/" + library_name;
|
|
|
|
void* hndl = dlopen(full_name.c_str(), RTLD_NOW);
|
|
|
|
if (hndl != nullptr) {
|
|
|
|
result->reset(new PosixDynamicLibrary(full_name, hndl));
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Status::IOError(
|
|
|
|
IOErrorMsg("Failed to open shared library: xs", name), dlerror());
|
|
|
|
}
|
|
|
|
#endif // !ROCKSDB_NO_DYNAMIC_EXTENSION
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
void Schedule(void (*function)(void* arg1), void* arg, Priority pri = LOW,
|
|
|
|
void* tag = nullptr,
|
|
|
|
void (*unschedFunction)(void* arg) = nullptr) override;
|
2015-03-17 01:49:14 +00:00
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
int UnSchedule(void* arg, Priority pri) override;
|
2013-03-19 21:39:28 +00:00
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
void StartThread(void (*function)(void* arg), void* arg) override;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
void WaitForJoin() override;
|
2014-02-26 01:47:37 +00:00
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
unsigned int GetThreadPoolQueueLen(Priority pri = LOW) const override;
|
2014-03-10 23:14:48 +00:00
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
Status GetThreadList(std::vector<ThreadStatus>* thread_list) override {
|
2014-12-22 20:20:17 +00:00
|
|
|
assert(thread_status_updater_);
|
|
|
|
return thread_status_updater_->GetThreadList(thread_list);
|
|
|
|
}
|
|
|
|
|
2014-05-29 17:57:22 +00:00
|
|
|
static uint64_t gettid(pthread_t tid) {
|
2011-03-18 22:37:00 +00:00
|
|
|
uint64_t thread_id = 0;
|
2011-03-21 19:40:57 +00:00
|
|
|
memcpy(&thread_id, &tid, std::min(sizeof(thread_id), sizeof(tid)));
|
2011-07-21 02:40:18 +00:00
|
|
|
return thread_id;
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2014-05-29 17:57:22 +00:00
|
|
|
static uint64_t gettid() {
|
|
|
|
pthread_t tid = pthread_self();
|
|
|
|
return gettid(tid);
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
uint64_t GetThreadID() const override { return gettid(pthread_self()); }
|
2015-06-11 21:18:02 +00:00
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
uint64_t NowMicros() override {
|
2015-03-03 19:29:31 +00:00
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, nullptr);
|
|
|
|
return static_cast<uint64_t>(tv.tv_sec) * 1000000 + tv.tv_usec;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
uint64_t NowNanos() override {
|
2020-06-18 16:50:05 +00:00
|
|
|
#if defined(OS_LINUX) || defined(OS_FREEBSD) || defined(OS_GNU_KFREEBSD) || \
|
|
|
|
defined(OS_AIX)
|
2015-03-03 19:29:31 +00:00
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
return static_cast<uint64_t>(ts.tv_sec) * 1000000000 + ts.tv_nsec;
|
2017-04-22 03:41:37 +00:00
|
|
|
#elif defined(OS_SOLARIS)
|
|
|
|
return gethrtime();
|
2015-03-17 11:03:11 +00:00
|
|
|
#elif defined(__MACH__)
|
2015-03-03 19:29:31 +00:00
|
|
|
clock_serv_t cclock;
|
|
|
|
mach_timespec_t ts;
|
|
|
|
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
|
|
|
|
clock_get_time(cclock, &ts);
|
|
|
|
mach_port_deallocate(mach_task_self(), cclock);
|
|
|
|
return static_cast<uint64_t>(ts.tv_sec) * 1000000000 + ts.tv_nsec;
|
2015-03-18 18:26:10 +00:00
|
|
|
#else
|
|
|
|
return std::chrono::duration_cast<std::chrono::nanoseconds>(
|
|
|
|
std::chrono::steady_clock::now().time_since_epoch()).count();
|
|
|
|
#endif
|
[RocksDB] Added nano second stopwatch and new perf counters to track block read cost
Summary: The pupose of this diff is to expose per user-call level precise timing of block read, so that we can answer questions like: a Get() costs me 100ms, is that somehow related to loading blocks from file system, or sth else? We will answer that with EXACTLY how many blocks have been read, how much time was spent on transfering the bytes from os, how much time was spent on checksum verification and how much time was spent on block decompression, just for that one Get. A nano second stopwatch was introduced to track time with higher precision. The cost/precision of the stopwatch is also measured in unit-test. On my dev box, retrieving one time instance costs about 30ns, on average. The deviation of timing results is good enough to track 100ns-1us level events. And the overhead could be safely ignored for 100us level events (10000 instances/s), for example, a viewstate thrift call.
Test Plan: perf_context_test, also testing with viewstate shadow traffic.
Reviewers: dhruba
Reviewed By: dhruba
CC: leveldb, xjin
Differential Revision: https://reviews.facebook.net/D12351
2013-06-04 06:09:15 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
uint64_t NowCPUNanos() override {
|
2020-06-18 16:50:05 +00:00
|
|
|
#if defined(OS_LINUX) || defined(OS_FREEBSD) || defined(OS_GNU_KFREEBSD) || \
|
|
|
|
defined(OS_AIX) || (defined(__MACH__) && defined(__MAC_10_12))
|
2018-12-20 20:00:40 +00:00
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
|
|
|
|
return static_cast<uint64_t>(ts.tv_sec) * 1000000000 + ts.tv_nsec;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
void SleepForMicroseconds(int micros) override { usleep(micros); }
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
Status GetHostName(char* name, uint64_t len) override {
|
2014-11-13 19:39:30 +00:00
|
|
|
int ret = gethostname(name, static_cast<size_t>(len));
|
2012-08-14 22:20:36 +00:00
|
|
|
if (ret < 0) {
|
Introduce a new storage specific Env API (#5761)
Summary:
The current Env API encompasses both storage/file operations, as well as OS related operations. Most of the APIs return a Status, which does not have enough metadata about an error, such as whether its retry-able or not, scope (i.e fault domain) of the error etc., that may be required in order to properly handle a storage error. The file APIs also do not provide enough control over the IO SLA, such as timeout, prioritization, hinting about placement and redundancy etc.
This PR separates out the file/storage APIs from Env into a new FileSystem class. The APIs are updated to return an IOStatus with metadata about the error, as well as to take an IOOptions structure as input in order to allow more control over the IO.
The user can set both ```options.env``` and ```options.file_system``` to specify that RocksDB should use the former for OS related operations and the latter for storage operations. Internally, a ```CompositeEnvWrapper``` has been introduced that inherits from ```Env``` and redirects individual methods to either an ```Env``` implementation or the ```FileSystem``` as appropriate. When options are sanitized during ```DB::Open```, ```options.env``` is replaced with a newly allocated ```CompositeEnvWrapper``` instance if both env and file_system have been specified. This way, the rest of the RocksDB code can continue to function as before.
This PR also ports PosixEnv to the new API by splitting it into two - PosixEnv and PosixFileSystem. PosixEnv is defined as a sub-class of CompositeEnvWrapper, and threading/time functions are overridden with Posix specific implementations in order to avoid an extra level of indirection.
The ```CompositeEnvWrapper``` translates ```IOStatus``` return code to ```Status```, and sets the severity to ```kSoftError``` if the io_status is retryable. The error handling code in RocksDB can then recover the DB automatically.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5761
Differential Revision: D18868376
Pulled By: anand1976
fbshipit-source-id: 39efe18a162ea746fabac6360ff529baba48486f
2019-12-13 22:47:08 +00:00
|
|
|
if (errno == EFAULT || errno == EINVAL) {
|
2012-08-14 22:20:36 +00:00
|
|
|
return Status::InvalidArgument(strerror(errno));
|
Introduce a new storage specific Env API (#5761)
Summary:
The current Env API encompasses both storage/file operations, as well as OS related operations. Most of the APIs return a Status, which does not have enough metadata about an error, such as whether its retry-able or not, scope (i.e fault domain) of the error etc., that may be required in order to properly handle a storage error. The file APIs also do not provide enough control over the IO SLA, such as timeout, prioritization, hinting about placement and redundancy etc.
This PR separates out the file/storage APIs from Env into a new FileSystem class. The APIs are updated to return an IOStatus with metadata about the error, as well as to take an IOOptions structure as input in order to allow more control over the IO.
The user can set both ```options.env``` and ```options.file_system``` to specify that RocksDB should use the former for OS related operations and the latter for storage operations. Internally, a ```CompositeEnvWrapper``` has been introduced that inherits from ```Env``` and redirects individual methods to either an ```Env``` implementation or the ```FileSystem``` as appropriate. When options are sanitized during ```DB::Open```, ```options.env``` is replaced with a newly allocated ```CompositeEnvWrapper``` instance if both env and file_system have been specified. This way, the rest of the RocksDB code can continue to function as before.
This PR also ports PosixEnv to the new API by splitting it into two - PosixEnv and PosixFileSystem. PosixEnv is defined as a sub-class of CompositeEnvWrapper, and threading/time functions are overridden with Posix specific implementations in order to avoid an extra level of indirection.
The ```CompositeEnvWrapper``` translates ```IOStatus``` return code to ```Status```, and sets the severity to ```kSoftError``` if the io_status is retryable. The error handling code in RocksDB can then recover the DB automatically.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5761
Differential Revision: D18868376
Pulled By: anand1976
fbshipit-source-id: 39efe18a162ea746fabac6360ff529baba48486f
2019-12-13 22:47:08 +00:00
|
|
|
} else {
|
2017-06-26 19:42:21 +00:00
|
|
|
return IOError("GetHostName", name, errno);
|
Introduce a new storage specific Env API (#5761)
Summary:
The current Env API encompasses both storage/file operations, as well as OS related operations. Most of the APIs return a Status, which does not have enough metadata about an error, such as whether its retry-able or not, scope (i.e fault domain) of the error etc., that may be required in order to properly handle a storage error. The file APIs also do not provide enough control over the IO SLA, such as timeout, prioritization, hinting about placement and redundancy etc.
This PR separates out the file/storage APIs from Env into a new FileSystem class. The APIs are updated to return an IOStatus with metadata about the error, as well as to take an IOOptions structure as input in order to allow more control over the IO.
The user can set both ```options.env``` and ```options.file_system``` to specify that RocksDB should use the former for OS related operations and the latter for storage operations. Internally, a ```CompositeEnvWrapper``` has been introduced that inherits from ```Env``` and redirects individual methods to either an ```Env``` implementation or the ```FileSystem``` as appropriate. When options are sanitized during ```DB::Open```, ```options.env``` is replaced with a newly allocated ```CompositeEnvWrapper``` instance if both env and file_system have been specified. This way, the rest of the RocksDB code can continue to function as before.
This PR also ports PosixEnv to the new API by splitting it into two - PosixEnv and PosixFileSystem. PosixEnv is defined as a sub-class of CompositeEnvWrapper, and threading/time functions are overridden with Posix specific implementations in order to avoid an extra level of indirection.
The ```CompositeEnvWrapper``` translates ```IOStatus``` return code to ```Status```, and sets the severity to ```kSoftError``` if the io_status is retryable. The error handling code in RocksDB can then recover the DB automatically.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5761
Differential Revision: D18868376
Pulled By: anand1976
fbshipit-source-id: 39efe18a162ea746fabac6360ff529baba48486f
2019-12-13 22:47:08 +00:00
|
|
|
}
|
2012-08-14 22:20:36 +00:00
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
Status GetCurrentTime(int64_t* unix_time) override {
|
2013-03-01 02:04:58 +00:00
|
|
|
time_t ret = time(nullptr);
|
2012-08-14 22:20:36 +00:00
|
|
|
if (ret == (time_t) -1) {
|
2017-06-26 19:42:21 +00:00
|
|
|
return IOError("GetCurrentTime", "", errno);
|
2012-08-14 22:20:36 +00:00
|
|
|
}
|
|
|
|
*unix_time = (int64_t) ret;
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
Introduce a new storage specific Env API (#5761)
Summary:
The current Env API encompasses both storage/file operations, as well as OS related operations. Most of the APIs return a Status, which does not have enough metadata about an error, such as whether its retry-able or not, scope (i.e fault domain) of the error etc., that may be required in order to properly handle a storage error. The file APIs also do not provide enough control over the IO SLA, such as timeout, prioritization, hinting about placement and redundancy etc.
This PR separates out the file/storage APIs from Env into a new FileSystem class. The APIs are updated to return an IOStatus with metadata about the error, as well as to take an IOOptions structure as input in order to allow more control over the IO.
The user can set both ```options.env``` and ```options.file_system``` to specify that RocksDB should use the former for OS related operations and the latter for storage operations. Internally, a ```CompositeEnvWrapper``` has been introduced that inherits from ```Env``` and redirects individual methods to either an ```Env``` implementation or the ```FileSystem``` as appropriate. When options are sanitized during ```DB::Open```, ```options.env``` is replaced with a newly allocated ```CompositeEnvWrapper``` instance if both env and file_system have been specified. This way, the rest of the RocksDB code can continue to function as before.
This PR also ports PosixEnv to the new API by splitting it into two - PosixEnv and PosixFileSystem. PosixEnv is defined as a sub-class of CompositeEnvWrapper, and threading/time functions are overridden with Posix specific implementations in order to avoid an extra level of indirection.
The ```CompositeEnvWrapper``` translates ```IOStatus``` return code to ```Status```, and sets the severity to ```kSoftError``` if the io_status is retryable. The error handling code in RocksDB can then recover the DB automatically.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5761
Differential Revision: D18868376
Pulled By: anand1976
fbshipit-source-id: 39efe18a162ea746fabac6360ff529baba48486f
2019-12-13 22:47:08 +00:00
|
|
|
ThreadStatusUpdater* GetThreadStatusUpdater() const override {
|
|
|
|
return Env::GetThreadStatusUpdater();
|
2012-08-14 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
Introduce a new storage specific Env API (#5761)
Summary:
The current Env API encompasses both storage/file operations, as well as OS related operations. Most of the APIs return a Status, which does not have enough metadata about an error, such as whether its retry-able or not, scope (i.e fault domain) of the error etc., that may be required in order to properly handle a storage error. The file APIs also do not provide enough control over the IO SLA, such as timeout, prioritization, hinting about placement and redundancy etc.
This PR separates out the file/storage APIs from Env into a new FileSystem class. The APIs are updated to return an IOStatus with metadata about the error, as well as to take an IOOptions structure as input in order to allow more control over the IO.
The user can set both ```options.env``` and ```options.file_system``` to specify that RocksDB should use the former for OS related operations and the latter for storage operations. Internally, a ```CompositeEnvWrapper``` has been introduced that inherits from ```Env``` and redirects individual methods to either an ```Env``` implementation or the ```FileSystem``` as appropriate. When options are sanitized during ```DB::Open```, ```options.env``` is replaced with a newly allocated ```CompositeEnvWrapper``` instance if both env and file_system have been specified. This way, the rest of the RocksDB code can continue to function as before.
This PR also ports PosixEnv to the new API by splitting it into two - PosixEnv and PosixFileSystem. PosixEnv is defined as a sub-class of CompositeEnvWrapper, and threading/time functions are overridden with Posix specific implementations in order to avoid an extra level of indirection.
The ```CompositeEnvWrapper``` translates ```IOStatus``` return code to ```Status```, and sets the severity to ```kSoftError``` if the io_status is retryable. The error handling code in RocksDB can then recover the DB automatically.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5761
Differential Revision: D18868376
Pulled By: anand1976
fbshipit-source-id: 39efe18a162ea746fabac6360ff529baba48486f
2019-12-13 22:47:08 +00:00
|
|
|
std::string GenerateUniqueId() override { return Env::GenerateUniqueId(); }
|
|
|
|
|
2012-11-29 00:42:36 +00:00
|
|
|
// Allow increasing the number of worker threads.
|
2019-02-19 21:36:04 +00:00
|
|
|
void SetBackgroundThreads(int num, Priority pri) override {
|
2017-08-03 22:36:28 +00:00
|
|
|
assert(pri >= Priority::BOTTOM && pri <= Priority::HIGH);
|
2013-09-12 07:53:30 +00:00
|
|
|
thread_pools_[pri].SetBackgroundThreads(num);
|
2012-09-19 22:21:09 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
int GetBackgroundThreads(Priority pri) override {
|
2017-08-03 22:36:28 +00:00
|
|
|
assert(pri >= Priority::BOTTOM && pri <= Priority::HIGH);
|
2017-05-23 18:04:25 +00:00
|
|
|
return thread_pools_[pri].GetBackgroundThreads();
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
Status SetAllowNonOwnerAccess(bool allow_non_owner_access) override {
|
2018-04-13 20:05:28 +00:00
|
|
|
allow_non_owner_access_ = allow_non_owner_access;
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2014-11-03 22:11:33 +00:00
|
|
|
// Allow increasing the number of worker threads.
|
2019-02-19 21:36:04 +00:00
|
|
|
void IncBackgroundThreadsIfNeeded(int num, Priority pri) override {
|
2017-08-03 22:36:28 +00:00
|
|
|
assert(pri >= Priority::BOTTOM && pri <= Priority::HIGH);
|
2014-11-03 22:11:33 +00:00
|
|
|
thread_pools_[pri].IncBackgroundThreadsIfNeeded(num);
|
|
|
|
}
|
|
|
|
|
2020-06-13 20:23:40 +00:00
|
|
|
void LowerThreadPoolIOPriority(Priority pool) override {
|
2017-08-03 22:36:28 +00:00
|
|
|
assert(pool >= Priority::BOTTOM && pool <= Priority::HIGH);
|
2014-08-14 03:49:58 +00:00
|
|
|
#ifdef OS_LINUX
|
|
|
|
thread_pools_[pool].LowerIOPriority();
|
2018-04-24 15:38:01 +00:00
|
|
|
#else
|
|
|
|
(void)pool;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-06-13 20:23:40 +00:00
|
|
|
void LowerThreadPoolCPUPriority(Priority pool) override {
|
2018-04-24 15:38:01 +00:00
|
|
|
assert(pool >= Priority::BOTTOM && pool <= Priority::HIGH);
|
2020-06-13 20:23:40 +00:00
|
|
|
thread_pools_[pool].LowerCPUPriority(CpuPriority::kLow);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status LowerThreadPoolCPUPriority(Priority pool, CpuPriority pri) override {
|
|
|
|
assert(pool >= Priority::BOTTOM && pool <= Priority::HIGH);
|
|
|
|
thread_pools_[pool].LowerCPUPriority(pri);
|
|
|
|
return Status::OK();
|
2014-08-14 03:49:58 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
std::string TimeToString(uint64_t secondsSince1970) override {
|
2012-10-19 21:00:53 +00:00
|
|
|
const time_t seconds = (time_t)secondsSince1970;
|
|
|
|
struct tm t;
|
|
|
|
int maxsize = 64;
|
|
|
|
std::string dummy;
|
|
|
|
dummy.reserve(maxsize);
|
|
|
|
dummy.resize(maxsize);
|
|
|
|
char* p = &dummy[0];
|
|
|
|
localtime_r(&seconds, &t);
|
|
|
|
snprintf(p, maxsize,
|
|
|
|
"%04d/%02d/%02d-%02d:%02d:%02d ",
|
|
|
|
t.tm_year + 1900,
|
|
|
|
t.tm_mon + 1,
|
|
|
|
t.tm_mday,
|
|
|
|
t.tm_hour,
|
|
|
|
t.tm_min,
|
|
|
|
t.tm_sec);
|
|
|
|
return dummy;
|
|
|
|
}
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
private:
|
2020-03-24 04:50:42 +00:00
|
|
|
friend Env* Env::Default();
|
|
|
|
// Constructs the default Env, a singleton
|
|
|
|
PosixEnv();
|
|
|
|
|
|
|
|
// The below 4 members are only used by the default PosixEnv instance.
|
|
|
|
// Non-default instances simply maintain references to the backing
|
|
|
|
// members in te default instance
|
|
|
|
std::vector<ThreadPoolImpl> thread_pools_storage_;
|
|
|
|
pthread_mutex_t mu_storage_;
|
|
|
|
std::vector<pthread_t> threads_to_join_storage_;
|
|
|
|
bool allow_non_owner_access_storage_;
|
|
|
|
|
|
|
|
std::vector<ThreadPoolImpl>& thread_pools_;
|
|
|
|
pthread_mutex_t& mu_;
|
|
|
|
std::vector<pthread_t>& threads_to_join_;
|
2018-04-13 20:05:28 +00:00
|
|
|
// If true, allow non owner read access for db files. Otherwise, non-owner
|
|
|
|
// has no access to db files.
|
2020-03-24 04:50:42 +00:00
|
|
|
bool& allow_non_owner_access_;
|
2013-09-12 07:53:30 +00:00
|
|
|
};
|
|
|
|
|
2015-10-07 17:04:05 +00:00
|
|
|
PosixEnv::PosixEnv()
|
2020-03-24 04:50:42 +00:00
|
|
|
: CompositeEnvWrapper(this, FileSystem::Default()),
|
|
|
|
thread_pools_storage_(Priority::TOTAL),
|
|
|
|
allow_non_owner_access_storage_(true),
|
|
|
|
thread_pools_(thread_pools_storage_),
|
|
|
|
mu_(mu_storage_),
|
|
|
|
threads_to_join_(threads_to_join_storage_),
|
|
|
|
allow_non_owner_access_(allow_non_owner_access_storage_) {
|
2016-08-26 17:41:35 +00:00
|
|
|
ThreadPoolImpl::PthreadCall("mutex_init", pthread_mutex_init(&mu_, nullptr));
|
2014-11-20 18:49:32 +00:00
|
|
|
for (int pool_id = 0; pool_id < Env::Priority::TOTAL; ++pool_id) {
|
|
|
|
thread_pools_[pool_id].SetThreadPriority(
|
|
|
|
static_cast<Env::Priority>(pool_id));
|
2014-12-22 20:20:17 +00:00
|
|
|
// This allows later initializing the thread-local-env of each thread.
|
|
|
|
thread_pools_[pool_id].SetHostEnv(this);
|
2014-11-20 18:49:32 +00:00
|
|
|
}
|
2014-12-22 20:20:17 +00:00
|
|
|
thread_status_updater_ = CreateThreadStatusUpdater();
|
2013-09-12 07:53:30 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 04:50:42 +00:00
|
|
|
PosixEnv::PosixEnv(const PosixEnv* default_env, std::shared_ptr<FileSystem> fs)
|
|
|
|
: CompositeEnvWrapper(this, fs),
|
|
|
|
thread_pools_(default_env->thread_pools_),
|
|
|
|
mu_(default_env->mu_),
|
|
|
|
threads_to_join_(default_env->threads_to_join_),
|
|
|
|
allow_non_owner_access_(default_env->allow_non_owner_access_) {
|
|
|
|
thread_status_updater_ = default_env->thread_status_updater_;
|
|
|
|
}
|
|
|
|
|
2015-03-17 01:49:14 +00:00
|
|
|
void PosixEnv::Schedule(void (*function)(void* arg1), void* arg, Priority pri,
|
Running manual compactions in parallel with other automatic or manual compactions in restricted cases
Summary:
This diff provides a framework for doing manual
compactions in parallel with other compactions. We now have a deque of manual compactions. We also pass manual compactions as an argument from RunManualCompactions down to
BackgroundCompactions, so that RunManualCompactions can be reentrant.
Parallelism is controlled by the two routines
ConflictingManualCompaction to allow/disallow new parallel/manual
compactions based on already existing ManualCompactions. In this diff, by default manual compactions still have to run exclusive of other compactions. However, by setting the compaction option, exclusive_manual_compaction to false, it is possible to run other compactions in parallel with a manual compaction. However, we are still restricted to one manual compaction per column family at a time. All of these restrictions will be relaxed in future diffs.
I will be adding more tests later.
Test Plan: Rocksdb regression + new tests + valgrind
Reviewers: igor, anthony, IslamAbdelRahman, kradhakrishnan, yhchiang, sdong
Reviewed By: sdong
Subscribers: yoshinorim, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D47973
2015-12-14 19:20:34 +00:00
|
|
|
void* tag, void (*unschedFunction)(void* arg)) {
|
2017-08-03 22:36:28 +00:00
|
|
|
assert(pri >= Priority::BOTTOM && pri <= Priority::HIGH);
|
Running manual compactions in parallel with other automatic or manual compactions in restricted cases
Summary:
This diff provides a framework for doing manual
compactions in parallel with other compactions. We now have a deque of manual compactions. We also pass manual compactions as an argument from RunManualCompactions down to
BackgroundCompactions, so that RunManualCompactions can be reentrant.
Parallelism is controlled by the two routines
ConflictingManualCompaction to allow/disallow new parallel/manual
compactions based on already existing ManualCompactions. In this diff, by default manual compactions still have to run exclusive of other compactions. However, by setting the compaction option, exclusive_manual_compaction to false, it is possible to run other compactions in parallel with a manual compaction. However, we are still restricted to one manual compaction per column family at a time. All of these restrictions will be relaxed in future diffs.
I will be adding more tests later.
Test Plan: Rocksdb regression + new tests + valgrind
Reviewers: igor, anthony, IslamAbdelRahman, kradhakrishnan, yhchiang, sdong
Reviewed By: sdong
Subscribers: yoshinorim, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D47973
2015-12-14 19:20:34 +00:00
|
|
|
thread_pools_[pri].Schedule(function, arg, tag, unschedFunction);
|
2015-03-17 01:49:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int PosixEnv::UnSchedule(void* arg, Priority pri) {
|
|
|
|
return thread_pools_[pri].UnSchedule(arg);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2014-03-10 23:14:48 +00:00
|
|
|
unsigned int PosixEnv::GetThreadPoolQueueLen(Priority pri) const {
|
2017-08-03 22:36:28 +00:00
|
|
|
assert(pri >= Priority::BOTTOM && pri <= Priority::HIGH);
|
2014-03-10 23:14:48 +00:00
|
|
|
return thread_pools_[pri].GetQueueLen();
|
|
|
|
}
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
struct StartThreadState {
|
|
|
|
void (*user_function)(void*);
|
|
|
|
void* arg;
|
|
|
|
};
|
2014-08-30 04:21:49 +00:00
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
static void* StartThreadWrapper(void* arg) {
|
|
|
|
StartThreadState* state = reinterpret_cast<StartThreadState*>(arg);
|
|
|
|
state->user_function(state->arg);
|
|
|
|
delete state;
|
2013-03-01 02:04:58 +00:00
|
|
|
return nullptr;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PosixEnv::StartThread(void (*function)(void* arg), void* arg) {
|
|
|
|
pthread_t t;
|
|
|
|
StartThreadState* state = new StartThreadState;
|
|
|
|
state->user_function = function;
|
|
|
|
state->arg = arg;
|
2016-08-26 17:41:35 +00:00
|
|
|
ThreadPoolImpl::PthreadCall(
|
2015-10-27 12:03:43 +00:00
|
|
|
"start thread", pthread_create(&t, nullptr, &StartThreadWrapper, state));
|
2016-08-26 17:41:35 +00:00
|
|
|
ThreadPoolImpl::PthreadCall("lock", pthread_mutex_lock(&mu_));
|
2013-03-19 21:39:28 +00:00
|
|
|
threads_to_join_.push_back(t);
|
2016-08-26 17:41:35 +00:00
|
|
|
ThreadPoolImpl::PthreadCall("unlock", pthread_mutex_unlock(&mu_));
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 01:47:37 +00:00
|
|
|
void PosixEnv::WaitForJoin() {
|
|
|
|
for (const auto tid : threads_to_join_) {
|
|
|
|
pthread_join(tid, nullptr);
|
|
|
|
}
|
|
|
|
threads_to_join_.clear();
|
|
|
|
}
|
|
|
|
|
2011-10-31 17:22:06 +00:00
|
|
|
} // namespace
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2013-10-18 21:50:54 +00:00
|
|
|
std::string Env::GenerateUniqueId() {
|
|
|
|
std::string uuid_file = "/proc/sys/kernel/random/uuid";
|
2015-07-21 00:20:40 +00:00
|
|
|
|
|
|
|
Status s = FileExists(uuid_file);
|
|
|
|
if (s.ok()) {
|
2013-10-18 21:50:54 +00:00
|
|
|
std::string uuid;
|
2015-07-21 00:20:40 +00:00
|
|
|
s = ReadFileToString(this, uuid_file, &uuid);
|
2013-10-18 21:50:54 +00:00
|
|
|
if (s.ok()) {
|
|
|
|
return uuid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Could not read uuid_file - generate uuid using "nanos-random"
|
|
|
|
Random64 r(time(nullptr));
|
|
|
|
uint64_t random_uuid_portion =
|
|
|
|
r.Uniform(std::numeric_limits<uint64_t>::max());
|
|
|
|
uint64_t nanos_uuid_portion = NowNanos();
|
|
|
|
char uuid2[200];
|
2013-11-17 07:44:39 +00:00
|
|
|
snprintf(uuid2,
|
|
|
|
200,
|
|
|
|
"%lx-%lx",
|
|
|
|
(unsigned long)nanos_uuid_portion,
|
|
|
|
(unsigned long)random_uuid_portion);
|
2013-10-18 21:50:54 +00:00
|
|
|
return uuid2;
|
|
|
|
}
|
|
|
|
|
2016-04-21 17:37:27 +00:00
|
|
|
//
|
|
|
|
// Default Posix Env
|
|
|
|
//
|
2011-03-18 22:37:00 +00:00
|
|
|
Env* Env::Default() {
|
Ensure the destruction order of PosixEnv and ThreadLocalPtr
Summary:
By default, RocksDB initializes the singletons of ThreadLocalPtr first, then initializes PosixEnv
via static initializer. Destructor terminates objects in reverse order, so terminating PosixEnv
(calling pthread_mutex_lock), then ThreadLocal (calling pthread_mutex_destroy).
However, in certain case, application might initialize PosixEnv first, then ThreadLocalPtr.
This will cause core dump at the end of the program (eg. https://github.com/facebook/mysql-5.6/issues/122)
This patch fix this issue by ensuring the destruction order by moving the global static singletons
to function static singletons. Since function static singletons are initialized when the function is first
called, this property allows us invoke to enforce the construction of the static PosixEnv and the
singletons of ThreadLocalPtr by calling the function where the ThreadLocalPtr singletons belongs
right before we initialize the static PosixEnv.
Test Plan: Verified in the MyRocks.
Reviewers: yoshinorim, IslamAbdelRahman, rven, kradhakrishnan, anthony, sdong, MarkCallaghan
Reviewed By: anthony
Subscribers: dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D51789
2015-12-11 08:21:58 +00:00
|
|
|
// The following function call initializes the singletons of ThreadLocalPtr
|
|
|
|
// right before the static default_env. This guarantees default_env will
|
|
|
|
// always being destructed before the ThreadLocalPtr singletons get
|
|
|
|
// destructed as C++ guarantees that the destructions of static variables
|
|
|
|
// is in the reverse order of their constructions.
|
|
|
|
//
|
|
|
|
// Since static members are destructed in the reverse order
|
|
|
|
// of their construction, having this call here guarantees that
|
|
|
|
// the destructor of static PosixEnv will go first, then the
|
|
|
|
// the singletons of ThreadLocalPtr.
|
|
|
|
ThreadLocalPtr::InitSingletons();
|
2018-06-05 19:51:05 +00:00
|
|
|
CompressionContextCache::InitSingleton();
|
2018-06-04 22:52:31 +00:00
|
|
|
INIT_SYNC_POINT_SINGLETONS();
|
2013-04-23 01:10:28 +00:00
|
|
|
static PosixEnv default_env;
|
2020-03-24 04:50:42 +00:00
|
|
|
return &default_env;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Env> NewCompositeEnv(std::shared_ptr<FileSystem> fs) {
|
|
|
|
PosixEnv* default_env = static_cast<PosixEnv*>(Env::Default());
|
|
|
|
return std::unique_ptr<Env>(new PosixEnv(default_env, fs));
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2020-09-23 19:54:29 +00:00
|
|
|
|
|
|
|
#endif
|