Explorar el Código

Properly fake corrupted translog (#49918)

The fake translog corruption in the test sometimes generates invalid translog files where some
assertions do not hold (e.g. minSeqNo <= maxSeqNo or minTranslogGen <= translogGen)

Closes #49909
Yannick Welsch hace 5 años
padre
commit
6e82050233

+ 15 - 5
server/src/test/java/org/elasticsearch/index/translog/TestTranslog.java

@@ -92,11 +92,21 @@ public class TestTranslog {
                 // if we crashed while rolling a generation then we might have copied `translog.ckp` to its numbered generation file but
                 // have not yet written a new `translog.ckp`. During recovery we must also verify that this file is intact, so it's ok to
                 // corrupt this file too (either by writing the wrong information, correctly formatted, or by properly corrupting it)
-                final Checkpoint checkpointCopy = LuceneTestCase.usually(random) ? checkpoint
-                        : new Checkpoint(checkpoint.offset + random.nextInt(2), checkpoint.numOps + random.nextInt(2),
-                            checkpoint.generation + random.nextInt(2), checkpoint.minSeqNo + random.nextInt(2),
-                            checkpoint.maxSeqNo + random.nextInt(2), checkpoint.globalCheckpoint + random.nextInt(2),
-                            checkpoint.minTranslogGeneration + random.nextInt(2), checkpoint.trimmedAboveSeqNo + random.nextInt(2));
+                final Checkpoint checkpointCopy;
+                if (LuceneTestCase.usually(random)) {
+                    checkpointCopy = checkpoint;
+                } else {
+                    long newTranslogGeneration = checkpoint.generation + random.nextInt(2);
+                    long newMinTranslogGeneration = Math.min(newTranslogGeneration, checkpoint.minTranslogGeneration + random.nextInt(2));
+                    long newMaxSeqNo = checkpoint.maxSeqNo + random.nextInt(2);
+                    long newMinSeqNo = Math.min(newMaxSeqNo, checkpoint.minSeqNo + random.nextInt(2));
+                    long newTrimmedAboveSeqNo = Math.min(newMaxSeqNo, checkpoint.trimmedAboveSeqNo + random.nextInt(2));
+
+                    checkpointCopy = new Checkpoint(checkpoint.offset + random.nextInt(2), checkpoint.numOps + random.nextInt(2),
+                        newTranslogGeneration, newMinSeqNo,
+                        newMaxSeqNo, checkpoint.globalCheckpoint + random.nextInt(2),
+                        newMinTranslogGeneration, newTrimmedAboveSeqNo);
+                }
                 Checkpoint.write(FileChannel::open, unnecessaryCheckpointCopyPath, checkpointCopy,
                     StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW);