99
1010package org .elasticsearch .lucene ;
1111
12- import org .elasticsearch .client .ResponseException ;
1312import org .elasticsearch .cluster .metadata .IndexMetadata ;
1413import org .elasticsearch .common .settings .Settings ;
1514import org .elasticsearch .index .IndexSettings ;
15+ import org .elasticsearch .index .translog .Translog ;
1616import org .elasticsearch .repositories .fs .FsRepository ;
1717import org .elasticsearch .test .cluster .util .Version ;
1818
2222import static org .elasticsearch .cluster .metadata .MetadataIndexStateService .VERIFIED_BEFORE_CLOSE_SETTING ;
2323import static org .elasticsearch .cluster .metadata .MetadataIndexStateService .VERIFIED_READ_ONLY_SETTING ;
2424import static org .hamcrest .Matchers .contains ;
25- import static org .hamcrest .Matchers .containsString ;
2625import static org .hamcrest .Matchers .either ;
2726import static org .hamcrest .Matchers .equalTo ;
2827import static org .hamcrest .Matchers .is ;
28+ import static org .hamcrest .Matchers .not ;
2929
3030public class FullClusterRestartLuceneIndexCompatibilityIT extends FullClusterRestartIndexCompatibilityTestCase {
3131
@@ -51,7 +51,6 @@ public void testIndexUpgrade() throws Exception {
5151 Settings .builder ()
5252 .put (IndexMetadata .SETTING_NUMBER_OF_SHARDS , 1 )
5353 .put (IndexMetadata .SETTING_NUMBER_OF_REPLICAS , randomInt (2 ))
54- .put (IndexSettings .INDEX_SOFT_DELETES_SETTING .getKey (), true )
5554 .build ()
5655 );
5756 indexDocs (index , numDocs );
@@ -111,12 +110,10 @@ public void testIndexUpgrade() throws Exception {
111110 .putNull (IndexMetadata .APIBlock .WRITE .settingName ())
112111 .putNull (IndexMetadata .APIBlock .READ_ONLY .settingName ())
113112 );
114- logger .debug ("--> but attempts to re-opening [{}] should fail due to the missing block" , index );
115- var ex = expectThrows (ResponseException .class , () -> openIndex (index ));
116- assertThat (ex .getMessage (), containsString ("must be marked as read-only" ));
117113
118- // TODO this could be randomized once we support recovering verified-before-close closed indices with no write/ro block
119- addIndexBlock (index , IndexMetadata .APIBlock .WRITE );
114+ assertThat (indexBlocks (index ), contains (INDEX_CLOSED_BLOCK ));
115+ assertIndexSetting (index , VERIFIED_BEFORE_CLOSE_SETTING , is (true ));
116+ assertIndexSetting (index , VERIFIED_READ_ONLY_SETTING , is (true ));
120117 }
121118
122119 var block = indexBlocks (index ).stream ().filter (c -> c .equals (INDEX_WRITE_BLOCK ) || c .equals (INDEX_READ_ONLY_BLOCK )).findFirst ();
@@ -128,11 +125,11 @@ public void testIndexUpgrade() throws Exception {
128125 .putNull (IndexMetadata .APIBlock .READ_ONLY .settingName ())
129126 .put (IndexMetadata .APIBlock .WRITE .settingName (), true )
130127 );
131- }
132128
133- assertThat (indexBlocks (index ), isClosed ? contains (INDEX_CLOSED_BLOCK , INDEX_WRITE_BLOCK ) : contains (INDEX_WRITE_BLOCK ));
134- assertIndexSetting (index , VERIFIED_BEFORE_CLOSE_SETTING , is (isClosed ));
135- assertIndexSetting (index , VERIFIED_READ_ONLY_SETTING , is (true ));
129+ assertThat (indexBlocks (index ), isClosed ? contains (INDEX_CLOSED_BLOCK , INDEX_WRITE_BLOCK ) : contains (INDEX_WRITE_BLOCK ));
130+ assertIndexSetting (index , VERIFIED_BEFORE_CLOSE_SETTING , is (isClosed ));
131+ assertIndexSetting (index , VERIFIED_READ_ONLY_SETTING , is (true ));
132+ }
136133
137134 var numberOfReplicas = getNumberOfReplicas (index );
138135 if (0 < numberOfReplicas ) {
@@ -173,6 +170,71 @@ public void testIndexUpgrade() throws Exception {
173170 }
174171 }
175172
173+ /**
174+ * Creates an index on N-2, closes it on N-1 (without marking it as read-only), then upgrades to N.
175+ */
176+ public void testClosedIndexUpgrade () throws Exception {
177+ final String index = suffix ("index" );
178+ final int numDocs = 2437 ;
179+
180+ if (isFullyUpgradedTo (VERSION_MINUS_2 )) {
181+ createIndex (
182+ client (),
183+ index ,
184+ Settings .builder ()
185+ .put (IndexMetadata .SETTING_NUMBER_OF_SHARDS , 1 )
186+ .put (IndexMetadata .SETTING_NUMBER_OF_REPLICAS , randomInt (2 ))
187+ .put (IndexSettings .INDEX_TRANSLOG_DURABILITY_SETTING .getKey (), randomFrom (Translog .Durability .values ()))
188+ .build ()
189+ );
190+ indexDocs (index , numDocs );
191+ return ;
192+ }
193+
194+ assertThat (indexVersion (index ), equalTo (VERSION_MINUS_2 ));
195+ ensureGreen (index );
196+
197+ if (isIndexClosed (index ) == false ) {
198+ assertDocCount (client (), index , numDocs );
199+ }
200+
201+ if (isFullyUpgradedTo (VERSION_MINUS_1 )) {
202+ logger .debug ("--> [{}] closing index before upgrade without adding a read_only/write block" , index );
203+ closeIndex (index );
204+
205+ assertThat (indexBlocks (index ), contains (INDEX_CLOSED_BLOCK ));
206+ assertThat (indexBlocks (index ), not (contains (INDEX_WRITE_BLOCK )));
207+ assertIndexSetting (index , VERIFIED_BEFORE_CLOSE_SETTING , is (true ));
208+ assertIndexSetting (index , VERIFIED_READ_ONLY_SETTING , is (false ));
209+ return ;
210+ }
211+
212+ if (isFullyUpgradedTo (VERSION_CURRENT )) {
213+ assertThat (indexBlocks (index ), contains (INDEX_CLOSED_BLOCK ));
214+ assertIndexSetting (index , VERIFIED_BEFORE_CLOSE_SETTING , is (true ));
215+ assertIndexSetting (index , VERIFIED_READ_ONLY_SETTING , is (false ));
216+
217+ logger .debug ("--> re-opening index [{}] will add a write block" , index );
218+ openIndex (index );
219+ ensureGreen (index );
220+
221+ assertThat (indexBlocks (index ), contains (INDEX_WRITE_BLOCK ));
222+ assertIndexSetting (index , VERIFIED_BEFORE_CLOSE_SETTING , is (false ));
223+ assertIndexSetting (index , VERIFIED_READ_ONLY_SETTING , is (true ));
224+ assertDocCount (client (), index , numDocs );
225+
226+ logger .debug ("--> closing index [{}]" , index );
227+ closeIndex (index );
228+ ensureGreen (index );
229+
230+ assertThat (indexBlocks (index ), contains (INDEX_CLOSED_BLOCK , INDEX_WRITE_BLOCK ));
231+ assertIndexSetting (index , VERIFIED_BEFORE_CLOSE_SETTING , is (true ));
232+ assertIndexSetting (index , VERIFIED_READ_ONLY_SETTING , is (true ));
233+
234+ deleteIndex (index );
235+ }
236+ }
237+
176238 /**
177239 * Creates an index on N-2, marks as read-only on N-1 and creates a snapshot, then restores the snapshot on N.
178240 */
@@ -190,11 +252,7 @@ public void testRestoreIndex() throws Exception {
190252 createIndex (
191253 client (),
192254 index ,
193- Settings .builder ()
194- .put (IndexMetadata .SETTING_NUMBER_OF_SHARDS , 1 )
195- .put (IndexMetadata .SETTING_NUMBER_OF_REPLICAS , 0 )
196- .put (IndexSettings .INDEX_SOFT_DELETES_SETTING .getKey (), true )
197- .build ()
255+ Settings .builder ().put (IndexMetadata .SETTING_NUMBER_OF_SHARDS , 1 ).put (IndexMetadata .SETTING_NUMBER_OF_REPLICAS , 0 ).build ()
198256 );
199257
200258 logger .debug ("--> indexing [{}] docs in [{}]" , numDocs , index );
@@ -272,11 +330,7 @@ public void testRestoreIndexOverClosedIndex() throws Exception {
272330 createIndex (
273331 client (),
274332 index ,
275- Settings .builder ()
276- .put (IndexMetadata .SETTING_NUMBER_OF_SHARDS , 1 )
277- .put (IndexMetadata .SETTING_NUMBER_OF_REPLICAS , 0 )
278- .put (IndexSettings .INDEX_SOFT_DELETES_SETTING .getKey (), true )
279- .build ()
333+ Settings .builder ().put (IndexMetadata .SETTING_NUMBER_OF_SHARDS , 1 ).put (IndexMetadata .SETTING_NUMBER_OF_REPLICAS , 0 ).build ()
280334 );
281335
282336 logger .debug ("--> indexing [{}] docs in [{}]" , numDocs , index );
0 commit comments