Skip to content

Commit d6d2edf

Browse files
committed
Fix .tasks index strict mapping: parent_id should be parent_task_id (#48393)
* Fix .tasks index strict mapping: parent_id should be parent_task_id The .tasks index has mappings that's strictly defined. `parent_task_id` was defined as `parent_id` though which would cause an exception in case a task is persisted that has a parent task id set. While at it, a couple of compiler warnings were addressed and a test request builder was removed in favour of using its corresponding request. * increment version
1 parent 9c48ed1 commit d6d2edf

File tree

4 files changed

+62
-77
lines changed

4 files changed

+62
-77
lines changed

server/src/main/java/org/elasticsearch/tasks/TaskResultsService.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555
import java.util.Iterator;
5656
import java.util.Map;
5757

58-
import static org.elasticsearch.common.unit.TimeValue.timeValueMillis;
5958
import static org.elasticsearch.action.admin.cluster.node.tasks.get.GetTaskAction.TASKS_ORIGIN;
59+
import static org.elasticsearch.common.unit.TimeValue.timeValueMillis;
6060

6161
/**
6262
* Service that can store task results.
@@ -73,7 +73,7 @@ public class TaskResultsService {
7373

7474
public static final String TASK_RESULT_MAPPING_VERSION_META_FIELD = "version";
7575

76-
public static final int TASK_RESULT_MAPPING_VERSION = 2;
76+
public static final int TASK_RESULT_MAPPING_VERSION = 3;
7777

7878
/**
7979
* The backoff policy to use when saving a task result fails. The total wait

server/src/main/resources/org/elasticsearch/tasks/task-index-mapping.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"id": {
2020
"type": "long"
2121
},
22-
"parent_id": {
22+
"parent_task_id": {
2323
"type": "keyword"
2424
},
2525
"node": {

server/src/test/java/org/elasticsearch/action/admin/cluster/node/tasks/TasksIT.java

+50-40
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,14 @@ public void testTaskCounts() {
143143
}
144144

145145
public void testMasterNodeOperationTasks() {
146-
registerTaskManageListeners(ClusterHealthAction.NAME);
146+
registerTaskManagerListeners(ClusterHealthAction.NAME);
147147

148148
// First run the health on the master node - should produce only one task on the master node
149149
internalCluster().masterClient().admin().cluster().prepareHealth().get();
150150
assertEquals(1, numberOfEvents(ClusterHealthAction.NAME, Tuple::v1)); // counting only registration events
151151
assertEquals(1, numberOfEvents(ClusterHealthAction.NAME, event -> event.v1() == false)); // counting only unregistration events
152152

153-
resetTaskManageListeners(ClusterHealthAction.NAME);
153+
resetTaskManagerListeners(ClusterHealthAction.NAME);
154154

155155
// Now run the health on a non-master node - should produce one task on master and one task on another node
156156
internalCluster().nonMasterClient().admin().cluster().prepareHealth().get();
@@ -167,8 +167,8 @@ public void testMasterNodeOperationTasks() {
167167
}
168168

169169
public void testTransportReplicationAllShardsTasks() {
170-
registerTaskManageListeners(ValidateQueryAction.NAME); // main task
171-
registerTaskManageListeners(ValidateQueryAction.NAME + "[s]"); // shard
170+
registerTaskManagerListeners(ValidateQueryAction.NAME); // main task
171+
registerTaskManagerListeners(ValidateQueryAction.NAME + "[s]"); // shard
172172
// level
173173
// tasks
174174
createIndex("test");
@@ -186,8 +186,8 @@ public void testTransportReplicationAllShardsTasks() {
186186
}
187187

188188
public void testTransportBroadcastByNodeTasks() {
189-
registerTaskManageListeners(UpgradeAction.NAME); // main task
190-
registerTaskManageListeners(UpgradeAction.NAME + "[n]"); // node level tasks
189+
registerTaskManagerListeners(UpgradeAction.NAME); // main task
190+
registerTaskManagerListeners(UpgradeAction.NAME + "[n]"); // node level tasks
191191
createIndex("test");
192192
ensureGreen("test"); // Make sure all shards are allocated
193193
client().admin().indices().prepareUpgrade("test").get();
@@ -202,8 +202,8 @@ public void testTransportBroadcastByNodeTasks() {
202202
}
203203

204204
public void testTransportReplicationSingleShardTasks() {
205-
registerTaskManageListeners(ValidateQueryAction.NAME); // main task
206-
registerTaskManageListeners(ValidateQueryAction.NAME + "[s]"); // shard level tasks
205+
registerTaskManagerListeners(ValidateQueryAction.NAME); // main task
206+
registerTaskManagerListeners(ValidateQueryAction.NAME + "[s]"); // shard level tasks
207207
createIndex("test");
208208
ensureGreen("test"); // Make sure all shards are allocated
209209
client().admin().indices().prepareValidateQuery("test").get();
@@ -218,9 +218,9 @@ public void testTransportReplicationSingleShardTasks() {
218218

219219

220220
public void testTransportBroadcastReplicationTasks() {
221-
registerTaskManageListeners(RefreshAction.NAME); // main task
222-
registerTaskManageListeners(RefreshAction.NAME + "[s]"); // shard level tasks
223-
registerTaskManageListeners(RefreshAction.NAME + "[s][*]"); // primary and replica shard tasks
221+
registerTaskManagerListeners(RefreshAction.NAME); // main task
222+
registerTaskManagerListeners(RefreshAction.NAME + "[s]"); // shard level tasks
223+
registerTaskManagerListeners(RefreshAction.NAME + "[s][*]"); // primary and replica shard tasks
224224
createIndex("test");
225225
ensureGreen("test"); // Make sure all shards are allocated
226226
client().admin().indices().prepareRefresh("test").get();
@@ -292,10 +292,10 @@ public void testTransportBroadcastReplicationTasks() {
292292
}
293293

294294
public void testTransportBulkTasks() {
295-
registerTaskManageListeners(BulkAction.NAME); // main task
296-
registerTaskManageListeners(BulkAction.NAME + "[s]"); // shard task
297-
registerTaskManageListeners(BulkAction.NAME + "[s][p]"); // shard task on primary
298-
registerTaskManageListeners(BulkAction.NAME + "[s][r]"); // shard task on replica
295+
registerTaskManagerListeners(BulkAction.NAME); // main task
296+
registerTaskManagerListeners(BulkAction.NAME + "[s]"); // shard task
297+
registerTaskManagerListeners(BulkAction.NAME + "[s][p]"); // shard task on primary
298+
registerTaskManagerListeners(BulkAction.NAME + "[s][r]"); // shard task on replica
299299
createIndex("test");
300300
ensureGreen("test"); // Make sure all shards are allocated to catch replication tasks
301301
// ensures the mapping is available on all nodes so we won't retry the request (in case replicas don't have the right mapping).
@@ -345,10 +345,9 @@ public void testTransportBulkTasks() {
345345
assertParentTask(findEvents(BulkAction.NAME + "[s][r]", Tuple::v1), shardTask);
346346
}
347347

348-
349348
public void testSearchTaskDescriptions() {
350-
registerTaskManageListeners(SearchAction.NAME); // main task
351-
registerTaskManageListeners(SearchAction.NAME + "[*]"); // shard task
349+
registerTaskManagerListeners(SearchAction.NAME); // main task
350+
registerTaskManagerListeners(SearchAction.NAME + "[*]"); // shard task
352351
createIndex("test");
353352
ensureGreen("test"); // Make sure all shards are allocated to catch replication tasks
354353
client().prepareIndex("test", "doc", "test_id").setSource("{\"foo\": \"bar\"}", XContentType.JSON)
@@ -494,8 +493,9 @@ public void waitForTaskCompletion(Task task) {
494493
public void testTasksCancellation() throws Exception {
495494
// Start blocking test task
496495
// Get real client (the plugin is not registered on transport nodes)
497-
ActionFuture<TestTaskPlugin.NodesResponse> future = new TestTaskPlugin.NodesRequestBuilder(client(),
498-
TestTaskPlugin.TestTaskAction.INSTANCE).execute();
496+
TestTaskPlugin.NodesRequest request = new TestTaskPlugin.NodesRequest("test");
497+
ActionFuture<TestTaskPlugin.NodesResponse> future = client().execute(TestTaskPlugin.TestTaskAction.INSTANCE, request);
498+
499499
logger.info("--> started test tasks");
500500

501501
// Wait for the task to start on all nodes
@@ -516,8 +516,8 @@ public void testTasksCancellation() throws Exception {
516516

517517
public void testTasksUnblocking() throws Exception {
518518
// Start blocking test task
519-
ActionFuture<TestTaskPlugin.NodesResponse> future =
520-
new TestTaskPlugin.NodesRequestBuilder(client(), TestTaskPlugin.TestTaskAction.INSTANCE).execute();
519+
TestTaskPlugin.NodesRequest request = new TestTaskPlugin.NodesRequest("test");
520+
ActionFuture<TestTaskPlugin.NodesResponse> future = client().execute(TestTaskPlugin.TestTaskAction.INSTANCE, request);
521521
// Wait for the task to start on all nodes
522522
assertBusy(() -> assertEquals(internalCluster().size(),
523523
client().admin().cluster().prepareListTasks().setActions(TestTaskPlugin.TestTaskAction.NAME + "[n]").get().getTasks().size()));
@@ -580,8 +580,9 @@ public void testGetTaskWaitForCompletionWithStoringResult() throws Exception {
580580
private <T> void waitForCompletionTestCase(boolean storeResult, Function<TaskId, ActionFuture<T>> wait, Consumer<T> validator)
581581
throws Exception {
582582
// Start blocking test task
583-
ActionFuture<TestTaskPlugin.NodesResponse> future = new TestTaskPlugin.NodesRequestBuilder(client(),
584-
TestTaskPlugin.TestTaskAction.INSTANCE).setShouldStoreResult(storeResult).execute();
583+
TestTaskPlugin.NodesRequest request = new TestTaskPlugin.NodesRequest("test");
584+
request.setShouldStoreResult(storeResult);
585+
ActionFuture<TestTaskPlugin.NodesResponse> future = client().execute(TestTaskPlugin.TestTaskAction.INSTANCE, request);
585586

586587
ActionFuture<T> waitResponseFuture;
587588
TaskId taskId;
@@ -654,8 +655,8 @@ public void testGetTaskWaitForTimeout() throws Exception {
654655
*/
655656
private void waitForTimeoutTestCase(Function<TaskId, ? extends Iterable<? extends Throwable>> wait) throws Exception {
656657
// Start blocking test task
657-
ActionFuture<TestTaskPlugin.NodesResponse> future = new TestTaskPlugin.NodesRequestBuilder(client(),
658-
TestTaskPlugin.TestTaskAction.INSTANCE).execute();
658+
TestTaskPlugin.NodesRequest request = new TestTaskPlugin.NodesRequest("test");
659+
ActionFuture<TestTaskPlugin.NodesResponse> future = client().execute(TestTaskPlugin.TestTaskAction.INSTANCE, request);
659660
try {
660661
TaskId taskId = waitForTestTaskStartOnAllNodes();
661662

@@ -722,12 +723,17 @@ public void testTasksWaitForAllTask() throws Exception {
722723
assertThat(response.getTasks().size(), greaterThanOrEqualTo(1));
723724
}
724725

725-
public void testTaskStoringSuccesfulResult() throws Exception {
726-
registerTaskManageListeners(TestTaskPlugin.TestTaskAction.NAME); // we need this to get task id of the process
726+
public void testTaskStoringSuccessfulResult() throws Exception {
727+
registerTaskManagerListeners(TestTaskPlugin.TestTaskAction.NAME); // we need this to get task id of the process
727728

728729
// Start non-blocking test task
729-
new TestTaskPlugin.NodesRequestBuilder(client(), TestTaskPlugin.TestTaskAction.INSTANCE)
730-
.setShouldStoreResult(true).setShouldBlock(false).get();
730+
TestTaskPlugin.NodesRequest request = new TestTaskPlugin.NodesRequest("test");
731+
request.setShouldStoreResult(true);
732+
request.setShouldBlock(false);
733+
TaskId parentTaskId = new TaskId("parent_node", randomLong());
734+
request.setParentTask(parentTaskId);
735+
736+
client().execute(TestTaskPlugin.TestTaskAction.INSTANCE, request).get();
731737

732738
List<TaskInfo> events = findEvents(TestTaskPlugin.TestTaskAction.NAME, Tuple::v1);
733739

@@ -741,6 +747,7 @@ public void testTaskStoringSuccesfulResult() throws Exception {
741747
assertNull(taskResult.getError());
742748

743749
assertEquals(taskInfo.getTaskId(), taskResult.getTask().getTaskId());
750+
assertEquals(taskInfo.getParentTaskId(), taskResult.getTask().getParentTaskId());
744751
assertEquals(taskInfo.getType(), taskResult.getTask().getType());
745752
assertEquals(taskInfo.getAction(), taskResult.getTask().getAction());
746753
assertEquals(taskInfo.getDescription(), taskResult.getTask().getDescription());
@@ -770,14 +777,16 @@ public void testTaskStoringSuccesfulResult() throws Exception {
770777
}
771778

772779
public void testTaskStoringFailureResult() throws Exception {
773-
registerTaskManageListeners(TestTaskPlugin.TestTaskAction.NAME); // we need this to get task id of the process
780+
registerTaskManagerListeners(TestTaskPlugin.TestTaskAction.NAME); // we need this to get task id of the process
781+
782+
TestTaskPlugin.NodesRequest request = new TestTaskPlugin.NodesRequest("test");
783+
request.setShouldFail(true);
784+
request.setShouldStoreResult(true);
785+
request.setShouldBlock(false);
774786

775787
// Start non-blocking test task that should fail
776788
assertThrows(
777-
new TestTaskPlugin.NodesRequestBuilder(client(), TestTaskPlugin.TestTaskAction.INSTANCE)
778-
.setShouldFail(true)
779-
.setShouldStoreResult(true)
780-
.setShouldBlock(false),
789+
client().execute(TestTaskPlugin.TestTaskAction.INSTANCE, request),
781790
IllegalStateException.class
782791
);
783792

@@ -858,7 +867,7 @@ public void tearDown() throws Exception {
858867
/**
859868
* Registers recording task event listeners with the given action mask on all nodes
860869
*/
861-
private void registerTaskManageListeners(String actionMasks) {
870+
private void registerTaskManagerListeners(String actionMasks) {
862871
for (String nodeName : internalCluster().getNodeNames()) {
863872
DiscoveryNode node = internalCluster().getInstance(ClusterService.class, nodeName).localNode();
864873
RecordingTaskManagerListener listener = new RecordingTaskManagerListener(node.getId(), actionMasks.split(","));
@@ -871,7 +880,7 @@ private void registerTaskManageListeners(String actionMasks) {
871880
/**
872881
* Resets all recording task event listeners with the given action mask on all nodes
873882
*/
874-
private void resetTaskManageListeners(String actionMasks) {
883+
private void resetTaskManagerListeners(String actionMasks) {
875884
for (Map.Entry<Tuple<String, String>, RecordingTaskManagerListener> entry : listeners.entrySet()) {
876885
if (actionMasks == null || entry.getKey().v2().equals(actionMasks)) {
877886
entry.getValue().reset();
@@ -925,11 +934,12 @@ private void assertParentTask(TaskInfo task, TaskInfo parentTask) {
925934
assertEquals(parentTask.getId(), task.getParentTaskId().getId());
926935
}
927936

928-
private ResourceNotFoundException expectNotFound(ThrowingRunnable r) {
937+
private void expectNotFound(ThrowingRunnable r) {
929938
Exception e = expectThrows(Exception.class, r);
930939
ResourceNotFoundException notFound = (ResourceNotFoundException) ExceptionsHelper.unwrap(e, ResourceNotFoundException.class);
931-
if (notFound == null) throw new RuntimeException("Expected ResourceNotFoundException", e);
932-
return notFound;
940+
if (notFound == null) {
941+
throw new AssertionError("Expected " + ResourceNotFoundException.class.getSimpleName(), e);
942+
}
933943
}
934944

935945
/**

server/src/test/java/org/elasticsearch/action/admin/cluster/node/tasks/TestTaskPlugin.java

+9-34
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.elasticsearch.action.support.nodes.BaseNodeResponse;
3232
import org.elasticsearch.action.support.nodes.BaseNodesRequest;
3333
import org.elasticsearch.action.support.nodes.BaseNodesResponse;
34-
import org.elasticsearch.action.support.nodes.NodesOperationRequestBuilder;
3534
import org.elasticsearch.action.support.nodes.TransportNodesAction;
3635
import org.elasticsearch.action.support.tasks.BaseTasksRequest;
3736
import org.elasticsearch.action.support.tasks.BaseTasksResponse;
@@ -138,11 +137,11 @@ public NodeResponse(DiscoveryNode node) {
138137

139138
public static class NodesResponse extends BaseNodesResponse<NodeResponse> implements ToXContentFragment {
140139

141-
public NodesResponse(StreamInput in) throws IOException {
140+
NodesResponse(StreamInput in) throws IOException {
142141
super(in);
143142
}
144143

145-
public NodesResponse(ClusterName clusterName, List<NodeResponse> nodes, List<FailedNodeException> failures) {
144+
NodesResponse(ClusterName clusterName, List<NodeResponse> nodes, List<FailedNodeException> failures) {
146145
super(clusterName, nodes, failures);
147146
}
148147

@@ -168,8 +167,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
168167
}
169168

170169
public static class NodeRequest extends BaseNodeRequest {
171-
protected String requestName;
172-
protected boolean shouldBlock;
170+
protected final String requestName;
171+
protected final boolean shouldBlock;
173172

174173
public NodeRequest(StreamInput in) throws IOException {
175174
super(in);
@@ -214,7 +213,7 @@ public static class NodesRequest extends BaseNodesRequest<NodesRequest> {
214213
shouldFail = in.readBoolean();
215214
}
216215

217-
public NodesRequest(String requestName, String... nodesIds) {
216+
NodesRequest(String requestName, String... nodesIds) {
218217
super(nodesIds);
219218
this.requestName = requestName;
220219
}
@@ -336,37 +335,13 @@ private TestTaskAction() {
336335
}
337336
}
338337

339-
public static class NodesRequestBuilder extends NodesOperationRequestBuilder<NodesRequest, NodesResponse, NodesRequestBuilder> {
340-
341-
protected NodesRequestBuilder(ElasticsearchClient client, ActionType<NodesResponse> action) {
342-
super(client, action, new NodesRequest("test"));
343-
}
344-
345-
346-
public NodesRequestBuilder setShouldStoreResult(boolean shouldStoreResult) {
347-
request().setShouldStoreResult(shouldStoreResult);
348-
return this;
349-
}
350-
351-
public NodesRequestBuilder setShouldBlock(boolean shouldBlock) {
352-
request().setShouldBlock(shouldBlock);
353-
return this;
354-
}
355-
356-
public NodesRequestBuilder setShouldFail(boolean shouldFail) {
357-
request().setShouldFail(shouldFail);
358-
return this;
359-
}
360-
}
361-
362-
363338
public static class UnblockTestTaskResponse implements Writeable {
364339

365-
public UnblockTestTaskResponse() {
340+
UnblockTestTaskResponse() {
366341

367342
}
368343

369-
public UnblockTestTaskResponse(StreamInput in) {
344+
UnblockTestTaskResponse(StreamInput in) {
370345
}
371346

372347
@Override
@@ -393,13 +368,13 @@ public static class UnblockTestTasksResponse extends BaseTasksResponse {
393368

394369
private List<UnblockTestTaskResponse> tasks;
395370

396-
public UnblockTestTasksResponse(List<UnblockTestTaskResponse> tasks, List<TaskOperationFailure> taskFailures, List<? extends
371+
UnblockTestTasksResponse(List<UnblockTestTaskResponse> tasks, List<TaskOperationFailure> taskFailures, List<? extends
397372
FailedNodeException> nodeFailures) {
398373
super(taskFailures, nodeFailures);
399374
this.tasks = tasks == null ? Collections.emptyList() : Collections.unmodifiableList(new ArrayList<>(tasks));
400375
}
401376

402-
public UnblockTestTasksResponse(StreamInput in) throws IOException {
377+
UnblockTestTasksResponse(StreamInput in) throws IOException {
403378
super(in);
404379
int taskCount = in.readVInt();
405380
List<UnblockTestTaskResponse> builder = new ArrayList<>();

0 commit comments

Comments
 (0)