Search in sources :

Example 6 with ActiveShardCount

use of org.elasticsearch.action.support.ActiveShardCount in project elasticsearch by elastic.

the class TransportShrinkActionTests method testShrinkIndexSettings.

public void testShrinkIndexSettings() {
    String indexName = randomAsciiOfLength(10);
    // create one that won't fail
    ClusterState clusterState = ClusterState.builder(createClusterState(indexName, randomIntBetween(2, 10), 0, Settings.builder().put("index.blocks.write", true).build())).nodes(DiscoveryNodes.builder().add(newNode("node1"))).build();
    AllocationService service = new AllocationService(Settings.builder().build(), new AllocationDeciders(Settings.EMPTY, Collections.singleton(new MaxRetryAllocationDecider(Settings.EMPTY))), new TestGatewayAllocator(), new BalancedShardsAllocator(Settings.EMPTY), EmptyClusterInfoService.INSTANCE);
    RoutingTable routingTable = service.reroute(clusterState, "reroute").routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    // now we start the shard
    routingTable = service.applyStartedShards(clusterState, routingTable.index(indexName).shardsWithState(ShardRoutingState.INITIALIZING)).routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    int numSourceShards = clusterState.metaData().index(indexName).getNumberOfShards();
    DocsStats stats = new DocsStats(randomIntBetween(0, (IndexWriter.MAX_DOCS) / numSourceShards), randomIntBetween(1, 1000));
    ShrinkRequest target = new ShrinkRequest("target", indexName);
    final ActiveShardCount activeShardCount = randomBoolean() ? ActiveShardCount.ALL : ActiveShardCount.ONE;
    target.setWaitForActiveShards(activeShardCount);
    CreateIndexClusterStateUpdateRequest request = TransportShrinkAction.prepareCreateIndexRequest(target, clusterState, (i) -> stats, new IndexNameExpressionResolver(Settings.EMPTY));
    assertNotNull(request.shrinkFrom());
    assertEquals(indexName, request.shrinkFrom().getName());
    assertEquals("1", request.settings().get("index.number_of_shards"));
    assertEquals("shrink_index", request.cause());
    assertEquals(request.waitForActiveShards(), activeShardCount);
}
Also used : TestGatewayAllocator(org.elasticsearch.test.gateway.TestGatewayAllocator) ClusterState(org.elasticsearch.cluster.ClusterState) BalancedShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) MaxRetryAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider) ActiveShardCount(org.elasticsearch.action.support.ActiveShardCount) CreateIndexClusterStateUpdateRequest(org.elasticsearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) DocsStats(org.elasticsearch.index.shard.DocsStats) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService)

Example 7 with ActiveShardCount

use of org.elasticsearch.action.support.ActiveShardCount in project crate by crate.

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.elasticsearch.index.shard.ShardId) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) ActiveShardCount(org.elasticsearch.action.support.ActiveShardCount)

Example 8 with ActiveShardCount

use of org.elasticsearch.action.support.ActiveShardCount in project crate by crate.

the class TransportCreatePartitionsAction method executeCreateIndices.

/**
 * This code is more or less the same as the stuff in {@link MetadataCreateIndexService}
 * but optimized for bulk operation without separate mapping/alias/index settings.
 */
private ClusterState executeCreateIndices(ClusterState currentState, CreatePartitionsRequest request) throws Exception {
    List<String> indicesToCreate = new ArrayList<>(request.indices().size());
    List<String> removalReasons = new ArrayList<>(request.indices().size());
    List<Index> createdIndices = new ArrayList<>(request.indices().size());
    try {
        validateAndFilterExistingIndices(currentState, indicesToCreate, request);
        if (indicesToCreate.isEmpty()) {
            return currentState;
        }
        Map<String, Map<String, Object>> mappings = new HashMap<>();
        Map<String, AliasMetadata> templatesAliases = new HashMap<>();
        List<String> templateNames = new ArrayList<>();
        List<IndexTemplateMetadata> templates = findTemplates(request, currentState);
        applyTemplates(mappings, templatesAliases, templateNames, templates);
        Metadata.Builder newMetadataBuilder = Metadata.builder(currentState.metadata());
        for (String index : indicesToCreate) {
            Settings indexSettings = createIndexSettings(currentState, templates);
            shardLimitValidator.validateShardLimit(indexSettings, currentState);
            int routingNumShards = IndexMetadata.INDEX_NUMBER_OF_ROUTING_SHARDS_SETTING.get(indexSettings);
            String testIndex = indicesToCreate.get(0);
            IndexMetadata.Builder tmpImdBuilder = IndexMetadata.builder(testIndex).setRoutingNumShards(routingNumShards);
            // Set up everything, now locally create the index to see that things are ok, and apply
            final IndexMetadata tmpImd = tmpImdBuilder.settings(indexSettings).build();
            ActiveShardCount waitForActiveShards = tmpImd.getWaitForActiveShards();
            if (!waitForActiveShards.validate(tmpImd.getNumberOfReplicas())) {
                throw new IllegalArgumentException("invalid wait_for_active_shards[" + waitForActiveShards + "]: cannot be greater than number of shard copies [" + (tmpImd.getNumberOfReplicas() + 1) + "]");
            }
            // create the index here (on the master) to validate it can be created, as well as adding the mapping
            IndexService indexService = indicesService.createIndex(tmpImd, Collections.emptyList(), false);
            createdIndices.add(indexService.index());
            // now add the mappings
            MapperService mapperService = indexService.mapperService();
            if (!mappings.isEmpty()) {
                assert mappings.size() == 1 : "Must have at most 1 mapping type";
                var entry = mappings.entrySet().iterator().next();
                try {
                    mapperService.merge(entry.getKey(), entry.getValue(), MapperService.MergeReason.MAPPING_UPDATE);
                } catch (MapperParsingException mpe) {
                    removalReasons.add("failed on parsing mappings on index creation");
                    throw mpe;
                }
            }
            // now, update the mappings with the actual source
            HashMap<String, MappingMetadata> mappingsMetadata = new HashMap<>();
            DocumentMapper mapper = mapperService.documentMapper();
            if (mapper != null) {
                MappingMetadata mappingMd = new MappingMetadata(mapper);
                mappingsMetadata.put(mapper.type(), mappingMd);
            }
            final IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(index).setRoutingNumShards(routingNumShards).settings(indexSettings);
            for (MappingMetadata mappingMd : mappingsMetadata.values()) {
                indexMetadataBuilder.putMapping(mappingMd);
            }
            for (AliasMetadata aliasMetadata : templatesAliases.values()) {
                indexMetadataBuilder.putAlias(aliasMetadata);
            }
            indexMetadataBuilder.state(IndexMetadata.State.OPEN);
            final IndexMetadata indexMetadata;
            try {
                indexMetadata = indexMetadataBuilder.build();
            } catch (Exception e) {
                removalReasons.add("failed to build index metadata");
                throw e;
            }
            logger.info("[{}] creating index, cause [bulk], templates {}, shards [{}]/[{}], mappings {}", index, templateNames, indexMetadata.getNumberOfShards(), indexMetadata.getNumberOfReplicas(), mappings.keySet());
            indexService.getIndexEventListener().beforeIndexAddedToCluster(indexMetadata.getIndex(), indexMetadata.getSettings());
            newMetadataBuilder.put(indexMetadata, false);
            removalReasons.add("cleaning up after validating index on master");
        }
        Metadata newMetadata = newMetadataBuilder.build();
        ClusterState updatedState = ClusterState.builder(currentState).metadata(newMetadata).build();
        RoutingTable.Builder routingTableBuilder = RoutingTable.builder(updatedState.routingTable());
        for (String index : indicesToCreate) {
            routingTableBuilder.addAsNew(updatedState.metadata().index(index));
        }
        return allocationService.reroute(ClusterState.builder(updatedState).routingTable(routingTableBuilder.build()).build(), "bulk-index-creation");
    } finally {
        for (int i = 0; i < createdIndices.size(); i++) {
            // Index was already partially created - need to clean up
            String removalReason = removalReasons.size() > i ? removalReasons.get(i) : "failed to create index";
            indicesService.removeIndex(createdIndices.get(i), IndicesClusterStateService.AllocatedIndices.IndexRemovalReason.NO_LONGER_ASSIGNED, removalReason);
        }
    }
}
Also used : MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) HashMap(java.util.HashMap) IndexService(org.elasticsearch.index.IndexService) MetadataCreateIndexService(org.elasticsearch.cluster.metadata.MetadataCreateIndexService) ArrayList(java.util.ArrayList) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) AliasMetadata(org.elasticsearch.cluster.metadata.AliasMetadata) MappingMetadata(org.elasticsearch.cluster.metadata.MappingMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) Index(org.elasticsearch.index.Index) IndexMetadata(org.elasticsearch.cluster.metadata.IndexMetadata) Settings(org.elasticsearch.common.settings.Settings) ClusterState(org.elasticsearch.cluster.ClusterState) AliasMetadata(org.elasticsearch.cluster.metadata.AliasMetadata) IndexTemplateMetadata(org.elasticsearch.cluster.metadata.IndexTemplateMetadata) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) ActiveShardCount(org.elasticsearch.action.support.ActiveShardCount) ElasticsearchException(org.elasticsearch.ElasticsearchException) ResourceAlreadyExistsException(org.elasticsearch.ResourceAlreadyExistsException) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) IOException(java.io.IOException) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) Map(java.util.Map) HashMap(java.util.HashMap) MappingMetadata(org.elasticsearch.cluster.metadata.MappingMetadata) MapperService(org.elasticsearch.index.mapper.MapperService)

Aggregations

ActiveShardCount (org.elasticsearch.action.support.ActiveShardCount)8 ClusterState (org.elasticsearch.cluster.ClusterState)4 CreateIndexClusterStateUpdateRequest (org.elasticsearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest)3 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)3 Settings (org.elasticsearch.common.settings.Settings)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ElasticsearchException (org.elasticsearch.ElasticsearchException)2 ResourceAlreadyExistsException (org.elasticsearch.ResourceAlreadyExistsException)2 AllocationService (org.elasticsearch.cluster.routing.allocation.AllocationService)2 Index (org.elasticsearch.index.Index)2 IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)2 IndexService (org.elasticsearch.index.IndexService)2 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)2 MapperService (org.elasticsearch.index.mapper.MapperService)2 ObjectCursor (com.carrotsearch.hppc.cursors.ObjectCursor)1 ObjectObjectCursor (com.carrotsearch.hppc.cursors.ObjectObjectCursor)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1