Skip to content

Commit

Permalink
Remove Support for VERSION_CHECKPOINTS Translogs (elastic#42782)
Browse files Browse the repository at this point in the history
* Closes elastic#42699
  • Loading branch information
original-brownbear authored Jun 2, 2019
1 parent 68648ec commit 56a662e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
import java.nio.channels.FileChannel;
import java.nio.file.Path;

import static org.elasticsearch.index.seqno.SequenceNumbers.UNASSIGNED_PRIMARY_TERM;

/**
* Each translog file is started with a translog header then followed by translog operations.
*/
Expand Down Expand Up @@ -123,6 +121,9 @@ static TranslogHeader read(final String translogUUID, final Path path, final Fil
if (version == VERSION_CHECKSUMS) {
throw new IllegalStateException("pre-2.0 translog found [" + path + "]");
}
if (version == VERSION_CHECKPOINTS) {
throw new IllegalStateException("pre-6.3 translog found [" + path + "]");
}
// Read the translogUUID
final int uuidLen = in.readInt();
if (uuidLen > channel.size()) {
Expand All @@ -141,17 +142,10 @@ static TranslogHeader read(final String translogUUID, final Path path, final Fil
" this translog file belongs to a different translog");
}
// Read the primary term
final long primaryTerm;
if (version == VERSION_PRIMARY_TERM) {
primaryTerm = in.readLong();
} else {
assert version == VERSION_CHECKPOINTS : "Unknown header version [" + version + "]";
primaryTerm = UNASSIGNED_PRIMARY_TERM;
}
assert version == VERSION_PRIMARY_TERM;
final long primaryTerm = in.readLong();
// Verify the checksum
if (version >= VERSION_PRIMARY_TERM) {
Translog.verifyChecksum(in);
}
Translog.verifyChecksum(in);
assert primaryTerm >= 0 : "Primary term must be non-negative [" + primaryTerm + "]; translog path [" + path + "]";

final int headerSizeInBytes = headerSizeInBytes(version, uuid.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,17 @@

package org.elasticsearch.index.translog;

import org.apache.lucene.codecs.CodecUtil;
import org.apache.lucene.store.OutputStreamDataOutput;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.UUIDs;
import org.elasticsearch.common.io.stream.OutputStreamStreamOutput;
import org.elasticsearch.index.seqno.SequenceNumbers;
import org.elasticsearch.test.ESTestCase;

import java.io.IOException;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.lessThan;

public class TranslogHeaderTests extends ESTestCase {

Expand Down Expand Up @@ -72,40 +65,10 @@ public void testCurrentHeaderVersion() throws Exception {
});
}

public void testHeaderWithoutPrimaryTerm() throws Exception {
final String translogUUID = UUIDs.randomBase64UUID();
final long generation = randomNonNegativeLong();
final Path translogFile = createTempDir().resolve(Translog.getFilename(generation));
try (FileChannel channel = FileChannel.open(translogFile, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)) {
writeHeaderWithoutTerm(channel, translogUUID);
assertThat((int)channel.position(), lessThan(TranslogHeader.headerSizeInBytes(translogUUID)));
}
try (FileChannel channel = FileChannel.open(translogFile, StandardOpenOption.READ)) {
final TranslogHeader inHeader = TranslogHeader.read(translogUUID, translogFile, channel);
assertThat(inHeader.getTranslogUUID(), equalTo(translogUUID));
assertThat(inHeader.getPrimaryTerm(), equalTo(SequenceNumbers.UNASSIGNED_PRIMARY_TERM));
assertThat(inHeader.sizeInBytes(), equalTo((int)channel.position()));
}
expectThrows(TranslogCorruptedException.class, () -> {
try (FileChannel channel = FileChannel.open(translogFile, StandardOpenOption.READ)) {
TranslogHeader.read(UUIDs.randomBase64UUID(), translogFile, channel);
}
});
}

static void writeHeaderWithoutTerm(FileChannel channel, String translogUUID) throws IOException {
final OutputStreamStreamOutput out = new OutputStreamStreamOutput(Channels.newOutputStream(channel));
CodecUtil.writeHeader(new OutputStreamDataOutput(out), TranslogHeader.TRANSLOG_CODEC, TranslogHeader.VERSION_CHECKPOINTS);
final BytesRef uuid = new BytesRef(translogUUID);
out.writeInt(uuid.length);
out.writeBytes(uuid.bytes, uuid.offset, uuid.length);
channel.force(true);
assertThat(channel.position(), equalTo(43L));
}

public void testLegacyTranslogVersions() throws Exception {
public void testLegacyTranslogVersions() {
checkFailsToOpen("/org/elasticsearch/index/translog/translog-v0.binary", IllegalStateException.class, "pre-1.4 translog");
checkFailsToOpen("/org/elasticsearch/index/translog/translog-v1.binary", IllegalStateException.class, "pre-2.0 translog");
checkFailsToOpen("/org/elasticsearch/index/translog/translog-v2.binary", IllegalStateException.class, "pre-6.3 translog");
checkFailsToOpen("/org/elasticsearch/index/translog/translog-v1-truncated.binary", IllegalStateException.class, "pre-2.0 translog");
checkFailsToOpen("/org/elasticsearch/index/translog/translog-v1-corrupted-magic.binary",
TranslogCorruptedException.class, "translog looks like version 1 or later, but has corrupted header");
Expand Down
Binary file not shown.

0 comments on commit 56a662e

Please sign in to comment.