channel_trace_test.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. *
  3. * Copyright 2017 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * 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, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include "src/core/lib/channel/channel_trace.h"
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <gtest/gtest.h>
  22. #include <grpc/grpc_security.h>
  23. #include <grpc/support/alloc.h>
  24. #include <grpc/support/log.h>
  25. #include <grpc/support/string_util.h>
  26. #include "src/core/lib/channel/channelz.h"
  27. #include "src/core/lib/channel/channelz_registry.h"
  28. #include "src/core/lib/gpr/useful.h"
  29. #include "src/core/lib/iomgr/exec_ctx.h"
  30. #include "src/core/lib/json/json.h"
  31. #include "src/core/lib/surface/channel.h"
  32. #include "test/core/util/test_config.h"
  33. #include "test/cpp/util/channel_trace_proto_helper.h"
  34. namespace grpc_core {
  35. namespace channelz {
  36. namespace testing {
  37. // testing peer to access channel internals
  38. class ChannelNodePeer {
  39. public:
  40. explicit ChannelNodePeer(ChannelNode* node) : node_(node) {}
  41. ChannelTrace* trace() const { return &node_->trace_; }
  42. private:
  43. ChannelNode* node_;
  44. };
  45. size_t GetSizeofTraceEvent() { return sizeof(ChannelTrace::TraceEvent); }
  46. namespace {
  47. void ValidateJsonArraySize(const Json& array, size_t expected) {
  48. if (expected == 0) {
  49. ASSERT_EQ(array.type(), Json::Type::JSON_NULL);
  50. } else {
  51. ASSERT_EQ(array.type(), Json::Type::ARRAY);
  52. EXPECT_EQ(array.array_value().size(), expected);
  53. }
  54. }
  55. void ValidateChannelTraceData(const Json& json,
  56. size_t num_events_logged_expected,
  57. size_t actual_num_events_expected) {
  58. ASSERT_EQ(json.type(), Json::Type::OBJECT);
  59. Json::Object object = json.object_value();
  60. Json& num_events_logged_json = object["numEventsLogged"];
  61. ASSERT_EQ(num_events_logged_json.type(), Json::Type::STRING);
  62. size_t num_events_logged = static_cast<size_t>(
  63. strtol(num_events_logged_json.string_value().c_str(), nullptr, 0));
  64. ASSERT_EQ(num_events_logged, num_events_logged_expected);
  65. Json& start_time_json = object["creationTimestamp"];
  66. ASSERT_EQ(start_time_json.type(), Json::Type::STRING);
  67. ValidateJsonArraySize(object["events"], actual_num_events_expected);
  68. }
  69. void AddSimpleTrace(ChannelTrace* tracer) {
  70. tracer->AddTraceEvent(ChannelTrace::Severity::Info,
  71. grpc_slice_from_static_string("simple trace"));
  72. }
  73. // checks for the existence of all the required members of the tracer.
  74. void ValidateChannelTraceCustom(ChannelTrace* tracer, size_t num_events_logged,
  75. size_t num_events_expected) {
  76. Json json = tracer->RenderJson();
  77. ASSERT_EQ(json.type(), Json::Type::OBJECT);
  78. std::string json_str = json.Dump();
  79. grpc::testing::ValidateChannelTraceProtoJsonTranslation(json_str.c_str());
  80. ValidateChannelTraceData(json, num_events_logged, num_events_expected);
  81. }
  82. void ValidateChannelTrace(ChannelTrace* tracer, size_t num_events_logged) {
  83. ValidateChannelTraceCustom(tracer, num_events_logged, num_events_logged);
  84. }
  85. class ChannelFixture {
  86. public:
  87. explicit ChannelFixture(int max_tracer_event_memory) {
  88. grpc_arg client_a = grpc_channel_arg_integer_create(
  89. const_cast<char*>(GRPC_ARG_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE),
  90. max_tracer_event_memory);
  91. grpc_channel_args client_args = {1, &client_a};
  92. grpc_channel_credentials* creds = grpc_insecure_credentials_create();
  93. channel_ = grpc_channel_create("fake_target", creds, &client_args);
  94. grpc_channel_credentials_release(creds);
  95. }
  96. ~ChannelFixture() { grpc_channel_destroy(channel_); }
  97. grpc_channel* channel() { return channel_; }
  98. private:
  99. grpc_channel* channel_;
  100. };
  101. } // anonymous namespace
  102. const int kEventListMemoryLimit = 1024 * 1024;
  103. // Tests basic ChannelTrace functionality like construction, adding trace, and
  104. // lookups by uuid.
  105. TEST(ChannelTracerTest, BasicTest) {
  106. ExecCtx exec_ctx;
  107. ChannelTrace tracer(kEventListMemoryLimit);
  108. AddSimpleTrace(&tracer);
  109. AddSimpleTrace(&tracer);
  110. tracer.AddTraceEvent(ChannelTrace::Severity::Info,
  111. grpc_slice_from_static_string("trace three"));
  112. tracer.AddTraceEvent(ChannelTrace::Severity::Error,
  113. grpc_slice_from_static_string("trace four error"));
  114. ValidateChannelTrace(&tracer, 4);
  115. AddSimpleTrace(&tracer);
  116. AddSimpleTrace(&tracer);
  117. ValidateChannelTrace(&tracer, 6);
  118. AddSimpleTrace(&tracer);
  119. AddSimpleTrace(&tracer);
  120. AddSimpleTrace(&tracer);
  121. AddSimpleTrace(&tracer);
  122. ValidateChannelTrace(&tracer, 10);
  123. }
  124. // Tests more complex functionality, like a parent channel tracking
  125. // subchannles. This exercises the ref/unref patterns since the parent tracer
  126. // and this function will both hold refs to the subchannel.
  127. TEST(ChannelTracerTest, ComplexTest) {
  128. ExecCtx exec_ctx;
  129. ChannelTrace tracer(kEventListMemoryLimit);
  130. AddSimpleTrace(&tracer);
  131. AddSimpleTrace(&tracer);
  132. ChannelFixture channel1(kEventListMemoryLimit);
  133. RefCountedPtr<ChannelNode> sc1 =
  134. MakeRefCounted<ChannelNode>("fake_target", kEventListMemoryLimit, 0);
  135. ChannelNodePeer sc1_peer(sc1.get());
  136. tracer.AddTraceEventWithReference(
  137. ChannelTrace::Severity::Info,
  138. grpc_slice_from_static_string("subchannel one created"), sc1);
  139. ValidateChannelTrace(&tracer, 3);
  140. AddSimpleTrace(sc1_peer.trace());
  141. AddSimpleTrace(sc1_peer.trace());
  142. AddSimpleTrace(sc1_peer.trace());
  143. ValidateChannelTrace(sc1_peer.trace(), 3);
  144. AddSimpleTrace(sc1_peer.trace());
  145. AddSimpleTrace(sc1_peer.trace());
  146. AddSimpleTrace(sc1_peer.trace());
  147. ValidateChannelTrace(sc1_peer.trace(), 6);
  148. AddSimpleTrace(&tracer);
  149. AddSimpleTrace(&tracer);
  150. ValidateChannelTrace(&tracer, 5);
  151. ChannelFixture channel2(kEventListMemoryLimit);
  152. RefCountedPtr<ChannelNode> sc2 =
  153. MakeRefCounted<ChannelNode>("fake_target", kEventListMemoryLimit, 0);
  154. tracer.AddTraceEventWithReference(
  155. ChannelTrace::Severity::Info,
  156. grpc_slice_from_static_string("LB channel two created"), sc2);
  157. tracer.AddTraceEventWithReference(
  158. ChannelTrace::Severity::Warning,
  159. grpc_slice_from_static_string("subchannel one inactive"), sc1);
  160. ValidateChannelTrace(&tracer, 7);
  161. AddSimpleTrace(&tracer);
  162. AddSimpleTrace(&tracer);
  163. AddSimpleTrace(&tracer);
  164. AddSimpleTrace(&tracer);
  165. AddSimpleTrace(&tracer);
  166. AddSimpleTrace(&tracer);
  167. sc1.reset();
  168. sc2.reset();
  169. }
  170. // Test a case in which the parent channel has subchannels and the subchannels
  171. // have connections. Ensures that everything lives as long as it should then
  172. // gets deleted.
  173. TEST(ChannelTracerTest, TestNesting) {
  174. ExecCtx exec_ctx;
  175. ChannelTrace tracer(kEventListMemoryLimit);
  176. AddSimpleTrace(&tracer);
  177. AddSimpleTrace(&tracer);
  178. ValidateChannelTrace(&tracer, 2);
  179. ChannelFixture channel1(kEventListMemoryLimit);
  180. RefCountedPtr<ChannelNode> sc1 =
  181. MakeRefCounted<ChannelNode>("fake_target", kEventListMemoryLimit, 0);
  182. ChannelNodePeer sc1_peer(sc1.get());
  183. tracer.AddTraceEventWithReference(
  184. ChannelTrace::Severity::Info,
  185. grpc_slice_from_static_string("subchannel one created"), sc1);
  186. ValidateChannelTrace(&tracer, 3);
  187. AddSimpleTrace(sc1_peer.trace());
  188. ChannelFixture channel2(kEventListMemoryLimit);
  189. RefCountedPtr<ChannelNode> conn1 =
  190. MakeRefCounted<ChannelNode>("fake_target", kEventListMemoryLimit, 0);
  191. ChannelNodePeer conn1_peer(conn1.get());
  192. // nesting one level deeper.
  193. sc1_peer.trace()->AddTraceEventWithReference(
  194. ChannelTrace::Severity::Info,
  195. grpc_slice_from_static_string("connection one created"), conn1);
  196. ValidateChannelTrace(&tracer, 3);
  197. AddSimpleTrace(conn1_peer.trace());
  198. AddSimpleTrace(&tracer);
  199. AddSimpleTrace(&tracer);
  200. ValidateChannelTrace(&tracer, 5);
  201. ValidateChannelTrace(conn1_peer.trace(), 1);
  202. ChannelFixture channel3(kEventListMemoryLimit);
  203. RefCountedPtr<ChannelNode> sc2 =
  204. MakeRefCounted<ChannelNode>("fake_target", kEventListMemoryLimit, 0);
  205. tracer.AddTraceEventWithReference(
  206. ChannelTrace::Severity::Info,
  207. grpc_slice_from_static_string("subchannel two created"), sc2);
  208. // this trace should not get added to the parents children since it is already
  209. // present in the tracer.
  210. tracer.AddTraceEventWithReference(
  211. ChannelTrace::Severity::Warning,
  212. grpc_slice_from_static_string("subchannel one inactive"), sc1);
  213. AddSimpleTrace(&tracer);
  214. ValidateChannelTrace(&tracer, 8);
  215. sc1.reset();
  216. sc2.reset();
  217. conn1.reset();
  218. }
  219. TEST(ChannelTracerTest, TestSmallMemoryLimit) {
  220. ExecCtx exec_ctx;
  221. // doesn't make sense, but serves a testing purpose for the channel tracing
  222. // bookkeeping. All tracing events added should will get immediately garbage
  223. // collected.
  224. const int kSmallMemoryLimit = 1;
  225. ChannelTrace tracer(kSmallMemoryLimit);
  226. AddSimpleTrace(&tracer);
  227. AddSimpleTrace(&tracer);
  228. tracer.AddTraceEvent(ChannelTrace::Severity::Info,
  229. grpc_slice_from_static_string("trace three"));
  230. tracer.AddTraceEvent(ChannelTrace::Severity::Error,
  231. grpc_slice_from_static_string("trace four error"));
  232. ValidateChannelTraceCustom(&tracer, 4, 0);
  233. AddSimpleTrace(&tracer);
  234. AddSimpleTrace(&tracer);
  235. ValidateChannelTraceCustom(&tracer, 6, 0);
  236. AddSimpleTrace(&tracer);
  237. AddSimpleTrace(&tracer);
  238. AddSimpleTrace(&tracer);
  239. AddSimpleTrace(&tracer);
  240. ValidateChannelTraceCustom(&tracer, 10, 0);
  241. }
  242. TEST(ChannelTracerTest, TestEviction) {
  243. ExecCtx exec_ctx;
  244. const int kTraceEventSize = GetSizeofTraceEvent();
  245. const int kNumEvents = 5;
  246. ChannelTrace tracer(kTraceEventSize * kNumEvents);
  247. for (int i = 1; i <= kNumEvents; ++i) {
  248. AddSimpleTrace(&tracer);
  249. ValidateChannelTrace(&tracer, i);
  250. }
  251. // at this point the list is full, and each subsequent enntry will cause an
  252. // eviction.
  253. for (int i = 1; i <= kNumEvents; ++i) {
  254. AddSimpleTrace(&tracer);
  255. ValidateChannelTraceCustom(&tracer, kNumEvents + i, kNumEvents);
  256. }
  257. }
  258. TEST(ChannelTracerTest, TestMultipleEviction) {
  259. ExecCtx exec_ctx;
  260. const int kTraceEventSize = GetSizeofTraceEvent();
  261. const int kNumEvents = 5;
  262. ChannelTrace tracer(kTraceEventSize * kNumEvents);
  263. for (int i = 1; i <= kNumEvents; ++i) {
  264. AddSimpleTrace(&tracer);
  265. ValidateChannelTrace(&tracer, i);
  266. }
  267. // at this point the list is full, and each subsequent enntry will cause an
  268. // eviction. We will now add in a trace event that has a copied string. This
  269. // uses more memory, so it will cause a double eviciction
  270. tracer.AddTraceEvent(
  271. ChannelTrace::Severity::Info,
  272. grpc_slice_from_copied_string(
  273. "long enough string to trigger a multiple eviction"));
  274. ValidateChannelTraceCustom(&tracer, kNumEvents + 1, kNumEvents - 1);
  275. }
  276. TEST(ChannelTracerTest, TestTotalEviction) {
  277. ExecCtx exec_ctx;
  278. const int kTraceEventSize = GetSizeofTraceEvent();
  279. const int kNumEvents = 5;
  280. ChannelTrace tracer(kTraceEventSize * kNumEvents);
  281. for (int i = 1; i <= kNumEvents; ++i) {
  282. AddSimpleTrace(&tracer);
  283. ValidateChannelTrace(&tracer, i);
  284. }
  285. // at this point the list is full. Now we add such a big slice that
  286. // everything gets evicted.
  287. grpc_slice huge_slice = grpc_slice_malloc(kTraceEventSize * (kNumEvents + 1));
  288. tracer.AddTraceEvent(ChannelTrace::Severity::Info, huge_slice);
  289. ValidateChannelTraceCustom(&tracer, kNumEvents + 1, 0);
  290. }
  291. } // namespace testing
  292. } // namespace channelz
  293. } // namespace grpc_core
  294. int main(int argc, char** argv) {
  295. grpc::testing::TestEnvironment env(argc, argv);
  296. grpc_init();
  297. ::testing::InitGoogleTest(&argc, argv);
  298. int ret = RUN_ALL_TESTS();
  299. grpc_shutdown();
  300. return ret;
  301. }