Search in sources :

Example 61 with DiscoveryNode

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);
    }
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) SubmitAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) TestClientUtils.randomUser(org.opensearch.search.asynchronous.utils.TestClientUtils.randomUser) User(org.opensearch.commons.authuser.User) SubmitAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest) AsynchronousSearchContextId(org.opensearch.search.asynchronous.context.AsynchronousSearchContextId) TestThreadPool(org.opensearch.threadpool.TestThreadPool) ThreadPool(org.opensearch.threadpool.ThreadPool) InternalAsynchronousSearchStats(org.opensearch.search.asynchronous.stats.InternalAsynchronousSearchStats) TestThreadPool(org.opensearch.threadpool.TestThreadPool) CountDownLatch(java.util.concurrent.CountDownLatch) AsynchronousSearchActiveStore(org.opensearch.search.asynchronous.context.active.AsynchronousSearchActiveStore) AsynchronousSearchProgressListener(org.opensearch.search.asynchronous.listener.AsynchronousSearchProgressListener) AsynchronousSearchTestCase.mockAsynchronousSearchProgressListener(org.opensearch.search.asynchronous.commons.AsynchronousSearchTestCase.mockAsynchronousSearchProgressListener) AsynchronousSearchTask(org.opensearch.search.asynchronous.task.AsynchronousSearchTask) ClusterService(org.opensearch.cluster.service.ClusterService) TimeValue(org.opensearch.common.unit.TimeValue)

Example 62 with DiscoveryNode

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);
    }
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) SubmitAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) TestClientUtils.randomUser(org.opensearch.search.asynchronous.utils.TestClientUtils.randomUser) User(org.opensearch.commons.authuser.User) SubmitAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest) AsynchronousSearchContextId(org.opensearch.search.asynchronous.context.AsynchronousSearchContextId) TestThreadPool(org.opensearch.threadpool.TestThreadPool) ThreadPool(org.opensearch.threadpool.ThreadPool) InternalAsynchronousSearchStats(org.opensearch.search.asynchronous.stats.InternalAsynchronousSearchStats) TestThreadPool(org.opensearch.threadpool.TestThreadPool) CountDownLatch(java.util.concurrent.CountDownLatch) AsynchronousSearchActiveStore(org.opensearch.search.asynchronous.context.active.AsynchronousSearchActiveStore) AsynchronousSearchProgressListener(org.opensearch.search.asynchronous.listener.AsynchronousSearchProgressListener) AsynchronousSearchTestCase.mockAsynchronousSearchProgressListener(org.opensearch.search.asynchronous.commons.AsynchronousSearchTestCase.mockAsynchronousSearchProgressListener) AsynchronousSearchTask(org.opensearch.search.asynchronous.task.AsynchronousSearchTask) ClusterService(org.opensearch.cluster.service.ClusterService) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) TimeValue(org.opensearch.common.unit.TimeValue)

Example 63 with DiscoveryNode

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);
    }
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) SearchSuccessfulEvent(org.opensearch.search.asynchronous.context.state.event.SearchSuccessfulEvent) SearchFailureEvent(org.opensearch.search.asynchronous.context.state.event.SearchFailureEvent) TestThreadPool(org.opensearch.threadpool.TestThreadPool) SearchStartedEvent(org.opensearch.search.asynchronous.context.state.event.SearchStartedEvent) AsynchronousSearchStateMachine(org.opensearch.search.asynchronous.context.state.AsynchronousSearchStateMachine) BeginPersistEvent(org.opensearch.search.asynchronous.context.state.event.BeginPersistEvent) AsynchronousSearchStateMachineClosedException(org.opensearch.search.asynchronous.context.state.AsynchronousSearchStateMachineClosedException) TimeValue(org.opensearch.common.unit.TimeValue) SearchDeletedEvent(org.opensearch.search.asynchronous.context.state.event.SearchDeletedEvent) AsynchronousSearchContextId(org.opensearch.search.asynchronous.context.AsynchronousSearchContextId) AsynchronousSearchActiveContext(org.opensearch.search.asynchronous.context.active.AsynchronousSearchActiveContext) AsynchronousSearchActiveStore(org.opensearch.search.asynchronous.context.active.AsynchronousSearchActiveStore) AsynchronousSearchProgressListener(org.opensearch.search.asynchronous.listener.AsynchronousSearchProgressListener) AsynchronousSearchTask(org.opensearch.search.asynchronous.task.AsynchronousSearchTask) TestUtils.createClusterService(org.opensearch.search.asynchronous.utils.TestUtils.createClusterService) ClusterService(org.opensearch.cluster.service.ClusterService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 64 with DiscoveryNode

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();
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) DiscoveryNodeRole(org.opensearch.cluster.node.DiscoveryNodeRole) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) HashSet(java.util.HashSet)

Example 65 with DiscoveryNode

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());
}
Also used : AsynchronousSearchService(org.opensearch.search.asynchronous.service.AsynchronousSearchService) ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) TransportRequest(org.opensearch.transport.TransportRequest) AcknowledgedResponse(org.opensearch.search.asynchronous.response.AcknowledgedResponse) ClusterChangedEvent(org.opensearch.cluster.ClusterChangedEvent) ClusterService(org.opensearch.cluster.service.ClusterService) TransportService(org.opensearch.transport.TransportService) MockTransport(org.opensearch.test.transport.MockTransport) AsynchronousSearchPersistenceService(org.opensearch.search.asynchronous.service.AsynchronousSearchPersistenceService) OpenSearchException(org.opensearch.OpenSearchException) Settings(org.opensearch.common.settings.Settings)

Aggregations

DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)673 ClusterState (org.opensearch.cluster.ClusterState)164 Settings (org.opensearch.common.settings.Settings)152 ArrayList (java.util.ArrayList)137 DiscoveryNodes (org.opensearch.cluster.node.DiscoveryNodes)123 ThreadPool (org.opensearch.threadpool.ThreadPool)103 ClusterService (org.opensearch.cluster.service.ClusterService)100 CountDownLatch (java.util.concurrent.CountDownLatch)98 HashSet (java.util.HashSet)93 TransportService (org.opensearch.transport.TransportService)92 IOException (java.io.IOException)89 ActionListener (org.opensearch.action.ActionListener)88 Matchers.containsString (org.hamcrest.Matchers.containsString)84 ClusterName (org.opensearch.cluster.ClusterName)82 Version (org.opensearch.Version)80 List (java.util.List)78 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)78 HashMap (java.util.HashMap)77 TimeValue (org.opensearch.common.unit.TimeValue)77 ShardId (org.opensearch.index.shard.ShardId)76