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).
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
|
|
|
|
2021-01-26 06:07:26 +00:00
|
|
|
#include "monitoring/instrumented_mutex.h"
|
2018-09-27 22:25:47 +00:00
|
|
|
#include "port/port.h"
|
2021-01-26 06:07:26 +00:00
|
|
|
#include "rocksdb/system_clock.h"
|
2018-09-27 22:25:47 +00:00
|
|
|
#include "util/mutexlock.h"
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2018-09-27 22:25:47 +00:00
|
|
|
|
2019-05-23 23:26:08 +00:00
|
|
|
// Simple wrapper around port::Thread that supports calling a callback every
|
|
|
|
// X seconds. If you pass in 0, then it will call your callback repeatedly
|
|
|
|
// without delay.
|
2018-09-27 22:25:47 +00:00
|
|
|
class RepeatableThread {
|
|
|
|
public:
|
|
|
|
RepeatableThread(std::function<void()> function,
|
2021-03-15 11:32:24 +00:00
|
|
|
const std::string& thread_name, SystemClock* clock,
|
|
|
|
uint64_t delay_us, uint64_t initial_delay_us = 0)
|
2018-09-27 22:25:47 +00:00
|
|
|
: function_(function),
|
|
|
|
thread_name_("rocksdb:" + thread_name),
|
2021-01-26 06:07:26 +00:00
|
|
|
clock_(clock),
|
2018-09-27 22:25:47 +00:00
|
|
|
delay_us_(delay_us),
|
|
|
|
initial_delay_us_(initial_delay_us),
|
2021-01-26 06:07:26 +00:00
|
|
|
mutex_(clock),
|
2018-09-27 22:25:47 +00:00
|
|
|
cond_var_(&mutex_),
|
|
|
|
running_(true),
|
|
|
|
#ifndef NDEBUG
|
|
|
|
waiting_(false),
|
|
|
|
run_count_(0),
|
|
|
|
#endif
|
|
|
|
thread_([this] { thread(); }) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void cancel() {
|
|
|
|
{
|
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
|
|
|
InstrumentedMutexLock l(&mutex_);
|
2018-09-27 22:25:47 +00:00
|
|
|
if (!running_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
running_ = false;
|
|
|
|
cond_var_.SignalAll();
|
|
|
|
}
|
|
|
|
thread_.join();
|
|
|
|
}
|
|
|
|
|
2019-02-20 23:46:59 +00:00
|
|
|
bool IsRunning() { return running_; }
|
|
|
|
|
2018-09-27 22:25:47 +00:00
|
|
|
~RepeatableThread() { cancel(); }
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Wait until RepeatableThread starting waiting, call the optional callback,
|
|
|
|
// then wait for one run of RepeatableThread. Tests can use provide a
|
2021-01-26 06:07:26 +00:00
|
|
|
// custom clock object to mock time, and use the callback here to bump current
|
2018-09-27 22:25:47 +00:00
|
|
|
// time and trigger RepeatableThread. See repeatable_thread_test for example.
|
|
|
|
//
|
|
|
|
// Note: only support one caller of this method.
|
|
|
|
void TEST_WaitForRun(std::function<void()> callback = nullptr) {
|
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
|
|
|
InstrumentedMutexLock l(&mutex_);
|
2018-09-27 22:25:47 +00:00
|
|
|
while (!waiting_) {
|
|
|
|
cond_var_.Wait();
|
|
|
|
}
|
|
|
|
uint64_t prev_count = run_count_;
|
|
|
|
if (callback != nullptr) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
cond_var_.SignalAll();
|
|
|
|
while (!(run_count_ > prev_count)) {
|
|
|
|
cond_var_.Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool wait(uint64_t delay) {
|
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
|
|
|
InstrumentedMutexLock l(&mutex_);
|
2018-09-27 22:25:47 +00:00
|
|
|
if (running_ && delay > 0) {
|
2021-01-26 06:07:26 +00:00
|
|
|
uint64_t wait_until = clock_->NowMicros() + delay;
|
2018-09-27 22:25:47 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
waiting_ = true;
|
|
|
|
cond_var_.SignalAll();
|
|
|
|
#endif
|
|
|
|
while (running_) {
|
|
|
|
cond_var_.TimedWait(wait_until);
|
2021-01-26 06:07:26 +00:00
|
|
|
if (clock_->NowMicros() >= wait_until) {
|
2018-09-27 22:25:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifndef NDEBUG
|
|
|
|
waiting_ = false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return running_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void thread() {
|
|
|
|
#if defined(_GNU_SOURCE) && defined(__GLIBC_PREREQ)
|
|
|
|
#if __GLIBC_PREREQ(2, 12)
|
|
|
|
// Set thread name.
|
|
|
|
auto thread_handle = thread_.native_handle();
|
|
|
|
int ret __attribute__((__unused__)) =
|
|
|
|
pthread_setname_np(thread_handle, thread_name_.c_str());
|
|
|
|
assert(ret == 0);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
assert(delay_us_ > 0);
|
|
|
|
if (!wait(initial_delay_us_)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
function_();
|
|
|
|
#ifndef NDEBUG
|
|
|
|
{
|
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
|
|
|
InstrumentedMutexLock l(&mutex_);
|
2018-09-27 22:25:47 +00:00
|
|
|
run_count_++;
|
|
|
|
cond_var_.SignalAll();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} while (wait(delay_us_));
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::function<void()> function_;
|
|
|
|
const std::string thread_name_;
|
2021-03-15 11:32:24 +00:00
|
|
|
SystemClock* clock_;
|
2018-09-27 22:25:47 +00:00
|
|
|
const uint64_t delay_us_;
|
|
|
|
const uint64_t initial_delay_us_;
|
|
|
|
|
|
|
|
// Mutex lock should be held when accessing running_, waiting_
|
|
|
|
// and run_count_.
|
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
|
|
|
InstrumentedMutex mutex_;
|
|
|
|
InstrumentedCondVar cond_var_;
|
2018-09-27 22:25:47 +00:00
|
|
|
bool running_;
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// RepeatableThread waiting for timeout.
|
|
|
|
bool waiting_;
|
|
|
|
// Times function_ had run.
|
|
|
|
uint64_t run_count_;
|
|
|
|
#endif
|
|
|
|
port::Thread thread_;
|
|
|
|
};
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|