Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use endianness reverse util for writing metadata state file #78309

Merged
merged 1 commit into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.