Search in sources :

Example 1 with CloseIndexResponse

use of org.opensearch.action.admin.indices.close.CloseIndexResponse in project OpenSearch by opensearch-project.

the class CloseIndexIT method testCloseIndexWaitForActiveShards.

public void testCloseIndexWaitForActiveShards() throws Exception {
    final String indexName = randomAlphaOfLength(10).toLowerCase(Locale.ROOT);
    createIndex(indexName, Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 2).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, // no replicas to avoid recoveries that could fail the index closing
    0).build());
    final int nbDocs = randomIntBetween(0, 50);
    indexRandom(randomBoolean(), false, randomBoolean(), IntStream.range(0, nbDocs).mapToObj(i -> client().prepareIndex(indexName).setId(String.valueOf(i)).setSource("num", i)).collect(toList()));
    ensureGreen(indexName);
    final CloseIndexResponse closeIndexResponse = client().admin().indices().prepareClose(indexName).setWaitForActiveShards(ActiveShardCount.DEFAULT).get();
    assertThat(client().admin().cluster().prepareHealth(indexName).get().getStatus(), is(ClusterHealthStatus.GREEN));
    assertTrue(closeIndexResponse.isAcknowledged());
    assertTrue(closeIndexResponse.isShardsAcknowledged());
    assertThat(closeIndexResponse.getIndices().get(0), notNullValue());
    assertThat(closeIndexResponse.getIndices().get(0).hasFailures(), is(false));
    assertThat(closeIndexResponse.getIndices().get(0).getIndex().getName(), equalTo(indexName));
    assertIndexIsClosed(indexName);
}
Also used : CloseIndexResponse(org.opensearch.action.admin.indices.close.CloseIndexResponse) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 2 with CloseIndexResponse

use of org.opensearch.action.admin.indices.close.CloseIndexResponse in project OpenSearch by opensearch-project.

the class ReplicaToPrimaryPromotionIT method testPromoteReplicaToPrimary.

public void testPromoteReplicaToPrimary() throws Exception {
    final String indexName = randomAlphaOfLength(5).toLowerCase(Locale.ROOT);
    createIndex(indexName);
    final int numOfDocs = scaledRandomIntBetween(0, 200);
    if (numOfDocs > 0) {
        try (BackgroundIndexer indexer = new BackgroundIndexer(indexName, "_doc", client(), numOfDocs)) {
            waitForDocs(numOfDocs, indexer);
        }
        refresh(indexName);
    }
    assertHitCount(client().prepareSearch(indexName).setSize(0).get(), numOfDocs);
    ensureGreen(indexName);
    // sometimes test with a closed index
    final IndexMetadata.State indexState = randomFrom(IndexMetadata.State.OPEN, IndexMetadata.State.CLOSE);
    if (indexState == IndexMetadata.State.CLOSE) {
        CloseIndexResponse closeIndexResponse = client().admin().indices().prepareClose(indexName).get();
        assertThat("close index not acked - " + closeIndexResponse, closeIndexResponse.isAcknowledged(), equalTo(true));
        ensureGreen(indexName);
    }
    // pick up a data node that contains a random primary shard
    ClusterState state = client(internalCluster().getMasterName()).admin().cluster().prepareState().get().getState();
    final int numShards = state.metadata().index(indexName).getNumberOfShards();
    final ShardRouting primaryShard = state.routingTable().index(indexName).shard(randomIntBetween(0, numShards - 1)).primaryShard();
    final DiscoveryNode randomNode = state.nodes().resolveNode(primaryShard.currentNodeId());
    // stop the random data node, all remaining shards are promoted to primaries
    internalCluster().stopRandomNode(InternalTestCluster.nameFilter(randomNode.getName()));
    ensureYellowAndNoInitializingShards(indexName);
    state = client(internalCluster().getMasterName()).admin().cluster().prepareState().get().getState();
    for (IndexShardRoutingTable shardRoutingTable : state.routingTable().index(indexName)) {
        for (ShardRouting shardRouting : shardRoutingTable.activeShards()) {
            assertThat(shardRouting + " should be promoted as a primary", shardRouting.primary(), is(true));
        }
    }
    if (indexState == IndexMetadata.State.CLOSE) {
        assertAcked(client().admin().indices().prepareOpen(indexName));
        ensureYellowAndNoInitializingShards(indexName);
    }
    assertHitCount(client().prepareSearch(indexName).setSize(0).get(), numOfDocs);
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) BackgroundIndexer(org.opensearch.test.BackgroundIndexer) CloseIndexResponse(org.opensearch.action.admin.indices.close.CloseIndexResponse) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ShardRouting(org.opensearch.cluster.routing.ShardRouting)

Example 3 with CloseIndexResponse

use of org.opensearch.action.admin.indices.close.CloseIndexResponse in project OpenSearch by opensearch-project.

the class ReopenWhileClosingIT method testReopenDuringCloseOnMultipleIndices.

public void testReopenDuringCloseOnMultipleIndices() throws Exception {
    List<String> dataOnlyNodes = internalCluster().startDataOnlyNodes(randomIntBetween(2, 3));
    final List<String> indices = new ArrayList<>();
    for (int i = 0; i < randomIntBetween(2, 10); i++) {
        indices.add("index-" + i);
        createIndexWithDocs(indices.get(i), dataOnlyNodes);
    }
    ensureYellowAndNoInitializingShards(indices.toArray(Strings.EMPTY_ARRAY));
    final CountDownLatch block = new CountDownLatch(1);
    final Releasable releaseBlock = interceptVerifyShardBeforeCloseActions(randomFrom(indices), block::countDown);
    ActionFuture<CloseIndexResponse> closeIndexResponse = client().admin().indices().prepareClose("index-*").execute();
    assertTrue("Waiting for index to have a closing blocked", block.await(60, TimeUnit.SECONDS));
    assertFalse(closeIndexResponse.isDone());
    indices.forEach(ReopenWhileClosingIT::assertIndexIsBlocked);
    final List<String> reopenedIndices = randomSubsetOf(randomIntBetween(1, indices.size()), indices);
    assertAcked(client().admin().indices().prepareOpen(reopenedIndices.toArray(Strings.EMPTY_ARRAY)));
    releaseBlock.close();
    assertFalse(closeIndexResponse.get().isAcknowledged());
    indices.forEach(index -> {
        if (reopenedIndices.contains(index)) {
            assertIndexIsOpened(index);
        } else {
            assertIndexIsClosed(index);
        }
    });
}
Also used : CloseIndexResponse(org.opensearch.action.admin.indices.close.CloseIndexResponse) ArrayList(java.util.ArrayList) Releasable(org.opensearch.common.lease.Releasable) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 4 with CloseIndexResponse

use of org.opensearch.action.admin.indices.close.CloseIndexResponse 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 5 with CloseIndexResponse

use of org.opensearch.action.admin.indices.close.CloseIndexResponse in project OpenSearch by opensearch-project.

the class ReopenWhileClosingIT method testReopenDuringClose.

public void testReopenDuringClose() throws Exception {
    List<String> dataOnlyNodes = internalCluster().startDataOnlyNodes(randomIntBetween(2, 3));
    final String indexName = "test";
    createIndexWithDocs(indexName, dataOnlyNodes);
    ensureYellowAndNoInitializingShards(indexName);
    final CountDownLatch block = new CountDownLatch(1);
    final Releasable releaseBlock = interceptVerifyShardBeforeCloseActions(indexName, block::countDown);
    ActionFuture<CloseIndexResponse> closeIndexResponse = client().admin().indices().prepareClose(indexName).execute();
    assertTrue("Waiting for index to have a closing blocked", block.await(60, TimeUnit.SECONDS));
    assertIndexIsBlocked(indexName);
    assertFalse(closeIndexResponse.isDone());
    assertAcked(client().admin().indices().prepareOpen(indexName));
    releaseBlock.close();
    assertFalse(closeIndexResponse.get().isAcknowledged());
    assertIndexIsOpened(indexName);
}
Also used : CloseIndexResponse(org.opensearch.action.admin.indices.close.CloseIndexResponse) Releasable(org.opensearch.common.lease.Releasable) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

CloseIndexResponse (org.opensearch.action.admin.indices.close.CloseIndexResponse)7 Matchers.containsString (org.hamcrest.Matchers.containsString)3 ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ClusterState (org.opensearch.cluster.ClusterState)2 IndexShardRoutingTable (org.opensearch.cluster.routing.IndexShardRoutingTable)2 IntObjectCursor (com.carrotsearch.hppc.cursors.IntObjectCursor)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Collections.singleton (java.util.Collections.singleton)1 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)1 EnumSet (java.util.EnumSet)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 SortedMap (java.util.SortedMap)1 Consumer (java.util.function.Consumer)1