Search in sources :

Example 11 with IndexNameExpressionResolver

use of org.opensearch.cluster.metadata.IndexNameExpressionResolver in project OpenSearch by opensearch-project.

the class BroadcastReplicationTests method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    MockNioTransport transport = new MockNioTransport(Settings.EMPTY, Version.CURRENT, threadPool, new NetworkService(Collections.emptyList()), PageCacheRecycler.NON_RECYCLING_INSTANCE, new NamedWriteableRegistry(Collections.emptyList()), circuitBreakerService);
    clusterService = createClusterService(threadPool);
    transportService = new TransportService(clusterService.getSettings(), transport, threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> clusterService.localNode(), null, Collections.emptySet());
    transportService.start();
    transportService.acceptIncomingRequests();
    broadcastReplicationAction = new TestBroadcastReplicationAction(clusterService, transportService, new ActionFilters(new HashSet<>()), new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY)), null);
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) Date(java.util.Date) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService) TestThreadPool(org.opensearch.threadpool.TestThreadPool) Version(org.opensearch.Version) ClusterServiceUtils.setState(org.opensearch.test.ClusterServiceUtils.setState) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) ConcurrentCollections(org.opensearch.common.util.concurrent.ConcurrentCollections) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) After(org.junit.After) ActionListener(org.opensearch.action.ActionListener) UnavailableShardsException(org.opensearch.action.UnavailableShardsException) AfterClass(org.junit.AfterClass) Matchers.lessThanOrEqualTo(org.hamcrest.Matchers.lessThanOrEqualTo) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) Task(org.opensearch.tasks.Task) RestStatus(org.opensearch.rest.RestStatus) ClusterStateCreationUtils.state(org.opensearch.action.support.replication.ClusterStateCreationUtils.state) TransportService(org.opensearch.transport.TransportService) Tuple(org.opensearch.common.collect.Tuple) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) ActionFilters(org.opensearch.action.support.ActionFilters) List(java.util.List) ActionTestUtils(org.opensearch.action.support.ActionTestUtils) Matchers.equalTo(org.hamcrest.Matchers.equalTo) BroadcastResponse(org.opensearch.action.support.broadcast.BroadcastResponse) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) FlushResponse(org.opensearch.action.admin.indices.flush.FlushResponse) BroadcastRequest(org.opensearch.action.support.broadcast.BroadcastRequest) BeforeClass(org.junit.BeforeClass) FlushRequest(org.opensearch.action.admin.indices.flush.FlushRequest) ThreadPool(org.opensearch.threadpool.ThreadPool) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) HashSet(java.util.HashSet) ClusterState(org.opensearch.cluster.ClusterState) ShardRoutingState(org.opensearch.cluster.routing.ShardRoutingState) Before(org.junit.Before) StreamInput(org.opensearch.common.io.stream.StreamInput) MockNioTransport(org.opensearch.transport.nio.MockNioTransport) ClusterStateCreationUtils.stateWithAssignedPrimariesAndOneReplica(org.opensearch.action.support.replication.ClusterStateCreationUtils.stateWithAssignedPrimariesAndOneReplica) ClusterServiceUtils.createClusterService(org.opensearch.test.ClusterServiceUtils.createClusterService) TransportFlushAction(org.opensearch.action.admin.indices.flush.TransportFlushAction) IOException(java.io.IOException) IOUtils(org.opensearch.core.internal.io.IOUtils) ShardId(org.opensearch.index.shard.ShardId) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ClusterStateCreationUtils.stateWithNoShard(org.opensearch.action.support.replication.ClusterStateCreationUtils.stateWithNoShard) CircuitBreakerService(org.opensearch.indices.breaker.CircuitBreakerService) ClusterService(org.opensearch.cluster.service.ClusterService) NetworkService(org.opensearch.common.network.NetworkService) NoShardAvailableActionException(org.opensearch.action.NoShardAvailableActionException) Collections(java.util.Collections) PageCacheRecycler(org.opensearch.common.util.PageCacheRecycler) TransportService(org.opensearch.transport.TransportService) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) MockNioTransport(org.opensearch.transport.nio.MockNioTransport) NetworkService(org.opensearch.common.network.NetworkService) ActionFilters(org.opensearch.action.support.ActionFilters) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) Before(org.junit.Before)

Example 12 with IndexNameExpressionResolver

use of org.opensearch.cluster.metadata.IndexNameExpressionResolver in project OpenSearch by opensearch-project.

the class SearchIndexNameMatcherTests method setUpMatchers.

@Before
public void setUpMatchers() {
    Metadata.Builder metadataBuilder = Metadata.builder().put(indexBuilder("index1").putAlias(AliasMetadata.builder("alias"))).put(indexBuilder("index2").putAlias(AliasMetadata.builder("alias"))).put(indexBuilder("index3"));
    ClusterState state = ClusterState.builder(new ClusterName("_name")).metadata(metadataBuilder).build();
    ClusterService clusterService = mock(ClusterService.class);
    when(clusterService.state()).thenReturn(state);
    matcher = new SearchIndexNameMatcher("index1", "", clusterService, new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY)));
    remoteMatcher = new SearchIndexNameMatcher("index1", "cluster", clusterService, new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY)));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) ClusterService(org.opensearch.cluster.service.ClusterService) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) ClusterName(org.opensearch.cluster.ClusterName) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) Before(org.junit.Before)

Example 13 with IndexNameExpressionResolver

use of org.opensearch.cluster.metadata.IndexNameExpressionResolver in project OpenSearch by opensearch-project.

the class IndexModuleTests method testRegisterCustomRecoveryStateFactory.

public void testRegisterCustomRecoveryStateFactory() throws IOException {
    final Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).put(IndexModule.INDEX_RECOVERY_TYPE_SETTING.getKey(), "test_recovery").build();
    final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings(index, settings);
    RecoveryState recoveryState = mock(RecoveryState.class);
    final Map<String, IndexStorePlugin.RecoveryStateFactory> recoveryStateFactories = singletonMap("test_recovery", (shardRouting, targetNode, sourceNode) -> recoveryState);
    final IndexModule module = new IndexModule(indexSettings, emptyAnalysisRegistry, new InternalEngineFactory(), new EngineConfigFactory(indexSettings), Collections.emptyMap(), () -> true, new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY)), recoveryStateFactories);
    final IndexService indexService = newIndexService(module);
    ShardRouting shard = createInitializedShardRouting();
    assertThat(indexService.createRecoveryState(shard, mock(DiscoveryNode.class), mock(DiscoveryNode.class)), is(recoveryState));
    indexService.close("closing", false);
}
Also used : ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) EngineConfigFactory(org.opensearch.index.engine.EngineConfigFactory) InternalEngineFactory(org.opensearch.index.engine.InternalEngineFactory) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) ShardRouting(org.opensearch.cluster.routing.ShardRouting) RecoveryState(org.opensearch.indices.recovery.RecoveryState) Settings(org.opensearch.common.settings.Settings)

Example 14 with IndexNameExpressionResolver

use of org.opensearch.cluster.metadata.IndexNameExpressionResolver in project OpenSearch by opensearch-project.

the class IndexModuleTests method testRegisterIndexStore.

public void testRegisterIndexStore() throws IOException {
    final Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).put(IndexModule.INDEX_STORE_TYPE_SETTING.getKey(), "foo_store").build();
    final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings(index, settings);
    final Map<String, IndexStorePlugin.DirectoryFactory> indexStoreFactories = singletonMap("foo_store", new FooFunction());
    final IndexModule module = new IndexModule(indexSettings, emptyAnalysisRegistry, new InternalEngineFactory(), new EngineConfigFactory(indexSettings), indexStoreFactories, () -> true, new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY)), Collections.emptyMap());
    final IndexService indexService = newIndexService(module);
    assertThat(indexService.getDirectoryFactory(), instanceOf(FooFunction.class));
    indexService.close("simon says", false);
}
Also used : EngineConfigFactory(org.opensearch.index.engine.EngineConfigFactory) FsDirectoryFactory(org.opensearch.index.store.FsDirectoryFactory) InternalEngineFactory(org.opensearch.index.engine.InternalEngineFactory) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) Settings(org.opensearch.common.settings.Settings)

Example 15 with IndexNameExpressionResolver

use of org.opensearch.cluster.metadata.IndexNameExpressionResolver in project ml-commons by opensearch-project.

the class AnomalyLocalizerImplTests method setup.

@Before
@SuppressWarnings("unchecked")
public void setup() {
    MockitoAnnotations.openMocks(this);
    settings = Settings.builder().build();
    testState = setupTestClusterState();
    IndicesOptions = new String[] { "indexName" };
    invalidIndicesOptions = new String[] {};
    anomalyLocalizer = spy(new AnomalyLocalizerImpl(client, settings, clusterService, indexNameExpressionResolver));
    input = new AnomalyLocalizationInput(indexName, Arrays.asList(attributeFieldNameOne), Arrays.asList(agg), timeFieldName, startTime, endTime, minTimeInterval, numOutput, Optional.empty(), Optional.empty());
    when(valueOne.value()).thenReturn(0.);
    when(valueOne.getName()).thenReturn(agg.getName());
    SearchResponse respOne = mock(SearchResponse.class);
    when(respOne.getAggregations()).thenReturn(new Aggregations(Arrays.asList(valueOne)));
    MultiSearchResponse.Item itemOne = new MultiSearchResponse.Item(respOne, null);
    when(valueTwo.value()).thenReturn(10.);
    when(valueTwo.getName()).thenReturn(agg.getName());
    SearchResponse respTwo = mock(SearchResponse.class);
    when(respTwo.getAggregations()).thenReturn(new Aggregations(Arrays.asList(valueTwo)));
    MultiSearchResponse.Item itemTwo = new MultiSearchResponse.Item(respTwo, null);
    MultiSearchResponse multiSearchResponse = new MultiSearchResponse(new MultiSearchResponse.Item[] { itemOne, itemTwo }, 0);
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        ActionListener<MultiSearchResponse> listener = (ActionListener<MultiSearchResponse>) args[1];
        listener.onResponse(multiSearchResponse);
        return null;
    }).when(client).multiSearch(any(), any());
    CompositeAggregation.Bucket bucketOne = mock(CompositeAggregation.Bucket.class);
    Map<String, Object> bucketOneKey = new HashMap<>();
    String bucketOneKeyValue = "bucketOneKeyValue";
    bucketOneKey.put(attributeFieldNameOne, bucketOneKeyValue);
    when(bucketOne.getKey()).thenReturn(bucketOneKey);
    when(bucketOne.getAggregations()).thenReturn(new Aggregations(Arrays.asList(valueOne)));
    CompositeAggregation compositeOne = mock(CompositeAggregation.class);
    when(compositeOne.getName()).thenReturn(agg.getName());
    doReturn(Arrays.asList(bucketOne)).when(compositeOne).getBuckets();
    when(compositeOne.afterKey()).thenReturn(bucketOneKey);
    SearchResponse respBucketOne = mock(SearchResponse.class);
    when(respBucketOne.getAggregations()).thenReturn(new Aggregations(Arrays.asList(compositeOne))).thenReturn(new Aggregations(Collections.emptyList()));
    CompositeAggregation.Bucket bucketOneNew = mock(CompositeAggregation.Bucket.class);
    when(bucketOneNew.getKey()).thenReturn(bucketOneKey);
    when(bucketOneNew.getAggregations()).thenReturn(new Aggregations(Arrays.asList(valueTwo)));
    Map<String, Object> bucketTwoKey = new HashMap<>();
    String bucketTwoKeyValue = "bucketTwoKeyValue";
    bucketTwoKey.put(attributeFieldNameOne, bucketTwoKeyValue);
    when(valueThree.value()).thenReturn(0.);
    when(valueThree.getName()).thenReturn(agg.getName());
    CompositeAggregation.Bucket bucketTwoNew = mock(CompositeAggregation.Bucket.class);
    when(bucketTwoNew.getKey()).thenReturn(bucketTwoKey);
    when(bucketTwoNew.getAggregations()).thenReturn(new Aggregations(Arrays.asList(valueThree)));
    CompositeAggregation compositeTwo = mock(CompositeAggregation.class);
    when(compositeTwo.getName()).thenReturn(agg.getName());
    doReturn(Arrays.asList(bucketTwoNew, bucketOneNew, bucketTwoNew)).when(compositeTwo).getBuckets();
    when(compositeTwo.afterKey()).thenReturn(bucketOneKey);
    SearchResponse respBucketTwo = mock(SearchResponse.class);
    when(respBucketTwo.getAggregations()).thenReturn(new Aggregations(Arrays.asList(compositeTwo))).thenReturn(new Aggregations(Collections.emptyList()));
    Filters.Bucket filterBucketOne = mock(Filters.Bucket.class);
    when(filterBucketOne.getKeyAsString()).thenReturn(String.valueOf(0));
    when(filterBucketOne.getAggregations()).thenReturn(new Aggregations(Arrays.asList(valueOne)));
    Filters filters = mock(Filters.class);
    when(filters.getName()).thenReturn(agg.getName());
    doReturn(Arrays.asList(filterBucketOne)).when(filters).getBuckets();
    SearchResponse filtersResp = mock(SearchResponse.class);
    when(filtersResp.getAggregations()).thenReturn(new Aggregations(Arrays.asList(filters)));
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        ActionListener<SearchResponse> listener = (ActionListener<SearchResponse>) args[1];
        listener.onResponse(respBucketOne);
        return null;
    }).doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        ActionListener<SearchResponse> listener = (ActionListener<SearchResponse>) args[1];
        listener.onResponse(respBucketOne);
        return null;
    }).doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        ActionListener<SearchResponse> listener = (ActionListener<SearchResponse>) args[1];
        listener.onResponse(respBucketTwo);
        return null;
    }).doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        ActionListener<SearchResponse> listener = (ActionListener<SearchResponse>) args[1];
        listener.onResponse(respBucketTwo);
        return null;
    }).doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        ActionListener<SearchResponse> listener = (ActionListener<SearchResponse>) args[1];
        listener.onResponse(filtersResp);
        return null;
    }).when(client).search(any(), any());
    expectedOutput = new AnomalyLocalizationOutput();
    AnomalyLocalizationOutput.Result result = new AnomalyLocalizationOutput.Result();
    expectedBucketOne = new AnomalyLocalizationOutput.Bucket();
    expectedBucketOne.setStartTime(0);
    expectedBucketOne.setEndTime(1);
    expectedBucketOne.setOverallAggValue(0);
    expectedBucketTwo = new AnomalyLocalizationOutput.Bucket();
    expectedBucketTwo.setStartTime(1);
    expectedBucketTwo.setEndTime(2);
    expectedBucketTwo.setOverallAggValue(10);
    entity = new AnomalyLocalizationOutput.Entity();
    entity.setKey(Arrays.asList(bucketOneKeyValue));
    entity.setNewValue(valueTwo.value());
    entity.setBaseValue(valueOne.value());
    entity.setContributionValue(valueTwo.value());
    expectedBucketTwo.setEntities(Arrays.asList(entity));
    result.getBuckets().add(expectedBucketOne);
    result.getBuckets().add(expectedBucketTwo);
    expectedOutput.getResults().put(agg.getName(), result);
}
Also used : Arrays(java.util.Arrays) ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) Metadata(org.opensearch.cluster.metadata.Metadata) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Version(org.opensearch.Version) MockitoAnnotations(org.mockito.MockitoAnnotations) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) AnomalyLocalizationInput(org.opensearch.ml.common.input.execute.anomalylocalization.AnomalyLocalizationInput) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ActionListener(org.opensearch.action.ActionListener) Mockito.doReturn(org.mockito.Mockito.doReturn) AfterClass(org.junit.AfterClass) Client(org.opensearch.client.Client) Filters(org.opensearch.search.aggregations.bucket.filter.Filters) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) DiscoveryNodeRole(org.opensearch.cluster.node.DiscoveryNodeRole) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) TransportAddress(org.opensearch.common.transport.TransportAddress) QueryBuilder(org.opensearch.index.query.QueryBuilder) SingleValue(org.opensearch.search.aggregations.metrics.NumericMetricsAggregation.SingleValue) Optional(java.util.Optional) Mockito.any(org.mockito.Mockito.any) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) Mockito.mock(org.mockito.Mockito.mock) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) Mock(org.mockito.Mock) HashMap(java.util.HashMap) Aggregations(org.opensearch.search.aggregations.Aggregations) IndicesOptions(org.opensearch.action.support.IndicesOptions) Mockito.spy(org.mockito.Mockito.spy) HashSet(java.util.HashSet) ClusterState(org.opensearch.cluster.ClusterState) ArgumentCaptor(org.mockito.ArgumentCaptor) AnomalyLocalizationOutput(org.opensearch.ml.common.output.execute.anomalylocalization.AnomalyLocalizationOutput) SearchResponse(org.opensearch.action.search.SearchResponse) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) CompositeAggregation(org.opensearch.search.aggregations.bucket.composite.CompositeAggregation) Mockito.verify(org.mockito.Mockito.verify) AggregationBuilders(org.opensearch.search.aggregations.AggregationBuilders) Rule(org.junit.Rule) ClusterService(org.opensearch.cluster.service.ClusterService) ClusterName(org.opensearch.cluster.ClusterName) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CompositeAggregation(org.opensearch.search.aggregations.bucket.composite.CompositeAggregation) HashMap(java.util.HashMap) Aggregations(org.opensearch.search.aggregations.Aggregations) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Filters(org.opensearch.search.aggregations.bucket.filter.Filters) AnomalyLocalizationInput(org.opensearch.ml.common.input.execute.anomalylocalization.AnomalyLocalizationInput) AnomalyLocalizationOutput(org.opensearch.ml.common.output.execute.anomalylocalization.AnomalyLocalizationOutput) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) ActionListener(org.opensearch.action.ActionListener) Before(org.junit.Before)

Aggregations

IndexNameExpressionResolver (org.opensearch.cluster.metadata.IndexNameExpressionResolver)39 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)26 ClusterState (org.opensearch.cluster.ClusterState)20 Matchers.containsString (org.hamcrest.Matchers.containsString)18 Settings (org.opensearch.common.settings.Settings)15 ThreadPool (org.opensearch.threadpool.ThreadPool)14 ClusterService (org.opensearch.cluster.service.ClusterService)11 Metadata (org.opensearch.cluster.metadata.Metadata)10 Tuple (org.opensearch.common.collect.Tuple)10 IOException (java.io.IOException)9 Before (org.junit.Before)9 ClusterName (org.opensearch.cluster.ClusterName)9 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)9 TestThreadPool (org.opensearch.threadpool.TestThreadPool)8 ActionListener (org.opensearch.action.ActionListener)7 ActionFilters (org.opensearch.action.support.ActionFilters)7 ClusterSettings (org.opensearch.common.settings.ClusterSettings)7 Index (org.opensearch.index.Index)7 TransportService (org.opensearch.transport.TransportService)7 HashSet (java.util.HashSet)5