Params withTaskId(TaskId taskId) { if (taskId != null && taskId.isSet()) { return putParam("task_id", taskId.toString()); } return this; }
Params withParentTaskId(TaskId parentTaskId) { if (parentTaskId != null && parentTaskId.isSet()) { return putParam("parent_task_id", parentTaskId.toString()); } return this; }
@Override public String toString() { if (isSet()) { return nodeId + ":" + id; } else { return "unset"; } }
@Override public ActionRequestValidationException validate() { ActionRequestValidationException validationException = null; if (taskId.isSet() && nodes.length > 0) { validationException = addValidationError("task id cannot be used together with node ids", validationException); } return validationException; }
@Override public ActionRequestValidationException validate() { ActionRequestValidationException validationException = null; if (false == getTaskId().isSet()) { validationException = addValidationError("task id is required", validationException); } return validationException; }
private void buildTaskGroups() { Map<TaskId, TaskGroup.Builder> taskGroups = new HashMap<>(); List<TaskGroup.Builder> topLevelTasks = new ArrayList<>(); // First populate all tasks for (TaskInfo taskInfo : this.tasks) { taskGroups.put(taskInfo.getTaskId(), TaskGroup.builder(taskInfo)); } // Now go through all task group builders and add children to their parents for (TaskGroup.Builder taskGroup : taskGroups.values()) { TaskId parentTaskId = taskGroup.getTaskInfo().getParentTaskId(); if (parentTaskId.isSet()) { TaskGroup.Builder parentTask = taskGroups.get(parentTaskId); if (parentTask != null) { // we found parent in the list of tasks - add it to the parent list parentTask.addGroup(taskGroup); } else { // we got zombie or the parent was filtered out - add it to the top task list topLevelTasks.add(taskGroup); } } else { // top level task - add it to the top task list topLevelTasks.add(taskGroup); } } this.groups = Collections.unmodifiableList(topLevelTasks.stream().map(TaskGroup.Builder::build).collect(Collectors.toList())); }
private void registerCancellableTask(Task task) { CancellableTask cancellableTask = (CancellableTask) task; CancellableTaskHolder holder = new CancellableTaskHolder(cancellableTask); CancellableTaskHolder oldHolder = cancellableTasks.put(task.getId(), holder); assert oldHolder == null; // Check if this task was banned before we start it if (task.getParentTaskId().isSet() && banedParents.isEmpty() == false) { String reason = banedParents.get(task.getParentTaskId()); if (reason != null) { try { holder.cancel(reason); throw new IllegalStateException("Task cancelled before it started: " + reason); } finally { // let's clean up the registration unregister(task); } } } }
protected String[] resolveNodes(TasksRequest request, ClusterState clusterState) { if (request.getTaskId().isSet()) { return new String[]{request.getTaskId().getNodeId()}; } else { return clusterState.nodes().resolveNodes(request.getNodes()); } }
protected void processTasks(TasksRequest request, Consumer<OperationTask> operation) { if (request.getTaskId().isSet()) { // we are only checking one task, we can optimize it Task task = taskManager.getTask(request.getTaskId().getId()); if (task != null) { if (request.match(task)) { operation.accept((OperationTask) task); } else { throw new ResourceNotFoundException("task [{}] doesn't support this operation", request.getTaskId()); } } else { throw new ResourceNotFoundException("task [{}] is missing", request.getTaskId()); } } else { for (Task task : taskManager.getTasks().values()) { if (request.match(task)) { operation.accept((OperationTask) task); } } } }
public boolean match(Task task) { if (getActions() != null && getActions().length > 0 && Regex.simpleMatch(getActions(), task.getAction()) == false) { return false; } if (getTaskId().isSet()) { if(getTaskId().getId() != task.getId()) { return false; } } if (parentTaskId.isSet()) { if (parentTaskId.equals(task.getParentTaskId()) == false) { return false; } } return true; } }
protected void processTasks(CancelTasksRequest request, Consumer<CancellableTask> operation) { if (request.getTaskId().isSet()) { // we are only checking one task, we can optimize it CancellableTask task = taskManager.getCancellableTask(request.getTaskId().getId()); if (task != null) { if (request.match(task)) { operation.accept(task); } else { throw new IllegalArgumentException("task [" + request.getTaskId() + "] doesn't support this operation"); } } else { if (taskManager.getTask(request.getTaskId().getId()) != null) { // The task exists, but doesn't support cancellation throw new IllegalArgumentException("task [" + request.getTaskId() + "] doesn't support cancellation"); } else { throw new ResourceNotFoundException("task [{}] doesn't support cancellation", request.getTaskId()); } } } else { for (CancellableTask task : taskManager.getCancellableTasks().values()) { if (request.match(task)) { operation.accept(task); } } } }
@Override public void applyClusterState(ClusterChangedEvent event) { lastDiscoveryNodes = event.state().getNodes(); if (event.nodesRemoved()) { synchronized (banedParents) { lastDiscoveryNodes = event.state().getNodes(); // Remove all bans that were registered by nodes that are no longer in the cluster state Iterator<TaskId> banIterator = banedParents.keySet().iterator(); while (banIterator.hasNext()) { TaskId taskId = banIterator.next(); if (lastDiscoveryNodes.nodeExists(taskId.getNodeId()) == false) { logger.debug("Removing ban for the parent [{}] on the node [{}], reason: the parent node is gone", taskId, event.state().getNodes().getLocalNode()); banIterator.remove(); } } } // Cancel cancellable tasks for the nodes that are gone for (Map.Entry<Long, CancellableTaskHolder> taskEntry : cancellableTasks.entrySet()) { CancellableTaskHolder holder = taskEntry.getValue(); CancellableTask task = holder.getTask(); TaskId parentTaskId = task.getParentTaskId(); if (parentTaskId.isSet() && lastDiscoveryNodes.nodeExists(parentTaskId.getNodeId()) == false) { if (task.cancelOnParentLeaving()) { holder.cancel("Coordinating node [" + parentTaskId.getNodeId() + "] left the cluster"); } } } } }
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.field("node", taskId.getNodeId()); builder.field("id", taskId.getId()); builder.field("type", type); builder.field("action", action); if (status != null) { builder.field("status", status, params); } if (description != null) { builder.field("description", description); } builder.timeField("start_time_in_millis", "start_time", startTime); if (builder.humanReadable()) { builder.field("running_time", new TimeValue(runningTimeNanos, TimeUnit.NANOSECONDS).toString()); } builder.field("running_time_in_nanos", runningTimeNanos); builder.field("cancellable", cancellable); if (parentTaskId.isSet()) { builder.field("parent_task_id", parentTaskId.toString()); } builder.startObject("headers"); for(Map.Entry<String, String> attribute : headers.entrySet()) { builder.field(attribute.getKey(), attribute.getValue()); } builder.endObject(); return builder; }
@Override public String toString() { if (isSet()) { return nodeId + ":" + id; } else { return "unset"; } }
@Override public ActionRequestValidationException validate() { ActionRequestValidationException validationException = null; if (taskId.isSet() && nodes.length > 0) { validationException = addValidationError("task id cannot be used together with node ids", validationException); } return validationException; }
Params withParentTaskId(TaskId parentTaskId) { if (parentTaskId != null && parentTaskId.isSet()) { return putParam("parent_task_id", parentTaskId.toString()); } return this; }
@Override public ActionRequestValidationException validate() { ActionRequestValidationException validationException = null; if (false == getTaskId().isSet()) { validationException = addValidationError("task id is required", validationException); } return validationException; }
Params withTaskId(TaskId taskId) { if (taskId != null && taskId.isSet()) { return putParam("task_id", taskId.toString()); } return this; }
private void buildRow(Table table, boolean fullId, boolean detailed, DiscoveryNodes discoveryNodes, TaskInfo taskInfo) { table.startRow(); String nodeId = taskInfo.getTaskId().getNodeId(); DiscoveryNode node = discoveryNodes.get(nodeId); table.addCell(taskInfo.getId()); table.addCell(taskInfo.getAction()); table.addCell(taskInfo.getTaskId().toString()); if (taskInfo.getParentTaskId().isSet()) { table.addCell(taskInfo.getParentTaskId().toString()); } else { table.addCell("-"); } table.addCell(taskInfo.getType()); table.addCell(taskInfo.getStartTime()); table.addCell(FORMATTER.format(Instant.ofEpochMilli(taskInfo.getStartTime()))); table.addCell(taskInfo.getRunningTimeNanos()); table.addCell(TimeValue.timeValueNanos(taskInfo.getRunningTimeNanos()).toString()); // Node information. Note that the node may be null because it has left the cluster between when we got this response and now. table.addCell(fullId ? nodeId : Strings.substring(nodeId, 0, 4)); table.addCell(node == null ? "-" : node.getHostAddress()); table.addCell(node.getAddress().address().getPort()); table.addCell(node == null ? "-" : node.getName()); table.addCell(node == null ? "-" : node.getVersion().toString()); if (detailed) { table.addCell(taskInfo.getDescription()); } table.endRow(); }
@Override public ActionRequestValidationException validate() { ActionRequestValidationException validationException = null; if (false == getTaskId().isSet()) { validationException = addValidationError("task id is required", validationException); } return validationException; }