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