write_controller_test.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. #include "db/write_controller.h"
  7. #include <array>
  8. #include <ratio>
  9. #include "rocksdb/system_clock.h"
  10. #include "test_util/testharness.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. namespace {
  13. class TimeSetClock : public SystemClockWrapper {
  14. public:
  15. explicit TimeSetClock() : SystemClockWrapper(nullptr) {}
  16. const char* Name() const override { return "TimeSetClock"; }
  17. uint64_t now_micros_ = 6666;
  18. uint64_t NowNanos() override { return now_micros_ * std::milli::den; }
  19. };
  20. } // anonymous namespace
  21. class WriteControllerTest : public testing::Test {
  22. public:
  23. WriteControllerTest() { clock_ = std::make_shared<TimeSetClock>(); }
  24. std::shared_ptr<TimeSetClock> clock_;
  25. };
  26. // Make tests easier to read
  27. #define MILLION *1000000u
  28. #define MB MILLION
  29. #define MBPS MILLION
  30. #define SECS MILLION // in microseconds
  31. TEST_F(WriteControllerTest, BasicAPI) {
  32. WriteController controller(40 MBPS); // also set max delayed rate
  33. EXPECT_EQ(controller.delayed_write_rate(), 40 MBPS);
  34. EXPECT_FALSE(controller.IsStopped());
  35. EXPECT_FALSE(controller.NeedsDelay());
  36. EXPECT_EQ(0, controller.GetDelay(clock_.get(), 100 MB));
  37. // set, get
  38. controller.set_delayed_write_rate(20 MBPS);
  39. EXPECT_EQ(controller.delayed_write_rate(), 20 MBPS);
  40. EXPECT_FALSE(controller.IsStopped());
  41. EXPECT_FALSE(controller.NeedsDelay());
  42. EXPECT_EQ(0, controller.GetDelay(clock_.get(), 100 MB));
  43. {
  44. // set with token, get
  45. auto delay_token_0 = controller.GetDelayToken(10 MBPS);
  46. EXPECT_EQ(controller.delayed_write_rate(), 10 MBPS);
  47. EXPECT_FALSE(controller.IsStopped());
  48. EXPECT_TRUE(controller.NeedsDelay());
  49. // test with delay
  50. EXPECT_EQ(2 SECS, controller.GetDelay(clock_.get(), 20 MB));
  51. clock_->now_micros_ += 2 SECS; // pay the "debt"
  52. auto delay_token_1 = controller.GetDelayToken(2 MBPS);
  53. EXPECT_EQ(10 SECS, controller.GetDelay(clock_.get(), 20 MB));
  54. clock_->now_micros_ += 10 SECS; // pay the "debt"
  55. auto delay_token_2 = controller.GetDelayToken(1 MBPS);
  56. EXPECT_EQ(20 SECS, controller.GetDelay(clock_.get(), 20 MB));
  57. clock_->now_micros_ += 20 SECS; // pay the "debt"
  58. auto delay_token_3 = controller.GetDelayToken(20 MBPS);
  59. EXPECT_EQ(1 SECS, controller.GetDelay(clock_.get(), 20 MB));
  60. clock_->now_micros_ += 1 SECS; // pay the "debt"
  61. // 60M is more than the max rate of 40M. Max rate will be used.
  62. EXPECT_EQ(controller.delayed_write_rate(), 20 MBPS);
  63. auto delay_token_4 =
  64. controller.GetDelayToken(controller.delayed_write_rate() * 3);
  65. EXPECT_EQ(controller.delayed_write_rate(), 40 MBPS);
  66. EXPECT_EQ(static_cast<uint64_t>(0.5 SECS),
  67. controller.GetDelay(clock_.get(), 20 MB));
  68. EXPECT_FALSE(controller.IsStopped());
  69. EXPECT_TRUE(controller.NeedsDelay());
  70. // Test stop tokens
  71. {
  72. auto stop_token_1 = controller.GetStopToken();
  73. EXPECT_TRUE(controller.IsStopped());
  74. EXPECT_EQ(0, controller.GetDelay(clock_.get(), 100 MB));
  75. {
  76. auto stop_token_2 = controller.GetStopToken();
  77. EXPECT_TRUE(controller.IsStopped());
  78. EXPECT_EQ(0, controller.GetDelay(clock_.get(), 100 MB));
  79. }
  80. EXPECT_TRUE(controller.IsStopped());
  81. EXPECT_EQ(0, controller.GetDelay(clock_.get(), 100 MB));
  82. }
  83. // Stop tokens released
  84. EXPECT_FALSE(controller.IsStopped());
  85. EXPECT_TRUE(controller.NeedsDelay());
  86. EXPECT_EQ(controller.delayed_write_rate(), 40 MBPS);
  87. // pay the previous "debt"
  88. clock_->now_micros_ += static_cast<uint64_t>(0.5 SECS);
  89. EXPECT_EQ(1 SECS, controller.GetDelay(clock_.get(), 40 MB));
  90. }
  91. // Delay tokens released
  92. EXPECT_FALSE(controller.NeedsDelay());
  93. }
  94. TEST_F(WriteControllerTest, StartFilled) {
  95. WriteController controller(10 MBPS);
  96. // Attempt to write two things that combined would be allowed within
  97. // a single refill interval
  98. auto delay_token_0 =
  99. controller.GetDelayToken(controller.delayed_write_rate());
  100. // Verify no delay because write rate has not been exceeded within
  101. // refill interval.
  102. EXPECT_EQ(0U, controller.GetDelay(clock_.get(), 2000u /*bytes*/));
  103. EXPECT_EQ(0U, controller.GetDelay(clock_.get(), 2000u /*bytes*/));
  104. // Allow refill (kMicrosPerRefill)
  105. clock_->now_micros_ += 1000;
  106. // Again
  107. EXPECT_EQ(0U, controller.GetDelay(clock_.get(), 2000u /*bytes*/));
  108. EXPECT_EQ(0U, controller.GetDelay(clock_.get(), 2000u /*bytes*/));
  109. // Control: something bigger that would exceed write rate within interval
  110. uint64_t delay = controller.GetDelay(clock_.get(), 10 MB);
  111. EXPECT_GT(1.0 * delay, 0.999 SECS);
  112. EXPECT_LT(1.0 * delay, 1.001 SECS);
  113. }
  114. TEST_F(WriteControllerTest, DebtAccumulation) {
  115. WriteController controller(10 MBPS);
  116. std::array<std::unique_ptr<WriteControllerToken>, 10> tokens;
  117. // Accumulate a time delay debt with no passage of time, like many column
  118. // families delaying writes simultaneously. (Old versions of WriteController
  119. // would reset the debt on every GetDelayToken.)
  120. uint64_t debt = 0;
  121. for (unsigned i = 0; i < tokens.size(); ++i) {
  122. tokens[i] = controller.GetDelayToken((i + 1u) MBPS);
  123. uint64_t delay = controller.GetDelay(clock_.get(), 63 MB);
  124. ASSERT_GT(delay, debt);
  125. uint64_t incremental = delay - debt;
  126. ASSERT_EQ(incremental, (63 SECS) / (i + 1u));
  127. debt += incremental;
  128. }
  129. // Pay down the debt
  130. clock_->now_micros_ += debt;
  131. debt = 0;
  132. // Now accumulate debt with some passage of time.
  133. for (unsigned i = 0; i < tokens.size(); ++i) {
  134. // Debt is accumulated in time, not in bytes, so this new write
  135. // limit is not applied to prior requested delays, even it they are
  136. // in progress.
  137. tokens[i] = controller.GetDelayToken((i + 1u) MBPS);
  138. uint64_t delay = controller.GetDelay(clock_.get(), 63 MB);
  139. ASSERT_GT(delay, debt);
  140. uint64_t incremental = delay - debt;
  141. ASSERT_EQ(incremental, (63 SECS) / (i + 1u));
  142. debt += incremental;
  143. uint64_t credit = debt / 2;
  144. clock_->now_micros_ += credit;
  145. debt -= credit;
  146. }
  147. // Pay down the debt
  148. clock_->now_micros_ += debt;
  149. debt = 0; // consistent state
  150. (void)debt; // appease clang-analyze
  151. // Verify paid down
  152. EXPECT_EQ(0U, controller.GetDelay(clock_.get(), 100u /*small bytes*/));
  153. // Accumulate another debt, without accounting, and releasing tokens
  154. for (unsigned i = 0; i < tokens.size(); ++i) {
  155. // Big and small are delayed
  156. ASSERT_LT(0U, controller.GetDelay(clock_.get(), 63 MB));
  157. ASSERT_LT(0U, controller.GetDelay(clock_.get(), 100u /*small bytes*/));
  158. tokens[i].reset();
  159. }
  160. // All tokens released.
  161. // Verify that releasing all tokens pays down debt, even with no time passage.
  162. tokens[0] = controller.GetDelayToken(1 MBPS);
  163. ASSERT_EQ(0U, controller.GetDelay(clock_.get(), 100u /*small bytes*/));
  164. }
  165. // This may or may not be a "good" feature, but it's an old feature
  166. TEST_F(WriteControllerTest, CreditAccumulation) {
  167. WriteController controller(10 MBPS);
  168. std::array<std::unique_ptr<WriteControllerToken>, 10> tokens;
  169. // Ensure started
  170. tokens[0] = controller.GetDelayToken(1 MBPS);
  171. ASSERT_EQ(10 SECS, controller.GetDelay(clock_.get(), 10 MB));
  172. clock_->now_micros_ += 10 SECS;
  173. // Accumulate a credit
  174. uint64_t credit = 1000 SECS /* see below: * 1 MB / 1 SEC */;
  175. clock_->now_micros_ += credit;
  176. // Spend some credit (burst of I/O)
  177. for (unsigned i = 0; i < tokens.size(); ++i) {
  178. tokens[i] = controller.GetDelayToken((i + 1u) MBPS);
  179. ASSERT_EQ(0U, controller.GetDelay(clock_.get(), 63 MB));
  180. // In WriteController, credit is accumulated in bytes, not in time.
  181. // After an "unnecessary" delay, all of our time credit will be
  182. // translated to bytes on the next operation, in this case with
  183. // setting 1 MBPS. So regardless of the rate at delay time, we just
  184. // account for the bytes.
  185. credit -= 63 MB;
  186. }
  187. // Spend remaining credit
  188. tokens[0] = controller.GetDelayToken(1 MBPS);
  189. ASSERT_EQ(0U, controller.GetDelay(clock_.get(), credit));
  190. // Verify
  191. ASSERT_EQ(10 SECS, controller.GetDelay(clock_.get(), 10 MB));
  192. clock_->now_micros_ += 10 SECS;
  193. // Accumulate a credit, no accounting
  194. clock_->now_micros_ += 1000 SECS;
  195. // Spend a small amount, releasing tokens
  196. for (unsigned i = 0; i < tokens.size(); ++i) {
  197. ASSERT_EQ(0U, controller.GetDelay(clock_.get(), 3 MB));
  198. tokens[i].reset();
  199. }
  200. // All tokens released.
  201. // Verify credit is wiped away on new delay.
  202. tokens[0] = controller.GetDelayToken(1 MBPS);
  203. ASSERT_EQ(10 SECS, controller.GetDelay(clock_.get(), 10 MB));
  204. }
  205. } // namespace ROCKSDB_NAMESPACE
  206. int main(int argc, char** argv) {
  207. ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
  208. ::testing::InitGoogleTest(&argc, argv);
  209. return RUN_ALL_TESTS();
  210. }