|
24 | 24 | import org.elasticsearch.Version; |
25 | 25 | import org.elasticsearch.action.ActionRequestValidationException; |
26 | 26 | import org.elasticsearch.action.admin.indices.alias.Alias; |
| 27 | +import org.elasticsearch.action.admin.indices.datastream.GetDataStreamsAction; |
| 28 | +import org.elasticsearch.action.admin.indices.get.GetIndexRequest; |
| 29 | +import org.elasticsearch.action.admin.indices.get.GetIndexResponse; |
27 | 30 | import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse; |
| 31 | +import org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateV2Action; |
| 32 | +import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest; |
| 33 | +import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateV2Action; |
28 | 34 | import org.elasticsearch.action.index.IndexRequest; |
29 | 35 | import org.elasticsearch.action.index.IndexResponse; |
30 | 36 | import org.elasticsearch.action.ingest.PutPipelineRequest; |
31 | 37 | import org.elasticsearch.action.support.master.AcknowledgedResponse; |
32 | 38 | import org.elasticsearch.action.support.replication.ReplicationRequest; |
| 39 | +import org.elasticsearch.cluster.metadata.DataStream; |
33 | 40 | import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 41 | +import org.elasticsearch.cluster.metadata.IndexTemplateV2; |
| 42 | +import org.elasticsearch.cluster.metadata.Template; |
| 43 | +import org.elasticsearch.common.Strings; |
34 | 44 | import org.elasticsearch.common.bytes.BytesReference; |
| 45 | +import org.elasticsearch.common.settings.Settings; |
35 | 46 | import org.elasticsearch.common.xcontent.XContentBuilder; |
36 | 47 | import org.elasticsearch.common.xcontent.XContentType; |
37 | 48 | import org.elasticsearch.ingest.IngestTestPlugin; |
|
44 | 55 | import java.util.Arrays; |
45 | 56 | import java.util.Collection; |
46 | 57 | import java.util.Collections; |
| 58 | +import java.util.Comparator; |
47 | 59 | import java.util.Map; |
48 | 60 | import java.util.concurrent.ExecutionException; |
49 | 61 | import java.util.concurrent.atomic.AtomicBoolean; |
50 | 62 | import java.util.concurrent.atomic.AtomicInteger; |
51 | 63 |
|
| 64 | +import static org.elasticsearch.action.DocWriteRequest.OpType.CREATE; |
52 | 65 | import static org.elasticsearch.action.DocWriteResponse.Result.CREATED; |
53 | 66 | import static org.elasticsearch.action.DocWriteResponse.Result.UPDATED; |
54 | 67 | import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; |
55 | 68 | import static org.elasticsearch.test.StreamsUtils.copyToStringFromClasspath; |
56 | 69 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 70 | +import static org.hamcrest.Matchers.arrayWithSize; |
57 | 71 | import static org.hamcrest.Matchers.containsInAnyOrder; |
58 | 72 | import static org.hamcrest.Matchers.containsString; |
59 | 73 | import static org.hamcrest.Matchers.equalTo; |
60 | 74 | import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 75 | +import static org.hamcrest.Matchers.hasItemInArray; |
| 76 | +import static org.hamcrest.Matchers.hasSize; |
61 | 77 | import static org.hamcrest.Matchers.is; |
62 | 78 | import static org.hamcrest.Matchers.oneOf; |
63 | 79 |
|
@@ -202,4 +218,87 @@ public void testDeleteIndexWhileIndexing() throws Exception { |
202 | 218 | assertFalse(thread.isAlive()); |
203 | 219 | } |
204 | 220 | } |
| 221 | + |
| 222 | + public void testMixedAutoCreate() { |
| 223 | + Settings settings = Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).build(); |
| 224 | + |
| 225 | + PutIndexTemplateV2Action.Request createTemplateRequest = new PutIndexTemplateV2Action.Request("logs-foo"); |
| 226 | + createTemplateRequest.indexTemplate( |
| 227 | + new IndexTemplateV2( |
| 228 | + Collections.singletonList("logs-foo*"), |
| 229 | + new Template(settings, null, null), |
| 230 | + null, null, null, null, |
| 231 | + new IndexTemplateV2.DataStreamTemplate("@timestamp")) |
| 232 | + ); |
| 233 | + client().execute(PutIndexTemplateV2Action.INSTANCE, createTemplateRequest).actionGet(); |
| 234 | + |
| 235 | + BulkRequest bulkRequest = new BulkRequest(); |
| 236 | + bulkRequest.add(new IndexRequest("logs-foobar").opType(CREATE).source("{}", XContentType.JSON)); |
| 237 | + bulkRequest.add(new IndexRequest("logs-foobaz").opType(CREATE).source("{}", XContentType.JSON)); |
| 238 | + bulkRequest.add(new IndexRequest("logs-barbaz").opType(CREATE).source("{}", XContentType.JSON)); |
| 239 | + bulkRequest.add(new IndexRequest("logs-barfoo").opType(CREATE).source("{}", XContentType.JSON)); |
| 240 | + BulkResponse bulkResponse = client().bulk(bulkRequest).actionGet(); |
| 241 | + assertThat("bulk failures: " + Strings.toString(bulkResponse), bulkResponse.hasFailures(), is(false)); |
| 242 | + |
| 243 | + bulkRequest = new BulkRequest(); |
| 244 | + bulkRequest.add(new IndexRequest("logs-foobar").opType(CREATE).source("{}", XContentType.JSON)); |
| 245 | + bulkRequest.add(new IndexRequest("logs-foobaz2").opType(CREATE).source("{}", XContentType.JSON)); |
| 246 | + bulkRequest.add(new IndexRequest("logs-barbaz").opType(CREATE).source("{}", XContentType.JSON)); |
| 247 | + bulkRequest.add(new IndexRequest("logs-barfoo2").opType(CREATE).source("{}", XContentType.JSON)); |
| 248 | + bulkResponse = client().bulk(bulkRequest).actionGet(); |
| 249 | + assertThat("bulk failures: " + Strings.toString(bulkResponse), bulkResponse.hasFailures(), is(false)); |
| 250 | + |
| 251 | + bulkRequest = new BulkRequest(); |
| 252 | + bulkRequest.add(new IndexRequest("logs-foobar").opType(CREATE).source("{}", XContentType.JSON)); |
| 253 | + bulkRequest.add(new IndexRequest("logs-foobaz2").opType(CREATE).source("{}", XContentType.JSON)); |
| 254 | + bulkRequest.add(new IndexRequest("logs-foobaz3").opType(CREATE).source("{}", XContentType.JSON)); |
| 255 | + bulkRequest.add(new IndexRequest("logs-barbaz").opType(CREATE).source("{}", XContentType.JSON)); |
| 256 | + bulkRequest.add(new IndexRequest("logs-barfoo2").opType(CREATE).source("{}", XContentType.JSON)); |
| 257 | + bulkRequest.add(new IndexRequest("logs-barfoo3").opType(CREATE).source("{}", XContentType.JSON)); |
| 258 | + bulkResponse = client().bulk(bulkRequest).actionGet(); |
| 259 | + assertThat("bulk failures: " + Strings.toString(bulkResponse), bulkResponse.hasFailures(), is(false)); |
| 260 | + |
| 261 | + GetDataStreamsAction.Request getDataStreamRequest = new GetDataStreamsAction.Request("*"); |
| 262 | + GetDataStreamsAction.Response getDataStreamsResponse = client().admin().indices().getDataStreams(getDataStreamRequest).actionGet(); |
| 263 | + assertThat(getDataStreamsResponse.getDataStreams(), hasSize(4)); |
| 264 | + getDataStreamsResponse.getDataStreams().sort(Comparator.comparing(DataStream::getName)); |
| 265 | + assertThat(getDataStreamsResponse.getDataStreams().get(0).getName(), equalTo("logs-foobar")); |
| 266 | + assertThat(getDataStreamsResponse.getDataStreams().get(1).getName(), equalTo("logs-foobaz")); |
| 267 | + assertThat(getDataStreamsResponse.getDataStreams().get(2).getName(), equalTo("logs-foobaz2")); |
| 268 | + assertThat(getDataStreamsResponse.getDataStreams().get(3).getName(), equalTo("logs-foobaz3")); |
| 269 | + |
| 270 | + GetIndexResponse getIndexResponse = client().admin().indices().getIndex(new GetIndexRequest().indices("logs-bar*")).actionGet(); |
| 271 | + assertThat(getIndexResponse.getIndices(), arrayWithSize(4)); |
| 272 | + assertThat(getIndexResponse.getIndices(), hasItemInArray("logs-barbaz")); |
| 273 | + assertThat(getIndexResponse.getIndices(), hasItemInArray("logs-barfoo")); |
| 274 | + assertThat(getIndexResponse.getIndices(), hasItemInArray("logs-barfoo2")); |
| 275 | + assertThat(getIndexResponse.getIndices(), hasItemInArray("logs-barfoo3")); |
| 276 | + |
| 277 | + DeleteIndexTemplateV2Action.Request deleteTemplateRequest = new DeleteIndexTemplateV2Action.Request("*"); |
| 278 | + client().execute(DeleteIndexTemplateV2Action.INSTANCE, deleteTemplateRequest).actionGet(); |
| 279 | + } |
| 280 | + |
| 281 | + public void testAutoCreateV1TemplateNoDataStream() { |
| 282 | + Settings settings = Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).build(); |
| 283 | + |
| 284 | + PutIndexTemplateRequest v1Request = new PutIndexTemplateRequest("logs-foo"); |
| 285 | + v1Request.patterns(Collections.singletonList("logs-foo*")); |
| 286 | + v1Request.settings(settings); |
| 287 | + v1Request.order(Integer.MAX_VALUE); // in order to avoid number_of_replicas being overwritten by random_template |
| 288 | + client().admin().indices().putTemplate(v1Request).actionGet(); |
| 289 | + |
| 290 | + BulkRequest bulkRequest = new BulkRequest(); |
| 291 | + bulkRequest.add(new IndexRequest("logs-foobar").opType(CREATE).source("{}", XContentType.JSON)); |
| 292 | + BulkResponse bulkResponse = client().bulk(bulkRequest).actionGet(); |
| 293 | + assertThat("bulk failures: " + Strings.toString(bulkResponse), bulkResponse.hasFailures(), is(false)); |
| 294 | + |
| 295 | + GetDataStreamsAction.Request getDataStreamRequest = new GetDataStreamsAction.Request("*"); |
| 296 | + GetDataStreamsAction.Response getDataStreamsResponse = client().admin().indices().getDataStreams(getDataStreamRequest).actionGet(); |
| 297 | + assertThat(getDataStreamsResponse.getDataStreams(), hasSize(0)); |
| 298 | + |
| 299 | + GetIndexResponse getIndexResponse = client().admin().indices().getIndex(new GetIndexRequest().indices("logs-foobar")).actionGet(); |
| 300 | + assertThat(getIndexResponse.getIndices(), arrayWithSize(1)); |
| 301 | + assertThat(getIndexResponse.getIndices(), hasItemInArray("logs-foobar")); |
| 302 | + assertThat(getIndexResponse.getSettings().get("logs-foobar").get(IndexMetadata.SETTING_NUMBER_OF_REPLICAS), equalTo("0")); |
| 303 | + } |
205 | 304 | } |
0 commit comments