99
1010package org .elasticsearch .lucene ;
1111
12- import org .elasticsearch .client .Request ;
1312import org .elasticsearch .cluster .metadata .IndexMetadata ;
14- import org .elasticsearch .common .Strings ;
1513import org .elasticsearch .common .settings .Settings ;
1614import org .elasticsearch .index .IndexSettings ;
1715import org .elasticsearch .repositories .fs .FsRepository ;
1816import org .elasticsearch .test .cluster .util .Version ;
1917
20- import java .util .stream .IntStream ;
21-
22- import static org .elasticsearch .test .rest .ObjectPath .createFromResponse ;
23- import static org .hamcrest .Matchers .allOf ;
2418import static org .hamcrest .Matchers .equalTo ;
25- import static org .hamcrest .Matchers .is ;
26- import static org .hamcrest .Matchers .notNullValue ;
2719
2820public class SearchableSnapshotCompatibilityIT extends AbstractLuceneIndexCompatibilityTestCase {
2921
@@ -37,24 +29,19 @@ public SearchableSnapshotCompatibilityIT(Version version) {
3729 super (version );
3830 }
3931
40- // TODO Add a test to mount the N-2 index on N-1 and then search it on N
41-
32+ /**
33+ * Creates an index and a snapshot on N-2, then mounts the snapshot on N.
34+ */
4235 public void testSearchableSnapshot () throws Exception {
4336 final String repository = suffix ("repository" );
4437 final String snapshot = suffix ("snapshot" );
4538 final String index = suffix ("index" );
4639 final int numDocs = 1234 ;
4740
48- logger .debug ("--> registering repository [{}]" , repository );
49- registerRepository (
50- client (),
51- repository ,
52- FsRepository .TYPE ,
53- true ,
54- Settings .builder ().put ("location" , REPOSITORY_PATH .getRoot ().getPath ()).build ()
55- );
56-
5741 if (VERSION_MINUS_2 .equals (clusterVersion ())) {
42+ logger .debug ("--> registering repository [{}]" , repository );
43+ registerRepository (client (), repository , FsRepository .TYPE , true , repositorySettings ());
44+
5845 logger .debug ("--> creating index [{}]" , index );
5946 createIndex (
6047 client (),
@@ -67,17 +54,7 @@ public void testSearchableSnapshot() throws Exception {
6754 );
6855
6956 logger .debug ("--> indexing [{}] docs in [{}]" , numDocs , index );
70- final var bulks = new StringBuilder ();
71- IntStream .range (0 , numDocs ).forEach (n -> bulks .append (Strings .format ("""
72- {"index":{"_id":"%s","_index":"%s"}}
73- {"test":"test"}
74- """ , n , index )));
75-
76- var bulkRequest = new Request ("POST" , "/_bulk" );
77- bulkRequest .setJsonEntity (bulks .toString ());
78- var bulkResponse = client ().performRequest (bulkRequest );
79- assertOK (bulkResponse );
80- assertThat (entityAsMap (bulkResponse ).get ("errors" ), allOf (notNullValue (), is (false )));
57+ indexDocs (index , numDocs );
8158
8259 logger .debug ("--> creating snapshot [{}]" , snapshot );
8360 createSnapshot (client (), repository , snapshot , true );
@@ -87,7 +64,7 @@ public void testSearchableSnapshot() throws Exception {
8764 if (VERSION_MINUS_1 .equals (clusterVersion ())) {
8865 ensureGreen (index );
8966
90- assertThat (indexLuceneVersion (index ), equalTo (VERSION_MINUS_2 ));
67+ assertThat (indexVersion (index ), equalTo (VERSION_MINUS_2 ));
9168 assertDocCount (client (), index , numDocs );
9269
9370 logger .debug ("--> deleting index [{}]" , index );
@@ -98,20 +75,75 @@ public void testSearchableSnapshot() throws Exception {
9875 if (VERSION_CURRENT .equals (clusterVersion ())) {
9976 var mountedIndex = suffix ("index-mounted" );
10077 logger .debug ("--> mounting index [{}] as [{}]" , index , mountedIndex );
78+ mountIndex (repository , snapshot , index , randomBoolean (), mountedIndex );
79+
80+ ensureGreen (mountedIndex );
81+
82+ assertThat (indexVersion (mountedIndex ), equalTo (VERSION_MINUS_2 ));
83+ assertDocCount (client (), mountedIndex , numDocs );
84+
85+ logger .debug ("--> adding replica to test peer-recovery" );
86+ updateIndexSettings (mountedIndex , Settings .builder ().put (IndexMetadata .SETTING_NUMBER_OF_REPLICAS , 1 ));
87+ ensureGreen (mountedIndex );
88+ }
89+ }
90+
91+ /**
92+ * Creates an index and a snapshot on N-2, mounts the snapshot on N -1 and then upgrades to N.
93+ */
94+ public void testSearchableSnapshotUpgrade () throws Exception {
95+ final String mountedIndex = suffix ("index-mounted" );
96+ final String repository = suffix ("repository" );
97+ final String snapshot = suffix ("snapshot" );
98+ final String index = suffix ("index" );
99+ final int numDocs = 4321 ;
100+
101+ if (VERSION_MINUS_2 .equals (clusterVersion ())) {
102+ logger .debug ("--> registering repository [{}]" , repository );
103+ registerRepository (client (), repository , FsRepository .TYPE , true , repositorySettings ());
104+
105+ logger .debug ("--> creating index [{}]" , index );
106+ createIndex (
107+ client (),
108+ index ,
109+ Settings .builder ()
110+ .put (IndexMetadata .SETTING_NUMBER_OF_SHARDS , 1 )
111+ .put (IndexMetadata .SETTING_NUMBER_OF_REPLICAS , 0 )
112+ .put (IndexSettings .INDEX_SOFT_DELETES_SETTING .getKey (), true )
113+ .build ()
114+ );
115+
116+ logger .debug ("--> indexing [{}] docs in [{}]" , numDocs , index );
117+ indexDocs (index , numDocs );
118+
119+ logger .debug ("--> creating snapshot [{}]" , snapshot );
120+ createSnapshot (client (), repository , snapshot , true );
121+
122+ logger .debug ("--> deleting index [{}]" , index );
123+ deleteIndex (index );
124+ return ;
125+ }
126+
127+ if (VERSION_MINUS_1 .equals (clusterVersion ())) {
128+ logger .debug ("--> mounting index [{}] as [{}]" , index , mountedIndex );
129+ mountIndex (repository , snapshot , index , randomBoolean (), mountedIndex );
130+
131+ ensureGreen (mountedIndex );
132+
133+ assertThat (indexVersion (mountedIndex ), equalTo (VERSION_MINUS_2 ));
134+ assertDocCount (client (), mountedIndex , numDocs );
135+ return ;
136+ }
137+
138+ if (VERSION_CURRENT .equals (clusterVersion ())) {
139+ ensureGreen (mountedIndex );
140+
141+ assertThat (indexVersion (mountedIndex ), equalTo (VERSION_MINUS_2 ));
142+ assertDocCount (client (), mountedIndex , numDocs );
101143
102- // Mounting the index will fail as Elasticsearch does not support reading N-2 yet
103- var request = new Request ("POST" , "/_snapshot/" + repository + "/" + snapshot + "/_mount" );
104- request .addParameter ("wait_for_completion" , "true" );
105- var storage = randomBoolean () ? "shared_cache" : "full_copy" ;
106- request .addParameter ("storage" , storage );
107- request .setJsonEntity (Strings .format ("""
108- {
109- "index": "%s",
110- "renamed_index": "%s"
111- }""" , index , mountedIndex ));
112- var responseBody = createFromResponse (client ().performRequest (request ));
113- assertThat (responseBody .evaluate ("snapshot.shards.total" ), equalTo ((int ) responseBody .evaluate ("snapshot.shards.failed" )));
114- assertThat (responseBody .evaluate ("snapshot.shards.successful" ), equalTo (0 ));
144+ logger .debug ("--> adding replica to test peer-recovery" );
145+ updateIndexSettings (mountedIndex , Settings .builder ().put (IndexMetadata .SETTING_NUMBER_OF_REPLICAS , 1 ));
146+ ensureGreen (mountedIndex );
115147 }
116148 }
117149}
0 commit comments