Search in sources :

Example 21 with Tuple

use of org.opensearch.common.collect.Tuple in project OpenSearch by opensearch-project.

the class TransportIndicesShardStoresAction method masterOperation.

@Override
protected void masterOperation(IndicesShardStoresRequest request, ClusterState state, ActionListener<IndicesShardStoresResponse> listener) {
    final RoutingTable routingTables = state.routingTable();
    final RoutingNodes routingNodes = state.getRoutingNodes();
    final String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(state, request);
    final Set<Tuple<ShardId, String>> shardsToFetch = new HashSet<>();
    logger.trace("using cluster state version [{}] to determine shards", state.version());
    // collect relevant shard ids of the requested indices for fetching store infos
    for (String index : concreteIndices) {
        IndexRoutingTable indexShardRoutingTables = routingTables.index(index);
        if (indexShardRoutingTables == null) {
            continue;
        }
        final String customDataPath = IndexMetadata.INDEX_DATA_PATH_SETTING.get(state.metadata().index(index).getSettings());
        for (IndexShardRoutingTable routing : indexShardRoutingTables) {
            final int shardId = routing.shardId().id();
            ClusterShardHealth shardHealth = new ClusterShardHealth(shardId, routing);
            if (request.shardStatuses().contains(shardHealth.getStatus())) {
                shardsToFetch.add(Tuple.tuple(routing.shardId(), customDataPath));
            }
        }
    }
    // async fetch store infos from all the nodes
    // NOTE: instead of fetching shard store info one by one from every node (nShards * nNodes requests)
    // we could fetch all shard store info from every node once (nNodes requests)
    // we have to implement a TransportNodesAction instead of using TransportNodesListGatewayStartedShards
    // for fetching shard stores info, that operates on a list of shards instead of a single shard
    new AsyncShardStoresInfoFetches(state.nodes(), routingNodes, shardsToFetch, listener).start();
}
Also used : IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) RoutingTable(org.opensearch.cluster.routing.RoutingTable) RoutingNodes(org.opensearch.cluster.routing.RoutingNodes) ClusterShardHealth(org.opensearch.cluster.health.ClusterShardHealth) Tuple(org.opensearch.common.collect.Tuple) HashSet(java.util.HashSet)

Example 22 with Tuple

use of org.opensearch.common.collect.Tuple in project OpenSearch by opensearch-project.

the class MetadataIndexStateService method closeIndices.

/**
 * Closes one or more indices.
 *
 * Closing indices is a 3 steps process: it first adds a write block to every indices to close, then waits for the operations on shards
 * to be terminated and finally closes the indices by moving their state to CLOSE.
 */
public void closeIndices(final CloseIndexClusterStateUpdateRequest request, final ActionListener<CloseIndexResponse> listener) {
    final Index[] concreteIndices = request.indices();
    if (concreteIndices == null || concreteIndices.length == 0) {
        throw new IllegalArgumentException("Index name is required");
    }
    List<String> writeIndices = new ArrayList<>();
    SortedMap<String, IndexAbstraction> lookup = clusterService.state().metadata().getIndicesLookup();
    for (Index index : concreteIndices) {
        IndexAbstraction ia = lookup.get(index.getName());
        if (ia != null && ia.getParentDataStream() != null && ia.getParentDataStream().getWriteIndex().getIndex().equals(index)) {
            writeIndices.add(index.getName());
        }
    }
    if (writeIndices.size() > 0) {
        throw new IllegalArgumentException("cannot close the following data stream write indices [" + Strings.collectionToCommaDelimitedString(writeIndices) + "]");
    }
    clusterService.submitStateUpdateTask("add-block-index-to-close " + Arrays.toString(concreteIndices), new ClusterStateUpdateTask(Priority.URGENT) {

        private final Map<Index, ClusterBlock> blockedIndices = new HashMap<>();

        @Override
        public ClusterState execute(final ClusterState currentState) {
            return addIndexClosedBlocks(concreteIndices, blockedIndices, currentState);
        }

        @Override
        public void clusterStateProcessed(final String source, final ClusterState oldState, final ClusterState newState) {
            if (oldState == newState) {
                assert blockedIndices.isEmpty() : "List of blocked indices is not empty but cluster state wasn't changed";
                listener.onResponse(new CloseIndexResponse(true, false, Collections.emptyList()));
            } else {
                assert blockedIndices.isEmpty() == false : "List of blocked indices is empty but cluster state was changed";
                threadPool.executor(ThreadPool.Names.MANAGEMENT).execute(new WaitForClosedBlocksApplied(blockedIndices, request, ActionListener.wrap(verifyResults -> clusterService.submitStateUpdateTask("close-indices", new ClusterStateUpdateTask(Priority.URGENT) {

                    private final List<IndexResult> indices = new ArrayList<>();

                    @Override
                    public ClusterState execute(final ClusterState currentState) throws Exception {
                        Tuple<ClusterState, Collection<IndexResult>> closingResult = closeRoutingTable(currentState, blockedIndices, verifyResults);
                        assert verifyResults.size() == closingResult.v2().size();
                        indices.addAll(closingResult.v2());
                        return allocationService.reroute(closingResult.v1(), "indices closed");
                    }

                    @Override
                    public void onFailure(final String source, final Exception e) {
                        listener.onFailure(e);
                    }

                    @Override
                    public void clusterStateProcessed(final String source, final ClusterState oldState, final ClusterState newState) {
                        final boolean acknowledged = indices.stream().noneMatch(IndexResult::hasFailures);
                        final String[] waitForIndices = indices.stream().filter(result -> result.hasFailures() == false).filter(result -> newState.routingTable().hasIndex(result.getIndex())).map(result -> result.getIndex().getName()).toArray(String[]::new);
                        if (waitForIndices.length > 0) {
                            activeShardsObserver.waitForActiveShards(waitForIndices, request.waitForActiveShards(), request.ackTimeout(), shardsAcknowledged -> {
                                if (shardsAcknowledged == false) {
                                    logger.debug("[{}] indices closed, but the operation timed out while waiting " + "for enough shards to be started.", Arrays.toString(waitForIndices));
                                }
                                // acknowledged maybe be false but some indices may have been correctly
                                // closed, so
                                // we maintain a kind of coherency by overriding the shardsAcknowledged
                                // value
                                // (see ShardsAcknowledgedResponse constructor)
                                boolean shardsAcked = acknowledged ? shardsAcknowledged : false;
                                listener.onResponse(new CloseIndexResponse(acknowledged, shardsAcked, indices));
                            }, listener::onFailure);
                        } else {
                            listener.onResponse(new CloseIndexResponse(acknowledged, false, indices));
                        }
                    }
                }), listener::onFailure)));
            }
        }

        @Override
        public void onFailure(final String source, final Exception e) {
            listener.onFailure(e);
        }

        @Override
        public TimeValue timeout() {
            return request.masterNodeTimeout();
        }
    });
}
Also used : Arrays(java.util.Arrays) CountDown(org.opensearch.common.util.concurrent.CountDown) NotifyOnceListener(org.opensearch.action.NotifyOnceListener) AllocationService(org.opensearch.cluster.routing.allocation.AllocationService) Version(org.opensearch.Version) OpenSearchException(org.opensearch.OpenSearchException) SnapshotsService(org.opensearch.snapshots.SnapshotsService) Strings(org.opensearch.common.Strings) CloseIndexResponse(org.opensearch.action.admin.indices.close.CloseIndexResponse) ConcurrentCollections(org.opensearch.common.util.concurrent.ConcurrentCollections) ClusterBlock(org.opensearch.cluster.block.ClusterBlock) Collections.singleton(java.util.Collections.singleton) TransportVerifyShardIndexBlockAction(org.opensearch.action.admin.indices.readonly.TransportVerifyShardIndexBlockAction) Map(java.util.Map) Inject(org.opensearch.common.inject.Inject) AddBlockShardResult(org.opensearch.action.admin.indices.readonly.AddIndexBlockResponse.AddBlockShardResult) ActionListener(org.opensearch.action.ActionListener) EnumSet(java.util.EnumSet) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) TimeValue(org.opensearch.common.unit.TimeValue) ImmutableOpenIntMap(org.opensearch.common.collect.ImmutableOpenIntMap) Index(org.opensearch.index.Index) Collection(java.util.Collection) IndicesService(org.opensearch.indices.IndicesService) SnapshotInProgressException(org.opensearch.snapshots.SnapshotInProgressException) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) RestStatus(org.opensearch.rest.RestStatus) ShardResult(org.opensearch.action.admin.indices.close.CloseIndexResponse.ShardResult) Collectors(java.util.stream.Collectors) Tuple(org.opensearch.common.collect.Tuple) List(java.util.List) Logger(org.apache.logging.log4j.Logger) ClusterStateUpdateTask(org.opensearch.cluster.ClusterStateUpdateTask) IndexResult(org.opensearch.action.admin.indices.close.CloseIndexResponse.IndexResult) ReplicationResponse(org.opensearch.action.support.replication.ReplicationResponse) AddIndexBlockClusterStateUpdateRequest(org.opensearch.action.admin.indices.readonly.AddIndexBlockClusterStateUpdateRequest) ClusterStateUpdateResponse(org.opensearch.cluster.ack.ClusterStateUpdateResponse) SortedMap(java.util.SortedMap) IntObjectCursor(com.carrotsearch.hppc.cursors.IntObjectCursor) APIBlock(org.opensearch.cluster.metadata.IndexMetadata.APIBlock) ActionRunnable(org.opensearch.action.ActionRunnable) ThreadPool(org.opensearch.threadpool.ThreadPool) Priority(org.opensearch.common.Priority) HashMap(java.util.HashMap) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) TransportVerifyShardBeforeCloseAction(org.opensearch.action.admin.indices.close.TransportVerifyShardBeforeCloseAction) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) IndexRoutingTable(org.opensearch.cluster.routing.IndexRoutingTable) CloseIndexClusterStateUpdateRequest(org.opensearch.action.admin.indices.close.CloseIndexClusterStateUpdateRequest) ClusterState(org.opensearch.cluster.ClusterState) AddIndexBlockResponse(org.opensearch.action.admin.indices.readonly.AddIndexBlockResponse) LegacyESVersion(org.opensearch.LegacyESVersion) RestoreService(org.opensearch.snapshots.RestoreService) AckedClusterStateUpdateTask(org.opensearch.cluster.AckedClusterStateUpdateTask) UUIDs(org.opensearch.common.UUIDs) ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks) Setting(org.opensearch.common.settings.Setting) ShardLimitValidator(org.opensearch.indices.ShardLimitValidator) ClusterBlockLevel(org.opensearch.cluster.block.ClusterBlockLevel) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) TaskId(org.opensearch.tasks.TaskId) ShardId(org.opensearch.index.shard.ShardId) Consumer(java.util.function.Consumer) AtomicArray(org.opensearch.common.util.concurrent.AtomicArray) AddBlockResult(org.opensearch.action.admin.indices.readonly.AddIndexBlockResponse.AddBlockResult) ActiveShardsObserver(org.opensearch.action.support.ActiveShardsObserver) ClusterService(org.opensearch.cluster.service.ClusterService) RoutingTable(org.opensearch.cluster.routing.RoutingTable) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) OpenIndexClusterStateUpdateResponse(org.opensearch.cluster.ack.OpenIndexClusterStateUpdateResponse) OpenIndexClusterStateUpdateRequest(org.opensearch.action.admin.indices.open.OpenIndexClusterStateUpdateRequest) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) ClusterState(org.opensearch.cluster.ClusterState) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ClusterStateUpdateTask(org.opensearch.cluster.ClusterStateUpdateTask) AckedClusterStateUpdateTask(org.opensearch.cluster.AckedClusterStateUpdateTask) Index(org.opensearch.index.Index) OpenSearchException(org.opensearch.OpenSearchException) SnapshotInProgressException(org.opensearch.snapshots.SnapshotInProgressException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ClusterBlock(org.opensearch.cluster.block.ClusterBlock) CloseIndexResponse(org.opensearch.action.admin.indices.close.CloseIndexResponse) IndexResult(org.opensearch.action.admin.indices.close.CloseIndexResponse.IndexResult) Tuple(org.opensearch.common.collect.Tuple) TimeValue(org.opensearch.common.unit.TimeValue)

Example 23 with Tuple

use of org.opensearch.common.collect.Tuple in project OpenSearch by opensearch-project.

the class ScriptContextInfoTests method testPrimitiveContext.

public void testPrimitiveContext() {
    String name = "primitive_context";
    ScriptContextInfo info = new ScriptContextInfo(name, PrimitiveContext.class);
    assertEquals(name, info.name);
    assertEquals("execute", info.execute.name);
    assertEquals("int", info.execute.returnType);
    assertEquals(4, info.execute.parameters.size());
    List<Tuple<String, String>> eparams = new ArrayList<>();
    eparams.add(new Tuple<>("boolean", "foo"));
    eparams.add(new Tuple<>("long", "bar"));
    eparams.add(new Tuple<>("short", "baz"));
    eparams.add(new Tuple<>("float", "qux"));
    for (int i = 0; i < info.execute.parameters.size(); i++) {
        assertEquals(eparams.get(i).v1(), info.execute.parameters.get(i).type);
        assertEquals(eparams.get(i).v2(), info.execute.parameters.get(i).name);
    }
    assertEquals(2, info.getters.size());
    HashMap<String, String> getters = new HashMap<String, String>() {

        {
            put("getByte", "byte");
            put("getChar", "char");
        }
    };
    for (ScriptContextInfo.ScriptMethodInfo getter : info.getters) {
        assertEquals(0, getter.parameters.size());
        String returnType = getters.remove(getter.name);
        assertNotNull(returnType);
        assertEquals(returnType, getter.returnType);
    }
    assertEquals(0, getters.size());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ScriptMethodInfo(org.opensearch.script.ScriptContextInfo.ScriptMethodInfo) Tuple(org.opensearch.common.collect.Tuple)

Example 24 with Tuple

use of org.opensearch.common.collect.Tuple in project OpenSearch by opensearch-project.

the class NestedAggregatorTests method testNestedOrdering_random.

public void testNestedOrdering_random() throws IOException {
    int numBooks = randomIntBetween(32, 512);
    List<Tuple<String, int[]>> books = new ArrayList<>();
    for (int i = 0; i < numBooks; i++) {
        int numChapters = randomIntBetween(1, 8);
        int[] chapters = new int[numChapters];
        for (int j = 0; j < numChapters; j++) {
            chapters[j] = randomIntBetween(2, 64);
        }
        books.add(Tuple.tuple(String.format(Locale.ROOT, "%03d", i), chapters));
    }
    try (Directory directory = newDirectory()) {
        try (RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) {
            int id = 0;
            for (Tuple<String, int[]> book : books) {
                iw.addDocuments(generateBook(String.format(Locale.ROOT, "%03d", id), new String[] { book.v1() }, book.v2()));
                id++;
            }
        }
        for (Tuple<String, int[]> book : books) {
            Arrays.sort(book.v2());
        }
        books.sort((o1, o2) -> {
            int cmp = Integer.compare(o1.v2()[0], o2.v2()[0]);
            if (cmp == 0) {
                return o1.v1().compareTo(o2.v1());
            } else {
                return cmp;
            }
        });
        try (IndexReader indexReader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) {
            MappedFieldType fieldType1 = new NumberFieldMapper.NumberFieldType("num_pages", NumberFieldMapper.NumberType.LONG);
            MappedFieldType fieldType2 = new KeywordFieldMapper.KeywordFieldType("author");
            TermsAggregationBuilder termsBuilder = new TermsAggregationBuilder("authors").userValueTypeHint(ValueType.STRING).size(books.size()).field("author").order(BucketOrder.compound(BucketOrder.aggregation("chapters>num_pages.value", true), BucketOrder.key(true)));
            NestedAggregationBuilder nestedBuilder = new NestedAggregationBuilder("chapters", "nested_chapters");
            MinAggregationBuilder minAgg = new MinAggregationBuilder("num_pages").field("num_pages");
            nestedBuilder.subAggregation(minAgg);
            termsBuilder.subAggregation(nestedBuilder);
            Terms terms = searchAndReduce(newSearcher(indexReader, false, true), new MatchAllDocsQuery(), termsBuilder, fieldType1, fieldType2);
            assertEquals(books.size(), terms.getBuckets().size());
            assertEquals("authors", terms.getName());
            for (int i = 0; i < books.size(); i++) {
                Tuple<String, int[]> book = books.get(i);
                Terms.Bucket bucket = terms.getBuckets().get(i);
                assertEquals(book.v1(), bucket.getKeyAsString());
                Min numPages = ((Nested) bucket.getAggregations().get("chapters")).getAggregations().get("num_pages");
                assertEquals(book.v2()[0], (int) numPages.getValue());
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) LongTerms(org.opensearch.search.aggregations.bucket.terms.LongTerms) Terms(org.opensearch.search.aggregations.bucket.terms.Terms) InternalTerms(org.opensearch.search.aggregations.bucket.terms.InternalTerms) StringTerms(org.opensearch.search.aggregations.bucket.terms.StringTerms) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) Min(org.opensearch.search.aggregations.metrics.Min) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) MinAggregationBuilder(org.opensearch.search.aggregations.metrics.MinAggregationBuilder) Tuple(org.opensearch.common.collect.Tuple) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 25 with Tuple

use of org.opensearch.common.collect.Tuple in project OpenSearch by opensearch-project.

the class InternalBinaryRangeTests method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    List<Tuple<BytesRef, BytesRef>> listOfRanges = new ArrayList<>();
    if (randomBoolean()) {
        listOfRanges.add(Tuple.tuple(null, new BytesRef(randomAlphaOfLength(15))));
    }
    if (randomBoolean()) {
        listOfRanges.add(Tuple.tuple(new BytesRef(randomAlphaOfLength(15)), null));
    }
    if (randomBoolean()) {
        listOfRanges.add(Tuple.tuple(null, null));
    }
    final int numRanges = Math.max(0, randomNumberOfBuckets() - listOfRanges.size());
    for (int i = 0; i < numRanges; i++) {
        BytesRef[] values = new BytesRef[2];
        values[0] = new BytesRef(randomAlphaOfLength(15));
        values[1] = new BytesRef(randomAlphaOfLength(15));
        Arrays.sort(values);
        listOfRanges.add(Tuple.tuple(values[0], values[1]));
    }
    Collections.shuffle(listOfRanges, random());
    ranges = Collections.unmodifiableList(listOfRanges);
}
Also used : ArrayList(java.util.ArrayList) Tuple(org.opensearch.common.collect.Tuple) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

Tuple (org.opensearch.common.collect.Tuple)151 ArrayList (java.util.ArrayList)65 List (java.util.List)49 IOException (java.io.IOException)45 Collections (java.util.Collections)44 HashMap (java.util.HashMap)40 Map (java.util.Map)40 Settings (org.opensearch.common.settings.Settings)38 ClusterState (org.opensearch.cluster.ClusterState)34 HashSet (java.util.HashSet)28 ShardId (org.opensearch.index.shard.ShardId)28 Arrays (java.util.Arrays)27 Collectors (java.util.stream.Collectors)26 Set (java.util.Set)25 Index (org.opensearch.index.Index)25 BytesReference (org.opensearch.common.bytes.BytesReference)24 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)24 CountDownLatch (java.util.concurrent.CountDownLatch)22 Version (org.opensearch.Version)21 Strings (org.opensearch.common.Strings)21