Skip to content

Commit ac6c0a1

Browse files
committed
Ensure that ExecuteEnrichPolicyStatus is properly registered.
When executing the enrich execute policy api and not waiting for completion, then querying for task via task list api can result into a serialization error. Relates to elastic#70554
1 parent 5c6e758 commit ac6c0a1

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/enrich/action/ExecuteEnrichPolicyStatus.java

+14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package org.elasticsearch.xpack.core.enrich.action;
88

99
import java.io.IOException;
10+
import java.util.Objects;
1011

1112
import org.elasticsearch.common.io.stream.StreamInput;
1213
import org.elasticsearch.common.io.stream.StreamOutput;
@@ -65,4 +66,17 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
6566
builder.endObject();
6667
return builder;
6768
}
69+
70+
@Override
71+
public boolean equals(Object o) {
72+
if (this == o) return true;
73+
if (o == null || getClass() != o.getClass()) return false;
74+
ExecuteEnrichPolicyStatus that = (ExecuteEnrichPolicyStatus) o;
75+
return Objects.equals(phase, that.phase);
76+
}
77+
78+
@Override
79+
public int hashCode() {
80+
return Objects.hash(phase);
81+
}
6882
}

x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/EnrichPlugin.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.elasticsearch.rest.RestController;
3636
import org.elasticsearch.rest.RestHandler;
3737
import org.elasticsearch.script.ScriptService;
38+
import org.elasticsearch.tasks.Task;
3839
import org.elasticsearch.threadpool.ThreadPool;
3940
import org.elasticsearch.watcher.ResourceWatcherService;
4041
import org.elasticsearch.xpack.core.XPackPlugin;
@@ -43,6 +44,7 @@
4344
import org.elasticsearch.xpack.core.enrich.action.DeleteEnrichPolicyAction;
4445
import org.elasticsearch.xpack.core.enrich.action.EnrichStatsAction;
4546
import org.elasticsearch.xpack.core.enrich.action.ExecuteEnrichPolicyAction;
47+
import org.elasticsearch.xpack.core.enrich.action.ExecuteEnrichPolicyStatus;
4648
import org.elasticsearch.xpack.core.enrich.action.GetEnrichPolicyAction;
4749
import org.elasticsearch.xpack.core.enrich.action.PutEnrichPolicyAction;
4850
import org.elasticsearch.xpack.enrich.action.EnrichCoordinatorProxyAction;
@@ -211,7 +213,8 @@ public List<NamedWriteableRegistry.Entry> getNamedWriteables() {
211213
NamedDiff.class,
212214
EnrichMetadata.TYPE,
213215
in -> EnrichMetadata.readDiffFrom(Metadata.Custom.class, EnrichMetadata.TYPE, in)
214-
)
216+
),
217+
new NamedWriteableRegistry.Entry(Task.Status.class, ExecuteEnrichPolicyStatus.NAME, ExecuteEnrichPolicyStatus::new)
215218
);
216219
}
217220

x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/action/EnrichStatsResponseTests.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package org.elasticsearch.xpack.enrich.action;
88

99
import org.elasticsearch.common.io.stream.Writeable;
10+
import org.elasticsearch.tasks.Task;
1011
import org.elasticsearch.tasks.TaskId;
1112
import org.elasticsearch.tasks.TaskInfo;
1213
import org.elasticsearch.test.AbstractWireSerializingTestCase;
@@ -50,6 +51,10 @@ protected Writeable.Reader<EnrichStatsAction.Response> instanceReader() {
5051
}
5152

5253
public static TaskInfo randomTaskInfo() {
54+
return randomTaskInfo(null);
55+
}
56+
57+
public static TaskInfo randomTaskInfo(Task.Status status) {
5358
TaskId taskId = new TaskId(randomAlphaOfLength(5), randomLong());
5459
String type = randomAlphaOfLength(5);
5560
String action = randomAlphaOfLength(5);
@@ -61,6 +66,6 @@ public static TaskInfo randomTaskInfo() {
6166
Map<String, String> headers = randomBoolean()
6267
? Collections.emptyMap()
6368
: Collections.singletonMap(randomAlphaOfLength(5), randomAlphaOfLength(5));
64-
return new TaskInfo(taskId, type, action, description, null, startTime, runningTimeNanos, cancellable, parentTaskId, headers);
69+
return new TaskInfo(taskId, type, action, description, status, startTime, runningTimeNanos, cancellable, parentTaskId, headers);
6570
}
6671
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
package org.elasticsearch.xpack.enrich.action;
8+
9+
import org.elasticsearch.Version;
10+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
11+
import org.elasticsearch.common.io.stream.Writeable;
12+
import org.elasticsearch.common.settings.Settings;
13+
import org.elasticsearch.tasks.TaskInfo;
14+
import org.elasticsearch.test.AbstractWireSerializingTestCase;
15+
import org.elasticsearch.xpack.core.enrich.action.ExecuteEnrichPolicyStatus;
16+
import org.elasticsearch.xpack.enrich.EnrichPlugin;
17+
18+
import static org.elasticsearch.xpack.enrich.action.EnrichStatsResponseTests.randomTaskInfo;
19+
import static org.hamcrest.Matchers.equalTo;
20+
21+
public class ExecuteEnrichPolicyStatusTests extends AbstractWireSerializingTestCase<ExecuteEnrichPolicyStatus> {
22+
23+
@Override
24+
protected Writeable.Reader<ExecuteEnrichPolicyStatus> instanceReader() {
25+
return ExecuteEnrichPolicyStatus::new;
26+
}
27+
28+
@Override
29+
protected ExecuteEnrichPolicyStatus createTestInstance() {
30+
return new ExecuteEnrichPolicyStatus(randomAlphaOfLengthBetween(2, 8));
31+
}
32+
33+
@Override
34+
protected NamedWriteableRegistry getNamedWriteableRegistry() {
35+
EnrichPlugin enrichPlugin = new EnrichPlugin(Settings.EMPTY);
36+
return new NamedWriteableRegistry(enrichPlugin.getNamedWriteables());
37+
}
38+
39+
public void testEnsureExecuteEnrichPolicyStatusIsRegistered() throws Exception {
40+
TaskInfo testInstance = randomTaskInfo(createTestInstance());
41+
TaskInfo instance = copyWriteable(testInstance, getNamedWriteableRegistry(), TaskInfo::new, Version.CURRENT);
42+
assertThat(testInstance, equalTo(instance));
43+
}
44+
}

0 commit comments

Comments
 (0)