Coverage Report

Created: 2024-09-19 09:45

/proc/self/cwd/envoy/upstream/host_description.h
Line
Count
Source (jump to first uncovered line)
1
#pragma once
2
3
#include <map>
4
#include <memory>
5
#include <string>
6
#include <vector>
7
8
#include "envoy/common/time.h"
9
#include "envoy/config/core/v3/base.pb.h"
10
#include "envoy/network/address.h"
11
#include "envoy/network/transport_socket.h"
12
#include "envoy/stats/primitive_stats_macros.h"
13
#include "envoy/stats/stats_macros.h"
14
#include "envoy/upstream/health_check_host_monitor.h"
15
#include "envoy/upstream/outlier_detection.h"
16
#include "envoy/upstream/resource_manager.h"
17
18
#include "absl/strings/string_view.h"
19
20
namespace Envoy {
21
namespace Upstream {
22
23
using MetadataConstSharedPtr = std::shared_ptr<const envoy::config::core::v3::Metadata>;
24
25
/**
26
 * All per host stats. @see stats_macros.h
27
 *
28
 * {rq_success, rq_error} have specific semantics driven by the needs of EDS load reporting. See
29
 * envoy.api.v2.endpoint.UpstreamLocalityStats for the definitions of success/error. These are
30
 * latched by LoadStatsReporter, independent of the normal stats sink flushing.
31
 */
32
#define ALL_HOST_STATS(COUNTER, GAUGE)                                                             \
33
0
  COUNTER(cx_connect_fail)                                                                         \
34
0
  COUNTER(cx_total)                                                                                \
35
0
  COUNTER(rq_error)                                                                                \
36
0
  COUNTER(rq_success)                                                                              \
37
0
  COUNTER(rq_timeout)                                                                              \
38
0
  COUNTER(rq_total)                                                                                \
39
0
  GAUGE(cx_active)                                                                                 \
40
0
  GAUGE(rq_active)
41
42
/**
43
 * All per host stats defined. @see stats_macros.h
44
 */
45
struct HostStats {
46
  ALL_HOST_STATS(GENERATE_PRIMITIVE_COUNTER_STRUCT, GENERATE_PRIMITIVE_GAUGE_STRUCT);
47
48
  // Provide access to name,counter pairs.
49
0
  std::vector<std::pair<absl::string_view, Stats::PrimitiveCounterReference>> counters() {
50
0
    return {ALL_HOST_STATS(PRIMITIVE_COUNTER_NAME_AND_REFERENCE, IGNORE_PRIMITIVE_GAUGE)};
51
0
  }
Unexecuted instantiation: Envoy::Upstream::HostStats::counters()
Unexecuted instantiation: Envoy::Upstream::HostStats::counters()
52
53
  // Provide access to name,gauge pairs.
54
0
  std::vector<std::pair<absl::string_view, Stats::PrimitiveGaugeReference>> gauges() {
55
0
    return {ALL_HOST_STATS(IGNORE_PRIMITIVE_COUNTER, PRIMITIVE_GAUGE_NAME_AND_REFERENCE)};
56
0
  }
Unexecuted instantiation: Envoy::Upstream::HostStats::gauges()
Unexecuted instantiation: Envoy::Upstream::HostStats::gauges()
57
};
58
59
/**
60
 * Weakly-named load metrics to be reported as EndpointLoadMetricStats. Individual stats are
61
 * accumulated by calling add(), which combines stats with the same name. The aggregated stats are
62
 * retrieved by calling latch(), which also clears the current load metrics.
63
 */
64
class LoadMetricStats {
65
public:
66
534k
  virtual ~LoadMetricStats() = default;
67
68
  struct Stat {
69
    uint64_t num_requests_with_metric = 0;
70
    double total_metric_value = 0.0;
71
  };
72
73
  using StatMap = absl::flat_hash_map<std::string, Stat>;
74
  using StatMapPtr = std::unique_ptr<StatMap>;
75
76
  // Adds the given stat to the map. If the stat already exists in the map, then the stat is
77
  // combined with the existing map entry by incrementing num_requests_with_metric and summing the
78
  // total_metric_value fields. Otherwise, the stat is added with the provided value to the map,
79
  // which retains all entries until the next call to latch(). This allows metrics to be added
80
  // whose keys may not necessarily be known at startup time.
81
  virtual void add(const absl::string_view key, double value) PURE;
82
83
  // Returns an owning pointer to the current load metrics and clears the map.
84
  virtual StatMapPtr latch() PURE;
85
};
86
87
class ClusterInfo;
88
89
/**
90
 * A description of an upstream host.
91
 */
92
class HostDescription {
93
public:
94
  using AddressVector = std::vector<Network::Address::InstanceConstSharedPtr>;
95
  using SharedConstAddressVector = std::shared_ptr<const AddressVector>;
96
97
534k
  virtual ~HostDescription() = default;
98
99
  /**
100
   * @return whether the host is a canary.
101
   */
102
  virtual bool canary() const PURE;
103
104
  /**
105
   * Update the canary status of the host.
106
   */
107
  virtual void canary(bool is_canary) PURE;
108
109
  /**
110
   * @return the metadata associated with this host
111
   */
112
  virtual MetadataConstSharedPtr metadata() const PURE;
113
114
  /**
115
   * Set the current metadata.
116
   */
117
  virtual void metadata(MetadataConstSharedPtr new_metadata) PURE;
118
119
  /**
120
   * @return the cluster the host is a member of.
121
   */
122
  virtual const ClusterInfo& cluster() const PURE;
123
124
  /**
125
   * @return true if the cluster can create a connection for this priority, false otherwise.
126
   * @param priority the priority the connection would have.
127
   */
128
  virtual bool canCreateConnection(Upstream::ResourcePriority priority) const PURE;
129
130
  /**
131
   * @return the host's outlier detection monitor.
132
   */
133
  virtual Outlier::DetectorHostMonitor& outlierDetector() const PURE;
134
135
  /**
136
   * Set the host's outlier detector monitor. Outlier detector monitors are assumed to be thread
137
   * safe, however a new outlier detector monitor must be installed before the host is used across
138
   * threads. Thus, this routine should only be called on the main thread before the host is used
139
   * across threads.
140
   */
141
  virtual void setOutlierDetector(Outlier::DetectorHostMonitorPtr&& outlier_detector) PURE;
142
143
  /**
144
   * @return the host's health checker monitor.
145
   */
146
  virtual HealthCheckHostMonitor& healthChecker() const PURE;
147
148
  /**
149
   * Set the host's health checker monitor. Monitors are assumed to be thread safe, however
150
   * a new monitor must be installed before the host is used across threads. Thus,
151
   * this routine should only be called on the main thread before the host is used across threads.
152
   */
153
  virtual void setHealthChecker(HealthCheckHostMonitorPtr&& health_checker) PURE;
154
155
  /**
156
   * @return The hostname used as the host header for health checking.
157
   */
158
  virtual const std::string& hostnameForHealthChecks() const PURE;
159
160
  /**
161
   * @return the hostname associated with the host if any.
162
   * Empty string "" indicates that hostname is not a DNS name.
163
   */
164
  virtual const std::string& hostname() const PURE;
165
166
  /**
167
   * @return the transport socket factory responsible for this host.
168
   */
169
  virtual Network::UpstreamTransportSocketFactory& transportSocketFactory() const PURE;
170
171
  /**
172
   * @return the address used to connect to the host.
173
   */
174
  virtual Network::Address::InstanceConstSharedPtr address() const PURE;
175
176
  /**
177
   * @return nullptr, or a optional list of additional addresses which the host
178
   *         resolved to. These addresses may be used to create upstream
179
   *         connections if the primary address is unreachable.
180
   *
181
   * The address-list is returned as a shared_ptr<const vector<...>> because in
182
   * some implements of HostDescription, the address-list can be mutated
183
   * asynchronously. Those implementations must use a lock to ensure this is
184
   * safe. However this is not sufficient when returning the addressList by
185
   * reference.
186
   *
187
   * Caller must check return-value for nullptr before accessing the vector.
188
   */
189
  virtual SharedConstAddressVector addressListOrNull() const PURE;
190
191
  /**
192
   * @return host specific stats.
193
   */
194
  virtual HostStats& stats() const PURE;
195
196
  /**
197
   * @return custom stats for multi-dimensional load balancing.
198
   */
199
  virtual LoadMetricStats& loadMetricStats() const PURE;
200
201
  /**
202
   * @return the locality of the host (deployment specific). This will be the default instance if
203
   *         unknown.
204
   */
205
  virtual const envoy::config::core::v3::Locality& locality() const PURE;
206
207
  /**
208
   * @return the metadata associated with the locality endpoints the host belongs to.
209
   */
210
  virtual const MetadataConstSharedPtr localityMetadata() const PURE;
211
212
  /**
213
   * @return the human readable name of the host's locality zone as a StatName.
214
   */
215
  virtual Stats::StatName localityZoneStatName() const PURE;
216
217
  /**
218
   * @return the address used to health check the host.
219
   */
220
  virtual Network::Address::InstanceConstSharedPtr healthCheckAddress() const PURE;
221
222
  /**
223
   * @return the priority of the host.
224
   */
225
  virtual uint32_t priority() const PURE;
226
227
  /**
228
   * Set the current priority.
229
   */
230
  virtual void priority(uint32_t) PURE;
231
232
  /**
233
   * @return timestamp of when host has transitioned from unhealthy to
234
   *         healthy state via an active healthchecking.
235
   */
236
  virtual absl::optional<MonotonicTime> lastHcPassTime() const PURE;
237
238
  /**
239
   * Set the timestamp of when the host has transitioned from unhealthy to healthy state via an
240
   * active health checking.
241
   */
242
  virtual void setLastHcPassTime(MonotonicTime last_hc_pass_time) PURE;
243
244
  virtual Network::UpstreamTransportSocketFactory&
245
  resolveTransportSocketFactory(const Network::Address::InstanceConstSharedPtr& dest_address,
246
                                const envoy::config::core::v3::Metadata* metadata) const PURE;
247
};
248
249
using HostDescriptionConstSharedPtr = std::shared_ptr<const HostDescription>;
250
251
2.40k
#define ALL_TRANSPORT_SOCKET_MATCH_STATS(COUNTER) COUNTER(total_match_count)
252
253
/**
254
 * The stats for transport socket match.
255
 */
256
struct TransportSocketMatchStats {
257
  ALL_TRANSPORT_SOCKET_MATCH_STATS(GENERATE_COUNTER_STRUCT)
258
};
259
260
/**
261
 * Library to determine what transport socket configuration to use for a given host.
262
 */
263
class TransportSocketMatcher {
264
public:
265
  struct MatchData {
266
    MatchData(Network::UpstreamTransportSocketFactory& factory, TransportSocketMatchStats& stats,
267
              std::string name)
268
403k
        : factory_(factory), stats_(stats), name_(std::move(name)) {}
269
    Network::UpstreamTransportSocketFactory& factory_;
270
    TransportSocketMatchStats& stats_;
271
    std::string name_;
272
  };
273
403k
  virtual ~TransportSocketMatcher() = default;
274
275
  /**
276
   * Resolve the transport socket configuration for a particular host.
277
   * @param endpoint_metadata the metadata of the given host.
278
   * @param locality_metadata the metadata of the host's locality.
279
   * @return the match information of the transport socket selected.
280
   */
281
  virtual MatchData resolve(const envoy::config::core::v3::Metadata* endpoint_metadata,
282
                            const envoy::config::core::v3::Metadata* locality_metadata) const PURE;
283
284
  /*
285
   * return true if all matches support ALPN, false otherwise.
286
   */
287
  virtual bool allMatchesSupportAlpn() const PURE;
288
};
289
290
using TransportSocketMatcherPtr = std::unique_ptr<TransportSocketMatcher>;
291
292
} // namespace Upstream
293
} // namespace Envoy