@@ -60,7 +60,12 @@ protected Collection<Class<? extends Plugin>> nodePlugins() {
6060
6161
6262 public void testRolloverOnEmptyIndex () throws Exception {
63- assertAcked (prepareCreate ("test_index-1" ).addAlias (new Alias ("test_alias" )).get ());
63+ Alias testAlias = new Alias ("test_alias" );
64+ boolean explicitWriteIndex = randomBoolean ();
65+ if (explicitWriteIndex ) {
66+ testAlias .writeIndex (true );
67+ }
68+ assertAcked (prepareCreate ("test_index-1" ).addAlias (testAlias ).get ());
6469 final RolloverResponse response = client ().admin ().indices ().prepareRolloverIndex ("test_alias" ).get ();
6570 assertThat (response .getOldIndex (), equalTo ("test_index-1" ));
6671 assertThat (response .getNewIndex (), equalTo ("test_index-000002" ));
@@ -69,7 +74,12 @@ public void testRolloverOnEmptyIndex() throws Exception {
6974 assertThat (response .getConditionStatus ().size (), equalTo (0 ));
7075 final ClusterState state = client ().admin ().cluster ().prepareState ().get ().getState ();
7176 final IndexMetaData oldIndex = state .metaData ().index ("test_index-1" );
72- assertFalse (oldIndex .getAliases ().containsKey ("test_alias" ));
77+ if (explicitWriteIndex ) {
78+ assertTrue (oldIndex .getAliases ().containsKey ("test_alias" ));
79+ assertFalse (oldIndex .getAliases ().get ("test_alias" ).writeIndex ());
80+ } else {
81+ assertFalse (oldIndex .getAliases ().containsKey ("test_alias" ));
82+ }
7383 final IndexMetaData newIndex = state .metaData ().index ("test_index-000002" );
7484 assertTrue (newIndex .getAliases ().containsKey ("test_alias" ));
7585 }
@@ -97,8 +107,49 @@ public void testRollover() throws Exception {
97107 is (both (greaterThanOrEqualTo (beforeTime )).and (lessThanOrEqualTo (client ().threadPool ().absoluteTimeInMillis () + 1000L ))));
98108 }
99109
110+ public void testRolloverWithExplicitWriteIndex () throws Exception {
111+ long beforeTime = client ().threadPool ().absoluteTimeInMillis () - 1000L ;
112+ assertAcked (prepareCreate ("test_index-2" ).addAlias (new Alias ("test_alias" ).writeIndex (true )).get ());
113+ index ("test_index-2" , "type1" , "1" , "field" , "value" );
114+ flush ("test_index-2" );
115+ final RolloverResponse response = client ().admin ().indices ().prepareRolloverIndex ("test_alias" ).get ();
116+ assertThat (response .getOldIndex (), equalTo ("test_index-2" ));
117+ assertThat (response .getNewIndex (), equalTo ("test_index-000003" ));
118+ assertThat (response .isDryRun (), equalTo (false ));
119+ assertThat (response .isRolledOver (), equalTo (true ));
120+ assertThat (response .getConditionStatus ().size (), equalTo (0 ));
121+ final ClusterState state = client ().admin ().cluster ().prepareState ().get ().getState ();
122+ final IndexMetaData oldIndex = state .metaData ().index ("test_index-2" );
123+ assertTrue (oldIndex .getAliases ().containsKey ("test_alias" ));
124+ assertFalse (oldIndex .getAliases ().get ("test_alias" ).writeIndex ());
125+ final IndexMetaData newIndex = state .metaData ().index ("test_index-000003" );
126+ assertTrue (newIndex .getAliases ().containsKey ("test_alias" ));
127+ assertTrue (newIndex .getAliases ().get ("test_alias" ).writeIndex ());
128+ assertThat (oldIndex .getRolloverInfos ().size (), equalTo (1 ));
129+ assertThat (oldIndex .getRolloverInfos ().get ("test_alias" ).getAlias (), equalTo ("test_alias" ));
130+ assertThat (oldIndex .getRolloverInfos ().get ("test_alias" ).getMetConditions (), is (empty ()));
131+ assertThat (oldIndex .getRolloverInfos ().get ("test_alias" ).getTime (),
132+ is (both (greaterThanOrEqualTo (beforeTime )).and (lessThanOrEqualTo (client ().threadPool ().absoluteTimeInMillis () + 1000L ))));
133+ }
134+
135+ public void testRolloverWithNoWriteIndex () {
136+ Boolean firstIsWriteIndex = randomFrom (false , null );
137+ assertAcked (prepareCreate ("index1" ).addAlias (new Alias ("alias" ).writeIndex (firstIsWriteIndex )).get ());
138+ if (firstIsWriteIndex == null ) {
139+ assertAcked (prepareCreate ("index2" ).addAlias (new Alias ("alias" ).writeIndex (randomFrom (false , null ))).get ());
140+ }
141+ IllegalArgumentException exception = expectThrows (IllegalArgumentException .class ,
142+ () -> client ().admin ().indices ().prepareRolloverIndex ("alias" ).dryRun (randomBoolean ()).get ());
143+ assertThat (exception .getMessage (), equalTo ("source alias [alias] does not point to a write index" ));
144+ }
145+
100146 public void testRolloverWithIndexSettings () throws Exception {
101- assertAcked (prepareCreate ("test_index-2" ).addAlias (new Alias ("test_alias" )).get ());
147+ Alias testAlias = new Alias ("test_alias" );
148+ boolean explicitWriteIndex = randomBoolean ();
149+ if (explicitWriteIndex ) {
150+ testAlias .writeIndex (true );
151+ }
152+ assertAcked (prepareCreate ("test_index-2" ).addAlias (testAlias ).get ());
102153 index ("test_index-2" , "type1" , "1" , "field" , "value" );
103154 flush ("test_index-2" );
104155 final Settings settings = Settings .builder ()
@@ -114,12 +165,17 @@ public void testRolloverWithIndexSettings() throws Exception {
114165 assertThat (response .getConditionStatus ().size (), equalTo (0 ));
115166 final ClusterState state = client ().admin ().cluster ().prepareState ().get ().getState ();
116167 final IndexMetaData oldIndex = state .metaData ().index ("test_index-2" );
117- assertFalse (oldIndex .getAliases ().containsKey ("test_alias" ));
118168 final IndexMetaData newIndex = state .metaData ().index ("test_index-000003" );
119169 assertThat (newIndex .getNumberOfShards (), equalTo (1 ));
120170 assertThat (newIndex .getNumberOfReplicas (), equalTo (0 ));
121171 assertTrue (newIndex .getAliases ().containsKey ("test_alias" ));
122172 assertTrue (newIndex .getAliases ().containsKey ("extra_alias" ));
173+ if (explicitWriteIndex ) {
174+ assertFalse (oldIndex .getAliases ().get ("test_alias" ).writeIndex ());
175+ assertTrue (newIndex .getAliases ().get ("test_alias" ).writeIndex ());
176+ } else {
177+ assertFalse (oldIndex .getAliases ().containsKey ("test_alias" ));
178+ }
123179 }
124180
125181 public void testRolloverDryRun () throws Exception {
@@ -140,7 +196,12 @@ public void testRolloverDryRun() throws Exception {
140196 }
141197
142198 public void testRolloverConditionsNotMet () throws Exception {
143- assertAcked (prepareCreate ("test_index-0" ).addAlias (new Alias ("test_alias" )).get ());
199+ boolean explicitWriteIndex = randomBoolean ();
200+ Alias testAlias = new Alias ("test_alias" );
201+ if (explicitWriteIndex ) {
202+ testAlias .writeIndex (true );
203+ }
204+ assertAcked (prepareCreate ("test_index-0" ).addAlias (testAlias ).get ());
144205 index ("test_index-0" , "type1" , "1" , "field" , "value" );
145206 flush ("test_index-0" );
146207 final RolloverResponse response = client ().admin ().indices ().prepareRolloverIndex ("test_alias" )
@@ -160,12 +221,22 @@ public void testRolloverConditionsNotMet() throws Exception {
160221 final ClusterState state = client ().admin ().cluster ().prepareState ().get ().getState ();
161222 final IndexMetaData oldIndex = state .metaData ().index ("test_index-0" );
162223 assertTrue (oldIndex .getAliases ().containsKey ("test_alias" ));
224+ if (explicitWriteIndex ) {
225+ assertTrue (oldIndex .getAliases ().get ("test_alias" ).writeIndex ());
226+ } else {
227+ assertNull (oldIndex .getAliases ().get ("test_alias" ).writeIndex ());
228+ }
163229 final IndexMetaData newIndex = state .metaData ().index ("test_index-000001" );
164230 assertNull (newIndex );
165231 }
166232
167233 public void testRolloverWithNewIndexName () throws Exception {
168- assertAcked (prepareCreate ("test_index" ).addAlias (new Alias ("test_alias" )).get ());
234+ Alias testAlias = new Alias ("test_alias" );
235+ boolean explicitWriteIndex = randomBoolean ();
236+ if (explicitWriteIndex ) {
237+ testAlias .writeIndex (true );
238+ }
239+ assertAcked (prepareCreate ("test_index" ).addAlias (testAlias ).get ());
169240 index ("test_index" , "type1" , "1" , "field" , "value" );
170241 flush ("test_index" );
171242 final RolloverResponse response = client ().admin ().indices ().prepareRolloverIndex ("test_alias" )
@@ -177,9 +248,14 @@ public void testRolloverWithNewIndexName() throws Exception {
177248 assertThat (response .getConditionStatus ().size (), equalTo (0 ));
178249 final ClusterState state = client ().admin ().cluster ().prepareState ().get ().getState ();
179250 final IndexMetaData oldIndex = state .metaData ().index ("test_index" );
180- assertFalse (oldIndex .getAliases ().containsKey ("test_alias" ));
181251 final IndexMetaData newIndex = state .metaData ().index ("test_new_index" );
182252 assertTrue (newIndex .getAliases ().containsKey ("test_alias" ));
253+ if (explicitWriteIndex ) {
254+ assertFalse (oldIndex .getAliases ().get ("test_alias" ).writeIndex ());
255+ assertTrue (newIndex .getAliases ().get ("test_alias" ).writeIndex ());
256+ } else {
257+ assertFalse (oldIndex .getAliases ().containsKey ("test_alias" ));
258+ }
183259 }
184260
185261 public void testRolloverOnExistingIndex () throws Exception {
0 commit comments