| 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 |
#ifndef GRPC_TEST_CPP_END2END_CONNECTION_ATTEMPT_INJECTOR_H |
| 16 |
#define GRPC_TEST_CPP_END2END_CONNECTION_ATTEMPT_INJECTOR_H |
| 17 |
|
| 18 |
#include <memory> |
| 19 |
|
| 20 |
#include "src/core/lib/event_engine/channel_args_endpoint_config.h" |
| 21 |
#include "src/core/lib/gprpp/time.h" |
| 22 |
#include "src/core/lib/iomgr/tcp_client.h" |
| 23 |
#include "src/core/lib/iomgr/timer.h" |
| 24 |
|
| 25 |
namespace grpc { |
| 26 |
namespace testing { |
| 27 |
|
| 28 |
// Allows injecting connection-establishment delays into C-core. |
| 29 |
// Typical usage: |
| 30 |
// |
| 31 |
// // At grpc_init() time. |
| 32 |
// ConnectionAttemptInjector::Init(); |
| 33 |
// |
| 34 |
// // Instantiate when injection is desired. |
| 35 |
// ConnectionAttemptInjector injector; |
| 36 |
// |
| 37 |
// // To inject a hold for the next connection attempt for a given port. |
| 38 |
// auto hold = injector.AddHold(port); |
| 39 |
// hold.Wait(); |
| 40 |
// // ...do stuff... |
| 41 |
// hold.Resume(); // Or hold.Fail() if you want to force a failure. |
| 42 |
// |
| 43 |
// // Inject a fixed delay for all connection attempts. |
| 44 |
// injector.SetDelay(grpc_core::Duration::Seconds(10)); |
| 45 |
// |
| 46 |
// The injection is global, so there must be only one ConnectionAttemptInjector |
| 47 |
// object at any one time. |
| 48 |
// |
| 49 |
// Note: This must be "final" to avoid tsan problems in both the ctor |
| 50 |
// and dtor related to initializing the vtable. |
| 51 |
class ConnectionAttemptInjector final { |
| 52 |
private: |
| 53 |
// Forward declarations. |
| 54 |
class QueuedAttempt; |
| 55 |
|
| 56 |
grpc_core::Mutex mu_; |
| 57 |
|
| 58 |
public: |
| 59 |
class Hold { |
| 60 |
public: |
| 61 |
// Do not instantiate directly -- must be created via AddHold(). |
| 62 |
Hold(ConnectionAttemptInjector* injector, int port, |
| 63 |
bool intercept_completion); |
| 64 |
|
| 65 |
// Waits for the connection attempt to start. |
| 66 |
// After this returns, exactly one of Resume() or Fail() must be called. |
| 67 |
void Wait(); |
| 68 |
|
| 69 |
// Resumes a connection attempt. Must be called after Wait(). |
| 70 |
void Resume(); |
| 71 |
|
| 72 |
// Fails a connection attempt. Must be called after Wait(). |
| 73 |
void Fail(grpc_error_handle error); |
| 74 |
|
| 75 |
// If the hold was created with intercept_completion=true, then this |
| 76 |
// can be called after Resume() to wait for the connection attempt |
| 77 |
// to complete. |
| 78 |
void WaitForCompletion(); |
| 79 |
|
| 80 |
// Returns true if the connection attempt has been started. |
| 81 |
bool IsStarted(); |
| 82 |
|
| 83 |
private: |
| 84 |
friend class ConnectionAttemptInjector; |
| 85 |
|
| 86 |
static void OnComplete(void* arg, grpc_error_handle error); |
| 87 |
|
| 88 |
ConnectionAttemptInjector* injector_; |
| 89 |
const int port_; |
| 90 |
const bool intercept_completion_; |
| 91 |
std::unique_ptr<QueuedAttempt> queued_attempt_ |
| 92 |
ABSL_GUARDED_BY(&ConnectionAttemptInjector::mu_); |
| 93 |
grpc_core::CondVar start_cv_; |
| 94 |
grpc_closure on_complete_; |
| 95 |
grpc_closure* original_on_complete_; |
| 96 |
grpc_core::CondVar complete_cv_; |
| 97 |
}; |
| 98 |
|
| 99 |
// Global initializer. Replaces the iomgr TCP client vtable. |
| 100 |
// Must be called exactly once after grpc_init() but before any TCP |
| 101 |
// connections are established. |
| 102 |
static void Init(); |
| 103 |
|
| 104 |
ConnectionAttemptInjector(); |
| 105 |
~ConnectionAttemptInjector(); |
| 106 |
|
| 107 |
// Adds a hold for a given port. The caller may then use Wait() on |
| 108 |
// the resulting Hold object to wait for the connection attempt to start. |
| 109 |
// If intercept_completion is true, the caller can use WaitForCompletion() |
| 110 |
// on the resulting Hold object. |
| 111 |
std::unique_ptr<Hold> AddHold(int port, bool intercept_completion = false); |
| 112 |
|
| 113 |
// Set a fixed delay for all RPCs. Will be used only if there is no |
| 114 |
// hold for the connection attempt. |
| 115 |
void SetDelay(grpc_core::Duration delay); |
| 116 |
|
| 117 |
private: |
| 118 |
static grpc_tcp_client_vtable kDelayedConnectVTable; |
| 119 |
|
| 120 |
// Represents a queued attempt. |
| 121 |
// The caller must invoke either Resume() or Fail() before destroying. |
| 122 |
class QueuedAttempt { |
| 123 |
public: |
| 124 |
QueuedAttempt(grpc_closure* closure, grpc_endpoint** ep, |
| 125 |
grpc_pollset_set* interested_parties, |
| 126 |
const grpc_event_engine::experimental::EndpointConfig& config, |
| 127 |
const grpc_resolved_address* addr, |
| 128 |
grpc_core::Timestamp deadline); |
| 129 |
~QueuedAttempt(); |
| 130 |
|
| 131 |
// Caller must invoke this from a thread with an ExecCtx. |
| 132 |
void Resume(); |
| 133 |
|
| 134 |
// Caller must invoke this from a thread with an ExecCtx. |
| 135 |
void Fail(grpc_error_handle error); |
| 136 |
|
| 137 |
private: |
| 138 |
grpc_closure* closure_; |
| 139 |
grpc_endpoint** endpoint_; |
| 140 |
grpc_pollset_set* interested_parties_; |
| 141 |
grpc_event_engine::experimental::ChannelArgsEndpointConfig config_; |
| 142 |
grpc_resolved_address address_; |
| 143 |
grpc_core::Timestamp deadline_; |
| 144 |
}; |
| 145 |
|
| 146 |
// Injects a delay before continuing a connection attempt. |
| 147 |
class InjectedDelay { |
| 148 |
public: |
| 149 |
virtual ~InjectedDelay() = default; |
| 150 |
|
| 151 |
InjectedDelay(grpc_core::Duration duration, grpc_closure* closure, |
| 152 |
grpc_endpoint** ep, grpc_pollset_set* interested_parties, |
| 153 |
const grpc_event_engine::experimental::EndpointConfig& config, |
| 154 |
const grpc_resolved_address* addr, |
| 155 |
grpc_core::Timestamp deadline); |
| 156 |
|
| 157 |
private: |
| 158 |
static void TimerCallback(void* arg, grpc_error_handle /*error*/); |
| 159 |
|
| 160 |
QueuedAttempt attempt_; |
| 161 |
grpc_timer timer_; |
| 162 |
grpc_closure timer_callback_; |
| 163 |
}; |
| 164 |
|
| 165 |
// Invoked for every TCP connection attempt. |
| 166 |
void HandleConnection( |
| 167 |
grpc_closure* closure, grpc_endpoint** ep, |
| 168 |
grpc_pollset_set* interested_parties, |
| 169 |
const grpc_event_engine::experimental::EndpointConfig& config, |
| 170 |
const grpc_resolved_address* addr, grpc_core::Timestamp deadline); |
| 171 |
|
| 172 |
static void AttemptConnection( |
| 173 |
grpc_closure* closure, grpc_endpoint** ep, |
| 174 |
grpc_pollset_set* interested_parties, |
| 175 |
const grpc_event_engine::experimental::EndpointConfig& config, |
| 176 |
const grpc_resolved_address* addr, grpc_core::Timestamp deadline); |
| 177 |
|
| 178 |
// Replacement iomgr tcp_connect vtable functions that use the current |
| 179 |
// ConnectionAttemptInjector object. |
| 180 |
static int64_t TcpConnect( |
| 181 |
grpc_closure* closure, grpc_endpoint** ep, |
| 182 |
grpc_pollset_set* interested_parties, |
| 183 |
const grpc_event_engine::experimental::EndpointConfig& config, |
| 184 |
const grpc_resolved_address* addr, grpc_core::Timestamp deadline); |
| 185 |
static bool TcpConnectCancel(int64_t connection_handle); |
| 186 |
|
| 187 |
std::vector<Hold*> holds_ ABSL_GUARDED_BY(&mu_); |
| 188 |
absl::optional<grpc_core::Duration> delay_ ABSL_GUARDED_BY(&mu_); |
| 189 |
}; |
| 190 |
|
| 191 |
} // namespace testing |
| 192 |
} // namespace grpc |
| 193 |
|
| 194 |
#endif // GRPC_TEST_CPP_END2END_CONNECTION_ATTEMPT_INJECTOR_H |