Search in sources :

Example 6 with TestParams

use of org.opensearch.persistent.TestPersistentTasksPlugin.TestParams in project OpenSearch by opensearch-project.

the class PersistentTasksCustomMetadataTests method testSerializationContext.

@SuppressWarnings("unchecked")
public void testSerializationContext() throws Exception {
    PersistentTasksCustomMetadata testInstance = createTestInstance();
    for (int i = 0; i < randomInt(10); i++) {
        testInstance = (PersistentTasksCustomMetadata) makeTestChanges(testInstance);
    }
    ToXContent.MapParams params = new ToXContent.MapParams(Collections.singletonMap(Metadata.CONTEXT_MODE_PARAM, randomFrom(CONTEXT_MODE_SNAPSHOT, CONTEXT_MODE_GATEWAY)));
    XContentType xContentType = randomFrom(XContentType.values());
    BytesReference shuffled = toShuffledXContent(testInstance, xContentType, params, false);
    PersistentTasksCustomMetadata newInstance;
    try (XContentParser parser = createParser(XContentFactory.xContent(xContentType), shuffled)) {
        newInstance = doParseInstance(parser);
    }
    assertNotSame(newInstance, testInstance);
    assertEquals(testInstance.tasks().size(), newInstance.tasks().size());
    for (PersistentTask<?> testTask : testInstance.tasks()) {
        PersistentTask<TestParams> newTask = (PersistentTask<TestParams>) newInstance.getTask(testTask.getId());
        assertNotNull(newTask);
        // Things that should be serialized
        assertEquals(testTask.getTaskName(), newTask.getTaskName());
        assertEquals(testTask.getId(), newTask.getId());
        assertEquals(testTask.getState(), newTask.getState());
        assertEquals(testTask.getParams(), newTask.getParams());
        // Things that shouldn't be serialized
        assertEquals(0, newTask.getAllocationId());
        assertNull(newTask.getExecutorNode());
    }
}
Also used : ToXContent(org.opensearch.common.xcontent.ToXContent) BytesReference(org.opensearch.common.bytes.BytesReference) XContentType(org.opensearch.common.xcontent.XContentType) TestParams(org.opensearch.persistent.TestPersistentTasksPlugin.TestParams) XContentParser(org.opensearch.common.xcontent.XContentParser) PersistentTask(org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask)

Example 7 with TestParams

use of org.opensearch.persistent.TestPersistentTasksPlugin.TestParams in project OpenSearch by opensearch-project.

the class PersistentTasksCustomMetadataTests method testMinVersionSerialization.

public void testMinVersionSerialization() throws IOException {
    PersistentTasksCustomMetadata.Builder tasks = PersistentTasksCustomMetadata.builder();
    Version minVersion = allReleasedVersions().stream().filter(Version::isRelease).findFirst().orElseThrow(NoSuchElementException::new);
    final Version streamVersion = randomVersionBetween(random(), minVersion, getPreviousVersion(Version.CURRENT));
    tasks.addTask("test_compatible_version", TestPersistentTasksExecutor.NAME, new TestParams(null, randomVersionBetween(random(), minVersion, streamVersion), randomBoolean() ? Optional.empty() : Optional.of("test")), randomAssignment());
    tasks.addTask("test_incompatible_version", TestPersistentTasksExecutor.NAME, new TestParams(null, randomVersionBetween(random(), compatibleFutureVersion(streamVersion), Version.CURRENT), randomBoolean() ? Optional.empty() : Optional.of("test")), randomAssignment());
    final BytesStreamOutput out = new BytesStreamOutput();
    out.setVersion(streamVersion);
    Set<String> features = new HashSet<>();
    if (randomBoolean()) {
        features.add("test");
    }
    out.setFeatures(features);
    tasks.build().writeTo(out);
    final StreamInput input = out.bytes().streamInput();
    input.setVersion(streamVersion);
    PersistentTasksCustomMetadata read = new PersistentTasksCustomMetadata(new NamedWriteableAwareStreamInput(input, getNamedWriteableRegistry()));
    assertThat(read.taskMap().keySet(), equalTo(Collections.singleton("test_compatible_version")));
}
Also used : VersionUtils.compatibleFutureVersion(org.opensearch.test.VersionUtils.compatibleFutureVersion) Version(org.opensearch.Version) VersionUtils.getPreviousVersion(org.opensearch.test.VersionUtils.getPreviousVersion) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.opensearch.common.io.stream.StreamInput) Builder(org.opensearch.persistent.PersistentTasksCustomMetadata.Builder) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) TestParams(org.opensearch.persistent.TestPersistentTasksPlugin.TestParams) NoSuchElementException(java.util.NoSuchElementException) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) HashSet(java.util.HashSet)

Example 8 with TestParams

use of org.opensearch.persistent.TestPersistentTasksPlugin.TestParams in project OpenSearch by opensearch-project.

the class PersistentTasksNodeServiceTests method testStartTask.

public void testStartTask() {
    PersistentTasksService persistentTasksService = mock(PersistentTasksService.class);
    @SuppressWarnings("unchecked") PersistentTasksExecutor<TestParams> action = mock(PersistentTasksExecutor.class);
    when(action.getExecutor()).thenReturn(ThreadPool.Names.SAME);
    when(action.getTaskName()).thenReturn(TestPersistentTasksExecutor.NAME);
    int nonLocalNodesCount = randomInt(10);
    // need to account for 5 original tasks on each node and their relocations
    for (int i = 0; i < (nonLocalNodesCount + 1) * 10; i++) {
        TaskId parentId = new TaskId("cluster", i);
        when(action.createTask(anyLong(), anyString(), anyString(), eq(parentId), any(), any())).thenReturn(new TestPersistentTasksPlugin.TestTask(i, "persistent", "test", "", parentId, Collections.emptyMap()));
    }
    PersistentTasksExecutorRegistry registry = new PersistentTasksExecutorRegistry(Collections.singletonList(action));
    MockExecutor executor = new MockExecutor();
    PersistentTasksNodeService coordinator = new PersistentTasksNodeService(persistentTasksService, registry, new TaskManager(Settings.EMPTY, threadPool, Collections.emptySet()), executor);
    ClusterState state = createInitialClusterState(nonLocalNodesCount, Settings.EMPTY);
    PersistentTasksCustomMetadata.Builder tasks = PersistentTasksCustomMetadata.builder();
    boolean added = false;
    if (nonLocalNodesCount > 0) {
        for (int i = 0; i < randomInt(5); i++) {
            tasks.addTask(UUIDs.base64UUID(), TestPersistentTasksExecutor.NAME, new TestParams("other_" + i), new Assignment("other_node_" + randomInt(nonLocalNodesCount), "test assignment on other node"));
            if (added == false && randomBoolean()) {
                added = true;
                tasks.addTask(UUIDs.base64UUID(), TestPersistentTasksExecutor.NAME, new TestParams("this_param"), new Assignment("this_node", "test assignment on this node"));
            }
        }
    }
    if (added == false) {
        logger.info("No local node action was added");
    }
    Metadata.Builder metadata = Metadata.builder(state.metadata());
    metadata.putCustom(PersistentTasksCustomMetadata.TYPE, tasks.build());
    ClusterState newClusterState = ClusterState.builder(state).metadata(metadata).build();
    coordinator.clusterChanged(new ClusterChangedEvent("test", newClusterState, state));
    if (added) {
        // ActionType for this node was added, let's make sure it was invoked
        assertThat(executor.executions.size(), equalTo(1));
        // Add task on some other node
        state = newClusterState;
        newClusterState = addTask(state, TestPersistentTasksExecutor.NAME, null, "some_other_node");
        coordinator.clusterChanged(new ClusterChangedEvent("test", newClusterState, state));
        // Make sure action wasn't called again
        assertThat(executor.executions.size(), equalTo(1));
        assertThat(executor.get(0).task.isCompleted(), is(false));
        // Start another task on this node
        state = newClusterState;
        newClusterState = addTask(state, TestPersistentTasksExecutor.NAME, new TestParams("this_param"), "this_node");
        coordinator.clusterChanged(new ClusterChangedEvent("test", newClusterState, state));
        // Make sure action was called this time
        assertThat(executor.size(), equalTo(2));
        assertThat(executor.get(1).task.isCompleted(), is(false));
        // Finish both tasks
        executor.get(0).task.markAsFailed(new RuntimeException());
        executor.get(1).task.markAsCompleted();
        assertThat(executor.get(0).task.isCompleted(), is(true));
        assertThat(executor.get(1).task.isCompleted(), is(true));
        String failedTaskId = executor.get(0).task.getPersistentTaskId();
        String finishedTaskId = executor.get(1).task.getPersistentTaskId();
        executor.clear();
        // Add task on some other node
        state = newClusterState;
        newClusterState = addTask(state, TestPersistentTasksExecutor.NAME, null, "some_other_node");
        coordinator.clusterChanged(new ClusterChangedEvent("test", newClusterState, state));
        // Make sure action wasn't called again
        assertThat(executor.size(), equalTo(0));
        // Simulate reallocation of the failed task on the same node
        state = newClusterState;
        newClusterState = reallocateTask(state, failedTaskId, "this_node");
        coordinator.clusterChanged(new ClusterChangedEvent("test", newClusterState, state));
        // Simulate removal of the finished task
        state = newClusterState;
        newClusterState = removeTask(state, finishedTaskId);
        coordinator.clusterChanged(new ClusterChangedEvent("test", newClusterState, state));
        // Make sure action was only allocated on this node once
        assertThat(executor.size(), equalTo(1));
    }
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) TaskId(org.opensearch.tasks.TaskId) Metadata(org.opensearch.cluster.metadata.Metadata) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) Mockito.anyString(org.mockito.Mockito.anyString) Assignment(org.opensearch.persistent.PersistentTasksCustomMetadata.Assignment) TaskManager(org.opensearch.tasks.TaskManager) TestParams(org.opensearch.persistent.TestPersistentTasksPlugin.TestParams)

Example 9 with TestParams

use of org.opensearch.persistent.TestPersistentTasksPlugin.TestParams in project OpenSearch by opensearch-project.

the class PersistentTasksNodeServiceTests method testParamsStatusAndNodeTaskAreDelegated.

public void testParamsStatusAndNodeTaskAreDelegated() throws Exception {
    PersistentTasksService persistentTasksService = mock(PersistentTasksService.class);
    @SuppressWarnings("unchecked") PersistentTasksExecutor<TestParams> action = mock(PersistentTasksExecutor.class);
    when(action.getExecutor()).thenReturn(ThreadPool.Names.SAME);
    when(action.getTaskName()).thenReturn(TestPersistentTasksExecutor.NAME);
    TaskId parentId = new TaskId("cluster", 1);
    AllocatedPersistentTask nodeTask = new TestPersistentTasksPlugin.TestTask(0, "persistent", "test", "", parentId, Collections.emptyMap());
    when(action.createTask(anyLong(), anyString(), anyString(), eq(parentId), any(), any())).thenReturn(nodeTask);
    PersistentTasksExecutorRegistry registry = new PersistentTasksExecutorRegistry(Collections.singletonList(action));
    MockExecutor executor = new MockExecutor();
    PersistentTasksNodeService coordinator = new PersistentTasksNodeService(persistentTasksService, registry, new TaskManager(Settings.EMPTY, threadPool, Collections.emptySet()), executor);
    ClusterState state = createInitialClusterState(1, Settings.EMPTY);
    PersistentTaskState taskState = new TestPersistentTasksPlugin.State("_test_phase");
    PersistentTasksCustomMetadata.Builder tasks = PersistentTasksCustomMetadata.builder();
    String taskId = UUIDs.base64UUID();
    TestParams taskParams = new TestParams("other_0");
    tasks.addTask(taskId, TestPersistentTasksExecutor.NAME, taskParams, new Assignment("this_node", "test assignment on other node"));
    tasks.updateTaskState(taskId, taskState);
    Metadata.Builder metadata = Metadata.builder(state.metadata());
    metadata.putCustom(PersistentTasksCustomMetadata.TYPE, tasks.build());
    ClusterState newClusterState = ClusterState.builder(state).metadata(metadata).build();
    coordinator.clusterChanged(new ClusterChangedEvent("test", newClusterState, state));
    assertThat(executor.size(), equalTo(1));
    assertThat(executor.get(0).params, sameInstance(taskParams));
    assertThat(executor.get(0).state, sameInstance(taskState));
    assertThat(executor.get(0).task, sameInstance(nodeTask));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) TaskId(org.opensearch.tasks.TaskId) Metadata(org.opensearch.cluster.metadata.Metadata) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) Mockito.anyString(org.mockito.Mockito.anyString) Assignment(org.opensearch.persistent.PersistentTasksCustomMetadata.Assignment) TaskManager(org.opensearch.tasks.TaskManager) ClusterState(org.opensearch.cluster.ClusterState) TestParams(org.opensearch.persistent.TestPersistentTasksPlugin.TestParams)

Example 10 with TestParams

use of org.opensearch.persistent.TestPersistentTasksPlugin.TestParams in project OpenSearch by opensearch-project.

the class PersistentTasksNodeServiceTests method testRegisterTaskFails.

public void testRegisterTaskFails() throws InterruptedException {
    CountDownLatch latch = new CountDownLatch(1);
    final Client mockClient = mock(Client.class);
    final ThreadPool threadPool = mock(ThreadPool.class);
    when(threadPool.getThreadContext()).thenReturn(new ThreadContext(Settings.EMPTY));
    when(threadPool.generic()).thenReturn(OpenSearchExecutors.newDirectExecutorService());
    when(mockClient.threadPool()).thenReturn(threadPool);
    when(mockClient.settings()).thenReturn(Settings.EMPTY);
    PersistentTasksService persistentTasksService = new PersistentTasksService(null, null, mockClient) {

        @Override
        public void sendCompletionRequest(String taskId, long taskAllocationId, Exception taskFailure, ActionListener<PersistentTask<?>> listener) {
            assertThat(taskFailure, instanceOf(RuntimeException.class));
            assertThat(taskFailure.getMessage(), equalTo("Something went wrong"));
            listener.onResponse(mock(PersistentTask.class));
            latch.countDown();
        }
    };
    @SuppressWarnings("unchecked") PersistentTasksExecutor<TestParams> action = mock(PersistentTasksExecutor.class);
    when(action.getExecutor()).thenReturn(ThreadPool.Names.SAME);
    when(action.getTaskName()).thenReturn(TestPersistentTasksExecutor.NAME);
    when(action.createTask(anyLong(), anyString(), anyString(), any(), any(), any())).thenThrow(new RuntimeException("Something went wrong"));
    PersistentTasksExecutorRegistry registry = new PersistentTasksExecutorRegistry(Collections.singletonList(action));
    MockExecutor executor = new MockExecutor();
    PersistentTasksNodeService coordinator = new PersistentTasksNodeService(persistentTasksService, registry, new TaskManager(Settings.EMPTY, threadPool, Collections.emptySet()), executor);
    ClusterState state = createInitialClusterState(0, Settings.EMPTY);
    PersistentTasksCustomMetadata.Builder tasks = PersistentTasksCustomMetadata.builder();
    tasks.addTask(UUIDs.base64UUID(), TestPersistentTasksExecutor.NAME, new TestParams("this_param"), new Assignment("this_node", "test assignment on this node"));
    Metadata.Builder metadata = Metadata.builder(state.metadata());
    metadata.putCustom(PersistentTasksCustomMetadata.TYPE, tasks.build());
    ClusterState newClusterState = ClusterState.builder(state).metadata(metadata).build();
    coordinator.clusterChanged(new ClusterChangedEvent("test", newClusterState, state));
    // Failed to start the task, make sure it wasn't invoked further
    assertThat(executor.executions.size(), equalTo(0));
    assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) ThreadPool(org.opensearch.threadpool.ThreadPool) TestThreadPool(org.opensearch.threadpool.TestThreadPool) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Metadata(org.opensearch.cluster.metadata.Metadata) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) Mockito.anyString(org.mockito.Mockito.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) PersistentTask(org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask) Assignment(org.opensearch.persistent.PersistentTasksCustomMetadata.Assignment) TaskManager(org.opensearch.tasks.TaskManager) ActionListener(org.opensearch.action.ActionListener) TestParams(org.opensearch.persistent.TestPersistentTasksPlugin.TestParams) Client(org.opensearch.client.Client)

Aggregations

TestParams (org.opensearch.persistent.TestPersistentTasksPlugin.TestParams)21 PersistentTask (org.opensearch.persistent.PersistentTasksCustomMetadata.PersistentTask)12 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)8 ClusterState (org.opensearch.cluster.ClusterState)8 TaskInfo (org.opensearch.tasks.TaskInfo)7 Mockito.anyString (org.mockito.Mockito.anyString)6 Metadata (org.opensearch.cluster.metadata.Metadata)6 ClusterChangedEvent (org.opensearch.cluster.ClusterChangedEvent)5 Assignment (org.opensearch.persistent.PersistentTasksCustomMetadata.Assignment)5 ActionListener (org.opensearch.action.ActionListener)4 Settings (org.opensearch.common.settings.Settings)4 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)3 TaskManager (org.opensearch.tasks.TaskManager)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Matchers.equalTo (org.hamcrest.Matchers.equalTo)2 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)2