brpc_circuit_breaker_unittest.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. // brpc - A framework to host and access services throughout Baidu.
  18. // Date: 2018/09/19 14:51:06
  19. #include <pthread.h>
  20. #include <gtest/gtest.h>
  21. #include <gflags/gflags.h>
  22. #include "butil/macros.h"
  23. #include "bthread/bthread.h"
  24. #include "brpc/circuit_breaker.h"
  25. #include "brpc/socket.h"
  26. #include "brpc/server.h"
  27. #include "echo.pb.h"
  28. namespace {
  29. void initialize_random() {
  30. srand(time(0));
  31. }
  32. const int kShortWindowSize = 500;
  33. const int kLongWindowSize = 1000;
  34. const int kShortWindowErrorPercent = 10;
  35. const int kLongWindowErrorPercent = 5;
  36. const int kMinIsolationDurationMs = 10;
  37. const int kMaxIsolationDurationMs = 200;
  38. const int kErrorCodeForFailed = 131;
  39. const int kErrorCodeForSucc = 0;
  40. const int kErrorCost = 1000;
  41. const int kLatency = 1000;
  42. const int kThreadNum = 3;
  43. } // namespace
  44. namespace brpc {
  45. DECLARE_int32(circuit_breaker_short_window_size);
  46. DECLARE_int32(circuit_breaker_long_window_size);
  47. DECLARE_int32(circuit_breaker_short_window_error_percent);
  48. DECLARE_int32(circuit_breaker_long_window_error_percent);
  49. DECLARE_int32(circuit_breaker_min_isolation_duration_ms);
  50. DECLARE_int32(circuit_breaker_max_isolation_duration_ms);
  51. } // namespace brpc
  52. int main(int argc, char* argv[]) {
  53. brpc::FLAGS_circuit_breaker_short_window_size = kShortWindowSize;
  54. brpc::FLAGS_circuit_breaker_long_window_size = kLongWindowSize;
  55. brpc::FLAGS_circuit_breaker_short_window_error_percent = kShortWindowErrorPercent;
  56. brpc::FLAGS_circuit_breaker_long_window_error_percent = kLongWindowErrorPercent;
  57. brpc::FLAGS_circuit_breaker_min_isolation_duration_ms = kMinIsolationDurationMs;
  58. brpc::FLAGS_circuit_breaker_max_isolation_duration_ms = kMaxIsolationDurationMs;
  59. testing::InitGoogleTest(&argc, argv);
  60. GFLAGS_NS::ParseCommandLineFlags(&argc, &argv, true);
  61. return RUN_ALL_TESTS();
  62. }
  63. pthread_once_t initialize_random_control = PTHREAD_ONCE_INIT;
  64. struct FeedbackControl {
  65. FeedbackControl(int req_num, int error_percent,
  66. brpc::CircuitBreaker* circuit_breaker)
  67. : _req_num(req_num)
  68. , _error_percent(error_percent)
  69. , _circuit_breaker(circuit_breaker)
  70. , _healthy_cnt(0)
  71. , _unhealthy_cnt(0)
  72. , _healthy(true) {}
  73. int _req_num;
  74. int _error_percent;
  75. brpc::CircuitBreaker* _circuit_breaker;
  76. int _healthy_cnt;
  77. int _unhealthy_cnt;
  78. bool _healthy;
  79. };
  80. class CircuitBreakerTest : public ::testing::Test {
  81. protected:
  82. CircuitBreakerTest() {
  83. pthread_once(&initialize_random_control, initialize_random);
  84. };
  85. virtual ~CircuitBreakerTest() {};
  86. virtual void SetUp() {};
  87. virtual void TearDown() {};
  88. static void* feed_back_thread(void* data) {
  89. FeedbackControl* fc = static_cast<FeedbackControl*>(data);
  90. for (int i = 0; i < fc->_req_num; ++i) {
  91. bool healthy = false;
  92. if (rand() % 100 < fc->_error_percent) {
  93. healthy = fc->_circuit_breaker->OnCallEnd(kErrorCodeForFailed, kErrorCost);
  94. } else {
  95. healthy = fc->_circuit_breaker->OnCallEnd(kErrorCodeForSucc, kLatency);
  96. }
  97. fc->_healthy = healthy;
  98. if (healthy) {
  99. ++fc->_healthy_cnt;
  100. } else {
  101. ++fc->_unhealthy_cnt;
  102. }
  103. }
  104. return fc;
  105. }
  106. void StartFeedbackThread(std::vector<pthread_t>* thread_list,
  107. std::vector<std::unique_ptr<FeedbackControl>>* fc_list,
  108. int error_percent) {
  109. thread_list->clear();
  110. fc_list->clear();
  111. for (int i = 0; i < kThreadNum; ++i) {
  112. pthread_t tid = 0;
  113. FeedbackControl* fc =
  114. new FeedbackControl(2 * kLongWindowSize, error_percent, &_circuit_breaker);
  115. fc_list->emplace_back(fc);
  116. pthread_create(&tid, nullptr, feed_back_thread, fc);
  117. thread_list->push_back(tid);
  118. }
  119. }
  120. brpc::CircuitBreaker _circuit_breaker;
  121. };
  122. TEST_F(CircuitBreakerTest, should_not_isolate) {
  123. std::vector<pthread_t> thread_list;
  124. std::vector<std::unique_ptr<FeedbackControl>> fc_list;
  125. StartFeedbackThread(&thread_list, &fc_list, 3);
  126. for (int i = 0; i < kThreadNum; ++i) {
  127. void* ret_data = nullptr;
  128. ASSERT_EQ(pthread_join(thread_list[i], &ret_data), 0);
  129. FeedbackControl* fc = static_cast<FeedbackControl*>(ret_data);
  130. EXPECT_EQ(fc->_unhealthy_cnt, 0);
  131. EXPECT_TRUE(fc->_healthy);
  132. }
  133. }
  134. TEST_F(CircuitBreakerTest, should_isolate) {
  135. std::vector<pthread_t> thread_list;
  136. std::vector<std::unique_ptr<FeedbackControl>> fc_list;
  137. StartFeedbackThread(&thread_list, &fc_list, 50);
  138. for (int i = 0; i < kThreadNum; ++i) {
  139. void* ret_data = nullptr;
  140. ASSERT_EQ(pthread_join(thread_list[i], &ret_data), 0);
  141. FeedbackControl* fc = static_cast<FeedbackControl*>(ret_data);
  142. EXPECT_GT(fc->_unhealthy_cnt, 0);
  143. EXPECT_FALSE(fc->_healthy);
  144. }
  145. }
  146. TEST_F(CircuitBreakerTest, isolation_duration_grow_and_reset) {
  147. std::vector<pthread_t> thread_list;
  148. std::vector<std::unique_ptr<FeedbackControl>> fc_list;
  149. StartFeedbackThread(&thread_list, &fc_list, 100);
  150. for (int i = 0; i < kThreadNum; ++i) {
  151. void* ret_data = nullptr;
  152. ASSERT_EQ(pthread_join(thread_list[i], &ret_data), 0);
  153. FeedbackControl* fc = static_cast<FeedbackControl*>(ret_data);
  154. EXPECT_FALSE(fc->_healthy);
  155. EXPECT_LE(fc->_healthy_cnt, kShortWindowSize);
  156. EXPECT_GT(fc->_unhealthy_cnt, 0);
  157. }
  158. EXPECT_EQ(_circuit_breaker.isolation_duration_ms(), kMinIsolationDurationMs);
  159. _circuit_breaker.Reset();
  160. StartFeedbackThread(&thread_list, &fc_list, 100);
  161. for (int i = 0; i < kThreadNum; ++i) {
  162. void* ret_data = nullptr;
  163. ASSERT_EQ(pthread_join(thread_list[i], &ret_data), 0);
  164. FeedbackControl* fc = static_cast<FeedbackControl*>(ret_data);
  165. EXPECT_FALSE(fc->_healthy);
  166. EXPECT_LE(fc->_healthy_cnt, kShortWindowSize);
  167. EXPECT_GT(fc->_unhealthy_cnt, 0);
  168. }
  169. EXPECT_EQ(_circuit_breaker.isolation_duration_ms(), kMinIsolationDurationMs * 2);
  170. _circuit_breaker.Reset();
  171. StartFeedbackThread(&thread_list, &fc_list, 100);
  172. for (int i = 0; i < kThreadNum; ++i) {
  173. void* ret_data = nullptr;
  174. ASSERT_EQ(pthread_join(thread_list[i], &ret_data), 0);
  175. FeedbackControl* fc = static_cast<FeedbackControl*>(ret_data);
  176. EXPECT_FALSE(fc->_healthy);
  177. EXPECT_LE(fc->_healthy_cnt, kShortWindowSize);
  178. EXPECT_GT(fc->_unhealthy_cnt, 0);
  179. }
  180. EXPECT_EQ(_circuit_breaker.isolation_duration_ms(), kMinIsolationDurationMs * 4);
  181. _circuit_breaker.Reset();
  182. ::usleep((kMaxIsolationDurationMs + kMinIsolationDurationMs) * 1000);
  183. StartFeedbackThread(&thread_list, &fc_list, 100);
  184. for (int i = 0; i < kThreadNum; ++i) {
  185. void* ret_data = nullptr;
  186. ASSERT_EQ(pthread_join(thread_list[i], &ret_data), 0);
  187. FeedbackControl* fc = static_cast<FeedbackControl*>(ret_data);
  188. EXPECT_FALSE(fc->_healthy);
  189. EXPECT_LE(fc->_healthy_cnt, kShortWindowSize);
  190. EXPECT_GT(fc->_unhealthy_cnt, 0);
  191. }
  192. EXPECT_EQ(_circuit_breaker.isolation_duration_ms(), kMinIsolationDurationMs);
  193. }
  194. TEST_F(CircuitBreakerTest, maximum_isolation_duration) {
  195. brpc::FLAGS_circuit_breaker_max_isolation_duration_ms =
  196. brpc::FLAGS_circuit_breaker_min_isolation_duration_ms + 1;
  197. ASSERT_LT(brpc::FLAGS_circuit_breaker_max_isolation_duration_ms,
  198. 2 * brpc::FLAGS_circuit_breaker_min_isolation_duration_ms);
  199. std::vector<pthread_t> thread_list;
  200. std::vector<std::unique_ptr<FeedbackControl>> fc_list;
  201. _circuit_breaker.Reset();
  202. StartFeedbackThread(&thread_list, &fc_list, 100);
  203. for (int i = 0; i < kThreadNum; ++i) {
  204. void* ret_data = nullptr;
  205. ASSERT_EQ(pthread_join(thread_list[i], &ret_data), 0);
  206. FeedbackControl* fc = static_cast<FeedbackControl*>(ret_data);
  207. EXPECT_FALSE(fc->_healthy);
  208. EXPECT_LE(fc->_healthy_cnt, kShortWindowSize);
  209. EXPECT_GT(fc->_unhealthy_cnt, 0);
  210. }
  211. EXPECT_EQ(_circuit_breaker.isolation_duration_ms(),
  212. brpc::FLAGS_circuit_breaker_max_isolation_duration_ms);
  213. }