Search in sources :

Example 1 with TaskAwareRequest

use of org.opensearch.tasks.TaskAwareRequest in project OpenSearch by opensearch-project.

the class PersistentTasksNodeService method startTask.

private <Params extends PersistentTaskParams> void startTask(PersistentTask<Params> taskInProgress) {
    PersistentTasksExecutor<Params> executor = persistentTasksExecutorRegistry.getPersistentTaskExecutorSafe(taskInProgress.getTaskName());
    TaskAwareRequest request = new TaskAwareRequest() {

        TaskId parentTaskId = new TaskId("cluster", taskInProgress.getAllocationId());

        @Override
        public void setParentTask(TaskId taskId) {
            throw new UnsupportedOperationException("parent task if for persistent tasks shouldn't change");
        }

        @Override
        public TaskId getParentTask() {
            return parentTaskId;
        }

        @Override
        public Task createTask(long id, String type, String action, TaskId parentTaskId, Map<String, String> headers) {
            return executor.createTask(id, type, action, parentTaskId, taskInProgress, headers);
        }
    };
    AllocatedPersistentTask task;
    try {
        task = (AllocatedPersistentTask) taskManager.register("persistent", taskInProgress.getTaskName() + "[c]", request);
    } catch (Exception e) {
        logger.error("Fatal error registering persistent task [" + taskInProgress.getTaskName() + "] with id [" + taskInProgress.getId() + "] and allocation id [" + taskInProgress.getAllocationId() + "], removing from persistent tasks", e);
        notifyMasterOfFailedTask(taskInProgress, e);
        return;
    }
    boolean processed = false;
    try {
        task.init(persistentTasksService, taskManager, taskInProgress.getId(), taskInProgress.getAllocationId());
        logger.trace("Persistent task [{}] with id [{}] and allocation id [{}] was created", task.getAction(), task.getPersistentTaskId(), task.getAllocationId());
        try {
            runningTasks.put(taskInProgress.getAllocationId(), task);
            nodePersistentTasksExecutor.executeTask(taskInProgress.getParams(), taskInProgress.getState(), task, executor);
        } catch (Exception e) {
            // Submit task failure
            task.markAsFailed(e);
        }
        processed = true;
    } finally {
        if (processed == false) {
            // something went wrong - unregistering task
            logger.warn("Persistent task [{}] with id [{}] and allocation id [{}] failed to create", task.getAction(), task.getPersistentTaskId(), task.getAllocationId());
            taskManager.unregister(task);
        }
    }
}
Also used : TaskId(org.opensearch.tasks.TaskId) TaskAwareRequest(org.opensearch.tasks.TaskAwareRequest) HashMap(java.util.HashMap) Map(java.util.Map) IOException(java.io.IOException)

Example 2 with TaskAwareRequest

use of org.opensearch.tasks.TaskAwareRequest in project OpenSearch by opensearch-project.

the class TransportServiceDeserializationFailureTests method testDeserializationFailureLogIdentifiesListener.

public void testDeserializationFailureLogIdentifiesListener() {
    final DiscoveryNode localNode = new DiscoveryNode("local", buildNewFakeTransportAddress(), Version.CURRENT);
    final DiscoveryNode otherNode = new DiscoveryNode("other", buildNewFakeTransportAddress(), Version.CURRENT);
    final Settings settings = Settings.builder().put(NODE_NAME_SETTING.getKey(), "local").build();
    final DeterministicTaskQueue deterministicTaskQueue = new DeterministicTaskQueue(settings, random());
    final String testActionName = "internal:test-action";
    final MockTransport transport = new MockTransport() {

        @Override
        protected void onSendRequest(long requestId, String action, TransportRequest request, DiscoveryNode node) {
            if (action.equals(TransportService.HANDSHAKE_ACTION_NAME)) {
                handleResponse(requestId, new TransportService.HandshakeResponse(otherNode, new ClusterName(""), Version.CURRENT));
            }
        }
    };
    final TransportService transportService = transport.createTransportService(Settings.EMPTY, deterministicTaskQueue.getThreadPool(), TransportService.NOOP_TRANSPORT_INTERCEPTOR, ignored -> localNode, null, Collections.emptySet());
    transportService.registerRequestHandler(testActionName, ThreadPool.Names.SAME, TransportRequest.Empty::new, (request, channel, task) -> channel.sendResponse(TransportResponse.Empty.INSTANCE));
    transportService.start();
    transportService.acceptIncomingRequests();
    final PlainActionFuture<Void> connectionFuture = new PlainActionFuture<>();
    transportService.connectToNode(otherNode, connectionFuture);
    assertTrue(connectionFuture.isDone());
    {
        // requests without a parent task are recorded directly in the response context
        transportService.sendRequest(otherNode, testActionName, TransportRequest.Empty.INSTANCE, TransportRequestOptions.EMPTY, new TransportResponseHandler<TransportResponse.Empty>() {

            @Override
            public void handleResponse(TransportResponse.Empty response) {
                fail("should not be called");
            }

            @Override
            public void handleException(TransportException exp) {
                fail("should not be called");
            }

            @Override
            public String executor() {
                return ThreadPool.Names.SAME;
            }

            @Override
            public TransportResponse.Empty read(StreamInput in) {
                throw new AssertionError("should not be called");
            }

            @Override
            public String toString() {
                return "test handler without parent";
            }
        });
        final List<Transport.ResponseContext<? extends TransportResponse>> responseContexts = transport.getResponseHandlers().prune(ignored -> true);
        assertThat(responseContexts, hasSize(1));
        final TransportResponseHandler<? extends TransportResponse> handler = responseContexts.get(0).handler();
        assertThat(handler, hasToString(containsString("test handler without parent")));
    }
    {
        // requests with a parent task get wrapped up by the transport service, including the action name
        final Task parentTask = transportService.getTaskManager().register("test", "test-action", new TaskAwareRequest() {

            @Override
            public void setParentTask(TaskId taskId) {
                fail("should not be called");
            }

            @Override
            public TaskId getParentTask() {
                return TaskId.EMPTY_TASK_ID;
            }
        });
        transportService.sendChildRequest(otherNode, testActionName, TransportRequest.Empty.INSTANCE, parentTask, TransportRequestOptions.EMPTY, new TransportResponseHandler<TransportResponse.Empty>() {

            @Override
            public void handleResponse(TransportResponse.Empty response) {
                fail("should not be called");
            }

            @Override
            public void handleException(TransportException exp) {
                fail("should not be called");
            }

            @Override
            public String executor() {
                return ThreadPool.Names.SAME;
            }

            @Override
            public TransportResponse.Empty read(StreamInput in) {
                throw new AssertionError("should not be called");
            }

            @Override
            public String toString() {
                return "test handler with parent";
            }
        });
        final List<Transport.ResponseContext<? extends TransportResponse>> responseContexts = transport.getResponseHandlers().prune(ignored -> true);
        assertThat(responseContexts, hasSize(1));
        final TransportResponseHandler<? extends TransportResponse> handler = responseContexts.get(0).handler();
        assertThat(handler, hasToString(allOf(containsString("test handler with parent"), containsString(testActionName))));
    }
}
Also used : StreamInput(org.opensearch.common.io.stream.StreamInput) Matchers.hasToString(org.hamcrest.Matchers.hasToString) DeterministicTaskQueue(org.opensearch.cluster.coordination.DeterministicTaskQueue) Matchers.allOf(org.hamcrest.Matchers.allOf) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) ThreadPool(org.opensearch.threadpool.ThreadPool) TaskId(org.opensearch.tasks.TaskId) Version(org.opensearch.Version) Settings(org.opensearch.common.settings.Settings) Task(org.opensearch.tasks.Task) TaskAwareRequest(org.opensearch.tasks.TaskAwareRequest) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) List(java.util.List) NODE_NAME_SETTING(org.opensearch.node.Node.NODE_NAME_SETTING) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) MockTransport(org.opensearch.test.transport.MockTransport) ClusterName(org.opensearch.cluster.ClusterName) Matchers.hasSize(org.hamcrest.Matchers.hasSize) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Task(org.opensearch.tasks.Task) TaskId(org.opensearch.tasks.TaskId) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) TaskAwareRequest(org.opensearch.tasks.TaskAwareRequest) DeterministicTaskQueue(org.opensearch.cluster.coordination.DeterministicTaskQueue) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) MockTransport(org.opensearch.test.transport.MockTransport) StreamInput(org.opensearch.common.io.stream.StreamInput) ClusterName(org.opensearch.cluster.ClusterName) List(java.util.List) MockTransport(org.opensearch.test.transport.MockTransport) Settings(org.opensearch.common.settings.Settings)

Aggregations

TaskAwareRequest (org.opensearch.tasks.TaskAwareRequest)2 TaskId (org.opensearch.tasks.TaskId)2 IOException (java.io.IOException)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Matchers.allOf (org.hamcrest.Matchers.allOf)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.hasSize (org.hamcrest.Matchers.hasSize)1 Matchers.hasToString (org.hamcrest.Matchers.hasToString)1 Version (org.opensearch.Version)1 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)1 ClusterName (org.opensearch.cluster.ClusterName)1 DeterministicTaskQueue (org.opensearch.cluster.coordination.DeterministicTaskQueue)1 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)1 StreamInput (org.opensearch.common.io.stream.StreamInput)1 Settings (org.opensearch.common.settings.Settings)1 NODE_NAME_SETTING (org.opensearch.node.Node.NODE_NAME_SETTING)1 Task (org.opensearch.tasks.Task)1