YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/rocksdb/db/write_controller.h
Line
Count
Source (jump to first uncovered line)
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2
//  This source code is licensed under the BSD-style license found in the
3
//  LICENSE file in the root directory of this source tree. An additional grant
4
//  of patent rights can be found in the PATENTS file in the same directory.
5
//
6
// The following only applies to changes made to this file as part of YugaByte development.
7
//
8
// Portions Copyright (c) YugaByte, Inc.
9
//
10
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
11
// in compliance with the License.  You may obtain a copy of the License at
12
//
13
// http://www.apache.org/licenses/LICENSE-2.0
14
//
15
// Unless required by applicable law or agreed to in writing, software distributed under the License
16
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
17
// or implied.  See the License for the specific language governing permissions and limitations
18
// under the License.
19
//
20
21
#pragma once
22
23
#include <memory>
24
25
namespace rocksdb {
26
27
class Env;
28
class WriteControllerToken;
29
30
// WriteController is controlling write stalls in our write code-path. Write
31
// stalls happen when compaction can't keep up with write rate.
32
// All of the methods here (including WriteControllerToken's destructors) need
33
// to be called while holding DB mutex
34
class WriteController {
35
 public:
36
  explicit WriteController(uint64_t _delayed_write_rate = 1024u * 1024u * 32u)
37
      : total_stopped_(0),
38
        total_delayed_(0),
39
        total_compaction_pressure_(0),
40
        bytes_left_(0),
41
435k
        last_refill_time_(0) {
42
435k
    set_delayed_write_rate(_delayed_write_rate);
43
435k
  }
44
  ~WriteController() = default;
45
46
  // When an actor (column family) requests a stop token, all writes will be
47
  // stopped until the stop token is released (deleted)
48
  std::unique_ptr<WriteControllerToken> GetStopToken();
49
  // When an actor (column family) requests a delay token, total delay for all
50
  // writes to the DB will be controlled under the delayed write rate. Every
51
  // write needs to call GetDelay() with number of bytes writing to the DB,
52
  // which returns number of microseconds to sleep.
53
  std::unique_ptr<WriteControllerToken> GetDelayToken(
54
      uint64_t delayed_write_rate);
55
  // When an actor (column family) requests a moderate token, compaction
56
  // threads will be increased
57
  std::unique_ptr<WriteControllerToken> GetCompactionPressureToken();
58
59
  // these three metods are querying the state of the WriteController
60
  bool IsStopped() const;
61
34.7M
  bool NeedsDelay() const { return total_delayed_ > 0; }
62
1.26M
  bool NeedSpeedupCompaction() const {
63
1.26M
    return IsStopped() || 
NeedsDelay()1.25M
||
total_compaction_pressure_ > 01.25M
;
64
1.26M
  }
65
  // return how many microseconds the caller needs to sleep after the call
66
  // num_bytes: how many number of bytes to put into the DB.
67
  // Prerequisite: DB mutex held.
68
  uint64_t GetDelay(Env* env, uint64_t num_bytes);
69
436k
  void set_delayed_write_rate(uint64_t write_rate) {
70
    // avoid divide 0
71
436k
    if (write_rate == 0) {
72
0
      write_rate = 1u;
73
0
    }
74
436k
    delayed_write_rate_ = write_rate;
75
436k
  }
76
1.82k
  uint64_t delayed_write_rate() const { return delayed_write_rate_; }
77
78
 private:
79
  friend class WriteControllerToken;
80
  friend class StopWriteToken;
81
  friend class DelayWriteToken;
82
  friend class CompactionPressureToken;
83
84
  int total_stopped_;
85
  int total_delayed_;
86
  int total_compaction_pressure_;
87
  uint64_t bytes_left_;
88
  uint64_t last_refill_time_;
89
  uint64_t delayed_write_rate_;
90
};
91
92
class WriteControllerToken {
93
 public:
94
  explicit WriteControllerToken(WriteController* controller)
95
1.24M
      : controller_(controller) {}
96
1.20M
  virtual ~WriteControllerToken() {}
97
98
 protected:
99
  WriteController* controller_;
100
101
 private:
102
  // no copying allowed
103
  WriteControllerToken(const WriteControllerToken&) = delete;
104
  void operator=(const WriteControllerToken&) = delete;
105
};
106
107
class StopWriteToken : public WriteControllerToken {
108
 public:
109
  explicit StopWriteToken(WriteController* controller)
110
4.41k
      : WriteControllerToken(controller) {}
111
  virtual ~StopWriteToken();
112
};
113
114
class DelayWriteToken : public WriteControllerToken {
115
 public:
116
  explicit DelayWriteToken(WriteController* controller)
117
906
      : WriteControllerToken(controller) {}
118
  virtual ~DelayWriteToken();
119
};
120
121
class CompactionPressureToken : public WriteControllerToken {
122
 public:
123
  explicit CompactionPressureToken(WriteController* controller)
124
1.23M
      : WriteControllerToken(controller) {}
125
  virtual ~CompactionPressureToken();
126
};
127
128
}  // namespace rocksdb