Search in sources :

Example 21 with TransportService

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

the class EntityResultTransportActionTests method setUp.

@SuppressWarnings("unchecked")
@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    actionFilters = mock(ActionFilters.class);
    transportService = mock(TransportService.class);
    adCircuitBreakerService = mock(ADCircuitBreakerService.class);
    when(adCircuitBreakerService.isOpen()).thenReturn(false);
    checkpointDao = mock(CheckpointDao.class);
    detectorId = "123";
    entities = new HashMap<>();
    start = 10L;
    end = 20L;
    request = new EntityResultRequest(detectorId, entities, start, end);
    clock = mock(Clock.class);
    now = Instant.now();
    when(clock.instant()).thenReturn(now);
    manager = new ModelManager(null, clock, 0, 0, 0, 0, 0, 0, null, null, mock(EntityColdStarter.class), null, null);
    provider = mock(CacheProvider.class);
    entityCache = mock(EntityCache.class);
    when(provider.get()).thenReturn(entityCache);
    String field = "a";
    detector = TestHelpers.randomAnomalyDetectorUsingCategoryFields(detectorId, Arrays.asList(field));
    stateManager = mock(NodeStateManager.class);
    doAnswer(invocation -> {
        ActionListener<Optional<AnomalyDetector>> listener = invocation.getArgument(1);
        listener.onResponse(Optional.of(detector));
        return null;
    }).when(stateManager).getAnomalyDetector(any(String.class), any(ActionListener.class));
    cacheMissEntity = "0.0.0.1";
    cacheMissData = new double[] { 0.1 };
    cacheHitEntity = "0.0.0.2";
    cacheHitData = new double[] { 0.2 };
    cacheMissEntityObj = Entity.createSingleAttributeEntity(detector.getCategoryField().get(0), cacheMissEntity);
    entities.put(cacheMissEntityObj, cacheMissData);
    cacheHitEntityObj = Entity.createSingleAttributeEntity(detector.getCategoryField().get(0), cacheHitEntity);
    entities.put(cacheHitEntityObj, cacheHitData);
    tooLongEntity = randomAlphaOfLength(AnomalyDetectorSettings.MAX_ENTITY_LENGTH + 1);
    tooLongData = new double[] { 0.3 };
    entities.put(Entity.createSingleAttributeEntity(detector.getCategoryField().get(0), tooLongEntity), tooLongData);
    ModelState<EntityModel> state = MLUtil.randomModelState(new RandomModelStateConfig.Builder().fullModel(true).build());
    when(entityCache.get(eq(cacheMissEntityObj.getModelId(detectorId).get()), any())).thenReturn(null);
    when(entityCache.get(eq(cacheHitEntityObj.getModelId(detectorId).get()), any())).thenReturn(state);
    List<Entity> coldEntities = new ArrayList<>();
    coldEntities.add(cacheMissEntityObj);
    when(entityCache.selectUpdateCandidate(any(), anyString(), any())).thenReturn(Pair.of(new ArrayList<>(), coldEntities));
    settings = Settings.builder().put(AnomalyDetectorSettings.COOLDOWN_MINUTES.getKey(), TimeValue.timeValueMinutes(5)).build();
    AnomalyDetectionIndices indexUtil = mock(AnomalyDetectionIndices.class);
    when(indexUtil.getSchemaVersion(any())).thenReturn(CommonValue.NO_SCHEMA_VERSION);
    resultWriteQueue = mock(ResultWriteWorker.class);
    checkpointReadQueue = mock(CheckpointReadWorker.class);
    minSamples = 1;
    coldStarter = mock(EntityColdStarter.class);
    doAnswer(invocation -> {
        ModelState<EntityModel> modelState = invocation.getArgument(0);
        modelState.getModel().clear();
        return null;
    }).when(coldStarter).trainModelFromExistingSamples(any(), anyInt());
    coldEntityQueue = mock(ColdEntityWorker.class);
    entityResult = new EntityResultTransportAction(actionFilters, transportService, manager, adCircuitBreakerService, provider, stateManager, indexUtil, resultWriteQueue, checkpointReadQueue, coldEntityQueue, threadPool);
    // timeout in 60 seconds
    timeoutMs = 60000L;
}
Also used : Entity(org.opensearch.ad.model.Entity) EntityCache(org.opensearch.ad.caching.EntityCache) ArrayList(java.util.ArrayList) CheckpointReadWorker(org.opensearch.ad.ratelimit.CheckpointReadWorker) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Clock(java.time.Clock) ResultWriteWorker(org.opensearch.ad.ratelimit.ResultWriteWorker) NodeStateManager(org.opensearch.ad.NodeStateManager) RandomModelStateConfig(test.org.opensearch.ad.util.RandomModelStateConfig) EntityColdStarter(org.opensearch.ad.ml.EntityColdStarter) ColdEntityWorker(org.opensearch.ad.ratelimit.ColdEntityWorker) ADCircuitBreakerService(org.opensearch.ad.breaker.ADCircuitBreakerService) Optional(java.util.Optional) EntityModel(org.opensearch.ad.ml.EntityModel) ActionFilters(org.opensearch.action.support.ActionFilters) ModelManager(org.opensearch.ad.ml.ModelManager) CacheProvider(org.opensearch.ad.caching.CacheProvider) CheckpointDao(org.opensearch.ad.ml.CheckpointDao) ActionListener(org.opensearch.action.ActionListener) TransportService(org.opensearch.transport.TransportService) AnomalyDetectionIndices(org.opensearch.ad.indices.AnomalyDetectionIndices) Before(org.junit.Before)

Example 22 with TransportService

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

the class RCFPollingTests method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    clusterService = mock(ClusterService.class);
    hashRing = mock(HashRing.class);
    transportAddress1 = new TransportAddress(new InetSocketAddress(InetAddress.getByName("1.2.3.4"), 9300));
    manager = mock(ModelManager.class);
    transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet());
    future = new PlainActionFuture<>();
    request = new RCFPollingRequest(detectorId);
    model0Id = SingleStreamModelIdMapper.getRcfModelId(detectorId, 0);
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        @SuppressWarnings("unchecked") ActionListener<Long> listener = (ActionListener<Long>) args[2];
        listener.onResponse(totalUpdates);
        return null;
    }).when(manager).getTotalUpdates(any(String.class), any(String.class), any());
    normalTransportInterceptor = new TransportInterceptor() {

        @Override
        public AsyncSender interceptSender(AsyncSender sender) {
            return new AsyncSender() {

                @Override
                public <T extends TransportResponse> void sendRequest(Transport.Connection connection, String action, TransportRequest request, TransportRequestOptions options, TransportResponseHandler<T> handler) {
                    if (RCFPollingAction.NAME.equals(action)) {
                        sender.sendRequest(connection, action, request, options, rcfRollingHandler(handler));
                    } else {
                        sender.sendRequest(connection, action, request, options, handler);
                    }
                }
            };
        }
    };
    failureTransportInterceptor = new TransportInterceptor() {

        @Override
        public AsyncSender interceptSender(AsyncSender sender) {
            return new AsyncSender() {

                @Override
                public <T extends TransportResponse> void sendRequest(Transport.Connection connection, String action, TransportRequest request, TransportRequestOptions options, TransportResponseHandler<T> handler) {
                    if (RCFPollingAction.NAME.equals(action)) {
                        sender.sendRequest(connection, action, request, options, rcfFailureRollingHandler(handler));
                    } else {
                        sender.sendRequest(connection, action, request, options, handler);
                    }
                }
            };
        }
    };
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) JsonDeserializer(test.org.opensearch.ad.util.JsonDeserializer) BeforeClass(org.junit.BeforeClass) ModelManager(org.opensearch.ad.ml.ModelManager) HashRing(org.opensearch.ad.cluster.HashRing) ToXContent(org.opensearch.common.xcontent.ToXContent) TransportInterceptor(org.opensearch.transport.TransportInterceptor) AbstractADTest(org.opensearch.ad.AbstractADTest) Version(org.opensearch.Version) JsonPathNotFoundException(org.opensearch.ad.common.exception.JsonPathNotFoundException) GsonBuilder(com.google.gson.GsonBuilder) AnomalyDetectionException(org.opensearch.ad.common.exception.AnomalyDetectionException) InetAddress(java.net.InetAddress) Transport(org.opensearch.transport.Transport) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) Gson(com.google.gson.Gson) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ActionListener(org.opensearch.action.ActionListener) Before(org.junit.Before) StreamInput(org.opensearch.common.io.stream.StreamInput) AfterClass(org.junit.AfterClass) CommonName(org.opensearch.ad.constant.CommonName) TransportRequest(org.opensearch.transport.TransportRequest) TransportRequestOptions(org.opensearch.transport.TransportRequestOptions) TransportResponseHandler(org.opensearch.transport.TransportResponseHandler) IOException(java.io.IOException) Settings(org.opensearch.common.settings.Settings) Mockito.when(org.mockito.Mockito.when) Task(org.opensearch.tasks.Task) TransportResponse(org.opensearch.transport.TransportResponse) InetSocketAddress(java.net.InetSocketAddress) TransportService(org.opensearch.transport.TransportService) TransportAddress(org.opensearch.common.transport.TransportAddress) ActionFilters(org.opensearch.action.support.ActionFilters) TestHelpers(org.opensearch.ad.TestHelpers) FakeNode(test.org.opensearch.ad.util.FakeNode) ClusterService(org.opensearch.cluster.service.ClusterService) Optional(java.util.Optional) SingleStreamModelIdMapper(org.opensearch.ad.ml.SingleStreamModelIdMapper) ConnectTransportException(org.opensearch.transport.ConnectTransportException) Collections(java.util.Collections) TransportException(org.opensearch.transport.TransportException) Mockito.mock(org.mockito.Mockito.mock) TransportInterceptor(org.opensearch.transport.TransportInterceptor) TransportAddress(org.opensearch.common.transport.TransportAddress) InetSocketAddress(java.net.InetSocketAddress) HashRing(org.opensearch.ad.cluster.HashRing) TransportRequestOptions(org.opensearch.transport.TransportRequestOptions) TransportRequest(org.opensearch.transport.TransportRequest) ModelManager(org.opensearch.ad.ml.ModelManager) ClusterService(org.opensearch.cluster.service.ClusterService) ActionListener(org.opensearch.action.ActionListener) TransportService(org.opensearch.transport.TransportService) Transport(org.opensearch.transport.Transport) Before(org.junit.Before)

Example 23 with TransportService

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

the class RCFPollingTests method testGetRemoteNormalResponse.

public void testGetRemoteNormalResponse() {
    setupTestNodes(normalTransportInterceptor, Settings.EMPTY);
    try {
        TransportService realTransportService = testNodes[0].transportService;
        clusterService = testNodes[0].clusterService;
        action = new RCFPollingTransportAction(new ActionFilters(Collections.emptySet()), realTransportService, Settings.EMPTY, manager, hashRing, clusterService);
        when(hashRing.getOwningNodeWithSameLocalAdVersionForRealtimeAD(any(String.class))).thenReturn(Optional.of(testNodes[1].discoveryNode()));
        registerHandler(testNodes[1]);
        action.doExecute(null, request, future);
        RCFPollingResponse response = future.actionGet();
        assertEquals(totalUpdates, response.getTotalUpdates());
    } finally {
        tearDownTestNodes();
    }
}
Also used : TransportService(org.opensearch.transport.TransportService) ActionFilters(org.opensearch.action.support.ActionFilters)

Example 24 with TransportService

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

the class RCFResultTests method testExecutionException.

@SuppressWarnings("unchecked")
public void testExecutionException() {
    TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet());
    ModelManager manager = mock(ModelManager.class);
    ADCircuitBreakerService adCircuitBreakerService = mock(ADCircuitBreakerService.class);
    RCFResultTransportAction action = new RCFResultTransportAction(mock(ActionFilters.class), transportService, manager, adCircuitBreakerService, hashRing);
    doThrow(NullPointerException.class).when(manager).getTRcfResult(any(String.class), any(String.class), any(double[].class), any(ActionListener.class));
    when(adCircuitBreakerService.isOpen()).thenReturn(false);
    final PlainActionFuture<RCFResultResponse> future = new PlainActionFuture<>();
    RCFResultRequest request = new RCFResultRequest("123", "123-rcf-1", new double[] { 0 });
    action.doExecute(mock(Task.class), request, future);
    expectThrows(NullPointerException.class, () -> future.actionGet());
}
Also used : Task(org.opensearch.tasks.Task) ADCircuitBreakerService(org.opensearch.ad.breaker.ADCircuitBreakerService) ActionFilters(org.opensearch.action.support.ActionFilters) ModelManager(org.opensearch.ad.ml.ModelManager) ActionListener(org.opensearch.action.ActionListener) TransportService(org.opensearch.transport.TransportService) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) Transport(org.opensearch.transport.Transport)

Example 25 with TransportService

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

the class RCFResultTests method testCircuitBreaker.

@SuppressWarnings("unchecked")
public void testCircuitBreaker() {
    TransportService transportService = new TransportService(Settings.EMPTY, mock(Transport.class), null, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet());
    ModelManager manager = mock(ModelManager.class);
    ADCircuitBreakerService breakerService = mock(ADCircuitBreakerService.class);
    RCFResultTransportAction action = new RCFResultTransportAction(mock(ActionFilters.class), transportService, manager, breakerService, hashRing);
    doAnswer(invocation -> {
        ActionListener<ThresholdingResult> listener = invocation.getArgument(3);
        listener.onResponse(new ThresholdingResult(grade, 0d, 0.5, totalUpdates, 0, attribution, pastValues, expectedValuesList, likelihood, threshold, 30));
        return null;
    }).when(manager).getTRcfResult(any(String.class), any(String.class), any(double[].class), any(ActionListener.class));
    when(breakerService.isOpen()).thenReturn(true);
    final PlainActionFuture<RCFResultResponse> future = new PlainActionFuture<>();
    RCFResultRequest request = new RCFResultRequest("123", "123-rcf-1", new double[] { 0 });
    action.doExecute(mock(Task.class), request, future);
    expectThrows(LimitExceededException.class, () -> future.actionGet());
}
Also used : Task(org.opensearch.tasks.Task) ADCircuitBreakerService(org.opensearch.ad.breaker.ADCircuitBreakerService) ActionFilters(org.opensearch.action.support.ActionFilters) ModelManager(org.opensearch.ad.ml.ModelManager) ThresholdingResult(org.opensearch.ad.ml.ThresholdingResult) ActionListener(org.opensearch.action.ActionListener) TransportService(org.opensearch.transport.TransportService) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) Transport(org.opensearch.transport.Transport)

Aggregations

TransportService (org.opensearch.transport.TransportService)177 Settings (org.opensearch.common.settings.Settings)99 ClusterService (org.opensearch.cluster.service.ClusterService)90 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)88 ActionListener (org.opensearch.action.ActionListener)75 ThreadPool (org.opensearch.threadpool.ThreadPool)73 ActionFilters (org.opensearch.action.support.ActionFilters)72 Version (org.opensearch.Version)51 IOException (java.io.IOException)49 Collections (java.util.Collections)49 ClusterState (org.opensearch.cluster.ClusterState)48 ArrayList (java.util.ArrayList)47 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)46 List (java.util.List)42 Before (org.junit.Before)42 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)41 TimeValue (org.opensearch.common.unit.TimeValue)40 Map (java.util.Map)38 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)38 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)38