|
24 | 24 | import org.elasticsearch.action.bulk.BulkRequest; |
25 | 25 | import org.elasticsearch.action.bulk.BulkResponse; |
26 | 26 | import org.elasticsearch.action.index.IndexRequest; |
| 27 | +import org.elasticsearch.action.ingest.DeletePipelineRequest; |
| 28 | +import org.elasticsearch.action.ingest.DeletePipelineTransportAction; |
| 29 | +import org.elasticsearch.action.ingest.PutPipelineRequest; |
| 30 | +import org.elasticsearch.action.ingest.PutPipelineTransportAction; |
27 | 31 | import org.elasticsearch.cluster.block.ClusterBlockException; |
28 | 32 | import org.elasticsearch.cluster.metadata.ComposableIndexTemplate; |
29 | 33 | import org.elasticsearch.cluster.metadata.IndexMetadata; |
30 | 34 | import org.elasticsearch.cluster.metadata.MappingMetadata; |
31 | 35 | import org.elasticsearch.cluster.metadata.MetadataIndexStateService; |
32 | 36 | import org.elasticsearch.cluster.metadata.Template; |
| 37 | +import org.elasticsearch.common.bytes.BytesArray; |
33 | 38 | import org.elasticsearch.common.compress.CompressedXContent; |
34 | 39 | import org.elasticsearch.common.settings.Settings; |
35 | 40 | import org.elasticsearch.common.time.DateFormatter; |
|
38 | 43 | import org.elasticsearch.datastreams.DataStreamsPlugin; |
39 | 44 | import org.elasticsearch.index.IndexSettings; |
40 | 45 | import org.elasticsearch.index.mapper.DateFieldMapper; |
| 46 | +import org.elasticsearch.ingest.common.IngestCommonPlugin; |
41 | 47 | import org.elasticsearch.plugins.Plugin; |
42 | 48 | import org.elasticsearch.reindex.ReindexPlugin; |
43 | 49 | import org.elasticsearch.test.ESIntegTestCase; |
44 | 50 | import org.elasticsearch.test.transport.MockTransportService; |
45 | 51 | import org.elasticsearch.xcontent.XContentType; |
46 | 52 | import org.elasticsearch.xpack.migrate.MigratePlugin; |
| 53 | +import org.elasticsearch.xpack.migrate.MigrateTemplateRegistry; |
| 54 | +import org.junit.After; |
47 | 55 |
|
48 | 56 | import java.io.IOException; |
49 | 57 | import java.time.Instant; |
|
56 | 64 | import static org.elasticsearch.cluster.metadata.MetadataIndexTemplateService.DEFAULT_TIMESTAMP_FIELD; |
57 | 65 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
58 | 66 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; |
| 67 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponse; |
59 | 68 | import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; |
60 | 69 | import static org.hamcrest.Matchers.equalTo; |
61 | 70 |
|
62 | 71 | public class ReindexDatastreamIndexTransportActionIT extends ESIntegTestCase { |
| 72 | + @After |
| 73 | + private void cleanupCluster() throws Exception { |
| 74 | + clusterAdmin().execute( |
| 75 | + DeletePipelineTransportAction.TYPE, |
| 76 | + new DeletePipelineRequest(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT, MigrateTemplateRegistry.REINDEX_DATA_STREAM_PIPELINE_NAME) |
| 77 | + ); |
| 78 | + super.cleanUpCluster(); |
| 79 | + } |
63 | 80 |
|
64 | 81 | private static final String MAPPING = """ |
65 | 82 | { |
66 | 83 | "_doc":{ |
67 | 84 | "dynamic":"strict", |
68 | 85 | "properties":{ |
69 | | - "foo1":{ |
70 | | - "type":"text" |
71 | | - } |
| 86 | + "foo1": {"type":"text"}, |
| 87 | + "@timestamp": {"type":"date"} |
72 | 88 | } |
73 | 89 | } |
74 | 90 | } |
75 | 91 | """; |
76 | 92 |
|
77 | 93 | @Override |
78 | 94 | protected Collection<Class<? extends Plugin>> nodePlugins() { |
79 | | - return List.of(MigratePlugin.class, ReindexPlugin.class, MockTransportService.TestPlugin.class, DataStreamsPlugin.class); |
| 95 | + return List.of( |
| 96 | + MigratePlugin.class, |
| 97 | + ReindexPlugin.class, |
| 98 | + MockTransportService.TestPlugin.class, |
| 99 | + DataStreamsPlugin.class, |
| 100 | + IngestCommonPlugin.class |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + private static String DATA_STREAM_MAPPING = """ |
| 105 | + { |
| 106 | + "dynamic": true, |
| 107 | + "_data_stream_timestamp": { |
| 108 | + "enabled": true |
| 109 | + }, |
| 110 | + "properties": { |
| 111 | + "@timestamp": {"type":"date"} |
| 112 | + } |
| 113 | + } |
| 114 | + """; |
| 115 | + |
| 116 | + public void testTimestamp0AddedIfMissing() { |
| 117 | + var sourceIndex = randomAlphaOfLength(20).toLowerCase(Locale.ROOT); |
| 118 | + indicesAdmin().create(new CreateIndexRequest(sourceIndex)).actionGet(); |
| 119 | + |
| 120 | + // add doc without timestamp |
| 121 | + addDoc(sourceIndex, "{\"foo\":\"baz\"}"); |
| 122 | + |
| 123 | + // add timestamp to source mapping |
| 124 | + indicesAdmin().preparePutMapping(sourceIndex).setSource(DATA_STREAM_MAPPING, XContentType.JSON).get(); |
| 125 | + |
| 126 | + // call reindex |
| 127 | + var destIndex = client().execute(ReindexDataStreamIndexAction.INSTANCE, new ReindexDataStreamIndexAction.Request(sourceIndex)) |
| 128 | + .actionGet() |
| 129 | + .getDestIndex(); |
| 130 | + |
| 131 | + assertResponse(prepareSearch(destIndex), response -> { |
| 132 | + Map<String, Object> sourceAsMap = response.getHits().getAt(0).getSourceAsMap(); |
| 133 | + assertEquals(Integer.valueOf(0), sourceAsMap.get(DEFAULT_TIMESTAMP_FIELD)); |
| 134 | + }); |
| 135 | + } |
| 136 | + |
| 137 | + public void testTimestampNotAddedIfExists() { |
| 138 | + var sourceIndex = randomAlphaOfLength(20).toLowerCase(Locale.ROOT); |
| 139 | + indicesAdmin().create(new CreateIndexRequest(sourceIndex)).actionGet(); |
| 140 | + |
| 141 | + // add doc with timestamp |
| 142 | + String time = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.formatMillis(System.currentTimeMillis()); |
| 143 | + var doc = String.format(Locale.ROOT, "{\"%s\":\"%s\"}", DEFAULT_TIMESTAMP_FIELD, time); |
| 144 | + addDoc(sourceIndex, doc); |
| 145 | + |
| 146 | + // add timestamp to source mapping |
| 147 | + indicesAdmin().preparePutMapping(sourceIndex).setSource(DATA_STREAM_MAPPING, XContentType.JSON).get(); |
| 148 | + |
| 149 | + // call reindex |
| 150 | + var destIndex = client().execute(ReindexDataStreamIndexAction.INSTANCE, new ReindexDataStreamIndexAction.Request(sourceIndex)) |
| 151 | + .actionGet() |
| 152 | + .getDestIndex(); |
| 153 | + |
| 154 | + assertResponse(prepareSearch(destIndex), response -> { |
| 155 | + Map<String, Object> sourceAsMap = response.getHits().getAt(0).getSourceAsMap(); |
| 156 | + assertEquals(time, sourceAsMap.get(DEFAULT_TIMESTAMP_FIELD)); |
| 157 | + }); |
| 158 | + } |
| 159 | + |
| 160 | + public void testCustomReindexPipeline() { |
| 161 | + String customPipeline = """ |
| 162 | + { |
| 163 | + "processors": [ |
| 164 | + { |
| 165 | + "set": { |
| 166 | + "field": "cheese", |
| 167 | + "value": "gorgonzola" |
| 168 | + } |
| 169 | + } |
| 170 | + ], |
| 171 | + "version": 1000 |
| 172 | + } |
| 173 | + """; |
| 174 | + |
| 175 | + PutPipelineRequest putRequest = new PutPipelineRequest( |
| 176 | + TEST_REQUEST_TIMEOUT, |
| 177 | + TEST_REQUEST_TIMEOUT, |
| 178 | + MigrateTemplateRegistry.REINDEX_DATA_STREAM_PIPELINE_NAME, |
| 179 | + new BytesArray(customPipeline), |
| 180 | + XContentType.JSON |
| 181 | + ); |
| 182 | + |
| 183 | + clusterAdmin().execute(PutPipelineTransportAction.TYPE, putRequest).actionGet(); |
| 184 | + |
| 185 | + var sourceIndex = randomAlphaOfLength(20).toLowerCase(Locale.ROOT); |
| 186 | + indicesAdmin().create(new CreateIndexRequest(sourceIndex)).actionGet(); |
| 187 | + |
| 188 | + // add doc with timestamp |
| 189 | + String time = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.formatMillis(System.currentTimeMillis()); |
| 190 | + var doc = String.format(Locale.ROOT, "{\"%s\":\"%s\"}", DEFAULT_TIMESTAMP_FIELD, time); |
| 191 | + addDoc(sourceIndex, doc); |
| 192 | + |
| 193 | + // add timestamp to source mapping |
| 194 | + indicesAdmin().preparePutMapping(sourceIndex).setSource(DATA_STREAM_MAPPING, XContentType.JSON).get(); |
| 195 | + |
| 196 | + String destIndex = client().execute(ReindexDataStreamIndexAction.INSTANCE, new ReindexDataStreamIndexAction.Request(sourceIndex)) |
| 197 | + .actionGet() |
| 198 | + .getDestIndex(); |
| 199 | + |
| 200 | + assertResponse(prepareSearch(destIndex), response -> { |
| 201 | + Map<String, Object> sourceAsMap = response.getHits().getAt(0).getSourceAsMap(); |
| 202 | + assertEquals("gorgonzola", sourceAsMap.get("cheese")); |
| 203 | + assertEquals(time, sourceAsMap.get(DEFAULT_TIMESTAMP_FIELD)); |
| 204 | + }); |
80 | 205 | } |
81 | 206 |
|
82 | 207 | public void testDestIndexDeletedIfExists() throws Exception { |
@@ -200,7 +325,7 @@ public void testSettingsAddedBeforeReindex() throws Exception { |
200 | 325 | assertEquals(refreshInterval, settingsResponse.getSetting(destIndex, IndexSettings.INDEX_REFRESH_INTERVAL_SETTING.getKey())); |
201 | 326 | } |
202 | 327 |
|
203 | | - public void testMappingsAddedToDestIndex() throws Exception { |
| 328 | + public void testMappingsAddedToDestIndex() { |
204 | 329 | var sourceIndex = randomAlphaOfLength(20).toLowerCase(Locale.ROOT); |
205 | 330 | indicesAdmin().create(new CreateIndexRequest(sourceIndex).mapping(MAPPING)).actionGet(); |
206 | 331 |
|
@@ -479,12 +604,9 @@ private static String formatInstant(Instant instant) { |
479 | 604 | return DateFormatter.forPattern(FormatNames.STRICT_DATE_OPTIONAL_TIME.getName()).format(instant); |
480 | 605 | } |
481 | 606 |
|
482 | | - private static String getIndexUUID(String index) { |
483 | | - return indicesAdmin().getIndex(new GetIndexRequest(TEST_REQUEST_TIMEOUT).indices(index)) |
484 | | - .actionGet() |
485 | | - .getSettings() |
486 | | - .get(index) |
487 | | - .get(IndexMetadata.SETTING_INDEX_UUID); |
| 607 | + void addDoc(String index, String doc) { |
| 608 | + BulkRequest bulkRequest = new BulkRequest(); |
| 609 | + bulkRequest.add(new IndexRequest(index).opType(DocWriteRequest.OpType.CREATE).source(doc, XContentType.JSON)); |
| 610 | + client().bulk(bulkRequest).actionGet(); |
488 | 611 | } |
489 | | - |
490 | 612 | } |
0 commit comments