Debian Codegraph @

test/cpp/end2end/connection_attempt_injector.cc

raw
1 // Copyright 2016 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "test/cpp/end2end/connection_attempt_injector.h"
16
17 #include <memory>
18
19 #include "absl/memory/memory.h"
20 #include "absl/utility/utility.h"
21
22 #include "src/core/lib/address_utils/sockaddr_utils.h"
23 #include "src/core/lib/gprpp/sync.h"
24
25 // defined in tcp_client.cc
26 extern grpc_tcp_client_vtable* grpc_tcp_client_impl;
27
28 using ::grpc_event_engine::experimental::EndpointConfig;
29
30 namespace grpc {
31 namespace testing {
32
33 //
34 // ConnectionAttemptInjector static setup
35 //
36
37 namespace {
38
39 grpc_tcp_client_vtable* g_original_vtable = nullptr;
40
41 grpc_core::Mutex* g_mu = nullptr;
42 ConnectionAttemptInjector* g_injector ABSL_GUARDED_BY(*g_mu) = nullptr;
43
44 } // namespace
45
46 grpc_tcp_client_vtable ConnectionAttemptInjector::kDelayedConnectVTable = {
47 ConnectionAttemptInjector::TcpConnect,
48 ConnectionAttemptInjector::TcpConnectCancel};
49
50 void ConnectionAttemptInjector::Init() {
51 g_mu = new grpc_core::Mutex();
52 g_original_vtable = grpc_tcp_client_impl;
53 grpc_tcp_client_impl = &kDelayedConnectVTable;
54 }
55
56 int64_t ConnectionAttemptInjector::TcpConnect(
57 grpc_closure* closure, grpc_endpoint** ep,
58 grpc_pollset_set* interested_parties, const EndpointConfig& config,
59 const grpc_resolved_address* addr, grpc_core::Timestamp deadline) {
60 grpc_core::MutexLock lock(g_mu);
61 // If there's no injector, use the original vtable.
62 if (g_injector == nullptr) {
63 g_original_vtable->connect(closure, ep, interested_parties, config, addr,
64 deadline);
65 return 0;
66 }
67 // Otherwise, use the injector.
68 g_injector->HandleConnection(closure, ep, interested_parties, config, addr,
69 deadline);
70 return 0;
71 }
72
73 // TODO(vigneshbabu): This method should check whether the connect attempt has
74 // actually been started, and if so, it should call
75 // g_original_vtable->cancel_connect(). If the attempt has not actually been
76 // started, it should mark the connect request as cancelled, so that when the
77 // request is resumed, it will not actually proceed.
78 bool ConnectionAttemptInjector::TcpConnectCancel(
79 int64_t /*connection_handle*/) {
80 return false;
81 }
82
83 //
84 // ConnectionAttemptInjector instance
85 //
86
87 ConnectionAttemptInjector::ConnectionAttemptInjector() {
88 // Fail if ConnectionAttemptInjector::Init() was not called after
89 // grpc_init() to inject the vtable.
90 GPR_ASSERT(grpc_tcp_client_impl == &kDelayedConnectVTable);
91 grpc_core::MutexLock lock(g_mu);
92 GPR_ASSERT(g_injector == nullptr);
93 g_injector = this;
94 }
95
96 ConnectionAttemptInjector::~ConnectionAttemptInjector() {
97 grpc_core::MutexLock lock(g_mu);
98 g_injector = nullptr;
99 }
100
101 std::unique_ptr<ConnectionAttemptInjector::Hold>
102 ConnectionAttemptInjector::AddHold(int port, bool intercept_completion) {
103 grpc_core::MutexLock lock(&mu_);
104 auto hold = std::make_unique<Hold>(this, port, intercept_completion);
105 holds_.push_back(hold.get());
106 return hold;
107 }
108
109 void ConnectionAttemptInjector::SetDelay(grpc_core::Duration delay) {
110 grpc_core::MutexLock lock(&mu_);
111 delay_ = delay;
112 }
113
114 void ConnectionAttemptInjector::HandleConnection(
115 grpc_closure* closure, grpc_endpoint** ep,
116 grpc_pollset_set* interested_parties, const EndpointConfig& config,
117 const grpc_resolved_address* addr, grpc_core::Timestamp deadline) {
118 const int port = grpc_sockaddr_get_port(addr);
119 gpr_log(GPR_INFO, "==> HandleConnection(): port=%d", port);
120 {
121 grpc_core::MutexLock lock(&mu_);
122 // First, check if there's a hold request for this port.
123 for (auto it = holds_.begin(); it != holds_.end(); ++it) {
124 Hold* hold = *it;
125 if (port == hold->port_) {
126 gpr_log(GPR_INFO, "*** INTERCEPTING CONNECTION ATTEMPT");
127 if (hold->intercept_completion_) {
128 hold->original_on_complete_ = closure;
129 closure = GRPC_CLOSURE_INIT(&hold->on_complete_, Hold::OnComplete,
130 hold, nullptr);
131 }
132 hold->queued_attempt_ = std::make_unique<QueuedAttempt>(
133 closure, ep, interested_parties, config, addr, deadline);
134 hold->start_cv_.Signal();
135 holds_.erase(it);
136 return;
137 }
138 }
139 // Otherwise, if there's a configured delay, impose it.
140 if (delay_.has_value()) {
141 new InjectedDelay(*delay_, closure, ep, interested_parties, config, addr,
142 deadline);
143 return;
144 }
145 }
146 // Anything we're not holding or delaying should proceed normally.
147 g_original_vtable->connect(closure, ep, interested_parties, config, addr,
148 deadline);
149 }
150
151 //
152 // ConnectionAttemptInjector::QueuedAttempt
153 //
154
155 ConnectionAttemptInjector::QueuedAttempt::QueuedAttempt(
156 grpc_closure* closure, grpc_endpoint** ep,
157 grpc_pollset_set* interested_parties, const EndpointConfig& config,
158 const grpc_resolved_address* addr, grpc_core::Timestamp deadline)
159 : closure_(closure),
160 endpoint_(ep),
161 interested_parties_(interested_parties),
162 config_(*reinterpret_cast<const grpc_event_engine::experimental::
163 ChannelArgsEndpointConfig*>(&config)),
164 deadline_(deadline) {
165 memcpy(&address_, addr, sizeof(address_));
166 }
167
168 ConnectionAttemptInjector::QueuedAttempt::~QueuedAttempt() {
169 GPR_ASSERT(closure_ == nullptr);
170 }
171
172 void ConnectionAttemptInjector::QueuedAttempt::Resume() {
173 GPR_ASSERT(closure_ != nullptr);
174 g_original_vtable->connect(closure_, endpoint_, interested_parties_, config_,
175 &address_, deadline_);
176 closure_ = nullptr;
177 }
178
179 void ConnectionAttemptInjector::QueuedAttempt::Fail(grpc_error_handle error) {
180 GPR_ASSERT(closure_ != nullptr);
181 grpc_core::ExecCtx::Run(DEBUG_LOCATION, closure_, error);
182 closure_ = nullptr;
183 }
184
185 //
186 // ConnectionAttemptInjector::InjectedDelay
187 //
188
189 ConnectionAttemptInjector::InjectedDelay::InjectedDelay(
190 grpc_core::Duration duration, grpc_closure* closure, grpc_endpoint** ep,
191 grpc_pollset_set* interested_parties, const EndpointConfig& config,
192 const grpc_resolved_address* addr, grpc_core::Timestamp deadline)
193 : attempt_(closure, ep, interested_parties, config, addr, deadline) {
194 GRPC_CLOSURE_INIT(&timer_callback_, TimerCallback, this, nullptr);
195 grpc_core::Timestamp now = grpc_core::Timestamp::Now();
196 duration = std::min(duration, deadline - now);
197 grpc_timer_init(&timer_, now + duration, &timer_callback_);
198 }
199
200 void ConnectionAttemptInjector::InjectedDelay::TimerCallback(
201 void* arg, grpc_error_handle /*error*/) {
202 auto* self = static_cast<InjectedDelay*>(arg);
203 self->attempt_.Resume();
204 delete self;
205 }
206
207 //
208 // ConnectionAttemptInjector::Hold
209 //
210
211 ConnectionAttemptInjector::Hold::Hold(ConnectionAttemptInjector* injector,
212 int port, bool intercept_completion)
213 : injector_(injector),
214 port_(port),
215 intercept_completion_(intercept_completion) {}
216
217 void ConnectionAttemptInjector::Hold::Wait() {
218 gpr_log(GPR_INFO, "=== WAITING FOR CONNECTION ATTEMPT ON PORT %d ===", port_);
219 grpc_core::MutexLock lock(&injector_->mu_);
220 while (queued_attempt_ == nullptr) {
221 start_cv_.Wait(&injector_->mu_);
222 }
223 gpr_log(GPR_INFO, "=== CONNECTION ATTEMPT STARTED ON PORT %d ===", port_);
224 }
225
226 void ConnectionAttemptInjector::Hold::Resume() {
227 gpr_log(GPR_INFO, "=== RESUMING CONNECTION ATTEMPT ON PORT %d ===", port_);
228 grpc_core::ExecCtx exec_ctx;
229 std::unique_ptr<QueuedAttempt> attempt;
230 {
231 grpc_core::MutexLock lock(&injector_->mu_);
232 attempt = std::move(queued_attempt_);
233 }
234 attempt->Resume();
235 }
236
237 void ConnectionAttemptInjector::Hold::Fail(grpc_error_handle error) {
238 gpr_log(GPR_INFO, "=== FAILING CONNECTION ATTEMPT ON PORT %d ===", port_);
239 grpc_core::ExecCtx exec_ctx;
240 std::unique_ptr<QueuedAttempt> attempt;
241 {
242 grpc_core::MutexLock lock(&injector_->mu_);
243 attempt = std::move(queued_attempt_);
244 }
245 attempt->Fail(error);
246 }
247
248 void ConnectionAttemptInjector::Hold::WaitForCompletion() {
249 gpr_log(GPR_INFO,
250 "=== WAITING FOR CONNECTION COMPLETION ON PORT %d ===", port_);
251 grpc_core::MutexLock lock(&injector_->mu_);
252 while (original_on_complete_ != nullptr) {
253 complete_cv_.Wait(&injector_->mu_);
254 }
255 gpr_log(GPR_INFO, "=== CONNECTION COMPLETED ON PORT %d ===", port_);
256 }
257
258 bool ConnectionAttemptInjector::Hold::IsStarted() {
259 grpc_core::MutexLock lock(&injector_->mu_);
260 return !start_cv_.WaitWithDeadline(&injector_->mu_, absl::Now());
261 }
262
263 void ConnectionAttemptInjector::Hold::OnComplete(void* arg,
264 grpc_error_handle error) {
265 auto* self = static_cast<Hold*>(arg);
266 grpc_closure* on_complete;
267 {
268 grpc_core::MutexLock lock(&self->injector_->mu_);
269 on_complete = self->original_on_complete_;
270 self->original_on_complete_ = nullptr;
271 self->complete_cv_.Signal();
272 }
273 grpc_core::Closure::Run(DEBUG_LOCATION, on_complete, error);
274 }
275
276 } // namespace testing
277 } // namespace grpc