forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_grpc_mux_impl.cc
323 lines (287 loc) · 13.6 KB
/
new_grpc_mux_impl.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include "source/common/config/new_grpc_mux_impl.h"
#include "envoy/service/discovery/v3/discovery.pb.h"
#include "source/common/common/assert.h"
#include "source/common/common/backoff_strategy.h"
#include "source/common/common/token_bucket_impl.h"
#include "source/common/config/utility.h"
#include "source/common/config/xds_context_params.h"
#include "source/common/config/xds_resource.h"
#include "source/common/memory/utils.h"
#include "source/common/protobuf/protobuf.h"
#include "source/common/protobuf/utility.h"
namespace Envoy {
namespace Config {
namespace {
class AllMuxesState {
public:
void insert(NewGrpcMuxImpl* mux) { muxes_.insert(mux); }
void erase(NewGrpcMuxImpl* mux) { muxes_.erase(mux); }
void shutdownAll() {
for (auto& mux : muxes_) {
mux->shutdown();
}
}
private:
absl::flat_hash_set<NewGrpcMuxImpl*> muxes_;
};
using AllMuxes = ThreadSafeSingleton<AllMuxesState>;
} // namespace
NewGrpcMuxImpl::NewGrpcMuxImpl(Grpc::RawAsyncClientPtr&& async_client,
Event::Dispatcher& dispatcher,
const Protobuf::MethodDescriptor& service_method,
Random::RandomGenerator& random, Stats::Scope& scope,
const RateLimitSettings& rate_limit_settings,
const LocalInfo::LocalInfo& local_info,
CustomConfigValidatorsPtr&& config_validators)
: grpc_stream_(this, std::move(async_client), service_method, random, dispatcher, scope,
rate_limit_settings),
local_info_(local_info), config_validators_(std::move(config_validators)),
dynamic_update_callback_handle_(local_info.contextProvider().addDynamicContextUpdateCallback(
[this](absl::string_view resource_type_url) {
onDynamicContextUpdate(resource_type_url);
})),
dispatcher_(dispatcher) {
AllMuxes::get().insert(this);
}
NewGrpcMuxImpl::~NewGrpcMuxImpl() { AllMuxes::get().erase(this); }
void NewGrpcMuxImpl::shutdownAll() { AllMuxes::get().shutdownAll(); }
void NewGrpcMuxImpl::onDynamicContextUpdate(absl::string_view resource_type_url) {
auto sub = subscriptions_.find(resource_type_url);
if (sub == subscriptions_.end()) {
return;
}
sub->second->sub_state_.setMustSendDiscoveryRequest();
trySendDiscoveryRequests();
}
ScopedResume NewGrpcMuxImpl::pause(const std::string& type_url) {
return pause(std::vector<std::string>{type_url});
}
ScopedResume NewGrpcMuxImpl::pause(const std::vector<std::string> type_urls) {
for (const auto& type_url : type_urls) {
pausable_ack_queue_.pause(type_url);
}
return std::make_unique<Cleanup>([this, type_urls]() {
for (const auto& type_url : type_urls) {
pausable_ack_queue_.resume(type_url);
if (!pausable_ack_queue_.paused(type_url)) {
trySendDiscoveryRequests();
}
}
});
}
void NewGrpcMuxImpl::onDiscoveryResponse(
std::unique_ptr<envoy::service::discovery::v3::DeltaDiscoveryResponse>&& message,
ControlPlaneStats& control_plane_stats) {
ENVOY_LOG(debug, "Received DeltaDiscoveryResponse for {} at version {}", message->type_url(),
message->system_version_info());
auto sub = subscriptions_.find(message->type_url());
if (sub == subscriptions_.end()) {
ENVOY_LOG(warn,
"Dropping received DeltaDiscoveryResponse (with version {}) for non-existent "
"subscription {}.",
message->system_version_info(), message->type_url());
return;
}
if (message->has_control_plane()) {
control_plane_stats.identifier_.set(message->control_plane().identifier());
if (message->control_plane().identifier() != sub->second->control_plane_identifier_) {
sub->second->control_plane_identifier_ = message->control_plane().identifier();
ENVOY_LOG(debug, "Receiving gRPC updates for {} from {}", message->type_url(),
sub->second->control_plane_identifier_);
}
}
kickOffAck(sub->second->sub_state_.handleResponse(*message));
Memory::Utils::tryShrinkHeap();
}
void NewGrpcMuxImpl::onStreamEstablished() {
for (auto& [type_url, subscription] : subscriptions_) {
UNREFERENCED_PARAMETER(type_url);
subscription->sub_state_.markStreamFresh();
}
pausable_ack_queue_.clear();
trySendDiscoveryRequests();
}
void NewGrpcMuxImpl::onEstablishmentFailure() {
// If this happens while Envoy is still initializing, the onConfigUpdateFailed() we ultimately
// call on CDS will cause LDS to start up, which adds to subscriptions_ here. So, to avoid a
// crash, the iteration needs to dance around a little: collect pointers to all
// SubscriptionStates, call on all those pointers we haven't yet called on, repeat if there are
// now more SubscriptionStates.
absl::flat_hash_map<std::string, DeltaSubscriptionState*> all_subscribed;
absl::flat_hash_map<std::string, DeltaSubscriptionState*> already_called;
do {
for (auto& [type_url, subscription] : subscriptions_) {
all_subscribed[type_url] = &subscription->sub_state_;
}
for (auto& sub : all_subscribed) {
if (already_called.insert(sub).second) { // insert succeeded ==> not already called
sub.second->handleEstablishmentFailure();
}
}
} while (all_subscribed.size() != subscriptions_.size());
}
void NewGrpcMuxImpl::onWriteable() { trySendDiscoveryRequests(); }
void NewGrpcMuxImpl::kickOffAck(UpdateAck ack) {
pausable_ack_queue_.push(std::move(ack));
trySendDiscoveryRequests();
}
// TODO(fredlas) to be removed from the GrpcMux interface very soon.
void NewGrpcMuxImpl::start() { grpc_stream_.establishNewStream(); }
GrpcMuxWatchPtr NewGrpcMuxImpl::addWatch(const std::string& type_url,
const absl::flat_hash_set<std::string>& resources,
SubscriptionCallbacks& callbacks,
OpaqueResourceDecoder& resource_decoder,
const SubscriptionOptions& options) {
auto entry = subscriptions_.find(type_url);
if (entry == subscriptions_.end()) {
// We don't yet have a subscription for type_url! Make one!
addSubscription(type_url, options.use_namespace_matching_);
return addWatch(type_url, resources, callbacks, resource_decoder, options);
}
Watch* watch = entry->second->watch_map_.addWatch(callbacks, resource_decoder);
// updateWatch() queues a discovery request if any of 'resources' are not yet subscribed.
updateWatch(type_url, watch, resources, options);
return std::make_unique<WatchImpl>(type_url, watch, *this, options);
}
// Updates the list of resource names watched by the given watch. If an added name is new across
// the whole subscription, or if a removed name has no other watch interested in it, then the
// subscription will enqueue and attempt to send an appropriate discovery request.
void NewGrpcMuxImpl::updateWatch(const std::string& type_url, Watch* watch,
const absl::flat_hash_set<std::string>& resources,
const SubscriptionOptions& options) {
ASSERT(watch != nullptr);
auto sub = subscriptions_.find(type_url);
RELEASE_ASSERT(sub != subscriptions_.end(),
fmt::format("Watch of {} has no subscription to update.", type_url));
// We need to prepare xdstp:// resources for the transport, by normalizing and adding any extra
// context parameters.
absl::flat_hash_set<std::string> effective_resources;
for (const auto& resource : resources) {
if (XdsResourceIdentifier::hasXdsTpScheme(resource)) {
auto xdstp_resource = XdsResourceIdentifier::decodeUrn(resource);
if (options.add_xdstp_node_context_params_) {
const auto context = XdsContextParams::encodeResource(
local_info_.contextProvider().nodeContext(), xdstp_resource.context(), {}, {});
xdstp_resource.mutable_context()->CopyFrom(context);
}
XdsResourceIdentifier::EncodeOptions encode_options;
encode_options.sort_context_params_ = true;
effective_resources.insert(XdsResourceIdentifier::encodeUrn(xdstp_resource, encode_options));
} else {
effective_resources.insert(resource);
}
}
auto added_removed = sub->second->watch_map_.updateWatchInterest(watch, effective_resources);
if (options.use_namespace_matching_) {
// This is to prevent sending out of requests that contain prefixes instead of resource names
sub->second->sub_state_.updateSubscriptionInterest({}, {});
} else {
sub->second->sub_state_.updateSubscriptionInterest(added_removed.added_,
added_removed.removed_);
}
// Tell the server about our change in interest, if any.
if (sub->second->sub_state_.subscriptionUpdatePending()) {
trySendDiscoveryRequests();
}
}
void NewGrpcMuxImpl::requestOnDemandUpdate(const std::string& type_url,
const absl::flat_hash_set<std::string>& for_update) {
auto sub = subscriptions_.find(type_url);
RELEASE_ASSERT(sub != subscriptions_.end(),
fmt::format("Watch of {} has no subscription to update.", type_url));
sub->second->sub_state_.updateSubscriptionInterest(for_update, {});
// Tell the server about our change in interest, if any.
if (sub->second->sub_state_.subscriptionUpdatePending()) {
trySendDiscoveryRequests();
}
}
void NewGrpcMuxImpl::removeWatch(const std::string& type_url, Watch* watch) {
updateWatch(type_url, watch, {}, {});
auto entry = subscriptions_.find(type_url);
ASSERT(entry != subscriptions_.end(),
fmt::format("removeWatch() called for non-existent subscription {}.", type_url));
entry->second->watch_map_.removeWatch(watch);
}
void NewGrpcMuxImpl::addSubscription(const std::string& type_url,
const bool use_namespace_matching) {
subscriptions_.emplace(
type_url, std::make_unique<SubscriptionStuff>(type_url, local_info_, use_namespace_matching,
dispatcher_, *config_validators_.get()));
subscription_ordering_.emplace_back(type_url);
}
void NewGrpcMuxImpl::trySendDiscoveryRequests() {
if (shutdown_) {
return;
}
while (true) {
// Do any of our subscriptions even want to send a request?
absl::optional<std::string> maybe_request_type = whoWantsToSendDiscoveryRequest();
if (!maybe_request_type.has_value()) {
break;
}
// If so, which one (by type_url)?
std::string next_request_type_url = maybe_request_type.value();
// If we don't have a subscription object for this request's type_url, drop the request.
auto sub = subscriptions_.find(next_request_type_url);
RELEASE_ASSERT(sub != subscriptions_.end(),
fmt::format("Tried to send discovery request for non-existent subscription {}.",
next_request_type_url));
// Try again later if paused/rate limited/stream down.
if (!canSendDiscoveryRequest(next_request_type_url)) {
break;
}
envoy::service::discovery::v3::DeltaDiscoveryRequest request;
// Get our subscription state to generate the appropriate DeltaDiscoveryRequest, and send.
if (!pausable_ack_queue_.empty()) {
// Because ACKs take precedence over plain requests, if there is anything in the queue, it's
// safe to assume it's of the type_url that we're wanting to send.
request = sub->second->sub_state_.getNextRequestWithAck(pausable_ack_queue_.popFront());
} else {
request = sub->second->sub_state_.getNextRequestAckless();
}
grpc_stream_.sendMessage(request);
}
grpc_stream_.maybeUpdateQueueSizeStat(pausable_ack_queue_.size());
}
// Checks whether external conditions allow sending a DeltaDiscoveryRequest. (Does not check
// whether we *want* to send a DeltaDiscoveryRequest).
bool NewGrpcMuxImpl::canSendDiscoveryRequest(const std::string& type_url) {
RELEASE_ASSERT(
!pausable_ack_queue_.paused(type_url),
fmt::format("canSendDiscoveryRequest() called on paused type_url {}. Pausedness is "
"supposed to be filtered out by whoWantsToSendDiscoveryRequest(). ",
type_url));
if (!grpc_stream_.grpcStreamAvailable()) {
ENVOY_LOG(trace, "No stream available to send a discovery request for {}.", type_url);
return false;
} else if (!grpc_stream_.checkRateLimitAllowsDrain()) {
ENVOY_LOG(trace, "{} discovery request hit rate limit; will try later.", type_url);
return false;
}
return true;
}
// Checks whether we have something to say in a DeltaDiscoveryRequest, which can be an ACK and/or
// a subscription update. (Does not check whether we *can* send that DeltaDiscoveryRequest).
// Returns the type_url we should send the DeltaDiscoveryRequest for (if any).
// First, prioritizes ACKs over non-ACK subscription interest updates.
// Then, prioritizes non-ACK updates in the order the various types
// of subscriptions were activated.
absl::optional<std::string> NewGrpcMuxImpl::whoWantsToSendDiscoveryRequest() {
// All ACKs are sent before plain updates. trySendDiscoveryRequests() relies on this. So, choose
// type_url from pausable_ack_queue_ if possible, before looking at pending updates.
if (!pausable_ack_queue_.empty()) {
return pausable_ack_queue_.front().type_url_;
}
// If we're looking to send multiple non-ACK requests, send them in the order that their
// subscriptions were initiated.
for (const auto& sub_type : subscription_ordering_) {
auto sub = subscriptions_.find(sub_type);
if (sub != subscriptions_.end() && sub->second->sub_state_.subscriptionUpdatePending() &&
!pausable_ack_queue_.paused(sub_type)) {
return sub->first;
}
}
return absl::nullopt;
}
} // namespace Config
} // namespace Envoy