Java Code Examples for org.elasticsearch.cluster.routing.ShardRouting#assignedToNode()
The following examples show how to use
org.elasticsearch.cluster.routing.ShardRouting#assignedToNode() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InternalTestCluster.java From crate with Apache License 2.0 | 6 votes |
/** * Returns a set of nodes that have at least one shard of the given index. */ public synchronized Set<String> nodesInclude(String index) { if (clusterService().state().routingTable().hasIndex(index)) { List<ShardRouting> allShards = clusterService().state().routingTable().allShards(index); DiscoveryNodes discoveryNodes = clusterService().state().getNodes(); Set<String> nodes = new HashSet<>(); for (ShardRouting shardRouting : allShards) { if (shardRouting.assignedToNode()) { DiscoveryNode discoveryNode = discoveryNodes.get(shardRouting.currentNodeId()); nodes.add(discoveryNode.getName()); } } return nodes; } return Collections.emptySet(); }
Example 2
Source File: TransportIndicesShardStoresAction.java From Elasticsearch with Apache License 2.0 | 5 votes |
private IndicesShardStoresResponse.StoreStatus.Allocation getAllocation(String index, int shardID, DiscoveryNode node) { for (ShardRouting shardRouting : routingNodes.node(node.id())) { ShardId shardId = shardRouting.shardId(); if (shardId.id() == shardID && shardId.getIndex().equals(index)) { if (shardRouting.primary()) { return IndicesShardStoresResponse.StoreStatus.Allocation.PRIMARY; } else if (shardRouting.assignedToNode()) { return IndicesShardStoresResponse.StoreStatus.Allocation.REPLICA; } else { return IndicesShardStoresResponse.StoreStatus.Allocation.UNUSED; } } } return IndicesShardStoresResponse.StoreStatus.Allocation.UNUSED; }
Example 3
Source File: InternalTestCluster.java From crate with Apache License 2.0 | 5 votes |
private IndexShard getShardOrNull(ClusterState clusterState, ShardRouting shardRouting) { if (shardRouting == null || shardRouting.assignedToNode() == false) { return null; } final DiscoveryNode assignedNode = clusterState.nodes().get(shardRouting.currentNodeId()); if (assignedNode == null) { return null; } return getInstance(IndicesService.class, assignedNode.getName()).getShardOrNull(shardRouting.shardId()); }
Example 4
Source File: RestAllocationAction.java From Elasticsearch with Apache License 2.0 | 4 votes |
private Table buildTable(RestRequest request, final ClusterStateResponse state, final NodesStatsResponse stats) { final ObjectIntScatterMap<String> allocs = new ObjectIntScatterMap<>(); for (ShardRouting shard : state.getState().routingTable().allShards()) { String nodeId = "UNASSIGNED"; if (shard.assignedToNode()) { nodeId = shard.currentNodeId(); } allocs.addTo(nodeId, 1); } Table table = getTableWithHeader(request); for (NodeStats nodeStats : stats.getNodes()) { DiscoveryNode node = nodeStats.getNode(); int shardCount = allocs.getOrDefault(node.id(), 0); ByteSizeValue total = nodeStats.getFs().getTotal().getTotal(); ByteSizeValue avail = nodeStats.getFs().getTotal().getAvailable(); //if we don't know how much we use (non data nodes), it means 0 long used = 0; short diskPercent = -1; if (total.bytes() > 0) { used = total.bytes() - avail.bytes(); if (used >= 0 && avail.bytes() >= 0) { diskPercent = (short) (used * 100 / (used + avail.bytes())); } } table.startRow(); table.addCell(shardCount); table.addCell(nodeStats.getIndices().getStore().getSize()); table.addCell(used < 0 ? null : new ByteSizeValue(used)); table.addCell(avail.bytes() < 0 ? null : avail); table.addCell(total.bytes() < 0 ? null : total); table.addCell(diskPercent < 0 ? null : diskPercent); table.addCell(node.getHostName()); table.addCell(node.getHostAddress()); table.addCell(node.name()); table.endRow(); } final String UNASSIGNED = "UNASSIGNED"; if (allocs.containsKey(UNASSIGNED)) { table.startRow(); table.addCell(allocs.get(UNASSIGNED)); table.addCell(null); table.addCell(null); table.addCell(null); table.addCell(null); table.addCell(null); table.addCell(null); table.addCell(null); table.addCell(UNASSIGNED); table.endRow(); } return table; }
Example 5
Source File: BalancedShardsAllocator.java From Elasticsearch with Apache License 2.0 | 4 votes |
@Override public boolean apply(ShardRouting input) { return input.assignedToNode(); }
Example 6
Source File: AwarenessAllocationDecider.java From Elasticsearch with Apache License 2.0 | 4 votes |
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"); }
Example 7
Source File: TransportBroadcastByNodeAction.java From Elasticsearch with Apache License 2.0 | 4 votes |
protected AsyncAction(Task task, Request request, ActionListener<Response> listener) { this.task = task; this.request = request; this.listener = listener; ClusterState state = clusterService.state(); if (request.getHeader(LoginUserContext.TENANT_FILTER) != null) { state = AuthService.filterState(state, state.metaData(), (Long) request.getHeader(LoginUserContext.TENANT_FILTER)); } clusterState = state; nodes = clusterState.nodes(); ClusterBlockException globalBlockException = checkGlobalBlock(clusterState, request); if (globalBlockException != null) { throw globalBlockException; } String[] concreteIndices = indexNameExpressionResolver.concreteIndices(clusterState, request); ClusterBlockException requestBlockException = checkRequestBlock(clusterState, request, concreteIndices); if (requestBlockException != null) { throw requestBlockException; } if (logger.isTraceEnabled()) { logger.trace("resolving shards for [{}] based on cluster state version [{}]", actionName, clusterState.version()); } ShardsIterator shardIt = shards(clusterState, request, concreteIndices); nodeIds = Maps.newHashMap(); for (ShardRouting shard : shardIt.asUnordered()) { // send a request to the shard only if it is assigned to a node that is in the local node's cluster state // a scenario in which a shard can be assigned but to a node that is not in the local node's cluster state // is when the shard is assigned to the master node, the local node has detected the master as failed // and a new master has not yet been elected; in this situation the local node will have removed the // master node from the local cluster state, but the shards assigned to the master will still be in the // routing table as such if (shard.assignedToNode() && nodes.get(shard.currentNodeId()) != null) { String nodeId = shard.currentNodeId(); if (!nodeIds.containsKey(nodeId)) { nodeIds.put(nodeId, new ArrayList<ShardRouting>()); } nodeIds.get(nodeId).add(shard); } else { unavailableShardExceptions.add( new NoShardAvailableActionException( shard.shardId(), " no shards available for shard " + shard.toString() + " while executing " + actionName ) ); } } responses = new AtomicReferenceArray<>(nodeIds.size()); }
Example 8
Source File: AwarenessAllocationDecider.java From crate with Apache License 2.0 | 4 votes |
private Decision underCapacity(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation, boolean moveToNode) { if (awarenessAttributes.isEmpty()) { return allocation.decision(Decision.YES, NAME, "allocation awareness is not enabled, set cluster setting [%s] to enable it", CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTE_SETTING.getKey()); } IndexMetaData indexMetaData = allocation.metaData().getIndexSafe(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().getAttributes().containsKey(awarenessAttribute)) { return allocation.decision(Decision.NO, NAME, "node does not contain the awareness attribute [%s]; required attributes cluster setting [%s=%s]", awarenessAttribute, CLUSTER_ROUTING_ALLOCATION_AWARENESS_ATTRIBUTE_SETTING.getKey(), allocation.debugDecision() ? Strings.collectionToCommaDelimitedString(awarenessAttributes) : null); } // 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.shardId())) { 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().getAttributes().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().getAttributes().get(awarenessAttribute), 0, -1); shardPerAttribute.addTo(node.node().getAttributes().get(awarenessAttribute), 1); } } else { shardPerAttribute.addTo(node.node().getAttributes().get(awarenessAttribute), 1); } } int numberOfAttributes = nodesPerAttribute.size(); List<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().getAttributes().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, "there are too many copies of the shard allocated to nodes with attribute [%s], there are [%d] total configured " + "shard copies for this shard id and [%d] total attribute values, expected the allocated shard count per " + "attribute [%d] to be less than or equal to the upper bound of the required number of shards per attribute [%d]", awarenessAttribute, shardCount, numberOfAttributes, currentNodeCount, requiredCountPerAttribute + leftoverPerAttribute); } // all is well, we are below or same as average if (currentNodeCount <= requiredCountPerAttribute) { continue; } } return allocation.decision(Decision.YES, NAME, "node meets all awareness attribute requirements"); }
Example 9
Source File: TransportBroadcastByNodeAction.java From crate with Apache License 2.0 | 4 votes |
protected AsyncAction(Task task, Request request, ActionListener<Response> listener) { this.task = task; this.request = request; this.listener = listener; clusterState = clusterService.state(); nodes = clusterState.nodes(); ClusterBlockException globalBlockException = checkGlobalBlock(clusterState, request); if (globalBlockException != null) { throw globalBlockException; } String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(clusterState, request); ClusterBlockException requestBlockException = checkRequestBlock(clusterState, request, concreteIndices); if (requestBlockException != null) { throw requestBlockException; } if (logger.isTraceEnabled()) { logger.trace("resolving shards for [{}] based on cluster state version [{}]", actionName, clusterState.version()); } ShardsIterator shardIt = shards(clusterState, request, concreteIndices); nodeIds = new HashMap<>(); for (ShardRouting shard : shardIt) { // send a request to the shard only if it is assigned to a node that is in the local node's cluster state // a scenario in which a shard can be assigned but to a node that is not in the local node's cluster state // is when the shard is assigned to the master node, the local node has detected the master as failed // and a new master has not yet been elected; in this situation the local node will have removed the // master node from the local cluster state, but the shards assigned to the master will still be in the // routing table as such if (shard.assignedToNode() && nodes.get(shard.currentNodeId()) != null) { String nodeId = shard.currentNodeId(); if (!nodeIds.containsKey(nodeId)) { nodeIds.put(nodeId, new ArrayList<>()); } nodeIds.get(nodeId).add(shard); } else { unavailableShardExceptions.add( new NoShardAvailableActionException( shard.shardId(), " no shards available for shard " + shard.toString() + " while executing " + actionName ) ); } } responses = new AtomicReferenceArray<>(nodeIds.size()); }