Search in sources :

Example 11 with Client

use of org.opensearch.client.Client in project k-NN by opensearch-project.

the class TrainingJobRouterTransportActionTests method testMultiNode_withoutCapacity.

public void testMultiNode_withoutCapacity() {
    // Mock datanodes in the cluster through mocking the cluster service
    List<String> nodeIds = ImmutableList.of("node-1", "node-2", "node-3");
    ImmutableOpenMap<String, DiscoveryNode> discoveryNodesMap = generateDiscoveryNodes(nodeIds);
    ClusterService clusterService = generateMockedClusterService(discoveryNodesMap);
    // Create a response to be returned with job route decision info
    List<TrainingJobRouteDecisionInfoNodeResponse> responseList = new ArrayList<>();
    // First node has no capacity
    responseList.add(new TrainingJobRouteDecisionInfoNodeResponse(discoveryNodesMap.get(nodeIds.get(0)), 1));
    // Second node has no capacity
    responseList.add(new TrainingJobRouteDecisionInfoNodeResponse(discoveryNodesMap.get(nodeIds.get(1)), 1));
    // Third node has no capacity
    responseList.add(new TrainingJobRouteDecisionInfoNodeResponse(discoveryNodesMap.get(nodeIds.get(1)), 1));
    TrainingJobRouteDecisionInfoResponse infoResponse = new TrainingJobRouteDecisionInfoResponse(ClusterName.DEFAULT, responseList, Collections.emptyList());
    TransportService transportService = mock(TransportService.class);
    Client client = mock(Client.class);
    // Setup the action
    TrainingJobRouterTransportAction transportAction = new TrainingJobRouterTransportAction(transportService, new ActionFilters(Collections.emptySet()), clusterService, client);
    // Select the node
    DiscoveryNode selectedNode = transportAction.selectNode(null, infoResponse);
    assertNull(selectedNode);
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) ArrayList(java.util.ArrayList) ActionFilters(org.opensearch.action.support.ActionFilters) ClusterService(org.opensearch.cluster.service.ClusterService) TransportService(org.opensearch.transport.TransportService) Client(org.opensearch.client.Client)

Example 12 with Client

use of org.opensearch.client.Client in project k-NN by opensearch-project.

the class TrainingJobRouterTransportActionTests method testTrainingIndexSize.

@SuppressWarnings("unchecked")
public void testTrainingIndexSize() {
    String trainingIndexName = "training-index";
    int dimension = 133;
    int vectorCount = 1000000;
    // 519,531.25 KB ~= 520 MB
    int expectedSize = dimension * vectorCount * Float.BYTES / BYTES_PER_KILOBYTES + 1;
    // Setup the request
    TrainingModelRequest trainingModelRequest = new TrainingModelRequest(null, KNNMethodContext.getDefault(), dimension, trainingIndexName, "training-field", null, "description");
    // Mock client to return the right number of docs
    TotalHits totalHits = new TotalHits(vectorCount, TotalHits.Relation.EQUAL_TO);
    SearchHits searchHits = new SearchHits(new SearchHit[2], totalHits, 1.0f);
    SearchResponse searchResponse = mock(SearchResponse.class);
    when(searchResponse.getHits()).thenReturn(searchHits);
    Client client = mock(Client.class);
    doAnswer(invocationOnMock -> {
        ((ActionListener<SearchResponse>) invocationOnMock.getArguments()[1]).onResponse(searchResponse);
        return null;
    }).when(client).search(any(), any());
    // Setup the action
    ClusterService clusterService = mock(ClusterService.class);
    TransportService transportService = mock(TransportService.class);
    TrainingJobRouterTransportAction transportAction = new TrainingJobRouterTransportAction(transportService, new ActionFilters(Collections.emptySet()), clusterService, client);
    ActionListener<Integer> listener = ActionListener.wrap(size -> assertEquals(expectedSize, size.intValue()), e -> fail(e.getMessage()));
    transportAction.getTrainingIndexSizeInKB(trainingModelRequest, listener);
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) ActionFilters(org.opensearch.action.support.ActionFilters) SearchResponse(org.opensearch.action.search.SearchResponse) ClusterService(org.opensearch.cluster.service.ClusterService) ActionListener(org.opensearch.action.ActionListener) TransportService(org.opensearch.transport.TransportService) SearchHits(org.opensearch.search.SearchHits) Client(org.opensearch.client.Client)

Example 13 with Client

use of org.opensearch.client.Client in project anomaly-detection by opensearch-project.

the class NoPowermockSearchFeatureDaoTests method testGetHighestCountEntitiesExhaustedPages.

@SuppressWarnings("unchecked")
public void testGetHighestCountEntitiesExhaustedPages() throws InterruptedException {
    SearchResponse response1 = createPageResponse(attrs1);
    CompositeAggregation emptyComposite = mock(CompositeAggregation.class);
    when(emptyComposite.getName()).thenReturn(SearchFeatureDao.AGG_NAME_TOP);
    when(emptyComposite.afterKey()).thenReturn(null);
    // empty bucket
    when(emptyComposite.getBuckets()).thenAnswer((Answer<List<CompositeAggregation.Bucket>>) invocation -> {
        return new ArrayList<CompositeAggregation.Bucket>();
    });
    Aggregations emptyAggs = new Aggregations(Collections.singletonList(emptyComposite));
    SearchResponseSections emptySections = new SearchResponseSections(SearchHits.empty(), emptyAggs, null, false, null, null, 1);
    SearchResponse emptyResponse = new SearchResponse(emptySections, null, 1, 1, 0, 0, ShardSearchFailure.EMPTY_ARRAY, Clusters.EMPTY);
    CountDownLatch inProgress = new CountDownLatch(2);
    doAnswer(invocation -> {
        ActionListener<SearchResponse> listener = invocation.getArgument(1);
        inProgress.countDown();
        if (inProgress.getCount() == 1) {
            listener.onResponse(response1);
        } else {
            listener.onResponse(emptyResponse);
        }
        return null;
    }).when(client).search(any(), any());
    ActionListener<List<Entity>> listener = mock(ActionListener.class);
    searchFeatureDao = new SearchFeatureDao(client, xContentRegistry(), interpolator, clientUtil, settings, clusterService, AnomalyDetectorSettings.NUM_SAMPLES_PER_TREE, clock, 2, 1, 60_000L);
    searchFeatureDao.getHighestCountEntities(detector, 10L, 20L, listener);
    ArgumentCaptor<List<Entity>> captor = ArgumentCaptor.forClass(List.class);
    verify(listener).onResponse(captor.capture());
    List<Entity> result = captor.getValue();
    assertEquals(1, result.size());
    assertEquals(Entity.createEntityByReordering(attrs1), result.get(0));
    // both counts are used in client.search
    assertTrue(inProgress.await(10000L, TimeUnit.MILLISECONDS));
}
Also used : Arrays(java.util.Arrays) IsInstanceOf.instanceOf(org.hamcrest.core.IsInstanceOf.instanceOf) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService) AbstractADTest(org.opensearch.ad.AbstractADTest) Releasables(org.opensearch.common.lease.Releasables) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) InternalMax(org.opensearch.search.aggregations.metrics.InternalMax) MockBigArrays(org.opensearch.common.util.MockBigArrays) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) SingleFeatureLinearUniformInterpolator(org.opensearch.ad.dataprocessor.SingleFeatureLinearUniformInterpolator) InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) ZoneOffset(java.time.ZoneOffset) ActionListener(org.opensearch.action.ActionListener) MockPageCacheRecycler(org.opensearch.common.util.MockPageCacheRecycler) DateFormatter(org.opensearch.common.time.DateFormatter) Client(org.opensearch.client.Client) HyperLogLogPlusPlus(org.opensearch.search.aggregations.metrics.HyperLogLogPlusPlus) Clusters(org.opensearch.action.search.SearchResponse.Clusters) BytesRef(org.apache.lucene.util.BytesRef) SearchHit(org.opensearch.search.SearchHit) Collection(java.util.Collection) Feature(org.opensearch.ad.model.Feature) Settings(org.opensearch.common.settings.Settings) StandardCharsets(java.nio.charset.StandardCharsets) InvocationTargetException(java.lang.reflect.InvocationTargetException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) LinearUniformInterpolator(org.opensearch.ad.dataprocessor.LinearUniformInterpolator) Logger(org.apache.logging.log4j.Logger) TestHelpers(org.opensearch.ad.TestHelpers) Entry(java.util.Map.Entry) Optional(java.util.Optional) Mockito.mock(org.mockito.Mockito.mock) InternalDateRange(org.opensearch.search.aggregations.bucket.range.InternalDateRange) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BucketOrder(org.opensearch.search.aggregations.BucketOrder) InternalFilter(org.opensearch.search.aggregations.bucket.filter.InternalFilter) StringTerms(org.opensearch.search.aggregations.bucket.terms.StringTerms) DocValueFormat(org.opensearch.search.DocValueFormat) AggregatorFactories(org.opensearch.search.aggregations.AggregatorFactories) HashMap(java.util.HashMap) Aggregations(org.opensearch.search.aggregations.Aggregations) AbstractHyperLogLogPlusPlus(org.opensearch.search.aggregations.metrics.AbstractHyperLogLogPlusPlus) SearchHits(org.opensearch.search.SearchHits) SimpleImmutableEntry(java.util.AbstractMap.SimpleImmutableEntry) BitMixer(com.carrotsearch.hppc.BitMixer) Constructor(java.lang.reflect.Constructor) AnomalyDetectorSettings(org.opensearch.ad.settings.AnomalyDetectorSettings) InternalOrder(org.opensearch.search.aggregations.InternalOrder) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ArgumentCaptor(org.mockito.ArgumentCaptor) ImmutableList(com.google.common.collect.ImmutableList) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) SearchRequest(org.opensearch.action.search.SearchRequest) SearchResponse(org.opensearch.action.search.SearchResponse) ClusterSettings(org.opensearch.common.settings.ClusterSettings) InternalCardinality(org.opensearch.search.aggregations.metrics.InternalCardinality) QueryBuilders(org.opensearch.index.query.QueryBuilders) AbstractHyperLogLog(org.opensearch.search.aggregations.metrics.AbstractHyperLogLog) ClientUtil(org.opensearch.ad.util.ClientUtil) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) CompositeAggregation(org.opensearch.search.aggregations.bucket.composite.CompositeAggregation) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) TotalHits(org.apache.lucene.search.TotalHits) InternalFilters(org.opensearch.search.aggregations.bucket.filter.InternalFilters) ChronoUnit(java.time.temporal.ChronoUnit) Entity(org.opensearch.ad.model.Entity) SearchResponseSections(org.opensearch.action.search.SearchResponseSections) DateFieldMapper(org.opensearch.index.mapper.DateFieldMapper) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) InternalBucket(org.opensearch.search.aggregations.bucket.filter.InternalFilters.InternalBucket) ClusterService(org.opensearch.cluster.service.ClusterService) Clock(java.time.Clock) IntervalTimeConfiguration(org.opensearch.ad.model.IntervalTimeConfiguration) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) SumAggregationBuilder(org.opensearch.search.aggregations.metrics.SumAggregationBuilder) Entity(org.opensearch.ad.model.Entity) CompositeAggregation(org.opensearch.search.aggregations.bucket.composite.CompositeAggregation) SearchResponseSections(org.opensearch.action.search.SearchResponseSections) InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) Aggregations(org.opensearch.search.aggregations.Aggregations) CountDownLatch(java.util.concurrent.CountDownLatch) SearchResponse(org.opensearch.action.search.SearchResponse) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) InternalBucket(org.opensearch.search.aggregations.bucket.filter.InternalFilters.InternalBucket) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList)

Example 14 with Client

use of org.opensearch.client.Client in project anomaly-detection by opensearch-project.

the class NoPowermockSearchFeatureDaoTests method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    serviceField = "service";
    hostField = "host";
    detector = mock(AnomalyDetector.class);
    when(detector.isMultientityDetector()).thenReturn(true);
    when(detector.getCategoryField()).thenReturn(Arrays.asList(new String[] { serviceField, hostField }));
    detectorId = "123";
    when(detector.getDetectorId()).thenReturn(detectorId);
    when(detector.getTimeField()).thenReturn("testTimeField");
    when(detector.getIndices()).thenReturn(Arrays.asList("testIndices"));
    IntervalTimeConfiguration detectionInterval = new IntervalTimeConfiguration(1, ChronoUnit.MINUTES);
    when(detector.getDetectionInterval()).thenReturn(detectionInterval);
    when(detector.getFilterQuery()).thenReturn(QueryBuilders.matchAllQuery());
    client = mock(Client.class);
    interpolator = new LinearUniformInterpolator(new SingleFeatureLinearUniformInterpolator());
    clientUtil = mock(ClientUtil.class);
    settings = Settings.EMPTY;
    ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.unmodifiableSet(new HashSet<>(Arrays.asList(AnomalyDetectorSettings.MAX_ENTITIES_FOR_PREVIEW, AnomalyDetectorSettings.PAGE_SIZE))));
    clusterService = mock(ClusterService.class);
    when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
    clock = mock(Clock.class);
    searchFeatureDao = new SearchFeatureDao(client, // Important. Without this, ParseUtils cannot parse anything
    xContentRegistry(), interpolator, clientUtil, settings, clusterService, AnomalyDetectorSettings.NUM_SAMPLES_PER_TREE, clock, 1, 1, 60_000L);
    String app0 = "app_0";
    String server1 = "server_1";
    attrs1 = new HashMap<>();
    attrs1.put(serviceField, app0);
    attrs1.put(hostField, server1);
    String server2 = "server_2";
    attrs1 = new HashMap<>();
    attrs1.put(serviceField, app0);
    attrs1.put(hostField, server2);
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) ClientUtil(org.opensearch.ad.util.ClientUtil) IntervalTimeConfiguration(org.opensearch.ad.model.IntervalTimeConfiguration) Clock(java.time.Clock) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) SingleFeatureLinearUniformInterpolator(org.opensearch.ad.dataprocessor.SingleFeatureLinearUniformInterpolator) ClusterService(org.opensearch.cluster.service.ClusterService) SingleFeatureLinearUniformInterpolator(org.opensearch.ad.dataprocessor.SingleFeatureLinearUniformInterpolator) LinearUniformInterpolator(org.opensearch.ad.dataprocessor.LinearUniformInterpolator) Client(org.opensearch.client.Client) HashSet(java.util.HashSet)

Example 15 with Client

use of org.opensearch.client.Client in project anomaly-detection by opensearch-project.

the class RolloverTests method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    Client client = mock(Client.class);
    indicesClient = mock(IndicesAdminClient.class);
    AdminClient adminClient = mock(AdminClient.class);
    clusterService = mock(ClusterService.class);
    ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.unmodifiableSet(new HashSet<>(Arrays.asList(AnomalyDetectorSettings.AD_RESULT_HISTORY_MAX_DOCS_PER_SHARD, AnomalyDetectorSettings.AD_RESULT_HISTORY_ROLLOVER_PERIOD, AnomalyDetectorSettings.AD_RESULT_HISTORY_RETENTION_PERIOD, AnomalyDetectorSettings.MAX_PRIMARY_SHARDS))));
    clusterName = new ClusterName("test");
    when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
    ThreadPool threadPool = mock(ThreadPool.class);
    Settings settings = Settings.EMPTY;
    when(client.admin()).thenReturn(adminClient);
    when(adminClient.indices()).thenReturn(indicesClient);
    DiscoveryNodeFilterer nodeFilter = mock(DiscoveryNodeFilterer.class);
    numberOfNodes = 2;
    when(nodeFilter.getNumberOfEligibleDataNodes()).thenReturn(numberOfNodes);
    adIndices = new AnomalyDetectionIndices(client, clusterService, threadPool, settings, nodeFilter, AnomalyDetectorSettings.MAX_UPDATE_RETRY_TIMES);
    clusterAdminClient = mock(ClusterAdminClient.class);
    when(adminClient.cluster()).thenReturn(clusterAdminClient);
    doAnswer(invocation -> {
        ClusterStateRequest clusterStateRequest = invocation.getArgument(0);
        assertEquals(AnomalyDetectionIndices.ALL_AD_RESULTS_INDEX_PATTERN, clusterStateRequest.indices()[0]);
        @SuppressWarnings("unchecked") ActionListener<ClusterStateResponse> listener = (ActionListener<ClusterStateResponse>) invocation.getArgument(1);
        listener.onResponse(new ClusterStateResponse(clusterName, clusterState, true));
        return null;
    }).when(clusterAdminClient).state(any(), any());
    defaultMaxDocs = AnomalyDetectorSettings.AD_RESULT_HISTORY_MAX_DOCS_PER_SHARD.getDefault(Settings.EMPTY);
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) DiscoveryNodeFilterer(org.opensearch.ad.util.DiscoveryNodeFilterer) ClusterAdminClient(org.opensearch.client.ClusterAdminClient) ClusterStateResponse(org.opensearch.action.admin.cluster.state.ClusterStateResponse) ClusterStateRequest(org.opensearch.action.admin.cluster.state.ClusterStateRequest) ThreadPool(org.opensearch.threadpool.ThreadPool) IndicesAdminClient(org.opensearch.client.IndicesAdminClient) ClusterService(org.opensearch.cluster.service.ClusterService) ActionListener(org.opensearch.action.ActionListener) ClusterName(org.opensearch.cluster.ClusterName) Client(org.opensearch.client.Client) AdminClient(org.opensearch.client.AdminClient) ClusterAdminClient(org.opensearch.client.ClusterAdminClient) IndicesAdminClient(org.opensearch.client.IndicesAdminClient) AnomalyDetectorSettings(org.opensearch.ad.settings.AnomalyDetectorSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings) AdminClient(org.opensearch.client.AdminClient) ClusterAdminClient(org.opensearch.client.ClusterAdminClient) IndicesAdminClient(org.opensearch.client.IndicesAdminClient) HashSet(java.util.HashSet)

Aggregations

Client (org.opensearch.client.Client)324 Settings (org.opensearch.common.settings.Settings)130 Test (org.junit.Test)90 IndexRequest (org.opensearch.action.index.IndexRequest)86 RestHelper (org.opensearch.security.test.helper.rest.RestHelper)69 ClusterService (org.opensearch.cluster.service.ClusterService)65 ArrayList (java.util.ArrayList)60 ActionListener (org.opensearch.action.ActionListener)59 SingleClusterTest (org.opensearch.security.test.SingleClusterTest)57 HttpResponse (org.opensearch.security.test.helper.rest.RestHelper.HttpResponse)57 List (java.util.List)53 Matchers.containsString (org.hamcrest.Matchers.containsString)51 SearchResponse (org.opensearch.action.search.SearchResponse)49 IOException (java.io.IOException)48 Map (java.util.Map)48 ThreadPool (org.opensearch.threadpool.ThreadPool)48 CreateIndexRequest (org.opensearch.action.admin.indices.create.CreateIndexRequest)46 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)43 HashSet (java.util.HashSet)39 GetResponse (org.opensearch.action.get.GetResponse)38