|
| 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 | +} |
0 commit comments