use of org.opensearch.cluster.node.DiscoveryNode in project asynchronous-search by opensearch-project.
the class AsynchronousSearchServiceUpdateContextTests method testUpdateContextPermitAcquisitionFailure.
public void testUpdateContextPermitAcquisitionFailure() throws InterruptedException {
DiscoveryNode discoveryNode = new DiscoveryNode("node", OpenSearchTestCase.buildNewFakeTransportAddress(), emptyMap(), DiscoveryNodeRole.BUILT_IN_ROLES, Version.CURRENT);
ThreadPool testThreadPool = null;
try {
testThreadPool = new TestThreadPool(OPEN_DISTRO_ASYNC_SEARCH_GENERIC_THREAD_POOL_NAME, executorBuilder);
ClusterService mockClusterService = getClusterService(discoveryNode, testThreadPool);
MockClient mockClient = new MockClient(testThreadPool);
AsynchronousSearchActiveStore mockStore = mock(AsynchronousSearchActiveStore.class);
AsynchronousSearchPersistenceService persistenceService = new AsynchronousSearchPersistenceService(mockClient, mockClusterService, testThreadPool);
AsynchronousSearchService asService = new AsynchronousSearchService(persistenceService, mockStore, mockClient, mockClusterService, testThreadPool, new InternalAsynchronousSearchStats(), new NamedWriteableRegistry(emptyList()));
TimeValue keepAlive = timeValueHours(9);
boolean keepOnCompletion = true;
User user1 = randomBoolean() ? randomUser() : null;
SearchRequest searchRequest = new SearchRequest();
SubmitAsynchronousSearchRequest submitAsynchronousSearchRequest = new SubmitAsynchronousSearchRequest(searchRequest);
submitAsynchronousSearchRequest.keepOnCompletion(keepOnCompletion);
submitAsynchronousSearchRequest.keepAlive(keepAlive);
AsynchronousSearchProgressListener asProgressListener = mockAsynchronousSearchProgressListener(testThreadPool);
AsynchronousSearchContextId asContextId = new AsynchronousSearchContextId(UUID.randomUUID().toString(), randomNonNegativeLong());
MockAsynchronousSearchActiveContext asActiveContext = new MockAsynchronousSearchActiveContext(asContextId, discoveryNode.getId(), keepAlive, true, testThreadPool, testThreadPool::absoluteTimeInMillis, asProgressListener, user1);
// bootstrap search
AsynchronousSearchTask task = new AsynchronousSearchTask(randomNonNegativeLong(), "transport", SearchAction.NAME, TaskId.EMPTY_TASK_ID, emptyMap(), asActiveContext, null, (c) -> {
});
asActiveContext.setTask(task);
simulateIsAlive = true;
simulateUncheckedException = true;
long oldExpiration = asActiveContext.getExpirationTimeMillis();
when(mockStore.getContext(asContextId)).thenReturn(Optional.of(asActiveContext));
CountDownLatch latch = new CountDownLatch(1);
asService.updateKeepAliveAndGetContext(asActiveContext.getAsynchronousSearchId(), timeValueHours(9), asActiveContext.getContextId(), randomUser(), new LatchedActionListener<>(wrap(r -> assertEquals("active context should not have been updated on permit acquisition failure", asActiveContext.getExpirationTimeMillis(), oldExpiration), e -> fail("expected update to succeed but got " + e.getMessage())), latch));
latch.await();
assertEquals("update should have been attempted on index", mockClient.updateCount.intValue(), 1);
mockClusterService.stop();
} finally {
ThreadPool.terminate(testThreadPool, 30, TimeUnit.SECONDS);
}
}
use of org.opensearch.cluster.node.DiscoveryNode in project asynchronous-search by opensearch-project.
the class AsynchronousSearchServiceUpdateContextTests method testUpdateContextPermitAcquisitionFailureKeepOnCompletionFalse.
public void testUpdateContextPermitAcquisitionFailureKeepOnCompletionFalse() throws InterruptedException {
DiscoveryNode discoveryNode = new DiscoveryNode("node", OpenSearchTestCase.buildNewFakeTransportAddress(), emptyMap(), DiscoveryNodeRole.BUILT_IN_ROLES, Version.CURRENT);
ThreadPool testThreadPool = null;
try {
testThreadPool = new TestThreadPool(OPEN_DISTRO_ASYNC_SEARCH_GENERIC_THREAD_POOL_NAME, executorBuilder);
ClusterService mockClusterService = getClusterService(discoveryNode, testThreadPool);
MockClient mockClient = new MockClient(testThreadPool);
AsynchronousSearchActiveStore mockStore = mock(AsynchronousSearchActiveStore.class);
AsynchronousSearchPersistenceService persistenceService = new AsynchronousSearchPersistenceService(mockClient, mockClusterService, testThreadPool);
AsynchronousSearchService asService = new AsynchronousSearchService(persistenceService, mockStore, mockClient, mockClusterService, testThreadPool, new InternalAsynchronousSearchStats(), new NamedWriteableRegistry(emptyList()));
TimeValue keepAlive = timeValueHours(9);
boolean keepOnCompletion = false;
User user1 = randomBoolean() ? randomUser() : null;
SearchRequest searchRequest = new SearchRequest();
SubmitAsynchronousSearchRequest submitAsynchronousSearchRequest = new SubmitAsynchronousSearchRequest(searchRequest);
submitAsynchronousSearchRequest.keepOnCompletion(keepOnCompletion);
submitAsynchronousSearchRequest.keepAlive(keepAlive);
AsynchronousSearchProgressListener asProgressListener = mockAsynchronousSearchProgressListener(testThreadPool);
AsynchronousSearchContextId asContextId = new AsynchronousSearchContextId(UUID.randomUUID().toString(), randomNonNegativeLong());
MockAsynchronousSearchActiveContext asActiveContext = new MockAsynchronousSearchActiveContext(asContextId, discoveryNode.getId(), keepAlive, false, testThreadPool, testThreadPool::absoluteTimeInMillis, asProgressListener, user1);
// bootstrap search
AsynchronousSearchTask task = new AsynchronousSearchTask(randomNonNegativeLong(), "transport", SearchAction.NAME, TaskId.EMPTY_TASK_ID, emptyMap(), asActiveContext, null, (c) -> {
});
asActiveContext.setTask(task);
simulateIsAlive = true;
simulateUncheckedException = true;
long oldExpiration = asActiveContext.getExpirationTimeMillis();
when(mockStore.getContext(asContextId)).thenReturn(Optional.of(asActiveContext));
CountDownLatch latch = new CountDownLatch(1);
asService.updateKeepAliveAndGetContext(asActiveContext.getAsynchronousSearchId(), timeValueHours(9), asActiveContext.getContextId(), randomUser(), new LatchedActionListener<>(wrap(r -> fail("expected update to fail but"), e -> assertTrue(e instanceof ResourceNotFoundException)), latch));
latch.await();
assertEquals("update should not have been attempted on index", mockClient.updateCount.intValue(), 0);
mockClusterService.stop();
} finally {
ThreadPool.terminate(testThreadPool, 30, TimeUnit.SECONDS);
}
}
use of org.opensearch.cluster.node.DiscoveryNode in project asynchronous-search by opensearch-project.
the class AsynchronousSearchStateMachineTests method testStateMachine.
public void testStateMachine() throws InterruptedException, BrokenBarrierException {
DiscoveryNode discoveryNode = new DiscoveryNode("node", OpenSearchTestCase.buildNewFakeTransportAddress(), Collections.emptyMap(), DiscoveryNodeRole.BUILT_IN_ROLES, Version.CURRENT);
TestThreadPool threadPool = null;
try {
threadPool = new TestThreadPool("test", executorBuilder);
ClusterService mockClusterService = createClusterService(settings, threadPool, discoveryNode, clusterSettings);
FakeClient fakeClient = new FakeClient(threadPool);
AsynchronousSearchActiveStore asActiveStore = new AsynchronousSearchActiveStore(mockClusterService);
AsynchronousSearchPersistenceService persistenceService = new AsynchronousSearchPersistenceService(fakeClient, mockClusterService, threadPool);
CustomContextListener customContextListener = new CustomContextListener();
AsynchronousSearchService asService = new AsynchronousSearchService(persistenceService, asActiveStore, fakeClient, mockClusterService, threadPool, customContextListener, new NamedWriteableRegistry(emptyList()));
AsynchronousSearchProgressListener asProgressListener = mockAsynchronousSearchProgressListener(threadPool);
AsynchronousSearchContextId asContextId = new AsynchronousSearchContextId(UUID.randomUUID().toString(), randomNonNegativeLong());
TimeValue keepAlive = TimeValue.timeValueDays(randomInt(100));
AsynchronousSearchActiveContext context = new AsynchronousSearchActiveContext(asContextId, discoveryNode.getId(), keepAlive, true, threadPool, threadPool::absoluteTimeInMillis, asProgressListener, null, () -> true);
assertNull(context.getTask());
assertEquals(context.getAsynchronousSearchState(), INIT);
AsynchronousSearchStateMachine stateMachine = asService.getStateMachine();
AtomicInteger numCompleted = new AtomicInteger();
AtomicInteger numFailure = new AtomicInteger();
doConcurrentStateMachineTrigger(stateMachine, new SearchStartedEvent(context, new AsynchronousSearchTask(randomNonNegativeLong(), "transport", SearchAction.NAME, TaskId.EMPTY_TASK_ID, emptyMap(), context, null, (a) -> {
})), RUNNING, IllegalStateException.class, Optional.empty());
boolean success = randomBoolean();
assertNotNull(context.getTask());
if (randomBoolean()) {
// delete running context
doConcurrentStateMachineTrigger(stateMachine, new SearchDeletedEvent(context), CLOSED, AsynchronousSearchStateMachineClosedException.class, Optional.empty());
} else {
if (success) {
doConcurrentStateMachineTrigger(stateMachine, new SearchFailureEvent(context, new RuntimeException("test")), FAILED, IllegalStateException.class, Optional.empty());
numFailure.getAndIncrement();
} else {
// success or failure
doConcurrentStateMachineTrigger(stateMachine, new SearchSuccessfulEvent(context, getMockSearchResponse()), SUCCEEDED, IllegalStateException.class, Optional.empty());
numCompleted.getAndIncrement();
}
doConcurrentStateMachineTrigger(stateMachine, new BeginPersistEvent(context), PERSISTING, IllegalStateException.class, Optional.of(AsynchronousSearchStateMachineClosedException.class));
waitUntil(() -> context.getAsynchronousSearchState().equals(CLOSED), 1, TimeUnit.MINUTES);
assertTrue(context.getAsynchronousSearchState().toString() + " numFailure : " + numFailure.get() + " numSuccess : " + numCompleted.get(), context.getAsynchronousSearchState().equals(CLOSED));
assertEquals(1, customContextListener.getPersistedCount() + customContextListener.getPersistFailedCount());
}
assertEquals(numCompleted.get(), customContextListener.getCompletedCount());
assertEquals(numFailure.get(), customContextListener.getFailedCount());
assertEquals("success:" + success, 0, customContextListener.getRunningCount());
assertEquals(1, customContextListener.getDeletedCount());
} finally {
ThreadPool.terminate(threadPool, 100, TimeUnit.MILLISECONDS);
}
}
use of org.opensearch.cluster.node.DiscoveryNode in project asynchronous-search by opensearch-project.
the class AsynchronousSearchManagementServiceTests method createDiscoveryNodes.
// Create the discovery nodes for a cluster state. For our testing purposes, we want
// the first to be cluster_manager, the second to be cluster_manager eligible, the third to be a data node,
// and the remainder can be any kinds of nodes (cluster_manager eligible, data, or both).
private static DiscoveryNodes createDiscoveryNodes(final int numNodes, final boolean isLocalClusterManager) {
assert (numNodes >= 3) : "the initial cluster state for event change tests should have a minimum of 3 nodes " + "so there are a minimum of 2 cluster_manager nodes for testing cluster_manager change events.";
final DiscoveryNodes.Builder builder = DiscoveryNodes.builder();
// randomly assign the local node if not cluster_manager
final int clusterManagerNodeIndex = isLocalClusterManager ? 0 : randomIntBetween(1, numNodes - 1);
for (int i = 0; i < numNodes; i++) {
final String nodeId = NODE_ID_PREFIX + i;
Set<DiscoveryNodeRole> roles = new HashSet<>();
if (i == 0) {
// local node id
builder.localNodeId(nodeId);
roles.add(DiscoveryNodeRole.MASTER_ROLE);
} else if (i == 1) {
// the alternate cluster_manager node
roles.add(DiscoveryNodeRole.MASTER_ROLE);
} else if (i == 2) {
// we need at least one data node
roles.add(DiscoveryNodeRole.DATA_ROLE);
} else {
// remaining nodes can be anything (except for cluster_manager)
if (randomBoolean()) {
roles.add(DiscoveryNodeRole.MASTER_ROLE);
}
if (randomBoolean()) {
roles.add(DiscoveryNodeRole.DATA_ROLE);
}
}
final DiscoveryNode node = newNode(nodeId, roles);
builder.add(node);
if (i == clusterManagerNodeIndex) {
builder.masterNodeId(nodeId);
}
}
return builder.build();
}
use of org.opensearch.cluster.node.DiscoveryNode in project asynchronous-search by opensearch-project.
the class AsynchronousSearchManagementServiceTests method testSchedulesResponseCleanupAtRefreshIntervals.
public void testSchedulesResponseCleanupAtRefreshIntervals() {
long refreshInterval = randomLongBetween(60000, 120000);
final Settings settings = Settings.builder().put(AsynchronousSearchManagementService.PERSISTED_RESPONSE_CLEAN_UP_INTERVAL_SETTING.getKey(), refreshInterval + "ms").build();
DiscoveryNode localNode = new DiscoveryNode("local-node", buildNewFakeTransportAddress(), Collections.singletonMap("asynchronous_search_enabled", "true"), Sets.newHashSet(DiscoveryNodeRole.DATA_ROLE), Version.CURRENT);
ClusterService mockClusterService = ClusterServiceUtils.createClusterService(deterministicTaskQueue.getThreadPool(), localNode);
final MockTransport mockTransport = new MockTransport() {
@Override
protected void onSendRequest(long requestId, String action, TransportRequest request, DiscoveryNode node) {
final boolean successResponse = randomBoolean();
if (successResponse) {
handleResponse(requestId, new AcknowledgedResponse(true));
} else {
handleRemoteError(requestId, new OpenSearchException("simulated error"));
}
}
};
final TransportService transportService = mockTransport.createTransportService(settings, deterministicTaskQueue.getThreadPool(), NOOP_TRANSPORT_INTERCEPTOR, boundTransportAddress -> new DiscoveryNode("local-node", buildNewFakeTransportAddress(), Version.CURRENT), null, emptySet());
transportService.start();
transportService.acceptIncomingRequests();
AsynchronousSearchManagementService managementService = new AsynchronousSearchManagementService(settings, mockClusterService, deterministicTaskQueue.getThreadPool(), Mockito.mock(AsynchronousSearchService.class), transportService, Mockito.mock(AsynchronousSearchPersistenceService.class));
final long startTimeMillis = deterministicTaskQueue.getCurrentTimeMillis();
final int numNodesInCluster = 3;
ClusterState previousState = createSimpleClusterState();
ClusterState newState = createState(numNodesInCluster, true, initialIndices);
managementService.clusterChanged(new ClusterChangedEvent("_na_", newState, previousState));
assertTrue(deterministicTaskQueue.hasRunnableTasks());
assertTrue(deterministicTaskQueue.hasDeferredTasks());
int rescheduledCount = 0;
for (int i = 1; i <= randomIntBetween(5, 10); i++) {
if (deterministicTaskQueue.hasRunnableTasks()) {
deterministicTaskQueue.runRandomTask();
} else {
assertThat(deterministicTaskQueue.getLatestDeferredExecutionTime(), is(refreshInterval * (rescheduledCount + 1)));
deterministicTaskQueue.advanceTime();
rescheduledCount++;
}
assertThat(deterministicTaskQueue.getCurrentTimeMillis() - startTimeMillis, is(refreshInterval * rescheduledCount));
}
managementService.clusterChanged(new ClusterChangedEvent("_na_", createState(numNodesInCluster, false, initialIndices), newState));
deterministicTaskQueue.runAllTasksInTimeOrder();
assertFalse(deterministicTaskQueue.hasRunnableTasks());
assertFalse(deterministicTaskQueue.hasDeferredTasks());
}
Aggregations