| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2018 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 <grpc/impl/codegen/port_platform.h> |
| 20 |
|
| 21 |
#include "src/cpp/server/load_reporter/load_data_store.h" |
| 22 |
|
| 23 |
#include <set> |
| 24 |
#include <vector> |
| 25 |
|
| 26 |
#include <gtest/gtest.h> |
| 27 |
|
| 28 |
#include <grpc/grpc.h> |
| 29 |
|
| 30 |
#include "src/cpp/server/load_reporter/constants.h" |
| 31 |
#include "test/core/util/port.h" |
| 32 |
#include "test/core/util/test_config.h" |
| 33 |
|
| 34 |
namespace grpc { |
| 35 |
namespace testing { |
| 36 |
namespace { |
| 37 |
|
| 38 |
using ::grpc::load_reporter::CallMetricValue; |
| 39 |
using ::grpc::load_reporter::kInvalidLbId; |
| 40 |
using ::grpc::load_reporter::LoadDataStore; |
| 41 |
using ::grpc::load_reporter::LoadRecordKey; |
| 42 |
using ::grpc::load_reporter::LoadRecordValue; |
| 43 |
using ::grpc::load_reporter::PerBalancerStore; |
| 44 |
|
| 45 |
class LoadDataStoreTest : public ::testing::Test { |
| 46 |
public: |
| 47 |
LoadDataStoreTest() |
| 48 |
: kKey1(kLbId1, kLbTag1, kUser1, kClientIp1), |
| 49 |
kKey2(kLbId2, kLbTag2, kUser2, kClientIp2) {} |
| 50 |
|
| 51 |
// Check whether per_balancer_stores contains a store which was originally |
| 52 |
// created for <hostname, lb_id, and load_key>. |
| 53 |
bool PerBalancerStoresContains( |
| 54 |
const LoadDataStore& load_data_store, |
| 55 |
const std::set<PerBalancerStore*>* per_balancer_stores, |
| 56 |
const std::string& hostname, const std::string& lb_id, |
| 57 |
const std::string& load_key) { |
| 58 |
auto original_per_balancer_store = |
| 59 |
load_data_store.FindPerBalancerStore(hostname, lb_id); |
| 60 |
EXPECT_NE(original_per_balancer_store, nullptr); |
| 61 |
EXPECT_EQ(original_per_balancer_store->lb_id(), lb_id); |
| 62 |
EXPECT_EQ(original_per_balancer_store->load_key(), load_key); |
| 63 |
for (auto per_balancer_store : *per_balancer_stores) { |
| 64 |
if (per_balancer_store == original_per_balancer_store) { |
| 65 |
return true; |
| 66 |
} |
| 67 |
} |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
std::string FormatLbId(size_t index) { |
| 72 |
return "kLbId" + std::to_string(index); |
| 73 |
} |
| 74 |
|
| 75 |
const std::string kHostname1 = "kHostname1"; |
| 76 |
const std::string kHostname2 = "kHostname2"; |
| 77 |
const std::string kLbId1 = "kLbId1"; |
| 78 |
const std::string kLbId2 = "kLbId2"; |
| 79 |
const std::string kLbId3 = "kLbId3"; |
| 80 |
const std::string kLbId4 = "kLbId4"; |
| 81 |
const std::string kLoadKey1 = "kLoadKey1"; |
| 82 |
const std::string kLoadKey2 = "kLoadKey2"; |
| 83 |
const std::string kLbTag1 = "kLbTag1"; |
| 84 |
const std::string kLbTag2 = "kLbTag2"; |
| 85 |
const std::string kUser1 = "kUser1"; |
| 86 |
const std::string kUser2 = "kUser2"; |
| 87 |
const std::string kClientIp1 = "00"; |
| 88 |
const std::string kClientIp2 = "02"; |
| 89 |
const std::string kMetric1 = "kMetric1"; |
| 90 |
const std::string kMetric2 = "kMetric2"; |
| 91 |
const LoadRecordKey kKey1; |
| 92 |
const LoadRecordKey kKey2; |
| 93 |
}; |
| 94 |
|
| 95 |
using PerBalancerStoreTest = LoadDataStoreTest; |
| 96 |
|
| 97 |
TEST_F(LoadDataStoreTest, AssignToSelf) { |
| 98 |
LoadDataStore load_data_store; |
| 99 |
load_data_store.ReportStreamCreated(kHostname1, kLbId1, kLoadKey1); |
| 100 |
auto assigned_stores = load_data_store.GetAssignedStores(kHostname1, kLbId1); |
| 101 |
EXPECT_TRUE(PerBalancerStoresContains(load_data_store, assigned_stores, |
| 102 |
kHostname1, kLbId1, kLoadKey1)); |
| 103 |
} |
| 104 |
|
| 105 |
TEST_F(LoadDataStoreTest, ReassignOrphanStores) { |
| 106 |
LoadDataStore load_data_store; |
| 107 |
load_data_store.ReportStreamCreated(kHostname1, kLbId1, kLoadKey1); |
| 108 |
load_data_store.ReportStreamCreated(kHostname1, kLbId2, kLoadKey1); |
| 109 |
load_data_store.ReportStreamCreated(kHostname1, kLbId3, kLoadKey2); |
| 110 |
load_data_store.ReportStreamCreated(kHostname2, kLbId4, kLoadKey1); |
| 111 |
// 1. Close the second stream. |
| 112 |
load_data_store.ReportStreamClosed(kHostname1, kLbId2); |
| 113 |
auto assigned_to_lb_id_1 = |
| 114 |
load_data_store.GetAssignedStores(kHostname1, kLbId1); |
| 115 |
// The orphaned store is re-assigned to kLbId1 with the same load key. |
| 116 |
EXPECT_TRUE(PerBalancerStoresContains(load_data_store, assigned_to_lb_id_1, |
| 117 |
kHostname1, kLbId1, kLoadKey1)); |
| 118 |
EXPECT_TRUE(PerBalancerStoresContains(load_data_store, assigned_to_lb_id_1, |
| 119 |
kHostname1, kLbId2, kLoadKey1)); |
| 120 |
// 2. Close the first stream. |
| 121 |
load_data_store.ReportStreamClosed(kHostname1, kLbId1); |
| 122 |
auto assigned_to_lb_id_3 = |
| 123 |
load_data_store.GetAssignedStores(kHostname1, kLbId3); |
| 124 |
// The orphaned stores are re-assigned to kLbId3 with the same host, |
| 125 |
// because there isn't any LB with the same load key. |
| 126 |
EXPECT_TRUE(PerBalancerStoresContains(load_data_store, assigned_to_lb_id_3, |
| 127 |
kHostname1, kLbId1, kLoadKey1)); |
| 128 |
EXPECT_TRUE(PerBalancerStoresContains(load_data_store, assigned_to_lb_id_3, |
| 129 |
kHostname1, kLbId2, kLoadKey1)); |
| 130 |
EXPECT_TRUE(PerBalancerStoresContains(load_data_store, assigned_to_lb_id_3, |
| 131 |
kHostname1, kLbId3, kLoadKey2)); |
| 132 |
// 3. Close the third stream. |
| 133 |
load_data_store.ReportStreamClosed(kHostname1, kLbId3); |
| 134 |
auto assigned_to_lb_id_4 = |
| 135 |
load_data_store.GetAssignedStores(kHostname2, kLbId4); |
| 136 |
// There is no active LB for the first host now. kLbId4 is active but |
| 137 |
// it's for the second host, so it wll NOT adopt the orphaned stores. |
| 138 |
EXPECT_FALSE(PerBalancerStoresContains(load_data_store, assigned_to_lb_id_4, |
| 139 |
kHostname1, kLbId1, kLoadKey1)); |
| 140 |
EXPECT_FALSE(PerBalancerStoresContains(load_data_store, assigned_to_lb_id_4, |
| 141 |
kHostname1, kLbId2, kLoadKey1)); |
| 142 |
EXPECT_FALSE(PerBalancerStoresContains(load_data_store, assigned_to_lb_id_4, |
| 143 |
kHostname1, kLbId3, kLoadKey2)); |
| 144 |
EXPECT_TRUE(PerBalancerStoresContains(load_data_store, assigned_to_lb_id_4, |
| 145 |
kHostname2, kLbId4, kLoadKey1)); |
| 146 |
} |
| 147 |
|
| 148 |
TEST_F(LoadDataStoreTest, OrphanAssignmentIsSticky) { |
| 149 |
LoadDataStore load_data_store; |
| 150 |
std::set<std::string> active_lb_ids; |
| 151 |
size_t num_lb_ids = 1000; |
| 152 |
for (size_t i = 0; i < num_lb_ids; ++i) { |
| 153 |
load_data_store.ReportStreamCreated(kHostname1, FormatLbId(i), kLoadKey1); |
| 154 |
active_lb_ids.insert(FormatLbId(i)); |
| 155 |
} |
| 156 |
std::string orphaned_lb_id = FormatLbId(std::rand() % num_lb_ids); |
| 157 |
load_data_store.ReportStreamClosed(kHostname1, orphaned_lb_id); |
| 158 |
active_lb_ids.erase(orphaned_lb_id); |
| 159 |
// Find which LB is assigned the orphaned store. |
| 160 |
std::string assigned_lb_id = ""; |
| 161 |
for (const auto& lb_id : active_lb_ids) { |
| 162 |
if (PerBalancerStoresContains( |
| 163 |
load_data_store, |
| 164 |
load_data_store.GetAssignedStores(kHostname1, lb_id), kHostname1, |
| 165 |
orphaned_lb_id, kLoadKey1)) { |
| 166 |
assigned_lb_id = lb_id; |
| 167 |
break; |
| 168 |
} |
| 169 |
} |
| 170 |
EXPECT_STRNE(assigned_lb_id.c_str(), ""); |
| 171 |
// Close 10 more stream, skipping the assigned_lb_id. The assignment of |
| 172 |
// orphaned_lb_id shouldn't change. |
| 173 |
for (size_t _ = 0; _ < 10; ++_) { |
| 174 |
std::string lb_id_to_close = ""; |
| 175 |
for (const auto& lb_id : active_lb_ids) { |
| 176 |
if (lb_id != assigned_lb_id) { |
| 177 |
lb_id_to_close = lb_id; |
| 178 |
break; |
| 179 |
} |
| 180 |
} |
| 181 |
EXPECT_STRNE(lb_id_to_close.c_str(), ""); |
| 182 |
load_data_store.ReportStreamClosed(kHostname1, lb_id_to_close); |
| 183 |
active_lb_ids.erase(lb_id_to_close); |
| 184 |
EXPECT_TRUE(PerBalancerStoresContains( |
| 185 |
load_data_store, |
| 186 |
load_data_store.GetAssignedStores(kHostname1, assigned_lb_id), |
| 187 |
kHostname1, orphaned_lb_id, kLoadKey1)); |
| 188 |
} |
| 189 |
// Close the assigned_lb_id, orphaned_lb_id will be re-assigned again. |
| 190 |
load_data_store.ReportStreamClosed(kHostname1, assigned_lb_id); |
| 191 |
active_lb_ids.erase(assigned_lb_id); |
| 192 |
size_t orphaned_lb_id_occurences = 0; |
| 193 |
for (const auto& lb_id : active_lb_ids) { |
| 194 |
if (PerBalancerStoresContains( |
| 195 |
load_data_store, |
| 196 |
load_data_store.GetAssignedStores(kHostname1, lb_id), kHostname1, |
| 197 |
orphaned_lb_id, kLoadKey1)) { |
| 198 |
orphaned_lb_id_occurences++; |
| 199 |
} |
| 200 |
} |
| 201 |
EXPECT_EQ(orphaned_lb_id_occurences, 1U); |
| 202 |
} |
| 203 |
|
| 204 |
TEST_F(LoadDataStoreTest, HostTemporarilyLoseAllStreams) { |
| 205 |
LoadDataStore load_data_store; |
| 206 |
load_data_store.ReportStreamCreated(kHostname1, kLbId1, kLoadKey1); |
| 207 |
load_data_store.ReportStreamCreated(kHostname2, kLbId2, kLoadKey1); |
| 208 |
auto store_lb_id_1 = load_data_store.FindPerBalancerStore(kHostname1, kLbId1); |
| 209 |
auto store_invalid_lb_id_1 = |
| 210 |
load_data_store.FindPerBalancerStore(kHostname1, kInvalidLbId); |
| 211 |
EXPECT_FALSE(store_lb_id_1->IsSuspended()); |
| 212 |
EXPECT_FALSE(store_invalid_lb_id_1->IsSuspended()); |
| 213 |
// Disconnect all the streams of the first host. |
| 214 |
load_data_store.ReportStreamClosed(kHostname1, kLbId1); |
| 215 |
// All the streams of that host are suspended. |
| 216 |
EXPECT_TRUE(store_lb_id_1->IsSuspended()); |
| 217 |
EXPECT_TRUE(store_invalid_lb_id_1->IsSuspended()); |
| 218 |
// Detailed load data won't be kept when the PerBalancerStore is suspended. |
| 219 |
store_lb_id_1->MergeRow(kKey1, LoadRecordValue()); |
| 220 |
store_invalid_lb_id_1->MergeRow(kKey1, LoadRecordValue()); |
| 221 |
EXPECT_EQ(store_lb_id_1->load_record_map().size(), 0U); |
| 222 |
EXPECT_EQ(store_invalid_lb_id_1->load_record_map().size(), 0U); |
| 223 |
// The stores for different hosts won't mix, even if the load key is the same. |
| 224 |
auto assigned_to_lb_id_2 = |
| 225 |
load_data_store.GetAssignedStores(kHostname2, kLbId2); |
| 226 |
EXPECT_EQ(assigned_to_lb_id_2->size(), 2U); |
| 227 |
EXPECT_TRUE(PerBalancerStoresContains(load_data_store, assigned_to_lb_id_2, |
| 228 |
kHostname2, kLbId2, kLoadKey1)); |
| 229 |
EXPECT_TRUE(PerBalancerStoresContains(load_data_store, assigned_to_lb_id_2, |
| 230 |
kHostname2, kInvalidLbId, "")); |
| 231 |
// A new stream is created for the first host. |
| 232 |
load_data_store.ReportStreamCreated(kHostname1, kLbId3, kLoadKey2); |
| 233 |
// The stores for the first host are resumed. |
| 234 |
EXPECT_FALSE(store_lb_id_1->IsSuspended()); |
| 235 |
EXPECT_FALSE(store_invalid_lb_id_1->IsSuspended()); |
| 236 |
store_lb_id_1->MergeRow(kKey1, LoadRecordValue()); |
| 237 |
store_invalid_lb_id_1->MergeRow(kKey1, LoadRecordValue()); |
| 238 |
EXPECT_EQ(store_lb_id_1->load_record_map().size(), 1U); |
| 239 |
EXPECT_EQ(store_invalid_lb_id_1->load_record_map().size(), 1U); |
| 240 |
// The resumed stores are assigned to the new LB. |
| 241 |
auto assigned_to_lb_id_3 = |
| 242 |
load_data_store.GetAssignedStores(kHostname1, kLbId3); |
| 243 |
EXPECT_EQ(assigned_to_lb_id_3->size(), 3U); |
| 244 |
EXPECT_TRUE(PerBalancerStoresContains(load_data_store, assigned_to_lb_id_3, |
| 245 |
kHostname1, kLbId1, kLoadKey1)); |
| 246 |
EXPECT_TRUE(PerBalancerStoresContains(load_data_store, assigned_to_lb_id_3, |
| 247 |
kHostname1, kInvalidLbId, "")); |
| 248 |
EXPECT_TRUE(PerBalancerStoresContains(load_data_store, assigned_to_lb_id_3, |
| 249 |
kHostname1, kLbId3, kLoadKey2)); |
| 250 |
} |
| 251 |
|
| 252 |
TEST_F(LoadDataStoreTest, OneStorePerLbId) { |
| 253 |
LoadDataStore load_data_store; |
| 254 |
EXPECT_EQ(load_data_store.FindPerBalancerStore(kHostname1, kLbId1), nullptr); |
| 255 |
EXPECT_EQ(load_data_store.FindPerBalancerStore(kHostname1, kInvalidLbId), |
| 256 |
nullptr); |
| 257 |
EXPECT_EQ(load_data_store.FindPerBalancerStore(kHostname2, kLbId2), nullptr); |
| 258 |
EXPECT_EQ(load_data_store.FindPerBalancerStore(kHostname2, kLbId3), nullptr); |
| 259 |
// Create The first stream. |
| 260 |
load_data_store.ReportStreamCreated(kHostname1, kLbId1, kLoadKey1); |
| 261 |
auto store_lb_id_1 = load_data_store.FindPerBalancerStore(kHostname1, kLbId1); |
| 262 |
auto store_invalid_lb_id_1 = |
| 263 |
load_data_store.FindPerBalancerStore(kHostname1, kInvalidLbId); |
| 264 |
// Two stores will be created: one is for the stream; the other one is for |
| 265 |
// kInvalidLbId. |
| 266 |
EXPECT_NE(store_lb_id_1, nullptr); |
| 267 |
EXPECT_NE(store_invalid_lb_id_1, nullptr); |
| 268 |
EXPECT_NE(store_lb_id_1, store_invalid_lb_id_1); |
| 269 |
EXPECT_EQ(load_data_store.FindPerBalancerStore(kHostname2, kLbId2), nullptr); |
| 270 |
EXPECT_EQ(load_data_store.FindPerBalancerStore(kHostname2, kLbId3), nullptr); |
| 271 |
// Create the second stream. |
| 272 |
load_data_store.ReportStreamCreated(kHostname2, kLbId3, kLoadKey1); |
| 273 |
auto store_lb_id_3 = load_data_store.FindPerBalancerStore(kHostname2, kLbId3); |
| 274 |
auto store_invalid_lb_id_2 = |
| 275 |
load_data_store.FindPerBalancerStore(kHostname2, kInvalidLbId); |
| 276 |
EXPECT_NE(store_lb_id_3, nullptr); |
| 277 |
EXPECT_NE(store_invalid_lb_id_2, nullptr); |
| 278 |
EXPECT_NE(store_lb_id_3, store_invalid_lb_id_2); |
| 279 |
// The PerBalancerStores created for different hosts are independent. |
| 280 |
EXPECT_NE(store_lb_id_3, store_invalid_lb_id_1); |
| 281 |
EXPECT_NE(store_invalid_lb_id_2, store_invalid_lb_id_1); |
| 282 |
EXPECT_EQ(load_data_store.FindPerBalancerStore(kHostname2, kLbId2), nullptr); |
| 283 |
} |
| 284 |
|
| 285 |
TEST_F(LoadDataStoreTest, ExactlyOnceAssignment) { |
| 286 |
LoadDataStore load_data_store; |
| 287 |
size_t num_create = 100; |
| 288 |
size_t num_close = 50; |
| 289 |
for (size_t i = 0; i < num_create; ++i) { |
| 290 |
load_data_store.ReportStreamCreated(kHostname1, FormatLbId(i), kLoadKey1); |
| 291 |
} |
| 292 |
for (size_t i = 0; i < num_close; ++i) { |
| 293 |
load_data_store.ReportStreamClosed(kHostname1, FormatLbId(i)); |
| 294 |
} |
| 295 |
std::set<std::string> reported_lb_ids; |
| 296 |
for (size_t i = num_close; i < num_create; ++i) { |
| 297 |
for (auto assigned_store : |
| 298 |
*load_data_store.GetAssignedStores(kHostname1, FormatLbId(i))) { |
| 299 |
EXPECT_TRUE(reported_lb_ids.insert(assigned_store->lb_id()).second); |
| 300 |
} |
| 301 |
} |
| 302 |
// Add one for kInvalidLbId. |
| 303 |
EXPECT_EQ(reported_lb_ids.size(), (num_create + 1)); |
| 304 |
EXPECT_NE(reported_lb_ids.find(kInvalidLbId), reported_lb_ids.end()); |
| 305 |
} |
| 306 |
|
| 307 |
TEST_F(LoadDataStoreTest, UnknownBalancerIdTracking) { |
| 308 |
LoadDataStore load_data_store; |
| 309 |
load_data_store.ReportStreamCreated(kHostname1, kLbId1, kLoadKey1); |
| 310 |
// Merge data for a known LB ID. |
| 311 |
LoadRecordValue v1(192); |
| 312 |
load_data_store.MergeRow(kHostname1, kKey1, v1); |
| 313 |
// Merge data for unknown LB ID. |
| 314 |
LoadRecordValue v2(23); |
| 315 |
EXPECT_FALSE(load_data_store.IsTrackedUnknownBalancerId(kLbId2)); |
| 316 |
load_data_store.MergeRow( |
| 317 |
kHostname1, LoadRecordKey(kLbId2, kLbTag1, kUser1, kClientIp1), v2); |
| 318 |
EXPECT_TRUE(load_data_store.IsTrackedUnknownBalancerId(kLbId2)); |
| 319 |
LoadRecordValue v3(952); |
| 320 |
load_data_store.MergeRow( |
| 321 |
kHostname2, LoadRecordKey(kLbId3, kLbTag1, kUser1, kClientIp1), v3); |
| 322 |
EXPECT_TRUE(load_data_store.IsTrackedUnknownBalancerId(kLbId3)); |
| 323 |
// The data kept for a known LB ID is correct. |
| 324 |
auto store_lb_id_1 = load_data_store.FindPerBalancerStore(kHostname1, kLbId1); |
| 325 |
EXPECT_EQ(store_lb_id_1->load_record_map().size(), 1U); |
| 326 |
EXPECT_EQ(store_lb_id_1->load_record_map().find(kKey1)->second.start_count(), |
| 327 |
v1.start_count()); |
| 328 |
EXPECT_EQ(store_lb_id_1->GetNumCallsInProgressForReport(), v1.start_count()); |
| 329 |
// No PerBalancerStore created for Unknown LB ID. |
| 330 |
EXPECT_EQ(load_data_store.FindPerBalancerStore(kHostname1, kLbId2), nullptr); |
| 331 |
EXPECT_EQ(load_data_store.FindPerBalancerStore(kHostname2, kLbId3), nullptr); |
| 332 |
// End all the started RPCs for kLbId1. |
| 333 |
LoadRecordValue v4(0, v1.start_count()); |
| 334 |
load_data_store.MergeRow(kHostname1, kKey1, v4); |
| 335 |
EXPECT_EQ(store_lb_id_1->load_record_map().size(), 1U); |
| 336 |
EXPECT_EQ(store_lb_id_1->load_record_map().find(kKey1)->second.start_count(), |
| 337 |
v1.start_count()); |
| 338 |
EXPECT_EQ(store_lb_id_1->load_record_map().find(kKey1)->second.ok_count(), |
| 339 |
v4.ok_count()); |
| 340 |
EXPECT_EQ(store_lb_id_1->GetNumCallsInProgressForReport(), 0U); |
| 341 |
EXPECT_FALSE(load_data_store.IsTrackedUnknownBalancerId(kLbId1)); |
| 342 |
// End all the started RPCs for kLbId2. |
| 343 |
LoadRecordValue v5(0, v2.start_count()); |
| 344 |
load_data_store.MergeRow( |
| 345 |
kHostname1, LoadRecordKey(kLbId2, kLbTag1, kUser1, kClientIp1), v5); |
| 346 |
EXPECT_FALSE(load_data_store.IsTrackedUnknownBalancerId(kLbId2)); |
| 347 |
// End some of the started RPCs for kLbId3. |
| 348 |
LoadRecordValue v6(0, v3.start_count() / 2); |
| 349 |
load_data_store.MergeRow( |
| 350 |
kHostname2, LoadRecordKey(kLbId3, kLbTag1, kUser1, kClientIp1), v6); |
| 351 |
EXPECT_TRUE(load_data_store.IsTrackedUnknownBalancerId(kLbId3)); |
| 352 |
} |
| 353 |
|
| 354 |
TEST_F(PerBalancerStoreTest, Suspend) { |
| 355 |
PerBalancerStore per_balancer_store(kLbId1, kLoadKey1); |
| 356 |
EXPECT_FALSE(per_balancer_store.IsSuspended()); |
| 357 |
// Suspend the store. |
| 358 |
per_balancer_store.Suspend(); |
| 359 |
EXPECT_TRUE(per_balancer_store.IsSuspended()); |
| 360 |
EXPECT_EQ(0U, per_balancer_store.load_record_map().size()); |
| 361 |
// Data merged when the store is suspended won't be kept. |
| 362 |
LoadRecordValue v1(139, 19); |
| 363 |
per_balancer_store.MergeRow(kKey1, v1); |
| 364 |
EXPECT_EQ(0U, per_balancer_store.load_record_map().size()); |
| 365 |
// Resume the store. |
| 366 |
per_balancer_store.Resume(); |
| 367 |
EXPECT_FALSE(per_balancer_store.IsSuspended()); |
| 368 |
EXPECT_EQ(0U, per_balancer_store.load_record_map().size()); |
| 369 |
// Data merged after the store is resumed will be kept. |
| 370 |
LoadRecordValue v2(23, 0, 51); |
| 371 |
per_balancer_store.MergeRow(kKey1, v2); |
| 372 |
EXPECT_EQ(1U, per_balancer_store.load_record_map().size()); |
| 373 |
// Suspend the store. |
| 374 |
per_balancer_store.Suspend(); |
| 375 |
EXPECT_TRUE(per_balancer_store.IsSuspended()); |
| 376 |
EXPECT_EQ(0U, per_balancer_store.load_record_map().size()); |
| 377 |
// Data merged when the store is suspended won't be kept. |
| 378 |
LoadRecordValue v3(62, 11); |
| 379 |
per_balancer_store.MergeRow(kKey1, v3); |
| 380 |
EXPECT_EQ(0U, per_balancer_store.load_record_map().size()); |
| 381 |
// Resume the store. |
| 382 |
per_balancer_store.Resume(); |
| 383 |
EXPECT_FALSE(per_balancer_store.IsSuspended()); |
| 384 |
EXPECT_EQ(0U, per_balancer_store.load_record_map().size()); |
| 385 |
// Data merged after the store is resumed will be kept. |
| 386 |
LoadRecordValue v4(225, 98); |
| 387 |
per_balancer_store.MergeRow(kKey1, v4); |
| 388 |
EXPECT_EQ(1U, per_balancer_store.load_record_map().size()); |
| 389 |
// In-progress count is always kept. |
| 390 |
EXPECT_EQ(per_balancer_store.GetNumCallsInProgressForReport(), |
| 391 |
v1.start_count() - v1.ok_count() + v2.start_count() - |
| 392 |
v2.error_count() + v3.start_count() - v3.ok_count() + |
| 393 |
v4.start_count() - v4.ok_count()); |
| 394 |
} |
| 395 |
|
| 396 |
TEST_F(PerBalancerStoreTest, DataAggregation) { |
| 397 |
PerBalancerStore per_balancer_store(kLbId1, kLoadKey1); |
| 398 |
// Construct some Values. |
| 399 |
LoadRecordValue v1(992, 34, 13, 234, 164, 173467); |
| 400 |
v1.InsertCallMetric(kMetric1, CallMetricValue(3, 2773.2)); |
| 401 |
LoadRecordValue v2(4842, 213, 9, 393, 974, 1345); |
| 402 |
v2.InsertCallMetric(kMetric1, CallMetricValue(7, 25.234)); |
| 403 |
v2.InsertCallMetric(kMetric2, CallMetricValue(2, 387.08)); |
| 404 |
// v3 doesn't change the number of in-progress RPCs. |
| 405 |
LoadRecordValue v3(293, 55, 293 - 55, 28764, 5284, 5772); |
| 406 |
v3.InsertCallMetric(kMetric1, CallMetricValue(61, 3465.0)); |
| 407 |
v3.InsertCallMetric(kMetric2, CallMetricValue(13, 672.0)); |
| 408 |
// The initial state of the store. |
| 409 |
uint64_t num_calls_in_progress = 0; |
| 410 |
EXPECT_FALSE(per_balancer_store.IsNumCallsInProgressChangedSinceLastReport()); |
| 411 |
EXPECT_EQ(per_balancer_store.GetNumCallsInProgressForReport(), |
| 412 |
num_calls_in_progress); |
| 413 |
// Merge v1 and get report of the number of in-progress calls. |
| 414 |
per_balancer_store.MergeRow(kKey1, v1); |
| 415 |
EXPECT_TRUE(per_balancer_store.IsNumCallsInProgressChangedSinceLastReport()); |
| 416 |
EXPECT_EQ(per_balancer_store.GetNumCallsInProgressForReport(), |
| 417 |
num_calls_in_progress += |
| 418 |
(v1.start_count() - v1.ok_count() - v1.error_count())); |
| 419 |
EXPECT_FALSE(per_balancer_store.IsNumCallsInProgressChangedSinceLastReport()); |
| 420 |
// Merge v2 and get report of the number of in-progress calls. |
| 421 |
per_balancer_store.MergeRow(kKey2, v2); |
| 422 |
EXPECT_TRUE(per_balancer_store.IsNumCallsInProgressChangedSinceLastReport()); |
| 423 |
EXPECT_EQ(per_balancer_store.GetNumCallsInProgressForReport(), |
| 424 |
num_calls_in_progress += |
| 425 |
(v2.start_count() - v2.ok_count() - v2.error_count())); |
| 426 |
EXPECT_FALSE(per_balancer_store.IsNumCallsInProgressChangedSinceLastReport()); |
| 427 |
// Merge v3 and get report of the number of in-progress calls. |
| 428 |
per_balancer_store.MergeRow(kKey1, v3); |
| 429 |
EXPECT_FALSE(per_balancer_store.IsNumCallsInProgressChangedSinceLastReport()); |
| 430 |
EXPECT_EQ(per_balancer_store.GetNumCallsInProgressForReport(), |
| 431 |
num_calls_in_progress); |
| 432 |
// LoadRecordValue for kKey1 is aggregated correctly. |
| 433 |
LoadRecordValue value_for_key1 = |
| 434 |
per_balancer_store.load_record_map().find(kKey1)->second; |
| 435 |
EXPECT_EQ(value_for_key1.start_count(), v1.start_count() + v3.start_count()); |
| 436 |
EXPECT_EQ(value_for_key1.ok_count(), v1.ok_count() + v3.ok_count()); |
| 437 |
EXPECT_EQ(value_for_key1.error_count(), v1.error_count() + v3.error_count()); |
| 438 |
EXPECT_EQ(value_for_key1.bytes_sent(), v1.bytes_sent() + v3.bytes_sent()); |
| 439 |
EXPECT_EQ(value_for_key1.bytes_recv(), v1.bytes_recv() + v3.bytes_recv()); |
| 440 |
EXPECT_EQ(value_for_key1.latency_ms(), v1.latency_ms() + v3.latency_ms()); |
| 441 |
EXPECT_EQ(value_for_key1.call_metrics().size(), 2U); |
| 442 |
EXPECT_EQ(value_for_key1.call_metrics().find(kMetric1)->second.num_calls(), |
| 443 |
v1.call_metrics().find(kMetric1)->second.num_calls() + |
| 444 |
v3.call_metrics().find(kMetric1)->second.num_calls()); |
| 445 |
EXPECT_EQ( |
| 446 |
value_for_key1.call_metrics().find(kMetric1)->second.total_metric_value(), |
| 447 |
v1.call_metrics().find(kMetric1)->second.total_metric_value() + |
| 448 |
v3.call_metrics().find(kMetric1)->second.total_metric_value()); |
| 449 |
EXPECT_EQ(value_for_key1.call_metrics().find(kMetric2)->second.num_calls(), |
| 450 |
v3.call_metrics().find(kMetric2)->second.num_calls()); |
| 451 |
EXPECT_EQ( |
| 452 |
value_for_key1.call_metrics().find(kMetric2)->second.total_metric_value(), |
| 453 |
v3.call_metrics().find(kMetric2)->second.total_metric_value()); |
| 454 |
// LoadRecordValue for kKey2 is aggregated (trivially) correctly. |
| 455 |
LoadRecordValue value_for_key2 = |
| 456 |
per_balancer_store.load_record_map().find(kKey2)->second; |
| 457 |
EXPECT_EQ(value_for_key2.start_count(), v2.start_count()); |
| 458 |
EXPECT_EQ(value_for_key2.ok_count(), v2.ok_count()); |
| 459 |
EXPECT_EQ(value_for_key2.error_count(), v2.error_count()); |
| 460 |
EXPECT_EQ(value_for_key2.bytes_sent(), v2.bytes_sent()); |
| 461 |
EXPECT_EQ(value_for_key2.bytes_recv(), v2.bytes_recv()); |
| 462 |
EXPECT_EQ(value_for_key2.latency_ms(), v2.latency_ms()); |
| 463 |
EXPECT_EQ(value_for_key2.call_metrics().size(), 2U); |
| 464 |
EXPECT_EQ(value_for_key2.call_metrics().find(kMetric1)->second.num_calls(), |
| 465 |
v2.call_metrics().find(kMetric1)->second.num_calls()); |
| 466 |
EXPECT_EQ( |
| 467 |
value_for_key2.call_metrics().find(kMetric1)->second.total_metric_value(), |
| 468 |
v2.call_metrics().find(kMetric1)->second.total_metric_value()); |
| 469 |
EXPECT_EQ(value_for_key2.call_metrics().find(kMetric2)->second.num_calls(), |
| 470 |
v2.call_metrics().find(kMetric2)->second.num_calls()); |
| 471 |
EXPECT_EQ( |
| 472 |
value_for_key2.call_metrics().find(kMetric2)->second.total_metric_value(), |
| 473 |
v2.call_metrics().find(kMetric2)->second.total_metric_value()); |
| 474 |
} |
| 475 |
|
| 476 |
} // namespace |
| 477 |
} // namespace testing |
| 478 |
} // namespace grpc |
| 479 |
|
| 480 |
int main(int argc, char** argv) { |
| 481 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 482 |
::testing::InitGoogleTest(&argc, argv); |
| 483 |
return RUN_ALL_TESTS(); |
| 484 |
} |