Skip to content

Commit 6e82050

Browse files
authored
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
1 parent fc3454b commit 6e82050

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

server/src/test/java/org/elasticsearch/index/translog/TestTranslog.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,21 @@ static void corruptRandomTranslogFile(Logger logger, Random random, Path translo
9292
// if we crashed while rolling a generation then we might have copied `translog.ckp` to its numbered generation file but
9393
// have not yet written a new `translog.ckp`. During recovery we must also verify that this file is intact, so it's ok to
9494
// corrupt this file too (either by writing the wrong information, correctly formatted, or by properly corrupting it)
95-
final Checkpoint checkpointCopy = LuceneTestCase.usually(random) ? checkpoint
96-
: new Checkpoint(checkpoint.offset + random.nextInt(2), checkpoint.numOps + random.nextInt(2),
97-
checkpoint.generation + random.nextInt(2), checkpoint.minSeqNo + random.nextInt(2),
98-
checkpoint.maxSeqNo + random.nextInt(2), checkpoint.globalCheckpoint + random.nextInt(2),
99-
checkpoint.minTranslogGeneration + random.nextInt(2), checkpoint.trimmedAboveSeqNo + random.nextInt(2));
95+
final Checkpoint checkpointCopy;
96+
if (LuceneTestCase.usually(random)) {
97+
checkpointCopy = checkpoint;
98+
} else {
99+
long newTranslogGeneration = checkpoint.generation + random.nextInt(2);
100+
long newMinTranslogGeneration = Math.min(newTranslogGeneration, checkpoint.minTranslogGeneration + random.nextInt(2));
101+
long newMaxSeqNo = checkpoint.maxSeqNo + random.nextInt(2);
102+
long newMinSeqNo = Math.min(newMaxSeqNo, checkpoint.minSeqNo + random.nextInt(2));
103+
long newTrimmedAboveSeqNo = Math.min(newMaxSeqNo, checkpoint.trimmedAboveSeqNo + random.nextInt(2));
104+
105+
checkpointCopy = new Checkpoint(checkpoint.offset + random.nextInt(2), checkpoint.numOps + random.nextInt(2),
106+
newTranslogGeneration, newMinSeqNo,
107+
newMaxSeqNo, checkpoint.globalCheckpoint + random.nextInt(2),
108+
newMinTranslogGeneration, newTrimmedAboveSeqNo);
109+
}
100110
Checkpoint.write(FileChannel::open, unnecessaryCheckpointCopyPath, checkpointCopy,
101111
StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW);
102112

0 commit comments

Comments
 (0)