Skip to content

Commit 32e2fc5

Browse files
CPS S2D4: do not expose skip_unavailable in API responses (#133335)
S2D4 requires that we not expose the `skip_unavailable` value in the responses to the user. This commit makes it unavailable for the `_cluster/stats` call.
1 parent a080ae6 commit 32e2fc5

File tree

4 files changed

+83
-6
lines changed

4 files changed

+83
-6
lines changed

server/src/internalClusterTest/java/org/elasticsearch/action/admin/cluster/stats/ClusterStatsRemoteIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ public void testRemoteClusterStats() throws ExecutionException, InterruptedExcep
9797
assertThat(remoteStats.get(clusterAlias).clusterUUID(), not(equalTo("")));
9898
assertThat(remoteStats.get(clusterAlias).mode(), oneOf("sniff", "proxy"));
9999
}
100-
assertFalse(remoteStats.get(REMOTE1).skipUnavailable());
101-
assertTrue(remoteStats.get(REMOTE2).skipUnavailable());
100+
assertFalse(remoteStats.get(REMOTE1).skipUnavailable().get());
101+
assertTrue(remoteStats.get(REMOTE2).skipUnavailable().get());
102102
}
103103

104104
private void setupClusters() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.action.admin.cluster.stats;
11+
12+
import org.elasticsearch.common.settings.Setting;
13+
import org.elasticsearch.common.settings.Settings;
14+
import org.elasticsearch.common.util.CollectionUtils;
15+
import org.elasticsearch.plugins.ClusterPlugin;
16+
import org.elasticsearch.plugins.Plugin;
17+
import org.elasticsearch.test.AbstractMultiClustersTestCase;
18+
import org.hamcrest.Matchers;
19+
20+
import java.util.Collection;
21+
import java.util.List;
22+
import java.util.Map;
23+
import java.util.Optional;
24+
25+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponse;
26+
27+
// TODO: Move this test to the Serverless repo once the IT framework is ready there.
28+
public class ServerlessClusterStatsShouldNotExposeSkipUnavailableIT extends AbstractMultiClustersTestCase {
29+
private static final String LINKED_CLUSTER_1 = "cluster-a";
30+
31+
public static class CpsPlugin extends Plugin implements ClusterPlugin {
32+
@Override
33+
public List<Setting<?>> getSettings() {
34+
return List.of(CpsEnableSetting);
35+
}
36+
}
37+
38+
private static final Setting<String> CpsEnableSetting = Setting.simpleString(
39+
"serverless.cross_project.enabled",
40+
Setting.Property.NodeScope
41+
);
42+
43+
@Override
44+
protected List<String> remoteClusterAlias() {
45+
return List.of(LINKED_CLUSTER_1);
46+
}
47+
48+
@Override
49+
protected Collection<Class<? extends Plugin>> nodePlugins(String clusterAlias) {
50+
return CollectionUtils.appendToCopy(super.nodePlugins(clusterAlias), CpsPlugin.class);
51+
}
52+
53+
@Override
54+
protected Settings nodeSettings() {
55+
return Settings.builder().put(super.nodeSettings()).put("serverless.cross_project.enabled", "true").build();
56+
}
57+
58+
@Override
59+
protected Map<String, Boolean> skipUnavailableForRemoteClusters() {
60+
return Map.of(LINKED_CLUSTER_1, randomBoolean());
61+
}
62+
63+
public void testSkipUnavailableShouldNotBeExposed() throws Exception {
64+
assertResponse(
65+
client().execute(TransportClusterStatsAction.TYPE, new ClusterStatsRequest(/* Do include remotes */ true)),
66+
result -> {
67+
// In the Serverless environment, skip_unavailable should map to `Optional.empty()`.
68+
assertThat(result.getRemoteClustersStats().get(LINKED_CLUSTER_1).skipUnavailable(), Matchers.is(Optional.empty()));
69+
// When this result is serialised to JSON, it should not mention `skip_unavailable`.
70+
assertThat(result.toString().contains("skip_unavailable"), Matchers.is(false));
71+
}
72+
);
73+
}
74+
}

server/src/main/java/org/elasticsearch/action/admin/cluster/stats/ClusterStatsResponse.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.List;
2727
import java.util.Locale;
2828
import java.util.Map;
29+
import java.util.Optional;
2930
import java.util.Set;
3031

3132
public class ClusterStatsResponse extends BaseNodesResponse<ClusterStatsNodeResponse> implements ToXContentFragment {
@@ -181,7 +182,7 @@ public String toString() {
181182
public record RemoteClusterStats(
182183
String clusterUUID,
183184
String mode,
184-
boolean skipUnavailable,
185+
Optional<Boolean> skipUnavailable,
185186
String transportCompress,
186187
Set<String> versions,
187188
String status,
@@ -192,7 +193,7 @@ public record RemoteClusterStats(
192193
long heapBytes,
193194
long memBytes
194195
) implements ToXContentFragment {
195-
public RemoteClusterStats(String mode, boolean skipUnavailable, String transportCompress) {
196+
public RemoteClusterStats(String mode, Optional<Boolean> skipUnavailable, String transportCompress) {
196197
this(
197198
"unavailable",
198199
mode,
@@ -231,7 +232,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
231232
builder.startObject();
232233
builder.field("cluster_uuid", clusterUUID);
233234
builder.field("mode", mode);
234-
builder.field("skip_unavailable", skipUnavailable);
235+
if (skipUnavailable.isPresent()) {
236+
builder.field("skip_unavailable", skipUnavailable.get());
237+
}
235238
builder.field("transport.compress", transportCompress);
236239
builder.field("status", status);
237240
builder.field("version", versions);

server/src/main/java/org/elasticsearch/action/admin/cluster/stats/TransportClusterStatsAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ RemoteClusterStats makeRemoteClusterStats(String clusterAlias) {
497497
var compression = RemoteClusterSettings.REMOTE_CLUSTER_COMPRESS.getConcreteSettingForNamespace(clusterAlias).get(settings);
498498
return new RemoteClusterStats(
499499
remoteConnectionInfo.getModeInfo().modeName(),
500-
remoteConnection.isSkipUnavailable(),
500+
remoteClusterService.isSkipUnavailable(clusterAlias),
501501
compression.toString()
502502
);
503503
}

0 commit comments

Comments
 (0)