9
9
10
10
package org .elasticsearch .transport ;
11
11
12
+ import org .apache .logging .log4j .Level ;
13
+ import org .elasticsearch .TransportVersion ;
12
14
import org .elasticsearch .action .ActionListener ;
15
+ import org .elasticsearch .action .support .PlainActionFuture ;
16
+ import org .elasticsearch .cluster .ClusterName ;
17
+ import org .elasticsearch .cluster .metadata .ProjectId ;
18
+ import org .elasticsearch .cluster .node .VersionInformation ;
13
19
import org .elasticsearch .common .settings .Settings ;
14
20
import org .elasticsearch .common .util .concurrent .ThreadContext ;
21
+ import org .elasticsearch .core .Strings ;
15
22
import org .elasticsearch .core .TimeValue ;
16
23
import org .elasticsearch .test .ESTestCase ;
17
24
import org .elasticsearch .test .EnumSerializationTestUtils ;
25
+ import org .elasticsearch .test .MockLog ;
26
+ import org .elasticsearch .test .junit .annotations .TestLogging ;
27
+ import org .elasticsearch .test .transport .MockTransportService ;
28
+ import org .elasticsearch .threadpool .TestThreadPool ;
29
+ import org .elasticsearch .threadpool .ThreadPool ;
18
30
31
+ import static org .elasticsearch .test .MockLog .assertThatLogger ;
19
32
import static org .elasticsearch .transport .RemoteClusterSettings .ProxyConnectionStrategySettings .PROXY_ADDRESS ;
20
33
import static org .elasticsearch .transport .RemoteClusterSettings .REMOTE_CONNECTION_MODE ;
21
34
import static org .elasticsearch .transport .RemoteClusterSettings .SniffConnectionStrategySettings .REMOTE_CLUSTER_SEEDS ;
@@ -171,21 +184,148 @@ public void testConnectionStrategySerialization() {
171
184
);
172
185
}
173
186
187
+ @ TestLogging (
188
+ value = "org.elasticsearch.transport.RemoteConnectionStrategyTests.FakeConnectionStrategy:DEBUG" ,
189
+ reason = "logging verification"
190
+ )
191
+ public void testConnectionAttemptLogging () {
192
+ final var originProjectId = randomUniqueProjectId ();
193
+ final var linkedProjectId = randomUniqueProjectId ();
194
+ final var alias = randomAlphanumericOfLength (10 );
195
+
196
+ try (
197
+ var threadPool = new TestThreadPool (getClass ().getName ());
198
+ var transportService = startTransport (threadPool );
199
+ var connectionManager = new RemoteConnectionManager (
200
+ alias ,
201
+ RemoteClusterCredentialsManager .EMPTY ,
202
+ new ClusterConnectionManager (TestProfiles .LIGHT_PROFILE , mock (Transport .class ), threadContext )
203
+ )
204
+ ) {
205
+ for (boolean shouldConnectFail : new boolean [] { true , false }) {
206
+ for (boolean isIntialConnectAttempt : new boolean [] { true , false }) {
207
+ final var strategy = new FakeConnectionStrategy (
208
+ originProjectId ,
209
+ linkedProjectId ,
210
+ alias ,
211
+ transportService ,
212
+ connectionManager
213
+ );
214
+ if (isIntialConnectAttempt == false ) {
215
+ waitForConnect (strategy );
216
+ }
217
+ strategy .setShouldConnectFail (shouldConnectFail );
218
+ final var expectedLogLevel = shouldConnectFail ? Level .WARN : Level .DEBUG ;
219
+ final var expectedLogMessage = Strings .format (
220
+ "Origin project [%s] %s to linked project [%s] with alias [%s] on %s attempt" ,
221
+ originProjectId ,
222
+ shouldConnectFail ? "failed to connect" : "successfully connected" ,
223
+ linkedProjectId ,
224
+ alias ,
225
+ isIntialConnectAttempt ? "the initial connection" : "a reconnection"
226
+ );
227
+ assertThatLogger (() -> {
228
+ if (shouldConnectFail ) {
229
+ assertThrows (RuntimeException .class , () -> waitForConnect (strategy ));
230
+ } else {
231
+ waitForConnect (strategy );
232
+ }
233
+ },
234
+ strategy .getClass (),
235
+ new MockLog .SeenEventExpectation (
236
+ "connection strategy should log at "
237
+ + expectedLogLevel
238
+ + " after a "
239
+ + (shouldConnectFail ? "failed" : "successful" )
240
+ + (isIntialConnectAttempt ? " initial connection attempt" : " reconnection attempt" ),
241
+ strategy .getClass ().getCanonicalName (),
242
+ expectedLogLevel ,
243
+ expectedLogMessage
244
+ )
245
+ );
246
+ }
247
+ }
248
+ }
249
+ }
250
+
251
+ private MockTransportService startTransport (ThreadPool threadPool ) {
252
+ boolean success = false ;
253
+ final Settings s = Settings .builder ().put (ClusterName .CLUSTER_NAME_SETTING .getKey (), "cluster1" ).put ("node.name" , "node1" ).build ();
254
+ MockTransportService newService = MockTransportService .createNewService (
255
+ s ,
256
+ VersionInformation .CURRENT ,
257
+ TransportVersion .current (),
258
+ threadPool
259
+ );
260
+ try {
261
+ newService .start ();
262
+ newService .acceptIncomingRequests ();
263
+ success = true ;
264
+ return newService ;
265
+ } finally {
266
+ if (success == false ) {
267
+ newService .close ();
268
+ }
269
+ }
270
+ }
271
+
272
+ private static void waitForConnect (RemoteConnectionStrategy strategy ) {
273
+ PlainActionFuture <Void > connectFuture = new PlainActionFuture <>();
274
+ strategy .connect (connectFuture );
275
+ connectFuture .actionGet ();
276
+ }
277
+
174
278
private static class FakeConnectionStrategy extends RemoteConnectionStrategy {
175
279
176
280
private final ConnectionStrategy strategy ;
281
+ private boolean shouldConnectFail ;
177
282
178
283
FakeConnectionStrategy (
284
+ ProjectId originProjectId ,
285
+ ProjectId linkedProjectId ,
286
+ String clusterAlias ,
287
+ TransportService transportService ,
288
+ RemoteConnectionManager connectionManager
289
+ ) {
290
+ this (
291
+ originProjectId ,
292
+ linkedProjectId ,
293
+ clusterAlias ,
294
+ transportService ,
295
+ connectionManager ,
296
+ randomFrom (RemoteConnectionStrategy .ConnectionStrategy .values ())
297
+ );
298
+ }
299
+
300
+ FakeConnectionStrategy (
301
+ String clusterAlias ,
302
+ TransportService transportService ,
303
+ RemoteConnectionManager connectionManager ,
304
+ RemoteConnectionStrategy .ConnectionStrategy strategy
305
+ ) {
306
+ this (ProjectId .DEFAULT , ProjectId .DEFAULT , clusterAlias , transportService , connectionManager , strategy );
307
+ }
308
+
309
+ FakeConnectionStrategy (
310
+ ProjectId originProjectId ,
311
+ ProjectId linkedProjectId ,
179
312
String clusterAlias ,
180
313
TransportService transportService ,
181
314
RemoteConnectionManager connectionManager ,
182
315
RemoteConnectionStrategy .ConnectionStrategy strategy
183
316
) {
184
317
super (switch (strategy ) {
185
- case PROXY -> new LinkedProjectConfig .ProxyLinkedProjectConfigBuilder (clusterAlias ).build ();
186
- case SNIFF -> new LinkedProjectConfig .SniffLinkedProjectConfigBuilder (clusterAlias ).build ();
318
+ case PROXY -> new LinkedProjectConfig .ProxyLinkedProjectConfigBuilder (originProjectId , linkedProjectId , clusterAlias )
319
+ .build ();
320
+ case SNIFF -> new LinkedProjectConfig .SniffLinkedProjectConfigBuilder (originProjectId , linkedProjectId , clusterAlias )
321
+ .build ();
187
322
}, transportService , connectionManager );
188
323
this .strategy = strategy ;
324
+ this .shouldConnectFail = false ;
325
+ }
326
+
327
+ void setShouldConnectFail (boolean shouldConnectFail ) {
328
+ this .shouldConnectFail = shouldConnectFail ;
189
329
}
190
330
191
331
@ Override
@@ -205,7 +345,11 @@ protected boolean shouldOpenMoreConnections() {
205
345
206
346
@ Override
207
347
protected void connectImpl (ActionListener <Void > listener ) {
208
-
348
+ if (shouldConnectFail ) {
349
+ listener .onFailure (new RuntimeException ("simulated failure" ));
350
+ } else {
351
+ listener .onResponse (null );
352
+ }
209
353
}
210
354
211
355
@ Override
0 commit comments