Skip to content

Commit 506107a

Browse files
committed
Add tests
1 parent 7399f51 commit 506107a

File tree

5 files changed

+210
-2
lines changed

5 files changed

+210
-2
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
package org.elasticsearch.action.admin.cluster.stats;
10+
11+
import org.elasticsearch.Version;
12+
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
13+
import org.elasticsearch.action.search.SearchRequest;
14+
import org.elasticsearch.client.internal.Client;
15+
import org.elasticsearch.cluster.health.ClusterHealthStatus;
16+
import org.elasticsearch.common.settings.Settings;
17+
import org.elasticsearch.core.TimeValue;
18+
import org.elasticsearch.index.query.MatchAllQueryBuilder;
19+
import org.elasticsearch.search.builder.SearchSourceBuilder;
20+
import org.elasticsearch.test.AbstractMultiClustersTestCase;
21+
import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
22+
import org.elasticsearch.test.ESIntegTestCase.Scope;
23+
import org.elasticsearch.test.InternalTestCluster;
24+
import org.junit.Assert;
25+
26+
import java.util.Collection;
27+
import java.util.List;
28+
import java.util.Map;
29+
import java.util.concurrent.ExecutionException;
30+
31+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
32+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponse;
33+
import static org.hamcrest.Matchers.equalTo;
34+
import static org.hamcrest.Matchers.equalToIgnoringCase;
35+
import static org.hamcrest.Matchers.greaterThan;
36+
import static org.hamcrest.Matchers.hasItem;
37+
import static org.hamcrest.Matchers.hasKey;
38+
import static org.hamcrest.Matchers.not;
39+
import static org.hamcrest.Matchers.oneOf;
40+
41+
@ClusterScope(scope = Scope.TEST, numDataNodes = 0)
42+
public class ClusterStatsRemoteIT extends AbstractMultiClustersTestCase {
43+
private static final String REMOTE1 = "cluster-a";
44+
private static final String REMOTE2 = "cluster-b";
45+
46+
private static final String INDEX_NAME = "demo";
47+
48+
@Override
49+
protected boolean reuseClusters() {
50+
return false;
51+
}
52+
53+
@Override
54+
protected Collection<String> remoteClusterAlias() {
55+
return List.of(REMOTE1, REMOTE2);
56+
}
57+
58+
@Override
59+
protected Map<String, Boolean> skipUnavailableForRemoteClusters() {
60+
return Map.of(REMOTE1, false, REMOTE2, true);
61+
}
62+
63+
public void testRemoteClusterStats() throws ExecutionException, InterruptedException {
64+
setupClusters();
65+
final Client client = client(LOCAL_CLUSTER);
66+
SearchRequest searchRequest = new SearchRequest("*", "*:*");
67+
searchRequest.allowPartialSearchResults(false);
68+
searchRequest.setCcsMinimizeRoundtrips(randomBoolean());
69+
searchRequest.source(new SearchSourceBuilder().query(new MatchAllQueryBuilder()).size(10));
70+
71+
// do a search
72+
assertResponse(cluster(LOCAL_CLUSTER).client().search(searchRequest), Assert::assertNotNull);
73+
// collect stats without remotes
74+
ClusterStatsResponse response = client.admin().cluster().prepareClusterStats().get();
75+
assertNotNull(response.getCcsMetrics());
76+
var remotesUsage = response.getCcsMetrics().getByRemoteCluster();
77+
assertThat(remotesUsage.size(), equalTo(3));
78+
assertNull(response.getRemoteClustersStats());
79+
// collect stats with remotes
80+
response = client.admin().cluster().prepareClusterStatsWithRemotes().get();
81+
assertNotNull(response.getCcsMetrics());
82+
remotesUsage = response.getCcsMetrics().getByRemoteCluster();
83+
assertThat(remotesUsage.size(), equalTo(3));
84+
assertNotNull(response.getRemoteClustersStats());
85+
var remoteStats = response.getRemoteClustersStats();
86+
assertThat(remoteStats.size(), equalTo(2));
87+
for (String clusterAlias : remoteClusterAlias()) {
88+
assertThat(remoteStats, hasKey(clusterAlias));
89+
assertThat(remotesUsage, hasKey(clusterAlias));
90+
assertThat(remoteStats.get(clusterAlias).getStatus(), equalToIgnoringCase(ClusterHealthStatus.GREEN.name()));
91+
assertThat(remoteStats.get(clusterAlias).getIndicesCount(), greaterThan(0L));
92+
assertThat(remoteStats.get(clusterAlias).getNodesCount(), greaterThan(0L));
93+
assertThat(remoteStats.get(clusterAlias).getShardsCount(), greaterThan(0L));
94+
assertThat(remoteStats.get(clusterAlias).getHeapBytes(), greaterThan(0L));
95+
assertThat(remoteStats.get(clusterAlias).getMemBytes(), greaterThan(0L));
96+
assertThat(remoteStats.get(clusterAlias).getIndicesBytes(), greaterThan(0L));
97+
assertThat(remoteStats.get(clusterAlias).getVersions(), hasItem(Version.CURRENT.toString()));
98+
assertThat(remoteStats.get(clusterAlias).getClusterUUID(), not(equalTo("")));
99+
assertThat(remoteStats.get(clusterAlias).getMode(), oneOf("sniff", "proxy"));
100+
}
101+
assertFalse(remoteStats.get(REMOTE1).isSkipUnavailable());
102+
assertTrue(remoteStats.get(REMOTE2).isSkipUnavailable());
103+
}
104+
105+
private void setupClusters() {
106+
int numShardsLocal = randomIntBetween(2, 10);
107+
Settings localSettings = indexSettings(numShardsLocal, randomIntBetween(0, 1)).build();
108+
assertAcked(
109+
client(LOCAL_CLUSTER).admin()
110+
.indices()
111+
.prepareCreate(INDEX_NAME)
112+
.setSettings(localSettings)
113+
.setMapping("@timestamp", "type=date", "f", "type=text")
114+
);
115+
indexDocs(client(LOCAL_CLUSTER));
116+
117+
int numShardsRemote = randomIntBetween(2, 10);
118+
for (String clusterAlias : remoteClusterAlias()) {
119+
final InternalTestCluster remoteCluster = cluster(clusterAlias);
120+
remoteCluster.ensureAtLeastNumDataNodes(randomIntBetween(1, 3));
121+
assertAcked(
122+
client(clusterAlias).admin()
123+
.indices()
124+
.prepareCreate(INDEX_NAME)
125+
.setSettings(indexSettings(numShardsRemote, randomIntBetween(0, 1)))
126+
.setMapping("@timestamp", "type=date", "f", "type=text")
127+
);
128+
assertFalse(
129+
client(clusterAlias).admin()
130+
.cluster()
131+
.prepareHealth(INDEX_NAME)
132+
.setWaitForGreenStatus()
133+
.setTimeout(TimeValue.timeValueSeconds(10))
134+
.get()
135+
.isTimedOut()
136+
);
137+
indexDocs(client(clusterAlias));
138+
}
139+
140+
}
141+
142+
private void indexDocs(Client client) {
143+
int numDocs = between(5, 20);
144+
for (int i = 0; i < numDocs; i++) {
145+
client.prepareIndex(INDEX_NAME).setSource("f", "v", "@timestamp", randomNonNegativeLong()).get();
146+
}
147+
client.admin().indices().prepareRefresh(INDEX_NAME).get();
148+
}
149+
150+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ public class ClusterStatsRequestBuilder extends NodesOperationRequestBuilder<
1919
public ClusterStatsRequestBuilder(ElasticsearchClient client) {
2020
super(client, TransportClusterStatsAction.TYPE, new ClusterStatsRequest());
2121
}
22+
public ClusterStatsRequestBuilder(ElasticsearchClient client, boolean doRemotes) {
23+
super(client, TransportClusterStatsAction.TYPE, new ClusterStatsRequest(doRemotes));
24+
}
2225
}

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

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ public class ClusterStatsResponse extends BaseNodesResponse<ClusterStatsNodeResp
3535
final ClusterHealthStatus status;
3636
final ClusterSnapshotStats clusterSnapshotStats;
3737
final RepositoryUsageStats repositoryUsageStats;
38-
3938
final CCSTelemetrySnapshot ccsMetrics;
4039
final long timestamp;
4140
final String clusterUUID;
@@ -105,6 +104,10 @@ public CCSTelemetrySnapshot getCcsMetrics() {
105104
return ccsMetrics;
106105
}
107106

107+
public Map<String, RemoteClusterStats> getRemoteClustersStats() {
108+
return remoteClustersStats;
109+
}
110+
108111
@Override
109112
public void writeTo(StreamOutput out) throws IOException {
110113
TransportAction.localOnly();
@@ -202,6 +205,54 @@ public RemoteClusterStats(
202205
this.memBytes = 0;
203206
}
204207
}
208+
public String getClusterUUID() {
209+
return clusterUUID;
210+
}
211+
212+
public String getMode() {
213+
return mode;
214+
}
215+
216+
public boolean isSkipUnavailable() {
217+
return skipUnavailable;
218+
}
219+
220+
public String getTransportCompress() {
221+
return transportCompress;
222+
}
223+
224+
public Set<String> getVersions() {
225+
return versions;
226+
}
227+
228+
public String getStatus() {
229+
return status;
230+
}
231+
232+
public long getNodesCount() {
233+
return nodesCount;
234+
}
235+
236+
public long getShardsCount() {
237+
return shardsCount;
238+
}
239+
240+
public long getIndicesCount() {
241+
return indicesCount;
242+
}
243+
244+
public long getIndicesBytes() {
245+
return indicesBytes;
246+
}
247+
248+
public long getHeapBytes() {
249+
return heapBytes;
250+
}
251+
252+
public long getMemBytes() {
253+
return memBytes;
254+
}
255+
205256

206257
@Override
207258
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {

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
@@ -217,7 +217,7 @@ private ActionFuture<Map<String, RemoteClusterStatsResponse>> getStatsFromRemote
217217
}
218218

219219
// TODO: make correct pool
220-
final var remoteClientResponseExecutor = transportService.getThreadPool().executor(ThreadPool.Names.MANAGEMENT);
220+
final var remoteClientResponseExecutor = transportService.getThreadPool().executor(ThreadPool.Names.SEARCH_COORDINATION);
221221
var remotes = remoteClusterService.getRegisteredRemoteClusterNames();
222222

223223
var remotesFuture = new PlainActionFuture<Map<String, RemoteClusterStatsResponse>>();

server/src/main/java/org/elasticsearch/client/internal/ClusterAdminClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ public ClusterStatsRequestBuilder prepareClusterStats() {
206206
return new ClusterStatsRequestBuilder(this);
207207
}
208208

209+
public ClusterStatsRequestBuilder prepareClusterStatsWithRemotes() {
210+
return new ClusterStatsRequestBuilder(this, true);
211+
}
212+
209213
public ActionFuture<NodesStatsResponse> nodesStats(final NodesStatsRequest request) {
210214
return execute(TransportNodesStatsAction.TYPE, request);
211215
}

0 commit comments

Comments
 (0)