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