2121import org .elasticsearch .action .support .IndicesOptions ;
2222import org .elasticsearch .cluster .metadata .ComposableIndexTemplate ;
2323import org .elasticsearch .cluster .metadata .IndexMetadata ;
24+ import org .elasticsearch .cluster .metadata .LifecycleExecutionState ;
2425import org .elasticsearch .cluster .metadata .Template ;
2526import org .elasticsearch .common .compress .CompressedXContent ;
27+ import org .elasticsearch .common .settings .Settings ;
28+ import org .elasticsearch .core .TimeValue ;
2629import org .elasticsearch .datastreams .DataStreamsPlugin ;
27- import org .elasticsearch .ingest .common .IngestCommonPlugin ;
2830import org .elasticsearch .plugins .Plugin ;
2931import org .elasticsearch .reindex .ReindexPlugin ;
3032import org .elasticsearch .test .ESIntegTestCase ;
3133import org .elasticsearch .test .transport .MockTransportService ;
3234import org .elasticsearch .xcontent .json .JsonXContent ;
35+ //import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin;
36+ import org .elasticsearch .xpack .core .ilm .LifecyclePolicy ;
37+ import org .elasticsearch .xpack .core .ilm .Phase ;
38+ import org .elasticsearch .xpack .core .ilm .StartILMRequest ;
39+ import org .elasticsearch .xpack .core .ilm .action .ILMActions ;
40+ import org .elasticsearch .xpack .core .ilm .action .PutLifecycleRequest ;
41+ //import org.elasticsearch.xpack.ilm.IndexLifecycle;
3342import org .elasticsearch .xpack .migrate .MigratePlugin ;
3443
3544import java .util .Collection ;
3645import java .util .List ;
3746import java .util .Locale ;
47+ import java .util .Map ;
3848import java .util .concurrent .TimeUnit ;
3949
4050import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertAcked ;
@@ -44,11 +54,11 @@ public class CopyIndexMetadataTransportActionIT extends ESIntegTestCase {
4454 @ Override
4555 protected Collection <Class <? extends Plugin >> nodePlugins () {
4656 return List .of (
57+ // LocalStateCompositeXPackPlugin.class,
4758 MigratePlugin .class ,
4859 ReindexPlugin .class ,
4960 MockTransportService .TestPlugin .class ,
50- DataStreamsPlugin .class ,
51- IngestCommonPlugin .class
61+ DataStreamsPlugin .class
5262 );
5363 }
5464
@@ -84,9 +94,105 @@ public void testCreationDate() throws Exception {
8494 assertEquals (sourceDate , destDate );
8595 }
8696
97+ public void testILMState () throws Exception {
98+
99+ Map <String , Phase > phases = Map .of (
100+ "warm" ,
101+ new Phase (
102+ "warm" ,
103+ TimeValue .ZERO ,
104+ Map .of ("rollover" , new org .elasticsearch .xpack .core .ilm .RolloverAction (null , null , null , 1L , null , null , null , null , null , null ))
105+ )
106+ );
107+
108+ var policyName = "my-policy" ;
109+ LifecyclePolicy policy = new LifecyclePolicy (policyName , phases );
110+ PutLifecycleRequest putLifecycleRequest = new PutLifecycleRequest (TEST_REQUEST_TIMEOUT , TEST_REQUEST_TIMEOUT , policy );
111+ assertAcked (client ().execute (ILMActions .PUT , putLifecycleRequest ).actionGet ());
112+
113+ var dataStream = createDataStream (policyName );
114+
115+ assertAcked (safeGet (client ().execute (ILMActions .START , new StartILMRequest (TEST_REQUEST_TIMEOUT , TEST_REQUEST_TIMEOUT ))));
116+
117+ // rollover a few times
118+ createDocument (dataStream );
119+ createDocument (dataStream );
120+ createDocument (dataStream );
121+
122+ var writeIndex = safeGet (
123+ indicesAdmin ().execute (
124+ GetDataStreamAction .INSTANCE ,
125+ new GetDataStreamAction .Request (TEST_REQUEST_TIMEOUT , new String [] { dataStream })
126+ )
127+ ).getDataStreams ().get (0 ).getDataStream ().getWriteIndex ().getName ();
128+
129+ var getIndexResponse = indicesAdmin ().getIndex (new GetIndexRequest (TEST_REQUEST_TIMEOUT ).indices (dataStream )).get ();
130+ for (var backingIndex : getIndexResponse .indices ()) {
131+
132+ var destIndex = randomAlphaOfLength (20 ).toLowerCase (Locale .ROOT );
133+ indicesAdmin ().create (new CreateIndexRequest (destIndex )).get ();
134+
135+ var metadataBefore = safeGet (
136+ clusterAdmin ().state (new ClusterStateRequest (TEST_REQUEST_TIMEOUT ).indices (backingIndex , destIndex ))
137+ ).getState ().metadata ();
138+ IndexMetadata source = metadataBefore .index (backingIndex );
139+ IndexMetadata destBefore = metadataBefore .index (destIndex );
140+
141+ assertNotEquals (
142+ source .getCustomData (LifecycleExecutionState .ILM_CUSTOM_METADATA_KEY ),
143+ destBefore .getCustomData (LifecycleExecutionState .ILM_CUSTOM_METADATA_KEY )
144+ );
145+
146+ // sanity check not equal before the copy
147+ if (backingIndex .equals (writeIndex )) {
148+ assertTrue (source .getRolloverInfos ().isEmpty ());
149+ assertTrue (destBefore .getRolloverInfos ().isEmpty ());
150+ } else {
151+ assertNotEquals (source .getRolloverInfos (), destBefore .getRolloverInfos ());
152+ }
153+
154+ // copy over the metadata
155+ copyMetadata (backingIndex , destIndex );
156+
157+ var metadataAfter = safeGet (clusterAdmin ().state (new ClusterStateRequest (TEST_REQUEST_TIMEOUT ).indices (destIndex )))
158+ .getState ()
159+ .metadata ();
160+ IndexMetadata destAfter = metadataAfter .index (destIndex );
161+
162+ // now rollover info should be equal
163+ assertEquals (source .getRolloverInfos (), destAfter .getRolloverInfos ());
164+
165+ Map <String , String > customData = source .getCustomData (LifecycleExecutionState .ILM_CUSTOM_METADATA_KEY );
166+ //
167+ assertEquals (
168+ source .getCustomData (LifecycleExecutionState .ILM_CUSTOM_METADATA_KEY ),
169+ destAfter .getCustomData (LifecycleExecutionState .ILM_CUSTOM_METADATA_KEY )
170+ );
171+
172+ }
173+ }
174+
175+
176+
177+
87178 public void testRolloverInfos () throws Exception {
88179
89- var dataStream = createDataStream ();
180+ var dataStream = createDataStream (null );
181+ Map <String , Phase > phases = Map .of (
182+ "warm" ,
183+ new Phase (
184+ "warm" ,
185+ TimeValue .ZERO ,
186+ Map .of ("rollover" , new org .elasticsearch .xpack .core .ilm .RolloverAction (null , null , null , 1L , null , null , null , null , null , null ))
187+ )
188+ );
189+
190+ var policyName = "my-policy" ;
191+ LifecyclePolicy policy = new LifecyclePolicy (policyName , phases );
192+ PutLifecycleRequest putLifecycleRequest = new PutLifecycleRequest (TEST_REQUEST_TIMEOUT , TEST_REQUEST_TIMEOUT , policy );
193+ assertAcked (client ().execute (ILMActions .PUT , putLifecycleRequest ).actionGet ());
194+
195+
90196
91197 // rollover a few times
92198 createDocument (dataStream );
@@ -115,6 +221,11 @@ public void testRolloverInfos() throws Exception {
115221 IndexMetadata source = metadataBefore .index (backingIndex );
116222 IndexMetadata destBefore = metadataBefore .index (destIndex );
117223
224+ // assertNotEquals(
225+ // source.getCustomData(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY),
226+ // destBefore.getCustomData(LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY)
227+ // );
228+
118229 // sanity check not equal before the copy
119230 if (backingIndex .equals (writeIndex )) {
120231 assertTrue (source .getRolloverInfos ().isEmpty ());
@@ -133,15 +244,37 @@ public void testRolloverInfos() throws Exception {
133244
134245 // now rollover info should be equal
135246 assertEquals (source .getRolloverInfos (), destAfter .getRolloverInfos ());
247+
248+ Map <String , String > customData = source .getCustomData (LifecycleExecutionState .ILM_CUSTOM_METADATA_KEY );
249+ //
250+ assertEquals (
251+ source .getCustomData (LifecycleExecutionState .ILM_CUSTOM_METADATA_KEY ),
252+ destAfter .getCustomData (LifecycleExecutionState .ILM_CUSTOM_METADATA_KEY )
253+ );
254+
136255 }
137256 }
138257
139- private String createDataStream () throws Exception {
258+ private String createDataStream (String ilmPolicy ) throws Exception {
140259 String dataStreamName = randomAlphaOfLength (10 ).toLowerCase (Locale .getDefault ());
141260
142- Template idxTemplate = new Template (null , new CompressedXContent ("""
143- {"properties":{"@timestamp":{"type":"date"},"data":{"type":"keyword"}}}
144- """ ), null );
261+ Settings settings = ilmPolicy != null ?
262+ Settings .builder ().put (IndexMetadata .LIFECYCLE_NAME , ilmPolicy ).build ()
263+ : null ;
264+
265+ String mapping = """
266+ {
267+ "properties": {
268+ "@timestamp": {
269+ "type":"date"
270+ },
271+ "data":{
272+ "type":"keyword"
273+ }
274+ }
275+ }
276+ """ ;
277+ Template idxTemplate = new Template (settings , new CompressedXContent (mapping ), null );
145278
146279 ComposableIndexTemplate template = ComposableIndexTemplate .builder ()
147280 .indexPatterns (List .of (dataStreamName + "*" ))
0 commit comments