Browse Source

Register ResyncTask.Status as a NamedWriteable (#36610)

Today, ResyncTask.Status is not registered, but appears as a task status
sometimes, leading to `Failed to deserialize response from handler` exceptions:

    java.lang.IllegalArgumentException: Unknown NamedWriteable [org.elasticsearch.tasks.Task$Status][resync]

This commit adds the missing registration.
David Turner 6 years ago
parent
commit
245cdd8c34

+ 3 - 0
server/src/main/java/org/elasticsearch/common/network/NetworkModule.java

@@ -38,6 +38,7 @@ import org.elasticsearch.common.util.PageCacheRecycler;
 import org.elasticsearch.common.xcontent.NamedXContentRegistry;
 import org.elasticsearch.common.xcontent.XContentParser;
 import org.elasticsearch.http.HttpServerTransport;
+import org.elasticsearch.index.shard.PrimaryReplicaSyncer.ResyncTask;
 import org.elasticsearch.indices.breaker.CircuitBreakerService;
 import org.elasticsearch.plugins.NetworkPlugin;
 import org.elasticsearch.tasks.RawTaskStatus;
@@ -94,6 +95,8 @@ public final class NetworkModule {
             new NamedWriteableRegistry.Entry(Task.Status.class, ReplicationTask.Status.NAME, ReplicationTask.Status::new));
         namedWriteables.add(
             new NamedWriteableRegistry.Entry(Task.Status.class, RawTaskStatus.NAME, RawTaskStatus::new));
+        namedWriteables.add(
+            new NamedWriteableRegistry.Entry(Task.Status.class, ResyncTask.Status.NAME, ResyncTask.Status::new));
     }
 
     private final Map<String, Supplier<Transport>> transportFactories = new HashMap<>();

+ 18 - 0
server/src/test/java/org/elasticsearch/index/shard/PrimaryReplicaSyncerTests.java

@@ -28,7 +28,11 @@ import org.elasticsearch.common.Strings;
 import org.elasticsearch.common.bytes.BytesArray;
 import org.elasticsearch.common.io.stream.ByteBufferStreamInput;
 import org.elasticsearch.common.io.stream.BytesStreamOutput;
+import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput;
+import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
+import org.elasticsearch.common.io.stream.StreamInput;
 import org.elasticsearch.common.lucene.uid.Versions;
+import org.elasticsearch.common.network.NetworkModule;
 import org.elasticsearch.common.settings.Settings;
 import org.elasticsearch.common.unit.ByteSizeValue;
 import org.elasticsearch.common.xcontent.ToXContent;
@@ -38,6 +42,7 @@ import org.elasticsearch.common.xcontent.XContentType;
 import org.elasticsearch.index.VersionType;
 import org.elasticsearch.index.mapper.SourceToParse;
 import org.elasticsearch.index.seqno.SequenceNumbers;
+import org.elasticsearch.tasks.Task;
 import org.elasticsearch.tasks.TaskManager;
 
 import java.io.IOException;
@@ -196,6 +201,19 @@ public class PrimaryReplicaSyncerTests extends IndexShardTestCase {
         assertEquals(status, serializedStatus);
     }
 
+    public void testStatusSerializationAsNamedWriteable() throws IOException {
+        PrimaryReplicaSyncer.ResyncTask.Status status = new PrimaryReplicaSyncer.ResyncTask.Status(randomAlphaOfLength(10),
+            randomIntBetween(0, 1000), randomIntBetween(0, 1000), randomIntBetween(0, 1000));
+        try (BytesStreamOutput out = new BytesStreamOutput()) {
+            out.writeNamedWriteable(status);
+            try (StreamInput in = new NamedWriteableAwareStreamInput(
+                new ByteBufferStreamInput(ByteBuffer.wrap(out.bytes().toBytesRef().bytes)),
+                new NamedWriteableRegistry(NetworkModule.getNamedWriteables()))) {
+                assertThat(in.readNamedWriteable(Task.Status.class), equalTo(status));
+            }
+        }
+    }
+
     public void testStatusEquals() throws IOException {
         PrimaryReplicaSyncer.ResyncTask task =
             new PrimaryReplicaSyncer.ResyncTask(0, "type", "action", "desc", null, Collections.emptyMap());