|
| 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.apache.lucene.document.LongPoint; |
| 12 | +import org.apache.lucene.index.DirectoryReader; |
| 13 | +import org.apache.lucene.index.PointValues; |
| 14 | +import org.elasticsearch.action.search.CanMatchNodeRequest; |
| 15 | +import org.elasticsearch.action.search.SearchRequest; |
| 16 | +import org.elasticsearch.action.search.SearchResponse; |
| 17 | +import org.elasticsearch.action.search.SearchTransportService; |
| 18 | +import org.elasticsearch.client.internal.Client; |
| 19 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 20 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 21 | +import org.elasticsearch.cluster.node.DiscoveryNodes; |
| 22 | +import org.elasticsearch.common.Strings; |
| 23 | +import org.elasticsearch.common.settings.Settings; |
| 24 | +import org.elasticsearch.common.util.CollectionUtils; |
| 25 | +import org.elasticsearch.index.IndexSettings; |
| 26 | +import org.elasticsearch.index.engine.EngineConfig; |
| 27 | +import org.elasticsearch.index.engine.EngineFactory; |
| 28 | +import org.elasticsearch.index.engine.InternalEngine; |
| 29 | +import org.elasticsearch.index.engine.InternalEngineFactory; |
| 30 | +import org.elasticsearch.index.query.RangeQueryBuilder; |
| 31 | +import org.elasticsearch.index.shard.IndexLongFieldRange; |
| 32 | +import org.elasticsearch.index.shard.ShardLongFieldRange; |
| 33 | +import org.elasticsearch.plugins.EnginePlugin; |
| 34 | +import org.elasticsearch.plugins.Plugin; |
| 35 | +import org.elasticsearch.search.builder.SearchSourceBuilder; |
| 36 | +import org.elasticsearch.test.AbstractMultiClustersTestCase; |
| 37 | +import org.elasticsearch.test.hamcrest.ElasticsearchAssertions; |
| 38 | +import org.elasticsearch.test.transport.MockTransportService; |
| 39 | +import org.elasticsearch.transport.TransportService; |
| 40 | +import org.hamcrest.Matchers; |
| 41 | + |
| 42 | +import java.io.IOException; |
| 43 | +import java.io.UncheckedIOException; |
| 44 | +import java.util.Arrays; |
| 45 | +import java.util.Collection; |
| 46 | +import java.util.List; |
| 47 | +import java.util.Optional; |
| 48 | + |
| 49 | +import static org.hamcrest.Matchers.equalTo; |
| 50 | +import static org.hamcrest.Matchers.in; |
| 51 | + |
| 52 | +public class CCSCanMatchIT extends AbstractMultiClustersTestCase { |
| 53 | + static final String REMOTE_CLUSTER = "cluster_a"; |
| 54 | + |
| 55 | + @Override |
| 56 | + protected Collection<String> remoteClusterAlias() { |
| 57 | + return List.of("cluster_a"); |
| 58 | + } |
| 59 | + |
| 60 | + private static class EngineWithExposingTimestamp extends InternalEngine { |
| 61 | + EngineWithExposingTimestamp(EngineConfig engineConfig) { |
| 62 | + super(engineConfig); |
| 63 | + assert IndexMetadata.INDEX_BLOCKS_WRITE_SETTING.get(config().getIndexSettings().getSettings()) : "require read-only index"; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public ShardLongFieldRange getRawFieldRange(String field) { |
| 68 | + try (Searcher searcher = acquireSearcher("test")) { |
| 69 | + final DirectoryReader directoryReader = searcher.getDirectoryReader(); |
| 70 | + |
| 71 | + final byte[] minPackedValue = PointValues.getMinPackedValue(directoryReader, field); |
| 72 | + final byte[] maxPackedValue = PointValues.getMaxPackedValue(directoryReader, field); |
| 73 | + if (minPackedValue == null || maxPackedValue == null) { |
| 74 | + assert minPackedValue == null && maxPackedValue == null |
| 75 | + : Arrays.toString(minPackedValue) + "-" + Arrays.toString(maxPackedValue); |
| 76 | + return ShardLongFieldRange.EMPTY; |
| 77 | + } |
| 78 | + |
| 79 | + return ShardLongFieldRange.of(LongPoint.decodeDimension(minPackedValue, 0), LongPoint.decodeDimension(maxPackedValue, 0)); |
| 80 | + } catch (IOException e) { |
| 81 | + throw new UncheckedIOException(e); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public static class ExposingTimestampEnginePlugin extends Plugin implements EnginePlugin { |
| 87 | + |
| 88 | + @Override |
| 89 | + public Optional<EngineFactory> getEngineFactory(IndexSettings indexSettings) { |
| 90 | + if (IndexMetadata.INDEX_BLOCKS_WRITE_SETTING.get(indexSettings.getSettings())) { |
| 91 | + return Optional.of(EngineWithExposingTimestamp::new); |
| 92 | + } else { |
| 93 | + return Optional.of(new InternalEngineFactory()); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + protected Collection<Class<? extends Plugin>> nodePlugins(String clusterAlias) { |
| 100 | + return CollectionUtils.appendToCopy(super.nodePlugins(clusterAlias), ExposingTimestampEnginePlugin.class); |
| 101 | + } |
| 102 | + |
| 103 | + int createIndexAndIndexDocs(String cluster, String index, int numberOfShards, long timestamp, boolean exposeTimestamp) |
| 104 | + throws Exception { |
| 105 | + Client client = client(cluster); |
| 106 | + ElasticsearchAssertions.assertAcked( |
| 107 | + client.admin() |
| 108 | + .indices() |
| 109 | + .prepareCreate(index) |
| 110 | + .setSettings( |
| 111 | + Settings.builder() |
| 112 | + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, numberOfShards) |
| 113 | + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) |
| 114 | + ) |
| 115 | + .setMapping("@timestamp", "type=date", "position", "type=long") |
| 116 | + ); |
| 117 | + int numDocs = between(100, 500); |
| 118 | + for (int i = 0; i < numDocs; i++) { |
| 119 | + client.prepareIndex(index).setSource("position", i, "@timestamp", timestamp + i).get(); |
| 120 | + } |
| 121 | + if (exposeTimestamp) { |
| 122 | + client.admin().indices().prepareClose(index).get(); |
| 123 | + client.admin() |
| 124 | + .indices() |
| 125 | + .prepareUpdateSettings(index) |
| 126 | + .setSettings(Settings.builder().put(IndexMetadata.INDEX_BLOCKS_WRITE_SETTING.getKey(), true).build()) |
| 127 | + .get(); |
| 128 | + client.admin().indices().prepareOpen(index).get(); |
| 129 | + assertBusy(() -> { |
| 130 | + IndexLongFieldRange timestampRange = cluster(cluster).clusterService().state().metadata().index(index).getTimestampRange(); |
| 131 | + assertTrue(Strings.toString(timestampRange), timestampRange.containsAllShardRanges()); |
| 132 | + }); |
| 133 | + } else { |
| 134 | + client.admin().indices().prepareRefresh(index).get(); |
| 135 | + } |
| 136 | + return numDocs; |
| 137 | + } |
| 138 | + |
| 139 | + public void testCanMatchOnTimeRange() throws Exception { |
| 140 | + long timestamp = randomLongBetween(10_000_000, 50_000_000); |
| 141 | + int oldLocalNumShards = randomIntBetween(1, 5); |
| 142 | + createIndexAndIndexDocs(LOCAL_CLUSTER, "local_old_index", oldLocalNumShards, timestamp - 10_000, true); |
| 143 | + int oldRemoteNumShards = randomIntBetween(1, 5); |
| 144 | + createIndexAndIndexDocs(REMOTE_CLUSTER, "remote_old_index", oldRemoteNumShards, timestamp - 10_000, true); |
| 145 | + |
| 146 | + int newLocalNumShards = randomIntBetween(1, 5); |
| 147 | + int localDocs = createIndexAndIndexDocs(LOCAL_CLUSTER, "local_new_index", newLocalNumShards, timestamp, randomBoolean()); |
| 148 | + int newRemoteNumShards = randomIntBetween(1, 5); |
| 149 | + int remoteDocs = createIndexAndIndexDocs(REMOTE_CLUSTER, "remote_new_index", newRemoteNumShards, timestamp, randomBoolean()); |
| 150 | + |
| 151 | + for (String cluster : List.of(LOCAL_CLUSTER, REMOTE_CLUSTER)) { |
| 152 | + for (TransportService ts : cluster(cluster).getInstances(TransportService.class)) { |
| 153 | + MockTransportService mockTransportService = (MockTransportService) ts; |
| 154 | + mockTransportService.addSendBehavior((connection, requestId, action, request, options) -> { |
| 155 | + if (action.equals(SearchTransportService.QUERY_CAN_MATCH_NODE_NAME)) { |
| 156 | + CanMatchNodeRequest canMatchNodeRequest = (CanMatchNodeRequest) request; |
| 157 | + List<String> indices = canMatchNodeRequest.getShardLevelRequests() |
| 158 | + .stream() |
| 159 | + .map(r -> r.shardId().getIndexName()) |
| 160 | + .toList(); |
| 161 | + assertThat("old indices should be prefiltered on coordinator node", "local_old_index", Matchers.not(in(indices))); |
| 162 | + assertThat("old indices should be prefiltered on coordinator node", "remote_old_index", Matchers.not(in(indices))); |
| 163 | + if (cluster.equals(LOCAL_CLUSTER)) { |
| 164 | + DiscoveryNode targetNode = connection.getNode(); |
| 165 | + DiscoveryNodes remoteNodes = cluster(REMOTE_CLUSTER).clusterService().state().nodes(); |
| 166 | + assertNull("No can_match requests sent across clusters", remoteNodes.get(targetNode.getId())); |
| 167 | + } |
| 168 | + } |
| 169 | + connection.sendRequest(requestId, action, request, options); |
| 170 | + }); |
| 171 | + } |
| 172 | + } |
| 173 | + try { |
| 174 | + for (boolean minimizeRoundTrips : List.of(true, false)) { |
| 175 | + SearchSourceBuilder source = new SearchSourceBuilder().query(new RangeQueryBuilder("@timestamp").from(timestamp)); |
| 176 | + SearchRequest request = new SearchRequest("local_*", "*:remote_*"); |
| 177 | + request.source(source).setCcsMinimizeRoundtrips(minimizeRoundTrips); |
| 178 | + SearchResponse searchResp = client().search(request).actionGet(); |
| 179 | + ElasticsearchAssertions.assertHitCount(searchResp, localDocs + remoteDocs); |
| 180 | + int totalShards = oldLocalNumShards + newLocalNumShards + oldRemoteNumShards + newRemoteNumShards; |
| 181 | + assertThat(searchResp.getTotalShards(), equalTo(totalShards)); |
| 182 | + assertThat(searchResp.getSkippedShards(), equalTo(oldLocalNumShards + oldRemoteNumShards)); |
| 183 | + } |
| 184 | + } finally { |
| 185 | + for (String cluster : List.of(LOCAL_CLUSTER, REMOTE_CLUSTER)) { |
| 186 | + for (TransportService ts : cluster(cluster).getInstances(TransportService.class)) { |
| 187 | + MockTransportService mockTransportService = (MockTransportService) ts; |
| 188 | + mockTransportService.clearAllRules(); |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | +} |
0 commit comments