From b63f9b967c544c972ff674e22eb671b98c966c7e Mon Sep 17 00:00:00 2001 From: Nhat Nguyen Date: Wed, 19 Dec 2018 11:19:25 -0500 Subject: [PATCH] Fix translog bwc serialization (#36676) Serializing of a Translog#Index from v7.0.0 to 6.x is broken since #29224 where we removed the _parent field. Relates #29224 --- .../java/org/elasticsearch/index/translog/Translog.java | 3 +++ .../org/elasticsearch/index/translog/TranslogTests.java | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/server/src/main/java/org/elasticsearch/index/translog/Translog.java b/server/src/main/java/org/elasticsearch/index/translog/Translog.java index 83d81222bf5..d8acba635f8 100644 --- a/server/src/main/java/org/elasticsearch/index/translog/Translog.java +++ b/server/src/main/java/org/elasticsearch/index/translog/Translog.java @@ -1227,6 +1227,9 @@ public class Translog extends AbstractIndexShardComponent implements IndexShardC out.writeString(type); out.writeBytesReference(source); out.writeOptionalString(routing); + if (format < FORMAT_NO_PARENT) { + out.writeOptionalString(null); // _parent + } out.writeLong(version); if (format < FORMAT_NO_VERSION_TYPE) { out.writeByte(VersionType.EXTERNAL.getValue()); diff --git a/server/src/test/java/org/elasticsearch/index/translog/TranslogTests.java b/server/src/test/java/org/elasticsearch/index/translog/TranslogTests.java index 7e73f9ef517..3eddeea2f2a 100644 --- a/server/src/test/java/org/elasticsearch/index/translog/TranslogTests.java +++ b/server/src/test/java/org/elasticsearch/index/translog/TranslogTests.java @@ -35,6 +35,7 @@ import org.apache.lucene.store.MockDirectoryWrapper; import org.apache.lucene.util.LineFileDocs; import org.apache.lucene.util.LuceneTestCase; import org.elasticsearch.Assertions; +import org.elasticsearch.Version; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.Randomness; import org.elasticsearch.common.Strings; @@ -72,6 +73,7 @@ import org.elasticsearch.index.shard.ShardId; import org.elasticsearch.index.translog.Translog.Location; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.IndexSettingsModule; +import org.elasticsearch.test.VersionUtils; import org.hamcrest.Matchers; import org.junit.After; import org.junit.Before; @@ -2814,9 +2816,12 @@ public class TranslogTests extends ESTestCase { Engine.IndexResult eIndexResult = new Engine.IndexResult(1, randomPrimaryTerm, randomSeqNum, true); Translog.Index index = new Translog.Index(eIndex, eIndexResult); + Version wireVersion = VersionUtils.randomVersionBetween(random(), Version.CURRENT.minimumCompatibilityVersion(), Version.CURRENT); BytesStreamOutput out = new BytesStreamOutput(); + out.setVersion(wireVersion); Translog.Operation.writeOperation(out, index); StreamInput in = out.bytes().streamInput(); + in.setVersion(wireVersion); Translog.Index serializedIndex = (Translog.Index) Translog.Operation.readOperation(in); assertEquals(index, serializedIndex); @@ -2826,8 +2831,10 @@ public class TranslogTests extends ESTestCase { Translog.Delete delete = new Translog.Delete(eDelete, eDeleteResult); out = new BytesStreamOutput(); + out.setVersion(wireVersion); Translog.Operation.writeOperation(out, delete); in = out.bytes().streamInput(); + in.setVersion(wireVersion); Translog.Delete serializedDelete = (Translog.Delete) Translog.Operation.readOperation(in); assertEquals(delete, serializedDelete); }