Skip to content

Commit 30fd5be

Browse files
isSkipUnavailable() returns Optional<T>
1 parent 92297c7 commit 30fd5be

File tree

4 files changed

+24
-15
lines changed

4 files changed

+24
-15
lines changed

server/src/main/java/org/elasticsearch/action/admin/indices/resolve/TransportResolveClusterAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ protected void doExecuteForked(Task task, ResolveClusterActionRequest request, A
174174
resolveClusterTask.ensureNotCancelled();
175175
String clusterAlias = remoteIndices.getKey();
176176
OriginalIndices originalIndices = remoteIndices.getValue();
177-
boolean skipUnavailable = remoteClusterService.isSkipUnavailable(clusterAlias);
177+
boolean skipUnavailable = remoteClusterService.isSkipUnavailable(clusterAlias).orElse(true) == false;
178178
RemoteClusterClient remoteClusterClient = remoteClusterService.getRemoteClusterClient(
179179
clusterAlias,
180180
searchCoordinationExecutor,

server/src/main/java/org/elasticsearch/action/fieldcaps/TransportFieldCapabilitiesAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ private void doExecuteForked(
332332
);
333333

334334
boolean ensureConnected = forceConnectTimeoutSecs != null
335-
|| transportService.getRemoteClusterService().isSkipUnavailable(clusterAlias) == false;
335+
|| transportService.getRemoteClusterService().isSkipUnavailable(clusterAlias).orElse(true) == false;
336336
transportService.getRemoteClusterService()
337337
.maybeEnsureConnectedAndGetConnection(clusterAlias, ensureConnected, connectionListener);
338338
}

server/src/main/java/org/elasticsearch/transport/RemoteClusterService.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.util.Iterator;
4949
import java.util.List;
5050
import java.util.Map;
51+
import java.util.Optional;
5152
import java.util.Set;
5253
import java.util.concurrent.CountDownLatch;
5354
import java.util.concurrent.Executor;
@@ -160,7 +161,7 @@ public boolean isRemoteClusterServerEnabled() {
160161
private final Map<ProjectId, Map<String, RemoteClusterConnection>> remoteClusters;
161162
private final RemoteClusterCredentialsManager remoteClusterCredentialsManager;
162163
private final ProjectResolver projectResolver;
163-
private final boolean inSkippableContext;
164+
private final boolean isCpsEnabled;
164165

165166
@FixForMultiProject(description = "Inject the ProjectResolver instance.")
166167
RemoteClusterService(Settings settings, TransportService transportService) {
@@ -182,7 +183,7 @@ public boolean isRemoteClusterServerEnabled() {
182183
* TODO: This is not the right way to check if we're in CPS context and is more of a temporary measure since
183184
* the functionality to do it the right way is not yet ready -- replace this code when it's ready.
184185
*/
185-
this.inSkippableContext = settings.getAsBoolean("serverless.cross_project.enabled", false);
186+
this.isCpsEnabled = settings.getAsBoolean("serverless.cross_project.enabled", false);
186187
}
187188

188189
/**
@@ -293,22 +294,28 @@ void ensureConnected(String clusterAlias, ActionListener<Void> listener) {
293294
}
294295

295296
/**
296-
* Returns whether the cluster identified by the provided alias is configured to be skipped when unavailable
297+
* Returns whether the cluster identified by the provided alias is configured to be skipped when unavailable.
298+
* @param clusterAlias Name of the cluster
299+
* @return A boolean optional that denotes if the cluster is configured to be skipped. In CPS-like environment,
300+
* it returns an empty value where we default/fall back to true.
297301
*/
298-
public boolean isSkipUnavailable(String clusterAlias) {
299-
return getRemoteClusterConnection(clusterAlias).isSkipUnavailable();
302+
public Optional<Boolean> isSkipUnavailable(String clusterAlias) {
303+
if (isCpsEnabled) {
304+
return Optional.empty();
305+
} else {
306+
return Optional.of(getRemoteClusterConnection(clusterAlias).isSkipUnavailable());
307+
}
300308
}
301309

302310
/**
303-
* Returns whether we're in a skippable context. Skippable context is true when either in CPS environment
304-
* or skip_unavailable is set to true for the specified cluster.
311+
* Signifies if an error can be skipped for the specified cluster based on skip_unavailable, or,
312+
* allow_partial_search_results if in CPS-like environment.
305313
* @param clusterAlias Name of the cluster
306314
* @param allowPartialSearchResults If partial results can be served for the search request.
307315
* @return boolean
308316
*/
309317
public boolean shouldSkipOnFailure(String clusterAlias, Boolean allowPartialSearchResults) {
310-
return (inSkippableContext && (allowPartialSearchResults != null && allowPartialSearchResults))
311-
|| getRemoteClusterConnection(clusterAlias).isSkipUnavailable();
318+
return isSkipUnavailable(clusterAlias).orElseGet(() -> allowPartialSearchResults != null && allowPartialSearchResults);
312319
}
313320

314321
public Transport.Connection getConnection(String cluster) {
@@ -675,7 +682,9 @@ public RemoteClusterClient getRemoteClusterClient(
675682
return new RemoteClusterAwareClient(transportService, clusterAlias, responseExecutor, switch (disconnectedStrategy) {
676683
case RECONNECT_IF_DISCONNECTED -> true;
677684
case FAIL_IF_DISCONNECTED -> false;
678-
case RECONNECT_UNLESS_SKIP_UNAVAILABLE -> transportService.getRemoteClusterService().isSkipUnavailable(clusterAlias) == false;
685+
case RECONNECT_UNLESS_SKIP_UNAVAILABLE -> transportService.getRemoteClusterService()
686+
.isSkipUnavailable(clusterAlias)
687+
.orElse(true) == false;
679688
});
680689
}
681690

server/src/test/java/org/elasticsearch/transport/RemoteClusterServiceTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,15 +1370,15 @@ public void testSkipUnavailable() {
13701370
service.start();
13711371
service.acceptIncomingRequests();
13721372

1373-
assertTrue(service.getRemoteClusterService().isSkipUnavailable("cluster1"));
1373+
assertTrue(service.getRemoteClusterService().isSkipUnavailable("cluster1").orElse(true));
13741374

13751375
if (randomBoolean()) {
13761376
updateSkipUnavailable(service.getRemoteClusterService(), "cluster1", false);
1377-
assertFalse(service.getRemoteClusterService().isSkipUnavailable("cluster1"));
1377+
assertFalse(service.getRemoteClusterService().isSkipUnavailable("cluster1").orElse(true));
13781378
}
13791379

13801380
updateSkipUnavailable(service.getRemoteClusterService(), "cluster1", true);
1381-
assertTrue(service.getRemoteClusterService().isSkipUnavailable("cluster1"));
1381+
assertTrue(service.getRemoteClusterService().isSkipUnavailable("cluster1").orElse(true));
13821382
}
13831383
}
13841384
}

0 commit comments

Comments
 (0)