当前位置: 首页>>代码示例>>Java>>正文


Java ShardRouting.initializing方法代码示例

本文整理汇总了Java中org.elasticsearch.cluster.routing.ShardRouting.initializing方法的典型用法代码示例。如果您正苦于以下问题:Java ShardRouting.initializing方法的具体用法?Java ShardRouting.initializing怎么用?Java ShardRouting.initializing使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.elasticsearch.cluster.routing.ShardRouting的用法示例。


在下文中一共展示了ShardRouting.initializing方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: failMissingShards

import org.elasticsearch.cluster.routing.ShardRouting; //导入方法依赖的package包/类
/**
 * Notifies master about shards that don't exist but are supposed to be active on this node.
 *
 * @param state new cluster state
 */
private void failMissingShards(final ClusterState state) {
    RoutingNode localRoutingNode = state.getRoutingNodes().node(state.nodes().getLocalNodeId());
    if (localRoutingNode == null) {
        return;
    }
    for (final ShardRouting shardRouting : localRoutingNode) {
        ShardId shardId = shardRouting.shardId();
        if (shardRouting.initializing() == false &&
            failedShardsCache.containsKey(shardId) == false &&
            indicesService.getShardOrNull(shardId) == null) {
            // the master thinks we are active, but we don't have this shard at all, mark it as failed
            sendFailShard(shardRouting, "master marked shard as active, but shard has not been created, mark shard as failed", null,
                state);
        }
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:22,代码来源:IndicesClusterStateService.java

示例2: sizeOfRelocatingShards

import org.elasticsearch.cluster.routing.ShardRouting; //导入方法依赖的package包/类
/**
 * Returns the size of all shards that are currently being relocated to
 * the node, but may not be finished transferring yet.
 *
 * If subtractShardsMovingAway is true then the size of shards moving away is subtracted from the total size of all shards
 */
static long sizeOfRelocatingShards(RoutingNode node, RoutingAllocation allocation,
                                   boolean subtractShardsMovingAway, String dataPath) {
    ClusterInfo clusterInfo = allocation.clusterInfo();
    long totalSize = 0;
    for (ShardRouting routing : node.shardsWithState(ShardRoutingState.RELOCATING, ShardRoutingState.INITIALIZING)) {
        String actualPath = clusterInfo.getDataPath(routing);
        if (dataPath.equals(actualPath)) {
            if (routing.initializing() && routing.relocatingNodeId() != null) {
                totalSize += getExpectedShardSize(routing, allocation, 0);
            } else if (subtractShardsMovingAway && routing.relocating()) {
                totalSize -= getExpectedShardSize(routing, allocation, 0);
            }
        }
    }
    return totalSize;
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:23,代码来源:DiskThresholdDecider.java

示例3: shardFailed

import org.elasticsearch.cluster.routing.ShardRouting; //导入方法依赖的package包/类
@Override
public void shardFailed(ShardRouting failedShard, UnassignedInfo unassignedInfo) {
    if (failedShard.primary() && failedShard.initializing()) {
        RecoverySource recoverySource = failedShard.recoverySource();
        if (recoverySource.getType() == RecoverySource.Type.SNAPSHOT) {
            Snapshot snapshot = ((SnapshotRecoverySource) recoverySource).snapshot();
            // mark restore entry for this shard as failed when it's due to a file corruption. There is no need wait on retries
            // to restore this shard on another node if the snapshot files are corrupt. In case where a node just left or crashed,
            // however, we only want to acknowledge the restore operation once it has been successfully restored on another node.
            if (unassignedInfo.getFailure() != null && Lucene.isCorruptionException(unassignedInfo.getFailure().getCause())) {
                changes(snapshot).failedShards.put(failedShard.shardId(), new ShardRestoreStatus(failedShard.currentNodeId(),
                    RestoreInProgress.State.FAILURE, unassignedInfo.getFailure().getCause().getMessage()));
            }
        }
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:17,代码来源:RestoreService.java

示例4: explainShard

import org.elasticsearch.cluster.routing.ShardRouting; //导入方法依赖的package包/类
public static ClusterAllocationExplanation explainShard(ShardRouting shardRouting, RoutingAllocation allocation,
                                                        ClusterInfo clusterInfo, boolean includeYesDecisions,
                                                        GatewayAllocator gatewayAllocator, ShardsAllocator shardAllocator) {
    allocation.setDebugMode(includeYesDecisions ? DebugMode.ON : DebugMode.EXCLUDE_YES_DECISIONS);

    ShardAllocationDecision shardDecision;
    if (shardRouting.initializing() || shardRouting.relocating()) {
        shardDecision = ShardAllocationDecision.NOT_TAKEN;
    } else {
        AllocateUnassignedDecision allocateDecision = shardRouting.unassigned() ?
            gatewayAllocator.decideUnassignedShardAllocation(shardRouting, allocation) : AllocateUnassignedDecision.NOT_TAKEN;
        if (allocateDecision.isDecisionTaken() == false) {
            shardDecision = shardAllocator.decideShardAllocation(shardRouting, allocation);
        } else {
            shardDecision = new ShardAllocationDecision(allocateDecision, MoveDecision.NOT_TAKEN);
        }
    }

    return new ClusterAllocationExplanation(shardRouting,
        shardRouting.currentNodeId() != null ? allocation.nodes().get(shardRouting.currentNodeId()) : null,
        shardRouting.relocatingNodeId() != null ? allocation.nodes().get(shardRouting.relocatingNodeId()) : null,
        clusterInfo, shardDecision);
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:24,代码来源:TransportClusterAllocationExplainAction.java

示例5: recover

import org.elasticsearch.cluster.routing.ShardRouting; //导入方法依赖的package包/类
private RecoveryResponse recover(final StartRecoveryRequest request) throws IOException {
    final IndexService indexService = indicesService.indexServiceSafe(request.shardId().getIndex());
    final IndexShard shard = indexService.getShard(request.shardId().id());

    // starting recovery from that our (the source) shard state is marking the shard to be in recovery mode as well, otherwise
    // the index operations will not be routed to it properly
    RoutingNode node = clusterService.state().getRoutingNodes().node(request.targetNode().getId());
    if (node == null) {
        logger.debug("delaying recovery of {} as source node {} is unknown", request.shardId(), request.targetNode());
        throw new DelayRecoveryException("source node does not have the node [" + request.targetNode() + "] in its state yet..");
    }

    ShardRouting routingEntry = shard.routingEntry();
    if (request.isPrimaryRelocation() && (routingEntry.relocating() == false || routingEntry.relocatingNodeId().equals(request.targetNode().getId()) == false)) {
        logger.debug("delaying recovery of {} as source shard is not marked yet as relocating to {}", request.shardId(), request.targetNode());
        throw new DelayRecoveryException("source shard is not marked yet as relocating to [" + request.targetNode() + "]");
    }

    ShardRouting targetShardRouting = node.getByShardId(request.shardId());
    if (targetShardRouting == null) {
        logger.debug("delaying recovery of {} as it is not listed as assigned to target node {}", request.shardId(), request.targetNode());
        throw new DelayRecoveryException("source node does not have the shard listed in its state as allocated on the node");
    }
    if (!targetShardRouting.initializing()) {
        logger.debug("delaying recovery of {} as it is not listed as initializing on the target node {}. known shards state is [{}]",
            request.shardId(), request.targetNode(), targetShardRouting.state());
        throw new DelayRecoveryException("source node has the state of the target shard to be [" + targetShardRouting.state() + "], expecting to be [initializing]");
    }

    RecoverySourceHandler handler = ongoingRecoveries.addNewRecovery(request, targetShardRouting.allocationId().getId(), shard);
    logger.trace("[{}][{}] starting recovery to {}", request.shardId().getIndex().getName(), request.shardId().id(), request.targetNode());
    try {
        return handler.recoverToTarget();
    } finally {
        ongoingRecoveries.remove(shard, handler);
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:38,代码来源:PeerRecoverySourceService.java

示例6: execute

import org.elasticsearch.cluster.routing.ShardRouting; //导入方法依赖的package包/类
@Override
public RerouteExplanation execute(RoutingAllocation allocation, boolean explain) {
    DiscoveryNode discoNode = allocation.nodes().resolveNode(node);
    ShardRouting shardRouting = null;
    RoutingNodes routingNodes = allocation.routingNodes();
    RoutingNode routingNode = routingNodes.node(discoNode.getId());
    IndexMetaData indexMetaData = null;
    if (routingNode != null) {
        indexMetaData = allocation.metaData().index(index());
        if (indexMetaData == null) {
            throw new IndexNotFoundException(index());
        }
        ShardId shardId = new ShardId(indexMetaData.getIndex(), shardId());
        shardRouting = routingNode.getByShardId(shardId);
    }
    if (shardRouting == null) {
        if (explain) {
            return new RerouteExplanation(this, allocation.decision(Decision.NO, "cancel_allocation_command",
                "can't cancel " + shardId + ", failed to find it on node " + discoNode));
        }
        throw new IllegalArgumentException("[cancel_allocation] can't cancel " + shardId + ", failed to find it on node " + discoNode);
    }
    if (shardRouting.primary() && allowPrimary == false) {
        if ((shardRouting.initializing() && shardRouting.relocatingNodeId() != null) == false) {
            // only allow cancelling initializing shard of primary relocation without allowPrimary flag
            if (explain) {
                return new RerouteExplanation(this, allocation.decision(Decision.NO, "cancel_allocation_command",
                    "can't cancel " + shardId + " on node " + discoNode + ", shard is primary and " +
                        shardRouting.state().name().toLowerCase(Locale.ROOT)));
            }
            throw new IllegalArgumentException("[cancel_allocation] can't cancel " + shardId + " on node " +
                discoNode + ", shard is primary and " + shardRouting.state().name().toLowerCase(Locale.ROOT));
        }
    }
    routingNodes.failShard(Loggers.getLogger(CancelAllocationCommand.class), shardRouting,
        new UnassignedInfo(UnassignedInfo.Reason.REROUTE_CANCELLED, null), indexMetaData, allocation.changes());
    return new RerouteExplanation(this, allocation.decision(Decision.YES, "cancel_allocation_command",
            "shard " + shardId + " on node " + discoNode + " can be cancelled"));
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:40,代码来源:CancelAllocationCommand.java

示例7: ClusterShardHealth

import org.elasticsearch.cluster.routing.ShardRouting; //导入方法依赖的package包/类
public ClusterShardHealth(final int shardId, final IndexShardRoutingTable shardRoutingTable) {
    this.shardId = shardId;
    int computeActiveShards = 0;
    int computeRelocatingShards = 0;
    int computeInitializingShards = 0;
    int computeUnassignedShards = 0;
    for (ShardRouting shardRouting : shardRoutingTable) {
        if (shardRouting.active()) {
            computeActiveShards++;
            if (shardRouting.relocating()) {
                // the shard is relocating, the one it is relocating to will be in initializing state, so we don't count it
                computeRelocatingShards++;
            }
        } else if (shardRouting.initializing()) {
            computeInitializingShards++;
        } else if (shardRouting.unassigned()) {
            computeUnassignedShards++;
        }
    }
    ClusterHealthStatus computeStatus;
    final ShardRouting primaryRouting = shardRoutingTable.primaryShard();
    if (primaryRouting.active()) {
        if (computeActiveShards == shardRoutingTable.size()) {
            computeStatus = ClusterHealthStatus.GREEN;
        } else {
            computeStatus = ClusterHealthStatus.YELLOW;
        }
    } else {
        computeStatus = getInactivePrimaryHealth(primaryRouting);
    }
    this.status = computeStatus;
    this.activeShards = computeActiveShards;
    this.relocatingShards = computeRelocatingShards;
    this.initializingShards = computeInitializingShards;
    this.unassignedShards = computeUnassignedShards;
    this.primaryActive = primaryRouting.active();
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:38,代码来源:ClusterShardHealth.java

示例8: sizeOfRelocatingShards

import org.elasticsearch.cluster.routing.ShardRouting; //导入方法依赖的package包/类
/**
 * Returns the size of all shards that are currently being relocated to
 * the node, but may not be finished transfering yet.
 *
 * If subtractShardsMovingAway is set then the size of shards moving away is subtracted from the total size
 * of all shards
 */
public static long sizeOfRelocatingShards(RoutingNode node, ClusterInfo clusterInfo, boolean subtractShardsMovingAway, String dataPath) {
    long totalSize = 0;
    for (ShardRouting routing : node.shardsWithState(ShardRoutingState.RELOCATING, ShardRoutingState.INITIALIZING)) {
        String actualPath = clusterInfo.getDataPath(routing);
        if (dataPath.equals(actualPath)) {
            if (routing.initializing() && routing.relocatingNodeId() != null) {
                totalSize += getShardSize(routing, clusterInfo);
            } else if (subtractShardsMovingAway && routing.relocating()) {
                totalSize -= getShardSize(routing, clusterInfo);
            }
        }
    }
    return totalSize;
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:22,代码来源:DiskThresholdDecider.java

示例9: canAllocate

import org.elasticsearch.cluster.routing.ShardRouting; //导入方法依赖的package包/类
@Override
public Decision canAllocate(RoutingNode node, RoutingAllocation allocation) {
    int currentRecoveries = 0;
    for (ShardRouting shard : node) {
        if (shard.initializing()) {
            currentRecoveries++;
        }
    }
    if (currentRecoveries >= concurrentRecoveries) {
        return allocation.decision(Decision.THROTTLE, NAME, "too many shards currently recovering [%d], limit: [%d]",
                currentRecoveries, concurrentRecoveries);
    } else {
        return allocation.decision(Decision.YES, NAME, "below shard recovery limit of [%d]", concurrentRecoveries);
    }
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:16,代码来源:ThrottlingAllocationDecider.java

示例10: ClusterShardHealth

import org.elasticsearch.cluster.routing.ShardRouting; //导入方法依赖的package包/类
public ClusterShardHealth(int shardId, final IndexShardRoutingTable shardRoutingTable) {
    this.shardId = shardId;
    for (ShardRouting shardRouting : shardRoutingTable) {
        if (shardRouting.active()) {
            activeShards++;
            if (shardRouting.relocating()) {
                // the shard is relocating, the one it is relocating to will be in initializing state, so we don't count it
                relocatingShards++;
            }
            if (shardRouting.primary()) {
                primaryActive = true;
            }
        } else if (shardRouting.initializing()) {
            initializingShards++;
        } else if (shardRouting.unassigned()) {
            unassignedShards++;
        }
    }
    if (primaryActive) {
        if (activeShards == shardRoutingTable.size()) {
            status = ClusterHealthStatus.GREEN;
        } else {
            status = ClusterHealthStatus.YELLOW;
        }
    } else {
        status = ClusterHealthStatus.RED;
    }
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:29,代码来源:ClusterShardHealth.java

示例11: testUnassignedShardsWithUnbalancedZones

import org.elasticsearch.cluster.routing.ShardRouting; //导入方法依赖的package包/类
public void testUnassignedShardsWithUnbalancedZones() {
    AllocationService strategy = createAllocationService(Settings.builder()
            .put("cluster.routing.allocation.node_concurrent_recoveries", 10)
            .put(ClusterRebalanceAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ALLOW_REBALANCE_SETTING.getKey(), "always")
            .put("cluster.routing.allocation.awareness.attributes", "zone")
            .build());

    logger.info("Building initial routing table for 'testUnassignedShardsWithUnbalancedZones'");

    MetaData metaData = MetaData.builder()
            .put(IndexMetaData.builder("test").settings(settings(Version.CURRENT)).numberOfShards(1).numberOfReplicas(4))
            .build();

    RoutingTable initialRoutingTable = RoutingTable.builder()
            .addAsNew(metaData.index("test"))
            .build();

    ClusterState clusterState = ClusterState.builder(org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).metaData(metaData).routingTable(initialRoutingTable).build();

    logger.info("--> adding 5 nodes in different zones and do rerouting");
    clusterState = ClusterState.builder(clusterState).nodes(DiscoveryNodes.builder()
                    .add(newNode("A-0", singletonMap("zone", "a")))
                    .add(newNode("A-1", singletonMap("zone", "a")))
                    .add(newNode("A-2", singletonMap("zone", "a")))
                    .add(newNode("A-3", singletonMap("zone", "a")))
                    .add(newNode("A-4", singletonMap("zone", "a")))
                    .add(newNode("B-0", singletonMap("zone", "b")))
    ).build();
    clusterState = strategy.reroute(clusterState, "reroute");
    assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(0));
    assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(1));

    logger.info("--> start the shard (primary)");
    clusterState = strategy.applyStartedShards(clusterState, clusterState.getRoutingNodes().shardsWithState(INITIALIZING));
    assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(3));
    assertThat(clusterState.getRoutingNodes().shardsWithState(UNASSIGNED).size(), equalTo(1)); // Unassigned shard is expected.

    // Cancel all initializing shards and move started primary to another node.
    AllocationCommands commands = new AllocationCommands();
    String primaryNode = null;
    for (ShardRouting routing : clusterState.routingTable().allShards()) {
        if (routing.primary()) {
            primaryNode = routing.currentNodeId();
        } else if (routing.initializing()) {
            commands.add(new CancelAllocationCommand(routing.shardId().getIndexName(), routing.id(), routing.currentNodeId(), false));
        }
    }
    commands.add(new MoveAllocationCommand("test", 0, primaryNode, "A-4"));

    clusterState = strategy.reroute(clusterState, commands, false, false).getClusterState();

    assertThat(clusterState.getRoutingNodes().shardsWithState(STARTED).size(), equalTo(0));
    assertThat(clusterState.getRoutingNodes().shardsWithState(RELOCATING).size(), equalTo(1));
    assertThat(clusterState.getRoutingNodes().shardsWithState(INITIALIZING).size(), equalTo(4)); // +1 for relocating shard.
    assertThat(clusterState.getRoutingNodes().shardsWithState(UNASSIGNED).size(), equalTo(1)); // Still 1 unassigned.
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:58,代码来源:AwarenessAllocationTests.java

示例12: recover

import org.elasticsearch.cluster.routing.ShardRouting; //导入方法依赖的package包/类
private RecoveryResponse recover(final StartRecoveryRequest request) {
    final IndexService indexService = indicesService.indexServiceSafe(request.shardId().index().name());
    final IndexShard shard = indexService.shardSafe(request.shardId().id());

    // starting recovery from that our (the source) shard state is marking the shard to be in recovery mode as well, otherwise
    // the index operations will not be routed to it properly
    RoutingNode node = clusterService.state().getRoutingNodes().node(request.targetNode().id());
    if (node == null) {
        logger.debug("delaying recovery of {} as source node {} is unknown", request.shardId(), request.targetNode());
        throw new DelayRecoveryException("source node does not have the node [" + request.targetNode() + "] in its state yet..");
    }
    ShardRouting targetShardRouting = null;
    for (ShardRouting shardRouting : node) {
        if (shardRouting.shardId().equals(request.shardId())) {
            targetShardRouting = shardRouting;
            break;
        }
    }
    if (targetShardRouting == null) {
        logger.debug("delaying recovery of {} as it is not listed as assigned to target node {}", request.shardId(), request.targetNode());
        throw new DelayRecoveryException("source node does not have the shard listed in its state as allocated on the node");
    }
    if (!targetShardRouting.initializing()) {
        logger.debug("delaying recovery of {} as it is not listed as initializing on the target node {}. known shards state is [{}]",
                request.shardId(), request.targetNode(), targetShardRouting.state());
        throw new DelayRecoveryException("source node has the state of the target shard to be [" + targetShardRouting.state() + "], expecting to be [initializing]");
    }

    logger.trace("[{}][{}] starting recovery to {}, mark_as_relocated {}", request.shardId().index().name(), request.shardId().id(), request.targetNode(), request.markAsRelocated());
    final RecoverySourceHandler handler;
    if (IndexMetaData.isOnSharedFilesystem(shard.indexSettings())) {
        handler = new SharedFSRecoverySourceHandler(shard, request, recoverySettings, transportService, logger);
    } else {
        // CRATE CHANGE:
        handler = new BlobRecoverySourceHandler(
                shard, request, recoverySettings, transportService, logger, blobTransferTarget, blobIndices);
    }
    ongoingRecoveries.add(shard, handler);
    try {
        return handler.recoverToTarget();
    } finally {
        ongoingRecoveries.remove(shard, handler);
    }
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:45,代码来源:BlobRecoverySource.java

示例13: recover

import org.elasticsearch.cluster.routing.ShardRouting; //导入方法依赖的package包/类
private RecoveryResponse recover(final StartRecoveryRequest request) {
    final IndexService indexService = indicesService.indexServiceSafe(request.shardId().index().name());
    final IndexShard shard = indexService.shardSafe(request.shardId().id());

    // starting recovery from that our (the source) shard state is marking the shard to be in recovery mode as well, otherwise
    // the index operations will not be routed to it properly
    RoutingNode node = clusterService.state().getRoutingNodes().node(request.targetNode().id());
    if (node == null) {
        logger.debug("delaying recovery of {} as source node {} is unknown", request.shardId(), request.targetNode());
        throw new DelayRecoveryException("source node does not have the node [" + request.targetNode() + "] in its state yet..");
    }
    ShardRouting targetShardRouting = null;
    for (ShardRouting shardRouting : node) {
        if (shardRouting.shardId().equals(request.shardId())) {
            targetShardRouting = shardRouting;
            break;
        }
    }
    if (targetShardRouting == null) {
        logger.debug("delaying recovery of {} as it is not listed as assigned to target node {}", request.shardId(), request.targetNode());
        throw new DelayRecoveryException("source node does not have the shard listed in its state as allocated on the node");
    }
    if (!targetShardRouting.initializing()) {
        logger.debug("delaying recovery of {} as it is not listed as initializing on the target node {}. known shards state is [{}]",
                request.shardId(), request.targetNode(), targetShardRouting.state());
        throw new DelayRecoveryException("source node has the state of the target shard to be [" + targetShardRouting.state() + "], expecting to be [initializing]");
    }

    logger.trace("[{}][{}] starting recovery to {}, mark_as_relocated {}", request.shardId().index().name(), request.shardId().id(), request.targetNode(), request.markAsRelocated());
    final RecoverySourceHandler handler;
    if (IndexMetaData.isOnSharedFilesystem(shard.indexSettings())) {
        handler = new SharedFSRecoverySourceHandler(shard, request, recoverySettings, transportService, logger);
    } else if (IndexMetaData.isIndexUsingDLEngine(shard.indexSettings())) {
        handler = new DLBasedIndexRecoverySourceHandler(shard, request, recoverySettings, transportService, logger);
    } else {
        handler = new RecoverySourceHandler(shard, request, recoverySettings, transportService, logger);
    }
    ongoingRecoveries.add(shard, handler);
    try {
        return handler.recoverToTarget();
    } finally {
        ongoingRecoveries.remove(shard, handler);
    }
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:45,代码来源:RecoverySource.java

示例14: underCapacity

import org.elasticsearch.cluster.routing.ShardRouting; //导入方法依赖的package包/类
private Decision underCapacity(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation, boolean moveToNode) {
    if (awarenessAttributes.length == 0) {
        return allocation.decision(Decision.YES, NAME, "no allocation awareness enabled");
    }

    IndexMetaData indexMetaData = allocation.metaData().index(shardRouting.index());
    int shardCount = indexMetaData.getNumberOfReplicas() + 1; // 1 for primary
    for (String awarenessAttribute : awarenessAttributes) {
        // the node the shard exists on must be associated with an awareness attribute
        if (!node.node().attributes().containsKey(awarenessAttribute)) {
            return allocation.decision(Decision.NO, NAME, "node does not contain awareness attribute: [%s]", awarenessAttribute);
        }

        // build attr_value -> nodes map
        ObjectIntHashMap<String> nodesPerAttribute = allocation.routingNodes().nodesPerAttributesCounts(awarenessAttribute);

        // build the count of shards per attribute value
        ObjectIntHashMap<String> shardPerAttribute = new ObjectIntHashMap<>();
        for (ShardRouting assignedShard : allocation.routingNodes().assignedShards(shardRouting)) {
            if (assignedShard.started() || assignedShard.initializing()) {
                // Note: this also counts relocation targets as that will be the new location of the shard.
                // Relocation sources should not be counted as the shard is moving away
                RoutingNode routingNode = allocation.routingNodes().node(assignedShard.currentNodeId());
                shardPerAttribute.addTo(routingNode.node().attributes().get(awarenessAttribute), 1);
            }
        }

        if (moveToNode) {
            if (shardRouting.assignedToNode()) {
                String nodeId = shardRouting.relocating() ? shardRouting.relocatingNodeId() : shardRouting.currentNodeId();
                if (!node.nodeId().equals(nodeId)) {
                    // we work on different nodes, move counts around
                    shardPerAttribute.putOrAdd(allocation.routingNodes().node(nodeId).node().attributes().get(awarenessAttribute), 0, -1);
                    shardPerAttribute.addTo(node.node().attributes().get(awarenessAttribute), 1);
                }
            } else {
                shardPerAttribute.addTo(node.node().attributes().get(awarenessAttribute), 1);
            }
        }

        int numberOfAttributes = nodesPerAttribute.size();
        String[] fullValues = forcedAwarenessAttributes.get(awarenessAttribute);
        if (fullValues != null) {
            for (String fullValue : fullValues) {
                if (!shardPerAttribute.containsKey(fullValue)) {
                    numberOfAttributes++;
                }
            }
        }
        // TODO should we remove ones that are not part of full list?

        int averagePerAttribute = shardCount / numberOfAttributes;
        int totalLeftover = shardCount % numberOfAttributes;
        int requiredCountPerAttribute;
        if (averagePerAttribute == 0) {
            // if we have more attributes values than shard count, no leftover
            totalLeftover = 0;
            requiredCountPerAttribute = 1;
        } else {
            requiredCountPerAttribute = averagePerAttribute;
        }
        int leftoverPerAttribute = totalLeftover == 0 ? 0 : 1;

        int currentNodeCount = shardPerAttribute.get(node.node().attributes().get(awarenessAttribute));
        // if we are above with leftover, then we know we are not good, even with mod
        if (currentNodeCount > (requiredCountPerAttribute + leftoverPerAttribute)) {
            return allocation.decision(Decision.NO, NAME,
                    "too many shards on node for attribute: [%s], required per attribute: [%d], node count: [%d], leftover: [%d]",
                    awarenessAttribute, requiredCountPerAttribute, currentNodeCount, leftoverPerAttribute);
        }
        // all is well, we are below or same as average
        if (currentNodeCount <= requiredCountPerAttribute) {
            continue;
        }
    }

    return allocation.decision(Decision.YES, NAME, "node meets awareness requirements");
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:79,代码来源:AwarenessAllocationDecider.java

示例15: execute

import org.elasticsearch.cluster.routing.ShardRouting; //导入方法依赖的package包/类
@Override
public RerouteExplanation execute(RoutingAllocation allocation, boolean explain) {
    DiscoveryNode discoNode = allocation.nodes().resolveNode(node);
    boolean found = false;
    for (RoutingNodes.RoutingNodeIterator it = allocation.routingNodes().routingNodeIter(discoNode.id()); it.hasNext(); ) {
        ShardRouting shardRouting = it.next();
        if (!shardRouting.shardId().equals(shardId)) {
            continue;
        }
        found = true;
        if (shardRouting.relocatingNodeId() != null) {
            if (shardRouting.initializing()) {
                // the shard is initializing and recovering from another node, simply cancel the recovery
                it.remove();
                // and cancel the relocating state from the shard its being relocated from
                RoutingNode relocatingFromNode = allocation.routingNodes().node(shardRouting.relocatingNodeId());
                if (relocatingFromNode != null) {
                    for (ShardRouting fromShardRouting : relocatingFromNode) {
                        if (fromShardRouting.isSameShard(shardRouting) && fromShardRouting.state() == RELOCATING) {
                            allocation.routingNodes().cancelRelocation(fromShardRouting);
                            break;
                        }
                    }
                }
            } else if (shardRouting.relocating()) {

                // the shard is relocating to another node, cancel the recovery on the other node, and deallocate this one
                if (!allowPrimary && shardRouting.primary()) {
                    // can't cancel a primary shard being initialized
                    if (explain) {
                        return new RerouteExplanation(this, allocation.decision(Decision.NO, "cancel_allocation_command",
                                "can't cancel " + shardId + " on node " + discoNode + ", shard is primary and initializing its state"));
                    }
                    throw new IllegalArgumentException("[cancel_allocation] can't cancel " + shardId + " on node " +
                            discoNode + ", shard is primary and initializing its state");
                }
                it.moveToUnassigned(new UnassignedInfo(UnassignedInfo.Reason.REROUTE_CANCELLED, null));
                // now, go and find the shard that is initializing on the target node, and cancel it as well...
                RoutingNodes.RoutingNodeIterator initializingNode = allocation.routingNodes().routingNodeIter(shardRouting.relocatingNodeId());
                if (initializingNode != null) {
                    while (initializingNode.hasNext()) {
                        ShardRouting initializingShardRouting = initializingNode.next();
                        if (initializingShardRouting.isRelocationTargetOf(shardRouting)) {
                            initializingNode.remove();
                        }
                    }
                }
            }
        } else {
            // the shard is not relocating, its either started, or initializing, just cancel it and move on...
            if (!allowPrimary && shardRouting.primary()) {
                // can't cancel a primary shard being initialized
                if (explain) {
                    return new RerouteExplanation(this, allocation.decision(Decision.NO, "cancel_allocation_command",
                            "can't cancel " + shardId + " on node " + discoNode + ", shard is primary and started"));
                }
                throw new IllegalArgumentException("[cancel_allocation] can't cancel " + shardId + " on node " +
                        discoNode + ", shard is primary and started");
            }
            it.moveToUnassigned(new UnassignedInfo(UnassignedInfo.Reason.REROUTE_CANCELLED, null));
        }
    }
    if (!found) {
        if (explain) {
            return new RerouteExplanation(this, allocation.decision(Decision.NO, "cancel_allocation_command",
                    "can't cancel " + shardId + ", failed to find it on node " + discoNode));
        }
        throw new IllegalArgumentException("[cancel_allocation] can't cancel " + shardId + ", failed to find it on node " + discoNode);
    }
    return new RerouteExplanation(this, allocation.decision(Decision.YES, "cancel_allocation_command",
            "shard " + shardId + " on node " + discoNode + " can be cancelled"));
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:73,代码来源:CancelAllocationCommand.java


注:本文中的org.elasticsearch.cluster.routing.ShardRouting.initializing方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。