Skip to content

Commit

Permalink
Don't use endianness reverse util for writing metadata state file (#7…
Browse files Browse the repository at this point in the history
…8309)

The endianness of different versions is handled when reading files.
  • Loading branch information
iverase authored Sep 27, 2021
1 parent 377f546 commit 28c74a0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,8 +55,11 @@ public abstract class MetadataStateFormat<T> {
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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Metadata> format = new MetadataStateFormat<Metadata>("global-") {

@Override
public void toXContent(XContentBuilder builder, Metadata state) {
public void toXContent(XContentBuilder builder, Metadata state) throws IOException {
fail("this test doesn't write");
}

Expand All @@ -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());
Expand Down
Binary file not shown.

0 comments on commit 28c74a0

Please sign in to comment.