1 //
2 // Copyright 2018 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "src/core/xds/grpc/xds_endpoint.h"
18
19 #include <vector>
20
21 #include "absl/strings/str_cat.h"
22 #include "absl/strings/str_join.h"
23
24 namespace grpc_core {
25
ToString() const26 std::string XdsEndpointResource::Priority::Locality::ToString() const {
27 std::vector<std::string> endpoint_strings;
28 for (const EndpointAddresses& endpoint : endpoints) {
29 endpoint_strings.emplace_back(endpoint.ToString());
30 }
31 return absl::StrCat("{name=", name->human_readable_string().as_string_view(),
32 ", lb_weight=", lb_weight, ", endpoints=[",
33 absl::StrJoin(endpoint_strings, ", "), "]}");
34 }
35
operator ==(const Priority & other) const36 bool XdsEndpointResource::Priority::operator==(const Priority& other) const {
37 if (localities.size() != other.localities.size()) return false;
38 auto it1 = localities.begin();
39 auto it2 = other.localities.begin();
40 while (it1 != localities.end()) {
41 if (*it1->first != *it2->first) return false;
42 if (it1->second != it2->second) return false;
43 ++it1;
44 ++it2;
45 }
46 return true;
47 }
48
ToString() const49 std::string XdsEndpointResource::Priority::ToString() const {
50 std::vector<std::string> locality_strings;
51 locality_strings.reserve(localities.size());
52 for (const auto& p : localities) {
53 locality_strings.emplace_back(p.second.ToString());
54 }
55 return absl::StrCat("[", absl::StrJoin(locality_strings, ", "), "]");
56 }
57
ShouldDrop(const std::string ** category_name)58 bool XdsEndpointResource::DropConfig::ShouldDrop(
59 const std::string** category_name) {
60 for (size_t i = 0; i < drop_category_list_.size(); ++i) {
61 const auto& drop_category = drop_category_list_[i];
62 // Generate a random number in [0, 1000000).
63 const uint32_t random = [&]() {
64 MutexLock lock(&mu_);
65 return absl::Uniform<uint32_t>(bit_gen_, 0, 1000000);
66 }();
67 if (random < drop_category.parts_per_million) {
68 *category_name = &drop_category.name;
69 return true;
70 }
71 }
72 return false;
73 }
74
ToString() const75 std::string XdsEndpointResource::DropConfig::ToString() const {
76 std::vector<std::string> category_strings;
77 for (const DropCategory& category : drop_category_list_) {
78 category_strings.emplace_back(
79 absl::StrCat(category.name, "=", category.parts_per_million));
80 }
81 return absl::StrCat("{[", absl::StrJoin(category_strings, ", "),
82 "], drop_all=", drop_all_, "}");
83 }
84
ToString() const85 std::string XdsEndpointResource::ToString() const {
86 std::vector<std::string> priority_strings;
87 for (size_t i = 0; i < priorities.size(); ++i) {
88 const Priority& priority = priorities[i];
89 priority_strings.emplace_back(
90 absl::StrCat("priority ", i, ": ", priority.ToString()));
91 }
92 return absl::StrCat(
93 "priorities=[", absl::StrJoin(priority_strings, ", "), "], drop_config=",
94 drop_config == nullptr ? "<null>" : drop_config->ToString());
95 }
96
97 } // namespace grpc_core
98