2018-10-22 05:14:58 +02:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
|
|
// 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).
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "rocksdb/env.h"
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2018-10-22 05:14:58 +02:00
|
|
|
|
Fix+clean up handling of mock sleeps (#7101)
Summary:
We have a number of tests hanging on MacOS and windows due to
mishandling of code for mock sleeps. In addition, the code was in
terrible shape because the same variable (addon_time_) would sometimes
refer to microseconds and sometimes to seconds. One test even assumed it
was nanoseconds but was written to pass anyway.
This has been cleaned up so that DB tests generally use a SpecialEnv
function to mock sleep, for either some number of microseconds or seconds
depending on the function called. But to call one of these, the test must first
call SetMockSleep (precondition enforced with assertion), which also turns
sleeps in RocksDB into mock sleeps. To also removes accounting for actual
clock time, call SetTimeElapseOnlySleepOnReopen, which implies
SetMockSleep (on DB re-open). This latter setting only works by applying
on DB re-open, otherwise havoc can ensue if Env goes back in time with
DB open.
More specifics:
Removed some unused test classes, and updated comments on the general
problem.
Fixed DBSSTTest.GetTotalSstFilesSize using a sync point callback instead
of mock time. For this we have the only modification to production code,
inserting a sync point callback in flush_job.cc, which is not a change to
production behavior.
Removed unnecessary resetting of mock times to 0 in many tests. RocksDB
deals in relative time. Any behaviors relying on absolute date/time are likely
a bug. (The above test DBSSTTest.GetTotalSstFilesSize was the only one
clearly injecting a specific absolute time for actual testing convenience.) Just
in case I misunderstood some test, I put this note in each replacement:
// NOTE: Presumed unnecessary and removed: resetting mock time in env
Strengthened some tests like MergeTestTime, MergeCompactionTimeTest, and
FilterCompactionTimeTest in db_test.cc
stats_history_test and blob_db_test are each their own beast, rather deeply
dependent on MockTimeEnv. Each gets its own variant of a work-around for
TimedWait in a mock time environment. (Reduces redundancy and
inconsistency in stats_history_test.)
Intended follow-up:
Remove TimedWait from the public API of InstrumentedCondVar, and only
make that accessible through Env by passing in an InstrumentedCondVar and
a deadline. Then the Env implementations mocking time can fix this problem
without using sync points. (Test infrastructure using sync points interferes
with individual tests' control over sync points.)
With that change, we can simplify/consolidate the scattered work-arounds.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/7101
Test Plan: make check on Linux and MacOS
Reviewed By: zhichao-cao
Differential Revision: D23032815
Pulled By: pdillinger
fbshipit-source-id: 7f33967ada8b83011fb54e8279365c008bd6610b
2020-08-11 21:39:49 +02:00
|
|
|
// NOTE: SpecialEnv offers most of this functionality, along with hooks
|
|
|
|
// for safe DB behavior under a mock time environment, so should be used
|
|
|
|
// instead of MockTimeEnv for DB tests.
|
2018-10-22 05:14:58 +02:00
|
|
|
class MockTimeEnv : public EnvWrapper {
|
|
|
|
public:
|
|
|
|
explicit MockTimeEnv(Env* base) : EnvWrapper(base) {}
|
|
|
|
|
2020-08-21 20:31:27 +02:00
|
|
|
virtual Status GetCurrentTime(int64_t* time_sec) override {
|
|
|
|
assert(time_sec != nullptr);
|
|
|
|
*time_sec = static_cast<int64_t>(current_time_us_ / kMicrosInSecond);
|
2018-10-22 05:14:58 +02:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2020-08-21 20:31:27 +02:00
|
|
|
virtual uint64_t NowSeconds() { return current_time_us_ / kMicrosInSecond; }
|
|
|
|
|
|
|
|
virtual uint64_t NowMicros() override { return current_time_us_; }
|
2018-10-22 05:14:58 +02:00
|
|
|
|
|
|
|
virtual uint64_t NowNanos() override {
|
2020-08-21 20:31:27 +02:00
|
|
|
assert(current_time_us_ <= std::numeric_limits<uint64_t>::max() / 1000);
|
|
|
|
return current_time_us_ * 1000;
|
2018-10-22 05:14:58 +02:00
|
|
|
}
|
|
|
|
|
Update RepeatableThreadTest with MockTimeEnv (#5107)
Summary:
**This PR updates RepeatableThread::wait, breaking some tests on OS X. The rest of the PR fixes the tests on OS X.**
`RepeatableThreadTest.MockEnvTest` uses `MockTimeEnv` and `RepeatableThread`. If `RepeatableThread::wait` calls `TimedWait` with a time smaller than or equal to the current (real) time, `TimedWait` returns immediately on certain platforms, e.g. OS X. #4560 addresses this issue by replacing `TimedWait` with `Wait` in test. This fixes the test but makes test/production code diverge, which is not optimal for test coverage. This PR proposes an alternative fix which unifies test and production code path for `RepeatableThread::wait`. We obtain the current (real) time in seconds and add 10 extra seconds to ensure that `RepeatableThread::wait` invokes `TimedWait` with a time greater than (real) current time. This is to prevent the `TimedWait` function from returning immediately without sleeping and releasing the mutex. If `TimedWait` returns immediately, the mutex will not be released, and `RepeatableThread::TEST_WaitForRun` never has a chance to execute the callback which, in this case, updates the result returned by `mock_env->NowMicros()`. Consequently, `RepeatableThread::wait` cannot break out of the loop, causing test to hang. The extra 10 seconds is a best-effort approach because there seems no reliable and deterministic way to provide the aforementioned guarantee. By the time `RepeatableThread::wait` is called, there is no guarantee that the `delay + mock_env->NowMicros()` will be greater than the current real time. However, 10 seconds should be sufficient in most cases. We will keep an eye for possible flakiness of this test.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5107
Differential Revision: D14680885
Pulled By: riversand963
fbshipit-source-id: d1ecbe10e1dacd110bd464cd01e188bfee72b89e
2019-03-29 18:03:02 +01:00
|
|
|
uint64_t RealNowMicros() { return target()->NowMicros(); }
|
|
|
|
|
2020-08-21 20:31:27 +02:00
|
|
|
void set_current_time(uint64_t time_sec) {
|
|
|
|
assert(time_sec < std::numeric_limits<uint64_t>::max() / kMicrosInSecond);
|
|
|
|
assert(time_sec * kMicrosInSecond >= current_time_us_);
|
|
|
|
current_time_us_ = time_sec * kMicrosInSecond;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's a fake sleep that just updates the Env current time, which is similar
|
|
|
|
// to `NoSleepEnv.SleepForMicroseconds()` and
|
|
|
|
// `SpecialEnv.MockSleepForMicroseconds()`.
|
|
|
|
// It's also similar to `set_current_time()`, which takes an absolute time in
|
|
|
|
// seconds, vs. this one takes the sleep in microseconds.
|
|
|
|
// Note: Not thread safe.
|
|
|
|
void MockSleepForMicroseconds(int micros) {
|
|
|
|
assert(micros >= 0);
|
|
|
|
assert(current_time_us_ + static_cast<uint64_t>(micros) >=
|
|
|
|
current_time_us_);
|
|
|
|
current_time_us_.fetch_add(micros);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MockSleepForSeconds(int seconds) {
|
|
|
|
assert(seconds >= 0);
|
|
|
|
uint64_t micros = static_cast<uint64_t>(seconds) * kMicrosInSecond;
|
|
|
|
assert(current_time_us_ + micros >= current_time_us_);
|
|
|
|
current_time_us_.fetch_add(micros);
|
2018-10-22 05:14:58 +02:00
|
|
|
}
|
|
|
|
|
2020-08-20 17:42:05 +02:00
|
|
|
// TODO: this is a workaround for the different behavior on different platform
|
|
|
|
// for timedwait timeout. Ideally timedwait API should be moved to env.
|
|
|
|
// details: PR #7101.
|
|
|
|
void InstallTimedWaitFixCallback() {
|
|
|
|
SyncPoint::GetInstance()->DisableProcessing();
|
|
|
|
SyncPoint::GetInstance()->ClearAllCallBacks();
|
|
|
|
#if defined(OS_MACOSX) && !defined(NDEBUG)
|
|
|
|
// This is an alternate way (vs. SpecialEnv) of dealing with the fact
|
|
|
|
// that on some platforms, pthread_cond_timedwait does not appear to
|
|
|
|
// release the lock for other threads to operate if the deadline time
|
|
|
|
// is already passed. (TimedWait calls are currently a bad abstraction
|
|
|
|
// because the deadline parameter is usually computed from Env time,
|
|
|
|
// but is interpreted in real clock time.)
|
|
|
|
SyncPoint::GetInstance()->SetCallBack(
|
|
|
|
"InstrumentedCondVar::TimedWaitInternal", [&](void* arg) {
|
|
|
|
uint64_t time_us = *reinterpret_cast<uint64_t*>(arg);
|
|
|
|
if (time_us < this->RealNowMicros()) {
|
|
|
|
*reinterpret_cast<uint64_t*>(arg) = this->RealNowMicros() + 1000;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
#endif // OS_MACOSX && !NDEBUG
|
|
|
|
SyncPoint::GetInstance()->EnableProcessing();
|
|
|
|
}
|
|
|
|
|
2018-10-22 05:14:58 +02:00
|
|
|
private:
|
2020-08-21 20:31:27 +02:00
|
|
|
std::atomic<uint64_t> current_time_us_{0};
|
2018-10-22 05:14:58 +02:00
|
|
|
};
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|