|
| 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.search.ccs; |
| 10 | + |
| 11 | +import org.elasticsearch.action.ActionFuture; |
| 12 | +import org.elasticsearch.action.search.ClearScrollRequest; |
| 13 | +import org.elasticsearch.action.search.SearchRequest; |
| 14 | +import org.elasticsearch.action.search.SearchResponse; |
| 15 | +import org.elasticsearch.client.Client; |
| 16 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 17 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 18 | +import org.elasticsearch.common.settings.Settings; |
| 19 | +import org.elasticsearch.core.TimeValue; |
| 20 | +import org.elasticsearch.index.query.MatchAllQueryBuilder; |
| 21 | +import org.elasticsearch.search.aggregations.bucket.terms.Terms; |
| 22 | +import org.elasticsearch.search.builder.SearchSourceBuilder; |
| 23 | +import org.elasticsearch.test.AbstractMultiClustersTestCase; |
| 24 | +import org.elasticsearch.test.InternalTestCluster; |
| 25 | +import org.elasticsearch.transport.TransportService; |
| 26 | +import org.hamcrest.Matchers; |
| 27 | + |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.Collection; |
| 30 | +import java.util.List; |
| 31 | +import java.util.stream.Collectors; |
| 32 | +import java.util.stream.StreamSupport; |
| 33 | + |
| 34 | +import static org.elasticsearch.search.aggregations.AggregationBuilders.terms; |
| 35 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 36 | +import static org.hamcrest.Matchers.equalTo; |
| 37 | + |
| 38 | +public class CrossClusterSearchLeakIT extends AbstractMultiClustersTestCase { |
| 39 | + |
| 40 | + @Override |
| 41 | + protected Collection<String> remoteClusterAlias() { |
| 42 | + return org.elasticsearch.core.List.of("cluster_a"); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + protected boolean reuseClusters() { |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + private int indexDocs(Client client, String field, String index) { |
| 51 | + int numDocs = between(1, 200); |
| 52 | + for (int i = 0; i < numDocs; i++) { |
| 53 | + client.prepareIndex(index, "_doc").setSource(field, "v" + i).get(); |
| 54 | + } |
| 55 | + client.admin().indices().prepareRefresh(index).get(); |
| 56 | + return numDocs; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * This test validates that we do not leak any memory when running CCS in various modes, actual validation is done by test framework |
| 61 | + * (leak detection) |
| 62 | + * <ul> |
| 63 | + * <li>proxy vs non-proxy</li> |
| 64 | + * <li>single-phase query-fetch or multi-phase</li> |
| 65 | + * <li>minimize roundtrip vs not</li> |
| 66 | + * <li>scroll vs no scroll</li> |
| 67 | + * </ul> |
| 68 | + */ |
| 69 | + public void testSearch() throws Exception { |
| 70 | + assertAcked(client(LOCAL_CLUSTER).admin().indices().prepareCreate("demo") |
| 71 | + .addMapping("_doc", "f", "type=keyword") |
| 72 | + .setSettings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, between(1, 3)))); |
| 73 | + indexDocs(client(LOCAL_CLUSTER), "ignored", "demo"); |
| 74 | + final InternalTestCluster remoteCluster = cluster("cluster_a"); |
| 75 | + int minRemotes = between(2, 5); |
| 76 | + remoteCluster.ensureAtLeastNumDataNodes(minRemotes); |
| 77 | + List<String> remoteDataNodes = StreamSupport.stream(remoteCluster.clusterService().state().nodes().spliterator(), false) |
| 78 | + .filter(DiscoveryNode::canContainData) |
| 79 | + .map(DiscoveryNode::getName) |
| 80 | + .collect(Collectors.toList()); |
| 81 | + assertThat(remoteDataNodes.size(), Matchers.greaterThanOrEqualTo(minRemotes)); |
| 82 | + List<String> seedNodes = randomSubsetOf(between(1, remoteDataNodes.size() - 1), remoteDataNodes); |
| 83 | + disconnectFromRemoteClusters(); |
| 84 | + configureRemoteCluster("cluster_a", seedNodes); |
| 85 | + final Settings.Builder allocationFilter = Settings.builder(); |
| 86 | + if (rarely()) { |
| 87 | + allocationFilter.put("index.routing.allocation.include._name", String.join(",", seedNodes)); |
| 88 | + } else { |
| 89 | + // Provoke using proxy connections |
| 90 | + allocationFilter.put("index.routing.allocation.exclude._name", String.join(",", seedNodes)); |
| 91 | + } |
| 92 | + assertAcked(client("cluster_a").admin().indices().prepareCreate("prod") |
| 93 | + .addMapping("_doc", "f", "type=keyword") |
| 94 | + .setSettings(Settings.builder().put(allocationFilter.build()) |
| 95 | + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, between(1, 3)))); |
| 96 | + assertFalse(client("cluster_a").admin().cluster().prepareHealth("prod") |
| 97 | + .setWaitForYellowStatus().setTimeout(TimeValue.timeValueSeconds(10)).get().isTimedOut()); |
| 98 | + int docs = indexDocs(client("cluster_a"), "f", "prod"); |
| 99 | + |
| 100 | + List<ActionFuture<SearchResponse>> futures = new ArrayList<>(); |
| 101 | + for (int i = 0; i < 10; ++i) { |
| 102 | + String[] indices = randomBoolean() ? new String[] { "demo", "cluster_a:prod" } : new String[] { "cluster_a:prod" }; |
| 103 | + final SearchRequest searchRequest = new SearchRequest(indices); |
| 104 | + searchRequest.allowPartialSearchResults(false); |
| 105 | + boolean scroll = randomBoolean(); |
| 106 | + searchRequest.source(new SearchSourceBuilder().query(new MatchAllQueryBuilder()) |
| 107 | + .aggregation(terms("f").field("f").size(docs + between(scroll ? 1 : 0, 10))).size(between(0, 1000))); |
| 108 | + if (scroll) { |
| 109 | + searchRequest.scroll("30s"); |
| 110 | + } |
| 111 | + searchRequest.setCcsMinimizeRoundtrips(rarely()); |
| 112 | + futures.add(client(LOCAL_CLUSTER).search(searchRequest)); |
| 113 | + } |
| 114 | + |
| 115 | + for (ActionFuture<SearchResponse> future : futures) { |
| 116 | + SearchResponse searchResponse = future.get(); |
| 117 | + if (searchResponse.getScrollId() != null) { |
| 118 | + ClearScrollRequest clearScrollRequest = new ClearScrollRequest(); |
| 119 | + clearScrollRequest.scrollIds(org.elasticsearch.core.List.of(searchResponse.getScrollId())); |
| 120 | + client(LOCAL_CLUSTER).clearScroll(clearScrollRequest).get(); |
| 121 | + } |
| 122 | + |
| 123 | + Terms terms = searchResponse.getAggregations().get("f"); |
| 124 | + assertThat(terms.getBuckets().size(), equalTo(docs)); |
| 125 | + for (Terms.Bucket bucket : terms.getBuckets()) { |
| 126 | + assertThat(bucket.getDocCount(), equalTo(1L)); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + protected void configureRemoteCluster(String clusterAlias, Collection<String> seedNodes) throws Exception { |
| 133 | + if (rarely()) { |
| 134 | + super.configureRemoteCluster(clusterAlias, seedNodes); |
| 135 | + } else { |
| 136 | + final Settings.Builder settings = Settings.builder(); |
| 137 | + final String seedNode = randomFrom(seedNodes); |
| 138 | + final TransportService transportService = cluster(clusterAlias).getInstance(TransportService.class, seedNode); |
| 139 | + final String seedAddress = transportService.boundAddress().publishAddress().toString(); |
| 140 | + |
| 141 | + settings.put("cluster.remote." + clusterAlias + ".mode", "proxy"); |
| 142 | + settings.put("cluster.remote." + clusterAlias + ".proxy_address", seedAddress); |
| 143 | + client().admin().cluster().prepareUpdateSettings().setPersistentSettings(settings).get(); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments