Search in sources :

Example 1 with ActiveShardCount

use of org.opensearch.action.support.ActiveShardCount in project OpenSearch by opensearch-project.

the class TransportClusterHealthAction method prepareResponse.

static int prepareResponse(final ClusterHealthRequest request, final ClusterHealthResponse response, final ClusterState clusterState, final IndexNameExpressionResolver indexNameExpressionResolver) {
    int waitForCounter = 0;
    if (request.waitForStatus() != null && response.getStatus().value() <= request.waitForStatus().value()) {
        waitForCounter++;
    }
    if (request.waitForNoRelocatingShards() && response.getRelocatingShards() == 0) {
        waitForCounter++;
    }
    if (request.waitForNoInitializingShards() && response.getInitializingShards() == 0) {
        waitForCounter++;
    }
    if (request.waitForActiveShards().equals(ActiveShardCount.NONE) == false) {
        ActiveShardCount waitForActiveShards = request.waitForActiveShards();
        assert waitForActiveShards.equals(ActiveShardCount.DEFAULT) == false : "waitForActiveShards must not be DEFAULT on the request object, instead it should be NONE";
        if (waitForActiveShards.equals(ActiveShardCount.ALL)) {
            if (response.getUnassignedShards() == 0 && response.getInitializingShards() == 0) {
                // if we are waiting for all shards to be active, then the num of unassigned and num of initializing shards must be 0
                waitForCounter++;
            }
        } else if (waitForActiveShards.enoughShardsActive(response.getActiveShards())) {
            // there are enough active shards to meet the requirements of the request
            waitForCounter++;
        }
    }
    if (CollectionUtils.isEmpty(request.indices()) == false) {
        try {
            indexNameExpressionResolver.concreteIndexNames(clusterState, IndicesOptions.strictExpand(), request);
            waitForCounter++;
        } catch (IndexNotFoundException e) {
            // no indices, make sure its RED
            response.setStatus(ClusterHealthStatus.RED);
        // missing indices, wait a bit more...
        }
    }
    if (!request.waitForNodes().isEmpty()) {
        if (request.waitForNodes().startsWith(">=")) {
            int expected = Integer.parseInt(request.waitForNodes().substring(2));
            if (response.getNumberOfNodes() >= expected) {
                waitForCounter++;
            }
        } else if (request.waitForNodes().startsWith("ge(")) {
            int expected = Integer.parseInt(request.waitForNodes().substring(3, request.waitForNodes().length() - 1));
            if (response.getNumberOfNodes() >= expected) {
                waitForCounter++;
            }
        } else if (request.waitForNodes().startsWith("<=")) {
            int expected = Integer.parseInt(request.waitForNodes().substring(2));
            if (response.getNumberOfNodes() <= expected) {
                waitForCounter++;
            }
        } else if (request.waitForNodes().startsWith("le(")) {
            int expected = Integer.parseInt(request.waitForNodes().substring(3, request.waitForNodes().length() - 1));
            if (response.getNumberOfNodes() <= expected) {
                waitForCounter++;
            }
        } else if (request.waitForNodes().startsWith(">")) {
            int expected = Integer.parseInt(request.waitForNodes().substring(1));
            if (response.getNumberOfNodes() > expected) {
                waitForCounter++;
            }
        } else if (request.waitForNodes().startsWith("gt(")) {
            int expected = Integer.parseInt(request.waitForNodes().substring(3, request.waitForNodes().length() - 1));
            if (response.getNumberOfNodes() > expected) {
                waitForCounter++;
            }
        } else if (request.waitForNodes().startsWith("<")) {
            int expected = Integer.parseInt(request.waitForNodes().substring(1));
            if (response.getNumberOfNodes() < expected) {
                waitForCounter++;
            }
        } else if (request.waitForNodes().startsWith("lt(")) {
            int expected = Integer.parseInt(request.waitForNodes().substring(3, request.waitForNodes().length() - 1));
            if (response.getNumberOfNodes() < expected) {
                waitForCounter++;
            }
        } else {
            int expected = Integer.parseInt(request.waitForNodes());
            if (response.getNumberOfNodes() == expected) {
                waitForCounter++;
            }
        }
    }
    return waitForCounter;
}
Also used : IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ActiveShardCount(org.opensearch.action.support.ActiveShardCount)

Example 2 with ActiveShardCount

use of org.opensearch.action.support.ActiveShardCount in project OpenSearch by opensearch-project.

the class ReplicationOperation method checkActiveShardCount.

/**
 * Checks whether we can perform a write based on the required active shard count setting.
 * Returns **null* if OK to proceed, or a string describing the reason to stop
 */
protected String checkActiveShardCount() {
    final ShardId shardId = primary.routingEntry().shardId();
    final ActiveShardCount waitForActiveShards = request.waitForActiveShards();
    if (waitForActiveShards == ActiveShardCount.NONE) {
        // not waiting for any shards
        return null;
    }
    final IndexShardRoutingTable shardRoutingTable = primary.getReplicationGroup().getRoutingTable();
    if (waitForActiveShards.enoughShardsActive(shardRoutingTable)) {
        return null;
    } else {
        final String resolvedShards = waitForActiveShards == ActiveShardCount.ALL ? Integer.toString(shardRoutingTable.shards().size()) : waitForActiveShards.toString();
        logger.trace("[{}] not enough active copies to meet shard count of [{}] (have {}, needed {}), scheduling a retry. op [{}], " + "request [{}]", shardId, waitForActiveShards, shardRoutingTable.activeShards().size(), resolvedShards, opType, request);
        return "Not enough active copies to meet shard count of [" + waitForActiveShards + "] (have " + shardRoutingTable.activeShards().size() + ", needed " + resolvedShards + ").";
    }
}
Also used : ShardId(org.opensearch.index.shard.ShardId) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) ActiveShardCount(org.opensearch.action.support.ActiveShardCount)

Example 3 with ActiveShardCount

use of org.opensearch.action.support.ActiveShardCount in project OpenSearch by opensearch-project.

the class MetadataRolloverServiceTests method testCreateIndexRequest.

public void testCreateIndexRequest() {
    String alias = randomAlphaOfLength(10);
    String rolloverIndex = randomAlphaOfLength(10);
    final RolloverRequest rolloverRequest = new RolloverRequest(alias, randomAlphaOfLength(10));
    final ActiveShardCount activeShardCount = randomBoolean() ? ActiveShardCount.ALL : ActiveShardCount.ONE;
    rolloverRequest.getCreateIndexRequest().waitForActiveShards(activeShardCount);
    final Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).build();
    rolloverRequest.getCreateIndexRequest().settings(settings);
    final CreateIndexClusterStateUpdateRequest createIndexRequest = MetadataRolloverService.prepareCreateIndexRequest(rolloverIndex, rolloverIndex, rolloverRequest.getCreateIndexRequest());
    assertThat(createIndexRequest.settings(), equalTo(settings));
    assertThat(createIndexRequest.index(), equalTo(rolloverIndex));
    assertThat(createIndexRequest.cause(), equalTo("rollover_index"));
}
Also used : CreateIndexClusterStateUpdateRequest(org.opensearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) ActiveShardCount(org.opensearch.action.support.ActiveShardCount) IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) Settings(org.opensearch.common.settings.Settings)

Example 4 with ActiveShardCount

use of org.opensearch.action.support.ActiveShardCount in project OpenSearch by opensearch-project.

the class MetadataRolloverServiceTests method testCreateIndexRequestForDataStream.

public void testCreateIndexRequestForDataStream() {
    DataStream dataStream = DataStreamTests.randomInstance();
    final String newWriteIndexName = DataStream.getDefaultBackingIndexName(dataStream.getName(), dataStream.getGeneration() + 1);
    final RolloverRequest rolloverRequest = new RolloverRequest(dataStream.getName(), randomAlphaOfLength(10));
    final ActiveShardCount activeShardCount = randomBoolean() ? ActiveShardCount.ALL : ActiveShardCount.ONE;
    rolloverRequest.getCreateIndexRequest().waitForActiveShards(activeShardCount);
    final Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetadata.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).build();
    rolloverRequest.getCreateIndexRequest().settings(settings);
    final CreateIndexClusterStateUpdateRequest createIndexRequest = MetadataRolloverService.prepareDataStreamCreateIndexRequest(dataStream.getName(), newWriteIndexName, rolloverRequest.getCreateIndexRequest());
    for (String settingKey : settings.keySet()) {
        assertThat(settings.get(settingKey), equalTo(createIndexRequest.settings().get(settingKey)));
    }
    assertThat(createIndexRequest.settings().get("index.hidden"), equalTo("true"));
    assertThat(createIndexRequest.index(), equalTo(newWriteIndexName));
    assertThat(createIndexRequest.cause(), equalTo("rollover_data_stream"));
}
Also used : CreateIndexClusterStateUpdateRequest(org.opensearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest) DataStream(org.opensearch.cluster.metadata.DataStream) Matchers.containsString(org.hamcrest.Matchers.containsString) ActiveShardCount(org.opensearch.action.support.ActiveShardCount) IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) Settings(org.opensearch.common.settings.Settings)

Example 5 with ActiveShardCount

use of org.opensearch.action.support.ActiveShardCount in project OpenSearch by opensearch-project.

the class RequestConvertersTests method setRandomWaitForActiveShards.

static void setRandomWaitForActiveShards(Consumer<ActiveShardCount> setter, ActiveShardCount defaultActiveShardCount, Map<String, String> expectedParams) {
    if (randomBoolean()) {
        int waitForActiveShardsInt = randomIntBetween(-1, 5);
        String waitForActiveShardsString;
        if (waitForActiveShardsInt == -1) {
            waitForActiveShardsString = "all";
        } else {
            waitForActiveShardsString = String.valueOf(waitForActiveShardsInt);
        }
        ActiveShardCount activeShardCount = ActiveShardCount.parseString(waitForActiveShardsString);
        setter.accept(activeShardCount);
        if (defaultActiveShardCount.equals(activeShardCount) == false) {
            expectedParams.put("wait_for_active_shards", waitForActiveShardsString);
        }
    }
}
Also used : ActiveShardCount(org.opensearch.action.support.ActiveShardCount)

Aggregations

ActiveShardCount (org.opensearch.action.support.ActiveShardCount)7 Matchers.containsString (org.hamcrest.Matchers.containsString)3 CreateIndexClusterStateUpdateRequest (org.opensearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest)3 IndexScopedSettings (org.opensearch.common.settings.IndexScopedSettings)2 Settings (org.opensearch.common.settings.Settings)2 CloseIndexResponse (org.opensearch.action.admin.indices.close.CloseIndexResponse)1 ClusterState (org.opensearch.cluster.ClusterState)1 DataStream (org.opensearch.cluster.metadata.DataStream)1 IndexShardRoutingTable (org.opensearch.cluster.routing.IndexShardRoutingTable)1 RoutingTable (org.opensearch.cluster.routing.RoutingTable)1 AllocationService (org.opensearch.cluster.routing.allocation.AllocationService)1 BalancedShardsAllocator (org.opensearch.cluster.routing.allocation.allocator.BalancedShardsAllocator)1 AllocationDeciders (org.opensearch.cluster.routing.allocation.decider.AllocationDeciders)1 MaxRetryAllocationDecider (org.opensearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider)1 IndexNotFoundException (org.opensearch.index.IndexNotFoundException)1 DocsStats (org.opensearch.index.shard.DocsStats)1 ShardId (org.opensearch.index.shard.ShardId)1 TestGatewayAllocator (org.opensearch.test.gateway.TestGatewayAllocator)1