diff options
author | Jacek <jacek.poreda@sonarsource.com> | 2020-01-09 09:26:53 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2020-11-05 20:06:21 +0000 |
commit | f4751bd13509f8d325d17cb4cf4ed9d85025f65f (patch) | |
tree | 369137df20a5df287bf77cdb7dcf49888f282f31 /server/sonar-webserver-core | |
parent | 8cdee7d30f96e87b8bb7ec55fdfd8101ab717dfd (diff) | |
download | sonarqube-f4751bd13509f8d325d17cb4cf4ed9d85025f65f.tar.gz sonarqube-f4751bd13509f8d325d17cb4cf4ed9d85025f65f.zip |
SONAR-12686 upgrade es client to 7.9.3 and move to HTTP
- add should minimum match eq 1 to user index queries
ES 7.X changed behaviour in case filter query with bool it defaults to '0'
https://www.elastic.co/guide/en/elasticsearch/reference/7.x/breaking-changes-7.0.html#_the_filter_context_has_been_removed
- fix issue index routing param
ES 7.X helped discover this bug as new setting has been auto configured which is 'index.number_of_routing_shards'.
This has changed how documents are distributed across shards depending on how many shards the index has.
Without that change issues docs has been incorrectly routed to the same shard hash as projects and it worked no matter what routing key you used projectUuid or auth_projectUuid.
- update ngram and edge_ngram names to match with es 7.x
nGram and edgeNgram has been deprecated in favour of ngram and edge_ngram
https://www.elastic.co/guide/en/elasticsearch/reference/current/breaking-changes-7.0.html#deprecated-ngram-edgengram-token-filter-cannot-be-used
- remove `_all : enabled` usage from UT
This field was already deprecated in 6.X, now it has been removed.
https://www.elastic.co/guide/en/elasticsearch/reference/current/breaking-changes-7.0.html#all-meta-field-removed
- add Elasticsearch High Level REST client dependency
- use sonar.search.port for ES HTTP
- main process use ES Rest client to check ES status
- sonar.cluster.search.hosts has HTTP ports on APP nodes
also sonar.search.port and sonar.search.host MUST be configured on each Search node with the host and HTTP port of the current node
- use Elasticsearch high level rest client
- use in EsTester
- use as primary es client
- use indices api to get all indices name instead of cluster api
- use cluster health api to check cluster state
- support raw requests for 'nodes/_stats' and '_cluster/stats'
- support raw requests for 'indices/_stats'
- leave netty4plugin as testCompile dependency it is used in UTs
- all ES non-test calls go through EsClient class
- add rest client ES profiling
Diffstat (limited to 'server/sonar-webserver-core')
9 files changed, 95 insertions, 93 deletions
diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/es/IndexerStartupTask.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/es/IndexerStartupTask.java index 8896b4f6869..955798333e9 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/es/IndexerStartupTask.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/es/IndexerStartupTask.java @@ -21,9 +21,7 @@ package org.sonar.server.es; import java.util.Set; import java.util.stream.Collectors; -import org.elasticsearch.action.admin.cluster.health.ClusterHealthAction; -import org.elasticsearch.client.Client; -import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest; import org.sonar.api.config.Configuration; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; @@ -108,8 +106,9 @@ public class IndexerStartupTask { } private void waitForIndexYellow(String index) { - Client nativeClient = esClient.nativeClient(); - ClusterHealthAction.INSTANCE.newRequestBuilder(nativeClient).setIndices(index).setWaitForYellowStatus().get(TimeValue.timeValueMinutes(10)); + esClient.clusterHealth(new ClusterHealthRequest() + .indices(index) + .waitForYellowStatus()); } private static String getSynchronousIndexingLogMessage(Set<IndexType> emptyTypes) { diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/es/MigrationEsClientImpl.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/es/MigrationEsClientImpl.java index 7d093a2f7e1..9cbafb12d11 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/es/MigrationEsClientImpl.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/es/MigrationEsClientImpl.java @@ -26,7 +26,9 @@ import java.util.Map; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; -import org.elasticsearch.action.admin.indices.stats.IndexStats; +import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; +import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; +import org.elasticsearch.client.indices.GetIndexRequest; import org.sonar.api.utils.log.Loggers; import org.sonar.core.util.stream.MoreCollectors; import org.sonar.server.platform.db.migration.es.MigrationEsClient; @@ -41,8 +43,8 @@ public class MigrationEsClientImpl implements MigrationEsClient { @Override public void deleteIndexes(String name, String... otherNames) { - Map<String, IndexStats> indices = client.nativeClient().admin().indices().prepareStats().get().getIndices(); - Set<String> existingIndices = indices.values().stream().map(IndexStats::getIndex).collect(MoreCollectors.toSet()); + String[] indices = client.getIndex(new GetIndexRequest("_all")).getIndices(); + Set<String> existingIndices = Arrays.stream(indices).collect(MoreCollectors.toSet()); Stream.concat(Stream.of(name), Arrays.stream(otherNames)) .distinct() .filter(existingIndices::contains) @@ -51,17 +53,18 @@ public class MigrationEsClientImpl implements MigrationEsClient { @Override public void addMappingToExistingIndex(String index, String type, String mappingName, String mappingType, Map<String, String> options) { - IndexStats stats = client.nativeClient().admin().indices().prepareStats().get().getIndex(index); - if (stats != null) { + String[] indices = client.getIndex(new GetIndexRequest(index)).getIndices(); + if (indices != null && indices.length == 1) { Loggers.get(getClass()).info("Add mapping [{}] to Elasticsearch index [{}]", mappingName, index); String mappingOptions = Stream.concat(Stream.of(Maps.immutableEntry("type", mappingType)), options.entrySet().stream()) .map(e -> e.getKey() + "=" + e.getValue()) .collect(Collectors.joining(",")); - client.nativeClient().admin().indices().preparePutMapping(index) - .setType(type) - .setSource(mappingName, mappingOptions) - .get(); + client.putMapping(new PutMappingRequest(index) + .type(type) + .source(mappingName, mappingOptions)); updatedIndices.add(index); + } else { + throw new IllegalStateException("Expected only one index to be found, actual [" + String.join(",", indices) + "]"); } } @@ -72,6 +75,6 @@ public class MigrationEsClientImpl implements MigrationEsClient { private void deleteIndex(String index) { Loggers.get(getClass()).info("Drop Elasticsearch index [{}]", index); - client.nativeClient().admin().indices().prepareDelete(index).get(); + client.deleteIndex(new DeleteIndexRequest(index)); } } diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/EsIndexesSection.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/EsIndexesSection.java index 7d0314480d5..f30781dae33 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/EsIndexesSection.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/EsIndexesSection.java @@ -19,16 +19,15 @@ */ package org.sonar.server.platform.monitoring; -import java.util.Map; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.action.admin.indices.stats.IndexStats; -import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse; import org.sonar.api.server.ServerSide; import org.sonar.api.utils.log.Loggers; import org.sonar.process.systeminfo.Global; import org.sonar.process.systeminfo.SystemInfoSection; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; import org.sonar.server.es.EsClient; +import org.sonar.server.es.response.IndexStats; +import org.sonar.server.es.response.IndicesStatsResponse; import static org.apache.commons.io.FileUtils.byteCountToDisplaySize; import static org.sonar.process.systeminfo.SystemInfoUtils.setAttribute; @@ -56,12 +55,12 @@ public class EsIndexesSection implements SystemInfoSection, Global { } private void completeIndexAttributes(ProtobufSystemInfo.Section.Builder protobuf) { - IndicesStatsResponse indicesStats = esClient.prepareStats().all().get(); - for (Map.Entry<String, IndexStats> indexStats : indicesStats.getIndices().entrySet()) { - String prefix = "Index " + indexStats.getKey() + " - "; - setAttribute(protobuf, prefix + "Docs", indexStats.getValue().getPrimaries().getDocs().getCount()); - setAttribute(protobuf, prefix + "Shards", indexStats.getValue().getShards().length); - setAttribute(protobuf, prefix + "Store Size", byteCountToDisplaySize(indexStats.getValue().getPrimaries().getStore().getSizeInBytes())); + IndicesStatsResponse indicesStats = esClient.indicesStats(); + for (IndexStats indexStats : indicesStats.getAllIndexStats()) { + String prefix = "Index " + indexStats.getName() + " - "; + setAttribute(protobuf, prefix + "Docs", indexStats.getDocCount()); + setAttribute(protobuf, prefix + "Shards", indexStats.getShardsCount()); + setAttribute(protobuf, prefix + "Store Size", byteCountToDisplaySize(indexStats.getStoreSizeBytes())); } } } diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/EsStateSection.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/EsStateSection.java index ebfa033e44d..aee69ff7b20 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/EsStateSection.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/EsStateSection.java @@ -21,15 +21,14 @@ package org.sonar.server.platform.monitoring; import java.util.Locale; import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.action.admin.cluster.node.stats.NodeStats; -import org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse; -import org.elasticsearch.action.admin.cluster.stats.ClusterStatsResponse; +import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest; import org.elasticsearch.cluster.health.ClusterHealthStatus; -import org.elasticsearch.common.breaker.CircuitBreaker; import org.sonar.api.utils.log.Loggers; import org.sonar.process.systeminfo.SystemInfoSection; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; import org.sonar.server.es.EsClient; +import org.sonar.server.es.response.NodeStats; +import org.sonar.server.es.response.NodeStatsResponse; import static java.lang.String.format; import static org.apache.commons.io.FileUtils.byteCountToDisplaySize; @@ -44,7 +43,7 @@ public class EsStateSection implements SystemInfoSection { } private ClusterHealthStatus getStateAsEnum() { - return clusterStats().getStatus(); + return esClient.clusterHealth(new ClusterHealthRequest()).getStatus(); } @Override @@ -54,7 +53,7 @@ public class EsStateSection implements SystemInfoSection { try { setAttribute(protobuf, "State", getStateAsEnum().name()); completeNodeAttributes(protobuf); - } catch (Exception es) { + } catch (Exception es) { Loggers.get(EsStateSection.class).warn("Failed to retrieve ES attributes. There will be only a single \"state\" attribute.", es); setAttribute(protobuf, "State", es.getCause() instanceof ElasticsearchException ? es.getCause().getMessage() : es.getMessage()); } @@ -62,42 +61,37 @@ public class EsStateSection implements SystemInfoSection { } private void completeNodeAttributes(ProtobufSystemInfo.Section.Builder protobuf) { - NodesStatsResponse nodesStats = esClient.prepareNodesStats() - .setFs(true) - .setProcess(true) - .setJvm(true) - .setIndices(true) - .setBreaker(true) - .get(); - if (!nodesStats.getNodes().isEmpty()) { - NodeStats stats = nodesStats.getNodes().get(0); - toProtobuf(stats, protobuf); + NodeStatsResponse nodesStatsResponse = esClient.nodesStats(); + + if (!nodesStatsResponse.getNodeStats().isEmpty()) { + toProtobuf(nodesStatsResponse.getNodeStats().get(0), protobuf); } } public static void toProtobuf(NodeStats stats, ProtobufSystemInfo.Section.Builder protobuf) { - setAttribute(protobuf, "CPU Usage (%)", stats.getProcess().getCpu().getPercent()); - setAttribute(protobuf, "Disk Available", byteCountToDisplaySize(stats.getFs().getTotal().getAvailable().getBytes())); - setAttribute(protobuf, "Store Size", byteCountToDisplaySize(stats.getIndices().getStore().getSizeInBytes())); - setAttribute(protobuf, "Translog Size", byteCountToDisplaySize(stats.getIndices().getTranslog().getTranslogSizeInBytes())); - setAttribute(protobuf, "Open File Descriptors", stats.getProcess().getOpenFileDescriptors()); - setAttribute(protobuf, "Max File Descriptors", stats.getProcess().getMaxFileDescriptors()); - setAttribute(protobuf, "JVM Heap Usage", formatPercent(stats.getJvm().getMem().getHeapUsedPercent())); - setAttribute(protobuf, "JVM Heap Used", byteCountToDisplaySize(stats.getJvm().getMem().getHeapUsed().getBytes())); - setAttribute(protobuf, "JVM Heap Max", byteCountToDisplaySize(stats.getJvm().getMem().getHeapMax().getBytes())); - setAttribute(protobuf, "JVM Non Heap Used", byteCountToDisplaySize(stats.getJvm().getMem().getNonHeapUsed().getBytes())); - setAttribute(protobuf, "JVM Threads", stats.getJvm().getThreads().getCount()); - setAttribute(protobuf, "Field Data Memory", byteCountToDisplaySize(stats.getIndices().getFieldData().getMemorySizeInBytes())); - setAttribute(protobuf, "Field Data Circuit Breaker Limit", byteCountToDisplaySize(stats.getBreaker().getStats(CircuitBreaker.FIELDDATA).getLimit())); - setAttribute(protobuf, "Field Data Circuit Breaker Estimation", byteCountToDisplaySize(stats.getBreaker().getStats(CircuitBreaker.FIELDDATA).getEstimated())); - setAttribute(protobuf, "Request Circuit Breaker Limit", byteCountToDisplaySize(stats.getBreaker().getStats(CircuitBreaker.REQUEST).getLimit())); - setAttribute(protobuf, "Request Circuit Breaker Estimation", byteCountToDisplaySize(stats.getBreaker().getStats(CircuitBreaker.REQUEST).getEstimated())); - setAttribute(protobuf, "Query Cache Memory", byteCountToDisplaySize(stats.getIndices().getQueryCache().getMemorySizeInBytes())); - setAttribute(protobuf, "Request Cache Memory", byteCountToDisplaySize(stats.getIndices().getRequestCache().getMemorySizeInBytes())); - } - - private ClusterStatsResponse clusterStats() { - return esClient.prepareClusterStats().get(); + setAttribute(protobuf, "CPU Usage (%)", stats.getCpuUsage()); + setAttribute(protobuf, "Disk Available", byteCountToDisplaySize(stats.getDiskAvailableBytes())); + setAttribute(protobuf, "Store Size", byteCountToDisplaySize(stats.getIndicesStats().getStoreSizeInBytes())); + setAttribute(protobuf, "Translog Size", byteCountToDisplaySize(stats.getIndicesStats().getTranslogSizeInBytes())); + setAttribute(protobuf, "Open File Descriptors", stats.getOpenFileDescriptors()); + setAttribute(protobuf, "Max File Descriptors", stats.getMaxFileDescriptors()); + setAttribute(protobuf, "JVM Heap Usage", formatPercent(stats.getJvmStats().getHeapUsedPercent())); + setAttribute(protobuf, "JVM Heap Used", byteCountToDisplaySize(stats.getJvmStats().getHeapUsedInBytes())); + setAttribute(protobuf, "JVM Heap Max", byteCountToDisplaySize(stats.getJvmStats().getHeapMaxInBytes())); + setAttribute(protobuf, "JVM Non Heap Used", byteCountToDisplaySize(stats.getJvmStats().getNonHeapUsedInBytes())); + setAttribute(protobuf, "JVM Threads", stats.getJvmStats().getThreadCount()); + setAttribute(protobuf, "Field Data Memory", byteCountToDisplaySize(stats.getIndicesStats().getFieldDataMemorySizeInBytes())); + setAttribute(protobuf, "Field Data Circuit Breaker Limit", + byteCountToDisplaySize(stats.getFieldDataCircuitBreakerLimit())); + setAttribute(protobuf, "Field Data Circuit Breaker Estimation", + byteCountToDisplaySize(stats.getFieldDataCircuitBreakerEstimation())); + setAttribute(protobuf, "Request Circuit Breaker Limit", + byteCountToDisplaySize(stats.getRequestCircuitBreakerLimit())); + setAttribute(protobuf, "Request Circuit Breaker Estimation", + byteCountToDisplaySize(stats.getRequestCircuitBreakerEstimation())); + setAttribute(protobuf, "Query Cache Memory", byteCountToDisplaySize(stats.getIndicesStats().getQueryCacheMemorySizeInBytes())); + setAttribute(protobuf, "Request Cache Memory", + byteCountToDisplaySize(stats.getIndicesStats().getRequestCacheMemorySizeInBytes())); } private static String formatPercent(long amount) { diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/cluster/EsClusterStateSection.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/cluster/EsClusterStateSection.java index 471893b03ee..e44efc3978e 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/cluster/EsClusterStateSection.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/cluster/EsClusterStateSection.java @@ -19,12 +19,12 @@ */ package org.sonar.server.platform.monitoring.cluster; -import org.elasticsearch.action.admin.cluster.stats.ClusterStatsResponse; import org.sonar.api.server.ServerSide; import org.sonar.process.systeminfo.Global; import org.sonar.process.systeminfo.SystemInfoSection; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; import org.sonar.server.es.EsClient; +import org.sonar.server.es.response.ClusterStatsResponse; import static org.sonar.process.systeminfo.SystemInfoUtils.setAttribute; @@ -45,9 +45,9 @@ public class EsClusterStateSection implements SystemInfoSection, Global { public ProtobufSystemInfo.Section toProtobuf() { ProtobufSystemInfo.Section.Builder protobuf = ProtobufSystemInfo.Section.newBuilder(); protobuf.setName("Search State"); - ClusterStatsResponse stats = esClient.prepareClusterStats().get(); - setAttribute(protobuf, "State", stats.getStatus().name()); - setAttribute(protobuf, "Nodes", stats.getNodesStats().getCounts().getTotal()); + ClusterStatsResponse stats = esClient.clusterStats(); + setAttribute(protobuf, "State", stats.getHealthStatus().name()); + setAttribute(protobuf, "Nodes", stats.getNodeCount()); return protobuf.build(); } diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/cluster/SearchNodesInfoLoaderImpl.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/cluster/SearchNodesInfoLoaderImpl.java index 96cf66fc03a..ce0a0c062fd 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/cluster/SearchNodesInfoLoaderImpl.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/platform/monitoring/cluster/SearchNodesInfoLoaderImpl.java @@ -22,11 +22,11 @@ package org.sonar.server.platform.monitoring.cluster; import java.util.ArrayList; import java.util.Collection; import java.util.List; -import org.elasticsearch.action.admin.cluster.node.stats.NodeStats; -import org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse; import org.sonar.api.server.ServerSide; import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo; import org.sonar.server.es.EsClient; +import org.sonar.server.es.response.NodeStats; +import org.sonar.server.es.response.NodeStatsResponse; import org.sonar.server.platform.monitoring.EsStateSection; @ServerSide @@ -39,28 +39,22 @@ public class SearchNodesInfoLoaderImpl implements SearchNodesInfoLoader { } public Collection<NodeInfo> load() { - NodesStatsResponse nodesStats = esClient.prepareNodesStats() - .setFs(true) - .setProcess(true) - .setJvm(true) - .setIndices(true) - .setBreaker(true) - .get(); + NodeStatsResponse response = esClient.nodesStats(); + List<NodeInfo> result = new ArrayList<>(); - nodesStats.getNodes().forEach(nodeStat -> result.add(toNodeInfo(nodeStat))); + response.getNodeStats().forEach(nodeStat -> result.add(toNodeInfo(nodeStat))); return result; } private static NodeInfo toNodeInfo(NodeStats stat) { - String nodeName = stat.getNode().getName(); + String nodeName = stat.getName(); NodeInfo info = new NodeInfo(nodeName); - info.setHost(stat.getHostname()); + info.setHost(stat.getHost()); ProtobufSystemInfo.Section.Builder section = ProtobufSystemInfo.Section.newBuilder(); section.setName("Search State"); EsStateSection.toProtobuf(stat, section); info.addSection(section.build()); - return info; } diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/es/MigrationEsClientImplTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/es/MigrationEsClientImplTest.java index 1746868bb25..f859000c2d2 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/es/MigrationEsClientImplTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/es/MigrationEsClientImplTest.java @@ -23,7 +23,8 @@ import com.google.common.collect.ImmutableMap; import java.util.Iterator; import java.util.Map; import javax.annotation.CheckForNull; -import org.elasticsearch.cluster.metadata.MappingMetaData; +import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest; +import org.elasticsearch.cluster.metadata.MappingMetadata; import org.elasticsearch.common.collect.ImmutableOpenMap; import org.junit.Rule; import org.junit.Test; @@ -33,6 +34,7 @@ import org.sonar.api.utils.log.LoggerLevel; import org.sonar.server.platform.db.migration.es.MigrationEsClient; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.sonar.server.es.newindex.SettingsConfiguration.newBuilder; public class MigrationEsClientImplTest { @@ -77,8 +79,8 @@ public class MigrationEsClientImplTest { underTest.addMappingToExistingIndex("as", "s", "new_field", "keyword", mappingOptions); assertThat(loadExistingIndices()).toIterable().contains("as"); - ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings = mappings(); - MappingMetaData mapping = mappings.get("as").get("s"); + ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings = mappings(); + MappingMetadata mapping = mappings.get("as").get("s"); assertThat(countMappingFields(mapping)).isEqualTo(1); assertThat(field(mapping, "new_field")).isNotNull(); @@ -87,22 +89,31 @@ public class MigrationEsClientImplTest { assertThat(underTest.getUpdatedIndices()).containsExactly("as"); } + @Test + public void shouldFailIfMoreThanOneIndexReturned() { + String indexPattern = "*s"; + Map<String, String> mappingOptions = ImmutableMap.of("norms", "false"); + assertThatThrownBy(() -> underTest.addMappingToExistingIndex(indexPattern, "s", "new_field", "keyword", mappingOptions)) + .isInstanceOf(IllegalStateException.class) + .hasMessageContaining("Expected only one index to be found, actual"); + } + private Iterator<String> loadExistingIndices() { - return es.client().nativeClient().admin().indices().prepareGetMappings().get().mappings().keysIt(); + return es.client().getMapping(new GetMappingsRequest()).mappings().keysIt(); } - private ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings() { - return es.client().nativeClient().admin().indices().prepareGetMappings().get().mappings(); + private ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings() { + return es.client().getMapping(new GetMappingsRequest()).mappings(); } @CheckForNull @SuppressWarnings("unchecked") - private Map<String, Object> field(MappingMetaData mapping, String field) { + private Map<String, Object> field(MappingMetadata mapping, String field) { Map<String, Object> props = (Map<String, Object>) mapping.getSourceAsMap().get("properties"); return (Map<String, Object>) props.get(field); } - private int countMappingFields(MappingMetaData mapping) { + private int countMappingFields(MappingMetadata mapping) { return ((Map) mapping.getSourceAsMap().get("properties")).size(); } diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/EsIndexesSectionTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/EsIndexesSectionTest.java index e340ecf56c4..43559c7dbfe 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/EsIndexesSectionTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/EsIndexesSectionTest.java @@ -27,6 +27,7 @@ import org.sonar.server.es.EsClient; import org.sonar.server.es.EsTester; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.sonar.process.systeminfo.SystemInfoUtils.attribute; @@ -58,7 +59,7 @@ public class EsIndexesSectionTest { public void attributes_displays_exception_message_when_cause_null_when_client_fails() { EsClient esClientMock = mock(EsClient.class); EsIndexesSection underTest = new EsIndexesSection(esClientMock); - when(esClientMock.prepareStats()).thenThrow(new RuntimeException("RuntimeException with no cause")); + when(esClientMock.indicesStats()).thenThrow(new RuntimeException("RuntimeException with no cause")); ProtobufSystemInfo.Section section = underTest.toProtobuf(); assertThatAttributeIs(section, "Error", "RuntimeException with no cause"); @@ -68,7 +69,7 @@ public class EsIndexesSectionTest { public void attributes_displays_exception_message_when_cause_is_not_ElasticSearchException_when_client_fails() { EsClient esClientMock = mock(EsClient.class); EsIndexesSection underTest = new EsIndexesSection(esClientMock); - when(esClientMock.prepareStats()).thenThrow(new RuntimeException("RuntimeException with cause not ES", new IllegalArgumentException("some cause message"))); + when(esClientMock.indicesStats()).thenThrow(new RuntimeException("RuntimeException with cause not ES", new IllegalArgumentException("some cause message"))); ProtobufSystemInfo.Section section = underTest.toProtobuf(); assertThatAttributeIs(section, "Error", "RuntimeException with cause not ES"); @@ -78,7 +79,7 @@ public class EsIndexesSectionTest { public void attributes_displays_cause_message_when_cause_is_ElasticSearchException_when_client_fails() { EsClient esClientMock = mock(EsClient.class); EsIndexesSection underTest = new EsIndexesSection(esClientMock); - when(esClientMock.prepareStats()).thenThrow(new RuntimeException("RuntimeException with ES cause", new ElasticsearchException("some cause message"))); + when(esClientMock.indicesStats()).thenThrow(new RuntimeException("RuntimeException with ES cause", new ElasticsearchException("some cause message"))); ProtobufSystemInfo.Section section = underTest.toProtobuf(); assertThatAttributeIs(section, "Error", "some cause message"); diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/EsStateSectionTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/EsStateSectionTest.java index f0445e2f831..d7da13bf907 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/EsStateSectionTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/platform/monitoring/EsStateSectionTest.java @@ -28,6 +28,7 @@ import org.sonar.server.es.EsClient; import org.sonar.server.es.EsTester; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.sonar.process.systeminfo.SystemInfoUtils.attribute; @@ -63,7 +64,7 @@ public class EsStateSectionTest { public void attributes_displays_exception_message_when_cause_null_when_client_fails() { EsClient esClientMock = mock(EsClient.class); EsStateSection underTest = new EsStateSection(esClientMock); - when(esClientMock.prepareClusterStats()).thenThrow(new RuntimeException("RuntimeException with no cause")); + when(esClientMock.clusterHealth(any())).thenThrow(new RuntimeException("RuntimeException with no cause")); ProtobufSystemInfo.Section section = underTest.toProtobuf(); assertThatAttributeIs(section, "State", "RuntimeException with no cause"); @@ -73,7 +74,7 @@ public class EsStateSectionTest { public void attributes_displays_exception_message_when_cause_is_not_ElasticSearchException_when_client_fails() { EsClient esClientMock = mock(EsClient.class); EsStateSection underTest = new EsStateSection(esClientMock); - when(esClientMock.prepareClusterStats()).thenThrow(new RuntimeException("RuntimeException with cause not ES", new IllegalArgumentException("some cause message"))); + when(esClientMock.clusterHealth(any())).thenThrow(new RuntimeException("RuntimeException with cause not ES", new IllegalArgumentException("some cause message"))); ProtobufSystemInfo.Section section = underTest.toProtobuf(); assertThatAttributeIs(section, "State", "RuntimeException with cause not ES"); @@ -83,7 +84,7 @@ public class EsStateSectionTest { public void attributes_displays_cause_message_when_cause_is_ElasticSearchException_when_client_fails() { EsClient esClientMock = mock(EsClient.class); EsStateSection underTest = new EsStateSection(esClientMock); - when(esClientMock.prepareClusterStats()).thenThrow(new RuntimeException("RuntimeException with ES cause", new ElasticsearchException("some cause message"))); + when(esClientMock.clusterHealth(any())).thenThrow(new RuntimeException("RuntimeException with ES cause", new ElasticsearchException("some cause message"))); ProtobufSystemInfo.Section section = underTest.toProtobuf(); assertThatAttributeIs(section, "State", "some cause message"); |