Skip to content

Commit 3aa0bbd

Browse files
committed
Add tests
1 parent fb03fea commit 3aa0bbd

File tree

5 files changed

+210
-2
lines changed

5 files changed

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

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

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

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();
@@ -203,6 +206,54 @@ public RemoteClusterStats(
203206
}
204207
}
205208

209+
public String getClusterUUID() {
210+
return clusterUUID;
211+
}
212+
213+
public String getMode() {
214+
return mode;
215+
}
216+
217+
public boolean isSkipUnavailable() {
218+
return skipUnavailable;
219+
}
220+
221+
public String getTransportCompress() {
222+
return transportCompress;
223+
}
224+
225+
public Set<String> getVersions() {
226+
return versions;
227+
}
228+
229+
public String getStatus() {
230+
return status;
231+
}
232+
233+
public long getNodesCount() {
234+
return nodesCount;
235+
}
236+
237+
public long getShardsCount() {
238+
return shardsCount;
239+
}
240+
241+
public long getIndicesCount() {
242+
return indicesCount;
243+
}
244+
245+
public long getIndicesBytes() {
246+
return indicesBytes;
247+
}
248+
249+
public long getHeapBytes() {
250+
return heapBytes;
251+
}
252+
253+
public long getMemBytes() {
254+
return memBytes;
255+
}
256+
206257
@Override
207258
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
208259
builder.startObject();

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)