YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/rocksdb/rate_limiter.h
Line
Count
Source
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
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
21
// Use of this source code is governed by a BSD-style license that can be
22
// found in the LICENSE file. See the AUTHORS file for names of contributors.
23
24
#pragma once
25
26
#include "yb/rocksdb/env.h"
27
28
namespace rocksdb {
29
30
class RateLimiter {
31
 public:
32
52.0k
  virtual ~RateLimiter() {}
33
34
  // This API allows user to dynamically change rate limiter's bytes per second.
35
  // REQUIRED: bytes_per_second > 0
36
  virtual void SetBytesPerSecond(int64_t bytes_per_second) = 0;
37
38
  // Request for token to write bytes. If this request can not be satisfied,
39
  // the call is blocked. Caller is responsible to make sure
40
  // bytes <= GetSingleBurstBytes()
41
  virtual void Request(const int64_t bytes, const Env::IOPriority pri) = 0;
42
43
  // Max bytes can be granted in a single burst
44
  virtual int64_t GetSingleBurstBytes() const = 0;
45
46
  // Total bytes that go though rate limiter
47
  virtual int64_t GetTotalBytesThrough(
48
      const Env::IOPriority pri = Env::IO_TOTAL) const = 0;
49
50
  // Total # of requests that go though rate limiter
51
  virtual int64_t GetTotalRequests(
52
      const Env::IOPriority pri = Env::IO_TOTAL) const = 0;
53
};
54
55
// Create a RateLimiter object, which can be shared among RocksDB instances to
56
// control write rate of flush and compaction.
57
// @rate_bytes_per_sec: this is the only parameter you want to set most of the
58
// time. It controls the total write rate of compaction and flush in bytes per
59
// second. Currently, RocksDB does not enforce rate limit for anything other
60
// than flush and compaction, e.g. write to WAL.
61
// @refill_period_us: this controls how often tokens are refilled. For example,
62
// when rate_bytes_per_sec is set to 10MB/s and refill_period_us is set to
63
// 100ms, then 1MB is refilled every 100ms internally. Larger value can lead to
64
// burstier writes while smaller value introduces more CPU overhead.
65
// The default should work for most cases.
66
// @fairness: RateLimiter accepts high-pri requests and low-pri requests.
67
// A low-pri request is usually blocked in favor of hi-pri request. Currently,
68
// RocksDB assigns low-pri to request from compaciton and high-pri to request
69
// from flush. Low-pri requests can get blocked if flush requests come in
70
// continuouly. This fairness parameter grants low-pri requests permission by
71
// 1/fairness chance even though high-pri requests exist to avoid starvation.
72
// You should be good by leaving it at default 10.
73
extern RateLimiter* NewGenericRateLimiter(
74
    int64_t rate_bytes_per_sec,
75
    int64_t refill_period_us = 100 * 1000,
76
    int32_t fairness = 10);
77
78
}  // namespace rocksdb