| 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 |
#ifndef GRPC_CORE_EXT_XDS_XDS_CLIENT_STATS_H |
| 20 |
#define GRPC_CORE_EXT_XDS_XDS_CLIENT_STATS_H |
| 21 |
|
| 22 |
#include <grpc/support/port_platform.h> |
| 23 |
|
| 24 |
#include <atomic> |
| 25 |
#include <cstdint> |
| 26 |
#include <map> |
| 27 |
#include <string> |
| 28 |
#include <utility> |
| 29 |
|
| 30 |
#include "absl/base/thread_annotations.h" |
| 31 |
#include "absl/strings/str_format.h" |
| 32 |
#include "absl/strings/string_view.h" |
| 33 |
|
| 34 |
#include "src/core/ext/xds/xds_bootstrap.h" |
| 35 |
#include "src/core/lib/gpr/useful.h" |
| 36 |
#include "src/core/lib/gprpp/ref_counted.h" |
| 37 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 38 |
#include "src/core/lib/gprpp/sync.h" |
| 39 |
|
| 40 |
namespace grpc_core { |
| 41 |
|
| 42 |
// Forward declaration to avoid circular dependency. |
| 43 |
class XdsClient; |
| 44 |
|
| 45 |
// Locality name. |
| 46 |
class XdsLocalityName : public RefCounted<XdsLocalityName> { |
| 47 |
public: |
| 48 |
struct Less { |
| 49 |
bool operator()(const XdsLocalityName* lhs, |
| 50 |
const XdsLocalityName* rhs) const { |
| 51 |
if (lhs == nullptr || rhs == nullptr) return QsortCompare(lhs, rhs); |
| 52 |
return lhs->Compare(*rhs) < 0; |
| 53 |
} |
| 54 |
|
| 55 |
bool operator()(const RefCountedPtr<XdsLocalityName>& lhs, |
| 56 |
const RefCountedPtr<XdsLocalityName>& rhs) const { |
| 57 |
return (*this)(lhs.get(), rhs.get()); |
| 58 |
} |
| 59 |
}; |
| 60 |
|
| 61 |
XdsLocalityName(std::string region, std::string zone, std::string sub_zone) |
| 62 |
: region_(std::move(region)), |
| 63 |
zone_(std::move(zone)), |
| 64 |
sub_zone_(std::move(sub_zone)) {} |
| 65 |
|
| 66 |
bool operator==(const XdsLocalityName& other) const { |
| 67 |
return region_ == other.region_ && zone_ == other.zone_ && |
| 68 |
sub_zone_ == other.sub_zone_; |
| 69 |
} |
| 70 |
|
| 71 |
bool operator!=(const XdsLocalityName& other) const { |
| 72 |
return !(*this == other); |
| 73 |
} |
| 74 |
|
| 75 |
int Compare(const XdsLocalityName& other) const { |
| 76 |
int cmp_result = region_.compare(other.region_); |
| 77 |
if (cmp_result != 0) return cmp_result; |
| 78 |
cmp_result = zone_.compare(other.zone_); |
| 79 |
if (cmp_result != 0) return cmp_result; |
| 80 |
return sub_zone_.compare(other.sub_zone_); |
| 81 |
} |
| 82 |
|
| 83 |
const std::string& region() const { return region_; } |
| 84 |
const std::string& zone() const { return zone_; } |
| 85 |
const std::string& sub_zone() const { return sub_zone_; } |
| 86 |
|
| 87 |
const std::string& AsHumanReadableString() { |
| 88 |
if (human_readable_string_.empty()) { |
| 89 |
human_readable_string_ = |
| 90 |
absl::StrFormat("{region=\"%s\", zone=\"%s\", sub_zone=\"%s\"}", |
| 91 |
region_, zone_, sub_zone_); |
| 92 |
} |
| 93 |
return human_readable_string_; |
| 94 |
} |
| 95 |
|
| 96 |
private: |
| 97 |
std::string region_; |
| 98 |
std::string zone_; |
| 99 |
std::string sub_zone_; |
| 100 |
std::string human_readable_string_; |
| 101 |
}; |
| 102 |
|
| 103 |
// Drop stats for an xds cluster. |
| 104 |
class XdsClusterDropStats : public RefCounted<XdsClusterDropStats> { |
| 105 |
public: |
| 106 |
// The total number of requests dropped for any reason is the sum of |
| 107 |
// uncategorized_drops, and dropped_requests map. |
| 108 |
using CategorizedDropsMap = std::map<std::string /* category */, uint64_t>; |
| 109 |
struct Snapshot { |
| 110 |
uint64_t uncategorized_drops = 0; |
| 111 |
// The number of requests dropped for the specific drop categories |
| 112 |
// outlined in the drop_overloads field in the EDS response. |
| 113 |
CategorizedDropsMap categorized_drops; |
| 114 |
|
| 115 |
Snapshot& operator+=(const Snapshot& other) { |
| 116 |
uncategorized_drops += other.uncategorized_drops; |
| 117 |
for (const auto& p : other.categorized_drops) { |
| 118 |
categorized_drops[p.first] += p.second; |
| 119 |
} |
| 120 |
return *this; |
| 121 |
} |
| 122 |
|
| 123 |
bool IsZero() const { |
| 124 |
if (uncategorized_drops != 0) return false; |
| 125 |
for (const auto& p : categorized_drops) { |
| 126 |
if (p.second != 0) return false; |
| 127 |
} |
| 128 |
return true; |
| 129 |
} |
| 130 |
}; |
| 131 |
|
| 132 |
XdsClusterDropStats(RefCountedPtr<XdsClient> xds_client, |
| 133 |
const XdsBootstrap::XdsServer& lrs_server, |
| 134 |
absl::string_view cluster_name, |
| 135 |
absl::string_view eds_service_name); |
| 136 |
~XdsClusterDropStats() override; |
| 137 |
|
| 138 |
// Returns a snapshot of this instance and resets all the counters. |
| 139 |
Snapshot GetSnapshotAndReset(); |
| 140 |
|
| 141 |
void AddUncategorizedDrops(); |
| 142 |
void AddCallDropped(const std::string& category); |
| 143 |
|
| 144 |
private: |
| 145 |
RefCountedPtr<XdsClient> xds_client_; |
| 146 |
const XdsBootstrap::XdsServer& lrs_server_; |
| 147 |
absl::string_view cluster_name_; |
| 148 |
absl::string_view eds_service_name_; |
| 149 |
std::atomic<uint64_t> uncategorized_drops_{0}; |
| 150 |
// Protects categorized_drops_. A mutex is necessary because the length of |
| 151 |
// dropped_requests can be accessed by both the picker (from data plane |
| 152 |
// mutex) and the load reporting thread (from the control plane combiner). |
| 153 |
Mutex mu_; |
| 154 |
CategorizedDropsMap categorized_drops_ ABSL_GUARDED_BY(mu_); |
| 155 |
}; |
| 156 |
|
| 157 |
// Locality stats for an xds cluster. |
| 158 |
class XdsClusterLocalityStats : public RefCounted<XdsClusterLocalityStats> { |
| 159 |
public: |
| 160 |
struct BackendMetric { |
| 161 |
uint64_t num_requests_finished_with_metric; |
| 162 |
double total_metric_value; |
| 163 |
|
| 164 |
BackendMetric& operator+=(const BackendMetric& other) { |
| 165 |
num_requests_finished_with_metric += |
| 166 |
other.num_requests_finished_with_metric; |
| 167 |
total_metric_value += other.total_metric_value; |
| 168 |
return *this; |
| 169 |
} |
| 170 |
|
| 171 |
bool IsZero() const { |
| 172 |
return num_requests_finished_with_metric == 0 && total_metric_value == 0; |
| 173 |
} |
| 174 |
}; |
| 175 |
|
| 176 |
struct Snapshot { |
| 177 |
uint64_t total_successful_requests; |
| 178 |
uint64_t total_requests_in_progress; |
| 179 |
uint64_t total_error_requests; |
| 180 |
uint64_t total_issued_requests; |
| 181 |
std::map<std::string, BackendMetric> backend_metrics; |
| 182 |
|
| 183 |
Snapshot& operator+=(const Snapshot& other) { |
| 184 |
total_successful_requests += other.total_successful_requests; |
| 185 |
total_requests_in_progress += other.total_requests_in_progress; |
| 186 |
total_error_requests += other.total_error_requests; |
| 187 |
total_issued_requests += other.total_issued_requests; |
| 188 |
for (const auto& p : other.backend_metrics) { |
| 189 |
backend_metrics[p.first] += p.second; |
| 190 |
} |
| 191 |
return *this; |
| 192 |
} |
| 193 |
|
| 194 |
bool IsZero() const { |
| 195 |
if (total_successful_requests != 0 || total_requests_in_progress != 0 || |
| 196 |
total_error_requests != 0 || total_issued_requests != 0) { |
| 197 |
return false; |
| 198 |
} |
| 199 |
for (const auto& p : backend_metrics) { |
| 200 |
if (!p.second.IsZero()) return false; |
| 201 |
} |
| 202 |
return true; |
| 203 |
} |
| 204 |
}; |
| 205 |
|
| 206 |
XdsClusterLocalityStats(RefCountedPtr<XdsClient> xds_client, |
| 207 |
const XdsBootstrap::XdsServer& lrs_server_, |
| 208 |
absl::string_view cluster_name, |
| 209 |
absl::string_view eds_service_name, |
| 210 |
RefCountedPtr<XdsLocalityName> name); |
| 211 |
~XdsClusterLocalityStats() override; |
| 212 |
|
| 213 |
// Returns a snapshot of this instance and resets all the counters. |
| 214 |
Snapshot GetSnapshotAndReset(); |
| 215 |
|
| 216 |
void AddCallStarted(); |
| 217 |
void AddCallFinished(bool fail = false); |
| 218 |
|
| 219 |
private: |
| 220 |
RefCountedPtr<XdsClient> xds_client_; |
| 221 |
const XdsBootstrap::XdsServer& lrs_server_; |
| 222 |
absl::string_view cluster_name_; |
| 223 |
absl::string_view eds_service_name_; |
| 224 |
RefCountedPtr<XdsLocalityName> name_; |
| 225 |
|
| 226 |
std::atomic<uint64_t> total_successful_requests_{0}; |
| 227 |
std::atomic<uint64_t> total_requests_in_progress_{0}; |
| 228 |
std::atomic<uint64_t> total_error_requests_{0}; |
| 229 |
std::atomic<uint64_t> total_issued_requests_{0}; |
| 230 |
|
| 231 |
// Protects backend_metrics_. A mutex is necessary because the length of |
| 232 |
// backend_metrics_ can be accessed by both the callback intercepting the |
| 233 |
// call's recv_trailing_metadata (not from the control plane work serializer) |
| 234 |
// and the load reporting thread (from the control plane work serializer). |
| 235 |
Mutex backend_metrics_mu_; |
| 236 |
std::map<std::string, BackendMetric> backend_metrics_ |
| 237 |
ABSL_GUARDED_BY(backend_metrics_mu_); |
| 238 |
}; |
| 239 |
|
| 240 |
} // namespace grpc_core |
| 241 |
|
| 242 |
#endif /* GRPC_CORE_EXT_XDS_XDS_CLIENT_STATS_H */ |