Skip to content

Commit

Permalink
Break stalls when no bg work is happening
Browse files Browse the repository at this point in the history
Summary:
Current stall will keep sleeping even if there is no Flush/Compactions to wait for, I changed the logic to break the stall if we are not flushing or compacting

db_bench command used
```
# fillrandom
# memtable size = 10MB
# value size = 1 MB
# num = 1000
# use /dev/shm
./db_bench --benchmarks="fillrandom,stats" --value_size=1048576 --write_buffer_size=10485760 --num=1000 --delayed_write_rate=XXXXX  --db="/dev/shm/new_stall" | grep "Cumulative stall"
```

```
Current results

# delayed_write_rate = 1000 Kb/sec
Cumulative stall: 00:00:9.031 H:M:S

# delayed_write_rate = 200 Kb/sec
Cumulative stall: 00:00:22.314 H:M:S

# delayed_write_rate = 100 Kb/sec
Cumulative stall: 00:00:42.784 H:M:S

# delayed_write_rate = 50 Kb/sec
Cumulative stall: 00:01:23.785 H:M:S

# delayed_write_rate = 25 Kb/sec
Cumulative stall: 00:02:45.702 H:M:S
```

```
New results

# delayed_write_rate = 1000 Kb/sec
Cumulative stall: 00:00:9.017 H:M:S

# delayed_write_rate = 200 Kb/sec
Cumulative stall: 00
Closes facebook#1884

Differential Revision: D4585439

Pulled By: IslamAbdelRahman

fbshipit-source-id: aed2198
  • Loading branch information
IslamAbdelRahman authored and facebook-github-bot committed Mar 17, 2017
1 parent e66221a commit d52f334
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
23 changes: 18 additions & 5 deletions db/db_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5134,17 +5134,30 @@ Status DBImpl::DelayWrite(uint64_t num_bytes,
bool delayed = false;
{
StopWatch sw(env_, stats_, WRITE_STALL, &time_delayed);
auto delay = write_controller_.GetDelay(env_, num_bytes);
uint64_t delay = write_controller_.GetDelay(env_, num_bytes);
if (delay > 0) {
if (write_options.no_slowdown) {
return Status::Incomplete();
}
mutex_.Unlock();
delayed = true;
TEST_SYNC_POINT("DBImpl::DelayWrite:Sleep");
// hopefully we don't have to sleep more than 2 billion microseconds
env_->SleepForMicroseconds(static_cast<int>(delay));

mutex_.Unlock();
// We will delay the write until we have slept for delay ms or
// we don't need a delay anymore
const uint64_t kDelayInterval = 1000;
uint64_t stall_end = sw.start_time() + delay;
while (write_controller_.NeedsDelay()) {
if (env_->NowMicros() >= stall_end) {
// We already delayed this write `delay` microseconds
break;
}

delayed = true;
// Sleep for 0.001 seconds
env_->SleepForMicroseconds(kDelayInterval);
}
mutex_.Lock();

}

while (bg_error_.ok() && write_controller_.IsStopped()) {
Expand Down
4 changes: 2 additions & 2 deletions db/write_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ uint64_t WriteController::GetDelay(Env* env, uint64_t num_bytes) {
if (total_stopped_ > 0) {
return 0;
}
if (total_delayed_ == 0) {
if (total_delayed_.load() == 0) {
return 0;
}

Expand Down Expand Up @@ -115,7 +115,7 @@ StopWriteToken::~StopWriteToken() {

DelayWriteToken::~DelayWriteToken() {
controller_->total_delayed_--;
assert(controller_->total_delayed_ >= 0);
assert(controller_->total_delayed_.load() >= 0);
}

CompactionPressureToken::~CompactionPressureToken() {
Expand Down
5 changes: 3 additions & 2 deletions db/write_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <stdint.h>

#include <atomic>
#include <memory>

namespace rocksdb {
Expand Down Expand Up @@ -45,7 +46,7 @@ class WriteController {

// these three metods are querying the state of the WriteController
bool IsStopped() const;
bool NeedsDelay() const { return total_delayed_ > 0; }
bool NeedsDelay() const { return total_delayed_.load() > 0; }
bool NeedSpeedupCompaction() const {
return IsStopped() || NeedsDelay() || total_compaction_pressure_ > 0;
}
Expand Down Expand Up @@ -87,7 +88,7 @@ class WriteController {
friend class CompactionPressureToken;

int total_stopped_;
int total_delayed_;
std::atomic<int> total_delayed_;
int total_compaction_pressure_;
uint64_t bytes_left_;
uint64_t last_refill_time_;
Expand Down
2 changes: 2 additions & 0 deletions util/stop_watch.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class StopWatch {
}
}

uint64_t start_time() const { return start_time_; }

private:
Env* const env_;
Statistics* statistics_;
Expand Down

0 comments on commit d52f334

Please sign in to comment.