From f7287b7bfc01e63ee8deb040d0e547316e45a055 Mon Sep 17 00:00:00 2001 From: iverase Date: Mon, 27 Sep 2021 12:08:29 +0200 Subject: [PATCH] Don't use endianness reverse util for writing metadata state file --- .../gateway/MetadataStateFormat.java | 22 ++++++++++++------ .../gateway/MetadataStateFormatTests.java | 17 ++++++++++---- .../gateway/{global-3.st => global-3-V1.st} | Bin .../org/elasticsearch/gateway/global-3-V2.st | Bin 0 -> 267 bytes 4 files changed, 28 insertions(+), 11 deletions(-) rename server/src/test/resources/org/elasticsearch/gateway/{global-3.st => global-3-V1.st} (100%) create mode 100644 server/src/test/resources/org/elasticsearch/gateway/global-3-V2.st diff --git a/server/src/main/java/org/elasticsearch/gateway/MetadataStateFormat.java b/server/src/main/java/org/elasticsearch/gateway/MetadataStateFormat.java index 62815c03f47df..34d48db543d46 100644 --- a/server/src/main/java/org/elasticsearch/gateway/MetadataStateFormat.java +++ b/server/src/main/java/org/elasticsearch/gateway/MetadataStateFormat.java @@ -10,7 +10,6 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.message.ParameterizedMessage; -import org.apache.lucene.backward_codecs.store.EndiannessReverserUtil; import org.apache.lucene.codecs.CodecUtil; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexFormatTooNewException; @@ -56,8 +55,11 @@ public abstract class MetadataStateFormat { public static final String STATE_FILE_EXTENSION = ".st"; private static final String STATE_FILE_CODEC = "state"; + // original version format private static final int MIN_COMPATIBLE_STATE_FILE_VERSION = 1; - private static final int STATE_FILE_VERSION = 1; + // Lucene directory API changed to LE, ES 8.0 + private static final int LE_VERSION = 2; + private static final int CURRENT_VERSION = LE_VERSION; private final String prefix; private final Pattern stateFilePattern; @@ -92,8 +94,8 @@ private void writeStateToFirstLocation(final T state, Path stateLocation, Direct throws WriteStateException { try { deleteFileIfExists(stateLocation, stateDir, tmpFileName); - try (IndexOutput out = EndiannessReverserUtil.createOutput(stateDir, tmpFileName, IOContext.DEFAULT)) { - CodecUtil.writeHeader(out, STATE_FILE_CODEC, STATE_FILE_VERSION); + try (IndexOutput out = stateDir.createOutput(tmpFileName, IOContext.DEFAULT)) { + CodecUtil.writeHeader(out, STATE_FILE_CODEC, CURRENT_VERSION); out.writeInt(FORMAT.index()); try (XContentBuilder builder = newXContentBuilder(FORMAT, new IndexOutputOutputStream(out) { @Override @@ -268,11 +270,17 @@ protected XContentBuilder newXContentBuilder(XContentType type, OutputStream str */ public final T read(NamedXContentRegistry namedXContentRegistry, Path file) throws IOException { try (Directory dir = newDirectory(file.getParent())) { - try (IndexInput indexInput = EndiannessReverserUtil.openInput(dir, file.getFileName().toString(), IOContext.DEFAULT)) { + try (IndexInput indexInput = dir.openInput(file.getFileName().toString(), IOContext.DEFAULT)) { // We checksum the entire file before we even go and parse it. If it's corrupted we barf right here. CodecUtil.checksumEntireFile(indexInput); - CodecUtil.checkHeader(indexInput, STATE_FILE_CODEC, MIN_COMPATIBLE_STATE_FILE_VERSION, STATE_FILE_VERSION); - final XContentType xContentType = XContentType.values()[indexInput.readInt()]; + final int format = + CodecUtil.checkHeader(indexInput, STATE_FILE_CODEC, MIN_COMPATIBLE_STATE_FILE_VERSION, CURRENT_VERSION); + final XContentType xContentType; + if (format < LE_VERSION) { + xContentType = XContentType.values()[Integer.reverseBytes(indexInput.readInt())]; + } else { + xContentType = XContentType.values()[indexInput.readInt()]; + } if (xContentType != FORMAT) { throw new IllegalStateException("expected state in " + file + " to be " + FORMAT + " format but was " + xContentType); } diff --git a/server/src/test/java/org/elasticsearch/gateway/MetadataStateFormatTests.java b/server/src/test/java/org/elasticsearch/gateway/MetadataStateFormatTests.java index 8dc61cc03e039..791ffda50cb63 100644 --- a/server/src/test/java/org/elasticsearch/gateway/MetadataStateFormatTests.java +++ b/server/src/test/java/org/elasticsearch/gateway/MetadataStateFormatTests.java @@ -42,14 +42,23 @@ @LuceneTestCase.SuppressFileSystems("ExtrasFS") // TODO: fix test to work with ExtrasFS public class MetadataStateFormatTests extends ESTestCase { + + public void testReadClusterStateV1() throws IOException { + assertReadClusterState("global-3-V1.st"); + } + + public void testReadClusterStateV2() throws IOException { + assertReadClusterState("global-3-V2.st"); + } + /** * Ensure we can read a pre-generated cluster state. */ - public void testReadClusterState() throws IOException { + private void assertReadClusterState(String clusterState) throws IOException { final MetadataStateFormat format = new MetadataStateFormat("global-") { @Override - public void toXContent(XContentBuilder builder, Metadata state) { + public void toXContent(XContentBuilder builder, Metadata state) throws IOException { fail("this test doesn't write"); } @@ -59,9 +68,9 @@ public Metadata fromXContent(XContentParser parser) throws IOException { } }; Path tmp = createTempDir(); - final InputStream resource = this.getClass().getResourceAsStream("global-3.st"); + final InputStream resource = this.getClass().getResourceAsStream(clusterState); assertThat(resource, notNullValue()); - Path dst = tmp.resolve("global-3.st"); + Path dst = tmp.resolve(clusterState); Files.copy(resource, dst); Metadata read = format.read(xContentRegistry(), dst); assertThat(read, notNullValue()); diff --git a/server/src/test/resources/org/elasticsearch/gateway/global-3.st b/server/src/test/resources/org/elasticsearch/gateway/global-3-V1.st similarity index 100% rename from server/src/test/resources/org/elasticsearch/gateway/global-3.st rename to server/src/test/resources/org/elasticsearch/gateway/global-3-V1.st diff --git a/server/src/test/resources/org/elasticsearch/gateway/global-3-V2.st b/server/src/test/resources/org/elasticsearch/gateway/global-3-V2.st new file mode 100644 index 0000000000000000000000000000000000000000..44b3f3142fd8fc52a5cf6899bfe1121ef04f30b8 GIT binary patch literal 267 zcmcD&o+Hj$T#{Il%D}+D#0Vs;G`U!Rb>ya&B=lGNOS9H3{4fBo*u%u7kF&`mE&EK99SEK2#+S(2ZdR9upumkLt)`}YBj P$uEHZ1%t`^4yOVDdH{1B literal 0 HcmV?d00001