1010import org .apache .http .entity .StringEntity ;
1111import org .elasticsearch .client .Request ;
1212import org .elasticsearch .client .Response ;
13+ import org .elasticsearch .client .RestClient ;
1314import org .elasticsearch .client .ResponseException ;
1415import org .elasticsearch .cluster .metadata .IndexMetaData ;
1516import org .elasticsearch .common .Strings ;
1819import org .elasticsearch .common .xcontent .XContentBuilder ;
1920import org .elasticsearch .common .xcontent .XContentHelper ;
2021import org .elasticsearch .common .xcontent .XContentType ;
21- import org .elasticsearch .common .xcontent .json .JsonXContent ;
2222import org .elasticsearch .test .rest .ESRestTestCase ;
2323import org .elasticsearch .xpack .core .indexlifecycle .AllocateAction ;
2424import org .elasticsearch .xpack .core .indexlifecycle .DeleteAction ;
2525import org .elasticsearch .xpack .core .indexlifecycle .ForceMergeAction ;
2626import org .elasticsearch .xpack .core .indexlifecycle .LifecycleAction ;
2727import org .elasticsearch .xpack .core .indexlifecycle .LifecyclePolicy ;
28- import org .elasticsearch .xpack .core .indexlifecycle .LifecycleSettings ;
2928import org .elasticsearch .xpack .core .indexlifecycle .Phase ;
3029import org .elasticsearch .xpack .core .indexlifecycle .ReadOnlyAction ;
30+ import org .elasticsearch .xpack .core .indexlifecycle .RolloverAction ;
3131import org .elasticsearch .xpack .core .indexlifecycle .ShrinkAction ;
3232import org .elasticsearch .xpack .core .indexlifecycle .Step .StepKey ;
3333import org .elasticsearch .xpack .core .indexlifecycle .TerminalPolicyStep ;
3636import java .io .IOException ;
3737import java .io .InputStream ;
3838import java .util .Collections ;
39+ import java .util .HashMap ;
3940import java .util .List ;
4041import java .util .Locale ;
4142import java .util .Map ;
@@ -52,25 +53,70 @@ public class TimeSeriesLifecycleActionsIT extends ESRestTestCase {
5253 private String policy ;
5354
5455 @ Before
55- public void refreshIndex () throws IOException {
56+ public void refreshIndex () {
5657 index = randomAlphaOfLength (10 ).toLowerCase (Locale .ROOT );
5758 policy = randomAlphaOfLength (5 );
58- Request request = new Request ("PUT" , "/_cluster/settings" );
59- XContentBuilder pollIntervalEntity = JsonXContent .contentBuilder ();
60- pollIntervalEntity .startObject ();
61- {
62- pollIntervalEntity .startObject ("transient" );
63- {
64- pollIntervalEntity .field (LifecycleSettings .LIFECYCLE_POLL_INTERVAL , "1s" );
65- }pollIntervalEntity .endObject ();
66- } pollIntervalEntity .endObject ();
67- request .setJsonEntity (Strings .toString (pollIntervalEntity ));
68- assertOK (adminClient ().performRequest (request ));
6959 }
7060
7161 public static void updatePolicy (String indexName , String policy ) throws IOException {
7262 Request request = new Request ("PUT" , "/" + indexName + "/_ilm/" + policy );
73- client ().performRequest (request );
63+ assertOK (client ().performRequest (request ));
64+ }
65+
66+ public void testFullPolicy () throws Exception {
67+ String originalIndex = index + "-000001" ;
68+ String shrunkenOriginalIndex = ShrinkAction .SHRUNKEN_INDEX_PREFIX + originalIndex ;
69+ String secondIndex = index + "-000002" ;
70+ createIndexWithSettings (originalIndex , Settings .builder ().put (IndexMetaData .SETTING_NUMBER_OF_SHARDS , 4 )
71+ .put (IndexMetaData .SETTING_NUMBER_OF_REPLICAS , 0 )
72+ .put ("index.routing.allocation.include._name" , "node-0" )
73+ .put (RolloverAction .LIFECYCLE_ROLLOVER_ALIAS , "alias" ));
74+
75+ // create policy
76+ Map <String , LifecycleAction > warmActions = new HashMap <>();
77+ warmActions .put (ForceMergeAction .NAME , new ForceMergeAction (1 ));
78+ warmActions .put (AllocateAction .NAME , new AllocateAction (1 , singletonMap ("_name" , "node-1,node-2" ), null , null ));
79+ warmActions .put (ShrinkAction .NAME , new ShrinkAction (1 ));
80+ Map <String , Phase > phases = new HashMap <>();
81+ phases .put ("hot" , new Phase ("hot" , TimeValue .ZERO , singletonMap (RolloverAction .NAME ,
82+ new RolloverAction (null , null , 1L ))));
83+ phases .put ("warm" , new Phase ("warm" , TimeValue .ZERO , warmActions ));
84+ phases .put ("cold" , new Phase ("cold" , TimeValue .ZERO , singletonMap (AllocateAction .NAME ,
85+ new AllocateAction (0 , singletonMap ("_name" , "node-3" ), null , null ))));
86+ phases .put ("delete" , new Phase ("delete" , TimeValue .ZERO , singletonMap (DeleteAction .NAME , new DeleteAction ())));
87+ LifecyclePolicy lifecyclePolicy = new LifecyclePolicy (policy , phases );
88+ // PUT policy
89+ XContentBuilder builder = jsonBuilder ();
90+ lifecyclePolicy .toXContent (builder , null );
91+ final StringEntity entity = new StringEntity (
92+ "{ \" policy\" :" + Strings .toString (builder ) + "}" , ContentType .APPLICATION_JSON );
93+ Request request = new Request ("PUT" , "_ilm/" + policy );
94+ request .setEntity (entity );
95+ assertOK (client ().performRequest (request ));
96+ // update policy on index
97+ updatePolicy (originalIndex , policy );
98+ // index document {"foo": "bar"} to trigger rollover
99+ index (client (), originalIndex , "_id" , "foo" , "bar" );
100+ assertBusy (() -> assertTrue (indexExists (secondIndex )));
101+ assertBusy (() -> assertFalse (indexExists (shrunkenOriginalIndex )));
102+ assertBusy (() -> assertFalse (indexExists (originalIndex )));
103+ }
104+
105+ public void testRolloverAction () throws Exception {
106+ String originalIndex = index + "-000001" ;
107+ String secondIndex = index + "-000002" ;
108+ createIndexWithSettings (originalIndex , Settings .builder ().put (IndexMetaData .SETTING_NUMBER_OF_SHARDS , 1 )
109+ .put (IndexMetaData .SETTING_NUMBER_OF_REPLICAS , 0 )
110+ .put (RolloverAction .LIFECYCLE_ROLLOVER_ALIAS , "alias" ));
111+
112+ // create policy
113+ createNewSingletonPolicy ("hot" , new RolloverAction (null , null , 1L ));
114+ // update policy on index
115+ updatePolicy (originalIndex , policy );
116+ // index document {"foo": "bar"} to trigger rollover
117+ index (client (), originalIndex , "_id" , "foo" , "bar" );
118+ assertBusy (() -> assertTrue (indexExists (secondIndex )));
119+ assertBusy (() -> assertTrue (indexExists (originalIndex )));
74120 }
75121
76122 public void testAllocateOnlyAllocation () throws Exception {
@@ -210,12 +256,26 @@ private void createNewSingletonPolicy(String phaseName, LifecycleAction action,
210256
211257 private void createIndexWithSettings (String index , Settings .Builder settings ) throws IOException {
212258 // create the test-index index
213- createIndex (index , settings .build ());
259+ Request request = new Request ("PUT" , "/" + index );
260+ request .setJsonEntity ("{\n \" settings\" : " + Strings .toString (settings .build ())
261+ + ", \" aliases\" : { \" alias\" : { \" is_write_index\" : true } } }" );
262+ client ().performRequest (request );
214263 // wait for the shards to initialize
215264 ensureGreen (index );
216265
217266 }
218267
268+ private static void index (RestClient client , String index , String id , Object ... fields ) throws IOException {
269+ XContentBuilder document = jsonBuilder ().startObject ();
270+ for (int i = 0 ; i < fields .length ; i += 2 ) {
271+ document .field ((String ) fields [i ], fields [i + 1 ]);
272+ }
273+ document .endObject ();
274+ final Request request = new Request ("POST" , "/" + index + "/_doc/" + id );
275+ request .setJsonEntity (Strings .toString (document ));
276+ assertOK (client .performRequest (request ));
277+ }
278+
219279 @ SuppressWarnings ("unchecked" )
220280 private Map <String , Object > getOnlyIndexSettings (String index ) throws IOException {
221281 Map <String , Object > response = (Map <String , Object >) getIndexSettings (index ).get (index );
0 commit comments