|
| 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.index; |
| 11 | + |
| 12 | +import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; |
| 13 | +import org.elasticsearch.action.admin.indices.create.TransportCreateIndexAction; |
| 14 | +import org.elasticsearch.action.admin.indices.shrink.ResizeAction; |
| 15 | +import org.elasticsearch.action.admin.indices.shrink.ResizeRequest; |
| 16 | +import org.elasticsearch.action.admin.indices.shrink.ResizeType; |
| 17 | +import org.elasticsearch.action.fieldcaps.FieldCapabilitiesIndexResponse; |
| 18 | +import org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequest; |
| 19 | +import org.elasticsearch.action.search.SearchResponse; |
| 20 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 21 | +import org.elasticsearch.common.settings.Settings; |
| 22 | +import org.elasticsearch.index.query.MatchQueryBuilder; |
| 23 | +import org.elasticsearch.search.SearchHit; |
| 24 | +import org.elasticsearch.test.ESIntegTestCase; |
| 25 | + |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 29 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; |
| 30 | +import static org.hamcrest.Matchers.equalTo; |
| 31 | +import static org.hamcrest.Matchers.hasSize; |
| 32 | + |
| 33 | +public class LookupIndexModeIT extends ESIntegTestCase { |
| 34 | + |
| 35 | + @Override |
| 36 | + protected int numberOfShards() { |
| 37 | + return 1; |
| 38 | + } |
| 39 | + |
| 40 | + public void testBasic() { |
| 41 | + internalCluster().ensureAtLeastNumDataNodes(1); |
| 42 | + Settings.Builder lookupSettings = Settings.builder().put("index.mode", "lookup"); |
| 43 | + if (randomBoolean()) { |
| 44 | + lookupSettings.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1); |
| 45 | + } |
| 46 | + CreateIndexRequest createRequest = new CreateIndexRequest("hosts"); |
| 47 | + createRequest.settings(lookupSettings); |
| 48 | + createRequest.simpleMapping("ip", "type=ip", "os", "type=keyword"); |
| 49 | + assertAcked(client().admin().indices().execute(TransportCreateIndexAction.TYPE, createRequest)); |
| 50 | + Settings settings = client().admin().indices().prepareGetSettings("hosts").get().getIndexToSettings().get("hosts"); |
| 51 | + assertThat(settings.get("index.mode"), equalTo("lookup")); |
| 52 | + assertThat(settings.get("index.auto_expand_replicas"), equalTo("0-all")); |
| 53 | + Map<String, String> allHosts = Map.of( |
| 54 | + "192.168.1.2", |
| 55 | + "Windows", |
| 56 | + "192.168.1.3", |
| 57 | + "MacOS", |
| 58 | + "192.168.1.4", |
| 59 | + "Linux", |
| 60 | + "192.168.1.5", |
| 61 | + "Android", |
| 62 | + "192.168.1.6", |
| 63 | + "iOS", |
| 64 | + "192.168.1.7", |
| 65 | + "Windows", |
| 66 | + "192.168.1.8", |
| 67 | + "MacOS", |
| 68 | + "192.168.1.9", |
| 69 | + "Linux", |
| 70 | + "192.168.1.10", |
| 71 | + "Linux", |
| 72 | + "192.168.1.11", |
| 73 | + "Windows" |
| 74 | + ); |
| 75 | + for (Map.Entry<String, String> e : allHosts.entrySet()) { |
| 76 | + client().prepareIndex("hosts").setSource("ip", e.getKey(), "os", e.getValue()).get(); |
| 77 | + } |
| 78 | + refresh("hosts"); |
| 79 | + assertAcked(client().admin().indices().prepareCreate("events").setSettings(Settings.builder().put("index.mode", "logsdb")).get()); |
| 80 | + int numDocs = between(1, 10); |
| 81 | + for (int i = 0; i < numDocs; i++) { |
| 82 | + String ip = randomFrom(allHosts.keySet()); |
| 83 | + String message = randomFrom("login", "logout", "shutdown", "restart"); |
| 84 | + client().prepareIndex("events").setSource("@timestamp", "2024-01-01", "ip", ip, "message", message).get(); |
| 85 | + } |
| 86 | + refresh("events"); |
| 87 | + // _search |
| 88 | + { |
| 89 | + SearchResponse resp = prepareSearch("events", "hosts").setQuery(new MatchQueryBuilder("_index_mode", "lookup")) |
| 90 | + .setSize(10000) |
| 91 | + .get(); |
| 92 | + for (SearchHit hit : resp.getHits()) { |
| 93 | + assertThat(hit.getIndex(), equalTo("hosts")); |
| 94 | + } |
| 95 | + assertHitCount(resp, allHosts.size()); |
| 96 | + resp.decRef(); |
| 97 | + } |
| 98 | + // field_caps |
| 99 | + { |
| 100 | + FieldCapabilitiesRequest request = new FieldCapabilitiesRequest(); |
| 101 | + request.indices("events", "hosts"); |
| 102 | + request.fields("*"); |
| 103 | + request.setMergeResults(false); |
| 104 | + request.indexFilter(new MatchQueryBuilder("_index_mode", "lookup")); |
| 105 | + var resp = client().fieldCaps(request).actionGet(); |
| 106 | + assertThat(resp.getIndexResponses(), hasSize(1)); |
| 107 | + FieldCapabilitiesIndexResponse indexResponse = resp.getIndexResponses().get(0); |
| 108 | + assertThat(indexResponse.getIndexMode(), equalTo(IndexMode.LOOKUP)); |
| 109 | + assertThat(indexResponse.getIndexName(), equalTo("hosts")); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + public void testRejectMoreThanOneShard() { |
| 114 | + int numberOfShards = between(2, 5); |
| 115 | + IllegalArgumentException error = expectThrows(IllegalArgumentException.class, () -> { |
| 116 | + client().admin() |
| 117 | + .indices() |
| 118 | + .prepareCreate("hosts") |
| 119 | + .setSettings(Settings.builder().put("index.mode", "lookup").put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, numberOfShards)) |
| 120 | + .setMapping("ip", "type=ip", "os", "type=keyword") |
| 121 | + .get(); |
| 122 | + }); |
| 123 | + assertThat( |
| 124 | + error.getMessage(), |
| 125 | + equalTo("index with [lookup] mode must have [index.number_of_shards] set to 1 or unset; provided " + numberOfShards) |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + public void testResizeLookupIndex() { |
| 130 | + Settings.Builder createSettings = Settings.builder().put("index.mode", "lookup"); |
| 131 | + if (randomBoolean()) { |
| 132 | + createSettings.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1); |
| 133 | + } |
| 134 | + CreateIndexRequest createIndexRequest = new CreateIndexRequest("lookup-1").settings(createSettings); |
| 135 | + assertAcked(client().admin().indices().execute(TransportCreateIndexAction.TYPE, createIndexRequest)); |
| 136 | + client().admin().indices().prepareAddBlock(IndexMetadata.APIBlock.WRITE, "lookup-1").get(); |
| 137 | + |
| 138 | + ResizeRequest clone = new ResizeRequest("lookup-2", "lookup-1"); |
| 139 | + clone.setResizeType(ResizeType.CLONE); |
| 140 | + assertAcked(client().admin().indices().execute(ResizeAction.INSTANCE, clone).actionGet()); |
| 141 | + Settings settings = client().admin().indices().prepareGetSettings("lookup-2").get().getIndexToSettings().get("lookup-2"); |
| 142 | + assertThat(settings.get("index.mode"), equalTo("lookup")); |
| 143 | + assertThat(settings.get("index.number_of_shards"), equalTo("1")); |
| 144 | + assertThat(settings.get("index.auto_expand_replicas"), equalTo("0-all")); |
| 145 | + |
| 146 | + ResizeRequest split = new ResizeRequest("lookup-3", "lookup-1"); |
| 147 | + split.setResizeType(ResizeType.SPLIT); |
| 148 | + split.getTargetIndexRequest().settings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 3)); |
| 149 | + IllegalArgumentException error = expectThrows( |
| 150 | + IllegalArgumentException.class, |
| 151 | + () -> client().admin().indices().execute(ResizeAction.INSTANCE, split).actionGet() |
| 152 | + ); |
| 153 | + assertThat( |
| 154 | + error.getMessage(), |
| 155 | + equalTo("index with [lookup] mode must have [index.number_of_shards] set to 1 or unset; provided 3") |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + public void testResizeRegularIndexToLookup() { |
| 160 | + String dataNode = internalCluster().startDataOnlyNode(); |
| 161 | + assertAcked( |
| 162 | + client().admin() |
| 163 | + .indices() |
| 164 | + .prepareCreate("regular-1") |
| 165 | + .setSettings( |
| 166 | + Settings.builder() |
| 167 | + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 2) |
| 168 | + .put("index.routing.allocation.require._name", dataNode) |
| 169 | + ) |
| 170 | + .setMapping("ip", "type=ip", "os", "type=keyword") |
| 171 | + .get() |
| 172 | + ); |
| 173 | + client().admin().indices().prepareAddBlock(IndexMetadata.APIBlock.WRITE, "regular-1").get(); |
| 174 | + client().admin() |
| 175 | + .indices() |
| 176 | + .prepareUpdateSettings("regular-1") |
| 177 | + .setSettings(Settings.builder().put("index.number_of_replicas", 0)) |
| 178 | + .get(); |
| 179 | + |
| 180 | + ResizeRequest clone = new ResizeRequest("lookup-3", "regular-1"); |
| 181 | + clone.setResizeType(ResizeType.CLONE); |
| 182 | + clone.getTargetIndexRequest().settings(Settings.builder().put("index.mode", "lookup")); |
| 183 | + IllegalArgumentException error = expectThrows( |
| 184 | + IllegalArgumentException.class, |
| 185 | + () -> client().admin().indices().execute(ResizeAction.INSTANCE, clone).actionGet() |
| 186 | + ); |
| 187 | + assertThat( |
| 188 | + error.getMessage(), |
| 189 | + equalTo("index with [lookup] mode must have [index.number_of_shards] set to 1 or unset; provided 2") |
| 190 | + ); |
| 191 | + |
| 192 | + ResizeRequest shrink = new ResizeRequest("lookup-4", "regular-1"); |
| 193 | + shrink.setResizeType(ResizeType.SHRINK); |
| 194 | + shrink.getTargetIndexRequest() |
| 195 | + .settings(Settings.builder().put("index.mode", "lookup").put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)); |
| 196 | + |
| 197 | + error = expectThrows( |
| 198 | + IllegalArgumentException.class, |
| 199 | + () -> client().admin().indices().execute(ResizeAction.INSTANCE, shrink).actionGet() |
| 200 | + ); |
| 201 | + assertThat(error.getMessage(), equalTo("can't change index.mode of index [regular-1] from [standard] to [lookup]")); |
| 202 | + } |
| 203 | + |
| 204 | + public void testDoNotOverrideAutoExpandReplicas() { |
| 205 | + internalCluster().ensureAtLeastNumDataNodes(1); |
| 206 | + Settings.Builder createSettings = Settings.builder().put("index.mode", "lookup"); |
| 207 | + if (randomBoolean()) { |
| 208 | + createSettings.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1); |
| 209 | + } |
| 210 | + createSettings.put("index.auto_expand_replicas", "3-5"); |
| 211 | + CreateIndexRequest createRequest = new CreateIndexRequest("hosts"); |
| 212 | + createRequest.settings(createSettings); |
| 213 | + createRequest.simpleMapping("ip", "type=ip", "os", "type=keyword"); |
| 214 | + assertAcked(client().admin().indices().execute(TransportCreateIndexAction.TYPE, createRequest)); |
| 215 | + Settings settings = client().admin().indices().prepareGetSettings("hosts").get().getIndexToSettings().get("hosts"); |
| 216 | + assertThat(settings.get("index.mode"), equalTo("lookup")); |
| 217 | + assertThat(settings.get("index.auto_expand_replicas"), equalTo("3-5")); |
| 218 | + } |
| 219 | +} |
0 commit comments