#include <atomic>
#include <memory>
+
#include "rocksdb/rate_limiter.h"
namespace ROCKSDB_NAMESPACE {
-class Env;
+class SystemClock;
class WriteControllerToken;
// WriteController is controlling write stalls in our write code-path. Write
: total_stopped_(0),
total_delayed_(0),
total_compaction_pressure_(0),
- bytes_left_(0),
- last_refill_time_(0),
+ credit_in_bytes_(0),
+ next_refill_time_(0),
low_pri_rate_limiter_(
NewGenericRateLimiter(low_pri_rate_bytes_per_sec)) {
set_max_delayed_write_rate(_delayed_write_rate);
bool IsStopped() const;
bool NeedsDelay() const { return total_delayed_.load() > 0; }
bool NeedSpeedupCompaction() const {
- return IsStopped() || NeedsDelay() || total_compaction_pressure_ > 0;
+ return IsStopped() || NeedsDelay() || total_compaction_pressure_.load() > 0;
}
// return how many microseconds the caller needs to sleep after the call
// num_bytes: how many number of bytes to put into the DB.
// Prerequisite: DB mutex held.
- uint64_t GetDelay(Env* env, uint64_t num_bytes);
+ uint64_t GetDelay(SystemClock* clock, uint64_t num_bytes);
void set_delayed_write_rate(uint64_t write_rate) {
// avoid divide 0
if (write_rate == 0) {
RateLimiter* low_pri_rate_limiter() { return low_pri_rate_limiter_.get(); }
private:
- uint64_t NowMicrosMonotonic(Env* env);
+ uint64_t NowMicrosMonotonic(SystemClock* clock);
friend class WriteControllerToken;
friend class StopWriteToken;
std::atomic<int> total_stopped_;
std::atomic<int> total_delayed_;
std::atomic<int> total_compaction_pressure_;
- uint64_t bytes_left_;
- uint64_t last_refill_time_;
- // write rate set when initialization or by `DBImpl::SetDBOptions`
+
+ // Number of bytes allowed to write without delay
+ uint64_t credit_in_bytes_;
+ // Next time that we can add more credit of bytes
+ uint64_t next_refill_time_;
+ // Write rate set when initialization or by `DBImpl::SetDBOptions`
uint64_t max_delayed_write_rate_;
- // current write rate
+ // Current write rate (bytes / second)
uint64_t delayed_write_rate_;
std::unique_ptr<RateLimiter> low_pri_rate_limiter_;