1313import org .elasticsearch .common .settings .Settings ;
1414import org .elasticsearch .index .query .QueryBuilder ;
1515import org .elasticsearch .index .query .RangeQueryBuilder ;
16+ import org .elasticsearch .test .transport .MockTransportService ;
17+ import org .elasticsearch .transport .NoSuchRemoteClusterException ;
18+ import org .elasticsearch .transport .TransportChannel ;
19+ import org .elasticsearch .transport .TransportResponse ;
20+ import org .elasticsearch .transport .TransportService ;
1621import org .elasticsearch .xpack .esql .VerificationException ;
1722
1823import java .io .IOException ;
@@ -346,12 +351,7 @@ public void testFilterWithMissingRemoteIndex() {
346351 }
347352 }
348353
349- public void testFilterWithUnavailableRemote () throws IOException {
350- int docsTest1 = 50 ;
351- int localShards = randomIntBetween (1 , 5 );
352- populateDateIndex (LOCAL_CLUSTER , LOCAL_INDEX , localShards , docsTest1 , "2024-11-26" );
353- cluster (REMOTE_CLUSTER_1 ).close ();
354-
354+ private void checkRemoteFailures () {
355355 for (var filter : List .of (
356356 new RangeQueryBuilder ("@timestamp" ).from ("2024-01-01" ).to ("now" ),
357357 new RangeQueryBuilder ("@timestamp" ).from ("2025-01-01" ).to ("now" )
@@ -365,13 +365,11 @@ public void testFilterWithUnavailableRemote() throws IOException {
365365 }
366366 }
367367
368- public void testFilterWithUnavailableRemoteAndSkipUnavailable () throws IOException {
369- setSkipUnavailable ( REMOTE_CLUSTER_1 , true ) ;
368+ private void checkRemoteWithSkipUnavailable () {
369+ int count = 0 ;
370370 int docsTest1 = 50 ;
371371 int localShards = randomIntBetween (1 , 5 );
372372 populateDateIndex (LOCAL_CLUSTER , LOCAL_INDEX , localShards , docsTest1 , "2024-11-26" );
373- cluster (REMOTE_CLUSTER_1 ).close ();
374- int count = 0 ;
375373
376374 for (var filter : List .of (
377375 new RangeQueryBuilder ("@timestamp" ).from ("2024-01-01" ).to ("now" ),
@@ -438,7 +436,75 @@ public void testFilterWithUnavailableRemoteAndSkipUnavailable() throws IOExcepti
438436 }
439437 }
440438 }
439+ }
440+
441+ public void testFilterWithUnavailableRemote () throws IOException {
442+ int docsTest1 = 50 ;
443+ int localShards = randomIntBetween (1 , 5 );
444+ populateDateIndex (LOCAL_CLUSTER , LOCAL_INDEX , localShards , docsTest1 , "2024-11-26" );
445+ cluster (REMOTE_CLUSTER_1 ).close ();
446+ checkRemoteFailures ();
447+ }
441448
449+ private void makeRemoteFailFieldCaps () {
450+ for (TransportService transportService : cluster (REMOTE_CLUSTER_1 ).getInstances (TransportService .class )) {
451+ MockTransportService ts = asInstanceOf (MockTransportService .class , transportService );
452+ ts .addRequestHandlingBehavior (
453+ EsqlResolveFieldsAction .NAME ,
454+ (handler , request , channel , task ) -> handler .messageReceived (request , new TransportChannel () {
455+ @ Override
456+ public String getProfileName () {
457+ return channel .getProfileName ();
458+ }
459+
460+ @ Override
461+ public void sendResponse (TransportResponse response ) {
462+ sendResponse (new NoSuchRemoteClusterException ("cluster [cluster-a] not found, skipping" ));
463+ }
464+
465+ @ Override
466+ public void sendResponse (Exception exception ) {
467+ channel .sendResponse (exception );
468+ }
469+ }, task )
470+ );
471+ }
472+ }
473+
474+ private void clearRemoteRules () {
475+ for (TransportService transportService : cluster (REMOTE_CLUSTER_1 ).getInstances (TransportService .class )) {
476+ MockTransportService ts = asInstanceOf (MockTransportService .class , transportService );
477+ ts .clearAllRules ();
478+ }
479+ }
480+
481+ // Test when the disconnect happens on the field-caps call itself
482+ public void testFilterWithUnavailableOnFieldcaps () throws IOException {
483+ int docsTest1 = 50 ;
484+ int localShards = randomIntBetween (1 , 5 );
485+ populateDateIndex (LOCAL_CLUSTER , LOCAL_INDEX , localShards , docsTest1 , "2024-11-26" );
486+ makeRemoteFailFieldCaps ();
487+ try {
488+ checkRemoteFailures ();
489+ } finally {
490+ clearRemoteRules ();
491+ }
492+ }
493+
494+ public void testFilterWithUnavailableRemoteAndSkipUnavailable () throws IOException {
495+ setSkipUnavailable (REMOTE_CLUSTER_1 , true );
496+ cluster (REMOTE_CLUSTER_1 ).close ();
497+ checkRemoteWithSkipUnavailable ();
498+ }
499+
500+ public void testFilterWithUnavailableFieldCapsAndSkipUnavailable () throws IOException {
501+ setSkipUnavailable (REMOTE_CLUSTER_1 , true );
502+ makeRemoteFailFieldCaps ();
503+ try {
504+ checkRemoteWithSkipUnavailable ();
505+ } finally {
506+ clearRemoteRules ();
507+ }
442508 }
443509
444510 protected void populateDateIndex (String clusterAlias , String indexName , int numShards , int numDocs , String date ) {
0 commit comments