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
|
2015-10-23 16:16:46 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#if defined(OS_LINUX)
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#endif
|
|
|
|
#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>
|
|
|
|
#include <time.h>
|
|
|
|
#include <algorithm>
|
2015-03-18 18:26:10 +00:00
|
|
|
// Get nano time includes
|
|
|
|
#if defined(OS_LINUX) || defined(OS_FREEBSD)
|
|
|
|
#elif defined(__MACH__)
|
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
|
|
|
|
2017-04-06 02:02:00 +00:00
|
|
|
#include "env/io_posix.h"
|
|
|
|
#include "env/posix_logger.h"
|
|
|
|
#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"
|
2015-10-23 16:16:46 +00:00
|
|
|
#include "util/logging.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
|
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
namespace rocksdb {
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2014-12-22 20:20:17 +00:00
|
|
|
ThreadStatusUpdater* CreateThreadStatusUpdater() {
|
|
|
|
return new ThreadStatusUpdater();
|
|
|
|
}
|
|
|
|
|
2018-04-13 20:05:28 +00:00
|
|
|
inline mode_t GetDBFileMode(bool allow_non_owner_access) {
|
|
|
|
return allow_non_owner_access ? 0644 : 0600;
|
|
|
|
}
|
|
|
|
|
2012-08-18 07:26:50 +00:00
|
|
|
// list of pathnames that are locked
|
|
|
|
static std::set<std::string> lockedFiles;
|
|
|
|
static port::Mutex mutex_lockedFiles;
|
|
|
|
|
2018-06-14 00:28:31 +00:00
|
|
|
static int LockOrUnlock(int fd, bool lock) {
|
2011-03-18 22:37:00 +00:00
|
|
|
errno = 0;
|
|
|
|
struct flock f;
|
|
|
|
memset(&f, 0, sizeof(f));
|
|
|
|
f.l_type = (lock ? F_WRLCK : F_UNLCK);
|
|
|
|
f.l_whence = SEEK_SET;
|
|
|
|
f.l_start = 0;
|
|
|
|
f.l_len = 0; // Lock/unlock entire file
|
2012-08-18 07:26:50 +00:00
|
|
|
int value = fcntl(fd, F_SETLK, &f);
|
2018-06-14 00:28:31 +00:00
|
|
|
|
2012-08-18 07:26:50 +00:00
|
|
|
return value;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2015-10-14 08:14:53 +00:00
|
|
|
class PosixFileLock : public FileLock {
|
|
|
|
public:
|
|
|
|
int fd_;
|
|
|
|
std::string filename;
|
|
|
|
};
|
|
|
|
|
2018-08-30 03:24:17 +00:00
|
|
|
int cloexec_flags(int flags, const EnvOptions* options) {
|
|
|
|
// If the system supports opening the file with cloexec enabled,
|
|
|
|
// do so, as this avoids a race condition if a db is opened around
|
|
|
|
// the same time that a child process is forked
|
|
|
|
#ifdef O_CLOEXEC
|
|
|
|
if (options == nullptr || options->set_fd_cloexec) {
|
|
|
|
flags |= O_CLOEXEC;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
class PosixEnv : public Env {
|
|
|
|
public:
|
|
|
|
PosixEnv();
|
2013-03-19 21:39:28 +00:00
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
~PosixEnv() override {
|
2013-09-12 07:53:30 +00:00
|
|
|
for (const auto tid : threads_to_join_) {
|
|
|
|
pthread_join(tid, nullptr);
|
|
|
|
}
|
2014-12-22 20:20:17 +00:00
|
|
|
for (int pool_id = 0; pool_id < Env::Priority::TOTAL; ++pool_id) {
|
|
|
|
thread_pools_[pool_id].JoinAllThreads();
|
|
|
|
}
|
2016-08-15 16:04:55 +00:00
|
|
|
// Delete the thread_status_updater_ only when the current Env is not
|
|
|
|
// Env::Default(). This is to avoid the free-after-use error when
|
|
|
|
// Env::Default() is destructed while some other child threads are
|
|
|
|
// still trying to update thread status.
|
|
|
|
if (this != Env::Default()) {
|
|
|
|
delete thread_status_updater_;
|
|
|
|
}
|
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-02-19 21:36:04 +00:00
|
|
|
Status NewSequentialFile(const std::string& fname,
|
|
|
|
std::unique_ptr<SequentialFile>* result,
|
|
|
|
const EnvOptions& options) override {
|
2013-01-20 10:07:13 +00:00
|
|
|
result->reset();
|
2017-01-12 00:42:07 +00:00
|
|
|
int fd = -1;
|
2018-08-30 03:24:17 +00:00
|
|
|
int flags = cloexec_flags(O_RDONLY, &options);
|
2017-01-12 00:42:07 +00:00
|
|
|
FILE* file = nullptr;
|
|
|
|
|
|
|
|
if (options.use_direct_reads && !options.use_mmap_reads) {
|
2017-02-16 18:25:06 +00:00
|
|
|
#ifdef ROCKSDB_LITE
|
|
|
|
return Status::IOError(fname, "Direct I/O not supported in RocksDB lite");
|
|
|
|
#endif // !ROCKSDB_LITE
|
2017-04-22 03:41:37 +00:00
|
|
|
#if !defined(OS_MACOSX) && !defined(OS_OPENBSD) && !defined(OS_SOLARIS)
|
2017-01-12 00:42:07 +00:00
|
|
|
flags |= O_DIRECT;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-03-31 21:45:26 +00:00
|
|
|
do {
|
2015-07-03 00:23:41 +00:00
|
|
|
IOSTATS_TIMER_GUARD(open_nanos);
|
2018-04-13 20:05:28 +00:00
|
|
|
fd = open(fname.c_str(), flags, GetDBFileMode(allow_non_owner_access_));
|
2017-01-12 00:42:07 +00:00
|
|
|
} while (fd < 0 && errno == EINTR);
|
|
|
|
if (fd < 0) {
|
2017-06-26 19:42:21 +00:00
|
|
|
return IOError("While opening a file for sequentially reading", fname,
|
|
|
|
errno);
|
2017-01-12 00:42:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SetFD_CLOEXEC(fd, &options);
|
|
|
|
|
|
|
|
if (options.use_direct_reads && !options.use_mmap_reads) {
|
2016-05-23 23:05:53 +00:00
|
|
|
#ifdef OS_MACOSX
|
|
|
|
if (fcntl(fd, F_NOCACHE, 1) == -1) {
|
|
|
|
close(fd);
|
2017-06-26 19:42:21 +00:00
|
|
|
return IOError("While fcntl NoCache", fname, errno);
|
2016-05-23 23:05:53 +00:00
|
|
|
}
|
|
|
|
#endif
|
2011-03-18 22:37:00 +00:00
|
|
|
} else {
|
2017-01-12 00:42:07 +00:00
|
|
|
do {
|
|
|
|
IOSTATS_TIMER_GUARD(open_nanos);
|
|
|
|
file = fdopen(fd, "r");
|
|
|
|
} while (file == nullptr && errno == EINTR);
|
|
|
|
if (file == nullptr) {
|
|
|
|
close(fd);
|
2017-06-26 19:42:21 +00:00
|
|
|
return IOError("While opening file for sequentially read", fname,
|
|
|
|
errno);
|
2017-01-12 00:42:07 +00:00
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2017-01-12 00:42:07 +00:00
|
|
|
result->reset(new PosixSequentialFile(fname, file, fd, options));
|
|
|
|
return Status::OK();
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
Status NewRandomAccessFile(const std::string& fname,
|
|
|
|
std::unique_ptr<RandomAccessFile>* result,
|
|
|
|
const EnvOptions& options) override {
|
2013-01-20 10:07:13 +00:00
|
|
|
result->reset();
|
2012-03-15 16:14:00 +00:00
|
|
|
Status s;
|
2015-07-03 00:23:41 +00:00
|
|
|
int fd;
|
2018-08-30 03:24:17 +00:00
|
|
|
int flags = cloexec_flags(O_RDONLY, &options);
|
|
|
|
|
2017-01-12 00:42:07 +00:00
|
|
|
if (options.use_direct_reads && !options.use_mmap_reads) {
|
2017-02-16 18:25:06 +00:00
|
|
|
#ifdef ROCKSDB_LITE
|
|
|
|
return Status::IOError(fname, "Direct I/O not supported in RocksDB lite");
|
|
|
|
#endif // !ROCKSDB_LITE
|
2017-04-22 03:41:37 +00:00
|
|
|
#if !defined(OS_MACOSX) && !defined(OS_OPENBSD) && !defined(OS_SOLARIS)
|
2017-01-12 00:42:07 +00:00
|
|
|
flags |= O_DIRECT;
|
|
|
|
TEST_SYNC_POINT_CALLBACK("NewRandomAccessFile:O_DIRECT", &flags);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2015-07-03 00:23:41 +00:00
|
|
|
IOSTATS_TIMER_GUARD(open_nanos);
|
2018-04-13 20:05:28 +00:00
|
|
|
fd = open(fname.c_str(), flags, GetDBFileMode(allow_non_owner_access_));
|
2017-01-12 00:42:07 +00:00
|
|
|
} while (fd < 0 && errno == EINTR);
|
|
|
|
if (fd < 0) {
|
2017-06-26 19:42:21 +00:00
|
|
|
return IOError("While open a file for random read", fname, errno);
|
2015-07-03 00:23:41 +00:00
|
|
|
}
|
2013-04-10 02:42:07 +00:00
|
|
|
SetFD_CLOEXEC(fd, &options);
|
2017-01-12 00:42:07 +00:00
|
|
|
|
|
|
|
if (options.use_mmap_reads && sizeof(void*) >= 8) {
|
2012-09-15 03:57:15 +00:00
|
|
|
// Use of mmap for random reads has been removed because it
|
|
|
|
// kills performance when storage is fast.
|
|
|
|
// Use mmap when virtual address-space is plentiful.
|
|
|
|
uint64_t size;
|
|
|
|
s = GetFileSize(fname, &size);
|
|
|
|
if (s.ok()) {
|
2013-03-01 02:04:58 +00:00
|
|
|
void* base = mmap(nullptr, size, PROT_READ, MAP_SHARED, fd, 0);
|
2012-09-15 03:57:15 +00:00
|
|
|
if (base != MAP_FAILED) {
|
2013-09-21 06:00:13 +00:00
|
|
|
result->reset(new PosixMmapReadableFile(fd, fname, base,
|
|
|
|
size, options));
|
2012-09-15 03:57:15 +00:00
|
|
|
} else {
|
2017-06-26 19:42:21 +00:00
|
|
|
s = IOError("while mmap file for read", fname, errno);
|
2018-05-25 17:47:56 +00:00
|
|
|
close(fd);
|
2012-09-15 03:57:15 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-12 00:42:07 +00:00
|
|
|
} else {
|
|
|
|
if (options.use_direct_reads && !options.use_mmap_reads) {
|
2016-05-23 23:05:53 +00:00
|
|
|
#ifdef OS_MACOSX
|
|
|
|
if (fcntl(fd, F_NOCACHE, 1) == -1) {
|
|
|
|
close(fd);
|
2017-06-26 19:42:21 +00:00
|
|
|
return IOError("while fcntl NoCache", fname, errno);
|
2016-05-23 23:05:53 +00:00
|
|
|
}
|
|
|
|
#endif
|
2016-04-21 17:37:27 +00:00
|
|
|
}
|
2013-03-15 00:00:04 +00:00
|
|
|
result->reset(new PosixRandomAccessFile(fname, fd, options));
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2012-03-15 16:14:00 +00:00
|
|
|
return s;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2017-05-10 21:54:35 +00:00
|
|
|
virtual Status OpenWritableFile(const std::string& fname,
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<WritableFile>* result,
|
2017-05-10 21:54:35 +00:00
|
|
|
const EnvOptions& options,
|
|
|
|
bool reopen = false) {
|
2013-01-20 10:07:13 +00:00
|
|
|
result->reset();
|
2011-03-18 22:37:00 +00:00
|
|
|
Status s;
|
2014-03-31 21:45:26 +00:00
|
|
|
int fd = -1;
|
2017-05-10 21:54:35 +00:00
|
|
|
int flags = (reopen) ? (O_CREAT | O_APPEND) : (O_CREAT | O_TRUNC);
|
2016-12-22 20:51:29 +00:00
|
|
|
// Direct IO mode with O_DIRECT flag or F_NOCAHCE (MAC OSX)
|
|
|
|
if (options.use_direct_writes && !options.use_mmap_writes) {
|
|
|
|
// Note: we should avoid O_APPEND here due to ta the following bug:
|
|
|
|
// POSIX requires that opening a file with the O_APPEND flag should
|
|
|
|
// have no affect on the location at which pwrite() writes data.
|
|
|
|
// However, on Linux, if a file is opened with O_APPEND, pwrite()
|
|
|
|
// appends data to the end of the file, regardless of the value of
|
|
|
|
// offset.
|
|
|
|
// More info here: https://linux.die.net/man/2/pwrite
|
2017-02-16 18:25:06 +00:00
|
|
|
#ifdef ROCKSDB_LITE
|
|
|
|
return Status::IOError(fname, "Direct I/O not supported in RocksDB lite");
|
2017-02-28 19:05:08 +00:00
|
|
|
#endif // ROCKSDB_LITE
|
2016-12-22 20:51:29 +00:00
|
|
|
flags |= O_WRONLY;
|
2017-04-22 03:41:37 +00:00
|
|
|
#if !defined(OS_MACOSX) && !defined(OS_OPENBSD) && !defined(OS_SOLARIS)
|
2016-12-22 20:51:29 +00:00
|
|
|
flags |= O_DIRECT;
|
|
|
|
#endif
|
|
|
|
TEST_SYNC_POINT_CALLBACK("NewWritableFile:O_DIRECT", &flags);
|
|
|
|
} else if (options.use_mmap_writes) {
|
|
|
|
// non-direct I/O
|
|
|
|
flags |= O_RDWR;
|
|
|
|
} else {
|
|
|
|
flags |= O_WRONLY;
|
|
|
|
}
|
|
|
|
|
2018-08-30 03:24:17 +00:00
|
|
|
flags = cloexec_flags(flags, &options);
|
|
|
|
|
2014-03-31 21:45:26 +00:00
|
|
|
do {
|
2015-07-03 00:23:41 +00:00
|
|
|
IOSTATS_TIMER_GUARD(open_nanos);
|
2018-04-13 20:05:28 +00:00
|
|
|
fd = open(fname.c_str(), flags, GetDBFileMode(allow_non_owner_access_));
|
2014-03-31 21:45:26 +00:00
|
|
|
} while (fd < 0 && errno == EINTR);
|
2016-12-22 20:51:29 +00:00
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
if (fd < 0) {
|
2017-06-26 19:42:21 +00:00
|
|
|
s = IOError("While open a file for appending", fname, errno);
|
2016-12-22 20:51:29 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
SetFD_CLOEXEC(fd, &options);
|
|
|
|
|
|
|
|
if (options.use_mmap_writes) {
|
|
|
|
if (!checkedDiskForMmap_) {
|
|
|
|
// this will be executed once in the program's lifetime.
|
|
|
|
// do not use mmapWrite on non ext-3/xfs/tmpfs systems.
|
|
|
|
if (!SupportsFastAllocate(fname)) {
|
|
|
|
forceMmapOff_ = true;
|
2013-03-13 20:50:26 +00:00
|
|
|
}
|
2016-12-22 20:51:29 +00:00
|
|
|
checkedDiskForMmap_ = true;
|
2013-03-13 20:50:26 +00:00
|
|
|
}
|
2016-12-22 20:51:29 +00:00
|
|
|
}
|
|
|
|
if (options.use_mmap_writes && !forceMmapOff_) {
|
|
|
|
result->reset(new PosixMmapFile(fname, fd, page_size_, options));
|
|
|
|
} else if (options.use_direct_writes && !options.use_mmap_writes) {
|
2016-05-23 23:05:53 +00:00
|
|
|
#ifdef OS_MACOSX
|
2016-12-22 20:51:29 +00:00
|
|
|
if (fcntl(fd, F_NOCACHE, 1) == -1) {
|
|
|
|
close(fd);
|
2017-06-26 19:42:21 +00:00
|
|
|
s = IOError("While fcntl NoCache an opened file for appending", fname,
|
|
|
|
errno);
|
2016-12-22 20:51:29 +00:00
|
|
|
return s;
|
2012-10-01 22:41:44 +00:00
|
|
|
}
|
2017-04-22 03:41:37 +00:00
|
|
|
#elif defined(OS_SOLARIS)
|
|
|
|
if (directio(fd, DIRECTIO_ON) == -1) {
|
|
|
|
if (errno != ENOTTY) { // ZFS filesystems don't support DIRECTIO_ON
|
|
|
|
close(fd);
|
2017-06-26 19:42:21 +00:00
|
|
|
s = IOError("While calling directio()", fname, errno);
|
2017-04-22 03:41:37 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
2016-12-22 20:51:29 +00:00
|
|
|
#endif
|
|
|
|
result->reset(new PosixWritableFile(fname, fd, options));
|
|
|
|
} else {
|
|
|
|
// disable mmap writes
|
|
|
|
EnvOptions no_mmap_writes_options = options;
|
|
|
|
no_mmap_writes_options.use_mmap_writes = false;
|
|
|
|
result->reset(new PosixWritableFile(fname, fd, no_mmap_writes_options));
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
Status NewWritableFile(const std::string& fname,
|
|
|
|
std::unique_ptr<WritableFile>* result,
|
|
|
|
const EnvOptions& options) override {
|
2017-05-10 21:54:35 +00:00
|
|
|
return OpenWritableFile(fname, result, options, false);
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
Status ReopenWritableFile(const std::string& fname,
|
|
|
|
std::unique_ptr<WritableFile>* result,
|
|
|
|
const EnvOptions& options) override {
|
2017-05-10 21:54:35 +00:00
|
|
|
return OpenWritableFile(fname, result, options, true);
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
Status ReuseWritableFile(const std::string& fname,
|
|
|
|
const std::string& old_fname,
|
|
|
|
std::unique_ptr<WritableFile>* result,
|
|
|
|
const EnvOptions& options) override {
|
2015-10-08 02:11:09 +00:00
|
|
|
result->reset();
|
|
|
|
Status s;
|
|
|
|
int fd = -1;
|
2016-12-22 20:51:29 +00:00
|
|
|
|
|
|
|
int flags = 0;
|
|
|
|
// Direct IO mode with O_DIRECT flag or F_NOCAHCE (MAC OSX)
|
|
|
|
if (options.use_direct_writes && !options.use_mmap_writes) {
|
2017-02-16 18:25:06 +00:00
|
|
|
#ifdef ROCKSDB_LITE
|
|
|
|
return Status::IOError(fname, "Direct I/O not supported in RocksDB lite");
|
|
|
|
#endif // !ROCKSDB_LITE
|
2016-12-22 20:51:29 +00:00
|
|
|
flags |= O_WRONLY;
|
2017-04-22 03:41:37 +00:00
|
|
|
#if !defined(OS_MACOSX) && !defined(OS_OPENBSD) && !defined(OS_SOLARIS)
|
2016-12-22 20:51:29 +00:00
|
|
|
flags |= O_DIRECT;
|
|
|
|
#endif
|
|
|
|
TEST_SYNC_POINT_CALLBACK("NewWritableFile:O_DIRECT", &flags);
|
|
|
|
} else if (options.use_mmap_writes) {
|
|
|
|
// mmap needs O_RDWR mode
|
|
|
|
flags |= O_RDWR;
|
|
|
|
} else {
|
|
|
|
flags |= O_WRONLY;
|
|
|
|
}
|
|
|
|
|
2018-08-30 03:24:17 +00:00
|
|
|
flags = cloexec_flags(flags, &options);
|
|
|
|
|
2015-10-08 02:11:09 +00:00
|
|
|
do {
|
|
|
|
IOSTATS_TIMER_GUARD(open_nanos);
|
2018-04-13 20:05:28 +00:00
|
|
|
fd = open(old_fname.c_str(), flags,
|
|
|
|
GetDBFileMode(allow_non_owner_access_));
|
2015-10-08 02:11:09 +00:00
|
|
|
} while (fd < 0 && errno == EINTR);
|
|
|
|
if (fd < 0) {
|
2017-06-26 19:42:21 +00:00
|
|
|
s = IOError("while reopen file for write", fname, errno);
|
2016-12-22 20:51:29 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetFD_CLOEXEC(fd, &options);
|
|
|
|
// rename into place
|
|
|
|
if (rename(old_fname.c_str(), fname.c_str()) != 0) {
|
2017-06-26 19:42:21 +00:00
|
|
|
s = IOError("while rename file to " + fname, old_fname, errno);
|
2016-12-22 20:51:29 +00:00
|
|
|
close(fd);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.use_mmap_writes) {
|
|
|
|
if (!checkedDiskForMmap_) {
|
|
|
|
// this will be executed once in the program's lifetime.
|
|
|
|
// do not use mmapWrite on non ext-3/xfs/tmpfs systems.
|
|
|
|
if (!SupportsFastAllocate(fname)) {
|
|
|
|
forceMmapOff_ = true;
|
2015-10-08 02:11:09 +00:00
|
|
|
}
|
2016-12-22 20:51:29 +00:00
|
|
|
checkedDiskForMmap_ = true;
|
2015-10-08 02:11:09 +00:00
|
|
|
}
|
2016-12-22 20:51:29 +00:00
|
|
|
}
|
|
|
|
if (options.use_mmap_writes && !forceMmapOff_) {
|
|
|
|
result->reset(new PosixMmapFile(fname, fd, page_size_, options));
|
|
|
|
} else if (options.use_direct_writes && !options.use_mmap_writes) {
|
|
|
|
#ifdef OS_MACOSX
|
|
|
|
if (fcntl(fd, F_NOCACHE, 1) == -1) {
|
|
|
|
close(fd);
|
2017-06-26 19:42:21 +00:00
|
|
|
s = IOError("while fcntl NoCache for reopened file for append", fname,
|
|
|
|
errno);
|
2016-12-22 20:51:29 +00:00
|
|
|
return s;
|
2015-10-08 02:11:09 +00:00
|
|
|
}
|
2017-04-22 03:41:37 +00:00
|
|
|
#elif defined(OS_SOLARIS)
|
|
|
|
if (directio(fd, DIRECTIO_ON) == -1) {
|
|
|
|
if (errno != ENOTTY) { // ZFS filesystems don't support DIRECTIO_ON
|
|
|
|
close(fd);
|
2017-06-26 19:42:21 +00:00
|
|
|
s = IOError("while calling directio()", fname, errno);
|
2017-04-22 03:41:37 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
2016-12-22 20:51:29 +00:00
|
|
|
#endif
|
|
|
|
result->reset(new PosixWritableFile(fname, fd, options));
|
|
|
|
} else {
|
|
|
|
// disable mmap writes
|
|
|
|
EnvOptions no_mmap_writes_options = options;
|
|
|
|
no_mmap_writes_options.use_mmap_writes = false;
|
|
|
|
result->reset(new PosixWritableFile(fname, fd, no_mmap_writes_options));
|
2015-10-08 02:11:09 +00:00
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
Status NewRandomRWFile(const std::string& fname,
|
|
|
|
std::unique_ptr<RandomRWFile>* result,
|
|
|
|
const EnvOptions& options) override {
|
2016-09-13 19:08:22 +00:00
|
|
|
int fd = -1;
|
2018-08-30 03:24:17 +00:00
|
|
|
int flags = cloexec_flags(O_RDWR, &options);
|
|
|
|
|
2016-09-13 19:08:22 +00:00
|
|
|
while (fd < 0) {
|
|
|
|
IOSTATS_TIMER_GUARD(open_nanos);
|
2018-08-30 03:24:17 +00:00
|
|
|
|
|
|
|
fd = open(fname.c_str(), flags, GetDBFileMode(allow_non_owner_access_));
|
2016-09-13 19:08:22 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
// Error while opening the file
|
|
|
|
if (errno == EINTR) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-06-26 19:42:21 +00:00
|
|
|
return IOError("While open file for random read/write", fname, errno);
|
2016-09-13 19:08:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SetFD_CLOEXEC(fd, &options);
|
|
|
|
result->reset(new PosixRandomRWFile(fname, fd, options));
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
Status NewMemoryMappedFileBuffer(
|
2018-04-30 19:23:45 +00:00
|
|
|
const std::string& fname,
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<MemoryMappedFileBuffer>* result) override {
|
2018-04-30 19:23:45 +00:00
|
|
|
int fd = -1;
|
|
|
|
Status status;
|
2018-08-30 03:24:17 +00:00
|
|
|
int flags = cloexec_flags(O_RDWR, nullptr);
|
|
|
|
|
2018-04-30 19:23:45 +00:00
|
|
|
while (fd < 0) {
|
|
|
|
IOSTATS_TIMER_GUARD(open_nanos);
|
2018-08-30 03:24:17 +00:00
|
|
|
fd = open(fname.c_str(), flags, 0644);
|
2018-04-30 19:23:45 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
// Error while opening the file
|
|
|
|
if (errno == EINTR) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
status =
|
|
|
|
IOError("While open file for raw mmap buffer access", fname, errno);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uint64_t size;
|
|
|
|
if (status.ok()) {
|
|
|
|
status = GetFileSize(fname, &size);
|
|
|
|
}
|
2018-05-01 20:18:01 +00:00
|
|
|
void* base = nullptr;
|
2018-04-30 19:23:45 +00:00
|
|
|
if (status.ok()) {
|
|
|
|
base = mmap(nullptr, static_cast<size_t>(size), PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED, fd, 0);
|
|
|
|
if (base == MAP_FAILED) {
|
|
|
|
status = IOError("while mmap file for read", fname, errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (status.ok()) {
|
|
|
|
result->reset(
|
|
|
|
new PosixMemoryMappedFileBuffer(base, static_cast<size_t>(size)));
|
|
|
|
}
|
|
|
|
if (fd >= 0) {
|
|
|
|
// don't need to keep it open after mmap has been called
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
Status NewDirectory(const std::string& name,
|
|
|
|
std::unique_ptr<Directory>* result) override {
|
2014-01-27 19:02:21 +00:00
|
|
|
result->reset();
|
2015-07-03 00:23:41 +00:00
|
|
|
int fd;
|
2018-08-30 03:24:17 +00:00
|
|
|
int flags = cloexec_flags(0, nullptr);
|
2015-07-03 00:23:41 +00:00
|
|
|
{
|
|
|
|
IOSTATS_TIMER_GUARD(open_nanos);
|
2018-08-30 03:24:17 +00:00
|
|
|
fd = open(name.c_str(), flags);
|
2015-07-03 00:23:41 +00:00
|
|
|
}
|
2014-01-27 19:02:21 +00:00
|
|
|
if (fd < 0) {
|
2017-06-26 19:42:21 +00:00
|
|
|
return IOError("While open directory", name, errno);
|
2014-01-27 19:02:21 +00:00
|
|
|
} else {
|
|
|
|
result->reset(new PosixDirectory(fd));
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
Status FileExists(const std::string& fname) override {
|
2015-07-21 00:20:40 +00:00
|
|
|
int result = access(fname.c_str(), F_OK);
|
|
|
|
|
|
|
|
if (result == 0) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2018-09-26 20:19:22 +00:00
|
|
|
int err = errno;
|
|
|
|
switch (err) {
|
2015-07-21 00:20:40 +00:00
|
|
|
case EACCES:
|
|
|
|
case ELOOP:
|
|
|
|
case ENAMETOOLONG:
|
|
|
|
case ENOENT:
|
|
|
|
case ENOTDIR:
|
|
|
|
return Status::NotFound();
|
|
|
|
default:
|
2018-09-26 20:19:22 +00:00
|
|
|
assert(err == EIO || err == ENOMEM);
|
|
|
|
return Status::IOError("Unexpected error(" + ToString(err) +
|
2015-07-21 00:20:40 +00:00
|
|
|
") accessing file `" + fname + "' ");
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
Status GetChildren(const std::string& dir,
|
|
|
|
std::vector<std::string>* result) override {
|
2011-03-18 22:37:00 +00:00
|
|
|
result->clear();
|
|
|
|
DIR* d = opendir(dir.c_str());
|
2013-03-01 02:04:58 +00:00
|
|
|
if (d == nullptr) {
|
2016-12-12 20:38:43 +00:00
|
|
|
switch (errno) {
|
|
|
|
case EACCES:
|
|
|
|
case ENOENT:
|
|
|
|
case ENOTDIR:
|
|
|
|
return Status::NotFound();
|
|
|
|
default:
|
2017-06-26 19:42:21 +00:00
|
|
|
return IOError("While opendir", dir, errno);
|
2016-12-12 20:38:43 +00:00
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
struct dirent* entry;
|
2013-03-01 02:04:58 +00:00
|
|
|
while ((entry = readdir(d)) != nullptr) {
|
2011-03-18 22:37:00 +00:00
|
|
|
result->push_back(entry->d_name);
|
|
|
|
}
|
|
|
|
closedir(d);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
Status DeleteFile(const std::string& fname) override {
|
2011-03-18 22:37:00 +00:00
|
|
|
Status result;
|
|
|
|
if (unlink(fname.c_str()) != 0) {
|
2017-06-26 19:42:21 +00:00
|
|
|
result = IOError("while unlink() file", fname, errno);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
Status CreateDir(const std::string& name) override {
|
2011-03-18 22:37:00 +00:00
|
|
|
Status result;
|
|
|
|
if (mkdir(name.c_str(), 0755) != 0) {
|
2017-06-26 19:42:21 +00:00
|
|
|
result = IOError("While mkdir", name, errno);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
Status CreateDirIfMissing(const std::string& name) override {
|
2012-11-26 21:56:45 +00:00
|
|
|
Status result;
|
|
|
|
if (mkdir(name.c_str(), 0755) != 0) {
|
|
|
|
if (errno != EEXIST) {
|
2017-06-26 19:42:21 +00:00
|
|
|
result = IOError("While mkdir if missing", name, errno);
|
2013-01-07 18:11:18 +00:00
|
|
|
} else if (!DirExists(name)) { // Check that name is actually a
|
|
|
|
// directory.
|
|
|
|
// Message is taken from mkdir
|
|
|
|
result = Status::IOError("`"+name+"' exists but is not a directory");
|
2012-11-26 21:56:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
Status DeleteDir(const std::string& name) override {
|
2011-03-18 22:37:00 +00:00
|
|
|
Status result;
|
|
|
|
if (rmdir(name.c_str()) != 0) {
|
2017-06-26 19:42:21 +00:00
|
|
|
result = IOError("file rmdir", name, errno);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
Status GetFileSize(const std::string& fname, uint64_t* size) override {
|
2011-03-18 22:37:00 +00:00
|
|
|
Status s;
|
|
|
|
struct stat sbuf;
|
|
|
|
if (stat(fname.c_str(), &sbuf) != 0) {
|
|
|
|
*size = 0;
|
2017-06-26 19:42:21 +00:00
|
|
|
s = IOError("while stat a file for size", fname, errno);
|
2011-03-18 22:37:00 +00:00
|
|
|
} else {
|
|
|
|
*size = sbuf.st_size;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
Status GetFileModificationTime(const std::string& fname,
|
|
|
|
uint64_t* file_mtime) override {
|
2012-11-26 21:56:45 +00:00
|
|
|
struct stat s;
|
|
|
|
if (stat(fname.c_str(), &s) !=0) {
|
2017-06-26 19:42:21 +00:00
|
|
|
return IOError("while stat a file for modification time", fname, errno);
|
2012-11-26 21:56:45 +00:00
|
|
|
}
|
|
|
|
*file_mtime = static_cast<uint64_t>(s.st_mtime);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2019-02-19 21:36:04 +00:00
|
|
|
Status RenameFile(const std::string& src,
|
|
|
|
const std::string& target) override {
|
2011-03-18 22:37:00 +00:00
|
|
|
Status result;
|
|
|
|
if (rename(src.c_str(), target.c_str()) != 0) {
|
2017-06-26 19:42:21 +00:00
|
|
|
result = IOError("While renaming a file to " + target, src, errno);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
Status LinkFile(const std::string& src, const std::string& target) override {
|
2014-11-14 19:38:26 +00:00
|
|
|
Status result;
|
|
|
|
if (link(src.c_str(), target.c_str()) != 0) {
|
|
|
|
if (errno == EXDEV) {
|
|
|
|
return Status::NotSupported("No cross FS links allowed");
|
|
|
|
}
|
2017-06-26 19:42:21 +00:00
|
|
|
result = IOError("while link file to " + target, src, errno);
|
2014-11-14 19:38:26 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-07-09 22:17:38 +00:00
|
|
|
Status NumFileLinks(const std::string& fname, uint64_t* count) override {
|
|
|
|
struct stat s;
|
|
|
|
if (stat(fname.c_str(), &s) != 0) {
|
|
|
|
return IOError("while stat a file for num file links", fname, errno);
|
|
|
|
}
|
|
|
|
*count = static_cast<uint64_t>(s.st_nlink);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
Status AreFilesSame(const std::string& first, const std::string& second,
|
|
|
|
bool* res) override {
|
2017-09-22 19:37:59 +00:00
|
|
|
struct stat statbuf[2];
|
|
|
|
if (stat(first.c_str(), &statbuf[0]) != 0) {
|
|
|
|
return IOError("stat file", first, errno);
|
|
|
|
}
|
|
|
|
if (stat(second.c_str(), &statbuf[1]) != 0) {
|
|
|
|
return IOError("stat file", second, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (major(statbuf[0].st_dev) != major(statbuf[1].st_dev) ||
|
|
|
|
minor(statbuf[0].st_dev) != minor(statbuf[1].st_dev) ||
|
|
|
|
statbuf[0].st_ino != statbuf[1].st_ino) {
|
|
|
|
*res = false;
|
|
|
|
} else {
|
|
|
|
*res = true;
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
Status LockFile(const std::string& fname, FileLock** lock) override {
|
2013-03-01 02:04:58 +00:00
|
|
|
*lock = nullptr;
|
2011-03-18 22:37:00 +00:00
|
|
|
Status result;
|
2018-06-14 00:28:31 +00:00
|
|
|
|
|
|
|
mutex_lockedFiles.Lock();
|
|
|
|
// If it already exists in the lockedFiles set, then it is already locked,
|
|
|
|
// and fail this lock attempt. Otherwise, insert it into lockedFiles.
|
|
|
|
// This check is needed because fcntl() does not detect lock conflict
|
|
|
|
// if the fcntl is issued by the same thread that earlier acquired
|
|
|
|
// this lock.
|
|
|
|
// We must do this check *before* opening the file:
|
|
|
|
// Otherwise, we will open a new file descriptor. Locks are associated with
|
|
|
|
// a process, not a file descriptor and when *any* file descriptor is closed,
|
|
|
|
// all locks the process holds for that *file* are released
|
|
|
|
if (lockedFiles.insert(fname).second == false) {
|
|
|
|
mutex_lockedFiles.Unlock();
|
|
|
|
errno = ENOLCK;
|
|
|
|
return IOError("lock ", fname, errno);
|
|
|
|
}
|
|
|
|
|
2015-07-03 00:23:41 +00:00
|
|
|
int fd;
|
2018-08-30 03:24:17 +00:00
|
|
|
int flags = cloexec_flags(O_RDWR | O_CREAT, nullptr);
|
|
|
|
|
2015-07-03 00:23:41 +00:00
|
|
|
{
|
|
|
|
IOSTATS_TIMER_GUARD(open_nanos);
|
2018-08-30 03:24:17 +00:00
|
|
|
fd = open(fname.c_str(), flags, 0644);
|
2015-07-03 00:23:41 +00:00
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
if (fd < 0) {
|
2017-06-26 19:42:21 +00:00
|
|
|
result = IOError("while open a file for lock", fname, errno);
|
2018-06-14 00:28:31 +00:00
|
|
|
} else if (LockOrUnlock(fd, true) == -1) {
|
|
|
|
// if there is an error in locking, then remove the pathname from lockedfiles
|
|
|
|
lockedFiles.erase(fname);
|
2017-06-26 19:42:21 +00:00
|
|
|
result = IOError("While lock file", fname, errno);
|
2011-03-18 22:37:00 +00:00
|
|
|
close(fd);
|
|
|
|
} else {
|
2013-04-10 02:42:07 +00:00
|
|
|
SetFD_CLOEXEC(fd, nullptr);
|
2011-03-18 22:37:00 +00:00
|
|
|
PosixFileLock* my_lock = new PosixFileLock;
|
|
|
|
my_lock->fd_ = fd;
|
2012-08-18 07:26:50 +00:00
|
|
|
my_lock->filename = fname;
|
2011-03-18 22:37:00 +00:00
|
|
|
*lock = my_lock;
|
|
|
|
}
|
2018-06-14 00:28:31 +00:00
|
|
|
|
|
|
|
mutex_lockedFiles.Unlock();
|
2011-03-18 22:37:00 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
Status UnlockFile(FileLock* lock) override {
|
2011-03-18 22:37:00 +00:00
|
|
|
PosixFileLock* my_lock = reinterpret_cast<PosixFileLock*>(lock);
|
|
|
|
Status result;
|
2018-06-14 00:28:31 +00:00
|
|
|
mutex_lockedFiles.Lock();
|
|
|
|
// If we are unlocking, then verify that we had locked it earlier,
|
|
|
|
// it should already exist in lockedFiles. Remove it from lockedFiles.
|
|
|
|
if (lockedFiles.erase(my_lock->filename) != 1) {
|
|
|
|
errno = ENOLCK;
|
|
|
|
result = IOError("unlock", my_lock->filename, errno);
|
|
|
|
} else if (LockOrUnlock(my_lock->fd_, false) == -1) {
|
2017-06-26 19:42:21 +00:00
|
|
|
result = IOError("unlock", my_lock->filename, errno);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
close(my_lock->fd_);
|
|
|
|
delete my_lock;
|
2018-06-14 00:28:31 +00:00
|
|
|
mutex_lockedFiles.Unlock();
|
2011-03-18 22:37:00 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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 GetTestDirectory(std::string* result) override {
|
2011-03-18 22:37:00 +00:00
|
|
|
const char* env = getenv("TEST_TMPDIR");
|
|
|
|
if (env && env[0] != '\0') {
|
|
|
|
*result = env;
|
|
|
|
} else {
|
|
|
|
char buf[100];
|
2013-10-05 05:32:05 +00:00
|
|
|
snprintf(buf, sizeof(buf), "/tmp/rocksdbtest-%d", int(geteuid()));
|
2011-03-18 22:37:00 +00:00
|
|
|
*result = buf;
|
|
|
|
}
|
|
|
|
// Directory may already exist
|
|
|
|
CreateDir(*result);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
Status GetFreeSpace(const std::string& fname, uint64_t* free_space) override {
|
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
|
|
|
struct statvfs sbuf;
|
|
|
|
|
|
|
|
if (statvfs(fname.c_str(), &sbuf) < 0) {
|
|
|
|
return IOError("While doing statvfs", fname, errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
*free_space = ((uint64_t)sbuf.f_bsize * sbuf.f_bfree);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
Status NewLogger(const std::string& fname,
|
|
|
|
std::shared_ptr<Logger>* result) override {
|
2015-07-03 00:23:41 +00:00
|
|
|
FILE* f;
|
|
|
|
{
|
|
|
|
IOSTATS_TIMER_GUARD(open_nanos);
|
2018-08-30 03:24:17 +00:00
|
|
|
f = fopen(fname.c_str(), "w"
|
|
|
|
#ifdef __GLIBC_PREREQ
|
|
|
|
#if __GLIBC_PREREQ(2, 7)
|
|
|
|
"e" // glibc extension to enable O_CLOEXEC
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
);
|
2015-07-03 00:23:41 +00:00
|
|
|
}
|
2013-03-01 02:04:58 +00:00
|
|
|
if (f == nullptr) {
|
2013-01-20 10:07:13 +00:00
|
|
|
result->reset();
|
2017-06-26 19:42:21 +00:00
|
|
|
return IOError("when fopen a file for new logger", fname, errno);
|
2011-07-21 02:40:18 +00:00
|
|
|
} else {
|
2013-04-10 02:42:07 +00:00
|
|
|
int fd = fileno(f);
|
t6913679: Use fallocate on LOG FILESS
Summary: Use fallocate on LOG FILES to
Test Plan:
make check
+
===check with strace===
[arya@devvm1441 ~/rocksdb] strace -e trace=fallocate ./ldb --db=/tmp/test_new scan
fallocate(3, 01, 0, 4194304) = 0
Reviewers: sdong, anthony, IslamAbdelRahman, kradhakrishnan, lgalanis, rven, igor
Reviewed By: igor
Subscribers: leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D45969
2015-09-02 18:17:02 +00:00
|
|
|
#ifdef ROCKSDB_FALLOCATE_PRESENT
|
2015-10-07 17:04:05 +00:00
|
|
|
fallocate(fd, FALLOC_FL_KEEP_SIZE, 0, 4 * 1024);
|
t6913679: Use fallocate on LOG FILESS
Summary: Use fallocate on LOG FILES to
Test Plan:
make check
+
===check with strace===
[arya@devvm1441 ~/rocksdb] strace -e trace=fallocate ./ldb --db=/tmp/test_new scan
fallocate(3, 01, 0, 4194304) = 0
Reviewers: sdong, anthony, IslamAbdelRahman, kradhakrishnan, lgalanis, rven, igor
Reviewed By: igor
Subscribers: leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D45969
2015-09-02 18:17:02 +00:00
|
|
|
#endif
|
2013-04-10 02:42:07 +00:00
|
|
|
SetFD_CLOEXEC(fd, nullptr);
|
2013-10-31 22:36:40 +00:00
|
|
|
result->reset(new PosixLogger(f, &PosixEnv::gettid, this));
|
2011-07-21 02:40:18 +00:00
|
|
|
return Status::OK();
|
2011-03-18 22:37:00 +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 {
|
2017-04-22 03:41:37 +00:00
|
|
|
#if defined(OS_LINUX) || defined(OS_FREEBSD) || 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 {
|
2019-01-30 00:23:21 +00:00
|
|
|
#if defined(OS_LINUX) || defined(OS_FREEBSD) || defined(OS_AIX) || \
|
|
|
|
defined(__MACH__)
|
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) {
|
|
|
|
if (errno == EFAULT || errno == EINVAL)
|
|
|
|
return Status::InvalidArgument(strerror(errno));
|
|
|
|
else
|
2017-06-26 19:42:21 +00:00
|
|
|
return IOError("GetHostName", name, errno);
|
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();
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
Status GetAbsolutePath(const std::string& db_path,
|
|
|
|
std::string* output_path) override {
|
2018-02-16 00:43:23 +00:00
|
|
|
if (!db_path.empty() && db_path[0] == '/') {
|
2012-08-14 22:20:36 +00:00
|
|
|
*output_path = db_path;
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
char the_path[256];
|
|
|
|
char* ret = getcwd(the_path, 256);
|
2013-03-01 02:04:58 +00:00
|
|
|
if (ret == nullptr) {
|
2012-08-14 22:20:36 +00:00
|
|
|
return Status::IOError(strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
*output_path = ret;
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
void LowerThreadPoolIOPriority(Priority pool = LOW) 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
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
void LowerThreadPoolCPUPriority(Priority pool = LOW) override {
|
2018-04-24 15:38:01 +00:00
|
|
|
assert(pool >= Priority::BOTTOM && pool <= Priority::HIGH);
|
|
|
|
#ifdef OS_LINUX
|
|
|
|
thread_pools_[pool].LowerCPUPriority();
|
2018-04-13 00:55:14 +00:00
|
|
|
#else
|
|
|
|
(void)pool;
|
2014-08-14 03:49:58 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-05-19 00:03:59 +00:00
|
|
|
EnvOptions OptimizeForLogWrite(const EnvOptions& env_options,
|
|
|
|
const DBOptions& db_options) const override {
|
2014-03-18 04:52:14 +00:00
|
|
|
EnvOptions optimized = env_options;
|
|
|
|
optimized.use_mmap_writes = false;
|
2016-12-22 20:51:29 +00:00
|
|
|
optimized.use_direct_writes = false;
|
2015-05-19 00:03:59 +00:00
|
|
|
optimized.bytes_per_sync = db_options.wal_bytes_per_sync;
|
2014-03-28 22:04:11 +00:00
|
|
|
// TODO(icanadi) it's faster if fallocate_with_keep_size is false, but it
|
|
|
|
// breaks TransactionLogIteratorStallAtLastRecord unit test. Fix the unit
|
|
|
|
// test and make this false
|
|
|
|
optimized.fallocate_with_keep_size = true;
|
2017-10-31 20:49:25 +00:00
|
|
|
optimized.writable_file_max_buffer_size =
|
|
|
|
db_options.writable_file_max_buffer_size;
|
2014-03-18 04:52:14 +00:00
|
|
|
return optimized;
|
|
|
|
}
|
|
|
|
|
2015-02-26 19:28:41 +00:00
|
|
|
EnvOptions OptimizeForManifestWrite(
|
|
|
|
const EnvOptions& env_options) const override {
|
2014-03-18 04:52:14 +00:00
|
|
|
EnvOptions optimized = env_options;
|
|
|
|
optimized.use_mmap_writes = false;
|
2016-12-22 20:51:29 +00:00
|
|
|
optimized.use_direct_writes = false;
|
2014-03-18 04:52:14 +00:00
|
|
|
optimized.fallocate_with_keep_size = true;
|
|
|
|
return optimized;
|
|
|
|
}
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
private:
|
2013-03-21 18:12:42 +00:00
|
|
|
bool checkedDiskForMmap_;
|
2016-12-22 20:51:29 +00:00
|
|
|
bool forceMmapOff_; // do we override Env options?
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2013-01-07 18:11:18 +00:00
|
|
|
// Returns true iff the named directory exists and is a directory.
|
|
|
|
virtual bool DirExists(const std::string& dname) {
|
|
|
|
struct stat statbuf;
|
|
|
|
if (stat(dname.c_str(), &statbuf) == 0) {
|
|
|
|
return S_ISDIR(statbuf.st_mode);
|
|
|
|
}
|
|
|
|
return false; // stat() failed return false
|
|
|
|
}
|
|
|
|
|
2013-03-13 20:50:26 +00:00
|
|
|
bool SupportsFastAllocate(const std::string& path) {
|
2013-12-11 19:18:00 +00:00
|
|
|
#ifdef ROCKSDB_FALLOCATE_PRESENT
|
2013-03-13 20:50:26 +00:00
|
|
|
struct statfs s;
|
|
|
|
if (statfs(path.c_str(), &s)){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
switch (s.f_type) {
|
|
|
|
case EXT4_SUPER_MAGIC:
|
|
|
|
return true;
|
|
|
|
case XFS_SUPER_MAGIC:
|
|
|
|
return true;
|
|
|
|
case TMPFS_MAGIC:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-17 07:44:39 +00:00
|
|
|
#else
|
2018-04-13 00:55:14 +00:00
|
|
|
(void)path;
|
2013-11-17 07:44:39 +00:00
|
|
|
return false;
|
|
|
|
#endif
|
2013-03-13 20:50:26 +00:00
|
|
|
}
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
size_t page_size_;
|
|
|
|
|
2016-08-26 17:41:35 +00:00
|
|
|
std::vector<ThreadPoolImpl> thread_pools_;
|
2013-09-12 07:53:30 +00:00
|
|
|
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.
|
|
|
|
bool allow_non_owner_access_;
|
2013-09-12 07:53:30 +00:00
|
|
|
};
|
|
|
|
|
2015-10-07 17:04:05 +00:00
|
|
|
PosixEnv::PosixEnv()
|
|
|
|
: checkedDiskForMmap_(false),
|
2016-12-22 20:51:29 +00:00
|
|
|
forceMmapOff_(false),
|
2015-10-07 17:04:05 +00:00
|
|
|
page_size_(getpagesize()),
|
2018-04-13 20:05:28 +00:00
|
|
|
thread_pools_(Priority::TOTAL),
|
|
|
|
allow_non_owner_access_(true) {
|
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
|
|
|
}
|
|
|
|
|
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;
|
2013-03-19 21:39:28 +00:00
|
|
|
return &default_env;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
} // namespace rocksdb
|