|
| 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", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.action.fieldcaps; |
| 11 | + |
| 12 | +import org.apache.lucene.document.LongPoint; |
| 13 | +import org.apache.lucene.index.DirectoryReader; |
| 14 | +import org.apache.lucene.index.PointValues; |
| 15 | +import org.elasticsearch.action.NoShardAvailableActionException; |
| 16 | +import org.elasticsearch.client.internal.Client; |
| 17 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 18 | +import org.elasticsearch.common.Strings; |
| 19 | +import org.elasticsearch.common.settings.Settings; |
| 20 | +import org.elasticsearch.common.util.CollectionUtils; |
| 21 | +import org.elasticsearch.index.IndexSettings; |
| 22 | +import org.elasticsearch.index.engine.EngineConfig; |
| 23 | +import org.elasticsearch.index.engine.EngineFactory; |
| 24 | +import org.elasticsearch.index.engine.InternalEngine; |
| 25 | +import org.elasticsearch.index.engine.InternalEngineFactory; |
| 26 | +import org.elasticsearch.index.query.RangeQueryBuilder; |
| 27 | +import org.elasticsearch.index.shard.IndexLongFieldRange; |
| 28 | +import org.elasticsearch.index.shard.ShardLongFieldRange; |
| 29 | +import org.elasticsearch.plugins.EnginePlugin; |
| 30 | +import org.elasticsearch.plugins.Plugin; |
| 31 | +import org.elasticsearch.test.ESIntegTestCase; |
| 32 | + |
| 33 | +import java.io.IOException; |
| 34 | +import java.io.UncheckedIOException; |
| 35 | +import java.util.Arrays; |
| 36 | +import java.util.Collection; |
| 37 | +import java.util.Optional; |
| 38 | + |
| 39 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 40 | +import static org.hamcrest.Matchers.empty; |
| 41 | +import static org.hamcrest.Matchers.equalTo; |
| 42 | +import static org.hamcrest.Matchers.hasSize; |
| 43 | +import static org.hamcrest.Matchers.instanceOf; |
| 44 | + |
| 45 | +public class FieldCapsWithFilterIT extends ESIntegTestCase { |
| 46 | + @Override |
| 47 | + protected boolean addMockInternalEngine() { |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + private static class EngineWithExposingTimestamp extends InternalEngine { |
| 52 | + EngineWithExposingTimestamp(EngineConfig engineConfig) { |
| 53 | + super(engineConfig); |
| 54 | + assert IndexMetadata.INDEX_BLOCKS_WRITE_SETTING.get(config().getIndexSettings().getSettings()) : "require read-only index"; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public ShardLongFieldRange getRawFieldRange(String field) { |
| 59 | + try (Searcher searcher = acquireSearcher("test")) { |
| 60 | + final DirectoryReader directoryReader = searcher.getDirectoryReader(); |
| 61 | + |
| 62 | + final byte[] minPackedValue = PointValues.getMinPackedValue(directoryReader, field); |
| 63 | + final byte[] maxPackedValue = PointValues.getMaxPackedValue(directoryReader, field); |
| 64 | + if (minPackedValue == null || maxPackedValue == null) { |
| 65 | + assert minPackedValue == null && maxPackedValue == null |
| 66 | + : Arrays.toString(minPackedValue) + "-" + Arrays.toString(maxPackedValue); |
| 67 | + return ShardLongFieldRange.EMPTY; |
| 68 | + } |
| 69 | + |
| 70 | + return ShardLongFieldRange.of(LongPoint.decodeDimension(minPackedValue, 0), LongPoint.decodeDimension(maxPackedValue, 0)); |
| 71 | + } catch (IOException e) { |
| 72 | + throw new UncheckedIOException(e); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public static class ExposingTimestampEnginePlugin extends Plugin implements EnginePlugin { |
| 78 | + @Override |
| 79 | + public Optional<EngineFactory> getEngineFactory(IndexSettings indexSettings) { |
| 80 | + if (IndexMetadata.INDEX_BLOCKS_WRITE_SETTING.get(indexSettings.getSettings())) { |
| 81 | + return Optional.of(EngineWithExposingTimestamp::new); |
| 82 | + } else { |
| 83 | + return Optional.of(new InternalEngineFactory()); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 90 | + return CollectionUtils.appendToCopy(super.nodePlugins(), ExposingTimestampEnginePlugin.class); |
| 91 | + } |
| 92 | + |
| 93 | + void createIndexAndIndexDocs(String index, Settings.Builder indexSettings, long timestamp, boolean exposeTimestamp) throws Exception { |
| 94 | + Client client = client(); |
| 95 | + assertAcked( |
| 96 | + client.admin() |
| 97 | + .indices() |
| 98 | + .prepareCreate(index) |
| 99 | + .setSettings(indexSettings) |
| 100 | + .setMapping("@timestamp", "type=date", "position", "type=long") |
| 101 | + ); |
| 102 | + int numDocs = between(100, 500); |
| 103 | + for (int i = 0; i < numDocs; i++) { |
| 104 | + client.prepareIndex(index).setSource("position", i, "@timestamp", timestamp + i).get(); |
| 105 | + } |
| 106 | + if (exposeTimestamp) { |
| 107 | + client.admin().indices().prepareClose(index).get(); |
| 108 | + client.admin() |
| 109 | + .indices() |
| 110 | + .prepareUpdateSettings(index) |
| 111 | + .setSettings(Settings.builder().put(IndexMetadata.INDEX_BLOCKS_WRITE_SETTING.getKey(), true).build()) |
| 112 | + .get(); |
| 113 | + client.admin().indices().prepareOpen(index).get(); |
| 114 | + assertBusy(() -> { |
| 115 | + IndexLongFieldRange timestampRange = clusterService().state().metadata().index(index).getTimestampRange(); |
| 116 | + assertTrue(Strings.toString(timestampRange), timestampRange.containsAllShardRanges()); |
| 117 | + }); |
| 118 | + } else { |
| 119 | + client.admin().indices().prepareRefresh(index).get(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + public void testSkipUnmatchedShards() throws Exception { |
| 124 | + long oldTimestamp = randomLongBetween(10_000_000, 20_000_000); |
| 125 | + long newTimestamp = randomLongBetween(30_000_000, 50_000_000); |
| 126 | + String redNode = internalCluster().startDataOnlyNode(); |
| 127 | + String blueNode = internalCluster().startDataOnlyNode(); |
| 128 | + createIndexAndIndexDocs( |
| 129 | + "index_old", |
| 130 | + indexSettings(between(1, 5), 0).put("index.routing.allocation.include._name", redNode), |
| 131 | + oldTimestamp, |
| 132 | + true |
| 133 | + ); |
| 134 | + internalCluster().stopNode(redNode); |
| 135 | + createIndexAndIndexDocs( |
| 136 | + "index_new", |
| 137 | + indexSettings(between(1, 5), 0).put("index.routing.allocation.include._name", blueNode), |
| 138 | + newTimestamp, |
| 139 | + false |
| 140 | + ); |
| 141 | + // fails without index filter |
| 142 | + { |
| 143 | + FieldCapabilitiesRequest request = new FieldCapabilitiesRequest(); |
| 144 | + request.indices("index_*"); |
| 145 | + request.fields("*"); |
| 146 | + request.setMergeResults(false); |
| 147 | + if (randomBoolean()) { |
| 148 | + request.indexFilter(new RangeQueryBuilder("@timestamp").from(oldTimestamp)); |
| 149 | + } |
| 150 | + var response = safeGet(client().execute(TransportFieldCapabilitiesAction.TYPE, request)); |
| 151 | + assertThat(response.getIndexResponses(), hasSize(1)); |
| 152 | + assertThat(response.getIndexResponses().get(0).getIndexName(), equalTo("index_new")); |
| 153 | + assertThat(response.getFailures(), hasSize(1)); |
| 154 | + assertThat(response.getFailures().get(0).getIndices(), equalTo(new String[] { "index_old" })); |
| 155 | + assertThat(response.getFailures().get(0).getException(), instanceOf(NoShardAvailableActionException.class)); |
| 156 | + } |
| 157 | + // skip unavailable shards with index filter |
| 158 | + { |
| 159 | + FieldCapabilitiesRequest request = new FieldCapabilitiesRequest(); |
| 160 | + request.indices("index_*"); |
| 161 | + request.fields("*"); |
| 162 | + request.indexFilter(new RangeQueryBuilder("@timestamp").from(newTimestamp)); |
| 163 | + request.setMergeResults(false); |
| 164 | + var response = safeGet(client().execute(TransportFieldCapabilitiesAction.TYPE, request)); |
| 165 | + assertThat(response.getIndexResponses(), hasSize(1)); |
| 166 | + assertThat(response.getIndexResponses().get(0).getIndexName(), equalTo("index_new")); |
| 167 | + assertThat(response.getFailures(), empty()); |
| 168 | + } |
| 169 | + // skip both indices on the coordinator, one the data nodes |
| 170 | + { |
| 171 | + FieldCapabilitiesRequest request = new FieldCapabilitiesRequest(); |
| 172 | + request.indices("index_*"); |
| 173 | + request.fields("*"); |
| 174 | + request.indexFilter(new RangeQueryBuilder("@timestamp").from(newTimestamp * 2L)); |
| 175 | + request.setMergeResults(false); |
| 176 | + var response = safeGet(client().execute(TransportFieldCapabilitiesAction.TYPE, request)); |
| 177 | + assertThat(response.getIndexResponses(), empty()); |
| 178 | + assertThat(response.getFailures(), empty()); |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments