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
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
2015-07-01 23:13:49 +00:00
|
|
|
#ifndef OS_WIN
|
2015-07-13 19:11:05 +00:00
|
|
|
#include <sys/ioctl.h>
|
2015-07-01 23:13:49 +00:00
|
|
|
#endif
|
2016-06-01 22:08:38 +00:00
|
|
|
|
2021-05-18 23:08:21 +00:00
|
|
|
#if defined(ROCKSDB_IOURING_PRESENT)
|
|
|
|
#include <liburing.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#endif
|
|
|
|
|
2015-07-13 19:11:05 +00:00
|
|
|
#include <sys/types.h>
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2014-10-27 22:41:05 +00:00
|
|
|
#include <atomic>
|
test: avoid vuln-inducing use of temporary directory
Summary:
Without this change, someone on the machine on which
I run "make check" could cause me to overwrite arbitrary
files owned by me, via a symlink attack.
Instead of using a predictable temporary directory and
accepting to use a preexisting one, always create a new
one using mkdtemp. If $TEST_IOCTL_FRIENDLY_TMPDIR is
set and usable, attempt first to find a usable
temporary directory therein. If not, or if unusable,
then try /var/tmp and /tmp. If none of those is usable
abort with a diagnostic.
To do that, I added a new class.
Its constructor finds a suitable directory or aborts,
the sole member prints that directory's name, and the
destructor unlinks what should be an empty directory.
Note that while the code before this did not remove
its temporary directory, there was only one per $UID.
Now, there would be at least one per run or one per
test, depending on implementation, so it is important
to remove them.
Test Plan:
Run this on a fedora rawhide system, where /tmp
is a tmpfs file system, and /var/tmp is ext4.
# This gives a diagnostic that /dev/shm is not suitable
# and ends up using /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/dev/shm ./env_test
# Uses /var/tmp; same as when envvar not set.
TEST_IOCTL_FRIENDLY_TMPDIR=/var/tmp ./env_test
# Uses /tmp unless it's tmpfs, in which case it gives
# a diagnostic and uses /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/tmp ./env_test
Reviewers: ljin, rven, igor.sugak, yhchiang, sdong, igor
Reviewed By: igor
Subscribers: dhruba
Differential Revision: https://reviews.facebook.net/D37287
2015-04-23 15:00:56 +00:00
|
|
|
#include <list>
|
Built-in support for generating unique IDs, bug fix (#8708)
Summary:
Env::GenerateUniqueId() works fine on Windows and on POSIX
where /proc/sys/kernel/random/uuid exists. Our other implementation is
flawed and easily produces collision in a new multi-threaded test.
As we rely more heavily on DB session ID uniqueness, this becomes a
serious issue.
This change combines several individually suitable entropy sources
for reliable generation of random unique IDs, with goal of uniqueness
and portability, not cryptographic strength nor maximum speed.
Specifically:
* Moves code for getting UUIDs from the OS to port::GenerateRfcUuid
rather than in Env implementation details. Callers are now told whether
the operation fails or succeeds.
* Adds an internal API GenerateRawUniqueId for generating high-quality
128-bit unique identifiers, by combining entropy from three "tracks":
* Lots of info from default Env like time, process id, and hostname.
* std::random_device
* port::GenerateRfcUuid (when working)
* Built-in implementations of Env::GenerateUniqueId() will now always
produce an RFC 4122 UUID string, either from platform-specific API or
by converting the output of GenerateRawUniqueId.
DB session IDs now use GenerateRawUniqueId while DB IDs (not as
critical) try to use port::GenerateRfcUuid but fall back on
GenerateRawUniqueId with conversion to an RFC 4122 UUID.
GenerateRawUniqueId is declared and defined under env/ rather than util/
or even port/ because of the Env dependency.
Likely follow-up: enhance GenerateRawUniqueId to be faster after the
first call and to guarantee uniqueness within the lifetime of a single
process (imparting the same property onto DB session IDs).
Pull Request resolved: https://github.com/facebook/rocksdb/pull/8708
Test Plan:
A new mini-stress test in env_test checks the various public
and internal APIs for uniqueness, including each track of
GenerateRawUniqueId individually. We can't hope to verify anywhere close
to 128 bits of entropy, but it can at least detect flaws as bad as the
old code. Serial execution of the new tests takes about 350 ms on
my machine.
Reviewed By: zhichao-cao, mrambacher
Differential Revision: D30563780
Pulled By: pdillinger
fbshipit-source-id: de4c9ff4b2f581cf784fcedb5f39f16e5185c364
2021-08-30 22:19:39 +00:00
|
|
|
#include <mutex>
|
|
|
|
#include <unordered_set>
|
2013-09-12 07:53:30 +00:00
|
|
|
|
2014-03-06 23:59:27 +00:00
|
|
|
#ifdef OS_LINUX
|
2016-02-12 22:17:14 +00:00
|
|
|
#include <fcntl.h>
|
skip ioctl-using tests when not supported
Summary:
[NB: this is a prerequisite for the /tmp-abuse-fixing patch]
This avoids spurious test failure on Linux systems
like Fedora for which /tmp is a tmpfs file system.
On a devtmpfs file
system, ioctl(fd, FS_IOC_GETVERSION, &version) returns -1 with
errno == ENOTTTY, indicating that that ioctl is not supported
on such a file system. Do not let this cause test failures, e.g.,
where env_test would assert that file->GetUniqueId(...) > 0.
Before this change, ./env_test would fail these three tests
on a fedora rawhide system:
[ FAILED ] 3 tests, listed below:
[ FAILED ] EnvPosixTest.RandomAccessUniqueID
[ FAILED ] EnvPosixTest.RandomAccessUniqueIDConcurrent
[ FAILED ] EnvPosixTest.RandomAccessUniqueIDDeletes
3 FAILED TESTS
The fix:
When support for that ioctl is lacking, skip each affected test.
Could be improved by noting which sub-tests are being skipped.
Test Plan:
run these on F21 and note that they now pass.
TEST_TMPDIR=/dev/shm/rdb ./env_test
./env_test
Reviewers: ljin, rven, igor.sugak, yhchiang, sdong, igor
Reviewed By: igor
Subscribers: dhruba
Differential Revision: https://reviews.facebook.net/D37323
2015-04-18 03:39:02 +00:00
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <stdlib.h>
|
2014-03-06 23:59:27 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2014-09-19 17:37:42 +00:00
|
|
|
#ifdef ROCKSDB_FALLOCATE_PRESENT
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
|
|
|
|
Built-in support for generating unique IDs, bug fix (#8708)
Summary:
Env::GenerateUniqueId() works fine on Windows and on POSIX
where /proc/sys/kernel/random/uuid exists. Our other implementation is
flawed and easily produces collision in a new multi-threaded test.
As we rely more heavily on DB session ID uniqueness, this becomes a
serious issue.
This change combines several individually suitable entropy sources
for reliable generation of random unique IDs, with goal of uniqueness
and portability, not cryptographic strength nor maximum speed.
Specifically:
* Moves code for getting UUIDs from the OS to port::GenerateRfcUuid
rather than in Env implementation details. Callers are now told whether
the operation fails or succeeds.
* Adds an internal API GenerateRawUniqueId for generating high-quality
128-bit unique identifiers, by combining entropy from three "tracks":
* Lots of info from default Env like time, process id, and hostname.
* std::random_device
* port::GenerateRfcUuid (when working)
* Built-in implementations of Env::GenerateUniqueId() will now always
produce an RFC 4122 UUID string, either from platform-specific API or
by converting the output of GenerateRawUniqueId.
DB session IDs now use GenerateRawUniqueId while DB IDs (not as
critical) try to use port::GenerateRfcUuid but fall back on
GenerateRawUniqueId with conversion to an RFC 4122 UUID.
GenerateRawUniqueId is declared and defined under env/ rather than util/
or even port/ because of the Env dependency.
Likely follow-up: enhance GenerateRawUniqueId to be faster after the
first call and to guarantee uniqueness within the lifetime of a single
process (imparting the same property onto DB session IDs).
Pull Request resolved: https://github.com/facebook/rocksdb/pull/8708
Test Plan:
A new mini-stress test in env_test checks the various public
and internal APIs for uniqueness, including each track of
GenerateRawUniqueId individually. We can't hope to verify anywhere close
to 128 bits of entropy, but it can at least detect flaws as bad as the
old code. Serial execution of the new tests takes about 350 ms on
my machine.
Reviewed By: zhichao-cao, mrambacher
Differential Revision: D30563780
Pulled By: pdillinger
fbshipit-source-id: de4c9ff4b2f581cf784fcedb5f39f16e5185c364
2021-08-30 22:19:39 +00:00
|
|
|
#include "db/db_impl/db_impl.h"
|
2021-09-21 15:53:03 +00:00
|
|
|
#include "env/emulated_clock.h"
|
2017-04-06 02:02:00 +00:00
|
|
|
#include "env/env_chroot.h"
|
2021-07-16 14:57:47 +00:00
|
|
|
#include "env/env_encryption_ctr.h"
|
2021-11-02 16:06:02 +00:00
|
|
|
#include "env/fs_readonly.h"
|
2022-01-05 00:44:54 +00:00
|
|
|
#include "env/mock_env.h"
|
Experimental support for SST unique IDs (#8990)
Summary:
* New public header unique_id.h and function GetUniqueIdFromTableProperties
which computes a universally unique identifier based on table properties
of table files from recent RocksDB versions.
* Generation of DB session IDs is refactored so that they are
guaranteed unique in the lifetime of a process running RocksDB.
(SemiStructuredUniqueIdGen, new test included.) Along with file numbers,
this enables SST unique IDs to be guaranteed unique among SSTs generated
in a single process, and "better than random" between processes.
See https://github.com/pdillinger/unique_id
* In addition to public API producing 'external' unique IDs, there is a function
for producing 'internal' unique IDs, with functions for converting between the
two. In short, the external ID is "safe" for things people might do with it, and
the internal ID enables more "power user" features for the future. Specifically,
the external ID goes through a hashing layer so that any subset of bits in the
external ID can be used as a hash of the full ID, while also preserving
uniqueness guarantees in the first 128 bits (bijective both on first 128 bits
and on full 192 bits).
Intended follow-up:
* Use the internal unique IDs in cache keys. (Avoid conflicts with https://github.com/facebook/rocksdb/issues/8912) (The file offset can be XORed into
the third 64-bit value of the unique ID.)
* Publish the external unique IDs in FileStorageInfo (https://github.com/facebook/rocksdb/issues/8968)
Pull Request resolved: https://github.com/facebook/rocksdb/pull/8990
Test Plan:
Unit tests added, and checking of unique ids in stress test.
NOTE in stress test we do not generate nearly enough files to thoroughly
stress uniqueness, but the test trims off pieces of the ID to check for
uniqueness so that we can infer (with some assumptions) stronger
properties in the aggregate.
Reviewed By: zhichao-cao, mrambacher
Differential Revision: D31582865
Pulled By: pdillinger
fbshipit-source-id: 1f620c4c86af9abe2a8d177b9ccf2ad2b9f48243
2021-10-19 06:28:28 +00:00
|
|
|
#include "env/unique_id_gen.h"
|
2019-06-01 00:19:43 +00:00
|
|
|
#include "logging/log_buffer.h"
|
2021-09-29 11:01:57 +00:00
|
|
|
#include "logging/logging.h"
|
2022-05-13 11:57:08 +00:00
|
|
|
#include "options/options_helper.h"
|
2019-09-16 22:14:51 +00:00
|
|
|
#include "port/malloc.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "port/port.h"
|
2022-01-05 00:44:54 +00:00
|
|
|
#include "port/stack_trace.h"
|
2021-07-16 14:57:47 +00:00
|
|
|
#include "rocksdb/convenience.h"
|
2016-05-07 00:42:50 +00:00
|
|
|
#include "rocksdb/env.h"
|
2021-07-16 14:57:47 +00:00
|
|
|
#include "rocksdb/env_encryption.h"
|
2021-09-08 05:40:37 +00:00
|
|
|
#include "rocksdb/file_system.h"
|
2021-01-26 06:07:26 +00:00
|
|
|
#include "rocksdb/system_clock.h"
|
2021-09-21 15:53:03 +00:00
|
|
|
#include "rocksdb/utilities/object_registry.h"
|
|
|
|
#include "test_util/mock_time_env.h"
|
2019-05-31 00:39:43 +00:00
|
|
|
#include "test_util/sync_point.h"
|
|
|
|
#include "test_util/testharness.h"
|
|
|
|
#include "test_util/testutil.h"
|
2013-01-31 23:20:24 +00:00
|
|
|
#include "util/coding.h"
|
2021-03-19 18:42:27 +00:00
|
|
|
#include "util/crc32c.h"
|
2013-09-12 07:53:30 +00:00
|
|
|
#include "util/mutexlock.h"
|
2020-07-09 21:33:42 +00:00
|
|
|
#include "util/random.h"
|
2015-03-20 00:29:37 +00:00
|
|
|
#include "util/string_util.h"
|
2022-02-03 23:00:03 +00:00
|
|
|
#include "utilities/counted_fs.h"
|
2021-11-02 16:06:02 +00:00
|
|
|
#include "utilities/env_timed.h"
|
2020-07-09 21:33:42 +00:00
|
|
|
#include "utilities/fault_injection_env.h"
|
|
|
|
#include "utilities/fault_injection_fs.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2020-02-22 15:59:38 +00:00
|
|
|
using port::kPageSize;
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
static const int kDelayMicros = 100000;
|
|
|
|
|
2016-06-01 22:08:38 +00:00
|
|
|
struct Deleter {
|
|
|
|
explicit Deleter(void (*fn)(void*)) : fn_(fn) {}
|
|
|
|
|
|
|
|
void operator()(void* ptr) {
|
|
|
|
assert(fn_);
|
|
|
|
assert(ptr);
|
|
|
|
(*fn_)(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void (*fn_)(void*);
|
|
|
|
};
|
|
|
|
|
2022-04-04 22:35:43 +00:00
|
|
|
extern "C" bool RocksDbIOUringEnable() { return true; }
|
|
|
|
|
2016-06-01 22:08:38 +00:00
|
|
|
std::unique_ptr<char, Deleter> NewAligned(const size_t size, const char ch) {
|
2016-04-21 17:37:27 +00:00
|
|
|
char* ptr = nullptr;
|
2016-06-01 22:08:38 +00:00
|
|
|
#ifdef OS_WIN
|
2022-10-25 00:54:14 +00:00
|
|
|
if (nullptr ==
|
|
|
|
(ptr = reinterpret_cast<char*>(_aligned_malloc(size, kPageSize)))) {
|
2016-06-01 22:08:38 +00:00
|
|
|
return std::unique_ptr<char, Deleter>(nullptr, Deleter(_aligned_free));
|
|
|
|
}
|
|
|
|
std::unique_ptr<char, Deleter> uptr(ptr, Deleter(_aligned_free));
|
|
|
|
#else
|
2017-02-22 00:33:54 +00:00
|
|
|
if (posix_memalign(reinterpret_cast<void**>(&ptr), kPageSize, size) != 0) {
|
2016-06-01 22:08:38 +00:00
|
|
|
return std::unique_ptr<char, Deleter>(nullptr, Deleter(free));
|
2016-04-21 17:37:27 +00:00
|
|
|
}
|
2016-06-01 22:08:38 +00:00
|
|
|
std::unique_ptr<char, Deleter> uptr(ptr, Deleter(free));
|
|
|
|
#endif
|
2016-04-21 17:37:27 +00:00
|
|
|
memset(uptr.get(), ch, size);
|
|
|
|
return uptr;
|
|
|
|
}
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
class EnvPosixTest : public testing::Test {
|
2011-03-18 22:37:00 +00:00
|
|
|
private:
|
|
|
|
port::Mutex mu_;
|
|
|
|
std::string events_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Env* env_;
|
2017-02-16 18:25:06 +00:00
|
|
|
bool direct_io_;
|
|
|
|
EnvPosixTest() : env_(Env::Default()), direct_io_(false) {}
|
2021-05-19 17:58:13 +00:00
|
|
|
~EnvPosixTest() {
|
|
|
|
SyncPoint::GetInstance()->DisableProcessing();
|
|
|
|
SyncPoint::GetInstance()->LoadDependency({});
|
|
|
|
SyncPoint::GetInstance()->ClearAllCallBacks();
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
};
|
|
|
|
|
2017-02-16 18:25:06 +00:00
|
|
|
class EnvPosixTestWithParam
|
|
|
|
: public EnvPosixTest,
|
|
|
|
public ::testing::WithParamInterface<std::pair<Env*, bool>> {
|
2016-05-07 00:42:50 +00:00
|
|
|
public:
|
2017-02-16 18:25:06 +00:00
|
|
|
EnvPosixTestWithParam() {
|
|
|
|
std::pair<Env*, bool> param_pair = GetParam();
|
|
|
|
env_ = param_pair.first;
|
|
|
|
direct_io_ = param_pair.second;
|
|
|
|
}
|
2017-01-25 23:51:18 +00:00
|
|
|
|
|
|
|
void WaitThreadPoolsEmpty() {
|
|
|
|
// Wait until the thread pools are empty.
|
|
|
|
while (env_->GetThreadPoolQueueLen(Env::Priority::LOW) != 0) {
|
|
|
|
Env::Default()->SleepForMicroseconds(kDelayMicros);
|
|
|
|
}
|
|
|
|
while (env_->GetThreadPoolQueueLen(Env::Priority::HIGH) != 0) {
|
|
|
|
Env::Default()->SleepForMicroseconds(kDelayMicros);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
~EnvPosixTestWithParam() override { WaitThreadPoolsEmpty(); }
|
2016-05-07 00:42:50 +00:00
|
|
|
};
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
static void SetBool(void* ptr) {
|
2017-01-25 23:51:18 +00:00
|
|
|
reinterpret_cast<std::atomic<bool>*>(ptr)->store(true);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 01:23:27 +00:00
|
|
|
TEST_F(EnvPosixTest, DISABLED_RunImmediately) {
|
2017-08-03 22:36:28 +00:00
|
|
|
for (int pri = Env::BOTTOM; pri < Env::TOTAL; ++pri) {
|
|
|
|
std::atomic<bool> called(false);
|
|
|
|
env_->SetBackgroundThreads(1, static_cast<Env::Priority>(pri));
|
|
|
|
env_->Schedule(&SetBool, &called, static_cast<Env::Priority>(pri));
|
|
|
|
Env::Default()->SleepForMicroseconds(kDelayMicros);
|
|
|
|
ASSERT_TRUE(called.load());
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 01:23:27 +00:00
|
|
|
TEST_F(EnvPosixTest, RunEventually) {
|
|
|
|
std::atomic<bool> called(false);
|
|
|
|
env_->StartThread(&SetBool, &called);
|
|
|
|
env_->WaitForJoin();
|
|
|
|
ASSERT_TRUE(called.load());
|
|
|
|
}
|
|
|
|
|
2018-03-06 19:47:42 +00:00
|
|
|
#ifdef OS_WIN
|
|
|
|
TEST_F(EnvPosixTest, AreFilesSame) {
|
|
|
|
{
|
|
|
|
bool tmp;
|
|
|
|
if (env_->AreFilesSame("", "", &tmp).IsNotSupported()) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"skipping EnvBasicTestWithParam.AreFilesSame due to "
|
|
|
|
"unsupported Env::AreFilesSame\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const EnvOptions soptions;
|
|
|
|
auto* env = Env::Default();
|
2018-07-14 00:18:39 +00:00
|
|
|
std::string same_file_name = test::PerThreadDBPath(env, "same_file");
|
2018-03-06 19:47:42 +00:00
|
|
|
std::string same_file_link_name = same_file_name + "_link";
|
|
|
|
|
|
|
|
std::unique_ptr<WritableFile> same_file;
|
2022-10-25 00:54:14 +00:00
|
|
|
ASSERT_OK(env->NewWritableFile(same_file_name, &same_file, soptions));
|
2018-03-06 19:47:42 +00:00
|
|
|
same_file->Append("random_data");
|
|
|
|
ASSERT_OK(same_file->Flush());
|
|
|
|
same_file.reset();
|
|
|
|
|
|
|
|
ASSERT_OK(env->LinkFile(same_file_name, same_file_link_name));
|
|
|
|
bool result = false;
|
|
|
|
ASSERT_OK(env->AreFilesSame(same_file_name, same_file_link_name, &result));
|
|
|
|
ASSERT_TRUE(result);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-04-13 20:05:28 +00:00
|
|
|
#ifdef OS_LINUX
|
2018-04-20 22:27:37 +00:00
|
|
|
TEST_F(EnvPosixTest, DISABLED_FilePermission) {
|
2018-04-13 20:05:28 +00:00
|
|
|
// Only works for Linux environment
|
|
|
|
if (env_ == Env::Default()) {
|
|
|
|
EnvOptions soptions;
|
2018-07-14 00:18:39 +00:00
|
|
|
std::vector<std::string> fileNames{
|
|
|
|
test::PerThreadDBPath(env_, "testfile"),
|
|
|
|
test::PerThreadDBPath(env_, "testfile1")};
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<WritableFile> wfile;
|
2018-04-13 20:05:28 +00:00
|
|
|
ASSERT_OK(env_->NewWritableFile(fileNames[0], &wfile, soptions));
|
2018-05-09 17:24:11 +00:00
|
|
|
ASSERT_OK(env_->NewWritableFile(fileNames[1], &wfile, soptions));
|
|
|
|
wfile.reset();
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<RandomRWFile> rwfile;
|
2018-04-13 20:05:28 +00:00
|
|
|
ASSERT_OK(env_->NewRandomRWFile(fileNames[1], &rwfile, soptions));
|
|
|
|
|
|
|
|
struct stat sb;
|
|
|
|
for (const auto& filename : fileNames) {
|
|
|
|
if (::stat(filename.c_str(), &sb) == 0) {
|
|
|
|
ASSERT_EQ(sb.st_mode & 0777, 0644);
|
|
|
|
}
|
2020-07-29 05:58:28 +00:00
|
|
|
ASSERT_OK(env_->DeleteFile(filename));
|
2018-04-13 20:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
env_->SetAllowNonOwnerAccess(false);
|
|
|
|
ASSERT_OK(env_->NewWritableFile(fileNames[0], &wfile, soptions));
|
2018-05-09 17:24:11 +00:00
|
|
|
ASSERT_OK(env_->NewWritableFile(fileNames[1], &wfile, soptions));
|
|
|
|
wfile.reset();
|
2018-04-13 20:05:28 +00:00
|
|
|
ASSERT_OK(env_->NewRandomRWFile(fileNames[1], &rwfile, soptions));
|
|
|
|
|
|
|
|
for (const auto& filename : fileNames) {
|
|
|
|
if (::stat(filename.c_str(), &sb) == 0) {
|
|
|
|
ASSERT_EQ(sb.st_mode & 0777, 0600);
|
|
|
|
}
|
2020-07-29 05:58:28 +00:00
|
|
|
ASSERT_OK(env_->DeleteFile(filename));
|
2018-04-13 20:05:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-13 20:23:40 +00:00
|
|
|
|
|
|
|
TEST_F(EnvPosixTest, LowerThreadPoolCpuPriority) {
|
|
|
|
std::atomic<CpuPriority> from_priority(CpuPriority::kNormal);
|
|
|
|
std::atomic<CpuPriority> to_priority(CpuPriority::kNormal);
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
|
|
|
"ThreadPoolImpl::BGThread::BeforeSetCpuPriority", [&](void* pri) {
|
|
|
|
from_priority.store(*reinterpret_cast<CpuPriority*>(pri));
|
|
|
|
});
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
|
|
|
"ThreadPoolImpl::BGThread::AfterSetCpuPriority", [&](void* pri) {
|
|
|
|
to_priority.store(*reinterpret_cast<CpuPriority*>(pri));
|
|
|
|
});
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
|
|
|
|
|
|
|
env_->SetBackgroundThreads(1, Env::BOTTOM);
|
|
|
|
env_->SetBackgroundThreads(1, Env::HIGH);
|
|
|
|
|
|
|
|
auto RunTask = [&](Env::Priority pool) {
|
|
|
|
std::atomic<bool> called(false);
|
|
|
|
env_->Schedule(&SetBool, &called, pool);
|
|
|
|
for (int i = 0; i < kDelayMicros; i++) {
|
|
|
|
if (called.load()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Env::Default()->SleepForMicroseconds(1);
|
|
|
|
}
|
|
|
|
ASSERT_TRUE(called.load());
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
// Same priority, no-op.
|
|
|
|
env_->LowerThreadPoolCPUPriority(Env::Priority::BOTTOM,
|
2020-07-29 05:58:28 +00:00
|
|
|
CpuPriority::kNormal)
|
|
|
|
.PermitUncheckedError();
|
2020-06-13 20:23:40 +00:00
|
|
|
RunTask(Env::Priority::BOTTOM);
|
|
|
|
ASSERT_EQ(from_priority, CpuPriority::kNormal);
|
|
|
|
ASSERT_EQ(to_priority, CpuPriority::kNormal);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Higher priority, no-op.
|
2020-07-29 05:58:28 +00:00
|
|
|
env_->LowerThreadPoolCPUPriority(Env::Priority::BOTTOM, CpuPriority::kHigh)
|
|
|
|
.PermitUncheckedError();
|
2020-06-13 20:23:40 +00:00
|
|
|
RunTask(Env::Priority::BOTTOM);
|
|
|
|
ASSERT_EQ(from_priority, CpuPriority::kNormal);
|
|
|
|
ASSERT_EQ(to_priority, CpuPriority::kNormal);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Lower priority from kNormal -> kLow.
|
2020-07-29 05:58:28 +00:00
|
|
|
env_->LowerThreadPoolCPUPriority(Env::Priority::BOTTOM, CpuPriority::kLow)
|
|
|
|
.PermitUncheckedError();
|
2020-06-13 20:23:40 +00:00
|
|
|
RunTask(Env::Priority::BOTTOM);
|
|
|
|
ASSERT_EQ(from_priority, CpuPriority::kNormal);
|
|
|
|
ASSERT_EQ(to_priority, CpuPriority::kLow);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Lower priority from kLow -> kIdle.
|
2020-07-29 05:58:28 +00:00
|
|
|
env_->LowerThreadPoolCPUPriority(Env::Priority::BOTTOM, CpuPriority::kIdle)
|
|
|
|
.PermitUncheckedError();
|
2020-06-13 20:23:40 +00:00
|
|
|
RunTask(Env::Priority::BOTTOM);
|
|
|
|
ASSERT_EQ(from_priority, CpuPriority::kLow);
|
|
|
|
ASSERT_EQ(to_priority, CpuPriority::kIdle);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Lower priority from kNormal -> kIdle for another pool.
|
2020-07-29 05:58:28 +00:00
|
|
|
env_->LowerThreadPoolCPUPriority(Env::Priority::HIGH, CpuPriority::kIdle)
|
|
|
|
.PermitUncheckedError();
|
2020-06-13 20:23:40 +00:00
|
|
|
RunTask(Env::Priority::HIGH);
|
|
|
|
ASSERT_EQ(from_priority, CpuPriority::kNormal);
|
|
|
|
ASSERT_EQ(to_priority, CpuPriority::kIdle);
|
|
|
|
}
|
|
|
|
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
|
|
|
|
}
|
2018-04-13 20:05:28 +00:00
|
|
|
#endif
|
|
|
|
|
2018-04-30 19:23:45 +00:00
|
|
|
TEST_F(EnvPosixTest, MemoryMappedFileBuffer) {
|
|
|
|
const int kFileBytes = 1 << 15; // 32 KB
|
|
|
|
std::string expected_data;
|
2018-07-14 00:18:39 +00:00
|
|
|
std::string fname = test::PerThreadDBPath(env_, "testfile");
|
2018-04-30 19:23:45 +00:00
|
|
|
{
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<WritableFile> wfile;
|
2018-04-30 19:23:45 +00:00
|
|
|
const EnvOptions soptions;
|
|
|
|
ASSERT_OK(env_->NewWritableFile(fname, &wfile, soptions));
|
|
|
|
|
|
|
|
Random rnd(301);
|
2020-07-09 21:33:42 +00:00
|
|
|
expected_data = rnd.RandomString(kFileBytes);
|
2018-04-30 19:23:45 +00:00
|
|
|
ASSERT_OK(wfile->Append(expected_data));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<MemoryMappedFileBuffer> mmap_buffer;
|
|
|
|
Status status = env_->NewMemoryMappedFileBuffer(fname, &mmap_buffer);
|
|
|
|
// it should be supported at least on linux
|
|
|
|
#if !defined(OS_LINUX)
|
|
|
|
if (status.IsNotSupported()) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"skipping EnvPosixTest.MemoryMappedFileBuffer due to "
|
|
|
|
"unsupported Env::NewMemoryMappedFileBuffer\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif // !defined(OS_LINUX)
|
|
|
|
|
|
|
|
ASSERT_OK(status);
|
|
|
|
ASSERT_NE(nullptr, mmap_buffer.get());
|
2018-05-24 22:05:00 +00:00
|
|
|
ASSERT_NE(nullptr, mmap_buffer->GetBase());
|
|
|
|
ASSERT_EQ(kFileBytes, mmap_buffer->GetLen());
|
|
|
|
std::string actual_data(reinterpret_cast<const char*>(mmap_buffer->GetBase()),
|
|
|
|
mmap_buffer->GetLen());
|
2018-04-30 19:23:45 +00:00
|
|
|
ASSERT_EQ(expected_data, actual_data);
|
|
|
|
}
|
|
|
|
|
2019-06-04 05:59:54 +00:00
|
|
|
#ifndef ROCKSDB_NO_DYNAMIC_EXTENSION
|
|
|
|
TEST_F(EnvPosixTest, LoadRocksDBLibrary) {
|
|
|
|
std::shared_ptr<DynamicLibrary> library;
|
|
|
|
std::function<void*(void*, const char*)> function;
|
|
|
|
Status status = env_->LoadLibrary("no-such-library", "", &library);
|
|
|
|
ASSERT_NOK(status);
|
|
|
|
ASSERT_EQ(nullptr, library.get());
|
|
|
|
status = env_->LoadLibrary("rocksdb", "", &library);
|
|
|
|
if (status.ok()) { // If we have can find a rocksdb shared library
|
|
|
|
ASSERT_NE(nullptr, library.get());
|
|
|
|
ASSERT_OK(library->LoadFunction("rocksdb_create_default_env",
|
|
|
|
&function)); // from C definition
|
|
|
|
ASSERT_NE(nullptr, function);
|
|
|
|
ASSERT_NOK(library->LoadFunction("no-such-method", &function));
|
|
|
|
ASSERT_EQ(nullptr, function);
|
|
|
|
ASSERT_OK(env_->LoadLibrary(library->Name(), "", &library));
|
|
|
|
} else {
|
|
|
|
ASSERT_EQ(nullptr, library.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // !ROCKSDB_NO_DYNAMIC_EXTENSION
|
|
|
|
|
|
|
|
#if !defined(OS_WIN) && !defined(ROCKSDB_NO_DYNAMIC_EXTENSION)
|
|
|
|
TEST_F(EnvPosixTest, LoadRocksDBLibraryWithSearchPath) {
|
|
|
|
std::shared_ptr<DynamicLibrary> library;
|
|
|
|
std::function<void*(void*, const char*)> function;
|
|
|
|
ASSERT_NOK(env_->LoadLibrary("no-such-library", "/tmp", &library));
|
|
|
|
ASSERT_EQ(nullptr, library.get());
|
|
|
|
ASSERT_NOK(env_->LoadLibrary("dl", "/tmp", &library));
|
|
|
|
ASSERT_EQ(nullptr, library.get());
|
|
|
|
Status status = env_->LoadLibrary("rocksdb", "/tmp:./", &library);
|
|
|
|
if (status.ok()) {
|
|
|
|
ASSERT_NE(nullptr, library.get());
|
|
|
|
ASSERT_OK(env_->LoadLibrary(library->Name(), "", &library));
|
|
|
|
}
|
|
|
|
char buff[1024];
|
|
|
|
std::string cwd = getcwd(buff, sizeof(buff));
|
|
|
|
|
|
|
|
status = env_->LoadLibrary("rocksdb", "/tmp:" + cwd, &library);
|
|
|
|
if (status.ok()) {
|
|
|
|
ASSERT_NE(nullptr, library.get());
|
|
|
|
ASSERT_OK(env_->LoadLibrary(library->Name(), "", &library));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // !OS_WIN && !ROCKSDB_NO_DYNAMIC_EXTENSION
|
|
|
|
|
2016-05-07 00:42:50 +00:00
|
|
|
TEST_P(EnvPosixTestWithParam, UnSchedule) {
|
2015-03-17 01:49:14 +00:00
|
|
|
std::atomic<bool> called(false);
|
|
|
|
env_->SetBackgroundThreads(1, Env::LOW);
|
|
|
|
|
|
|
|
/* Block the low priority queue */
|
2015-09-25 17:29:44 +00:00
|
|
|
test::SleepingBackgroundTask sleeping_task, sleeping_task1;
|
|
|
|
env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask, &sleeping_task,
|
2015-03-17 01:49:14 +00:00
|
|
|
Env::Priority::LOW);
|
|
|
|
|
|
|
|
/* Schedule another task */
|
2015-09-25 17:29:44 +00:00
|
|
|
env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask, &sleeping_task1,
|
2015-03-17 01:49:14 +00:00
|
|
|
Env::Priority::LOW, &sleeping_task1);
|
|
|
|
|
|
|
|
/* Remove it with a different tag */
|
|
|
|
ASSERT_EQ(0, env_->UnSchedule(&called, Env::Priority::LOW));
|
|
|
|
|
|
|
|
/* Remove it from the queue with the right tag */
|
|
|
|
ASSERT_EQ(1, env_->UnSchedule(&sleeping_task1, Env::Priority::LOW));
|
|
|
|
|
|
|
|
// Unblock background thread
|
|
|
|
sleeping_task.WakeUp();
|
|
|
|
|
|
|
|
/* Schedule another task */
|
|
|
|
env_->Schedule(&SetBool, &called);
|
2015-03-18 18:40:46 +00:00
|
|
|
for (int i = 0; i < kDelayMicros; i++) {
|
2017-01-25 23:51:18 +00:00
|
|
|
if (called.load()) {
|
2015-03-18 18:40:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
Env::Default()->SleepForMicroseconds(1);
|
|
|
|
}
|
2017-01-25 23:51:18 +00:00
|
|
|
ASSERT_TRUE(called.load());
|
2015-03-18 18:40:46 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(!sleeping_task.IsSleeping() && !sleeping_task1.IsSleeping());
|
2017-01-25 23:51:18 +00:00
|
|
|
WaitThreadPoolsEmpty();
|
2015-03-17 01:49:14 +00:00
|
|
|
}
|
|
|
|
|
2017-11-10 18:27:35 +00:00
|
|
|
// This tests assumes that the last scheduled
|
|
|
|
// task will run last. In fact, in the allotted
|
|
|
|
// sleeping time nothing may actually run or they may
|
|
|
|
// run in any order. The purpose of the test is unclear.
|
|
|
|
#ifndef OS_WIN
|
2016-05-07 00:42:50 +00:00
|
|
|
TEST_P(EnvPosixTestWithParam, RunMany) {
|
2020-06-04 16:49:44 +00:00
|
|
|
env_->SetBackgroundThreads(1, Env::LOW);
|
2014-10-27 21:50:21 +00:00
|
|
|
std::atomic<int> last_id(0);
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
struct CB {
|
2014-10-27 21:50:21 +00:00
|
|
|
std::atomic<int>* last_id_ptr; // Pointer to shared slot
|
|
|
|
int id; // Order# for the execution of this callback
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2014-10-27 21:50:21 +00:00
|
|
|
CB(std::atomic<int>* p, int i) : last_id_ptr(p), id(i) {}
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
static void Run(void* v) {
|
|
|
|
CB* cb = reinterpret_cast<CB*>(v);
|
2017-01-25 23:51:18 +00:00
|
|
|
int cur = cb->last_id_ptr->load();
|
2014-10-27 21:50:21 +00:00
|
|
|
ASSERT_EQ(cb->id - 1, cur);
|
2017-01-25 23:51:18 +00:00
|
|
|
cb->last_id_ptr->store(cb->id);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Schedule in different order than start time
|
|
|
|
CB cb1(&last_id, 1);
|
|
|
|
CB cb2(&last_id, 2);
|
|
|
|
CB cb3(&last_id, 3);
|
|
|
|
CB cb4(&last_id, 4);
|
|
|
|
env_->Schedule(&CB::Run, &cb1);
|
|
|
|
env_->Schedule(&CB::Run, &cb2);
|
|
|
|
env_->Schedule(&CB::Run, &cb3);
|
|
|
|
env_->Schedule(&CB::Run, &cb4);
|
2022-02-07 16:31:27 +00:00
|
|
|
// thread-pool pops a thread function and then run the function, which may
|
|
|
|
// cause threadpool is empty but the last function is still running. Add a
|
|
|
|
// dummy function at the end, to make sure the last callback is finished
|
|
|
|
// before threadpool is empty.
|
|
|
|
struct DummyCB {
|
|
|
|
static void Run(void*) {}
|
|
|
|
};
|
|
|
|
env_->Schedule(&DummyCB::Run, nullptr);
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2017-01-25 23:51:18 +00:00
|
|
|
WaitThreadPoolsEmpty();
|
2022-01-24 20:49:03 +00:00
|
|
|
ASSERT_EQ(4, last_id.load(std::memory_order_acquire));
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2017-11-10 18:27:35 +00:00
|
|
|
#endif
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
struct State {
|
|
|
|
port::Mutex mu;
|
|
|
|
int val;
|
|
|
|
int num_running;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void ThreadBody(void* arg) {
|
|
|
|
State* s = reinterpret_cast<State*>(arg);
|
|
|
|
s->mu.Lock();
|
|
|
|
s->val += 1;
|
|
|
|
s->num_running -= 1;
|
|
|
|
s->mu.Unlock();
|
|
|
|
}
|
|
|
|
|
2016-05-07 00:42:50 +00:00
|
|
|
TEST_P(EnvPosixTestWithParam, StartThread) {
|
2011-03-18 22:37:00 +00:00
|
|
|
State state;
|
|
|
|
state.val = 0;
|
|
|
|
state.num_running = 3;
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
env_->StartThread(&ThreadBody, &state);
|
|
|
|
}
|
|
|
|
while (true) {
|
|
|
|
state.mu.Lock();
|
|
|
|
int num = state.num_running;
|
|
|
|
state.mu.Unlock();
|
|
|
|
if (num == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Env::Default()->SleepForMicroseconds(kDelayMicros);
|
|
|
|
}
|
|
|
|
ASSERT_EQ(state.val, 3);
|
2017-01-25 23:51:18 +00:00
|
|
|
WaitThreadPoolsEmpty();
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2016-05-07 00:42:50 +00:00
|
|
|
TEST_P(EnvPosixTestWithParam, TwoPools) {
|
2016-09-06 19:28:55 +00:00
|
|
|
// Data structures to signal tasks to run.
|
|
|
|
port::Mutex mutex;
|
|
|
|
port::CondVar cv(&mutex);
|
|
|
|
bool should_start = false;
|
|
|
|
|
2013-09-14 00:26:54 +00:00
|
|
|
class CB {
|
|
|
|
public:
|
2016-09-06 19:28:55 +00:00
|
|
|
CB(const std::string& pool_name, int pool_size, port::Mutex* trigger_mu,
|
|
|
|
port::CondVar* trigger_cv, bool* _should_start)
|
2013-09-14 00:26:54 +00:00
|
|
|
: mu_(),
|
|
|
|
num_running_(0),
|
|
|
|
num_finished_(0),
|
|
|
|
pool_size_(pool_size),
|
2016-09-06 19:28:55 +00:00
|
|
|
pool_name_(pool_name),
|
|
|
|
trigger_mu_(trigger_mu),
|
|
|
|
trigger_cv_(trigger_cv),
|
|
|
|
should_start_(_should_start) {}
|
2013-09-14 00:26:54 +00:00
|
|
|
|
|
|
|
static void Run(void* v) {
|
|
|
|
CB* cb = reinterpret_cast<CB*>(v);
|
|
|
|
cb->Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Run() {
|
|
|
|
{
|
|
|
|
MutexLock l(&mu_);
|
|
|
|
num_running_++;
|
|
|
|
// make sure we don't have more than pool_size_ jobs running.
|
2014-11-03 22:11:33 +00:00
|
|
|
ASSERT_LE(num_running_, pool_size_.load());
|
2013-09-14 00:26:54 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 19:28:55 +00:00
|
|
|
{
|
|
|
|
MutexLock l(trigger_mu_);
|
|
|
|
while (!(*should_start_)) {
|
|
|
|
trigger_cv_->Wait();
|
|
|
|
}
|
|
|
|
}
|
2013-09-14 00:26:54 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
MutexLock l(&mu_);
|
|
|
|
num_running_--;
|
|
|
|
num_finished_++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int NumFinished() {
|
|
|
|
MutexLock l(&mu_);
|
|
|
|
return num_finished_;
|
|
|
|
}
|
|
|
|
|
2014-11-03 22:11:33 +00:00
|
|
|
void Reset(int pool_size) {
|
|
|
|
pool_size_.store(pool_size);
|
|
|
|
num_finished_ = 0;
|
|
|
|
}
|
|
|
|
|
2013-09-14 00:26:54 +00:00
|
|
|
private:
|
|
|
|
port::Mutex mu_;
|
|
|
|
int num_running_;
|
|
|
|
int num_finished_;
|
2014-11-03 22:11:33 +00:00
|
|
|
std::atomic<int> pool_size_;
|
2013-09-14 00:26:54 +00:00
|
|
|
std::string pool_name_;
|
2016-09-06 19:28:55 +00:00
|
|
|
port::Mutex* trigger_mu_;
|
|
|
|
port::CondVar* trigger_cv_;
|
|
|
|
bool* should_start_;
|
2013-09-14 00:26:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const int kLowPoolSize = 2;
|
|
|
|
const int kHighPoolSize = 4;
|
|
|
|
const int kJobs = 8;
|
|
|
|
|
2016-09-06 19:28:55 +00:00
|
|
|
CB low_pool_job("low", kLowPoolSize, &mutex, &cv, &should_start);
|
|
|
|
CB high_pool_job("high", kHighPoolSize, &mutex, &cv, &should_start);
|
2013-09-14 00:26:54 +00:00
|
|
|
|
|
|
|
env_->SetBackgroundThreads(kLowPoolSize);
|
|
|
|
env_->SetBackgroundThreads(kHighPoolSize, Env::Priority::HIGH);
|
|
|
|
|
2014-03-14 22:44:35 +00:00
|
|
|
ASSERT_EQ(0U, env_->GetThreadPoolQueueLen(Env::Priority::LOW));
|
|
|
|
ASSERT_EQ(0U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
|
2014-03-10 23:14:48 +00:00
|
|
|
|
2013-09-14 00:26:54 +00:00
|
|
|
// schedule same number of jobs in each pool
|
|
|
|
for (int i = 0; i < kJobs; i++) {
|
|
|
|
env_->Schedule(&CB::Run, &low_pool_job);
|
|
|
|
env_->Schedule(&CB::Run, &high_pool_job, Env::Priority::HIGH);
|
|
|
|
}
|
2014-03-10 23:14:48 +00:00
|
|
|
// Wait a short while for the jobs to be dispatched.
|
2016-09-06 19:28:55 +00:00
|
|
|
int sleep_count = 0;
|
|
|
|
while ((unsigned int)(kJobs - kLowPoolSize) !=
|
|
|
|
env_->GetThreadPoolQueueLen(Env::Priority::LOW) ||
|
|
|
|
(unsigned int)(kJobs - kHighPoolSize) !=
|
|
|
|
env_->GetThreadPoolQueueLen(Env::Priority::HIGH)) {
|
|
|
|
env_->SleepForMicroseconds(kDelayMicros);
|
|
|
|
if (++sleep_count > 100) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-14 23:56:58 +00:00
|
|
|
ASSERT_EQ((unsigned int)(kJobs - kLowPoolSize),
|
|
|
|
env_->GetThreadPoolQueueLen());
|
2014-03-14 22:44:35 +00:00
|
|
|
ASSERT_EQ((unsigned int)(kJobs - kLowPoolSize),
|
2014-03-10 23:14:48 +00:00
|
|
|
env_->GetThreadPoolQueueLen(Env::Priority::LOW));
|
2014-03-14 22:44:35 +00:00
|
|
|
ASSERT_EQ((unsigned int)(kJobs - kHighPoolSize),
|
2014-03-10 23:14:48 +00:00
|
|
|
env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
|
2013-09-14 00:26:54 +00:00
|
|
|
|
2016-09-06 19:28:55 +00:00
|
|
|
// Trigger jobs to run.
|
|
|
|
{
|
|
|
|
MutexLock l(&mutex);
|
|
|
|
should_start = true;
|
|
|
|
cv.SignalAll();
|
|
|
|
}
|
|
|
|
|
2013-09-14 00:26:54 +00:00
|
|
|
// wait for all jobs to finish
|
|
|
|
while (low_pool_job.NumFinished() < kJobs ||
|
|
|
|
high_pool_job.NumFinished() < kJobs) {
|
|
|
|
env_->SleepForMicroseconds(kDelayMicros);
|
|
|
|
}
|
2014-03-10 23:14:48 +00:00
|
|
|
|
2014-03-14 22:44:35 +00:00
|
|
|
ASSERT_EQ(0U, env_->GetThreadPoolQueueLen(Env::Priority::LOW));
|
|
|
|
ASSERT_EQ(0U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
|
2014-11-03 22:11:33 +00:00
|
|
|
|
2016-09-06 19:28:55 +00:00
|
|
|
// Hold jobs to schedule;
|
|
|
|
should_start = false;
|
|
|
|
|
2014-11-03 22:11:33 +00:00
|
|
|
// call IncBackgroundThreadsIfNeeded to two pools. One increasing and
|
|
|
|
// the other decreasing
|
|
|
|
env_->IncBackgroundThreadsIfNeeded(kLowPoolSize - 1, Env::Priority::LOW);
|
|
|
|
env_->IncBackgroundThreadsIfNeeded(kHighPoolSize + 1, Env::Priority::HIGH);
|
|
|
|
high_pool_job.Reset(kHighPoolSize + 1);
|
|
|
|
low_pool_job.Reset(kLowPoolSize);
|
|
|
|
|
|
|
|
// schedule same number of jobs in each pool
|
|
|
|
for (int i = 0; i < kJobs; i++) {
|
|
|
|
env_->Schedule(&CB::Run, &low_pool_job);
|
|
|
|
env_->Schedule(&CB::Run, &high_pool_job, Env::Priority::HIGH);
|
|
|
|
}
|
|
|
|
// Wait a short while for the jobs to be dispatched.
|
2016-09-06 19:28:55 +00:00
|
|
|
sleep_count = 0;
|
|
|
|
while ((unsigned int)(kJobs - kLowPoolSize) !=
|
|
|
|
env_->GetThreadPoolQueueLen(Env::Priority::LOW) ||
|
|
|
|
(unsigned int)(kJobs - (kHighPoolSize + 1)) !=
|
|
|
|
env_->GetThreadPoolQueueLen(Env::Priority::HIGH)) {
|
|
|
|
env_->SleepForMicroseconds(kDelayMicros);
|
|
|
|
if (++sleep_count > 100) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-11-03 22:11:33 +00:00
|
|
|
ASSERT_EQ((unsigned int)(kJobs - kLowPoolSize),
|
|
|
|
env_->GetThreadPoolQueueLen());
|
|
|
|
ASSERT_EQ((unsigned int)(kJobs - kLowPoolSize),
|
|
|
|
env_->GetThreadPoolQueueLen(Env::Priority::LOW));
|
|
|
|
ASSERT_EQ((unsigned int)(kJobs - (kHighPoolSize + 1)),
|
|
|
|
env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
|
|
|
|
|
2016-09-06 19:28:55 +00:00
|
|
|
// Trigger jobs to run.
|
|
|
|
{
|
|
|
|
MutexLock l(&mutex);
|
|
|
|
should_start = true;
|
|
|
|
cv.SignalAll();
|
|
|
|
}
|
|
|
|
|
2014-11-03 22:11:33 +00:00
|
|
|
// wait for all jobs to finish
|
|
|
|
while (low_pool_job.NumFinished() < kJobs ||
|
|
|
|
high_pool_job.NumFinished() < kJobs) {
|
|
|
|
env_->SleepForMicroseconds(kDelayMicros);
|
|
|
|
}
|
|
|
|
|
|
|
|
env_->SetBackgroundThreads(kHighPoolSize, Env::Priority::HIGH);
|
2017-01-25 23:51:18 +00:00
|
|
|
WaitThreadPoolsEmpty();
|
2013-09-14 00:26:54 +00:00
|
|
|
}
|
|
|
|
|
2016-05-07 00:42:50 +00:00
|
|
|
TEST_P(EnvPosixTestWithParam, DecreaseNumBgThreads) {
|
2022-10-25 00:54:14 +00:00
|
|
|
constexpr int kWaitMicros = 60000000; // 1min
|
2020-02-14 01:25:29 +00:00
|
|
|
|
2015-09-25 17:29:44 +00:00
|
|
|
std::vector<test::SleepingBackgroundTask> tasks(10);
|
2014-05-19 17:40:18 +00:00
|
|
|
|
|
|
|
// Set number of thread to 1 first.
|
|
|
|
env_->SetBackgroundThreads(1, Env::Priority::HIGH);
|
|
|
|
|
|
|
|
// Schedule 3 tasks. 0 running; Task 1, 2 waiting.
|
|
|
|
for (size_t i = 0; i < 3; i++) {
|
2015-09-25 17:29:44 +00:00
|
|
|
env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask, &tasks[i],
|
2014-05-19 17:40:18 +00:00
|
|
|
Env::Priority::HIGH);
|
|
|
|
}
|
2020-02-14 01:25:29 +00:00
|
|
|
ASSERT_FALSE(tasks[0].TimedWaitUntilSleeping(kWaitMicros));
|
2014-05-19 17:40:18 +00:00
|
|
|
ASSERT_EQ(2U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
|
|
|
|
ASSERT_TRUE(tasks[0].IsSleeping());
|
|
|
|
ASSERT_TRUE(!tasks[1].IsSleeping());
|
|
|
|
ASSERT_TRUE(!tasks[2].IsSleeping());
|
|
|
|
|
|
|
|
// Increase to 2 threads. Task 0, 1 running; 2 waiting
|
|
|
|
env_->SetBackgroundThreads(2, Env::Priority::HIGH);
|
2020-02-14 01:25:29 +00:00
|
|
|
ASSERT_FALSE(tasks[1].TimedWaitUntilSleeping(kWaitMicros));
|
2014-05-19 17:40:18 +00:00
|
|
|
ASSERT_EQ(1U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
|
|
|
|
ASSERT_TRUE(tasks[0].IsSleeping());
|
|
|
|
ASSERT_TRUE(tasks[1].IsSleeping());
|
|
|
|
ASSERT_TRUE(!tasks[2].IsSleeping());
|
|
|
|
|
|
|
|
// Shrink back to 1 thread. Still task 0, 1 running, 2 waiting
|
|
|
|
env_->SetBackgroundThreads(1, Env::Priority::HIGH);
|
|
|
|
Env::Default()->SleepForMicroseconds(kDelayMicros);
|
|
|
|
ASSERT_EQ(1U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
|
|
|
|
ASSERT_TRUE(tasks[0].IsSleeping());
|
|
|
|
ASSERT_TRUE(tasks[1].IsSleeping());
|
|
|
|
ASSERT_TRUE(!tasks[2].IsSleeping());
|
|
|
|
|
|
|
|
// The last task finishes. Task 0 running, 2 waiting.
|
|
|
|
tasks[1].WakeUp();
|
2020-02-14 01:25:29 +00:00
|
|
|
ASSERT_FALSE(tasks[1].TimedWaitUntilDone(kWaitMicros));
|
2014-05-19 17:40:18 +00:00
|
|
|
ASSERT_EQ(1U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
|
|
|
|
ASSERT_TRUE(tasks[0].IsSleeping());
|
|
|
|
ASSERT_TRUE(!tasks[1].IsSleeping());
|
|
|
|
ASSERT_TRUE(!tasks[2].IsSleeping());
|
|
|
|
|
|
|
|
// Increase to 5 threads. Task 0 and 2 running.
|
|
|
|
env_->SetBackgroundThreads(5, Env::Priority::HIGH);
|
2020-02-14 01:25:29 +00:00
|
|
|
ASSERT_FALSE(tasks[2].TimedWaitUntilSleeping(kWaitMicros));
|
|
|
|
ASSERT_EQ(0U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
|
2014-05-19 17:40:18 +00:00
|
|
|
ASSERT_TRUE(tasks[0].IsSleeping());
|
2020-02-14 01:25:29 +00:00
|
|
|
ASSERT_TRUE(!tasks[1].IsSleeping());
|
2014-05-19 17:40:18 +00:00
|
|
|
ASSERT_TRUE(tasks[2].IsSleeping());
|
|
|
|
|
|
|
|
// Change number of threads a couple of times while there is no sufficient
|
|
|
|
// tasks.
|
|
|
|
env_->SetBackgroundThreads(7, Env::Priority::HIGH);
|
|
|
|
tasks[2].WakeUp();
|
2020-02-14 01:25:29 +00:00
|
|
|
ASSERT_FALSE(tasks[2].TimedWaitUntilDone(kWaitMicros));
|
2014-05-19 17:40:18 +00:00
|
|
|
ASSERT_EQ(0U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
|
|
|
|
env_->SetBackgroundThreads(3, Env::Priority::HIGH);
|
|
|
|
Env::Default()->SleepForMicroseconds(kDelayMicros);
|
|
|
|
ASSERT_EQ(0U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
|
|
|
|
env_->SetBackgroundThreads(4, Env::Priority::HIGH);
|
|
|
|
Env::Default()->SleepForMicroseconds(kDelayMicros);
|
|
|
|
ASSERT_EQ(0U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
|
|
|
|
env_->SetBackgroundThreads(5, Env::Priority::HIGH);
|
|
|
|
Env::Default()->SleepForMicroseconds(kDelayMicros);
|
|
|
|
ASSERT_EQ(0U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
|
|
|
|
env_->SetBackgroundThreads(4, Env::Priority::HIGH);
|
|
|
|
Env::Default()->SleepForMicroseconds(kDelayMicros);
|
|
|
|
ASSERT_EQ(0U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
|
|
|
|
|
|
|
|
Env::Default()->SleepForMicroseconds(kDelayMicros * 50);
|
|
|
|
|
|
|
|
// Enqueue 5 more tasks. Thread pool size now is 4.
|
|
|
|
// Task 0, 3, 4, 5 running;6, 7 waiting.
|
|
|
|
for (size_t i = 3; i < 8; i++) {
|
2015-09-25 17:29:44 +00:00
|
|
|
env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask, &tasks[i],
|
2014-05-19 17:40:18 +00:00
|
|
|
Env::Priority::HIGH);
|
|
|
|
}
|
2020-02-14 01:25:29 +00:00
|
|
|
for (size_t i = 3; i <= 5; i++) {
|
|
|
|
ASSERT_FALSE(tasks[i].TimedWaitUntilSleeping(kWaitMicros));
|
|
|
|
}
|
2014-05-19 17:40:18 +00:00
|
|
|
ASSERT_EQ(2U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
|
2020-02-14 01:25:29 +00:00
|
|
|
ASSERT_TRUE(tasks[0].IsSleeping());
|
|
|
|
ASSERT_TRUE(!tasks[1].IsSleeping());
|
|
|
|
ASSERT_TRUE(!tasks[2].IsSleeping());
|
2014-05-19 17:40:18 +00:00
|
|
|
ASSERT_TRUE(tasks[3].IsSleeping());
|
|
|
|
ASSERT_TRUE(tasks[4].IsSleeping());
|
|
|
|
ASSERT_TRUE(tasks[5].IsSleeping());
|
|
|
|
ASSERT_TRUE(!tasks[6].IsSleeping());
|
|
|
|
ASSERT_TRUE(!tasks[7].IsSleeping());
|
|
|
|
|
|
|
|
// Wake up task 0, 3 and 4. Task 5, 6, 7 running.
|
|
|
|
tasks[0].WakeUp();
|
|
|
|
tasks[3].WakeUp();
|
|
|
|
tasks[4].WakeUp();
|
|
|
|
|
2020-02-14 01:25:29 +00:00
|
|
|
for (size_t i = 5; i < 8; i++) {
|
|
|
|
ASSERT_FALSE(tasks[i].TimedWaitUntilSleeping(kWaitMicros));
|
|
|
|
}
|
|
|
|
ASSERT_EQ(0U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
|
2014-05-19 17:40:18 +00:00
|
|
|
for (size_t i = 5; i < 8; i++) {
|
|
|
|
ASSERT_TRUE(tasks[i].IsSleeping());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shrink back to 1 thread. Still task 5, 6, 7 running
|
|
|
|
env_->SetBackgroundThreads(1, Env::Priority::HIGH);
|
|
|
|
Env::Default()->SleepForMicroseconds(kDelayMicros);
|
|
|
|
ASSERT_TRUE(tasks[5].IsSleeping());
|
|
|
|
ASSERT_TRUE(tasks[6].IsSleeping());
|
|
|
|
ASSERT_TRUE(tasks[7].IsSleeping());
|
|
|
|
|
|
|
|
// Wake up task 6. Task 5, 7 running
|
|
|
|
tasks[6].WakeUp();
|
2020-02-14 01:25:29 +00:00
|
|
|
ASSERT_FALSE(tasks[6].TimedWaitUntilDone(kWaitMicros));
|
2014-05-19 17:40:18 +00:00
|
|
|
ASSERT_TRUE(tasks[5].IsSleeping());
|
|
|
|
ASSERT_TRUE(!tasks[6].IsSleeping());
|
|
|
|
ASSERT_TRUE(tasks[7].IsSleeping());
|
|
|
|
|
|
|
|
// Wake up threads 7. Task 5 running
|
|
|
|
tasks[7].WakeUp();
|
2020-02-14 01:25:29 +00:00
|
|
|
ASSERT_FALSE(tasks[7].TimedWaitUntilDone(kWaitMicros));
|
2014-05-19 17:40:18 +00:00
|
|
|
ASSERT_TRUE(!tasks[7].IsSleeping());
|
|
|
|
|
|
|
|
// Enqueue thread 8 and 9. Task 5 running; one of 8, 9 might be running.
|
2015-09-25 17:29:44 +00:00
|
|
|
env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask, &tasks[8],
|
2014-05-19 17:40:18 +00:00
|
|
|
Env::Priority::HIGH);
|
2015-09-25 17:29:44 +00:00
|
|
|
env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask, &tasks[9],
|
2014-05-19 17:40:18 +00:00
|
|
|
Env::Priority::HIGH);
|
|
|
|
Env::Default()->SleepForMicroseconds(kDelayMicros);
|
2014-05-21 11:50:37 +00:00
|
|
|
ASSERT_GT(env_->GetThreadPoolQueueLen(Env::Priority::HIGH), (unsigned int)0);
|
2014-05-19 17:40:18 +00:00
|
|
|
ASSERT_TRUE(!tasks[8].IsSleeping() || !tasks[9].IsSleeping());
|
|
|
|
|
|
|
|
// Increase to 4 threads. Task 5, 8, 9 running.
|
|
|
|
env_->SetBackgroundThreads(4, Env::Priority::HIGH);
|
|
|
|
Env::Default()->SleepForMicroseconds(kDelayMicros);
|
2014-05-21 11:50:37 +00:00
|
|
|
ASSERT_EQ((unsigned int)0, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
|
2014-05-19 17:40:18 +00:00
|
|
|
ASSERT_TRUE(tasks[8].IsSleeping());
|
|
|
|
ASSERT_TRUE(tasks[9].IsSleeping());
|
|
|
|
|
|
|
|
// Shrink to 1 thread
|
|
|
|
env_->SetBackgroundThreads(1, Env::Priority::HIGH);
|
|
|
|
|
|
|
|
// Wake up thread 9.
|
|
|
|
tasks[9].WakeUp();
|
2020-02-14 01:25:29 +00:00
|
|
|
ASSERT_FALSE(tasks[9].TimedWaitUntilDone(kWaitMicros));
|
2014-05-19 17:40:18 +00:00
|
|
|
ASSERT_TRUE(!tasks[9].IsSleeping());
|
|
|
|
ASSERT_TRUE(tasks[8].IsSleeping());
|
|
|
|
|
|
|
|
// Wake up thread 8
|
|
|
|
tasks[8].WakeUp();
|
2020-02-14 01:25:29 +00:00
|
|
|
ASSERT_FALSE(tasks[8].TimedWaitUntilDone(kWaitMicros));
|
2014-05-19 17:40:18 +00:00
|
|
|
ASSERT_TRUE(!tasks[8].IsSleeping());
|
|
|
|
|
|
|
|
// Wake up the last thread
|
|
|
|
tasks[5].WakeUp();
|
2020-02-14 01:25:29 +00:00
|
|
|
ASSERT_FALSE(tasks[5].TimedWaitUntilDone(kWaitMicros));
|
2017-01-25 23:51:18 +00:00
|
|
|
WaitThreadPoolsEmpty();
|
2014-05-19 17:40:18 +00:00
|
|
|
}
|
|
|
|
|
2022-07-09 02:48:09 +00:00
|
|
|
TEST_P(EnvPosixTestWithParam, ReserveThreads) {
|
|
|
|
// Initialize the background thread to 1 in case other threads exist
|
|
|
|
// from the last unit test
|
|
|
|
env_->SetBackgroundThreads(1, Env::Priority::HIGH);
|
|
|
|
ASSERT_EQ(env_->GetBackgroundThreads(Env::HIGH), 1);
|
|
|
|
constexpr int kWaitMicros = 10000000; // 10seconds
|
|
|
|
std::vector<test::SleepingBackgroundTask> tasks(4);
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
|
|
|
// Set the sync point to ensure thread 0 can terminate
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency(
|
|
|
|
{{"ThreadPoolImpl::BGThread::Termination:th0",
|
|
|
|
"EnvTest::ReserveThreads:0"}});
|
|
|
|
// Empty the thread pool to ensure all the threads can start later
|
|
|
|
env_->SetBackgroundThreads(0, Env::Priority::HIGH);
|
|
|
|
TEST_SYNC_POINT("EnvTest::ReserveThreads:0");
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
|
|
|
// Set the sync point to ensure threads start and pass the sync point
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency(
|
|
|
|
{{"ThreadPoolImpl::BGThread::Start:th0", "EnvTest::ReserveThreads:1"},
|
|
|
|
{"ThreadPoolImpl::BGThread::Start:th1", "EnvTest::ReserveThreads:2"},
|
|
|
|
{"ThreadPoolImpl::BGThread::Start:th2", "EnvTest::ReserveThreads:3"},
|
|
|
|
{"ThreadPoolImpl::BGThread::Start:th3", "EnvTest::ReserveThreads:4"}});
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
|
|
|
|
|
|
|
// Set number of thread to 3 first.
|
|
|
|
env_->SetBackgroundThreads(3, Env::Priority::HIGH);
|
|
|
|
ASSERT_EQ(env_->GetBackgroundThreads(Env::HIGH), 3);
|
|
|
|
// Add sync points to ensure all 3 threads start
|
|
|
|
TEST_SYNC_POINT("EnvTest::ReserveThreads:1");
|
|
|
|
TEST_SYNC_POINT("EnvTest::ReserveThreads:2");
|
|
|
|
TEST_SYNC_POINT("EnvTest::ReserveThreads:3");
|
|
|
|
// Reserve 2 threads
|
|
|
|
ASSERT_EQ(2, env_->ReserveThreads(2, Env::Priority::HIGH));
|
|
|
|
|
|
|
|
// Schedule 3 tasks. Task 0 running (in this context, doing
|
|
|
|
// SleepingBackgroundTask); Task 1, 2 waiting; 3 reserved threads.
|
|
|
|
for (size_t i = 0; i < 3; i++) {
|
|
|
|
env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask, &tasks[i],
|
|
|
|
Env::Priority::HIGH);
|
|
|
|
}
|
|
|
|
ASSERT_FALSE(tasks[0].TimedWaitUntilSleeping(kWaitMicros));
|
|
|
|
ASSERT_EQ(2U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
|
|
|
|
ASSERT_TRUE(tasks[0].IsSleeping());
|
|
|
|
ASSERT_TRUE(!tasks[1].IsSleeping());
|
|
|
|
ASSERT_TRUE(!tasks[2].IsSleeping());
|
|
|
|
|
|
|
|
// Release 2 threads. Task 0, 1, 2 running; 0 reserved thread.
|
|
|
|
ASSERT_EQ(2, env_->ReleaseThreads(2, Env::Priority::HIGH));
|
|
|
|
ASSERT_FALSE(tasks[1].TimedWaitUntilSleeping(kWaitMicros));
|
|
|
|
ASSERT_FALSE(tasks[2].TimedWaitUntilSleeping(kWaitMicros));
|
|
|
|
ASSERT_EQ(0U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
|
|
|
|
ASSERT_TRUE(tasks[1].IsSleeping());
|
|
|
|
ASSERT_TRUE(tasks[2].IsSleeping());
|
|
|
|
// No more threads can be reserved
|
|
|
|
ASSERT_EQ(0, env_->ReserveThreads(3, Env::Priority::HIGH));
|
|
|
|
// Expand the number of background threads so that the last thread
|
|
|
|
// is waiting
|
|
|
|
env_->SetBackgroundThreads(4, Env::Priority::HIGH);
|
|
|
|
// Add sync point to ensure the 4th thread starts
|
|
|
|
TEST_SYNC_POINT("EnvTest::ReserveThreads:4");
|
|
|
|
// As the thread pool is expanded, we can reserve one more thread
|
|
|
|
ASSERT_EQ(1, env_->ReserveThreads(3, Env::Priority::HIGH));
|
|
|
|
// No more threads can be reserved
|
|
|
|
ASSERT_EQ(0, env_->ReserveThreads(3, Env::Priority::HIGH));
|
|
|
|
|
|
|
|
// Reset the sync points for the next iteration in BGThread or the
|
|
|
|
// next time Submit() is called
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency(
|
|
|
|
{{"ThreadPoolImpl::BGThread::WaitingThreadsInc",
|
|
|
|
"EnvTest::ReserveThreads:5"},
|
|
|
|
{"ThreadPoolImpl::BGThread::Termination", "EnvTest::ReserveThreads:6"},
|
|
|
|
{"ThreadPoolImpl::Submit::Enqueue", "EnvTest::ReserveThreads:7"}});
|
|
|
|
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
|
|
|
tasks[0].WakeUp();
|
|
|
|
ASSERT_FALSE(tasks[0].TimedWaitUntilDone(kWaitMicros));
|
|
|
|
// Add sync point to ensure the number of waiting threads increases
|
|
|
|
TEST_SYNC_POINT("EnvTest::ReserveThreads:5");
|
|
|
|
// 1 more thread can be reserved
|
|
|
|
ASSERT_EQ(1, env_->ReserveThreads(3, Env::Priority::HIGH));
|
|
|
|
// 2 reserved threads now
|
|
|
|
|
|
|
|
// Currently, two threads are blocked since the number of waiting
|
|
|
|
// threads is equal to the number of reserved threads (i.e., 2).
|
|
|
|
// If we reduce the number of background thread to 1, at least one thread
|
|
|
|
// will be the last excessive thread (here we have no control over the
|
|
|
|
// number of excessive threads because thread order does not
|
|
|
|
// necessarily follows the schedule order, but we ensure that the last thread
|
|
|
|
// shall not run any task by expanding the thread pool after we schedule
|
|
|
|
// the tasks), and thus they(it) become(s) unblocked, the number of waiting
|
|
|
|
// threads decreases to 0 or 1, but the number of reserved threads is still 2
|
|
|
|
env_->SetBackgroundThreads(1, Env::Priority::HIGH);
|
|
|
|
|
|
|
|
// Task 1,2 running; 2 reserved threads, however, in fact, we only have
|
|
|
|
// 0 or 1 waiting thread in the thread pool, proved by the
|
|
|
|
// following test, we CANNOT reserve 2 threads even though we just
|
|
|
|
// release 2
|
|
|
|
TEST_SYNC_POINT("EnvTest::ReserveThreads:6");
|
|
|
|
ASSERT_EQ(2, env_->ReleaseThreads(2, Env::Priority::HIGH));
|
|
|
|
ASSERT_GT(2, env_->ReserveThreads(2, Env::Priority::HIGH));
|
|
|
|
|
|
|
|
// Every new task will be put into the queue at this point
|
|
|
|
env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask, &tasks[3],
|
|
|
|
Env::Priority::HIGH);
|
|
|
|
TEST_SYNC_POINT("EnvTest::ReserveThreads:7");
|
|
|
|
ASSERT_EQ(1U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
|
|
|
|
ASSERT_TRUE(!tasks[3].IsSleeping());
|
|
|
|
|
|
|
|
// Set the number of threads to 3 so that Task 3 can dequeue
|
|
|
|
env_->SetBackgroundThreads(3, Env::Priority::HIGH);
|
|
|
|
// Wakup Task 1
|
|
|
|
tasks[1].WakeUp();
|
|
|
|
ASSERT_FALSE(tasks[1].TimedWaitUntilDone(kWaitMicros));
|
|
|
|
// Task 2, 3 running (Task 3 dequeue); 0 or 1 reserved thread
|
|
|
|
ASSERT_FALSE(tasks[3].TimedWaitUntilSleeping(kWaitMicros));
|
|
|
|
ASSERT_TRUE(tasks[3].IsSleeping());
|
|
|
|
ASSERT_EQ(0U, env_->GetThreadPoolQueueLen(Env::Priority::HIGH));
|
|
|
|
|
|
|
|
// At most 1 thread can be released
|
|
|
|
ASSERT_GT(2, env_->ReleaseThreads(3, Env::Priority::HIGH));
|
|
|
|
tasks[2].WakeUp();
|
|
|
|
ASSERT_FALSE(tasks[2].TimedWaitUntilDone(kWaitMicros));
|
|
|
|
tasks[3].WakeUp();
|
|
|
|
ASSERT_FALSE(tasks[3].TimedWaitUntilDone(kWaitMicros));
|
|
|
|
WaitThreadPoolsEmpty();
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
|
|
|
}
|
|
|
|
|
2017-01-09 23:37:57 +00:00
|
|
|
#if (defined OS_LINUX || defined OS_WIN)
|
2014-04-10 04:17:14 +00:00
|
|
|
namespace {
|
2013-01-31 23:20:24 +00:00
|
|
|
bool IsSingleVarint(const std::string& s) {
|
|
|
|
Slice slice(s);
|
|
|
|
|
|
|
|
uint64_t v;
|
|
|
|
if (!GetVarint64(&slice, &v)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return slice.size() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsUniqueIDValid(const std::string& s) {
|
|
|
|
return !s.empty() && !IsSingleVarint(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t MAX_ID_SIZE = 100;
|
|
|
|
char temp_id[MAX_ID_SIZE];
|
|
|
|
|
test: avoid vuln-inducing use of temporary directory
Summary:
Without this change, someone on the machine on which
I run "make check" could cause me to overwrite arbitrary
files owned by me, via a symlink attack.
Instead of using a predictable temporary directory and
accepting to use a preexisting one, always create a new
one using mkdtemp. If $TEST_IOCTL_FRIENDLY_TMPDIR is
set and usable, attempt first to find a usable
temporary directory therein. If not, or if unusable,
then try /var/tmp and /tmp. If none of those is usable
abort with a diagnostic.
To do that, I added a new class.
Its constructor finds a suitable directory or aborts,
the sole member prints that directory's name, and the
destructor unlinks what should be an empty directory.
Note that while the code before this did not remove
its temporary directory, there was only one per $UID.
Now, there would be at least one per run or one per
test, depending on implementation, so it is important
to remove them.
Test Plan:
Run this on a fedora rawhide system, where /tmp
is a tmpfs file system, and /var/tmp is ext4.
# This gives a diagnostic that /dev/shm is not suitable
# and ends up using /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/dev/shm ./env_test
# Uses /var/tmp; same as when envvar not set.
TEST_IOCTL_FRIENDLY_TMPDIR=/var/tmp ./env_test
# Uses /tmp unless it's tmpfs, in which case it gives
# a diagnostic and uses /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/tmp ./env_test
Reviewers: ljin, rven, igor.sugak, yhchiang, sdong, igor
Reviewed By: igor
Subscribers: dhruba
Differential Revision: https://reviews.facebook.net/D37287
2015-04-23 15:00:56 +00:00
|
|
|
} // namespace
|
skip ioctl-using tests when not supported
Summary:
[NB: this is a prerequisite for the /tmp-abuse-fixing patch]
This avoids spurious test failure on Linux systems
like Fedora for which /tmp is a tmpfs file system.
On a devtmpfs file
system, ioctl(fd, FS_IOC_GETVERSION, &version) returns -1 with
errno == ENOTTTY, indicating that that ioctl is not supported
on such a file system. Do not let this cause test failures, e.g.,
where env_test would assert that file->GetUniqueId(...) > 0.
Before this change, ./env_test would fail these three tests
on a fedora rawhide system:
[ FAILED ] 3 tests, listed below:
[ FAILED ] EnvPosixTest.RandomAccessUniqueID
[ FAILED ] EnvPosixTest.RandomAccessUniqueIDConcurrent
[ FAILED ] EnvPosixTest.RandomAccessUniqueIDDeletes
3 FAILED TESTS
The fix:
When support for that ioctl is lacking, skip each affected test.
Could be improved by noting which sub-tests are being skipped.
Test Plan:
run these on F21 and note that they now pass.
TEST_TMPDIR=/dev/shm/rdb ./env_test
./env_test
Reviewers: ljin, rven, igor.sugak, yhchiang, sdong, igor
Reviewed By: igor
Subscribers: dhruba
Differential Revision: https://reviews.facebook.net/D37323
2015-04-18 03:39:02 +00:00
|
|
|
|
|
|
|
// Determine whether we can use the FS_IOC_GETVERSION ioctl
|
test: avoid vuln-inducing use of temporary directory
Summary:
Without this change, someone on the machine on which
I run "make check" could cause me to overwrite arbitrary
files owned by me, via a symlink attack.
Instead of using a predictable temporary directory and
accepting to use a preexisting one, always create a new
one using mkdtemp. If $TEST_IOCTL_FRIENDLY_TMPDIR is
set and usable, attempt first to find a usable
temporary directory therein. If not, or if unusable,
then try /var/tmp and /tmp. If none of those is usable
abort with a diagnostic.
To do that, I added a new class.
Its constructor finds a suitable directory or aborts,
the sole member prints that directory's name, and the
destructor unlinks what should be an empty directory.
Note that while the code before this did not remove
its temporary directory, there was only one per $UID.
Now, there would be at least one per run or one per
test, depending on implementation, so it is important
to remove them.
Test Plan:
Run this on a fedora rawhide system, where /tmp
is a tmpfs file system, and /var/tmp is ext4.
# This gives a diagnostic that /dev/shm is not suitable
# and ends up using /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/dev/shm ./env_test
# Uses /var/tmp; same as when envvar not set.
TEST_IOCTL_FRIENDLY_TMPDIR=/var/tmp ./env_test
# Uses /tmp unless it's tmpfs, in which case it gives
# a diagnostic and uses /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/tmp ./env_test
Reviewers: ljin, rven, igor.sugak, yhchiang, sdong, igor
Reviewed By: igor
Subscribers: dhruba
Differential Revision: https://reviews.facebook.net/D37287
2015-04-23 15:00:56 +00:00
|
|
|
// on a file in directory DIR. Create a temporary file therein,
|
skip ioctl-using tests when not supported
Summary:
[NB: this is a prerequisite for the /tmp-abuse-fixing patch]
This avoids spurious test failure on Linux systems
like Fedora for which /tmp is a tmpfs file system.
On a devtmpfs file
system, ioctl(fd, FS_IOC_GETVERSION, &version) returns -1 with
errno == ENOTTTY, indicating that that ioctl is not supported
on such a file system. Do not let this cause test failures, e.g.,
where env_test would assert that file->GetUniqueId(...) > 0.
Before this change, ./env_test would fail these three tests
on a fedora rawhide system:
[ FAILED ] 3 tests, listed below:
[ FAILED ] EnvPosixTest.RandomAccessUniqueID
[ FAILED ] EnvPosixTest.RandomAccessUniqueIDConcurrent
[ FAILED ] EnvPosixTest.RandomAccessUniqueIDDeletes
3 FAILED TESTS
The fix:
When support for that ioctl is lacking, skip each affected test.
Could be improved by noting which sub-tests are being skipped.
Test Plan:
run these on F21 and note that they now pass.
TEST_TMPDIR=/dev/shm/rdb ./env_test
./env_test
Reviewers: ljin, rven, igor.sugak, yhchiang, sdong, igor
Reviewed By: igor
Subscribers: dhruba
Differential Revision: https://reviews.facebook.net/D37323
2015-04-18 03:39:02 +00:00
|
|
|
// try to apply the ioctl (save that result), cleanup and
|
|
|
|
// return the result. Return true if it is supported, and
|
|
|
|
// false if anything fails.
|
test: avoid vuln-inducing use of temporary directory
Summary:
Without this change, someone on the machine on which
I run "make check" could cause me to overwrite arbitrary
files owned by me, via a symlink attack.
Instead of using a predictable temporary directory and
accepting to use a preexisting one, always create a new
one using mkdtemp. If $TEST_IOCTL_FRIENDLY_TMPDIR is
set and usable, attempt first to find a usable
temporary directory therein. If not, or if unusable,
then try /var/tmp and /tmp. If none of those is usable
abort with a diagnostic.
To do that, I added a new class.
Its constructor finds a suitable directory or aborts,
the sole member prints that directory's name, and the
destructor unlinks what should be an empty directory.
Note that while the code before this did not remove
its temporary directory, there was only one per $UID.
Now, there would be at least one per run or one per
test, depending on implementation, so it is important
to remove them.
Test Plan:
Run this on a fedora rawhide system, where /tmp
is a tmpfs file system, and /var/tmp is ext4.
# This gives a diagnostic that /dev/shm is not suitable
# and ends up using /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/dev/shm ./env_test
# Uses /var/tmp; same as when envvar not set.
TEST_IOCTL_FRIENDLY_TMPDIR=/var/tmp ./env_test
# Uses /tmp unless it's tmpfs, in which case it gives
# a diagnostic and uses /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/tmp ./env_test
Reviewers: ljin, rven, igor.sugak, yhchiang, sdong, igor
Reviewed By: igor
Subscribers: dhruba
Differential Revision: https://reviews.facebook.net/D37287
2015-04-23 15:00:56 +00:00
|
|
|
// Note that this function "knows" that dir has just been created
|
|
|
|
// and is empty, so we create a simply-named test file: "f".
|
|
|
|
bool ioctl_support__FS_IOC_GETVERSION(const std::string& dir) {
|
2017-01-09 23:37:57 +00:00
|
|
|
#ifdef OS_WIN
|
|
|
|
return true;
|
|
|
|
#else
|
test: avoid vuln-inducing use of temporary directory
Summary:
Without this change, someone on the machine on which
I run "make check" could cause me to overwrite arbitrary
files owned by me, via a symlink attack.
Instead of using a predictable temporary directory and
accepting to use a preexisting one, always create a new
one using mkdtemp. If $TEST_IOCTL_FRIENDLY_TMPDIR is
set and usable, attempt first to find a usable
temporary directory therein. If not, or if unusable,
then try /var/tmp and /tmp. If none of those is usable
abort with a diagnostic.
To do that, I added a new class.
Its constructor finds a suitable directory or aborts,
the sole member prints that directory's name, and the
destructor unlinks what should be an empty directory.
Note that while the code before this did not remove
its temporary directory, there was only one per $UID.
Now, there would be at least one per run or one per
test, depending on implementation, so it is important
to remove them.
Test Plan:
Run this on a fedora rawhide system, where /tmp
is a tmpfs file system, and /var/tmp is ext4.
# This gives a diagnostic that /dev/shm is not suitable
# and ends up using /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/dev/shm ./env_test
# Uses /var/tmp; same as when envvar not set.
TEST_IOCTL_FRIENDLY_TMPDIR=/var/tmp ./env_test
# Uses /tmp unless it's tmpfs, in which case it gives
# a diagnostic and uses /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/tmp ./env_test
Reviewers: ljin, rven, igor.sugak, yhchiang, sdong, igor
Reviewed By: igor
Subscribers: dhruba
Differential Revision: https://reviews.facebook.net/D37287
2015-04-23 15:00:56 +00:00
|
|
|
const std::string file = dir + "/f";
|
|
|
|
int fd;
|
|
|
|
do {
|
|
|
|
fd = open(file.c_str(), O_CREAT | O_RDWR | O_TRUNC, 0644);
|
|
|
|
} while (fd < 0 && errno == EINTR);
|
skip ioctl-using tests when not supported
Summary:
[NB: this is a prerequisite for the /tmp-abuse-fixing patch]
This avoids spurious test failure on Linux systems
like Fedora for which /tmp is a tmpfs file system.
On a devtmpfs file
system, ioctl(fd, FS_IOC_GETVERSION, &version) returns -1 with
errno == ENOTTTY, indicating that that ioctl is not supported
on such a file system. Do not let this cause test failures, e.g.,
where env_test would assert that file->GetUniqueId(...) > 0.
Before this change, ./env_test would fail these three tests
on a fedora rawhide system:
[ FAILED ] 3 tests, listed below:
[ FAILED ] EnvPosixTest.RandomAccessUniqueID
[ FAILED ] EnvPosixTest.RandomAccessUniqueIDConcurrent
[ FAILED ] EnvPosixTest.RandomAccessUniqueIDDeletes
3 FAILED TESTS
The fix:
When support for that ioctl is lacking, skip each affected test.
Could be improved by noting which sub-tests are being skipped.
Test Plan:
run these on F21 and note that they now pass.
TEST_TMPDIR=/dev/shm/rdb ./env_test
./env_test
Reviewers: ljin, rven, igor.sugak, yhchiang, sdong, igor
Reviewed By: igor
Subscribers: dhruba
Differential Revision: https://reviews.facebook.net/D37323
2015-04-18 03:39:02 +00:00
|
|
|
long int version;
|
|
|
|
bool ok = (fd >= 0 && ioctl(fd, FS_IOC_GETVERSION, &version) >= 0);
|
|
|
|
|
|
|
|
close(fd);
|
test: avoid vuln-inducing use of temporary directory
Summary:
Without this change, someone on the machine on which
I run "make check" could cause me to overwrite arbitrary
files owned by me, via a symlink attack.
Instead of using a predictable temporary directory and
accepting to use a preexisting one, always create a new
one using mkdtemp. If $TEST_IOCTL_FRIENDLY_TMPDIR is
set and usable, attempt first to find a usable
temporary directory therein. If not, or if unusable,
then try /var/tmp and /tmp. If none of those is usable
abort with a diagnostic.
To do that, I added a new class.
Its constructor finds a suitable directory or aborts,
the sole member prints that directory's name, and the
destructor unlinks what should be an empty directory.
Note that while the code before this did not remove
its temporary directory, there was only one per $UID.
Now, there would be at least one per run or one per
test, depending on implementation, so it is important
to remove them.
Test Plan:
Run this on a fedora rawhide system, where /tmp
is a tmpfs file system, and /var/tmp is ext4.
# This gives a diagnostic that /dev/shm is not suitable
# and ends up using /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/dev/shm ./env_test
# Uses /var/tmp; same as when envvar not set.
TEST_IOCTL_FRIENDLY_TMPDIR=/var/tmp ./env_test
# Uses /tmp unless it's tmpfs, in which case it gives
# a diagnostic and uses /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/tmp ./env_test
Reviewers: ljin, rven, igor.sugak, yhchiang, sdong, igor
Reviewed By: igor
Subscribers: dhruba
Differential Revision: https://reviews.facebook.net/D37287
2015-04-23 15:00:56 +00:00
|
|
|
unlink(file.c_str());
|
skip ioctl-using tests when not supported
Summary:
[NB: this is a prerequisite for the /tmp-abuse-fixing patch]
This avoids spurious test failure on Linux systems
like Fedora for which /tmp is a tmpfs file system.
On a devtmpfs file
system, ioctl(fd, FS_IOC_GETVERSION, &version) returns -1 with
errno == ENOTTTY, indicating that that ioctl is not supported
on such a file system. Do not let this cause test failures, e.g.,
where env_test would assert that file->GetUniqueId(...) > 0.
Before this change, ./env_test would fail these three tests
on a fedora rawhide system:
[ FAILED ] 3 tests, listed below:
[ FAILED ] EnvPosixTest.RandomAccessUniqueID
[ FAILED ] EnvPosixTest.RandomAccessUniqueIDConcurrent
[ FAILED ] EnvPosixTest.RandomAccessUniqueIDDeletes
3 FAILED TESTS
The fix:
When support for that ioctl is lacking, skip each affected test.
Could be improved by noting which sub-tests are being skipped.
Test Plan:
run these on F21 and note that they now pass.
TEST_TMPDIR=/dev/shm/rdb ./env_test
./env_test
Reviewers: ljin, rven, igor.sugak, yhchiang, sdong, igor
Reviewed By: igor
Subscribers: dhruba
Differential Revision: https://reviews.facebook.net/D37323
2015-04-18 03:39:02 +00:00
|
|
|
|
|
|
|
return ok;
|
2017-01-09 23:37:57 +00:00
|
|
|
#endif
|
skip ioctl-using tests when not supported
Summary:
[NB: this is a prerequisite for the /tmp-abuse-fixing patch]
This avoids spurious test failure on Linux systems
like Fedora for which /tmp is a tmpfs file system.
On a devtmpfs file
system, ioctl(fd, FS_IOC_GETVERSION, &version) returns -1 with
errno == ENOTTTY, indicating that that ioctl is not supported
on such a file system. Do not let this cause test failures, e.g.,
where env_test would assert that file->GetUniqueId(...) > 0.
Before this change, ./env_test would fail these three tests
on a fedora rawhide system:
[ FAILED ] 3 tests, listed below:
[ FAILED ] EnvPosixTest.RandomAccessUniqueID
[ FAILED ] EnvPosixTest.RandomAccessUniqueIDConcurrent
[ FAILED ] EnvPosixTest.RandomAccessUniqueIDDeletes
3 FAILED TESTS
The fix:
When support for that ioctl is lacking, skip each affected test.
Could be improved by noting which sub-tests are being skipped.
Test Plan:
run these on F21 and note that they now pass.
TEST_TMPDIR=/dev/shm/rdb ./env_test
./env_test
Reviewers: ljin, rven, igor.sugak, yhchiang, sdong, igor
Reviewed By: igor
Subscribers: dhruba
Differential Revision: https://reviews.facebook.net/D37323
2015-04-18 03:39:02 +00:00
|
|
|
}
|
|
|
|
|
test: avoid vuln-inducing use of temporary directory
Summary:
Without this change, someone on the machine on which
I run "make check" could cause me to overwrite arbitrary
files owned by me, via a symlink attack.
Instead of using a predictable temporary directory and
accepting to use a preexisting one, always create a new
one using mkdtemp. If $TEST_IOCTL_FRIENDLY_TMPDIR is
set and usable, attempt first to find a usable
temporary directory therein. If not, or if unusable,
then try /var/tmp and /tmp. If none of those is usable
abort with a diagnostic.
To do that, I added a new class.
Its constructor finds a suitable directory or aborts,
the sole member prints that directory's name, and the
destructor unlinks what should be an empty directory.
Note that while the code before this did not remove
its temporary directory, there was only one per $UID.
Now, there would be at least one per run or one per
test, depending on implementation, so it is important
to remove them.
Test Plan:
Run this on a fedora rawhide system, where /tmp
is a tmpfs file system, and /var/tmp is ext4.
# This gives a diagnostic that /dev/shm is not suitable
# and ends up using /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/dev/shm ./env_test
# Uses /var/tmp; same as when envvar not set.
TEST_IOCTL_FRIENDLY_TMPDIR=/var/tmp ./env_test
# Uses /tmp unless it's tmpfs, in which case it gives
# a diagnostic and uses /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/tmp ./env_test
Reviewers: ljin, rven, igor.sugak, yhchiang, sdong, igor
Reviewed By: igor
Subscribers: dhruba
Differential Revision: https://reviews.facebook.net/D37287
2015-04-23 15:00:56 +00:00
|
|
|
// To ensure that Env::GetUniqueId-related tests work correctly, the files
|
|
|
|
// should be stored in regular storage like "hard disk" or "flash device",
|
|
|
|
// and not on a tmpfs file system (like /dev/shm and /tmp on some systems).
|
|
|
|
// Otherwise we cannot get the correct id.
|
|
|
|
//
|
|
|
|
// This function serves as the replacement for test::TmpDir(), which may be
|
|
|
|
// customized to be on a file system that doesn't work with GetUniqueId().
|
|
|
|
|
|
|
|
class IoctlFriendlyTmpdir {
|
|
|
|
public:
|
|
|
|
explicit IoctlFriendlyTmpdir() {
|
|
|
|
char dir_buf[100];
|
|
|
|
|
2022-10-25 00:54:14 +00:00
|
|
|
const char* fmt = "%s/rocksdb.XXXXXX";
|
|
|
|
const char* tmp = getenv("TEST_IOCTL_FRIENDLY_TMPDIR");
|
2017-01-09 23:37:57 +00:00
|
|
|
|
|
|
|
#ifdef OS_WIN
|
|
|
|
#define rmdir _rmdir
|
2022-10-25 00:54:14 +00:00
|
|
|
if (tmp == nullptr) {
|
2017-01-09 23:37:57 +00:00
|
|
|
tmp = getenv("TMP");
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(dir_buf, sizeof dir_buf, fmt, tmp);
|
|
|
|
auto result = _mktemp(dir_buf);
|
|
|
|
assert(result != nullptr);
|
|
|
|
BOOL ret = CreateDirectory(dir_buf, NULL);
|
|
|
|
assert(ret == TRUE);
|
|
|
|
dir_ = dir_buf;
|
|
|
|
#else
|
|
|
|
std::list<std::string> candidate_dir_list = {"/var/tmp", "/tmp"};
|
|
|
|
|
test: avoid vuln-inducing use of temporary directory
Summary:
Without this change, someone on the machine on which
I run "make check" could cause me to overwrite arbitrary
files owned by me, via a symlink attack.
Instead of using a predictable temporary directory and
accepting to use a preexisting one, always create a new
one using mkdtemp. If $TEST_IOCTL_FRIENDLY_TMPDIR is
set and usable, attempt first to find a usable
temporary directory therein. If not, or if unusable,
then try /var/tmp and /tmp. If none of those is usable
abort with a diagnostic.
To do that, I added a new class.
Its constructor finds a suitable directory or aborts,
the sole member prints that directory's name, and the
destructor unlinks what should be an empty directory.
Note that while the code before this did not remove
its temporary directory, there was only one per $UID.
Now, there would be at least one per run or one per
test, depending on implementation, so it is important
to remove them.
Test Plan:
Run this on a fedora rawhide system, where /tmp
is a tmpfs file system, and /var/tmp is ext4.
# This gives a diagnostic that /dev/shm is not suitable
# and ends up using /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/dev/shm ./env_test
# Uses /var/tmp; same as when envvar not set.
TEST_IOCTL_FRIENDLY_TMPDIR=/var/tmp ./env_test
# Uses /tmp unless it's tmpfs, in which case it gives
# a diagnostic and uses /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/tmp ./env_test
Reviewers: ljin, rven, igor.sugak, yhchiang, sdong, igor
Reviewed By: igor
Subscribers: dhruba
Differential Revision: https://reviews.facebook.net/D37287
2015-04-23 15:00:56 +00:00
|
|
|
// If $TEST_IOCTL_FRIENDLY_TMPDIR/rocksdb.XXXXXX fits, use
|
|
|
|
// $TEST_IOCTL_FRIENDLY_TMPDIR; subtract 2 for the "%s", and
|
|
|
|
// add 1 for the trailing NUL byte.
|
|
|
|
if (tmp && strlen(tmp) + strlen(fmt) - 2 + 1 <= sizeof dir_buf) {
|
|
|
|
// use $TEST_IOCTL_FRIENDLY_TMPDIR value
|
|
|
|
candidate_dir_list.push_front(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const std::string& d : candidate_dir_list) {
|
|
|
|
snprintf(dir_buf, sizeof dir_buf, fmt, d.c_str());
|
|
|
|
if (mkdtemp(dir_buf)) {
|
|
|
|
if (ioctl_support__FS_IOC_GETVERSION(dir_buf)) {
|
|
|
|
dir_ = dir_buf;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// Diagnose ioctl-related failure only if this is the
|
|
|
|
// directory specified via that envvar.
|
2016-04-22 23:49:12 +00:00
|
|
|
if (tmp && tmp == d) {
|
2022-10-25 00:54:14 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"TEST_IOCTL_FRIENDLY_TMPDIR-specified directory is "
|
|
|
|
"not suitable: %s\n",
|
|
|
|
d.c_str());
|
test: avoid vuln-inducing use of temporary directory
Summary:
Without this change, someone on the machine on which
I run "make check" could cause me to overwrite arbitrary
files owned by me, via a symlink attack.
Instead of using a predictable temporary directory and
accepting to use a preexisting one, always create a new
one using mkdtemp. If $TEST_IOCTL_FRIENDLY_TMPDIR is
set and usable, attempt first to find a usable
temporary directory therein. If not, or if unusable,
then try /var/tmp and /tmp. If none of those is usable
abort with a diagnostic.
To do that, I added a new class.
Its constructor finds a suitable directory or aborts,
the sole member prints that directory's name, and the
destructor unlinks what should be an empty directory.
Note that while the code before this did not remove
its temporary directory, there was only one per $UID.
Now, there would be at least one per run or one per
test, depending on implementation, so it is important
to remove them.
Test Plan:
Run this on a fedora rawhide system, where /tmp
is a tmpfs file system, and /var/tmp is ext4.
# This gives a diagnostic that /dev/shm is not suitable
# and ends up using /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/dev/shm ./env_test
# Uses /var/tmp; same as when envvar not set.
TEST_IOCTL_FRIENDLY_TMPDIR=/var/tmp ./env_test
# Uses /tmp unless it's tmpfs, in which case it gives
# a diagnostic and uses /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/tmp ./env_test
Reviewers: ljin, rven, igor.sugak, yhchiang, sdong, igor
Reviewed By: igor
Subscribers: dhruba
Differential Revision: https://reviews.facebook.net/D37287
2015-04-23 15:00:56 +00:00
|
|
|
}
|
|
|
|
rmdir(dir_buf); // ignore failure
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// mkdtemp failed: diagnose it, but don't give up.
|
|
|
|
fprintf(stderr, "mkdtemp(%s/...) failed: %s\n", d.c_str(),
|
2021-03-25 06:06:31 +00:00
|
|
|
errnoStr(errno).c_str());
|
test: avoid vuln-inducing use of temporary directory
Summary:
Without this change, someone on the machine on which
I run "make check" could cause me to overwrite arbitrary
files owned by me, via a symlink attack.
Instead of using a predictable temporary directory and
accepting to use a preexisting one, always create a new
one using mkdtemp. If $TEST_IOCTL_FRIENDLY_TMPDIR is
set and usable, attempt first to find a usable
temporary directory therein. If not, or if unusable,
then try /var/tmp and /tmp. If none of those is usable
abort with a diagnostic.
To do that, I added a new class.
Its constructor finds a suitable directory or aborts,
the sole member prints that directory's name, and the
destructor unlinks what should be an empty directory.
Note that while the code before this did not remove
its temporary directory, there was only one per $UID.
Now, there would be at least one per run or one per
test, depending on implementation, so it is important
to remove them.
Test Plan:
Run this on a fedora rawhide system, where /tmp
is a tmpfs file system, and /var/tmp is ext4.
# This gives a diagnostic that /dev/shm is not suitable
# and ends up using /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/dev/shm ./env_test
# Uses /var/tmp; same as when envvar not set.
TEST_IOCTL_FRIENDLY_TMPDIR=/var/tmp ./env_test
# Uses /tmp unless it's tmpfs, in which case it gives
# a diagnostic and uses /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/tmp ./env_test
Reviewers: ljin, rven, igor.sugak, yhchiang, sdong, igor
Reviewed By: igor
Subscribers: dhruba
Differential Revision: https://reviews.facebook.net/D37287
2015-04-23 15:00:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-11 00:34:38 +00:00
|
|
|
// check if it's running test within a docker container, in which case, the
|
|
|
|
// file system inside `overlayfs` may not support FS_IOC_GETVERSION
|
|
|
|
// skip the tests
|
|
|
|
struct stat buffer;
|
|
|
|
if (stat("/.dockerenv", &buffer) == 0) {
|
|
|
|
is_supported_ = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-25 00:54:14 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"failed to find an ioctl-friendly temporary directory;"
|
test: avoid vuln-inducing use of temporary directory
Summary:
Without this change, someone on the machine on which
I run "make check" could cause me to overwrite arbitrary
files owned by me, via a symlink attack.
Instead of using a predictable temporary directory and
accepting to use a preexisting one, always create a new
one using mkdtemp. If $TEST_IOCTL_FRIENDLY_TMPDIR is
set and usable, attempt first to find a usable
temporary directory therein. If not, or if unusable,
then try /var/tmp and /tmp. If none of those is usable
abort with a diagnostic.
To do that, I added a new class.
Its constructor finds a suitable directory or aborts,
the sole member prints that directory's name, and the
destructor unlinks what should be an empty directory.
Note that while the code before this did not remove
its temporary directory, there was only one per $UID.
Now, there would be at least one per run or one per
test, depending on implementation, so it is important
to remove them.
Test Plan:
Run this on a fedora rawhide system, where /tmp
is a tmpfs file system, and /var/tmp is ext4.
# This gives a diagnostic that /dev/shm is not suitable
# and ends up using /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/dev/shm ./env_test
# Uses /var/tmp; same as when envvar not set.
TEST_IOCTL_FRIENDLY_TMPDIR=/var/tmp ./env_test
# Uses /tmp unless it's tmpfs, in which case it gives
# a diagnostic and uses /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/tmp ./env_test
Reviewers: ljin, rven, igor.sugak, yhchiang, sdong, igor
Reviewed By: igor
Subscribers: dhruba
Differential Revision: https://reviews.facebook.net/D37287
2015-04-23 15:00:56 +00:00
|
|
|
" specify one via the TEST_IOCTL_FRIENDLY_TMPDIR envvar\n");
|
|
|
|
std::abort();
|
2017-01-09 23:37:57 +00:00
|
|
|
#endif
|
2022-08-11 00:34:38 +00:00
|
|
|
}
|
test: avoid vuln-inducing use of temporary directory
Summary:
Without this change, someone on the machine on which
I run "make check" could cause me to overwrite arbitrary
files owned by me, via a symlink attack.
Instead of using a predictable temporary directory and
accepting to use a preexisting one, always create a new
one using mkdtemp. If $TEST_IOCTL_FRIENDLY_TMPDIR is
set and usable, attempt first to find a usable
temporary directory therein. If not, or if unusable,
then try /var/tmp and /tmp. If none of those is usable
abort with a diagnostic.
To do that, I added a new class.
Its constructor finds a suitable directory or aborts,
the sole member prints that directory's name, and the
destructor unlinks what should be an empty directory.
Note that while the code before this did not remove
its temporary directory, there was only one per $UID.
Now, there would be at least one per run or one per
test, depending on implementation, so it is important
to remove them.
Test Plan:
Run this on a fedora rawhide system, where /tmp
is a tmpfs file system, and /var/tmp is ext4.
# This gives a diagnostic that /dev/shm is not suitable
# and ends up using /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/dev/shm ./env_test
# Uses /var/tmp; same as when envvar not set.
TEST_IOCTL_FRIENDLY_TMPDIR=/var/tmp ./env_test
# Uses /tmp unless it's tmpfs, in which case it gives
# a diagnostic and uses /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/tmp ./env_test
Reviewers: ljin, rven, igor.sugak, yhchiang, sdong, igor
Reviewed By: igor
Subscribers: dhruba
Differential Revision: https://reviews.facebook.net/D37287
2015-04-23 15:00:56 +00:00
|
|
|
|
2022-10-25 00:54:14 +00:00
|
|
|
~IoctlFriendlyTmpdir() { rmdir(dir_.c_str()); }
|
2017-01-09 23:37:57 +00:00
|
|
|
|
2022-10-25 00:54:14 +00:00
|
|
|
const std::string& name() const { return dir_; }
|
test: avoid vuln-inducing use of temporary directory
Summary:
Without this change, someone on the machine on which
I run "make check" could cause me to overwrite arbitrary
files owned by me, via a symlink attack.
Instead of using a predictable temporary directory and
accepting to use a preexisting one, always create a new
one using mkdtemp. If $TEST_IOCTL_FRIENDLY_TMPDIR is
set and usable, attempt first to find a usable
temporary directory therein. If not, or if unusable,
then try /var/tmp and /tmp. If none of those is usable
abort with a diagnostic.
To do that, I added a new class.
Its constructor finds a suitable directory or aborts,
the sole member prints that directory's name, and the
destructor unlinks what should be an empty directory.
Note that while the code before this did not remove
its temporary directory, there was only one per $UID.
Now, there would be at least one per run or one per
test, depending on implementation, so it is important
to remove them.
Test Plan:
Run this on a fedora rawhide system, where /tmp
is a tmpfs file system, and /var/tmp is ext4.
# This gives a diagnostic that /dev/shm is not suitable
# and ends up using /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/dev/shm ./env_test
# Uses /var/tmp; same as when envvar not set.
TEST_IOCTL_FRIENDLY_TMPDIR=/var/tmp ./env_test
# Uses /tmp unless it's tmpfs, in which case it gives
# a diagnostic and uses /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/tmp ./env_test
Reviewers: ljin, rven, igor.sugak, yhchiang, sdong, igor
Reviewed By: igor
Subscribers: dhruba
Differential Revision: https://reviews.facebook.net/D37287
2015-04-23 15:00:56 +00:00
|
|
|
|
2022-08-11 00:34:38 +00:00
|
|
|
bool is_supported() const { return is_supported_; }
|
|
|
|
|
test: avoid vuln-inducing use of temporary directory
Summary:
Without this change, someone on the machine on which
I run "make check" could cause me to overwrite arbitrary
files owned by me, via a symlink attack.
Instead of using a predictable temporary directory and
accepting to use a preexisting one, always create a new
one using mkdtemp. If $TEST_IOCTL_FRIENDLY_TMPDIR is
set and usable, attempt first to find a usable
temporary directory therein. If not, or if unusable,
then try /var/tmp and /tmp. If none of those is usable
abort with a diagnostic.
To do that, I added a new class.
Its constructor finds a suitable directory or aborts,
the sole member prints that directory's name, and the
destructor unlinks what should be an empty directory.
Note that while the code before this did not remove
its temporary directory, there was only one per $UID.
Now, there would be at least one per run or one per
test, depending on implementation, so it is important
to remove them.
Test Plan:
Run this on a fedora rawhide system, where /tmp
is a tmpfs file system, and /var/tmp is ext4.
# This gives a diagnostic that /dev/shm is not suitable
# and ends up using /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/dev/shm ./env_test
# Uses /var/tmp; same as when envvar not set.
TEST_IOCTL_FRIENDLY_TMPDIR=/var/tmp ./env_test
# Uses /tmp unless it's tmpfs, in which case it gives
# a diagnostic and uses /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/tmp ./env_test
Reviewers: ljin, rven, igor.sugak, yhchiang, sdong, igor
Reviewed By: igor
Subscribers: dhruba
Differential Revision: https://reviews.facebook.net/D37287
2015-04-23 15:00:56 +00:00
|
|
|
private:
|
|
|
|
std::string dir_;
|
2022-08-11 00:34:38 +00:00
|
|
|
|
|
|
|
bool is_supported_ = true;
|
test: avoid vuln-inducing use of temporary directory
Summary:
Without this change, someone on the machine on which
I run "make check" could cause me to overwrite arbitrary
files owned by me, via a symlink attack.
Instead of using a predictable temporary directory and
accepting to use a preexisting one, always create a new
one using mkdtemp. If $TEST_IOCTL_FRIENDLY_TMPDIR is
set and usable, attempt first to find a usable
temporary directory therein. If not, or if unusable,
then try /var/tmp and /tmp. If none of those is usable
abort with a diagnostic.
To do that, I added a new class.
Its constructor finds a suitable directory or aborts,
the sole member prints that directory's name, and the
destructor unlinks what should be an empty directory.
Note that while the code before this did not remove
its temporary directory, there was only one per $UID.
Now, there would be at least one per run or one per
test, depending on implementation, so it is important
to remove them.
Test Plan:
Run this on a fedora rawhide system, where /tmp
is a tmpfs file system, and /var/tmp is ext4.
# This gives a diagnostic that /dev/shm is not suitable
# and ends up using /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/dev/shm ./env_test
# Uses /var/tmp; same as when envvar not set.
TEST_IOCTL_FRIENDLY_TMPDIR=/var/tmp ./env_test
# Uses /tmp unless it's tmpfs, in which case it gives
# a diagnostic and uses /var/tmp.
TEST_IOCTL_FRIENDLY_TMPDIR=/tmp ./env_test
Reviewers: ljin, rven, igor.sugak, yhchiang, sdong, igor
Reviewed By: igor
Subscribers: dhruba
Differential Revision: https://reviews.facebook.net/D37287
2015-04-23 15:00:56 +00:00
|
|
|
};
|
2014-03-05 22:28:47 +00:00
|
|
|
|
2017-02-23 19:17:49 +00:00
|
|
|
TEST_F(EnvPosixTest, PositionedAppend) {
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<WritableFile> writable_file;
|
2017-02-23 19:17:49 +00:00
|
|
|
EnvOptions options;
|
|
|
|
options.use_direct_writes = true;
|
|
|
|
options.use_mmap_writes = false;
|
2022-08-11 00:34:38 +00:00
|
|
|
std::string fname = test::PerThreadDBPath(env_, "positioned_append");
|
|
|
|
SetupSyncPointsToMockDirectIO();
|
|
|
|
|
|
|
|
ASSERT_OK(env_->NewWritableFile(fname, &writable_file, options));
|
2017-02-23 19:17:49 +00:00
|
|
|
const size_t kBlockSize = 4096;
|
|
|
|
const size_t kDataSize = kPageSize;
|
|
|
|
// Write a page worth of 'a'
|
|
|
|
auto data_ptr = NewAligned(kDataSize, 'a');
|
|
|
|
Slice data_a(data_ptr.get(), kDataSize);
|
|
|
|
ASSERT_OK(writable_file->PositionedAppend(data_a, 0U));
|
|
|
|
// Write a page worth of 'b' right after the first sector
|
|
|
|
data_ptr = NewAligned(kDataSize, 'b');
|
|
|
|
Slice data_b(data_ptr.get(), kDataSize);
|
|
|
|
ASSERT_OK(writable_file->PositionedAppend(data_b, kBlockSize));
|
|
|
|
ASSERT_OK(writable_file->Close());
|
|
|
|
// The file now has 1 sector worth of a followed by a page worth of b
|
|
|
|
|
|
|
|
// Verify the above
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<SequentialFile> seq_file;
|
2022-08-11 00:34:38 +00:00
|
|
|
ASSERT_OK(env_->NewSequentialFile(fname, &seq_file, options));
|
2020-02-22 15:59:38 +00:00
|
|
|
size_t scratch_len = kPageSize * 2;
|
|
|
|
std::unique_ptr<char[]> scratch(new char[scratch_len]);
|
2017-02-23 19:17:49 +00:00
|
|
|
Slice result;
|
2020-02-22 15:59:38 +00:00
|
|
|
ASSERT_OK(seq_file->Read(scratch_len, &result, scratch.get()));
|
2017-02-23 19:17:49 +00:00
|
|
|
ASSERT_EQ(kPageSize + kBlockSize, result.size());
|
|
|
|
ASSERT_EQ('a', result[kBlockSize - 1]);
|
|
|
|
ASSERT_EQ('b', result[kBlockSize]);
|
2016-11-19 01:06:37 +00:00
|
|
|
}
|
skip ioctl-using tests when not supported
Summary:
[NB: this is a prerequisite for the /tmp-abuse-fixing patch]
This avoids spurious test failure on Linux systems
like Fedora for which /tmp is a tmpfs file system.
On a devtmpfs file
system, ioctl(fd, FS_IOC_GETVERSION, &version) returns -1 with
errno == ENOTTTY, indicating that that ioctl is not supported
on such a file system. Do not let this cause test failures, e.g.,
where env_test would assert that file->GetUniqueId(...) > 0.
Before this change, ./env_test would fail these three tests
on a fedora rawhide system:
[ FAILED ] 3 tests, listed below:
[ FAILED ] EnvPosixTest.RandomAccessUniqueID
[ FAILED ] EnvPosixTest.RandomAccessUniqueIDConcurrent
[ FAILED ] EnvPosixTest.RandomAccessUniqueIDDeletes
3 FAILED TESTS
The fix:
When support for that ioctl is lacking, skip each affected test.
Could be improved by noting which sub-tests are being skipped.
Test Plan:
run these on F21 and note that they now pass.
TEST_TMPDIR=/dev/shm/rdb ./env_test
./env_test
Reviewers: ljin, rven, igor.sugak, yhchiang, sdong, igor
Reviewed By: igor
Subscribers: dhruba
Differential Revision: https://reviews.facebook.net/D37323
2015-04-18 03:39:02 +00:00
|
|
|
|
2019-10-12 01:18:14 +00:00
|
|
|
// `GetUniqueId()` temporarily returns zero on Windows. `BlockBasedTable` can
|
|
|
|
// handle a return value of zero but this test case cannot.
|
|
|
|
#ifndef OS_WIN
|
2017-02-16 18:25:06 +00:00
|
|
|
TEST_P(EnvPosixTestWithParam, RandomAccessUniqueID) {
|
|
|
|
// Create file.
|
|
|
|
if (env_ == Env::Default()) {
|
2016-04-21 17:37:27 +00:00
|
|
|
EnvOptions soptions;
|
2017-02-16 18:25:06 +00:00
|
|
|
soptions.use_direct_reads = soptions.use_direct_writes = direct_io_;
|
2016-04-21 17:37:27 +00:00
|
|
|
IoctlFriendlyTmpdir ift;
|
2022-08-11 00:34:38 +00:00
|
|
|
if (!ift.is_supported()) {
|
|
|
|
ROCKSDB_GTEST_BYPASS(
|
|
|
|
"FS_IOC_GETVERSION is not supported by the filesystem");
|
|
|
|
return;
|
|
|
|
}
|
2016-04-21 17:37:27 +00:00
|
|
|
std::string fname = ift.name() + "/testfile";
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<WritableFile> wfile;
|
2016-04-21 17:37:27 +00:00
|
|
|
ASSERT_OK(env_->NewWritableFile(fname, &wfile, soptions));
|
|
|
|
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<RandomAccessFile> file;
|
2016-04-21 17:37:27 +00:00
|
|
|
|
|
|
|
// Get Unique ID
|
|
|
|
ASSERT_OK(env_->NewRandomAccessFile(fname, &file, soptions));
|
|
|
|
size_t id_size = file->GetUniqueId(temp_id, MAX_ID_SIZE);
|
|
|
|
ASSERT_TRUE(id_size > 0);
|
|
|
|
std::string unique_id1(temp_id, id_size);
|
|
|
|
ASSERT_TRUE(IsUniqueIDValid(unique_id1));
|
|
|
|
|
|
|
|
// Get Unique ID again
|
|
|
|
ASSERT_OK(env_->NewRandomAccessFile(fname, &file, soptions));
|
|
|
|
id_size = file->GetUniqueId(temp_id, MAX_ID_SIZE);
|
|
|
|
ASSERT_TRUE(id_size > 0);
|
|
|
|
std::string unique_id2(temp_id, id_size);
|
|
|
|
ASSERT_TRUE(IsUniqueIDValid(unique_id2));
|
|
|
|
|
|
|
|
// Get Unique ID again after waiting some time.
|
|
|
|
env_->SleepForMicroseconds(1000000);
|
|
|
|
ASSERT_OK(env_->NewRandomAccessFile(fname, &file, soptions));
|
|
|
|
id_size = file->GetUniqueId(temp_id, MAX_ID_SIZE);
|
|
|
|
ASSERT_TRUE(id_size > 0);
|
|
|
|
std::string unique_id3(temp_id, id_size);
|
|
|
|
ASSERT_TRUE(IsUniqueIDValid(unique_id3));
|
|
|
|
|
|
|
|
// Check IDs are the same.
|
|
|
|
ASSERT_EQ(unique_id1, unique_id2);
|
|
|
|
ASSERT_EQ(unique_id2, unique_id3);
|
|
|
|
|
|
|
|
// Delete the file
|
2020-07-29 05:58:28 +00:00
|
|
|
ASSERT_OK(env_->DeleteFile(fname));
|
2016-04-21 17:37:27 +00:00
|
|
|
}
|
2013-01-31 23:20:24 +00:00
|
|
|
}
|
2019-10-12 01:18:14 +00:00
|
|
|
#endif // !defined(OS_WIN)
|
2013-01-31 23:20:24 +00:00
|
|
|
|
2014-03-06 23:59:27 +00:00
|
|
|
// only works in linux platforms
|
|
|
|
#ifdef ROCKSDB_FALLOCATE_PRESENT
|
2017-02-16 18:25:06 +00:00
|
|
|
TEST_P(EnvPosixTestWithParam, AllocateTest) {
|
|
|
|
if (env_ == Env::Default()) {
|
2022-08-11 00:34:38 +00:00
|
|
|
SetupSyncPointsToMockDirectIO();
|
|
|
|
std::string fname = test::PerThreadDBPath(env_, "preallocate_testfile");
|
2016-04-21 17:37:27 +00:00
|
|
|
// Try fallocate in a file to see whether the target file system supports
|
|
|
|
// it.
|
|
|
|
// Skip the test if fallocate is not supported.
|
2022-08-11 00:34:38 +00:00
|
|
|
std::string fname_test_fallocate =
|
|
|
|
test::PerThreadDBPath(env_, "preallocate_testfile_2");
|
2016-04-21 17:37:27 +00:00
|
|
|
int fd = -1;
|
|
|
|
do {
|
|
|
|
fd = open(fname_test_fallocate.c_str(), O_CREAT | O_RDWR | O_TRUNC, 0644);
|
|
|
|
} while (fd < 0 && errno == EINTR);
|
|
|
|
ASSERT_GT(fd, 0);
|
|
|
|
|
|
|
|
int alloc_status = fallocate(fd, 0, 0, 1);
|
|
|
|
|
|
|
|
int err_number = 0;
|
|
|
|
if (alloc_status != 0) {
|
|
|
|
err_number = errno;
|
2021-03-25 06:06:31 +00:00
|
|
|
fprintf(stderr, "Warning: fallocate() fails, %s\n",
|
|
|
|
errnoStr(err_number).c_str());
|
2016-04-21 17:37:27 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
ASSERT_OK(env_->DeleteFile(fname_test_fallocate));
|
|
|
|
if (alloc_status != 0 && err_number == EOPNOTSUPP) {
|
|
|
|
// The filesystem containing the file does not support fallocate
|
|
|
|
return;
|
|
|
|
}
|
2014-09-19 17:37:42 +00:00
|
|
|
|
2016-04-21 17:37:27 +00:00
|
|
|
EnvOptions soptions;
|
|
|
|
soptions.use_mmap_writes = false;
|
2017-02-16 18:25:06 +00:00
|
|
|
soptions.use_direct_reads = soptions.use_direct_writes = direct_io_;
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<WritableFile> wfile;
|
2016-04-21 17:37:27 +00:00
|
|
|
ASSERT_OK(env_->NewWritableFile(fname, &wfile, soptions));
|
2014-09-19 17:37:42 +00:00
|
|
|
|
2016-04-21 17:37:27 +00:00
|
|
|
// allocate 100 MB
|
|
|
|
size_t kPreallocateSize = 100 * 1024 * 1024;
|
|
|
|
size_t kBlockSize = 512;
|
|
|
|
size_t kDataSize = 1024 * 1024;
|
|
|
|
auto data_ptr = NewAligned(kDataSize, 'A');
|
|
|
|
Slice data(data_ptr.get(), kDataSize);
|
|
|
|
wfile->SetPreallocationBlockSize(kPreallocateSize);
|
|
|
|
wfile->PrepareWrite(wfile->GetFileSize(), kDataSize);
|
|
|
|
ASSERT_OK(wfile->Append(data));
|
|
|
|
ASSERT_OK(wfile->Flush());
|
|
|
|
|
|
|
|
struct stat f_stat;
|
|
|
|
ASSERT_EQ(stat(fname.c_str(), &f_stat), 0);
|
|
|
|
ASSERT_EQ((unsigned int)kDataSize, f_stat.st_size);
|
|
|
|
// verify that blocks are preallocated
|
|
|
|
// Note here that we don't check the exact number of blocks preallocated --
|
|
|
|
// we only require that number of allocated blocks is at least what we
|
|
|
|
// expect.
|
|
|
|
// It looks like some FS give us more blocks that we asked for. That's fine.
|
|
|
|
// It might be worth investigating further.
|
|
|
|
ASSERT_LE((unsigned int)(kPreallocateSize / kBlockSize), f_stat.st_blocks);
|
|
|
|
|
|
|
|
// close the file, should deallocate the blocks
|
|
|
|
wfile.reset();
|
|
|
|
|
|
|
|
stat(fname.c_str(), &f_stat);
|
|
|
|
ASSERT_EQ((unsigned int)kDataSize, f_stat.st_size);
|
|
|
|
// verify that preallocated blocks were deallocated on file close
|
|
|
|
// Because the FS might give us more blocks, we add a full page to the size
|
|
|
|
// and expect the number of blocks to be less or equal to that.
|
|
|
|
ASSERT_GE((f_stat.st_size + kPageSize + kBlockSize - 1) / kBlockSize,
|
|
|
|
(unsigned int)f_stat.st_blocks);
|
2014-09-19 17:37:42 +00:00
|
|
|
}
|
2014-03-06 23:59:27 +00:00
|
|
|
}
|
2014-09-04 17:23:45 +00:00
|
|
|
#endif // ROCKSDB_FALLOCATE_PRESENT
|
2014-03-06 23:59:27 +00:00
|
|
|
|
2013-01-31 23:20:24 +00:00
|
|
|
// Returns true if any of the strings in ss are the prefix of another string.
|
|
|
|
bool HasPrefix(const std::unordered_set<std::string>& ss) {
|
2022-10-25 00:54:14 +00:00
|
|
|
for (const std::string& s : ss) {
|
2013-01-31 23:20:24 +00:00
|
|
|
if (s.empty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (size_t i = 1; i < s.size(); ++i) {
|
|
|
|
if (ss.count(s.substr(0, i)) != 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-12 01:18:14 +00:00
|
|
|
// `GetUniqueId()` temporarily returns zero on Windows. `BlockBasedTable` can
|
|
|
|
// handle a return value of zero but this test case cannot.
|
|
|
|
#ifndef OS_WIN
|
2017-02-16 18:25:06 +00:00
|
|
|
TEST_P(EnvPosixTestWithParam, RandomAccessUniqueIDConcurrent) {
|
|
|
|
if (env_ == Env::Default()) {
|
2016-04-21 17:37:27 +00:00
|
|
|
// Check whether a bunch of concurrently existing files have unique IDs.
|
|
|
|
EnvOptions soptions;
|
2017-02-16 18:25:06 +00:00
|
|
|
soptions.use_direct_reads = soptions.use_direct_writes = direct_io_;
|
2016-04-21 17:37:27 +00:00
|
|
|
|
|
|
|
// Create the files
|
|
|
|
IoctlFriendlyTmpdir ift;
|
2022-08-11 00:34:38 +00:00
|
|
|
if (!ift.is_supported()) {
|
|
|
|
ROCKSDB_GTEST_BYPASS(
|
|
|
|
"FS_IOC_GETVERSION is not supported by the filesystem");
|
|
|
|
return;
|
|
|
|
}
|
2016-04-21 17:37:27 +00:00
|
|
|
std::vector<std::string> fnames;
|
|
|
|
for (int i = 0; i < 1000; ++i) {
|
2022-05-06 20:03:58 +00:00
|
|
|
fnames.push_back(ift.name() + "/" + "testfile" + std::to_string(i));
|
2016-04-21 17:37:27 +00:00
|
|
|
|
|
|
|
// Create file.
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<WritableFile> wfile;
|
2016-04-21 17:37:27 +00:00
|
|
|
ASSERT_OK(env_->NewWritableFile(fnames[i], &wfile, soptions));
|
|
|
|
}
|
2013-01-31 23:20:24 +00:00
|
|
|
|
2016-04-21 17:37:27 +00:00
|
|
|
// Collect and check whether the IDs are unique.
|
|
|
|
std::unordered_set<std::string> ids;
|
2020-06-24 23:20:55 +00:00
|
|
|
for (const std::string& fname : fnames) {
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<RandomAccessFile> file;
|
2016-04-21 17:37:27 +00:00
|
|
|
std::string unique_id;
|
|
|
|
ASSERT_OK(env_->NewRandomAccessFile(fname, &file, soptions));
|
|
|
|
size_t id_size = file->GetUniqueId(temp_id, MAX_ID_SIZE);
|
|
|
|
ASSERT_TRUE(id_size > 0);
|
|
|
|
unique_id = std::string(temp_id, id_size);
|
|
|
|
ASSERT_TRUE(IsUniqueIDValid(unique_id));
|
2013-01-31 23:20:24 +00:00
|
|
|
|
2016-04-21 17:37:27 +00:00
|
|
|
ASSERT_TRUE(ids.count(unique_id) == 0);
|
|
|
|
ids.insert(unique_id);
|
|
|
|
}
|
2013-01-31 23:20:24 +00:00
|
|
|
|
2016-04-21 17:37:27 +00:00
|
|
|
// Delete the files
|
2020-06-24 23:20:55 +00:00
|
|
|
for (const std::string& fname : fnames) {
|
2016-04-21 17:37:27 +00:00
|
|
|
ASSERT_OK(env_->DeleteFile(fname));
|
|
|
|
}
|
2013-01-31 23:20:24 +00:00
|
|
|
|
2016-04-21 17:37:27 +00:00
|
|
|
ASSERT_TRUE(!HasPrefix(ids));
|
2013-01-31 23:20:24 +00:00
|
|
|
}
|
2016-04-21 17:37:27 +00:00
|
|
|
}
|
2013-01-31 23:20:24 +00:00
|
|
|
|
2020-10-08 16:39:22 +00:00
|
|
|
// TODO: Disable the flaky test, it's a known issue that ext4 may return same
|
|
|
|
// key after file deletion. The issue is tracked in #7405, #7470.
|
|
|
|
TEST_P(EnvPosixTestWithParam, DISABLED_RandomAccessUniqueIDDeletes) {
|
2017-02-16 18:25:06 +00:00
|
|
|
if (env_ == Env::Default()) {
|
2016-04-21 17:37:27 +00:00
|
|
|
EnvOptions soptions;
|
2017-02-16 18:25:06 +00:00
|
|
|
soptions.use_direct_reads = soptions.use_direct_writes = direct_io_;
|
2016-04-21 17:37:27 +00:00
|
|
|
|
|
|
|
IoctlFriendlyTmpdir ift;
|
2022-08-11 00:34:38 +00:00
|
|
|
if (!ift.is_supported()) {
|
|
|
|
ROCKSDB_GTEST_BYPASS(
|
|
|
|
"FS_IOC_GETVERSION is not supported by the filesystem");
|
|
|
|
return;
|
|
|
|
}
|
2016-04-21 17:37:27 +00:00
|
|
|
std::string fname = ift.name() + "/" + "testfile";
|
|
|
|
|
|
|
|
// Check that after file is deleted we don't get same ID again in a new
|
|
|
|
// file.
|
|
|
|
std::unordered_set<std::string> ids;
|
|
|
|
for (int i = 0; i < 1000; ++i) {
|
|
|
|
// Create file.
|
|
|
|
{
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<WritableFile> wfile;
|
2016-04-21 17:37:27 +00:00
|
|
|
ASSERT_OK(env_->NewWritableFile(fname, &wfile, soptions));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get Unique ID
|
|
|
|
std::string unique_id;
|
|
|
|
{
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<RandomAccessFile> file;
|
2016-04-21 17:37:27 +00:00
|
|
|
ASSERT_OK(env_->NewRandomAccessFile(fname, &file, soptions));
|
|
|
|
size_t id_size = file->GetUniqueId(temp_id, MAX_ID_SIZE);
|
|
|
|
ASSERT_TRUE(id_size > 0);
|
|
|
|
unique_id = std::string(temp_id, id_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_TRUE(IsUniqueIDValid(unique_id));
|
|
|
|
ASSERT_TRUE(ids.count(unique_id) == 0);
|
|
|
|
ids.insert(unique_id);
|
|
|
|
|
|
|
|
// Delete the file
|
|
|
|
ASSERT_OK(env_->DeleteFile(fname));
|
|
|
|
}
|
2013-01-31 23:20:24 +00:00
|
|
|
|
2016-04-21 17:37:27 +00:00
|
|
|
ASSERT_TRUE(!HasPrefix(ids));
|
|
|
|
}
|
2013-01-31 23:20:24 +00:00
|
|
|
}
|
2019-10-12 01:18:14 +00:00
|
|
|
#endif // !defined(OS_WIN)
|
2013-01-31 23:20:24 +00:00
|
|
|
|
2019-06-05 16:38:23 +00:00
|
|
|
TEST_P(EnvPosixTestWithParam, MultiRead) {
|
|
|
|
EnvOptions soptions;
|
|
|
|
soptions.use_direct_reads = soptions.use_direct_writes = direct_io_;
|
|
|
|
std::string fname = test::PerThreadDBPath(env_, "testfile");
|
|
|
|
|
|
|
|
const size_t kSectorSize = 4096;
|
|
|
|
const size_t kNumSectors = 8;
|
|
|
|
|
|
|
|
// Create file.
|
|
|
|
{
|
|
|
|
std::unique_ptr<WritableFile> wfile;
|
2019-09-20 19:00:55 +00:00
|
|
|
#if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(OS_SOLARIS) && \
|
|
|
|
!defined(OS_AIX)
|
2019-06-05 16:38:23 +00:00
|
|
|
if (soptions.use_direct_writes) {
|
|
|
|
soptions.use_direct_writes = false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
ASSERT_OK(env_->NewWritableFile(fname, &wfile, soptions));
|
|
|
|
for (size_t i = 0; i < kNumSectors; ++i) {
|
2019-06-08 02:34:48 +00:00
|
|
|
auto data = NewAligned(kSectorSize * 8, static_cast<char>(i + 1));
|
2019-06-05 16:38:23 +00:00
|
|
|
Slice slice(data.get(), kSectorSize);
|
|
|
|
ASSERT_OK(wfile->Append(slice));
|
|
|
|
}
|
|
|
|
ASSERT_OK(wfile->Close());
|
|
|
|
}
|
|
|
|
|
2020-02-22 00:56:04 +00:00
|
|
|
// More attempts to simulate more partial result sequences.
|
|
|
|
for (uint32_t attempt = 0; attempt < 20; attempt++) {
|
|
|
|
// Random Read
|
|
|
|
Random rnd(301 + attempt);
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
2022-04-04 22:35:43 +00:00
|
|
|
"UpdateResults::io_uring_result", [&](void* arg) {
|
2020-02-22 00:56:04 +00:00
|
|
|
if (attempt > 0) {
|
|
|
|
// No failure in the first attempt.
|
|
|
|
size_t& bytes_read = *static_cast<size_t*>(arg);
|
|
|
|
if (rnd.OneIn(4)) {
|
|
|
|
bytes_read = 0;
|
|
|
|
} else if (rnd.OneIn(3)) {
|
|
|
|
bytes_read = static_cast<size_t>(
|
|
|
|
rnd.Uniform(static_cast<int>(bytes_read)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-07-18 22:37:29 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
2019-06-05 16:38:23 +00:00
|
|
|
std::unique_ptr<RandomAccessFile> file;
|
|
|
|
std::vector<ReadRequest> reqs(3);
|
|
|
|
std::vector<std::unique_ptr<char, Deleter>> data;
|
|
|
|
uint64_t offset = 0;
|
|
|
|
for (size_t i = 0; i < reqs.size(); ++i) {
|
|
|
|
reqs[i].offset = offset;
|
|
|
|
offset += 2 * kSectorSize;
|
|
|
|
reqs[i].len = kSectorSize;
|
|
|
|
data.emplace_back(NewAligned(kSectorSize, 0));
|
|
|
|
reqs[i].scratch = data.back().get();
|
|
|
|
}
|
2019-09-20 19:00:55 +00:00
|
|
|
#if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(OS_SOLARIS) && \
|
|
|
|
!defined(OS_AIX)
|
2019-06-05 16:38:23 +00:00
|
|
|
if (soptions.use_direct_reads) {
|
|
|
|
soptions.use_direct_reads = false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
ASSERT_OK(env_->NewRandomAccessFile(fname, &file, soptions));
|
|
|
|
ASSERT_OK(file->MultiRead(reqs.data(), reqs.size()));
|
|
|
|
for (size_t i = 0; i < reqs.size(); ++i) {
|
2019-09-20 19:00:55 +00:00
|
|
|
auto buf = NewAligned(kSectorSize * 8, static_cast<char>(i * 2 + 1));
|
2019-06-05 16:38:23 +00:00
|
|
|
ASSERT_OK(reqs[i].status);
|
|
|
|
ASSERT_EQ(memcmp(reqs[i].scratch, buf.get(), kSectorSize), 0);
|
|
|
|
}
|
2020-02-22 00:56:04 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
2019-06-05 16:38:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-29 00:40:13 +00:00
|
|
|
TEST_F(EnvPosixTest, MultiReadNonAlignedLargeNum) {
|
2021-05-19 17:24:12 +00:00
|
|
|
// In this test we don't do aligned read, so it doesn't work for
|
2020-02-29 00:40:13 +00:00
|
|
|
// direct I/O case.
|
|
|
|
EnvOptions soptions;
|
|
|
|
soptions.use_direct_reads = soptions.use_direct_writes = false;
|
|
|
|
std::string fname = test::PerThreadDBPath(env_, "testfile");
|
|
|
|
|
|
|
|
const size_t kTotalSize = 81920;
|
|
|
|
Random rnd(301);
|
2020-07-09 21:33:42 +00:00
|
|
|
std::string expected_data = rnd.RandomString(kTotalSize);
|
2020-02-29 00:40:13 +00:00
|
|
|
|
|
|
|
// Create file.
|
|
|
|
{
|
|
|
|
std::unique_ptr<WritableFile> wfile;
|
|
|
|
ASSERT_OK(env_->NewWritableFile(fname, &wfile, soptions));
|
|
|
|
ASSERT_OK(wfile->Append(expected_data));
|
|
|
|
ASSERT_OK(wfile->Close());
|
|
|
|
}
|
|
|
|
|
|
|
|
// More attempts to simulate more partial result sequences.
|
|
|
|
for (uint32_t attempt = 0; attempt < 25; attempt++) {
|
|
|
|
// Right now kIoUringDepth is hard coded as 256, so we need very large
|
|
|
|
// number of keys to cover the case of multiple rounds of submissions.
|
|
|
|
// Right now the test latency is still acceptable. If it ends up with
|
|
|
|
// too long, we can modify the io uring depth with SyncPoint here.
|
|
|
|
const int num_reads = rnd.Uniform(512) + 1;
|
|
|
|
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
2022-04-04 22:35:43 +00:00
|
|
|
"UpdateResults::io_uring_result", [&](void* arg) {
|
2020-02-29 00:40:13 +00:00
|
|
|
if (attempt > 5) {
|
|
|
|
// Improve partial result rates in second half of the run to
|
|
|
|
// cover the case of repeated partial results.
|
|
|
|
int odd = (attempt < 15) ? num_reads / 2 : 4;
|
|
|
|
// No failure in first several attempts.
|
|
|
|
size_t& bytes_read = *static_cast<size_t*>(arg);
|
|
|
|
if (rnd.OneIn(odd)) {
|
|
|
|
bytes_read = 0;
|
|
|
|
} else if (rnd.OneIn(odd / 2)) {
|
|
|
|
bytes_read = static_cast<size_t>(
|
|
|
|
rnd.Uniform(static_cast<int>(bytes_read)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
|
|
|
|
|
|
|
// Generate (offset, len) pairs
|
|
|
|
std::set<int> start_offsets;
|
|
|
|
for (int i = 0; i < num_reads; i++) {
|
|
|
|
int rnd_off;
|
|
|
|
// No repeat offsets.
|
2022-10-25 00:54:14 +00:00
|
|
|
while (start_offsets.find(rnd_off = rnd.Uniform(81920)) !=
|
|
|
|
start_offsets.end()) {
|
|
|
|
}
|
2020-02-29 00:40:13 +00:00
|
|
|
start_offsets.insert(rnd_off);
|
|
|
|
}
|
|
|
|
std::vector<size_t> offsets;
|
|
|
|
std::vector<size_t> lens;
|
|
|
|
// std::set already sorted the offsets.
|
2022-10-25 00:54:14 +00:00
|
|
|
for (int so : start_offsets) {
|
2020-02-29 00:40:13 +00:00
|
|
|
offsets.push_back(so);
|
|
|
|
}
|
2020-06-02 22:02:44 +00:00
|
|
|
for (size_t i = 0; i + 1 < offsets.size(); i++) {
|
2022-10-25 00:54:14 +00:00
|
|
|
lens.push_back(static_cast<size_t>(
|
|
|
|
rnd.Uniform(static_cast<int>(offsets[i + 1] - offsets[i])) + 1));
|
2020-02-29 00:40:13 +00:00
|
|
|
}
|
2022-10-25 00:54:14 +00:00
|
|
|
lens.push_back(static_cast<size_t>(
|
|
|
|
rnd.Uniform(static_cast<int>(kTotalSize - offsets.back())) + 1));
|
2020-02-29 00:40:13 +00:00
|
|
|
ASSERT_EQ(num_reads, lens.size());
|
|
|
|
|
|
|
|
// Create requests
|
|
|
|
std::vector<std::string> scratches;
|
|
|
|
scratches.reserve(num_reads);
|
|
|
|
std::vector<ReadRequest> reqs(num_reads);
|
|
|
|
for (size_t i = 0; i < reqs.size(); ++i) {
|
|
|
|
reqs[i].offset = offsets[i];
|
|
|
|
reqs[i].len = lens[i];
|
|
|
|
scratches.emplace_back(reqs[i].len, ' ');
|
|
|
|
reqs[i].scratch = const_cast<char*>(scratches.back().data());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query the data
|
|
|
|
std::unique_ptr<RandomAccessFile> file;
|
|
|
|
ASSERT_OK(env_->NewRandomAccessFile(fname, &file, soptions));
|
|
|
|
ASSERT_OK(file->MultiRead(reqs.data(), reqs.size()));
|
|
|
|
|
|
|
|
// Validate results
|
|
|
|
for (int i = 0; i < num_reads; ++i) {
|
|
|
|
ASSERT_OK(reqs[i].status);
|
2022-10-25 00:54:14 +00:00
|
|
|
ASSERT_EQ(
|
|
|
|
Slice(expected_data.data() + offsets[i], lens[i]).ToString(true),
|
|
|
|
reqs[i].result.ToString(true));
|
2020-02-29 00:40:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
|
|
|
}
|
|
|
|
}
|
2020-03-24 04:50:42 +00:00
|
|
|
|
2022-07-18 22:37:29 +00:00
|
|
|
TEST_F(EnvPosixTest, NonAlignedDirectIOMultiReadBeyondFileSize) {
|
|
|
|
EnvOptions soptions;
|
|
|
|
soptions.use_direct_reads = true;
|
|
|
|
soptions.use_direct_writes = false;
|
|
|
|
std::string fname = test::PerThreadDBPath(env_, "testfile");
|
|
|
|
|
|
|
|
Random rnd(301);
|
|
|
|
std::unique_ptr<WritableFile> wfile;
|
|
|
|
size_t alignment = 0;
|
|
|
|
// Create file.
|
|
|
|
{
|
|
|
|
ASSERT_OK(env_->NewWritableFile(fname, &wfile, soptions));
|
|
|
|
auto data_ptr = NewAligned(4095, 'b');
|
|
|
|
Slice data_b(data_ptr.get(), 4095);
|
|
|
|
ASSERT_OK(wfile->PositionedAppend(data_b, 0U));
|
|
|
|
ASSERT_OK(wfile->Close());
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(OS_SOLARIS) && \
|
|
|
|
!defined(OS_AIX) && !defined(OS_OPENBSD) && !defined(OS_FREEBSD)
|
|
|
|
if (soptions.use_direct_reads) {
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
|
|
|
"NewRandomAccessFile:O_DIRECT", [&](void* arg) {
|
|
|
|
int* val = static_cast<int*>(arg);
|
|
|
|
*val &= ~O_DIRECT;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
|
|
|
|
|
|
|
const int num_reads = 2;
|
|
|
|
// Create requests
|
|
|
|
std::vector<std::string> scratches;
|
|
|
|
scratches.reserve(num_reads);
|
|
|
|
std::vector<ReadRequest> reqs(num_reads);
|
|
|
|
|
|
|
|
std::unique_ptr<RandomAccessFile> file;
|
|
|
|
ASSERT_OK(env_->NewRandomAccessFile(fname, &file, soptions));
|
|
|
|
alignment = file->GetRequiredBufferAlignment();
|
|
|
|
ASSERT_EQ(num_reads, reqs.size());
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<char, Deleter>> data;
|
|
|
|
|
|
|
|
std::vector<size_t> offsets = {0, 2047};
|
|
|
|
std::vector<size_t> lens = {2047, 4096 - 2047};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < num_reads; i++) {
|
|
|
|
// Do alignment
|
|
|
|
reqs[i].offset = static_cast<uint64_t>(TruncateToPageBoundary(
|
|
|
|
alignment, static_cast<size_t>(/*offset=*/offsets[i])));
|
|
|
|
reqs[i].len =
|
|
|
|
Roundup(static_cast<size_t>(/*offset=*/offsets[i]) + /*length=*/lens[i],
|
|
|
|
alignment) -
|
|
|
|
reqs[i].offset;
|
|
|
|
|
|
|
|
size_t new_capacity = Roundup(reqs[i].len, alignment);
|
|
|
|
data.emplace_back(NewAligned(new_capacity, 0));
|
|
|
|
reqs[i].scratch = data.back().get();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query the data
|
|
|
|
ASSERT_OK(file->MultiRead(reqs.data(), reqs.size()));
|
|
|
|
|
|
|
|
// Validate results
|
|
|
|
for (size_t i = 0; i < num_reads; ++i) {
|
|
|
|
ASSERT_OK(reqs[i].status);
|
|
|
|
}
|
|
|
|
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
|
|
|
}
|
|
|
|
|
2021-05-18 23:08:21 +00:00
|
|
|
#if defined(ROCKSDB_IOURING_PRESENT)
|
|
|
|
void GenerateFilesAndRequest(Env* env, const std::string& fname,
|
|
|
|
std::vector<ReadRequest>* ret_reqs,
|
|
|
|
std::vector<std::string>* scratches) {
|
|
|
|
const size_t kTotalSize = 81920;
|
|
|
|
Random rnd(301);
|
|
|
|
std::string expected_data = rnd.RandomString(kTotalSize);
|
|
|
|
|
|
|
|
// Create file.
|
|
|
|
{
|
|
|
|
std::unique_ptr<WritableFile> wfile;
|
|
|
|
ASSERT_OK(env->NewWritableFile(fname, &wfile, EnvOptions()));
|
|
|
|
ASSERT_OK(wfile->Append(expected_data));
|
|
|
|
ASSERT_OK(wfile->Close());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Right now kIoUringDepth is hard coded as 256, so we need very large
|
|
|
|
// number of keys to cover the case of multiple rounds of submissions.
|
|
|
|
// Right now the test latency is still acceptable. If it ends up with
|
|
|
|
// too long, we can modify the io uring depth with SyncPoint here.
|
|
|
|
const int num_reads = 3;
|
|
|
|
std::vector<size_t> offsets = {10000, 20000, 30000};
|
|
|
|
std::vector<size_t> lens = {3000, 200, 100};
|
|
|
|
|
|
|
|
// Create requests
|
|
|
|
scratches->reserve(num_reads);
|
|
|
|
std::vector<ReadRequest>& reqs = *ret_reqs;
|
|
|
|
reqs.resize(num_reads);
|
|
|
|
for (int i = 0; i < num_reads; ++i) {
|
|
|
|
reqs[i].offset = offsets[i];
|
|
|
|
reqs[i].len = lens[i];
|
|
|
|
scratches->emplace_back(reqs[i].len, ' ');
|
|
|
|
reqs[i].scratch = const_cast<char*>(scratches->back().data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EnvPosixTest, MultiReadIOUringError) {
|
2021-05-19 17:24:12 +00:00
|
|
|
// In this test we don't do aligned read, so we can't do direct I/O.
|
2021-05-18 23:08:21 +00:00
|
|
|
EnvOptions soptions;
|
|
|
|
soptions.use_direct_reads = soptions.use_direct_writes = false;
|
|
|
|
std::string fname = test::PerThreadDBPath(env_, "testfile");
|
|
|
|
|
|
|
|
std::vector<std::string> scratches;
|
|
|
|
std::vector<ReadRequest> reqs;
|
|
|
|
GenerateFilesAndRequest(env_, fname, &reqs, &scratches);
|
|
|
|
// Query the data
|
|
|
|
std::unique_ptr<RandomAccessFile> file;
|
|
|
|
ASSERT_OK(env_->NewRandomAccessFile(fname, &file, soptions));
|
|
|
|
|
|
|
|
bool io_uring_wait_cqe_called = false;
|
2021-05-19 17:58:13 +00:00
|
|
|
SyncPoint::GetInstance()->SetCallBack(
|
2021-05-18 23:08:21 +00:00
|
|
|
"PosixRandomAccessFile::MultiRead:io_uring_wait_cqe:return",
|
|
|
|
[&](void* arg) {
|
|
|
|
if (!io_uring_wait_cqe_called) {
|
|
|
|
io_uring_wait_cqe_called = true;
|
|
|
|
ssize_t& ret = *(static_cast<ssize_t*>(arg));
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
});
|
2021-05-19 17:58:13 +00:00
|
|
|
SyncPoint::GetInstance()->EnableProcessing();
|
2021-05-18 23:08:21 +00:00
|
|
|
|
|
|
|
Status s = file->MultiRead(reqs.data(), reqs.size());
|
|
|
|
if (io_uring_wait_cqe_called) {
|
|
|
|
ASSERT_NOK(s);
|
2021-05-19 17:24:12 +00:00
|
|
|
} else {
|
|
|
|
s.PermitUncheckedError();
|
2021-05-18 23:08:21 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 17:58:13 +00:00
|
|
|
SyncPoint::GetInstance()->DisableProcessing();
|
|
|
|
SyncPoint::GetInstance()->ClearAllCallBacks();
|
2021-05-18 23:08:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EnvPosixTest, MultiReadIOUringError2) {
|
2021-05-19 17:24:12 +00:00
|
|
|
// In this test we don't do aligned read, so we can't do direct I/O.
|
2021-05-18 23:08:21 +00:00
|
|
|
EnvOptions soptions;
|
|
|
|
soptions.use_direct_reads = soptions.use_direct_writes = false;
|
|
|
|
std::string fname = test::PerThreadDBPath(env_, "testfile");
|
|
|
|
|
|
|
|
std::vector<std::string> scratches;
|
|
|
|
std::vector<ReadRequest> reqs;
|
|
|
|
GenerateFilesAndRequest(env_, fname, &reqs, &scratches);
|
|
|
|
// Query the data
|
|
|
|
std::unique_ptr<RandomAccessFile> file;
|
|
|
|
ASSERT_OK(env_->NewRandomAccessFile(fname, &file, soptions));
|
|
|
|
|
|
|
|
bool io_uring_submit_and_wait_called = false;
|
2021-05-19 17:58:13 +00:00
|
|
|
SyncPoint::GetInstance()->SetCallBack(
|
2021-05-18 23:08:21 +00:00
|
|
|
"PosixRandomAccessFile::MultiRead:io_uring_submit_and_wait:return1",
|
|
|
|
[&](void* arg) {
|
|
|
|
io_uring_submit_and_wait_called = true;
|
|
|
|
ssize_t* ret = static_cast<ssize_t*>(arg);
|
|
|
|
(*ret)--;
|
|
|
|
});
|
2021-05-19 17:58:13 +00:00
|
|
|
SyncPoint::GetInstance()->SetCallBack(
|
2021-05-18 23:08:21 +00:00
|
|
|
"PosixRandomAccessFile::MultiRead:io_uring_submit_and_wait:return2",
|
|
|
|
[&](void* arg) {
|
|
|
|
struct io_uring* iu = static_cast<struct io_uring*>(arg);
|
|
|
|
struct io_uring_cqe* cqe;
|
|
|
|
assert(io_uring_wait_cqe(iu, &cqe) == 0);
|
|
|
|
io_uring_cqe_seen(iu, cqe);
|
|
|
|
});
|
2021-05-19 17:58:13 +00:00
|
|
|
SyncPoint::GetInstance()->EnableProcessing();
|
2021-05-18 23:08:21 +00:00
|
|
|
|
|
|
|
Status s = file->MultiRead(reqs.data(), reqs.size());
|
|
|
|
if (io_uring_submit_and_wait_called) {
|
|
|
|
ASSERT_NOK(s);
|
2021-05-19 17:24:12 +00:00
|
|
|
} else {
|
|
|
|
s.PermitUncheckedError();
|
2021-05-18 23:08:21 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 17:58:13 +00:00
|
|
|
SyncPoint::GetInstance()->DisableProcessing();
|
|
|
|
SyncPoint::GetInstance()->ClearAllCallBacks();
|
2021-05-18 23:08:21 +00:00
|
|
|
}
|
|
|
|
#endif // ROCKSDB_IOURING_PRESENT
|
|
|
|
|
2013-11-17 07:44:39 +00:00
|
|
|
// Only works in linux platforms
|
2017-01-09 23:37:57 +00:00
|
|
|
#ifdef OS_WIN
|
|
|
|
TEST_P(EnvPosixTestWithParam, DISABLED_InvalidateCache) {
|
|
|
|
#else
|
2016-04-21 17:37:27 +00:00
|
|
|
TEST_P(EnvPosixTestWithParam, InvalidateCache) {
|
2017-01-09 23:37:57 +00:00
|
|
|
#endif
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
|
|
|
EnvOptions soptions;
|
|
|
|
soptions.use_direct_reads = soptions.use_direct_writes = direct_io_;
|
|
|
|
std::string fname = test::PerThreadDBPath(env_, "testfile");
|
2014-03-05 22:28:47 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
const size_t kSectorSize = 512;
|
|
|
|
auto data = NewAligned(kSectorSize, 0);
|
|
|
|
Slice slice(data.get(), kSectorSize);
|
2013-01-31 23:20:24 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
// Create file.
|
|
|
|
{
|
|
|
|
std::unique_ptr<WritableFile> wfile;
|
2022-10-25 00:54:14 +00:00
|
|
|
#if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(OS_SOLARIS) && \
|
|
|
|
!defined(OS_AIX)
|
|
|
|
if (soptions.use_direct_writes) {
|
|
|
|
soptions.use_direct_writes = false;
|
|
|
|
}
|
2016-05-23 23:05:53 +00:00
|
|
|
#endif
|
2022-10-25 00:54:14 +00:00
|
|
|
ASSERT_OK(env_->NewWritableFile(fname, &wfile, soptions));
|
|
|
|
ASSERT_OK(wfile->Append(slice));
|
|
|
|
ASSERT_OK(wfile->InvalidateCache(0, 0));
|
|
|
|
ASSERT_OK(wfile->Close());
|
2020-02-22 00:56:04 +00:00
|
|
|
}
|
2013-01-31 23:20:24 +00:00
|
|
|
|
2022-10-25 00:54:14 +00:00
|
|
|
// Random Read
|
|
|
|
{
|
|
|
|
std::unique_ptr<RandomAccessFile> file;
|
|
|
|
auto scratch = NewAligned(kSectorSize, 0);
|
|
|
|
Slice result;
|
|
|
|
#if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(OS_SOLARIS) && \
|
|
|
|
!defined(OS_AIX)
|
|
|
|
if (soptions.use_direct_reads) {
|
|
|
|
soptions.use_direct_reads = false;
|
2013-01-31 23:20:24 +00:00
|
|
|
}
|
2022-10-25 00:54:14 +00:00
|
|
|
#endif
|
|
|
|
ASSERT_OK(env_->NewRandomAccessFile(fname, &file, soptions));
|
|
|
|
ASSERT_OK(file->Read(0, kSectorSize, &result, scratch.get()));
|
|
|
|
ASSERT_EQ(memcmp(scratch.get(), data.get(), kSectorSize), 0);
|
|
|
|
ASSERT_OK(file->InvalidateCache(0, 11));
|
|
|
|
ASSERT_OK(file->InvalidateCache(0, 0));
|
|
|
|
}
|
2013-01-31 23:20:24 +00:00
|
|
|
|
2022-10-25 00:54:14 +00:00
|
|
|
// Sequential Read
|
|
|
|
{
|
|
|
|
std::unique_ptr<SequentialFile> file;
|
|
|
|
auto scratch = NewAligned(kSectorSize, 0);
|
|
|
|
Slice result;
|
|
|
|
#if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(OS_SOLARIS) && \
|
|
|
|
!defined(OS_AIX)
|
|
|
|
if (soptions.use_direct_reads) {
|
|
|
|
soptions.use_direct_reads = false;
|
|
|
|
}
|
2016-05-23 23:05:53 +00:00
|
|
|
#endif
|
2022-10-25 00:54:14 +00:00
|
|
|
ASSERT_OK(env_->NewSequentialFile(fname, &file, soptions));
|
|
|
|
if (file->use_direct_io()) {
|
|
|
|
ASSERT_OK(file->PositionedRead(0, kSectorSize, &result, scratch.get()));
|
|
|
|
} else {
|
|
|
|
ASSERT_OK(file->Read(kSectorSize, &result, scratch.get()));
|
2016-04-21 17:37:27 +00:00
|
|
|
}
|
2022-10-25 00:54:14 +00:00
|
|
|
ASSERT_EQ(memcmp(scratch.get(), data.get(), kSectorSize), 0);
|
|
|
|
ASSERT_OK(file->InvalidateCache(0, 11));
|
|
|
|
ASSERT_OK(file->InvalidateCache(0, 0));
|
|
|
|
}
|
|
|
|
// Delete the file
|
|
|
|
ASSERT_OK(env_->DeleteFile(fname));
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearTrace();
|
2013-09-21 06:00:13 +00:00
|
|
|
}
|
2017-01-09 23:37:57 +00:00
|
|
|
#endif // OS_LINUX || OS_WIN
|
2013-09-21 06:00:13 +00:00
|
|
|
|
2014-03-21 17:26:02 +00:00
|
|
|
class TestLogger : public Logger {
|
|
|
|
public:
|
2015-02-01 19:08:19 +00:00
|
|
|
using Logger::Logv;
|
2019-02-19 21:36:04 +00:00
|
|
|
void Logv(const char* format, va_list ap) override {
|
2014-03-21 17:26:02 +00:00
|
|
|
log_count++;
|
|
|
|
|
|
|
|
char new_format[550];
|
|
|
|
std::fill_n(new_format, sizeof(new_format), '2');
|
|
|
|
{
|
|
|
|
va_list backup_ap;
|
|
|
|
va_copy(backup_ap, ap);
|
|
|
|
int n = vsnprintf(new_format, sizeof(new_format) - 1, format, backup_ap);
|
|
|
|
// 48 bytes for extra information + bytes allocated
|
|
|
|
|
2015-07-13 19:11:05 +00:00
|
|
|
// When we have n == -1 there is not a terminating zero expected
|
2015-07-01 23:13:49 +00:00
|
|
|
#ifdef OS_WIN
|
|
|
|
if (n < 0) {
|
|
|
|
char_0_count++;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-03-21 17:26:02 +00:00
|
|
|
if (new_format[0] == '[') {
|
|
|
|
// "[DEBUG] "
|
2022-05-25 01:20:17 +00:00
|
|
|
ASSERT_TRUE(n <= 56 + (512 - static_cast<int>(sizeof(port::TimeVal))));
|
2014-03-21 17:26:02 +00:00
|
|
|
} else {
|
2022-05-25 01:20:17 +00:00
|
|
|
ASSERT_TRUE(n <= 48 + (512 - static_cast<int>(sizeof(port::TimeVal))));
|
2014-03-21 17:26:02 +00:00
|
|
|
}
|
|
|
|
va_end(backup_ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < sizeof(new_format); i++) {
|
|
|
|
if (new_format[i] == 'x') {
|
|
|
|
char_x_count++;
|
|
|
|
} else if (new_format[i] == '\0') {
|
|
|
|
char_0_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int log_count;
|
|
|
|
int char_x_count;
|
|
|
|
int char_0_count;
|
|
|
|
};
|
|
|
|
|
2016-05-07 00:42:50 +00:00
|
|
|
TEST_P(EnvPosixTestWithParam, LogBufferTest) {
|
2014-03-21 17:26:02 +00:00
|
|
|
TestLogger test_logger;
|
2014-04-10 23:49:49 +00:00
|
|
|
test_logger.SetInfoLogLevel(InfoLogLevel::INFO_LEVEL);
|
2014-03-21 17:26:02 +00:00
|
|
|
test_logger.log_count = 0;
|
|
|
|
test_logger.char_x_count = 0;
|
|
|
|
test_logger.char_0_count = 0;
|
2014-04-10 23:49:49 +00:00
|
|
|
LogBuffer log_buffer(InfoLogLevel::INFO_LEVEL, &test_logger);
|
2014-04-10 22:27:42 +00:00
|
|
|
LogBuffer log_buffer_debug(DEBUG_LEVEL, &test_logger);
|
2014-03-21 17:26:02 +00:00
|
|
|
|
|
|
|
char bytes200[200];
|
|
|
|
std::fill_n(bytes200, sizeof(bytes200), '1');
|
|
|
|
bytes200[sizeof(bytes200) - 1] = '\0';
|
|
|
|
char bytes600[600];
|
|
|
|
std::fill_n(bytes600, sizeof(bytes600), '1');
|
|
|
|
bytes600[sizeof(bytes600) - 1] = '\0';
|
|
|
|
char bytes9000[9000];
|
|
|
|
std::fill_n(bytes9000, sizeof(bytes9000), '1');
|
|
|
|
bytes9000[sizeof(bytes9000) - 1] = '\0';
|
|
|
|
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_BUFFER(&log_buffer, "x%sx", bytes200);
|
|
|
|
ROCKS_LOG_BUFFER(&log_buffer, "x%sx", bytes600);
|
|
|
|
ROCKS_LOG_BUFFER(&log_buffer, "x%sx%sx%sx", bytes200, bytes200, bytes200);
|
|
|
|
ROCKS_LOG_BUFFER(&log_buffer, "x%sx%sx", bytes200, bytes600);
|
|
|
|
ROCKS_LOG_BUFFER(&log_buffer, "x%sx%sx", bytes600, bytes9000);
|
2014-03-21 17:26:02 +00:00
|
|
|
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_BUFFER(&log_buffer_debug, "x%sx", bytes200);
|
2014-04-10 22:27:42 +00:00
|
|
|
test_logger.SetInfoLogLevel(DEBUG_LEVEL);
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_BUFFER(&log_buffer_debug, "x%sx%sx%sx", bytes600, bytes9000,
|
|
|
|
bytes200);
|
2014-03-21 17:26:02 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(0, test_logger.log_count);
|
|
|
|
log_buffer.FlushBufferToLog();
|
|
|
|
log_buffer_debug.FlushBufferToLog();
|
|
|
|
ASSERT_EQ(6, test_logger.log_count);
|
|
|
|
ASSERT_EQ(6, test_logger.char_0_count);
|
|
|
|
ASSERT_EQ(10, test_logger.char_x_count);
|
|
|
|
}
|
|
|
|
|
2014-09-23 20:43:03 +00:00
|
|
|
class TestLogger2 : public Logger {
|
|
|
|
public:
|
|
|
|
explicit TestLogger2(size_t max_log_size) : max_log_size_(max_log_size) {}
|
2015-02-01 19:08:19 +00:00
|
|
|
using Logger::Logv;
|
2019-02-19 21:36:04 +00:00
|
|
|
void Logv(const char* format, va_list ap) override {
|
2014-09-23 20:43:03 +00:00
|
|
|
char new_format[2000];
|
|
|
|
std::fill_n(new_format, sizeof(new_format), '2');
|
|
|
|
{
|
|
|
|
va_list backup_ap;
|
|
|
|
va_copy(backup_ap, ap);
|
|
|
|
int n = vsnprintf(new_format, sizeof(new_format) - 1, format, backup_ap);
|
|
|
|
// 48 bytes for extra information + bytes allocated
|
2022-05-25 01:20:17 +00:00
|
|
|
ASSERT_TRUE(n <=
|
|
|
|
48 + static_cast<int>(max_log_size_ - sizeof(port::TimeVal)));
|
|
|
|
ASSERT_TRUE(n > static_cast<int>(max_log_size_ - sizeof(port::TimeVal)));
|
2014-09-23 20:43:03 +00:00
|
|
|
va_end(backup_ap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
size_t max_log_size_;
|
|
|
|
};
|
|
|
|
|
2016-05-07 00:42:50 +00:00
|
|
|
TEST_P(EnvPosixTestWithParam, LogBufferMaxSizeTest) {
|
2014-09-23 20:43:03 +00:00
|
|
|
char bytes9000[9000];
|
|
|
|
std::fill_n(bytes9000, sizeof(bytes9000), '1');
|
|
|
|
bytes9000[sizeof(bytes9000) - 1] = '\0';
|
|
|
|
|
|
|
|
for (size_t max_log_size = 256; max_log_size <= 1024;
|
|
|
|
max_log_size += 1024 - 256) {
|
|
|
|
TestLogger2 test_logger(max_log_size);
|
|
|
|
test_logger.SetInfoLogLevel(InfoLogLevel::INFO_LEVEL);
|
|
|
|
LogBuffer log_buffer(InfoLogLevel::INFO_LEVEL, &test_logger);
|
2017-03-16 02:22:52 +00:00
|
|
|
ROCKS_LOG_BUFFER_MAX_SZ(&log_buffer, max_log_size, "%s", bytes9000);
|
2014-09-23 20:43:03 +00:00
|
|
|
log_buffer.FlushBufferToLog();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-07 00:42:50 +00:00
|
|
|
TEST_P(EnvPosixTestWithParam, Preallocation) {
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
2018-07-14 00:18:39 +00:00
|
|
|
const std::string src = test::PerThreadDBPath(env_, "testfile");
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<WritableFile> srcfile;
|
2018-07-14 00:18:39 +00:00
|
|
|
EnvOptions soptions;
|
|
|
|
soptions.use_direct_reads = soptions.use_direct_writes = direct_io_;
|
2022-10-25 00:54:14 +00:00
|
|
|
#if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(OS_SOLARIS) && \
|
|
|
|
!defined(OS_AIX) && !defined(OS_OPENBSD) && !defined(OS_FREEBSD)
|
|
|
|
if (soptions.use_direct_writes) {
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
|
|
|
"NewWritableFile:O_DIRECT", [&](void* arg) {
|
|
|
|
int* val = static_cast<int*>(arg);
|
|
|
|
*val &= ~O_DIRECT;
|
|
|
|
});
|
|
|
|
}
|
2016-05-23 23:05:53 +00:00
|
|
|
#endif
|
2022-10-25 00:54:14 +00:00
|
|
|
ASSERT_OK(env_->NewWritableFile(src, &srcfile, soptions));
|
|
|
|
srcfile->SetPreallocationBlockSize(1024 * 1024);
|
|
|
|
|
|
|
|
// No writes should mean no preallocation
|
|
|
|
size_t block_size, last_allocated_block;
|
|
|
|
srcfile->GetPreallocationStatus(&block_size, &last_allocated_block);
|
|
|
|
ASSERT_EQ(last_allocated_block, 0UL);
|
|
|
|
|
|
|
|
// Small write should preallocate one block
|
|
|
|
size_t kStrSize = 4096;
|
|
|
|
auto data = NewAligned(kStrSize, 'A');
|
|
|
|
Slice str(data.get(), kStrSize);
|
|
|
|
srcfile->PrepareWrite(srcfile->GetFileSize(), kStrSize);
|
|
|
|
ASSERT_OK(srcfile->Append(str));
|
|
|
|
srcfile->GetPreallocationStatus(&block_size, &last_allocated_block);
|
|
|
|
ASSERT_EQ(last_allocated_block, 1UL);
|
|
|
|
|
|
|
|
// Write an entire preallocation block, make sure we increased by two.
|
|
|
|
{
|
|
|
|
auto buf_ptr = NewAligned(block_size, ' ');
|
|
|
|
Slice buf(buf_ptr.get(), block_size);
|
|
|
|
srcfile->PrepareWrite(srcfile->GetFileSize(), block_size);
|
|
|
|
ASSERT_OK(srcfile->Append(buf));
|
2016-04-21 17:37:27 +00:00
|
|
|
srcfile->GetPreallocationStatus(&block_size, &last_allocated_block);
|
2022-10-25 00:54:14 +00:00
|
|
|
ASSERT_EQ(last_allocated_block, 2UL);
|
|
|
|
}
|
2016-04-21 17:37:27 +00:00
|
|
|
|
2022-10-25 00:54:14 +00:00
|
|
|
// Write five more blocks at once, ensure we're where we need to be.
|
|
|
|
{
|
|
|
|
auto buf_ptr = NewAligned(block_size * 5, ' ');
|
|
|
|
Slice buf = Slice(buf_ptr.get(), block_size * 5);
|
|
|
|
srcfile->PrepareWrite(srcfile->GetFileSize(), buf.size());
|
|
|
|
ASSERT_OK(srcfile->Append(buf));
|
|
|
|
srcfile->GetPreallocationStatus(&block_size, &last_allocated_block);
|
|
|
|
ASSERT_EQ(last_allocated_block, 7UL);
|
|
|
|
}
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearTrace();
|
2014-10-31 22:08:10 +00:00
|
|
|
}
|
|
|
|
|
2016-02-09 22:54:32 +00:00
|
|
|
// Test that the two ways to get children file attributes (in bulk or
|
|
|
|
// individually) behave consistently.
|
2016-05-07 00:42:50 +00:00
|
|
|
TEST_P(EnvPosixTestWithParam, ConsistentChildrenAttributes) {
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
|
|
|
EnvOptions soptions;
|
|
|
|
soptions.use_direct_reads = soptions.use_direct_writes = direct_io_;
|
|
|
|
const int kNumChildren = 10;
|
2016-04-21 17:37:27 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
std::string data;
|
2020-06-25 19:07:47 +00:00
|
|
|
std::string test_base_dir = test::PerThreadDBPath(env_, "env_test_chr_attr");
|
2020-07-29 05:58:28 +00:00
|
|
|
env_->CreateDir(test_base_dir).PermitUncheckedError();
|
2020-02-20 20:07:53 +00:00
|
|
|
for (int i = 0; i < kNumChildren; ++i) {
|
2020-06-25 19:07:47 +00:00
|
|
|
const std::string path = test_base_dir + "/testfile_" + std::to_string(i);
|
2020-02-20 20:07:53 +00:00
|
|
|
std::unique_ptr<WritableFile> file;
|
2022-10-25 00:54:14 +00:00
|
|
|
#if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(OS_SOLARIS) && \
|
|
|
|
!defined(OS_AIX) && !defined(OS_OPENBSD) && !defined(OS_FREEBSD)
|
|
|
|
if (soptions.use_direct_writes) {
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
|
|
|
"NewWritableFile:O_DIRECT", [&](void* arg) {
|
|
|
|
int* val = static_cast<int*>(arg);
|
|
|
|
*val &= ~O_DIRECT;
|
|
|
|
});
|
|
|
|
}
|
2016-05-23 23:05:53 +00:00
|
|
|
#endif
|
2022-10-25 00:54:14 +00:00
|
|
|
ASSERT_OK(env_->NewWritableFile(path, &file, soptions));
|
|
|
|
auto buf_ptr = NewAligned(data.size(), 'T');
|
|
|
|
Slice buf(buf_ptr.get(), data.size());
|
|
|
|
ASSERT_OK(file->Append(buf));
|
|
|
|
data.append(std::string(4096, 'T'));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Env::FileAttributes> file_attrs;
|
|
|
|
ASSERT_OK(env_->GetChildrenFileAttributes(test_base_dir, &file_attrs));
|
|
|
|
for (int i = 0; i < kNumChildren; ++i) {
|
|
|
|
const std::string name = "testfile_" + std::to_string(i);
|
|
|
|
const std::string path = test_base_dir + "/" + name;
|
|
|
|
|
|
|
|
auto file_attrs_iter = std::find_if(
|
|
|
|
file_attrs.begin(), file_attrs.end(),
|
|
|
|
[&name](const Env::FileAttributes& fm) { return fm.name == name; });
|
|
|
|
ASSERT_TRUE(file_attrs_iter != file_attrs.end());
|
|
|
|
uint64_t size;
|
|
|
|
ASSERT_OK(env_->GetFileSize(path, &size));
|
|
|
|
ASSERT_EQ(size, 4096 * i);
|
|
|
|
ASSERT_EQ(size, file_attrs_iter->size_bytes);
|
|
|
|
}
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearTrace();
|
2016-02-09 22:54:32 +00:00
|
|
|
}
|
|
|
|
|
2015-07-03 00:23:41 +00:00
|
|
|
// Test that all WritableFileWrapper forwards all calls to WritableFile.
|
2016-05-07 00:42:50 +00:00
|
|
|
TEST_P(EnvPosixTestWithParam, WritableFileWrapper) {
|
2015-07-03 00:23:41 +00:00
|
|
|
class Base : public WritableFile {
|
|
|
|
public:
|
2022-10-25 00:54:14 +00:00
|
|
|
mutable int* step_;
|
2015-07-03 00:23:41 +00:00
|
|
|
|
2022-10-25 00:54:14 +00:00
|
|
|
void inc(int x) const { EXPECT_EQ(x, (*step_)++); }
|
2015-07-03 00:23:41 +00:00
|
|
|
|
2022-10-25 00:54:14 +00:00
|
|
|
explicit Base(int* step) : step_(step) { inc(0); }
|
2015-07-03 00:23:41 +00:00
|
|
|
|
2018-03-05 21:08:17 +00:00
|
|
|
Status Append(const Slice& /*data*/) override {
|
|
|
|
inc(1);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2018-10-24 19:05:24 +00:00
|
|
|
|
2021-03-19 18:42:27 +00:00
|
|
|
Status Append(
|
|
|
|
const Slice& /*data*/,
|
|
|
|
const DataVerificationInfo& /* verification_info */) override {
|
|
|
|
inc(1);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2018-10-24 19:05:24 +00:00
|
|
|
Status PositionedAppend(const Slice& /*data*/,
|
|
|
|
uint64_t /*offset*/) override {
|
|
|
|
inc(2);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2021-03-19 18:42:27 +00:00
|
|
|
Status PositionedAppend(
|
|
|
|
const Slice& /*data*/, uint64_t /*offset*/,
|
|
|
|
const DataVerificationInfo& /* verification_info */) override {
|
|
|
|
inc(2);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2018-10-24 19:05:24 +00:00
|
|
|
Status Truncate(uint64_t /*size*/) override {
|
|
|
|
inc(3);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status Close() override {
|
|
|
|
inc(4);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status Flush() override {
|
|
|
|
inc(5);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status Sync() override {
|
|
|
|
inc(6);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status Fsync() override {
|
|
|
|
inc(7);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsSyncThreadSafe() const override {
|
|
|
|
inc(8);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool use_direct_io() const override {
|
|
|
|
inc(9);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GetRequiredBufferAlignment() const override {
|
|
|
|
inc(10);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetIOPriority(Env::IOPriority /*pri*/) override { inc(11); }
|
|
|
|
|
|
|
|
Env::IOPriority GetIOPriority() override {
|
|
|
|
inc(12);
|
|
|
|
return Env::IOPriority::IO_LOW;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetWriteLifeTimeHint(Env::WriteLifeTimeHint /*hint*/) override {
|
|
|
|
inc(13);
|
|
|
|
}
|
|
|
|
|
|
|
|
Env::WriteLifeTimeHint GetWriteLifeTimeHint() override {
|
|
|
|
inc(14);
|
|
|
|
return Env::WriteLifeTimeHint::WLTH_NOT_SET;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t GetFileSize() override {
|
|
|
|
inc(15);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetPreallocationBlockSize(size_t /*size*/) override { inc(16); }
|
|
|
|
|
2018-03-05 21:08:17 +00:00
|
|
|
void GetPreallocationStatus(size_t* /*block_size*/,
|
|
|
|
size_t* /*last_allocated_block*/) override {
|
2018-10-24 19:05:24 +00:00
|
|
|
inc(17);
|
2015-07-03 00:23:41 +00:00
|
|
|
}
|
2018-10-24 19:05:24 +00:00
|
|
|
|
2018-03-05 21:08:17 +00:00
|
|
|
size_t GetUniqueId(char* /*id*/, size_t /*max_size*/) const override {
|
2018-10-24 19:05:24 +00:00
|
|
|
inc(18);
|
2015-07-03 00:23:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2018-10-24 19:05:24 +00:00
|
|
|
|
2018-03-05 21:08:17 +00:00
|
|
|
Status InvalidateCache(size_t /*offset*/, size_t /*length*/) override {
|
2018-10-24 19:05:24 +00:00
|
|
|
inc(19);
|
2015-07-03 00:23:41 +00:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2018-10-24 19:05:24 +00:00
|
|
|
Status RangeSync(uint64_t /*offset*/, uint64_t /*nbytes*/) override {
|
|
|
|
inc(20);
|
2015-07-03 00:23:41 +00:00
|
|
|
return Status::OK();
|
|
|
|
}
|
2018-10-24 19:05:24 +00:00
|
|
|
|
|
|
|
void PrepareWrite(size_t /*offset*/, size_t /*len*/) override { inc(21); }
|
|
|
|
|
|
|
|
Status Allocate(uint64_t /*offset*/, uint64_t /*len*/) override {
|
|
|
|
inc(22);
|
2015-07-03 00:23:41 +00:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2019-02-19 21:36:04 +00:00
|
|
|
~Base() override { inc(23); }
|
2015-07-03 00:23:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Wrapper : public WritableFileWrapper {
|
|
|
|
public:
|
|
|
|
explicit Wrapper(WritableFile* target) : WritableFileWrapper(target) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
int step = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
Base b(&step);
|
|
|
|
Wrapper w(&b);
|
2020-07-29 05:58:28 +00:00
|
|
|
ASSERT_OK(w.Append(Slice()));
|
|
|
|
ASSERT_OK(w.PositionedAppend(Slice(), 0));
|
|
|
|
ASSERT_OK(w.Truncate(0));
|
|
|
|
ASSERT_OK(w.Close());
|
|
|
|
ASSERT_OK(w.Flush());
|
|
|
|
ASSERT_OK(w.Sync());
|
|
|
|
ASSERT_OK(w.Fsync());
|
2018-10-24 19:05:24 +00:00
|
|
|
w.IsSyncThreadSafe();
|
|
|
|
w.use_direct_io();
|
|
|
|
w.GetRequiredBufferAlignment();
|
2015-07-03 00:23:41 +00:00
|
|
|
w.SetIOPriority(Env::IOPriority::IO_HIGH);
|
2018-10-24 19:05:24 +00:00
|
|
|
w.GetIOPriority();
|
|
|
|
w.SetWriteLifeTimeHint(Env::WriteLifeTimeHint::WLTH_NOT_SET);
|
|
|
|
w.GetWriteLifeTimeHint();
|
2015-07-03 00:23:41 +00:00
|
|
|
w.GetFileSize();
|
2018-10-24 19:05:24 +00:00
|
|
|
w.SetPreallocationBlockSize(0);
|
2015-07-03 00:23:41 +00:00
|
|
|
w.GetPreallocationStatus(nullptr, nullptr);
|
|
|
|
w.GetUniqueId(nullptr, 0);
|
2020-07-29 05:58:28 +00:00
|
|
|
ASSERT_OK(w.InvalidateCache(0, 0));
|
|
|
|
ASSERT_OK(w.RangeSync(0, 0));
|
2018-10-24 19:05:24 +00:00
|
|
|
w.PrepareWrite(0, 0);
|
2020-07-29 05:58:28 +00:00
|
|
|
ASSERT_OK(w.Allocate(0, 0));
|
2015-07-03 00:23:41 +00:00
|
|
|
}
|
|
|
|
|
2018-10-24 19:05:24 +00:00
|
|
|
EXPECT_EQ(24, step);
|
2015-07-03 00:23:41 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 19:08:22 +00:00
|
|
|
TEST_P(EnvPosixTestWithParam, PosixRandomRWFile) {
|
2018-07-14 00:18:39 +00:00
|
|
|
const std::string path = test::PerThreadDBPath(env_, "random_rw_file");
|
2016-09-13 19:08:22 +00:00
|
|
|
|
2020-07-29 05:58:28 +00:00
|
|
|
env_->DeleteFile(path).PermitUncheckedError();
|
2016-09-13 19:08:22 +00:00
|
|
|
|
|
|
|
std::unique_ptr<RandomRWFile> file;
|
2018-05-09 17:24:11 +00:00
|
|
|
|
|
|
|
// Cannot open non-existing file.
|
|
|
|
ASSERT_NOK(env_->NewRandomRWFile(path, &file, EnvOptions()));
|
|
|
|
|
2022-06-22 22:45:21 +00:00
|
|
|
// Create the file using WritableFile
|
2018-05-09 17:24:11 +00:00
|
|
|
{
|
|
|
|
std::unique_ptr<WritableFile> wf;
|
|
|
|
ASSERT_OK(env_->NewWritableFile(path, &wf, EnvOptions()));
|
|
|
|
}
|
|
|
|
|
2016-09-13 19:08:22 +00:00
|
|
|
ASSERT_OK(env_->NewRandomRWFile(path, &file, EnvOptions()));
|
|
|
|
|
|
|
|
char buf[10000];
|
|
|
|
Slice read_res;
|
|
|
|
|
|
|
|
ASSERT_OK(file->Write(0, "ABCD"));
|
|
|
|
ASSERT_OK(file->Read(0, 10, &read_res, buf));
|
|
|
|
ASSERT_EQ(read_res.ToString(), "ABCD");
|
|
|
|
|
|
|
|
ASSERT_OK(file->Write(2, "XXXX"));
|
|
|
|
ASSERT_OK(file->Read(0, 10, &read_res, buf));
|
|
|
|
ASSERT_EQ(read_res.ToString(), "ABXXXX");
|
|
|
|
|
|
|
|
ASSERT_OK(file->Write(10, "ZZZ"));
|
|
|
|
ASSERT_OK(file->Read(10, 10, &read_res, buf));
|
|
|
|
ASSERT_EQ(read_res.ToString(), "ZZZ");
|
|
|
|
|
|
|
|
ASSERT_OK(file->Write(11, "Y"));
|
|
|
|
ASSERT_OK(file->Read(10, 10, &read_res, buf));
|
|
|
|
ASSERT_EQ(read_res.ToString(), "ZYZ");
|
|
|
|
|
|
|
|
ASSERT_OK(file->Write(200, "FFFFF"));
|
|
|
|
ASSERT_OK(file->Read(200, 10, &read_res, buf));
|
|
|
|
ASSERT_EQ(read_res.ToString(), "FFFFF");
|
|
|
|
|
|
|
|
ASSERT_OK(file->Write(205, "XXXX"));
|
|
|
|
ASSERT_OK(file->Read(200, 10, &read_res, buf));
|
|
|
|
ASSERT_EQ(read_res.ToString(), "FFFFFXXXX");
|
|
|
|
|
|
|
|
ASSERT_OK(file->Write(5, "QQQQ"));
|
|
|
|
ASSERT_OK(file->Read(0, 9, &read_res, buf));
|
|
|
|
ASSERT_EQ(read_res.ToString(), "ABXXXQQQQ");
|
|
|
|
|
|
|
|
ASSERT_OK(file->Read(2, 4, &read_res, buf));
|
|
|
|
ASSERT_EQ(read_res.ToString(), "XXXQ");
|
|
|
|
|
|
|
|
// Close file and reopen it
|
2020-07-29 05:58:28 +00:00
|
|
|
ASSERT_OK(file->Close());
|
2016-09-13 19:08:22 +00:00
|
|
|
ASSERT_OK(env_->NewRandomRWFile(path, &file, EnvOptions()));
|
|
|
|
|
|
|
|
ASSERT_OK(file->Read(0, 9, &read_res, buf));
|
|
|
|
ASSERT_EQ(read_res.ToString(), "ABXXXQQQQ");
|
|
|
|
|
|
|
|
ASSERT_OK(file->Read(10, 3, &read_res, buf));
|
|
|
|
ASSERT_EQ(read_res.ToString(), "ZYZ");
|
|
|
|
|
|
|
|
ASSERT_OK(file->Read(200, 9, &read_res, buf));
|
|
|
|
ASSERT_EQ(read_res.ToString(), "FFFFFXXXX");
|
|
|
|
|
|
|
|
ASSERT_OK(file->Write(4, "TTTTTTTTTTTTTTTT"));
|
|
|
|
ASSERT_OK(file->Read(0, 10, &read_res, buf));
|
|
|
|
ASSERT_EQ(read_res.ToString(), "ABXXTTTTTT");
|
|
|
|
|
|
|
|
// Clean up
|
2020-07-29 05:58:28 +00:00
|
|
|
ASSERT_OK(env_->DeleteFile(path));
|
2016-09-13 19:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class RandomRWFileWithMirrorString {
|
|
|
|
public:
|
|
|
|
explicit RandomRWFileWithMirrorString(RandomRWFile* _file) : file_(_file) {}
|
|
|
|
|
|
|
|
void Write(size_t offset, const std::string& data) {
|
|
|
|
// Write to mirror string
|
|
|
|
StringWrite(offset, data);
|
|
|
|
|
|
|
|
// Write to file
|
|
|
|
Status s = file_->Write(offset, data);
|
|
|
|
ASSERT_OK(s) << s.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Read(size_t offset = 0, size_t n = 1000000) {
|
|
|
|
Slice str_res(nullptr, 0);
|
|
|
|
if (offset < file_mirror_.size()) {
|
|
|
|
size_t str_res_sz = std::min(file_mirror_.size() - offset, n);
|
|
|
|
str_res = Slice(file_mirror_.data() + offset, str_res_sz);
|
|
|
|
StopSliceAtNull(&str_res);
|
|
|
|
}
|
|
|
|
|
|
|
|
Slice file_res;
|
|
|
|
Status s = file_->Read(offset, n, &file_res, buf_);
|
|
|
|
ASSERT_OK(s) << s.ToString();
|
|
|
|
StopSliceAtNull(&file_res);
|
|
|
|
|
|
|
|
ASSERT_EQ(str_res.ToString(), file_res.ToString()) << offset << " " << n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetFile(RandomRWFile* _file) { file_ = _file; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void StringWrite(size_t offset, const std::string& src) {
|
|
|
|
if (offset + src.size() > file_mirror_.size()) {
|
|
|
|
file_mirror_.resize(offset + src.size(), '\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
char* pos = const_cast<char*>(file_mirror_.data() + offset);
|
|
|
|
memcpy(pos, src.data(), src.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void StopSliceAtNull(Slice* slc) {
|
|
|
|
for (size_t i = 0; i < slc->size(); i++) {
|
|
|
|
if ((*slc)[i] == '\0') {
|
|
|
|
*slc = Slice(slc->data(), i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char buf_[10000];
|
|
|
|
RandomRWFile* file_;
|
|
|
|
std::string file_mirror_;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_P(EnvPosixTestWithParam, PosixRandomRWFileRandomized) {
|
2018-07-14 00:18:39 +00:00
|
|
|
const std::string path = test::PerThreadDBPath(env_, "random_rw_file_rand");
|
2020-07-29 05:58:28 +00:00
|
|
|
env_->DeleteFile(path).PermitUncheckedError();
|
2016-09-13 19:08:22 +00:00
|
|
|
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<RandomRWFile> file;
|
2018-05-09 17:24:11 +00:00
|
|
|
|
|
|
|
#ifdef OS_LINUX
|
|
|
|
// Cannot open non-existing file.
|
|
|
|
ASSERT_NOK(env_->NewRandomRWFile(path, &file, EnvOptions()));
|
|
|
|
#endif
|
|
|
|
|
2022-06-22 22:45:21 +00:00
|
|
|
// Create the file using WritableFile
|
2018-05-09 17:24:11 +00:00
|
|
|
{
|
|
|
|
std::unique_ptr<WritableFile> wf;
|
|
|
|
ASSERT_OK(env_->NewWritableFile(path, &wf, EnvOptions()));
|
|
|
|
}
|
|
|
|
|
2016-09-13 19:08:22 +00:00
|
|
|
ASSERT_OK(env_->NewRandomRWFile(path, &file, EnvOptions()));
|
|
|
|
RandomRWFileWithMirrorString file_with_mirror(file.get());
|
|
|
|
|
|
|
|
Random rnd(301);
|
|
|
|
std::string buf;
|
|
|
|
for (int i = 0; i < 10000; i++) {
|
|
|
|
// Genrate random data
|
2020-07-09 21:33:42 +00:00
|
|
|
buf = rnd.RandomString(10);
|
2016-09-13 19:08:22 +00:00
|
|
|
|
|
|
|
// Pick random offset for write
|
|
|
|
size_t write_off = rnd.Next() % 1000;
|
|
|
|
file_with_mirror.Write(write_off, buf);
|
|
|
|
|
|
|
|
// Pick random offset for read
|
|
|
|
size_t read_off = rnd.Next() % 1000;
|
|
|
|
size_t read_sz = rnd.Next() % 20;
|
|
|
|
file_with_mirror.Read(read_off, read_sz);
|
|
|
|
|
|
|
|
if (i % 500 == 0) {
|
|
|
|
// Reopen the file every 500 iters
|
|
|
|
ASSERT_OK(env_->NewRandomRWFile(path, &file, EnvOptions()));
|
|
|
|
file_with_mirror.SetFile(file.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean up
|
2020-07-29 05:58:28 +00:00
|
|
|
ASSERT_OK(env_->DeleteFile(path));
|
2016-09-13 19:08:22 +00:00
|
|
|
}
|
|
|
|
|
2018-02-23 21:50:02 +00:00
|
|
|
class TestEnv : public EnvWrapper {
|
2022-10-25 00:54:14 +00:00
|
|
|
public:
|
|
|
|
explicit TestEnv() : EnvWrapper(Env::Default()), close_count(0) {}
|
|
|
|
const char* Name() const override { return "TestEnv"; }
|
|
|
|
class TestLogger : public Logger {
|
|
|
|
public:
|
|
|
|
using Logger::Logv;
|
|
|
|
explicit TestLogger(TestEnv* env_ptr) : Logger() { env = env_ptr; }
|
|
|
|
~TestLogger() override {
|
|
|
|
if (!closed_) {
|
|
|
|
Status s = CloseHelper();
|
|
|
|
s.PermitUncheckedError();
|
2018-03-07 00:13:05 +00:00
|
|
|
}
|
2022-10-25 00:54:14 +00:00
|
|
|
}
|
|
|
|
void Logv(const char* /*format*/, va_list /*ap*/) override {}
|
2018-02-23 21:50:02 +00:00
|
|
|
|
2022-10-25 00:54:14 +00:00
|
|
|
protected:
|
|
|
|
Status CloseImpl() override { return CloseHelper(); }
|
2018-02-23 21:50:02 +00:00
|
|
|
|
2022-10-25 00:54:14 +00:00
|
|
|
private:
|
|
|
|
Status CloseHelper() {
|
|
|
|
env->CloseCountInc();
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
TestEnv* env;
|
|
|
|
};
|
2018-02-23 21:50:02 +00:00
|
|
|
|
2018-03-07 00:13:05 +00:00
|
|
|
void CloseCountInc() { close_count++; }
|
2018-02-23 21:50:02 +00:00
|
|
|
|
2018-03-07 00:13:05 +00:00
|
|
|
int GetCloseCount() { return close_count; }
|
|
|
|
|
2019-02-19 21:36:04 +00:00
|
|
|
Status NewLogger(const std::string& /*fname*/,
|
|
|
|
std::shared_ptr<Logger>* result) override {
|
2018-03-07 00:13:05 +00:00
|
|
|
result->reset(new TestLogger(this));
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int close_count;
|
2018-02-23 21:50:02 +00:00
|
|
|
};
|
|
|
|
|
2020-04-17 21:36:51 +00:00
|
|
|
class EnvTest : public testing::Test {
|
|
|
|
public:
|
|
|
|
EnvTest() : test_directory_(test::PerThreadDBPath("env_test")) {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
const std::string test_directory_;
|
|
|
|
};
|
2018-03-07 00:13:05 +00:00
|
|
|
|
2018-02-23 21:50:02 +00:00
|
|
|
TEST_F(EnvTest, Close) {
|
2018-03-07 00:13:05 +00:00
|
|
|
TestEnv* env = new TestEnv();
|
2018-02-23 21:50:02 +00:00
|
|
|
std::shared_ptr<Logger> logger;
|
|
|
|
Status s;
|
|
|
|
|
|
|
|
s = env->NewLogger("", &logger);
|
2020-07-29 05:58:28 +00:00
|
|
|
ASSERT_OK(s);
|
|
|
|
ASSERT_OK(logger.get()->Close());
|
2018-02-23 21:50:02 +00:00
|
|
|
ASSERT_EQ(env->GetCloseCount(), 1);
|
|
|
|
// Call Close() again. CloseHelper() should not be called again
|
2020-07-29 05:58:28 +00:00
|
|
|
ASSERT_OK(logger.get()->Close());
|
2018-02-23 21:50:02 +00:00
|
|
|
ASSERT_EQ(env->GetCloseCount(), 1);
|
|
|
|
logger.reset();
|
|
|
|
ASSERT_EQ(env->GetCloseCount(), 1);
|
|
|
|
|
|
|
|
s = env->NewLogger("", &logger);
|
2020-07-29 05:58:28 +00:00
|
|
|
ASSERT_OK(s);
|
2018-02-23 21:50:02 +00:00
|
|
|
logger.reset();
|
|
|
|
ASSERT_EQ(env->GetCloseCount(), 2);
|
|
|
|
|
|
|
|
delete env;
|
|
|
|
}
|
|
|
|
|
2020-10-28 16:59:12 +00:00
|
|
|
class LogvWithInfoLogLevelLogger : public Logger {
|
|
|
|
public:
|
|
|
|
using Logger::Logv;
|
|
|
|
void Logv(const InfoLogLevel /* log_level */, const char* /* format */,
|
|
|
|
va_list /* ap */) override {}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(EnvTest, LogvWithInfoLogLevel) {
|
|
|
|
// Verifies the log functions work on a `Logger` that only overrides the
|
|
|
|
// `Logv()` overload including `InfoLogLevel`.
|
|
|
|
const std::string kSampleMessage("sample log message");
|
|
|
|
LogvWithInfoLogLevelLogger logger;
|
|
|
|
ROCKS_LOG_HEADER(&logger, "%s", kSampleMessage.c_str());
|
|
|
|
ROCKS_LOG_DEBUG(&logger, "%s", kSampleMessage.c_str());
|
|
|
|
ROCKS_LOG_INFO(&logger, "%s", kSampleMessage.c_str());
|
|
|
|
ROCKS_LOG_WARN(&logger, "%s", kSampleMessage.c_str());
|
|
|
|
ROCKS_LOG_ERROR(&logger, "%s", kSampleMessage.c_str());
|
|
|
|
ROCKS_LOG_FATAL(&logger, "%s", kSampleMessage.c_str());
|
|
|
|
}
|
|
|
|
|
2020-06-03 22:53:09 +00:00
|
|
|
INSTANTIATE_TEST_CASE_P(DefaultEnvWithoutDirectIO, EnvPosixTestWithParam,
|
|
|
|
::testing::Values(std::pair<Env*, bool>(Env::Default(),
|
|
|
|
false)));
|
|
|
|
INSTANTIATE_TEST_CASE_P(DefaultEnvWithDirectIO, EnvPosixTestWithParam,
|
|
|
|
::testing::Values(std::pair<Env*, bool>(Env::Default(),
|
|
|
|
true)));
|
2017-02-16 18:25:06 +00:00
|
|
|
|
2023-01-27 21:14:19 +00:00
|
|
|
#if !defined(OS_WIN)
|
2021-11-02 16:06:02 +00:00
|
|
|
static Env* GetChrootEnv() {
|
|
|
|
static std::unique_ptr<Env> chroot_env(
|
|
|
|
NewChrootEnv(Env::Default(), test::TmpDir(Env::Default())));
|
|
|
|
return chroot_env.get();
|
|
|
|
}
|
|
|
|
INSTANTIATE_TEST_CASE_P(ChrootEnvWithoutDirectIO, EnvPosixTestWithParam,
|
|
|
|
::testing::Values(std::pair<Env*, bool>(GetChrootEnv(),
|
|
|
|
false)));
|
|
|
|
INSTANTIATE_TEST_CASE_P(ChrootEnvWithDirectIO, EnvPosixTestWithParam,
|
|
|
|
::testing::Values(std::pair<Env*, bool>(GetChrootEnv(),
|
|
|
|
true)));
|
2023-01-27 21:14:19 +00:00
|
|
|
#endif // !defined(OS_WIN)
|
2016-05-07 00:42:50 +00:00
|
|
|
|
2020-03-24 04:50:42 +00:00
|
|
|
class EnvFSTestWithParam
|
|
|
|
: public ::testing::Test,
|
|
|
|
public ::testing::WithParamInterface<std::tuple<bool, bool, bool>> {
|
|
|
|
public:
|
|
|
|
EnvFSTestWithParam() {
|
|
|
|
bool env_non_null = std::get<0>(GetParam());
|
|
|
|
bool env_default = std::get<1>(GetParam());
|
|
|
|
bool fs_default = std::get<2>(GetParam());
|
|
|
|
|
|
|
|
env_ = env_non_null ? (env_default ? Env::Default() : nullptr) : nullptr;
|
|
|
|
fs_ = fs_default
|
|
|
|
? FileSystem::Default()
|
|
|
|
: std::make_shared<FaultInjectionTestFS>(FileSystem::Default());
|
|
|
|
if (env_non_null && env_default && !fs_default) {
|
|
|
|
env_ptr_ = NewCompositeEnv(fs_);
|
|
|
|
}
|
|
|
|
if (env_non_null && !env_default && fs_default) {
|
2022-10-25 00:54:14 +00:00
|
|
|
env_ptr_ =
|
|
|
|
std::unique_ptr<Env>(new FaultInjectionTestEnv(Env::Default()));
|
2020-03-24 04:50:42 +00:00
|
|
|
fs_.reset();
|
|
|
|
}
|
|
|
|
if (env_non_null && !env_default && !fs_default) {
|
|
|
|
env_ptr_.reset(new FaultInjectionTestEnv(Env::Default()));
|
|
|
|
composite_env_ptr_.reset(new CompositeEnvWrapper(env_ptr_.get(), fs_));
|
|
|
|
env_ = composite_env_ptr_.get();
|
|
|
|
} else {
|
|
|
|
env_ = env_ptr_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
dbname1_ = test::PerThreadDBPath("env_fs_test1");
|
|
|
|
dbname2_ = test::PerThreadDBPath("env_fs_test2");
|
|
|
|
}
|
|
|
|
|
|
|
|
~EnvFSTestWithParam() = default;
|
|
|
|
|
|
|
|
Env* env_;
|
|
|
|
std::unique_ptr<Env> env_ptr_;
|
|
|
|
std::unique_ptr<Env> composite_env_ptr_;
|
|
|
|
std::shared_ptr<FileSystem> fs_;
|
|
|
|
std::string dbname1_;
|
|
|
|
std::string dbname2_;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_P(EnvFSTestWithParam, OptionsTest) {
|
|
|
|
Options opts;
|
|
|
|
opts.env = env_;
|
|
|
|
opts.create_if_missing = true;
|
|
|
|
std::string dbname = dbname1_;
|
|
|
|
|
|
|
|
if (env_) {
|
|
|
|
if (fs_) {
|
|
|
|
ASSERT_EQ(fs_.get(), env_->GetFileSystem().get());
|
|
|
|
} else {
|
|
|
|
ASSERT_NE(FileSystem::Default().get(), env_->GetFileSystem().get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 2; ++i) {
|
|
|
|
DB* db;
|
|
|
|
Status s = DB::Open(opts, dbname, &db);
|
|
|
|
ASSERT_OK(s);
|
|
|
|
|
|
|
|
WriteOptions wo;
|
2020-08-21 02:16:56 +00:00
|
|
|
ASSERT_OK(db->Put(wo, "a", "a"));
|
|
|
|
ASSERT_OK(db->Flush(FlushOptions()));
|
|
|
|
ASSERT_OK(db->Put(wo, "b", "b"));
|
|
|
|
ASSERT_OK(db->Flush(FlushOptions()));
|
|
|
|
ASSERT_OK(db->CompactRange(CompactRangeOptions(), nullptr, nullptr));
|
2020-03-24 04:50:42 +00:00
|
|
|
|
|
|
|
std::string val;
|
|
|
|
ASSERT_OK(db->Get(ReadOptions(), "a", &val));
|
|
|
|
ASSERT_EQ("a", val);
|
|
|
|
ASSERT_OK(db->Get(ReadOptions(), "b", &val));
|
|
|
|
ASSERT_EQ("b", val);
|
|
|
|
|
2020-08-21 02:16:56 +00:00
|
|
|
ASSERT_OK(db->Close());
|
2020-03-24 04:50:42 +00:00
|
|
|
delete db;
|
2020-12-24 00:54:05 +00:00
|
|
|
ASSERT_OK(DestroyDB(dbname, opts));
|
2020-03-24 04:50:42 +00:00
|
|
|
|
|
|
|
dbname = dbname2_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The parameters are as follows -
|
|
|
|
// 1. True means Options::env is non-null, false means null
|
|
|
|
// 2. True means use Env::Default, false means custom
|
|
|
|
// 3. True means use FileSystem::Default, false means custom
|
2022-10-25 00:54:14 +00:00
|
|
|
INSTANTIATE_TEST_CASE_P(EnvFSTest, EnvFSTestWithParam,
|
|
|
|
::testing::Combine(::testing::Bool(), ::testing::Bool(),
|
|
|
|
::testing::Bool()));
|
2020-03-24 04:50:42 +00:00
|
|
|
// This test ensures that default Env and those allocated by
|
|
|
|
// NewCompositeEnv() all share the same threadpool
|
|
|
|
TEST_F(EnvTest, MultipleCompositeEnv) {
|
|
|
|
std::shared_ptr<FaultInjectionTestFS> fs1 =
|
2022-10-25 00:54:14 +00:00
|
|
|
std::make_shared<FaultInjectionTestFS>(FileSystem::Default());
|
2020-03-24 04:50:42 +00:00
|
|
|
std::shared_ptr<FaultInjectionTestFS> fs2 =
|
2022-10-25 00:54:14 +00:00
|
|
|
std::make_shared<FaultInjectionTestFS>(FileSystem::Default());
|
2020-03-24 04:50:42 +00:00
|
|
|
std::unique_ptr<Env> env1 = NewCompositeEnv(fs1);
|
|
|
|
std::unique_ptr<Env> env2 = NewCompositeEnv(fs2);
|
|
|
|
Env::Default()->SetBackgroundThreads(8, Env::HIGH);
|
|
|
|
Env::Default()->SetBackgroundThreads(16, Env::LOW);
|
|
|
|
ASSERT_EQ(env1->GetBackgroundThreads(Env::LOW), 16);
|
|
|
|
ASSERT_EQ(env1->GetBackgroundThreads(Env::HIGH), 8);
|
|
|
|
ASSERT_EQ(env2->GetBackgroundThreads(Env::LOW), 16);
|
|
|
|
ASSERT_EQ(env2->GetBackgroundThreads(Env::HIGH), 8);
|
|
|
|
}
|
|
|
|
|
2020-04-17 21:36:51 +00:00
|
|
|
TEST_F(EnvTest, IsDirectory) {
|
|
|
|
Status s = Env::Default()->CreateDirIfMissing(test_directory_);
|
|
|
|
ASSERT_OK(s);
|
|
|
|
const std::string test_sub_dir = test_directory_ + "sub1";
|
|
|
|
const std::string test_file_path = test_directory_ + "file1";
|
|
|
|
ASSERT_OK(Env::Default()->CreateDirIfMissing(test_sub_dir));
|
|
|
|
bool is_dir = false;
|
|
|
|
ASSERT_OK(Env::Default()->IsDirectory(test_sub_dir, &is_dir));
|
|
|
|
ASSERT_TRUE(is_dir);
|
|
|
|
{
|
|
|
|
std::unique_ptr<FSWritableFile> wfile;
|
|
|
|
s = Env::Default()->GetFileSystem()->NewWritableFile(
|
|
|
|
test_file_path, FileOptions(), &wfile, /*dbg=*/nullptr);
|
|
|
|
ASSERT_OK(s);
|
|
|
|
std::unique_ptr<WritableFileWriter> fwriter;
|
|
|
|
fwriter.reset(new WritableFileWriter(std::move(wfile), test_file_path,
|
2021-01-26 06:07:26 +00:00
|
|
|
FileOptions(),
|
2021-03-15 11:32:24 +00:00
|
|
|
SystemClock::Default().get()));
|
2020-04-17 21:36:51 +00:00
|
|
|
constexpr char buf[] = "test";
|
|
|
|
s = fwriter->Append(buf);
|
|
|
|
ASSERT_OK(s);
|
|
|
|
}
|
|
|
|
ASSERT_OK(Env::Default()->IsDirectory(test_file_path, &is_dir));
|
|
|
|
ASSERT_FALSE(is_dir);
|
|
|
|
}
|
|
|
|
|
2021-03-19 18:42:27 +00:00
|
|
|
TEST_F(EnvTest, EnvWriteVerificationTest) {
|
|
|
|
Status s = Env::Default()->CreateDirIfMissing(test_directory_);
|
|
|
|
const std::string test_file_path = test_directory_ + "file1";
|
|
|
|
ASSERT_OK(s);
|
|
|
|
std::shared_ptr<FaultInjectionTestFS> fault_fs(
|
|
|
|
new FaultInjectionTestFS(FileSystem::Default()));
|
|
|
|
fault_fs->SetChecksumHandoffFuncType(ChecksumType::kCRC32c);
|
|
|
|
std::unique_ptr<Env> fault_fs_env(NewCompositeEnv(fault_fs));
|
|
|
|
std::unique_ptr<WritableFile> file;
|
|
|
|
s = fault_fs_env->NewWritableFile(test_file_path, &file, EnvOptions());
|
|
|
|
ASSERT_OK(s);
|
|
|
|
|
|
|
|
DataVerificationInfo v_info;
|
|
|
|
std::string test_data = "test";
|
|
|
|
std::string checksum;
|
|
|
|
uint32_t v_crc32c = crc32c::Extend(0, test_data.c_str(), test_data.size());
|
|
|
|
PutFixed32(&checksum, v_crc32c);
|
|
|
|
v_info.checksum = Slice(checksum);
|
|
|
|
s = file->Append(Slice(test_data), v_info);
|
|
|
|
ASSERT_OK(s);
|
|
|
|
}
|
|
|
|
|
2021-09-21 15:53:03 +00:00
|
|
|
class CreateEnvTest : public testing::Test {
|
2021-07-16 14:57:47 +00:00
|
|
|
public:
|
2021-09-21 15:53:03 +00:00
|
|
|
CreateEnvTest() {
|
|
|
|
config_options_.ignore_unknown_options = false;
|
|
|
|
config_options_.ignore_unsupported_options = false;
|
|
|
|
}
|
|
|
|
ConfigOptions config_options_;
|
2021-07-16 14:57:47 +00:00
|
|
|
};
|
|
|
|
|
2021-09-21 15:53:03 +00:00
|
|
|
TEST_F(CreateEnvTest, LoadCTRProvider) {
|
|
|
|
config_options_.invoke_prepare_options = false;
|
2021-07-16 14:57:47 +00:00
|
|
|
std::string CTR = CTREncryptionProvider::kClassName();
|
|
|
|
std::shared_ptr<EncryptionProvider> provider;
|
|
|
|
// Test a provider with no cipher
|
|
|
|
ASSERT_OK(
|
2021-09-21 15:53:03 +00:00
|
|
|
EncryptionProvider::CreateFromString(config_options_, CTR, &provider));
|
2021-07-16 14:57:47 +00:00
|
|
|
ASSERT_NE(provider, nullptr);
|
|
|
|
ASSERT_EQ(provider->Name(), CTR);
|
2021-09-21 15:53:03 +00:00
|
|
|
ASSERT_NOK(provider->PrepareOptions(config_options_));
|
2021-07-16 14:57:47 +00:00
|
|
|
ASSERT_NOK(provider->ValidateOptions(DBOptions(), ColumnFamilyOptions()));
|
|
|
|
auto cipher = provider->GetOptions<std::shared_ptr<BlockCipher>>("Cipher");
|
|
|
|
ASSERT_NE(cipher, nullptr);
|
|
|
|
ASSERT_EQ(cipher->get(), nullptr);
|
|
|
|
provider.reset();
|
|
|
|
|
2021-09-21 15:53:03 +00:00
|
|
|
ASSERT_OK(EncryptionProvider::CreateFromString(config_options_,
|
2021-07-16 14:57:47 +00:00
|
|
|
CTR + "://test", &provider));
|
|
|
|
ASSERT_NE(provider, nullptr);
|
|
|
|
ASSERT_EQ(provider->Name(), CTR);
|
2021-09-21 15:53:03 +00:00
|
|
|
ASSERT_OK(provider->PrepareOptions(config_options_));
|
2021-07-16 14:57:47 +00:00
|
|
|
ASSERT_OK(provider->ValidateOptions(DBOptions(), ColumnFamilyOptions()));
|
|
|
|
cipher = provider->GetOptions<std::shared_ptr<BlockCipher>>("Cipher");
|
|
|
|
ASSERT_NE(cipher, nullptr);
|
|
|
|
ASSERT_NE(cipher->get(), nullptr);
|
|
|
|
ASSERT_STREQ(cipher->get()->Name(), "ROT13");
|
|
|
|
provider.reset();
|
|
|
|
|
2021-09-21 15:53:03 +00:00
|
|
|
ASSERT_OK(EncryptionProvider::CreateFromString(config_options_, "1://test",
|
2021-07-16 14:57:47 +00:00
|
|
|
&provider));
|
|
|
|
ASSERT_NE(provider, nullptr);
|
|
|
|
ASSERT_EQ(provider->Name(), CTR);
|
2021-09-21 15:53:03 +00:00
|
|
|
ASSERT_OK(provider->PrepareOptions(config_options_));
|
2021-07-16 14:57:47 +00:00
|
|
|
ASSERT_OK(provider->ValidateOptions(DBOptions(), ColumnFamilyOptions()));
|
|
|
|
cipher = provider->GetOptions<std::shared_ptr<BlockCipher>>("Cipher");
|
|
|
|
ASSERT_NE(cipher, nullptr);
|
|
|
|
ASSERT_NE(cipher->get(), nullptr);
|
|
|
|
ASSERT_STREQ(cipher->get()->Name(), "ROT13");
|
|
|
|
provider.reset();
|
|
|
|
|
|
|
|
ASSERT_OK(EncryptionProvider::CreateFromString(
|
2021-09-21 15:53:03 +00:00
|
|
|
config_options_, "id=" + CTR + "; cipher=ROT13", &provider));
|
2021-07-16 14:57:47 +00:00
|
|
|
ASSERT_NE(provider, nullptr);
|
|
|
|
ASSERT_EQ(provider->Name(), CTR);
|
|
|
|
cipher = provider->GetOptions<std::shared_ptr<BlockCipher>>("Cipher");
|
|
|
|
ASSERT_NE(cipher, nullptr);
|
|
|
|
ASSERT_NE(cipher->get(), nullptr);
|
|
|
|
ASSERT_STREQ(cipher->get()->Name(), "ROT13");
|
|
|
|
provider.reset();
|
|
|
|
}
|
|
|
|
|
2021-09-21 15:53:03 +00:00
|
|
|
TEST_F(CreateEnvTest, LoadROT13Cipher) {
|
2021-07-16 14:57:47 +00:00
|
|
|
std::shared_ptr<BlockCipher> cipher;
|
|
|
|
// Test a provider with no cipher
|
2021-09-21 15:53:03 +00:00
|
|
|
ASSERT_OK(BlockCipher::CreateFromString(config_options_, "ROT13", &cipher));
|
2021-07-16 14:57:47 +00:00
|
|
|
ASSERT_NE(cipher, nullptr);
|
|
|
|
ASSERT_STREQ(cipher->Name(), "ROT13");
|
|
|
|
}
|
2021-09-21 15:53:03 +00:00
|
|
|
|
|
|
|
TEST_F(CreateEnvTest, CreateDefaultSystemClock) {
|
|
|
|
std::shared_ptr<SystemClock> clock, copy;
|
|
|
|
ASSERT_OK(SystemClock::CreateFromString(config_options_,
|
|
|
|
SystemClock::kDefaultName(), &clock));
|
|
|
|
ASSERT_NE(clock, nullptr);
|
|
|
|
ASSERT_EQ(clock, SystemClock::Default());
|
|
|
|
std::string opts_str = clock->ToString(config_options_);
|
|
|
|
std::string mismatch;
|
|
|
|
ASSERT_OK(SystemClock::CreateFromString(config_options_, opts_str, ©));
|
|
|
|
ASSERT_TRUE(clock->AreEquivalent(config_options_, copy.get(), &mismatch));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CreateEnvTest, CreateMockSystemClock) {
|
|
|
|
std::shared_ptr<SystemClock> mock, copy;
|
|
|
|
|
2022-01-11 14:32:42 +00:00
|
|
|
config_options_.registry->AddLibrary("test")->AddFactory<SystemClock>(
|
2021-09-21 15:53:03 +00:00
|
|
|
MockSystemClock::kClassName(),
|
|
|
|
[](const std::string& /*uri*/, std::unique_ptr<SystemClock>* guard,
|
|
|
|
std::string* /* errmsg */) {
|
|
|
|
guard->reset(new MockSystemClock(nullptr));
|
|
|
|
return guard->get();
|
|
|
|
});
|
|
|
|
ASSERT_OK(SystemClock::CreateFromString(
|
|
|
|
config_options_, EmulatedSystemClock::kClassName(), &mock));
|
|
|
|
ASSERT_NE(mock, nullptr);
|
|
|
|
ASSERT_STREQ(mock->Name(), EmulatedSystemClock::kClassName());
|
|
|
|
ASSERT_EQ(mock->Inner(), SystemClock::Default().get());
|
|
|
|
std::string opts_str = mock->ToString(config_options_);
|
|
|
|
std::string mismatch;
|
|
|
|
ASSERT_OK(SystemClock::CreateFromString(config_options_, opts_str, ©));
|
|
|
|
ASSERT_TRUE(mock->AreEquivalent(config_options_, copy.get(), &mismatch));
|
|
|
|
|
|
|
|
std::string id = std::string("id=") + EmulatedSystemClock::kClassName() +
|
|
|
|
";target=" + MockSystemClock::kClassName();
|
|
|
|
|
|
|
|
ASSERT_OK(SystemClock::CreateFromString(config_options_, id, &mock));
|
|
|
|
ASSERT_NE(mock, nullptr);
|
|
|
|
ASSERT_STREQ(mock->Name(), EmulatedSystemClock::kClassName());
|
|
|
|
ASSERT_NE(mock->Inner(), nullptr);
|
|
|
|
ASSERT_STREQ(mock->Inner()->Name(), MockSystemClock::kClassName());
|
|
|
|
ASSERT_EQ(mock->Inner()->Inner(), SystemClock::Default().get());
|
|
|
|
opts_str = mock->ToString(config_options_);
|
|
|
|
ASSERT_OK(SystemClock::CreateFromString(config_options_, opts_str, ©));
|
|
|
|
ASSERT_TRUE(mock->AreEquivalent(config_options_, copy.get(), &mismatch));
|
|
|
|
ASSERT_OK(SystemClock::CreateFromString(
|
|
|
|
config_options_, EmulatedSystemClock::kClassName(), &mock));
|
|
|
|
}
|
2021-11-02 16:06:02 +00:00
|
|
|
|
|
|
|
TEST_F(CreateEnvTest, CreateReadOnlyFileSystem) {
|
|
|
|
std::shared_ptr<FileSystem> fs, copy;
|
|
|
|
|
|
|
|
ASSERT_OK(FileSystem::CreateFromString(
|
|
|
|
config_options_, ReadOnlyFileSystem::kClassName(), &fs));
|
|
|
|
ASSERT_NE(fs, nullptr);
|
|
|
|
ASSERT_STREQ(fs->Name(), ReadOnlyFileSystem::kClassName());
|
|
|
|
ASSERT_EQ(fs->Inner(), FileSystem::Default().get());
|
|
|
|
|
|
|
|
std::string opts_str = fs->ToString(config_options_);
|
|
|
|
std::string mismatch;
|
|
|
|
|
|
|
|
ASSERT_OK(FileSystem::CreateFromString(config_options_, opts_str, ©));
|
|
|
|
ASSERT_TRUE(fs->AreEquivalent(config_options_, copy.get(), &mismatch));
|
|
|
|
|
|
|
|
ASSERT_OK(FileSystem::CreateFromString(
|
|
|
|
config_options_,
|
|
|
|
std::string("id=") + ReadOnlyFileSystem::kClassName() +
|
|
|
|
"; target=" + TimedFileSystem::kClassName(),
|
|
|
|
&fs));
|
|
|
|
ASSERT_NE(fs, nullptr);
|
|
|
|
opts_str = fs->ToString(config_options_);
|
|
|
|
ASSERT_STREQ(fs->Name(), ReadOnlyFileSystem::kClassName());
|
|
|
|
ASSERT_NE(fs->Inner(), nullptr);
|
|
|
|
ASSERT_STREQ(fs->Inner()->Name(), TimedFileSystem::kClassName());
|
|
|
|
ASSERT_EQ(fs->Inner()->Inner(), FileSystem::Default().get());
|
|
|
|
ASSERT_OK(FileSystem::CreateFromString(config_options_, opts_str, ©));
|
|
|
|
ASSERT_TRUE(fs->AreEquivalent(config_options_, copy.get(), &mismatch));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CreateEnvTest, CreateTimedFileSystem) {
|
|
|
|
std::shared_ptr<FileSystem> fs, copy;
|
|
|
|
|
|
|
|
ASSERT_OK(FileSystem::CreateFromString(config_options_,
|
|
|
|
TimedFileSystem::kClassName(), &fs));
|
|
|
|
ASSERT_NE(fs, nullptr);
|
|
|
|
ASSERT_STREQ(fs->Name(), TimedFileSystem::kClassName());
|
|
|
|
ASSERT_EQ(fs->Inner(), FileSystem::Default().get());
|
|
|
|
|
|
|
|
std::string opts_str = fs->ToString(config_options_);
|
|
|
|
std::string mismatch;
|
|
|
|
|
|
|
|
ASSERT_OK(FileSystem::CreateFromString(config_options_, opts_str, ©));
|
|
|
|
ASSERT_TRUE(fs->AreEquivalent(config_options_, copy.get(), &mismatch));
|
|
|
|
|
|
|
|
ASSERT_OK(FileSystem::CreateFromString(
|
|
|
|
config_options_,
|
|
|
|
std::string("id=") + TimedFileSystem::kClassName() +
|
|
|
|
"; target=" + ReadOnlyFileSystem::kClassName(),
|
|
|
|
&fs));
|
|
|
|
ASSERT_NE(fs, nullptr);
|
|
|
|
opts_str = fs->ToString(config_options_);
|
|
|
|
ASSERT_STREQ(fs->Name(), TimedFileSystem::kClassName());
|
|
|
|
ASSERT_NE(fs->Inner(), nullptr);
|
|
|
|
ASSERT_STREQ(fs->Inner()->Name(), ReadOnlyFileSystem::kClassName());
|
|
|
|
ASSERT_EQ(fs->Inner()->Inner(), FileSystem::Default().get());
|
|
|
|
ASSERT_OK(FileSystem::CreateFromString(config_options_, opts_str, ©));
|
|
|
|
ASSERT_TRUE(fs->AreEquivalent(config_options_, copy.get(), &mismatch));
|
|
|
|
}
|
2022-02-03 23:00:03 +00:00
|
|
|
|
|
|
|
TEST_F(CreateEnvTest, CreateCountedFileSystem) {
|
|
|
|
std::shared_ptr<FileSystem> fs, copy;
|
|
|
|
|
|
|
|
ASSERT_OK(FileSystem::CreateFromString(config_options_,
|
|
|
|
CountedFileSystem::kClassName(), &fs));
|
|
|
|
ASSERT_NE(fs, nullptr);
|
|
|
|
ASSERT_STREQ(fs->Name(), CountedFileSystem::kClassName());
|
|
|
|
ASSERT_EQ(fs->Inner(), FileSystem::Default().get());
|
|
|
|
|
|
|
|
std::string opts_str = fs->ToString(config_options_);
|
|
|
|
std::string mismatch;
|
|
|
|
|
|
|
|
ASSERT_OK(FileSystem::CreateFromString(config_options_, opts_str, ©));
|
|
|
|
ASSERT_TRUE(fs->AreEquivalent(config_options_, copy.get(), &mismatch));
|
|
|
|
|
|
|
|
ASSERT_OK(FileSystem::CreateFromString(
|
|
|
|
config_options_,
|
|
|
|
std::string("id=") + CountedFileSystem::kClassName() +
|
|
|
|
"; target=" + ReadOnlyFileSystem::kClassName(),
|
|
|
|
&fs));
|
|
|
|
ASSERT_NE(fs, nullptr);
|
|
|
|
opts_str = fs->ToString(config_options_);
|
|
|
|
ASSERT_STREQ(fs->Name(), CountedFileSystem::kClassName());
|
|
|
|
ASSERT_NE(fs->Inner(), nullptr);
|
|
|
|
ASSERT_STREQ(fs->Inner()->Name(), ReadOnlyFileSystem::kClassName());
|
|
|
|
ASSERT_EQ(fs->Inner()->Inner(), FileSystem::Default().get());
|
|
|
|
ASSERT_OK(FileSystem::CreateFromString(config_options_, opts_str, ©));
|
|
|
|
ASSERT_TRUE(fs->AreEquivalent(config_options_, copy.get(), &mismatch));
|
|
|
|
}
|
|
|
|
|
2021-11-02 16:06:02 +00:00
|
|
|
#ifndef OS_WIN
|
|
|
|
TEST_F(CreateEnvTest, CreateChrootFileSystem) {
|
|
|
|
std::shared_ptr<FileSystem> fs, copy;
|
|
|
|
auto tmp_dir = test::TmpDir(Env::Default());
|
|
|
|
// The Chroot FileSystem has a required "chroot_dir" option.
|
|
|
|
ASSERT_NOK(FileSystem::CreateFromString(config_options_,
|
|
|
|
ChrootFileSystem::kClassName(), &fs));
|
|
|
|
|
|
|
|
// ChrootFileSystem fails with an invalid directory
|
|
|
|
ASSERT_NOK(FileSystem::CreateFromString(
|
|
|
|
config_options_,
|
|
|
|
std::string("chroot_dir=/No/Such/Directory; id=") +
|
|
|
|
ChrootFileSystem::kClassName(),
|
|
|
|
&fs));
|
|
|
|
std::string chroot_opts = std::string("chroot_dir=") + tmp_dir +
|
|
|
|
std::string("; id=") +
|
|
|
|
ChrootFileSystem::kClassName();
|
|
|
|
|
|
|
|
// Create a valid ChrootFileSystem with an inner Default
|
|
|
|
ASSERT_OK(FileSystem::CreateFromString(config_options_, chroot_opts, &fs));
|
|
|
|
ASSERT_NE(fs, nullptr);
|
|
|
|
ASSERT_STREQ(fs->Name(), ChrootFileSystem::kClassName());
|
|
|
|
ASSERT_EQ(fs->Inner(), FileSystem::Default().get());
|
|
|
|
std::string opts_str = fs->ToString(config_options_);
|
|
|
|
std::string mismatch;
|
|
|
|
ASSERT_OK(FileSystem::CreateFromString(config_options_, opts_str, ©));
|
|
|
|
ASSERT_TRUE(fs->AreEquivalent(config_options_, copy.get(), &mismatch));
|
|
|
|
|
|
|
|
// Create a valid ChrootFileSystem with an inner TimedFileSystem
|
|
|
|
ASSERT_OK(FileSystem::CreateFromString(
|
|
|
|
config_options_,
|
|
|
|
chroot_opts + "; target=" + TimedFileSystem::kClassName(), &fs));
|
|
|
|
ASSERT_NE(fs, nullptr);
|
|
|
|
ASSERT_STREQ(fs->Name(), ChrootFileSystem::kClassName());
|
|
|
|
ASSERT_NE(fs->Inner(), nullptr);
|
|
|
|
ASSERT_STREQ(fs->Inner()->Name(), TimedFileSystem::kClassName());
|
|
|
|
ASSERT_EQ(fs->Inner()->Inner(), FileSystem::Default().get());
|
|
|
|
opts_str = fs->ToString(config_options_);
|
|
|
|
ASSERT_OK(FileSystem::CreateFromString(config_options_, opts_str, ©));
|
|
|
|
ASSERT_TRUE(fs->AreEquivalent(config_options_, copy.get(), &mismatch));
|
|
|
|
|
|
|
|
// Create a TimedFileSystem with an inner ChrootFileSystem
|
|
|
|
ASSERT_OK(FileSystem::CreateFromString(
|
|
|
|
config_options_,
|
|
|
|
"target={" + chroot_opts + "}; id=" + TimedFileSystem::kClassName(),
|
|
|
|
&fs));
|
|
|
|
ASSERT_NE(fs, nullptr);
|
|
|
|
ASSERT_STREQ(fs->Name(), TimedFileSystem::kClassName());
|
|
|
|
ASSERT_NE(fs->Inner(), nullptr);
|
|
|
|
ASSERT_STREQ(fs->Inner()->Name(), ChrootFileSystem::kClassName());
|
|
|
|
ASSERT_EQ(fs->Inner()->Inner(), FileSystem::Default().get());
|
|
|
|
opts_str = fs->ToString(config_options_);
|
|
|
|
ASSERT_OK(FileSystem::CreateFromString(config_options_, opts_str, ©));
|
|
|
|
ASSERT_TRUE(fs->AreEquivalent(config_options_, copy.get(), &mismatch));
|
|
|
|
}
|
|
|
|
#endif // OS_WIN
|
|
|
|
|
|
|
|
TEST_F(CreateEnvTest, CreateEncryptedFileSystem) {
|
|
|
|
std::shared_ptr<FileSystem> fs, copy;
|
|
|
|
|
|
|
|
std::string base_opts =
|
|
|
|
std::string("provider=1://test; id=") + EncryptedFileSystem::kClassName();
|
|
|
|
// The EncryptedFileSystem requires a "provider" option.
|
|
|
|
ASSERT_NOK(FileSystem::CreateFromString(
|
|
|
|
config_options_, EncryptedFileSystem::kClassName(), &fs));
|
|
|
|
|
|
|
|
ASSERT_OK(FileSystem::CreateFromString(config_options_, base_opts, &fs));
|
|
|
|
|
|
|
|
ASSERT_NE(fs, nullptr);
|
|
|
|
ASSERT_STREQ(fs->Name(), EncryptedFileSystem::kClassName());
|
|
|
|
ASSERT_EQ(fs->Inner(), FileSystem::Default().get());
|
|
|
|
std::string opts_str = fs->ToString(config_options_);
|
|
|
|
std::string mismatch;
|
|
|
|
ASSERT_OK(FileSystem::CreateFromString(config_options_, opts_str, ©));
|
|
|
|
ASSERT_TRUE(fs->AreEquivalent(config_options_, copy.get(), &mismatch));
|
|
|
|
ASSERT_OK(FileSystem::CreateFromString(
|
|
|
|
config_options_, base_opts + "; target=" + TimedFileSystem::kClassName(),
|
|
|
|
&fs));
|
|
|
|
ASSERT_NE(fs, nullptr);
|
|
|
|
ASSERT_STREQ(fs->Name(), EncryptedFileSystem::kClassName());
|
|
|
|
ASSERT_NE(fs->Inner(), nullptr);
|
|
|
|
ASSERT_STREQ(fs->Inner()->Name(), TimedFileSystem::kClassName());
|
|
|
|
ASSERT_EQ(fs->Inner()->Inner(), FileSystem::Default().get());
|
|
|
|
opts_str = fs->ToString(config_options_);
|
|
|
|
ASSERT_OK(FileSystem::CreateFromString(config_options_, opts_str, ©));
|
|
|
|
ASSERT_TRUE(fs->AreEquivalent(config_options_, copy.get(), &mismatch));
|
|
|
|
}
|
|
|
|
|
2021-07-16 14:57:47 +00:00
|
|
|
|
Built-in support for generating unique IDs, bug fix (#8708)
Summary:
Env::GenerateUniqueId() works fine on Windows and on POSIX
where /proc/sys/kernel/random/uuid exists. Our other implementation is
flawed and easily produces collision in a new multi-threaded test.
As we rely more heavily on DB session ID uniqueness, this becomes a
serious issue.
This change combines several individually suitable entropy sources
for reliable generation of random unique IDs, with goal of uniqueness
and portability, not cryptographic strength nor maximum speed.
Specifically:
* Moves code for getting UUIDs from the OS to port::GenerateRfcUuid
rather than in Env implementation details. Callers are now told whether
the operation fails or succeeds.
* Adds an internal API GenerateRawUniqueId for generating high-quality
128-bit unique identifiers, by combining entropy from three "tracks":
* Lots of info from default Env like time, process id, and hostname.
* std::random_device
* port::GenerateRfcUuid (when working)
* Built-in implementations of Env::GenerateUniqueId() will now always
produce an RFC 4122 UUID string, either from platform-specific API or
by converting the output of GenerateRawUniqueId.
DB session IDs now use GenerateRawUniqueId while DB IDs (not as
critical) try to use port::GenerateRfcUuid but fall back on
GenerateRawUniqueId with conversion to an RFC 4122 UUID.
GenerateRawUniqueId is declared and defined under env/ rather than util/
or even port/ because of the Env dependency.
Likely follow-up: enhance GenerateRawUniqueId to be faster after the
first call and to guarantee uniqueness within the lifetime of a single
process (imparting the same property onto DB session IDs).
Pull Request resolved: https://github.com/facebook/rocksdb/pull/8708
Test Plan:
A new mini-stress test in env_test checks the various public
and internal APIs for uniqueness, including each track of
GenerateRawUniqueId individually. We can't hope to verify anywhere close
to 128 bits of entropy, but it can at least detect flaws as bad as the
old code. Serial execution of the new tests takes about 350 ms on
my machine.
Reviewed By: zhichao-cao, mrambacher
Differential Revision: D30563780
Pulled By: pdillinger
fbshipit-source-id: de4c9ff4b2f581cf784fcedb5f39f16e5185c364
2021-08-30 22:19:39 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr size_t kThreads = 8;
|
|
|
|
constexpr size_t kIdsPerThread = 1000;
|
|
|
|
|
|
|
|
// This is a mini-stress test to check for duplicates in functions like
|
|
|
|
// GenerateUniqueId()
|
|
|
|
template <typename IdType, class Hash = std::hash<IdType>>
|
|
|
|
struct NoDuplicateMiniStressTest {
|
|
|
|
std::unordered_set<IdType, Hash> ids;
|
|
|
|
std::mutex mutex;
|
|
|
|
Env* env;
|
|
|
|
|
|
|
|
NoDuplicateMiniStressTest() { env = Env::Default(); }
|
|
|
|
|
|
|
|
virtual ~NoDuplicateMiniStressTest() {}
|
|
|
|
|
|
|
|
void Run() {
|
|
|
|
std::array<std::thread, kThreads> threads;
|
|
|
|
for (size_t i = 0; i < kThreads; ++i) {
|
|
|
|
threads[i] = std::thread([&]() { ThreadFn(); });
|
|
|
|
}
|
|
|
|
for (auto& thread : threads) {
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
// All must be unique
|
|
|
|
ASSERT_EQ(ids.size(), kThreads * kIdsPerThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadFn() {
|
|
|
|
std::array<IdType, kIdsPerThread> my_ids;
|
|
|
|
// Generate in parallel threads as fast as possible
|
|
|
|
for (size_t i = 0; i < kIdsPerThread; ++i) {
|
|
|
|
my_ids[i] = Generate();
|
|
|
|
}
|
|
|
|
// Now collate
|
|
|
|
std::lock_guard<std::mutex> lock(mutex);
|
|
|
|
for (auto& id : my_ids) {
|
|
|
|
ids.insert(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual IdType Generate() = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
void VerifyRfcUuids(const std::unordered_set<std::string>& uuids) {
|
|
|
|
if (uuids.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
using uint64_pair_t = std::pair<uint64_t, uint64_t>;
|
|
|
|
struct HashUint64Pair {
|
|
|
|
std::size_t operator()(
|
|
|
|
std::pair<uint64_t, uint64_t> const& u) const noexcept {
|
|
|
|
// Assume suitable distribution already
|
|
|
|
return static_cast<size_t>(u.first ^ u.second);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST_F(EnvTest, GenerateUniqueId) {
|
|
|
|
struct MyStressTest : public NoDuplicateMiniStressTest<std::string> {
|
|
|
|
std::string Generate() override { return env->GenerateUniqueId(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
MyStressTest t;
|
|
|
|
t.Run();
|
|
|
|
|
|
|
|
// Basically verify RFC-4122 format
|
|
|
|
for (auto& uuid : t.ids) {
|
|
|
|
ASSERT_EQ(36U, uuid.size());
|
|
|
|
ASSERT_EQ('-', uuid[8]);
|
|
|
|
ASSERT_EQ('-', uuid[13]);
|
|
|
|
ASSERT_EQ('-', uuid[18]);
|
|
|
|
ASSERT_EQ('-', uuid[23]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EnvTest, GenerateDbSessionId) {
|
|
|
|
struct MyStressTest : public NoDuplicateMiniStressTest<std::string> {
|
|
|
|
std::string Generate() override { return DBImpl::GenerateDbSessionId(env); }
|
|
|
|
};
|
|
|
|
|
|
|
|
MyStressTest t;
|
|
|
|
t.Run();
|
|
|
|
|
|
|
|
// Basically verify session ID
|
|
|
|
for (auto& id : t.ids) {
|
|
|
|
ASSERT_EQ(20U, id.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool kRequirePortGenerateRfcUuid =
|
|
|
|
#if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_WIN)
|
|
|
|
true;
|
|
|
|
#else
|
|
|
|
false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TEST_F(EnvTest, PortGenerateRfcUuid) {
|
|
|
|
if (!kRequirePortGenerateRfcUuid) {
|
|
|
|
ROCKSDB_GTEST_SKIP("Not supported/expected on this platform");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
struct MyStressTest : public NoDuplicateMiniStressTest<std::string> {
|
|
|
|
std::string Generate() override {
|
|
|
|
std::string u;
|
|
|
|
assert(port::GenerateRfcUuid(&u));
|
|
|
|
return u;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
MyStressTest t;
|
|
|
|
t.Run();
|
|
|
|
|
|
|
|
// Extra verification on versions and variants
|
|
|
|
VerifyRfcUuids(t.ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test the atomic, linear generation of GenerateRawUuid
|
|
|
|
TEST_F(EnvTest, GenerateRawUniqueId) {
|
|
|
|
struct MyStressTest
|
|
|
|
: public NoDuplicateMiniStressTest<uint64_pair_t, HashUint64Pair> {
|
|
|
|
uint64_pair_t Generate() override {
|
|
|
|
uint64_pair_t p;
|
|
|
|
GenerateRawUniqueId(&p.first, &p.second);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
MyStressTest t;
|
|
|
|
t.Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that each entropy source ("track") is at least adequate
|
|
|
|
TEST_F(EnvTest, GenerateRawUniqueIdTrackPortUuidOnly) {
|
|
|
|
if (!kRequirePortGenerateRfcUuid) {
|
|
|
|
ROCKSDB_GTEST_SKIP("Not supported/expected on this platform");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MyStressTest
|
|
|
|
: public NoDuplicateMiniStressTest<uint64_pair_t, HashUint64Pair> {
|
|
|
|
uint64_pair_t Generate() override {
|
|
|
|
uint64_pair_t p;
|
|
|
|
TEST_GenerateRawUniqueId(&p.first, &p.second, false, true, true);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
MyStressTest t;
|
|
|
|
t.Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EnvTest, GenerateRawUniqueIdTrackEnvDetailsOnly) {
|
|
|
|
struct MyStressTest
|
|
|
|
: public NoDuplicateMiniStressTest<uint64_pair_t, HashUint64Pair> {
|
|
|
|
uint64_pair_t Generate() override {
|
|
|
|
uint64_pair_t p;
|
|
|
|
TEST_GenerateRawUniqueId(&p.first, &p.second, true, false, true);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
MyStressTest t;
|
|
|
|
t.Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EnvTest, GenerateRawUniqueIdTrackRandomDeviceOnly) {
|
|
|
|
struct MyStressTest
|
|
|
|
: public NoDuplicateMiniStressTest<uint64_pair_t, HashUint64Pair> {
|
|
|
|
uint64_pair_t Generate() override {
|
|
|
|
uint64_pair_t p;
|
|
|
|
TEST_GenerateRawUniqueId(&p.first, &p.second, true, true, false);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
MyStressTest t;
|
|
|
|
t.Run();
|
|
|
|
}
|
|
|
|
|
Experimental support for SST unique IDs (#8990)
Summary:
* New public header unique_id.h and function GetUniqueIdFromTableProperties
which computes a universally unique identifier based on table properties
of table files from recent RocksDB versions.
* Generation of DB session IDs is refactored so that they are
guaranteed unique in the lifetime of a process running RocksDB.
(SemiStructuredUniqueIdGen, new test included.) Along with file numbers,
this enables SST unique IDs to be guaranteed unique among SSTs generated
in a single process, and "better than random" between processes.
See https://github.com/pdillinger/unique_id
* In addition to public API producing 'external' unique IDs, there is a function
for producing 'internal' unique IDs, with functions for converting between the
two. In short, the external ID is "safe" for things people might do with it, and
the internal ID enables more "power user" features for the future. Specifically,
the external ID goes through a hashing layer so that any subset of bits in the
external ID can be used as a hash of the full ID, while also preserving
uniqueness guarantees in the first 128 bits (bijective both on first 128 bits
and on full 192 bits).
Intended follow-up:
* Use the internal unique IDs in cache keys. (Avoid conflicts with https://github.com/facebook/rocksdb/issues/8912) (The file offset can be XORed into
the third 64-bit value of the unique ID.)
* Publish the external unique IDs in FileStorageInfo (https://github.com/facebook/rocksdb/issues/8968)
Pull Request resolved: https://github.com/facebook/rocksdb/pull/8990
Test Plan:
Unit tests added, and checking of unique ids in stress test.
NOTE in stress test we do not generate nearly enough files to thoroughly
stress uniqueness, but the test trims off pieces of the ID to check for
uniqueness so that we can infer (with some assumptions) stronger
properties in the aggregate.
Reviewed By: zhichao-cao, mrambacher
Differential Revision: D31582865
Pulled By: pdillinger
fbshipit-source-id: 1f620c4c86af9abe2a8d177b9ccf2ad2b9f48243
2021-10-19 06:28:28 +00:00
|
|
|
TEST_F(EnvTest, SemiStructuredUniqueIdGenTest) {
|
|
|
|
// Must be thread safe and usable as a static
|
|
|
|
static SemiStructuredUniqueIdGen gen;
|
|
|
|
|
|
|
|
struct MyStressTest
|
|
|
|
: public NoDuplicateMiniStressTest<uint64_pair_t, HashUint64Pair> {
|
|
|
|
uint64_pair_t Generate() override {
|
|
|
|
uint64_pair_t p;
|
|
|
|
gen.GenerateNext(&p.first, &p.second);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
MyStressTest t;
|
|
|
|
t.Run();
|
|
|
|
}
|
|
|
|
|
2023-05-10 05:24:26 +00:00
|
|
|
TEST_F(EnvTest, SemiStructuredUniqueIdGenTestSmaller) {
|
|
|
|
// For small generated types, will cycle through all the possible values.
|
|
|
|
SemiStructuredUniqueIdGen gen;
|
|
|
|
std::vector<bool> hit(256);
|
|
|
|
for (int i = 0; i < 256; ++i) {
|
|
|
|
auto val = gen.GenerateNext<uint8_t>();
|
|
|
|
ASSERT_FALSE(hit[val]);
|
|
|
|
hit[val] = true;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 256; ++i) {
|
|
|
|
ASSERT_TRUE(hit[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 05:40:37 +00:00
|
|
|
TEST_F(EnvTest, FailureToCreateLockFile) {
|
|
|
|
auto env = Env::Default();
|
|
|
|
auto fs = env->GetFileSystem();
|
|
|
|
std::string dir = test::PerThreadDBPath(env, "lockdir");
|
|
|
|
std::string file = dir + "/lockfile";
|
|
|
|
|
|
|
|
// Ensure directory doesn't exist
|
|
|
|
ASSERT_OK(DestroyDir(env, dir));
|
|
|
|
|
|
|
|
// Make sure that we can acquire a file lock after the first attempt fails
|
|
|
|
FileLock* lock = nullptr;
|
|
|
|
ASSERT_NOK(fs->LockFile(file, IOOptions(), &lock, /*dbg*/ nullptr));
|
|
|
|
ASSERT_FALSE(lock);
|
|
|
|
|
|
|
|
ASSERT_OK(fs->CreateDir(dir, IOOptions(), /*dbg*/ nullptr));
|
|
|
|
ASSERT_OK(fs->LockFile(file, IOOptions(), &lock, /*dbg*/ nullptr));
|
|
|
|
ASSERT_OK(fs->UnlockFile(lock, IOOptions(), /*dbg*/ nullptr));
|
|
|
|
|
|
|
|
// Clean up
|
|
|
|
ASSERT_OK(DestroyDir(env, dir));
|
|
|
|
}
|
2022-01-05 00:44:54 +00:00
|
|
|
|
2022-05-13 11:57:08 +00:00
|
|
|
TEST_F(CreateEnvTest, CreateDefaultEnv) {
|
2022-01-05 00:44:54 +00:00
|
|
|
ConfigOptions options;
|
|
|
|
options.ignore_unsupported_options = false;
|
|
|
|
|
|
|
|
std::shared_ptr<Env> guard;
|
|
|
|
Env* env = nullptr;
|
|
|
|
ASSERT_OK(Env::CreateFromString(options, "", &env));
|
|
|
|
ASSERT_EQ(env, Env::Default());
|
|
|
|
|
|
|
|
env = nullptr;
|
|
|
|
ASSERT_OK(Env::CreateFromString(options, Env::kDefaultName(), &env));
|
|
|
|
ASSERT_EQ(env, Env::Default());
|
|
|
|
|
|
|
|
env = nullptr;
|
|
|
|
ASSERT_OK(Env::CreateFromString(options, "", &env, &guard));
|
|
|
|
ASSERT_EQ(env, Env::Default());
|
|
|
|
ASSERT_EQ(guard, nullptr);
|
|
|
|
|
|
|
|
env = nullptr;
|
|
|
|
ASSERT_OK(Env::CreateFromString(options, Env::kDefaultName(), &env, &guard));
|
|
|
|
ASSERT_EQ(env, Env::Default());
|
|
|
|
ASSERT_EQ(guard, nullptr);
|
|
|
|
|
|
|
|
std::string opt_str = env->ToString(options);
|
|
|
|
ASSERT_OK(Env::CreateFromString(options, opt_str, &env));
|
|
|
|
ASSERT_EQ(env, Env::Default());
|
|
|
|
ASSERT_OK(Env::CreateFromString(options, opt_str, &env, &guard));
|
|
|
|
ASSERT_EQ(env, Env::Default());
|
|
|
|
ASSERT_EQ(guard, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class WrappedEnv : public EnvWrapper {
|
|
|
|
public:
|
|
|
|
explicit WrappedEnv(Env* t) : EnvWrapper(t) {}
|
|
|
|
explicit WrappedEnv(const std::shared_ptr<Env>& t) : EnvWrapper(t) {}
|
|
|
|
static const char* kClassName() { return "WrappedEnv"; }
|
|
|
|
const char* Name() const override { return kClassName(); }
|
|
|
|
static void Register(ObjectLibrary& lib, const std::string& /*arg*/) {
|
2022-01-11 14:32:42 +00:00
|
|
|
lib.AddFactory<Env>(
|
|
|
|
WrappedEnv::kClassName(),
|
|
|
|
[](const std::string& /*uri*/, std::unique_ptr<Env>* guard,
|
|
|
|
std::string* /* errmsg */) {
|
|
|
|
guard->reset(new WrappedEnv(nullptr));
|
|
|
|
return guard->get();
|
|
|
|
});
|
2022-01-05 00:44:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
2022-05-13 11:57:08 +00:00
|
|
|
TEST_F(CreateEnvTest, CreateMockEnv) {
|
2022-01-05 00:44:54 +00:00
|
|
|
ConfigOptions options;
|
|
|
|
options.ignore_unsupported_options = false;
|
|
|
|
WrappedEnv::Register(*(options.registry->AddLibrary("test")), "");
|
|
|
|
std::shared_ptr<Env> guard, copy;
|
|
|
|
std::string opt_str;
|
|
|
|
|
|
|
|
Env* env = nullptr;
|
|
|
|
ASSERT_NOK(Env::CreateFromString(options, MockEnv::kClassName(), &env));
|
|
|
|
ASSERT_OK(
|
|
|
|
Env::CreateFromString(options, MockEnv::kClassName(), &env, &guard));
|
|
|
|
ASSERT_NE(env, nullptr);
|
|
|
|
ASSERT_NE(env, Env::Default());
|
|
|
|
opt_str = env->ToString(options);
|
|
|
|
ASSERT_OK(Env::CreateFromString(options, opt_str, &env, ©));
|
|
|
|
ASSERT_NE(copy, guard);
|
|
|
|
std::string mismatch;
|
|
|
|
ASSERT_TRUE(guard->AreEquivalent(options, copy.get(), &mismatch));
|
|
|
|
guard.reset(MockEnv::Create(Env::Default(), SystemClock::Default()));
|
|
|
|
opt_str = guard->ToString(options);
|
|
|
|
ASSERT_OK(Env::CreateFromString(options, opt_str, &env, ©));
|
|
|
|
std::unique_ptr<Env> wrapped_env(new WrappedEnv(Env::Default()));
|
|
|
|
guard.reset(MockEnv::Create(wrapped_env.get(), SystemClock::Default()));
|
|
|
|
opt_str = guard->ToString(options);
|
|
|
|
ASSERT_OK(Env::CreateFromString(options, opt_str, &env, ©));
|
|
|
|
opt_str = copy->ToString(options);
|
|
|
|
}
|
|
|
|
|
2022-05-13 11:57:08 +00:00
|
|
|
TEST_F(CreateEnvTest, CreateWrappedEnv) {
|
2022-01-05 00:44:54 +00:00
|
|
|
ConfigOptions options;
|
|
|
|
options.ignore_unsupported_options = false;
|
|
|
|
WrappedEnv::Register(*(options.registry->AddLibrary("test")), "");
|
|
|
|
Env* env = nullptr;
|
|
|
|
std::shared_ptr<Env> guard, copy;
|
|
|
|
std::string opt_str;
|
|
|
|
std::string mismatch;
|
|
|
|
|
|
|
|
ASSERT_NOK(Env::CreateFromString(options, WrappedEnv::kClassName(), &env));
|
|
|
|
ASSERT_OK(
|
|
|
|
Env::CreateFromString(options, WrappedEnv::kClassName(), &env, &guard));
|
|
|
|
ASSERT_NE(env, nullptr);
|
|
|
|
ASSERT_NE(env, Env::Default());
|
|
|
|
ASSERT_FALSE(guard->AreEquivalent(options, Env::Default(), &mismatch));
|
|
|
|
|
|
|
|
opt_str = env->ToString(options);
|
|
|
|
ASSERT_OK(Env::CreateFromString(options, opt_str, &env, ©));
|
|
|
|
ASSERT_NE(copy, guard);
|
|
|
|
ASSERT_TRUE(guard->AreEquivalent(options, copy.get(), &mismatch));
|
|
|
|
|
|
|
|
guard.reset(new WrappedEnv(std::make_shared<WrappedEnv>(Env::Default())));
|
|
|
|
ASSERT_NE(guard.get(), env);
|
|
|
|
opt_str = guard->ToString(options);
|
|
|
|
ASSERT_OK(Env::CreateFromString(options, opt_str, &env, ©));
|
|
|
|
ASSERT_NE(copy, guard);
|
|
|
|
ASSERT_TRUE(guard->AreEquivalent(options, copy.get(), &mismatch));
|
|
|
|
|
|
|
|
guard.reset(new WrappedEnv(std::make_shared<WrappedEnv>(
|
|
|
|
std::make_shared<WrappedEnv>(Env::Default()))));
|
|
|
|
ASSERT_NE(guard.get(), env);
|
|
|
|
opt_str = guard->ToString(options);
|
|
|
|
ASSERT_OK(Env::CreateFromString(options, opt_str, &env, ©));
|
|
|
|
ASSERT_NE(copy, guard);
|
|
|
|
ASSERT_TRUE(guard->AreEquivalent(options, copy.get(), &mismatch));
|
|
|
|
}
|
|
|
|
|
2022-05-13 11:57:08 +00:00
|
|
|
TEST_F(CreateEnvTest, CreateCompositeEnv) {
|
2022-01-05 00:44:54 +00:00
|
|
|
ConfigOptions options;
|
|
|
|
options.ignore_unsupported_options = false;
|
|
|
|
std::shared_ptr<Env> guard, copy;
|
|
|
|
Env* env = nullptr;
|
|
|
|
std::string mismatch, opt_str;
|
|
|
|
|
|
|
|
WrappedEnv::Register(*(options.registry->AddLibrary("test")), "");
|
|
|
|
std::unique_ptr<Env> base(NewCompositeEnv(FileSystem::Default()));
|
|
|
|
std::unique_ptr<Env> wrapped(new WrappedEnv(Env::Default()));
|
|
|
|
std::shared_ptr<FileSystem> timed_fs =
|
|
|
|
std::make_shared<TimedFileSystem>(FileSystem::Default());
|
|
|
|
std::shared_ptr<SystemClock> clock =
|
|
|
|
std::make_shared<EmulatedSystemClock>(SystemClock::Default());
|
|
|
|
|
|
|
|
opt_str = base->ToString(options);
|
|
|
|
ASSERT_NOK(Env::CreateFromString(options, opt_str, &env));
|
|
|
|
ASSERT_OK(Env::CreateFromString(options, opt_str, &env, &guard));
|
|
|
|
ASSERT_NE(env, nullptr);
|
|
|
|
ASSERT_NE(env, Env::Default());
|
|
|
|
ASSERT_EQ(env->GetFileSystem(), FileSystem::Default());
|
|
|
|
ASSERT_EQ(env->GetSystemClock(), SystemClock::Default());
|
|
|
|
|
|
|
|
base = NewCompositeEnv(timed_fs);
|
|
|
|
opt_str = base->ToString(options);
|
|
|
|
ASSERT_NOK(Env::CreateFromString(options, opt_str, &env));
|
|
|
|
ASSERT_OK(Env::CreateFromString(options, opt_str, &env, &guard));
|
|
|
|
ASSERT_NE(env, nullptr);
|
|
|
|
ASSERT_NE(env, Env::Default());
|
|
|
|
ASSERT_NE(env->GetFileSystem(), FileSystem::Default());
|
|
|
|
ASSERT_EQ(env->GetSystemClock(), SystemClock::Default());
|
|
|
|
|
|
|
|
env = nullptr;
|
|
|
|
guard.reset(new CompositeEnvWrapper(wrapped.get(), timed_fs));
|
|
|
|
opt_str = guard->ToString(options);
|
|
|
|
ASSERT_OK(Env::CreateFromString(options, opt_str, &env, ©));
|
|
|
|
ASSERT_NE(env, nullptr);
|
|
|
|
ASSERT_NE(env, Env::Default());
|
|
|
|
ASSERT_TRUE(guard->AreEquivalent(options, copy.get(), &mismatch));
|
|
|
|
|
|
|
|
env = nullptr;
|
|
|
|
guard.reset(new CompositeEnvWrapper(wrapped.get(), clock));
|
|
|
|
opt_str = guard->ToString(options);
|
|
|
|
ASSERT_OK(Env::CreateFromString(options, opt_str, &env, ©));
|
|
|
|
ASSERT_NE(env, nullptr);
|
|
|
|
ASSERT_NE(env, Env::Default());
|
|
|
|
ASSERT_TRUE(guard->AreEquivalent(options, copy.get(), &mismatch));
|
|
|
|
|
|
|
|
env = nullptr;
|
|
|
|
guard.reset(new CompositeEnvWrapper(wrapped.get(), timed_fs, clock));
|
|
|
|
opt_str = guard->ToString(options);
|
|
|
|
ASSERT_OK(Env::CreateFromString(options, opt_str, &env, ©));
|
|
|
|
ASSERT_NE(env, nullptr);
|
|
|
|
ASSERT_NE(env, Env::Default());
|
|
|
|
ASSERT_TRUE(guard->AreEquivalent(options, copy.get(), &mismatch));
|
2022-05-13 11:57:08 +00:00
|
|
|
|
|
|
|
guard.reset(new CompositeEnvWrapper(nullptr, timed_fs, clock));
|
|
|
|
ColumnFamilyOptions cf_opts;
|
|
|
|
DBOptions db_opts;
|
|
|
|
db_opts.env = guard.get();
|
|
|
|
auto comp = db_opts.env->CheckedCast<CompositeEnvWrapper>();
|
|
|
|
ASSERT_NE(comp, nullptr);
|
|
|
|
ASSERT_EQ(comp->Inner(), nullptr);
|
|
|
|
ASSERT_NOK(ValidateOptions(db_opts, cf_opts));
|
|
|
|
ASSERT_OK(db_opts.env->PrepareOptions(options));
|
|
|
|
ASSERT_NE(comp->Inner(), nullptr);
|
|
|
|
ASSERT_OK(ValidateOptions(db_opts, cf_opts));
|
2022-01-05 00:44:54 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 01:11:42 +00:00
|
|
|
// Forward declaration
|
|
|
|
class ReadAsyncFS;
|
|
|
|
|
|
|
|
struct MockIOHandle {
|
|
|
|
std::function<void(const FSReadRequest&, void*)> cb;
|
|
|
|
void* cb_arg;
|
|
|
|
bool create_io_error;
|
|
|
|
};
|
|
|
|
|
|
|
|
// ReadAsyncFS and ReadAsyncRandomAccessFile mocks the FS doing asynchronous
|
|
|
|
// reads by creating threads that submit read requests and then calling Poll API
|
|
|
|
// to obtain those results.
|
|
|
|
class ReadAsyncRandomAccessFile : public FSRandomAccessFileOwnerWrapper {
|
|
|
|
public:
|
|
|
|
ReadAsyncRandomAccessFile(ReadAsyncFS& fs,
|
|
|
|
std::unique_ptr<FSRandomAccessFile>& file)
|
|
|
|
: FSRandomAccessFileOwnerWrapper(std::move(file)), fs_(fs) {}
|
|
|
|
|
|
|
|
IOStatus ReadAsync(FSReadRequest& req, const IOOptions& opts,
|
|
|
|
std::function<void(const FSReadRequest&, void*)> cb,
|
|
|
|
void* cb_arg, void** io_handle, IOHandleDeleter* del_fn,
|
|
|
|
IODebugContext* dbg) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
ReadAsyncFS& fs_;
|
|
|
|
std::unique_ptr<FSRandomAccessFile> file_;
|
|
|
|
int counter = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ReadAsyncFS : public FileSystemWrapper {
|
|
|
|
public:
|
|
|
|
explicit ReadAsyncFS(const std::shared_ptr<FileSystem>& wrapped)
|
|
|
|
: FileSystemWrapper(wrapped) {}
|
|
|
|
|
|
|
|
static const char* kClassName() { return "ReadAsyncFS"; }
|
|
|
|
const char* Name() const override { return kClassName(); }
|
|
|
|
|
|
|
|
IOStatus NewRandomAccessFile(const std::string& fname,
|
|
|
|
const FileOptions& opts,
|
|
|
|
std::unique_ptr<FSRandomAccessFile>* result,
|
|
|
|
IODebugContext* dbg) override {
|
|
|
|
std::unique_ptr<FSRandomAccessFile> file;
|
|
|
|
IOStatus s = target()->NewRandomAccessFile(fname, opts, &file, dbg);
|
|
|
|
EXPECT_OK(s);
|
|
|
|
result->reset(new ReadAsyncRandomAccessFile(*this, file));
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
IOStatus Poll(std::vector<void*>& io_handles,
|
|
|
|
size_t /*min_completions*/) override {
|
|
|
|
// Wait for the threads completion.
|
|
|
|
for (auto& t : workers) {
|
|
|
|
t.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < io_handles.size(); i++) {
|
|
|
|
MockIOHandle* handle = static_cast<MockIOHandle*>(io_handles[i]);
|
|
|
|
if (handle->create_io_error) {
|
|
|
|
FSReadRequest req;
|
|
|
|
req.status = IOStatus::IOError();
|
|
|
|
handle->cb(req, handle->cb_arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return IOStatus::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::thread> workers;
|
|
|
|
};
|
|
|
|
|
|
|
|
IOStatus ReadAsyncRandomAccessFile::ReadAsync(
|
|
|
|
FSReadRequest& req, const IOOptions& opts,
|
|
|
|
std::function<void(const FSReadRequest&, void*)> cb, void* cb_arg,
|
|
|
|
void** io_handle, IOHandleDeleter* del_fn, IODebugContext* dbg) {
|
|
|
|
IOHandleDeleter deletefn = [](void* args) -> void {
|
|
|
|
delete (static_cast<MockIOHandle*>(args));
|
|
|
|
args = nullptr;
|
|
|
|
};
|
|
|
|
*del_fn = deletefn;
|
|
|
|
|
|
|
|
// Allocate and populate io_handle.
|
|
|
|
MockIOHandle* mock_handle = new MockIOHandle();
|
|
|
|
bool create_io_error = false;
|
|
|
|
if (counter % 2) {
|
|
|
|
create_io_error = true;
|
|
|
|
}
|
|
|
|
mock_handle->create_io_error = create_io_error;
|
|
|
|
mock_handle->cb = cb;
|
|
|
|
mock_handle->cb_arg = cb_arg;
|
|
|
|
*io_handle = static_cast<void*>(mock_handle);
|
|
|
|
counter++;
|
|
|
|
|
|
|
|
// Submit read request asynchronously.
|
|
|
|
std::function<void(FSReadRequest)> submit_request =
|
2022-03-11 02:28:31 +00:00
|
|
|
[&opts, cb, cb_arg, dbg, create_io_error, this](FSReadRequest _req) {
|
2022-03-02 01:11:42 +00:00
|
|
|
if (!create_io_error) {
|
2022-03-11 02:28:31 +00:00
|
|
|
_req.status = target()->Read(_req.offset, _req.len, opts,
|
|
|
|
&(_req.result), _req.scratch, dbg);
|
|
|
|
cb(_req, cb_arg);
|
2022-03-02 01:11:42 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
fs_.workers.emplace_back(submit_request, req);
|
|
|
|
return IOStatus::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
class TestAsyncRead : public testing::Test {
|
|
|
|
public:
|
|
|
|
TestAsyncRead() { env_ = Env::Default(); }
|
|
|
|
Env* env_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Tests the default implementation of ReadAsync API.
|
|
|
|
TEST_F(TestAsyncRead, ReadAsync) {
|
|
|
|
EnvOptions soptions;
|
|
|
|
std::shared_ptr<ReadAsyncFS> fs =
|
|
|
|
std::make_shared<ReadAsyncFS>(env_->GetFileSystem());
|
|
|
|
|
|
|
|
std::string fname = test::PerThreadDBPath(env_, "testfile");
|
|
|
|
|
|
|
|
const size_t kSectorSize = 4096;
|
|
|
|
const size_t kNumSectors = 8;
|
|
|
|
|
|
|
|
// 1. create & write to a file.
|
|
|
|
{
|
|
|
|
std::unique_ptr<FSWritableFile> wfile;
|
|
|
|
ASSERT_OK(
|
|
|
|
fs->NewWritableFile(fname, FileOptions(), &wfile, nullptr /*dbg*/));
|
|
|
|
|
|
|
|
for (size_t i = 0; i < kNumSectors; ++i) {
|
|
|
|
auto data = NewAligned(kSectorSize * 8, static_cast<char>(i + 1));
|
|
|
|
Slice slice(data.get(), kSectorSize);
|
|
|
|
ASSERT_OK(wfile->Append(slice, IOOptions(), nullptr));
|
|
|
|
}
|
|
|
|
ASSERT_OK(wfile->Close(IOOptions(), nullptr));
|
|
|
|
}
|
|
|
|
// 2. Read file
|
|
|
|
{
|
|
|
|
std::unique_ptr<FSRandomAccessFile> file;
|
|
|
|
ASSERT_OK(fs->NewRandomAccessFile(fname, FileOptions(), &file, nullptr));
|
|
|
|
|
|
|
|
IOOptions opts;
|
|
|
|
std::vector<void*> io_handles(kNumSectors);
|
|
|
|
std::vector<FSReadRequest> reqs(kNumSectors);
|
|
|
|
std::vector<std::unique_ptr<char, Deleter>> data;
|
|
|
|
std::vector<size_t> vals;
|
|
|
|
IOHandleDeleter del_fn;
|
|
|
|
uint64_t offset = 0;
|
|
|
|
|
|
|
|
// Initialize read requests
|
|
|
|
for (size_t i = 0; i < kNumSectors; i++) {
|
|
|
|
reqs[i].offset = offset;
|
|
|
|
reqs[i].len = kSectorSize;
|
|
|
|
data.emplace_back(NewAligned(kSectorSize, 0));
|
|
|
|
reqs[i].scratch = data.back().get();
|
|
|
|
vals.push_back(i);
|
|
|
|
offset += kSectorSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// callback function passed to async read.
|
|
|
|
std::function<void(const FSReadRequest&, void*)> callback =
|
|
|
|
[&](const FSReadRequest& req, void* cb_arg) {
|
|
|
|
assert(cb_arg != nullptr);
|
|
|
|
size_t i = *(reinterpret_cast<size_t*>(cb_arg));
|
|
|
|
reqs[i].offset = req.offset;
|
|
|
|
reqs[i].result = req.result;
|
|
|
|
reqs[i].status = req.status;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Submit asynchronous read requests.
|
|
|
|
for (size_t i = 0; i < kNumSectors; i++) {
|
|
|
|
void* cb_arg = static_cast<void*>(&(vals[i]));
|
|
|
|
ASSERT_OK(file->ReadAsync(reqs[i], opts, callback, cb_arg,
|
|
|
|
&(io_handles[i]), &del_fn, nullptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Poll for the submitted requests.
|
|
|
|
fs->Poll(io_handles, kNumSectors);
|
|
|
|
|
|
|
|
// Check the status of read requests.
|
|
|
|
for (size_t i = 0; i < kNumSectors; i++) {
|
|
|
|
if (i % 2) {
|
|
|
|
ASSERT_EQ(reqs[i].status, IOStatus::IOError());
|
|
|
|
} else {
|
|
|
|
auto buf = NewAligned(kSectorSize * 8, static_cast<char>(i + 1));
|
|
|
|
Slice expected_data(buf.get(), kSectorSize);
|
|
|
|
|
|
|
|
ASSERT_EQ(reqs[i].offset, i * kSectorSize);
|
|
|
|
ASSERT_OK(reqs[i].status);
|
|
|
|
ASSERT_EQ(expected_data.ToString(), reqs[i].result.ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete io_handles.
|
|
|
|
for (size_t i = 0; i < io_handles.size(); i++) {
|
|
|
|
del_fn(io_handles[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2022-01-05 00:44:54 +00:00
|
|
|
ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
|
2015-03-17 21:08:00 +00:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|