99
1010package org .elasticsearch .lucene ;
1111
12- import com .carrotsearch .randomizedtesting .TestMethodAndParams ;
13- import com .carrotsearch .randomizedtesting .annotations .Name ;
14- import com .carrotsearch .randomizedtesting .annotations .ParametersFactory ;
15- import com .carrotsearch .randomizedtesting .annotations .TestCaseOrdering ;
16-
1712import org .elasticsearch .client .Request ;
1813import org .elasticsearch .common .settings .Settings ;
1914import org .elasticsearch .core .Strings ;
2217import org .elasticsearch .test .cluster .local .distribution .DistributionType ;
2318import org .elasticsearch .test .cluster .util .Version ;
2419import org .elasticsearch .test .rest .ESRestTestCase ;
20+ import org .junit .After ;
2521import org .junit .Before ;
2622import org .junit .ClassRule ;
2723import org .junit .rules .RuleChain ;
2824import org .junit .rules .TemporaryFolder ;
2925import org .junit .rules .TestRule ;
3026
31- import java .util .Comparator ;
27+ import java .io .IOException ;
28+ import java .util .HashMap ;
3229import java .util .Locale ;
30+ import java .util .Map ;
3331import java .util .stream .IntStream ;
34- import java .util .stream .Stream ;
3532
3633import static org .elasticsearch .test .cluster .util .Version .CURRENT ;
3734import static org .elasticsearch .test .cluster .util .Version .fromString ;
4138import static org .hamcrest .Matchers .is ;
4239import static org .hamcrest .Matchers .notNullValue ;
4340
44- /**
45- * Test suite for Lucene indices backward compatibility with N-2 versions. The test suite creates a cluster in N-2 version, then upgrades it
46- * to N-1 version and finally upgrades it to the current version. Test methods are executed after each upgrade.
47- */
48- @ TestCaseOrdering (AbstractLuceneIndexCompatibilityTestCase .TestCaseOrdering .class )
49- public abstract class AbstractLuceneIndexCompatibilityTestCase extends ESRestTestCase {
41+ public abstract class AbstractIndexCompatibilityTestCase extends ESRestTestCase {
5042
5143 protected static final Version VERSION_MINUS_2 = fromString (System .getProperty ("tests.minimum.index.compatible" ));
5244 protected static final Version VERSION_MINUS_1 = fromString (System .getProperty ("tests.minimum.wire.compatible" ));
5345 protected static final Version VERSION_CURRENT = CURRENT ;
5446
55- protected static TemporaryFolder REPOSITORY_PATH = new TemporaryFolder ();
47+ protected static final int NODES = 3 ;
48+
49+ private static TemporaryFolder REPOSITORY_PATH = new TemporaryFolder ();
5650
5751 protected static LocalClusterConfigProvider clusterConfig = c -> {};
5852 private static ElasticsearchCluster cluster = ElasticsearchCluster .local ()
5953 .distribution (DistributionType .DEFAULT )
6054 .version (VERSION_MINUS_2 )
61- .nodes (2 )
55+ .nodes (NODES )
6256 .setting ("path.repo" , () -> REPOSITORY_PATH .getRoot ().getPath ())
6357 .setting ("xpack.security.enabled" , "false" )
6458 .setting ("xpack.ml.enabled" , "false" )
@@ -71,15 +65,44 @@ public abstract class AbstractLuceneIndexCompatibilityTestCase extends ESRestTes
7165
7266 private static boolean upgradeFailed = false ;
7367
74- private final Version clusterVersion ;
68+ @ Before
69+ public final void maybeUpgradeBeforeTest () throws Exception {
70+ // We want to use this test suite for the V9 upgrade, but we are not fully committed to necessarily having N-2 support
71+ // in V10, so we add a check here to ensure we'll revisit this decision once V10 exists.
72+ assertThat ("Explicit check that N-2 version is Elasticsearch 7" , VERSION_MINUS_2 .getMajor (), equalTo (7 ));
73+
74+ if (upgradeFailed == false ) {
75+ try {
76+ maybeUpgrade ();
77+ } catch (Exception e ) {
78+ upgradeFailed = true ;
79+ throw e ;
80+ }
81+ }
7582
76- public AbstractLuceneIndexCompatibilityTestCase ( @ Name ( "cluster" ) Version clusterVersion ) {
77- this . clusterVersion = clusterVersion ;
83+ // Skip remaining tests if upgrade failed
84+ assumeFalse ( "Cluster upgrade failed" , upgradeFailed ) ;
7885 }
7986
80- @ ParametersFactory
81- public static Iterable <Object []> parameters () {
82- return Stream .of (VERSION_MINUS_2 , VERSION_MINUS_1 , CURRENT ).map (v -> new Object [] { v }).toList ();
87+ protected abstract void maybeUpgrade () throws Exception ;
88+
89+ @ After
90+ public final void deleteSnapshotBlobCache () throws IOException {
91+ // TODO ES-10475: The .snapshot-blob-cache created in legacy version can block upgrades, we should probably delete it automatically
92+ try {
93+ var request = new Request ("DELETE" , "/.snapshot-blob-cache" );
94+ request .setOptions (
95+ expectWarnings (
96+ "this request accesses system indices: [.snapshot-blob-cache], but in a future major version, "
97+ + "direct access to system indices will be prevented by default"
98+ )
99+ );
100+ adminClient ().performRequest (request );
101+ } catch (IOException e ) {
102+ if (isNotFoundResponseException (e ) == false ) {
103+ throw e ;
104+ }
105+ }
83106 }
84107
85108 @ Override
@@ -92,26 +115,8 @@ protected boolean preserveClusterUponCompletion() {
92115 return true ;
93116 }
94117
95- @ Before
96- public void maybeUpgrade () throws Exception {
97- // We want to use this test suite for the V9 upgrade, but we are not fully committed to necessarily having N-2 support
98- // in V10, so we add a check here to ensure we'll revisit this decision once V10 exists.
99- assertThat ("Explicit check that N-2 version is Elasticsearch 7" , VERSION_MINUS_2 .getMajor (), equalTo (7 ));
100-
101- var currentVersion = clusterVersion ();
102- if (currentVersion .before (clusterVersion )) {
103- try {
104- cluster .upgradeToVersion (clusterVersion );
105- closeClients ();
106- initClient ();
107- } catch (Exception e ) {
108- upgradeFailed = true ;
109- throw e ;
110- }
111- }
112-
113- // Skip remaining tests if upgrade failed
114- assumeFalse ("Cluster upgrade failed" , upgradeFailed );
118+ protected ElasticsearchCluster cluster () {
119+ return cluster ;
115120 }
116121
117122 protected String suffix (String name ) {
@@ -124,12 +129,18 @@ protected Settings repositorySettings() {
124129 .build ();
125130 }
126131
127- protected static Version clusterVersion () throws Exception {
128- var response = assertOK (client ().performRequest (new Request ("GET" , "/" )));
129- var responseBody = createFromResponse (response );
130- var version = Version .fromString (responseBody .evaluate ("version.number" ).toString ());
131- assertThat ("Failed to retrieve cluster version" , version , notNullValue ());
132- return version ;
132+ protected static Map <String , Version > nodesVersions () throws Exception {
133+ var nodesInfos = getNodesInfo (adminClient ());
134+ assertThat (nodesInfos .size (), equalTo (NODES ));
135+ var versions = new HashMap <String , Version >();
136+ for (var nodeInfos : nodesInfos .values ()) {
137+ versions .put ((String ) nodeInfos .get ("name" ), Version .fromString ((String ) nodeInfos .get ("version" )));
138+ }
139+ return versions ;
140+ }
141+
142+ protected static boolean isFullyUpgradedTo (Version version ) throws Exception {
143+ return nodesVersions ().values ().stream ().allMatch (v -> v .equals (version ));
133144 }
134145
135146 protected static Version indexVersion (String indexName ) throws Exception {
@@ -181,16 +192,4 @@ protected static void restoreIndex(String repository, String snapshot, String in
181192 assertThat (responseBody .evaluate ("snapshot.shards.total" ), equalTo ((int ) responseBody .evaluate ("snapshot.shards.failed" )));
182193 assertThat (responseBody .evaluate ("snapshot.shards.successful" ), equalTo (0 ));
183194 }
184-
185- /**
186- * Execute the test suite with the parameters provided by the {@link #parameters()} in version order.
187- */
188- public static class TestCaseOrdering implements Comparator <TestMethodAndParams > {
189- @ Override
190- public int compare (TestMethodAndParams o1 , TestMethodAndParams o2 ) {
191- var version1 = (Version ) o1 .getInstanceArguments ().get (0 );
192- var version2 = (Version ) o2 .getInstanceArguments ().get (0 );
193- return version1 .compareTo (version2 );
194- }
195- }
196195}
0 commit comments