2018-09-27 22:25:47 +00: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).
|
|
|
|
|
2021-01-26 06:07:26 +00:00
|
|
|
#include "util/repeatable_thread.h"
|
|
|
|
|
2018-09-27 22:25:47 +00:00
|
|
|
#include <atomic>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include "db/db_test_util.h"
|
2021-01-26 06:07:26 +00:00
|
|
|
#include "test_util/mock_time_env.h"
|
2019-05-30 18:21:38 +00:00
|
|
|
#include "test_util/sync_point.h"
|
|
|
|
#include "test_util/testharness.h"
|
2018-09-27 22:25:47 +00:00
|
|
|
|
|
|
|
class RepeatableThreadTest : public testing::Test {
|
|
|
|
public:
|
|
|
|
RepeatableThreadTest()
|
2021-01-26 06:07:26 +00:00
|
|
|
: mock_clock_(std::make_shared<ROCKSDB_NAMESPACE::MockSystemClock>(
|
|
|
|
ROCKSDB_NAMESPACE::SystemClock::Default())) {}
|
2018-09-27 22:25:47 +00:00
|
|
|
|
|
|
|
protected:
|
2021-01-26 06:07:26 +00:00
|
|
|
std::shared_ptr<ROCKSDB_NAMESPACE::MockSystemClock> mock_clock_;
|
2018-09-27 22:25:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(RepeatableThreadTest, TimedTest) {
|
|
|
|
constexpr uint64_t kSecond = 1000000; // 1s = 1000000us
|
|
|
|
constexpr int kIteration = 3;
|
2021-01-26 06:07:26 +00:00
|
|
|
const auto& clock = ROCKSDB_NAMESPACE::SystemClock::Default();
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::port::Mutex mutex;
|
|
|
|
ROCKSDB_NAMESPACE::port::CondVar test_cv(&mutex);
|
2018-09-27 22:25:47 +00:00
|
|
|
int count = 0;
|
2021-01-26 06:07:26 +00:00
|
|
|
uint64_t prev_time = clock->NowMicros();
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::RepeatableThread thread(
|
2018-09-27 22:25:47 +00:00
|
|
|
[&] {
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::MutexLock l(&mutex);
|
2018-09-27 22:25:47 +00:00
|
|
|
count++;
|
2021-01-26 06:07:26 +00:00
|
|
|
uint64_t now = clock->NowMicros();
|
2018-09-27 22:25:47 +00:00
|
|
|
assert(count == 1 || prev_time + 1 * kSecond <= now);
|
|
|
|
prev_time = now;
|
|
|
|
if (count >= kIteration) {
|
|
|
|
test_cv.SignalAll();
|
|
|
|
}
|
|
|
|
},
|
2021-03-15 11:32:24 +00:00
|
|
|
"rt_test", clock.get(), 1 * kSecond);
|
2018-09-27 22:25:47 +00:00
|
|
|
// Wait for execution finish.
|
|
|
|
{
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::MutexLock l(&mutex);
|
2018-09-27 22:25:47 +00:00
|
|
|
while (count < kIteration) {
|
|
|
|
test_cv.Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test cancel
|
|
|
|
thread.cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(RepeatableThreadTest, MockEnvTest) {
|
|
|
|
constexpr uint64_t kSecond = 1000000; // 1s = 1000000us
|
|
|
|
constexpr int kIteration = 3;
|
2021-01-26 06:07:26 +00:00
|
|
|
mock_clock_->SetCurrentTime(0); // in seconds
|
2018-09-27 22:25:47 +00:00
|
|
|
std::atomic<int> count{0};
|
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 17:03:02 +00:00
|
|
|
|
|
|
|
#if defined(OS_MACOSX) && !defined(NDEBUG)
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->ClearAllCallBacks();
|
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->SetCallBack(
|
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 17:03:02 +00:00
|
|
|
"InstrumentedCondVar::TimedWaitInternal", [&](void* arg) {
|
|
|
|
// Obtain the current (real) time in seconds and add 1000 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 on certain platforms, e.g. OS X. 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
|
2021-01-26 06:07:26 +00:00
|
|
|
// mock_clock->NowMicros. Consequently, RepeatableThread::wait cannot
|
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 17:03:02 +00:00
|
|
|
// break out of the loop, causing test to hang. The extra 1000 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, it is no guarantee that the
|
2021-01-26 06:07:26 +00:00
|
|
|
// delay + mock_clock->NowMicros will be greater than the current real
|
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 17:03:02 +00:00
|
|
|
// time. However, 1000 seconds should be sufficient in most cases.
|
|
|
|
uint64_t time_us = *reinterpret_cast<uint64_t*>(arg);
|
2021-01-26 06:07:26 +00:00
|
|
|
if (time_us < mock_clock_->RealNowMicros()) {
|
|
|
|
*reinterpret_cast<uint64_t*>(arg) =
|
|
|
|
mock_clock_->RealNowMicros() + 1000;
|
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 17:03:02 +00:00
|
|
|
}
|
|
|
|
});
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
|
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 17:03:02 +00:00
|
|
|
#endif // OS_MACOSX && !NDEBUG
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
ROCKSDB_NAMESPACE::RepeatableThread thread(
|
2021-03-15 11:32:24 +00:00
|
|
|
[&] { count++; }, "rt_test", mock_clock_.get(), 1 * kSecond, 1 * kSecond);
|
2018-09-27 22:25:47 +00:00
|
|
|
for (int i = 1; i <= kIteration; i++) {
|
|
|
|
// Bump current time
|
2021-01-26 06:07:26 +00:00
|
|
|
thread.TEST_WaitForRun([&] { mock_clock_->SetCurrentTime(i); });
|
2018-09-27 22:25:47 +00:00
|
|
|
}
|
|
|
|
// Test function should be exectued exactly kIteraion times.
|
|
|
|
ASSERT_EQ(kIteration, count.load());
|
|
|
|
|
|
|
|
// Test cancel
|
|
|
|
thread.cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2022-10-18 07:35:35 +00:00
|
|
|
ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
|
2018-09-27 22:25:47 +00:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
|
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
}
|