protected TaskManager createTaskManager(Settings settings, ThreadPool threadPool, Set<String> taskHeaders) { return new TaskManager(settings, threadPool, taskHeaders); }
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 void processMessageReceived(Request request, TransportChannel channel) throws Exception { final Task task = taskManager.register(channel.getChannelType(), action, request); if (task == null) { handler.messageReceived(request, channel); } else { boolean success = false; try { handler.messageReceived(request, new TaskTransportChannel(taskManager, task, channel), task); success = true; } finally { if (success == false) { taskManager.unregister(task); } } } }
@Override public void messageReceived(final BanParentTaskRequest request, final TransportChannel channel) throws Exception { if (request.ban) { logger.debug("Received ban for the parent [{}] on the node [{}], reason: [{}]", request.parentTaskId, clusterService.localNode().getId(), request.reason); taskManager.setBan(request.parentTaskId, request.reason); } else { logger.debug("Removing ban for the parent [{}] on the node [{}]", request.parentTaskId, clusterService.localNode().getId()); taskManager.removeBan(request.parentTaskId); } channel.sendResponse(TransportResponse.Empty.INSTANCE); } }
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); } } } }
private void endTask() { taskManager.unregister(task); } }
/** * Execute the transport action on the local node, returning the {@link Task} used to track its execution and accepting a * {@link TaskListener} which listens for the completion of the action. */ public final Task execute(Request request, TaskListener<Response> listener) { Task task = taskManager.register("transport", actionName, request); execute(task, request, new ActionListener<Response>() { @Override public void onResponse(Response response) { if (task != null) { taskManager.unregister(task); } listener.onResponse(task, response); } @Override public void onFailure(Exception e) { if (task != null) { taskManager.unregister(task); } listener.onFailure(task, e); } }); return task; }
Scanner kb = new Scanner(System.in); ArrayList<Task> list = new ArrayList<Task>(); TaskManager taskMgr = new TaskManager(); String choice = kb.next(); if (choice.equalsIgnoreCase("a")) { taskMgr.makeTasks(); String date=kb.nextLine(); Date dueDate = sdf.parse(date); taskMgr.findTasksByDate(dueDate); taskMgr.displayTaskReportByDate(dueDate); String keyword=kb.nextLine(); System.out.println("Task that contain the term "+keyword); taskMgr.findTasksByKeyword(keyword); taskMgr.displayTaskReportByKeyword(keyword);
/** * Blocks the calling thread, waiting for the task to vanish from the TaskManager. */ public void waitForTaskCompletion(Task task, long untilInNanos) { while (System.nanoTime() - untilInNanos < 0) { if (getTask(task.getId()) == null) { return; } try { Thread.sleep(WAIT_FOR_COMPLETION_POLL.millis()); } catch (InterruptedException e) { throw new ElasticsearchException("Interrupted waiting for completion of [{}]", e, task); } } throw new ElasticsearchTimeoutException("Timed out waiting for completion of [{}]", task); }
/** * Returns a task with given id, or null if the task is not found. */ public Task getTask(long id) { Task task = tasks.get(id); if (task != null) { return task; } else { return getCancellableTask(id); } }
DiscoveryNodes childNodes = clusterService.state().nodes(); final BanLock banLock = new BanLock(childNodes.getSize(), () -> removeBanOnNodes(cancellableTask, childNodes)); canceled = taskManager.cancel(cancellableTask, request.getReason(), banLock::onTaskFinished); if (canceled) { canceled = taskManager.cancel(cancellableTask, request.getReason(), () -> listener.onResponse(cancellableTask.taskInfo(nodeId, false))); if (canceled) {
private void assertShardIndexCounter() throws Exception { assertBusy(() -> { final Collection<NodeAndClient> nodesAndClients = nodes.values(); for (NodeAndClient nodeAndClient : nodesAndClients) { IndicesService indexServices = getInstance(IndicesService.class, nodeAndClient.name); for (IndexService indexService : indexServices) { for (IndexShard indexShard : indexService) { int activeOperationsCount = indexShard.getActiveOperationsCount(); if (activeOperationsCount > 0) { TaskManager taskManager = getInstance(TransportService.class, nodeAndClient.name).getTaskManager(); DiscoveryNode localNode = getInstance(ClusterService.class, nodeAndClient.name).localNode(); List<TaskInfo> taskInfos = taskManager.getTasks().values().stream() .filter(task -> task instanceof ReplicationTask) .map(task -> task.taskInfo(localNode.getId(), true)) .collect(Collectors.toList()); ListTasksResponse response = new ListTasksResponse(taskInfos, Collections.emptyList(), Collections.emptyList()); try { XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint().value(response); throw new AssertionError("expected index shard counter on shard " + indexShard.shardId() + " on node " + nodeAndClient.name + " to be 0 but was " + activeOperationsCount + ". Current replication tasks on node:\n" + builder.string()); } catch (IOException e) { throw new RuntimeException("caught exception while building response [" + response + "]", e); } } } } } }); }
@Override public void onResponse(Response response) { taskManager.unregister(task); listener.onResponse(response); }
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); } } } }
Task task = taskManager.register("transport", actionName, request); if (task == null) { execute(null, request, listener);
@Override public void messageReceived(final BanParentTaskRequest request, final TransportChannel channel) throws Exception { if (request.ban) { logger.debug("Received ban for the parent [{}] on the node [{}], reason: [{}]", request.parentTaskId, clusterService.localNode().getId(), request.reason); taskManager.setBan(request.parentTaskId, request.reason); } else { logger.debug("Removing ban for the parent [{}] on the node [{}]", request.parentTaskId, clusterService.localNode().getId()); taskManager.removeBan(request.parentTaskId); } channel.sendResponse(TransportResponse.Empty.INSTANCE); } }
/** * Executed on the node that should be running the task to find and return the running task. Falls back to * {@link #getFinishedTaskFromIndex(Task, GetTaskRequest, ActionListener)} if the task isn't still running. */ void getRunningTaskFromNode(Task thisTask, GetTaskRequest request, ActionListener<GetTaskResponse> listener) { Task runningTask = taskManager.getTask(request.getTaskId().getId()); if (runningTask == null) { // Task isn't running, go look in the task index getFinishedTaskFromIndex(thisTask, request, listener); } else { if (request.getWaitForCompletion()) { // Shift to the generic thread pool and let it wait for the task to complete so we don't block any important threads. threadPool.generic().execute(new AbstractRunnable() { @Override protected void doRun() throws Exception { taskManager.waitForTaskCompletion(runningTask, waitForCompletionTimeout(request.getTimeout())); waitedForCompletion(thisTask, request, runningTask.taskInfo(clusterService.localNode().getId(), true), listener); } @Override public void onFailure(Exception e) { listener.onFailure(e); } }); } else { TaskInfo info = runningTask.taskInfo(clusterService.localNode().getId(), true); listener.onResponse(new GetTaskResponse(new TaskResult(false, info))); } } }
/** * Returns a task with given id, or null if the task is not found. */ public Task getTask(long id) { Task task = tasks.get(id); if (task != null) { return task; } else { return getCancellableTask(id); } }
DiscoveryNodes childNodes = clusterService.state().nodes(); final BanLock banLock = new BanLock(childNodes.getSize(), () -> removeBanOnNodes(cancellableTask, childNodes)); canceled = taskManager.cancel(cancellableTask, request.getReason(), banLock::onTaskFinished); if (canceled) { canceled = taskManager.cancel(cancellableTask, request.getReason(), () -> listener.onResponse(cancellableTask.taskInfo(nodeId, false))); if (canceled) {
@Override public void onResponse(Response response) { if (task != null) { taskManager.unregister(task); } listener.onResponse(task, response); }