@@ -252,6 +252,60 @@ public void testFailsOnMismatchedCommittedClusterUUIDs() throws IOException {
252
252
}
253
253
}
254
254
255
+ public void testFailsIfFreshestStateIsInStaleTerm () throws IOException {
256
+ final Path [] dataPaths1 = createDataPaths ();
257
+ final Path [] dataPaths2 = createDataPaths ();
258
+ final Path [] combinedPaths = Stream .concat (Arrays .stream (dataPaths1 ), Arrays .stream (dataPaths2 )).toArray (Path []::new );
259
+
260
+ final long staleCurrentTerm = randomLongBetween (1L , Long .MAX_VALUE - 1 );
261
+ final long freshCurrentTerm = randomLongBetween (staleCurrentTerm + 1 , Long .MAX_VALUE );
262
+
263
+ final long freshTerm = randomLongBetween (1L , Long .MAX_VALUE );
264
+ final long staleTerm = randomBoolean () ? freshTerm : randomLongBetween (1L , freshTerm );
265
+ final long freshVersion = randomLongBetween (2L , Long .MAX_VALUE );
266
+ final long staleVersion = staleTerm == freshTerm ? randomLongBetween (1L , freshVersion - 1 ) : randomLongBetween (1L , Long .MAX_VALUE );
267
+
268
+ try (NodeEnvironment nodeEnvironment = newNodeEnvironment (combinedPaths )) {
269
+ try (CoordinationState .PersistedState persistedState
270
+ = loadPersistedState (new LucenePersistedStateFactory (nodeEnvironment , xContentRegistry ()))) {
271
+ final ClusterState clusterState = persistedState .getLastAcceptedState ();
272
+ persistedState .setCurrentTerm (staleCurrentTerm );
273
+ persistedState .setLastAcceptedState (ClusterState .builder (clusterState )
274
+ .metaData (MetaData .builder (clusterState .metaData ()).version (1 )
275
+ .coordinationMetaData (CoordinationMetaData .builder (clusterState .coordinationMetaData ()).term (staleTerm ).build ()))
276
+ .version (staleVersion )
277
+ .build ());
278
+ }
279
+ }
280
+
281
+ try (NodeEnvironment nodeEnvironment = newNodeEnvironment (dataPaths1 )) {
282
+ try (CoordinationState .PersistedState persistedState
283
+ = loadPersistedState (new LucenePersistedStateFactory (nodeEnvironment , xContentRegistry ()))) {
284
+ persistedState .setCurrentTerm (freshCurrentTerm );
285
+ }
286
+ }
287
+
288
+ try (NodeEnvironment nodeEnvironment = newNodeEnvironment (dataPaths2 )) {
289
+ try (CoordinationState .PersistedState persistedState
290
+ = loadPersistedState (new LucenePersistedStateFactory (nodeEnvironment , xContentRegistry ()))) {
291
+ final ClusterState clusterState = persistedState .getLastAcceptedState ();
292
+ persistedState .setLastAcceptedState (ClusterState .builder (clusterState )
293
+ .metaData (MetaData .builder (clusterState .metaData ()).version (2 )
294
+ .coordinationMetaData (CoordinationMetaData .builder (clusterState .coordinationMetaData ()).term (freshTerm ).build ()))
295
+ .version (freshVersion )
296
+ .build ());
297
+ }
298
+ }
299
+
300
+ try (NodeEnvironment nodeEnvironment = newNodeEnvironment (combinedPaths )) {
301
+ assertThat (expectThrows (IllegalStateException .class ,
302
+ () -> loadPersistedState (new LucenePersistedStateFactory (nodeEnvironment , xContentRegistry ()))).getMessage (), allOf (
303
+ containsString ("inconsistent terms found" ),
304
+ containsString (Long .toString (staleCurrentTerm )),
305
+ containsString (Long .toString (freshCurrentTerm ))));
306
+ }
307
+ }
308
+
255
309
public void testFailsGracefullyOnExceptionDuringFlush () throws IOException {
256
310
final AtomicBoolean throwException = new AtomicBoolean ();
257
311
0 commit comments