Search in sources :

Example 96 with RoutingTable

use of org.elasticsearch.cluster.routing.RoutingTable in project elasticsearch by elastic.

the class TransportUpgradeAction method indicesWithMissingPrimaries.

/**
     * Finds all indices that have not all primaries available
     */
private Set<String> indicesWithMissingPrimaries(ClusterState clusterState, String[] concreteIndices) {
    Set<String> indices = new HashSet<>();
    RoutingTable routingTable = clusterState.routingTable();
    for (String index : concreteIndices) {
        IndexRoutingTable indexRoutingTable = routingTable.index(index);
        if (indexRoutingTable.allPrimaryShardsActive() == false) {
            indices.add(index);
        }
    }
    return indices;
}
Also used : IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) HashSet(java.util.HashSet)

Example 97 with RoutingTable

use of org.elasticsearch.cluster.routing.RoutingTable in project elasticsearch by elastic.

the class IndicesClusterStateService method updateShard.

private void updateShard(DiscoveryNodes nodes, ShardRouting shardRouting, Shard shard, RoutingTable routingTable, ClusterState clusterState) {
    final ShardRouting currentRoutingEntry = shard.routingEntry();
    assert currentRoutingEntry.isSameAllocation(shardRouting) : "local shard has a different allocation id but wasn't cleaning by removeShards. " + "cluster state: " + shardRouting + " local: " + currentRoutingEntry;
    try {
        shard.updateRoutingEntry(shardRouting);
        if (shardRouting.primary()) {
            IndexShardRoutingTable indexShardRoutingTable = routingTable.shardRoutingTable(shardRouting.shardId());
            Set<String> activeIds = indexShardRoutingTable.activeShards().stream().filter(sr -> nodes.get(sr.currentNodeId()).getVersion().onOrAfter(Version.V_6_0_0_alpha1_UNRELEASED)).map(r -> r.allocationId().getId()).collect(Collectors.toSet());
            Set<String> initializingIds = indexShardRoutingTable.getAllInitializingShards().stream().filter(sr -> nodes.get(sr.currentNodeId()).getVersion().onOrAfter(Version.V_6_0_0_alpha1_UNRELEASED)).map(r -> r.allocationId().getId()).collect(Collectors.toSet());
            shard.updateAllocationIdsFromMaster(activeIds, initializingIds);
        }
    } catch (Exception e) {
        failAndRemoveShard(shardRouting, true, "failed updating shard routing entry", e, clusterState);
        return;
    }
    final IndexShardState state = shard.state();
    if (shardRouting.initializing() && (state == IndexShardState.STARTED || state == IndexShardState.POST_RECOVERY)) {
        // we managed to tell the master we started), mark us as started
        if (logger.isTraceEnabled()) {
            logger.trace("{} master marked shard as initializing, but shard has state [{}], resending shard started to {}", shardRouting.shardId(), state, nodes.getMasterNode());
        }
        if (nodes.getMasterNode() != null) {
            shardStateAction.shardStarted(shardRouting, "master " + nodes.getMasterNode() + " marked shard as initializing, but shard state is [" + state + "], mark shard as started", SHARD_STATE_ACTION_LISTENER, clusterState);
        }
    }
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) Arrays(java.util.Arrays) CLOSED(org.elasticsearch.indices.cluster.IndicesClusterStateService.AllocatedIndices.IndexRemovalReason.CLOSED) Nullable(org.elasticsearch.common.Nullable) FAILURE(org.elasticsearch.indices.cluster.IndicesClusterStateService.AllocatedIndices.IndexRemovalReason.FAILURE) ConcurrentCollections(org.elasticsearch.common.util.concurrent.ConcurrentCollections) SearchService(org.elasticsearch.search.SearchService) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) Type(org.elasticsearch.cluster.routing.RecoverySource.Type) ClusterState(org.elasticsearch.cluster.ClusterState) Settings(org.elasticsearch.common.settings.Settings) Map(java.util.Map) SyncedFlushService(org.elasticsearch.indices.flush.SyncedFlushService) ThreadPool(org.elasticsearch.threadpool.ThreadPool) PeerRecoveryTargetService(org.elasticsearch.indices.recovery.PeerRecoveryTargetService) Set(java.util.Set) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) ShardLockObtainFailedException(org.elasticsearch.env.ShardLockObtainFailedException) ClusterChangedEvent(org.elasticsearch.cluster.ClusterChangedEvent) Collectors(java.util.stream.Collectors) NodeMappingRefreshAction(org.elasticsearch.cluster.action.index.NodeMappingRefreshAction) AbstractRunnable(org.elasticsearch.common.util.concurrent.AbstractRunnable) SnapshotShardsService(org.elasticsearch.snapshots.SnapshotShardsService) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Version(org.elasticsearch.Version) IndexComponent(org.elasticsearch.index.IndexComponent) Supplier(org.apache.logging.log4j.util.Supplier) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) RecoveryState(org.elasticsearch.indices.recovery.RecoveryState) ShardStateAction(org.elasticsearch.cluster.action.shard.ShardStateAction) NO_LONGER_ASSIGNED(org.elasticsearch.indices.cluster.IndicesClusterStateService.AllocatedIndices.IndexRemovalReason.NO_LONGER_ASSIGNED) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) Callback(org.elasticsearch.common.util.Callback) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) ClusterService(org.elasticsearch.cluster.service.ClusterService) IndexShardRelocatedException(org.elasticsearch.index.shard.IndexShardRelocatedException) PeerRecoverySourceService(org.elasticsearch.indices.recovery.PeerRecoverySourceService) RecoveryFailedException(org.elasticsearch.indices.recovery.RecoveryFailedException) HashMap(java.util.HashMap) Index(org.elasticsearch.index.Index) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) GlobalCheckpointSyncAction(org.elasticsearch.index.seqno.GlobalCheckpointSyncAction) ResourceAlreadyExistsException(org.elasticsearch.ResourceAlreadyExistsException) Inject(org.elasticsearch.common.inject.Inject) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) TimeValue(org.elasticsearch.common.unit.TimeValue) IndexSettings(org.elasticsearch.index.IndexSettings) IndicesService(org.elasticsearch.indices.IndicesService) ClusterStateApplier(org.elasticsearch.cluster.ClusterStateApplier) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) IndexShardState(org.elasticsearch.index.shard.IndexShardState) IndexEventListener(org.elasticsearch.index.shard.IndexEventListener) Iterator(java.util.Iterator) DELETED(org.elasticsearch.indices.cluster.IndicesClusterStateService.AllocatedIndices.IndexRemovalReason.DELETED) IndexService(org.elasticsearch.index.IndexService) GlobalCheckpointTracker(org.elasticsearch.index.seqno.GlobalCheckpointTracker) IndexShard(org.elasticsearch.index.shard.IndexShard) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) IOException(java.io.IOException) RepositoriesService(org.elasticsearch.repositories.RepositoriesService) AbstractLifecycleComponent(org.elasticsearch.common.component.AbstractLifecycleComponent) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) GatewayService(org.elasticsearch.gateway.GatewayService) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) IndexShardState(org.elasticsearch.index.shard.IndexShardState) ShardNotFoundException(org.elasticsearch.index.shard.ShardNotFoundException) ShardLockObtainFailedException(org.elasticsearch.env.ShardLockObtainFailedException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) IndexShardRelocatedException(org.elasticsearch.index.shard.IndexShardRelocatedException) RecoveryFailedException(org.elasticsearch.indices.recovery.RecoveryFailedException) ResourceAlreadyExistsException(org.elasticsearch.ResourceAlreadyExistsException) IOException(java.io.IOException)

Example 98 with RoutingTable

use of org.elasticsearch.cluster.routing.RoutingTable in project elasticsearch by elastic.

the class IndicesStore method clusterChanged.

@Override
public void clusterChanged(ClusterChangedEvent event) {
    if (!event.routingTableChanged()) {
        return;
    }
    if (event.state().blocks().disableStatePersistence()) {
        return;
    }
    RoutingTable routingTable = event.state().routingTable();
    // - closed indices don't need to be removed from the cache but we do it anyway for code simplicity
    for (Iterator<ShardId> it = folderNotFoundCache.iterator(); it.hasNext(); ) {
        ShardId shardId = it.next();
        if (routingTable.hasIndex(shardId.getIndex()) == false) {
            it.remove();
        }
    }
    // remove entries from cache which are allocated to this node
    final String localNodeId = event.state().nodes().getLocalNodeId();
    RoutingNode localRoutingNode = event.state().getRoutingNodes().node(localNodeId);
    if (localRoutingNode != null) {
        for (ShardRouting routing : localRoutingNode) {
            folderNotFoundCache.remove(routing.shardId());
        }
    }
    for (IndexRoutingTable indexRoutingTable : routingTable) {
        // Note, closed indices will not have any routing information, so won't be deleted
        for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) {
            ShardId shardId = indexShardRoutingTable.shardId();
            if (folderNotFoundCache.contains(shardId) == false && shardCanBeDeleted(localNodeId, indexShardRoutingTable)) {
                IndexService indexService = indicesService.indexService(indexRoutingTable.getIndex());
                final IndexSettings indexSettings;
                if (indexService == null) {
                    IndexMetaData indexMetaData = event.state().getMetaData().getIndexSafe(indexRoutingTable.getIndex());
                    indexSettings = new IndexSettings(indexMetaData, settings);
                } else {
                    indexSettings = indexService.getIndexSettings();
                }
                IndicesService.ShardDeletionCheckResult shardDeletionCheckResult = indicesService.canDeleteShardContent(shardId, indexSettings);
                switch(shardDeletionCheckResult) {
                    case FOLDER_FOUND_CAN_DELETE:
                        deleteShardIfExistElseWhere(event.state(), indexShardRoutingTable);
                        break;
                    case NO_FOLDER_FOUND:
                        folderNotFoundCache.add(shardId);
                        break;
                    case NO_LOCAL_STORAGE:
                        assert false : "shard deletion only runs on data nodes which always have local storage";
                        // nothing to do
                        break;
                    case STILL_ALLOCATED:
                        // nothing to do
                        break;
                    case SHARED_FILE_SYSTEM:
                        // nothing to do
                        break;
                    default:
                        assert false : "unknown shard deletion check result: " + shardDeletionCheckResult;
                }
            }
        }
    }
}
Also used : IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexService(org.elasticsearch.index.IndexService) IndexSettings(org.elasticsearch.index.IndexSettings) IndicesService(org.elasticsearch.indices.IndicesService) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ShardId(org.elasticsearch.index.shard.ShardId) IndexShardRoutingTable(org.elasticsearch.cluster.routing.IndexShardRoutingTable) IndexRoutingTable(org.elasticsearch.cluster.routing.IndexRoutingTable) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) RoutingNode(org.elasticsearch.cluster.routing.RoutingNode) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting)

Example 99 with RoutingTable

use of org.elasticsearch.cluster.routing.RoutingTable in project elasticsearch by elastic.

the class ClusterRerouteTests method createInitialClusterState.

private ClusterState createInitialClusterState(AllocationService service) {
    MetaData.Builder metaBuilder = MetaData.builder();
    metaBuilder.put(IndexMetaData.builder("idx").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(0));
    MetaData metaData = metaBuilder.build();
    RoutingTable.Builder routingTableBuilder = RoutingTable.builder();
    routingTableBuilder.addAsNew(metaData.index("idx"));
    RoutingTable routingTable = routingTableBuilder.build();
    ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(routingTable).build();
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2"))).build();
    RoutingTable prevRoutingTable = routingTable;
    routingTable = service.reroute(clusterState, "reroute").routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    assertEquals(prevRoutingTable.index("idx").shards().size(), 1);
    assertEquals(prevRoutingTable.index("idx").shard(0).shards().get(0).state(), UNASSIGNED);
    assertEquals(routingTable.index("idx").shards().size(), 1);
    assertEquals(routingTable.index("idx").shard(0).shards().get(0).state(), INITIALIZING);
    return clusterState;
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) MetaData(org.elasticsearch.cluster.metadata.MetaData) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 100 with RoutingTable

use of org.elasticsearch.cluster.routing.RoutingTable in project elasticsearch by elastic.

the class TransportShrinkActionTests method testErrorCondition.

public void testErrorCondition() {
    ClusterState state = createClusterState("source", randomIntBetween(2, 42), randomIntBetween(0, 10), Settings.builder().put("index.blocks.write", true).build());
    assertTrue(expectThrows(IllegalStateException.class, () -> TransportShrinkAction.prepareCreateIndexRequest(new ShrinkRequest("target", "source"), state, (i) -> new DocsStats(Integer.MAX_VALUE, randomIntBetween(1, 1000)), new IndexNameExpressionResolver(Settings.EMPTY))).getMessage().startsWith("Can't merge index with more than [2147483519] docs - too many documents in shards "));
    assertTrue(expectThrows(IllegalStateException.class, () -> {
        ShrinkRequest req = new ShrinkRequest("target", "source");
        req.getShrinkIndexRequest().settings(Settings.builder().put("index.number_of_shards", 4));
        ClusterState clusterState = createClusterState("source", 8, 1, Settings.builder().put("index.blocks.write", true).build());
        TransportShrinkAction.prepareCreateIndexRequest(req, clusterState, (i) -> i == 2 || i == 3 ? new DocsStats(Integer.MAX_VALUE / 2, randomIntBetween(1, 1000)) : null, new IndexNameExpressionResolver(Settings.EMPTY));
    }).getMessage().startsWith("Can't merge index with more than [2147483519] docs - too many documents in shards "));
    // create one that won't fail
    ClusterState clusterState = ClusterState.builder(createClusterState("source", 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("source").shardsWithState(ShardRoutingState.INITIALIZING)).routingTable();
    clusterState = ClusterState.builder(clusterState).routingTable(routingTable).build();
    TransportShrinkAction.prepareCreateIndexRequest(new ShrinkRequest("target", "source"), clusterState, (i) -> new DocsStats(randomIntBetween(1, 1000), randomIntBetween(1, 1000)), new IndexNameExpressionResolver(Settings.EMPTY));
}
Also used : MetaData(org.elasticsearch.cluster.metadata.MetaData) Arrays(java.util.Arrays) MaxRetryAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService) ClusterBlocks(org.elasticsearch.cluster.block.ClusterBlocks) ShardRoutingState(org.elasticsearch.cluster.routing.ShardRoutingState) HashSet(java.util.HashSet) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Settings(org.elasticsearch.common.settings.Settings) EmptyClusterInfoService(org.elasticsearch.cluster.EmptyClusterInfoService) AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) ClusterName(org.elasticsearch.cluster.ClusterName) ESTestCase(org.elasticsearch.test.ESTestCase) CreateIndexClusterStateUpdateRequest(org.elasticsearch.action.admin.indices.create.CreateIndexClusterStateUpdateRequest) Collections.emptyMap(java.util.Collections.emptyMap) DiscoveryNodes(org.elasticsearch.cluster.node.DiscoveryNodes) BalancedShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) TestGatewayAllocator(org.elasticsearch.test.gateway.TestGatewayAllocator) ActiveShardCount(org.elasticsearch.action.support.ActiveShardCount) IndexWriter(org.apache.lucene.index.IndexWriter) Version(org.elasticsearch.Version) DocsStats(org.elasticsearch.index.shard.DocsStats) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) Collections(java.util.Collections) TestGatewayAllocator(org.elasticsearch.test.gateway.TestGatewayAllocator) ClusterState(org.elasticsearch.cluster.ClusterState) RoutingTable(org.elasticsearch.cluster.routing.RoutingTable) BalancedShardsAllocator(org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator) DocsStats(org.elasticsearch.index.shard.DocsStats) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) AllocationDeciders(org.elasticsearch.cluster.routing.allocation.decider.AllocationDeciders) MaxRetryAllocationDecider(org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider) AllocationService(org.elasticsearch.cluster.routing.allocation.AllocationService)

Aggregations

RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)165 ClusterState (org.elasticsearch.cluster.ClusterState)144 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)140 MetaData (org.elasticsearch.cluster.metadata.MetaData)135 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)57 RoutingNodes (org.elasticsearch.cluster.routing.RoutingNodes)53 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)43 AllocationService (org.elasticsearch.cluster.routing.allocation.AllocationService)33 IndexRoutingTable (org.elasticsearch.cluster.routing.IndexRoutingTable)31 DiscoveryNodes (org.elasticsearch.cluster.node.DiscoveryNodes)23 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)21 ShardId (org.elasticsearch.index.shard.ShardId)21 Settings (org.elasticsearch.common.settings.Settings)20 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)18 TestGatewayAllocator (org.elasticsearch.test.gateway.TestGatewayAllocator)18 HashSet (java.util.HashSet)17 RoutingNode (org.elasticsearch.cluster.routing.RoutingNode)16 BalancedShardsAllocator (org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator)16 Matchers.containsString (org.hamcrest.Matchers.containsString)15 ClusterInfo (org.elasticsearch.cluster.ClusterInfo)14