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());
}
}
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")));
}
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));
}
}
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));
}
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));
}
Aggregations