Search in sources :

Example 26 with TransportService

use of org.opensearch.transport.TransportService in project anomaly-detection by opensearch-project.

the class DeleteModelTransportActionTests method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    ThreadPool threadPool = mock(ThreadPool.class);
    ClusterService clusterService = mock(ClusterService.class);
    localNodeID = "foo";
    when(clusterService.localNode()).thenReturn(new DiscoveryNode(localNodeID, buildNewFakeTransportAddress(), Version.CURRENT));
    when(clusterService.getClusterName()).thenReturn(new ClusterName("test"));
    TransportService transportService = mock(TransportService.class);
    ActionFilters actionFilters = mock(ActionFilters.class);
    NodeStateManager nodeStateManager = mock(NodeStateManager.class);
    ModelManager modelManager = mock(ModelManager.class);
    FeatureManager featureManager = mock(FeatureManager.class);
    CacheProvider cacheProvider = mock(CacheProvider.class);
    EntityCache entityCache = mock(EntityCache.class);
    when(cacheProvider.get()).thenReturn(entityCache);
    ADTaskCacheManager adTaskCacheManager = mock(ADTaskCacheManager.class);
    NodeStateManager stateManager = mock(NodeStateManager.class);
    action = new DeleteModelTransportAction(threadPool, clusterService, transportService, actionFilters, nodeStateManager, modelManager, featureManager, cacheProvider, adTaskCacheManager);
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) EntityCache(org.opensearch.ad.caching.EntityCache) ThreadPool(org.opensearch.threadpool.ThreadPool) ActionFilters(org.opensearch.action.support.ActionFilters) ModelManager(org.opensearch.ad.ml.ModelManager) CacheProvider(org.opensearch.ad.caching.CacheProvider) NodeStateManager(org.opensearch.ad.NodeStateManager) ClusterService(org.opensearch.cluster.service.ClusterService) ADTaskCacheManager(org.opensearch.ad.task.ADTaskCacheManager) TransportService(org.opensearch.transport.TransportService) ClusterName(org.opensearch.cluster.ClusterName) FeatureManager(org.opensearch.ad.feature.FeatureManager) Before(org.junit.Before)

Example 27 with TransportService

use of org.opensearch.transport.TransportService in project anomaly-detection by opensearch-project.

the class DeleteTests method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    node1 = "node1";
    node2 = "node2";
    nodename1 = "nodename1";
    nodename2 = "nodename2";
    DiscoveryNode discoveryNode1 = new DiscoveryNode(nodename1, node1, new TransportAddress(TransportAddress.META_ADDRESS, 9300), emptyMap(), emptySet(), Version.CURRENT);
    DiscoveryNode discoveryNode2 = new DiscoveryNode(nodename2, node2, new TransportAddress(TransportAddress.META_ADDRESS, 9301), emptyMap(), emptySet(), Version.CURRENT);
    List<DiscoveryNode> discoveryNodes = new ArrayList<DiscoveryNode>(2);
    discoveryNodes.add(discoveryNode1);
    discoveryNodes.add(discoveryNode2);
    DeleteModelNodeResponse nodeResponse1 = new DeleteModelNodeResponse(discoveryNode1);
    DeleteModelNodeResponse nodeResponse2 = new DeleteModelNodeResponse(discoveryNode2);
    deleteModelResponse = new ArrayList<>();
    deleteModelResponse.add(nodeResponse1);
    deleteModelResponse.add(nodeResponse2);
    failures = new ArrayList<>();
    failures.add(new FailedNodeException("node3", "blah", new OpenSearchException("foo")));
    response = new DeleteModelResponse(new ClusterName("Cluster"), deleteModelResponse, failures);
    clusterService = mock(ClusterService.class);
    when(clusterService.localNode()).thenReturn(discoveryNode1);
    when(clusterService.state()).thenReturn(ClusterCreation.state(new ClusterName("test"), discoveryNode2, discoveryNode1, discoveryNodes));
    transportService = mock(TransportService.class);
    threadPool = mock(ThreadPool.class);
    actionFilters = mock(ActionFilters.class);
    Settings settings = Settings.builder().put("plugins.anomaly_detection.request_timeout", TimeValue.timeValueSeconds(10)).build();
    task = mock(Task.class);
    when(task.getId()).thenReturn(1000L);
    client = mock(Client.class);
    when(client.settings()).thenReturn(settings);
    when(client.threadPool()).thenReturn(threadPool);
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) Task(org.opensearch.tasks.Task) TransportAddress(org.opensearch.common.transport.TransportAddress) ArrayList(java.util.ArrayList) ThreadPool(org.opensearch.threadpool.ThreadPool) ActionFilters(org.opensearch.action.support.ActionFilters) ClusterService(org.opensearch.cluster.service.ClusterService) TransportService(org.opensearch.transport.TransportService) ClusterName(org.opensearch.cluster.ClusterName) FailedNodeException(org.opensearch.action.FailedNodeException) OpenSearchException(org.opensearch.OpenSearchException) Client(org.opensearch.client.Client) Settings(org.opensearch.common.settings.Settings) Before(org.junit.Before)

Example 28 with TransportService

use of org.opensearch.transport.TransportService in project anomaly-detection by opensearch-project.

the class GetAnomalyDetectorTests method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    ClusterService clusterService = mock(ClusterService.class);
    ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.unmodifiableSet(new HashSet<>(Arrays.asList(AnomalyDetectorSettings.FILTER_BY_BACKEND_ROLES))));
    when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
    transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet());
    nodeFilter = mock(DiscoveryNodeFilterer.class);
    actionFilters = mock(ActionFilters.class);
    client = mock(Client.class);
    when(client.threadPool()).thenReturn(threadPool);
    adTaskManager = mock(ADTaskManager.class);
    action = new GetAnomalyDetectorTransportAction(transportService, nodeFilter, actionFilters, clusterService, client, Settings.EMPTY, xContentRegistry(), adTaskManager);
    entity = Entity.createSingleAttributeEntity(categoryField, entityValue);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) DiscoveryNodeFilterer(org.opensearch.ad.util.DiscoveryNodeFilterer) BeforeClass(org.junit.BeforeClass) OpenSearchStatusException(org.opensearch.OpenSearchStatusException) AbstractADTest(org.opensearch.ad.AbstractADTest) AnomalyDetectorSettings(org.opensearch.ad.settings.AnomalyDetectorSettings) HashSet(java.util.HashSet) Transport(org.opensearch.transport.Transport) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ActionListener(org.opensearch.action.ActionListener) ClusterSettings(org.opensearch.common.settings.ClusterSettings) GetResponse(org.opensearch.action.get.GetResponse) AfterClass(org.junit.AfterClass) Client(org.opensearch.client.Client) ANOMALY_DETECTORS_INDEX(org.opensearch.ad.model.AnomalyDetector.ANOMALY_DETECTORS_INDEX) GetRequest(org.opensearch.action.get.GetRequest) IOException(java.io.IOException) ADTaskManager(org.opensearch.ad.task.ADTaskManager) Settings(org.opensearch.common.settings.Settings) Mockito.when(org.mockito.Mockito.when) TransportService(org.opensearch.transport.TransportService) ActionFilters(org.opensearch.action.support.ActionFilters) CommonErrorMessages(org.opensearch.ad.constant.CommonErrorMessages) Entity(org.opensearch.ad.model.Entity) ClusterService(org.opensearch.cluster.service.ClusterService) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) ClusterService(org.opensearch.cluster.service.ClusterService) ClusterSettings(org.opensearch.common.settings.ClusterSettings) DiscoveryNodeFilterer(org.opensearch.ad.util.DiscoveryNodeFilterer) TransportService(org.opensearch.transport.TransportService) ADTaskManager(org.opensearch.ad.task.ADTaskManager) ActionFilters(org.opensearch.action.support.ActionFilters) Client(org.opensearch.client.Client) HashSet(java.util.HashSet)

Example 29 with TransportService

use of org.opensearch.transport.TransportService 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)

Example 30 with TransportService

use of org.opensearch.transport.TransportService in project asynchronous-search by opensearch-project.

the class AsynchronousSearchPersistenceServiceIT method testGetAndDeleteNonExistentId.

public void testGetAndDeleteNonExistentId() throws InterruptedException, IOException, ExecutionException {
    AsynchronousSearchPersistenceService persistenceService = getInstanceFromNode(AsynchronousSearchPersistenceService.class);
    TransportService transportService = getInstanceFromNode(TransportService.class);
    SearchResponse searchResponse = client().search(new SearchRequest(TEST_INDEX)).get();
    User user1 = TestClientUtils.randomUser();
    User user2 = TestClientUtils.randomUser();
    for (User originalUser : Arrays.asList(user1, null)) {
        AsynchronousSearchId asId = generateNewAsynchronousSearchId(transportService);
        AsynchronousSearchPersistenceModel model1 = new AsynchronousSearchPersistenceModel(System.currentTimeMillis(), System.currentTimeMillis() + new TimeValue(10, TimeUnit.DAYS).getMillis(), searchResponse, null, originalUser);
        CountDownLatch createLatch = new CountDownLatch(1);
        String id = AsynchronousSearchIdConverter.buildAsyncId(asId);
        persistenceService.storeResponse(id, model1, new LatchedActionListener<>(ActionListener.wrap(r -> assertSuccessfulResponseCreation(id, r), e -> {
            logger.debug("expect successful create, got", e);
            fail("Expected successful create, got " + e.getMessage());
        }), createLatch));
        createLatch.await();
        for (User currentuser : Arrays.asList(originalUser, user2)) {
            CountDownLatch latch = new CountDownLatch(2);
            // assert failure
            persistenceService.getResponse("id", currentuser, new LatchedActionListener<>(ActionListener.wrap((AsynchronousSearchPersistenceModel r) -> fail("Excepted resource_not_found_exception, got " + r), exception -> assertTrue("Expected resource_not_found expection, got " + exception.getClass().toString(), exception instanceof ResourceNotFoundException)), latch));
            // assert failure
            ActionListener<Boolean> wrap = ActionListener.wrap(r -> fail("Expected resource_not_found expection on delete, got acknowledgement " + r), ex -> assertTrue("Expected resource_not_found expection, got " + ex.getClass().toString(), ex instanceof ResourceNotFoundException));
            persistenceService.deleteResponse("id", currentuser, new LatchedActionListener<>(wrap, latch));
            latch.await();
        }
    }
}
Also used : GetAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.GetAsynchronousSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) DeleteAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.DeleteAsynchronousSearchRequest) SubmitAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest) User(org.opensearch.commons.authuser.User) CountDownLatch(java.util.concurrent.CountDownLatch) AsynchronousSearchResponse(org.opensearch.search.asynchronous.response.AsynchronousSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) TransportService(org.opensearch.transport.TransportService) AsynchronousSearchPersistenceService(org.opensearch.search.asynchronous.service.AsynchronousSearchPersistenceService) AsynchronousSearchId(org.opensearch.search.asynchronous.id.AsynchronousSearchId) ResourceNotFoundException(org.opensearch.ResourceNotFoundException) AsynchronousSearchPersistenceModel(org.opensearch.search.asynchronous.context.persistence.AsynchronousSearchPersistenceModel) TimeValue(org.opensearch.common.unit.TimeValue)

Aggregations

TransportService (org.opensearch.transport.TransportService)167 Settings (org.opensearch.common.settings.Settings)90 ClusterService (org.opensearch.cluster.service.ClusterService)86 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)83 ActionListener (org.opensearch.action.ActionListener)69 ActionFilters (org.opensearch.action.support.ActionFilters)69 ThreadPool (org.opensearch.threadpool.ThreadPool)68 IOException (java.io.IOException)48 Version (org.opensearch.Version)47 ClusterState (org.opensearch.cluster.ClusterState)47 ArrayList (java.util.ArrayList)45 Collections (java.util.Collections)41 Before (org.junit.Before)41 List (java.util.List)40 TimeValue (org.opensearch.common.unit.TimeValue)40 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)39 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)38 Map (java.util.Map)37 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)35 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)35