/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/rate_limiter.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 | | // 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 <atomic> |
27 | | #include <deque> |
28 | | #include "yb/rocksdb/port/port.h" |
29 | | #include "yb/rocksdb/util/mutexlock.h" |
30 | | #include "yb/rocksdb/util/random.h" |
31 | | #include "yb/rocksdb/env.h" |
32 | | #include "yb/rocksdb/rate_limiter.h" |
33 | | |
34 | | namespace rocksdb { |
35 | | |
36 | | class GenericRateLimiter : public RateLimiter { |
37 | | public: |
38 | | GenericRateLimiter(int64_t refill_bytes, |
39 | | int64_t refill_period_us, int32_t fairness); |
40 | | |
41 | | virtual ~GenericRateLimiter(); |
42 | | |
43 | | // This API allows user to dynamically change rate limiter's bytes per second. |
44 | | virtual void SetBytesPerSecond(int64_t bytes_per_second) override; |
45 | | |
46 | | // Request for token to write bytes. If this request can not be satisfied, |
47 | | // the call is blocked. Caller is responsible to make sure |
48 | | // bytes <= GetSingleBurstBytes() |
49 | | virtual void Request(const int64_t bytes, const Env::IOPriority pri) override; |
50 | | |
51 | 774k | virtual int64_t GetSingleBurstBytes() const override { |
52 | 774k | return refill_bytes_per_period_.load(std::memory_order_relaxed); |
53 | 774k | } |
54 | | |
55 | | virtual int64_t GetTotalBytesThrough( |
56 | 2 | const Env::IOPriority pri = Env::IO_TOTAL) const override { |
57 | 2 | MutexLock g(&request_mutex_); |
58 | 2 | if (pri == Env::IO_TOTAL) { |
59 | 2 | return total_bytes_through_[Env::IO_LOW] + |
60 | 2 | total_bytes_through_[Env::IO_HIGH]; |
61 | 2 | } |
62 | 0 | return total_bytes_through_[pri]; |
63 | 2 | } |
64 | | |
65 | | virtual int64_t GetTotalRequests( |
66 | 0 | const Env::IOPriority pri = Env::IO_TOTAL) const override { |
67 | 0 | MutexLock g(&request_mutex_); |
68 | 0 | if (pri == Env::IO_TOTAL) { |
69 | 0 | return total_requests_[Env::IO_LOW] + total_requests_[Env::IO_HIGH]; |
70 | 0 | } |
71 | 0 | return total_requests_[pri]; |
72 | 0 | } |
73 | | |
74 | | private: |
75 | | void Refill(); |
76 | 98.3k | int64_t CalculateRefillBytesPerPeriod(int64_t rate_bytes_per_sec) { |
77 | 98.3k | return rate_bytes_per_sec * refill_period_us_ / 1000000; |
78 | 98.3k | } |
79 | | |
80 | | // This mutex guard all internal states |
81 | | mutable port::Mutex request_mutex_; |
82 | | |
83 | | const int64_t refill_period_us_; |
84 | | // This variable can be changed dynamically. |
85 | | std::atomic<int64_t> refill_bytes_per_period_; |
86 | | Env* const env_; |
87 | | |
88 | | bool stop_; |
89 | | port::CondVar exit_cv_; |
90 | | int32_t requests_to_wait_; |
91 | | |
92 | | int64_t total_requests_[Env::IO_TOTAL]; |
93 | | int64_t total_bytes_through_[Env::IO_TOTAL]; |
94 | | int64_t available_bytes_; |
95 | | int64_t next_refill_us_; |
96 | | |
97 | | int32_t fairness_; |
98 | | Random rnd_; |
99 | | |
100 | | struct Req; |
101 | | Req* leader_; |
102 | | std::deque<Req*> queue_[Env::IO_TOTAL]; |
103 | | }; |
104 | | |
105 | | } // namespace rocksdb |