/** * Returns the shards id * * @return id of the shard */ public ShardId getShardId() { return shardId(); }
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("IndexShardRoutingTable(").append(shardId()).append("){"); final int numShards = shards.size(); for (int i = 0; i < numShards; i++) { sb.append(shards.get(i).shortSummary()); if (i < numShards - 1) { sb.append(", "); } } sb.append("}"); return sb.toString(); } }
public Builder addIndexShard(IndexShardRoutingTable indexShard) { shards.put(indexShard.shardId().id(), indexShard); return this; }
public String prettyPrint() { StringBuilder sb = new StringBuilder("-- index [" + index + "]\n"); List<IndexShardRoutingTable> ordered = new ArrayList<>(); for (IndexShardRoutingTable indexShard : this) { ordered.add(indexShard); } CollectionUtil.timSort(ordered, (o1, o2) -> { int v = o1.shardId().getIndex().getName().compareTo( o2.shardId().getIndex().getName()); if (v == 0) { v = Integer.compare(o1.shardId().id(), o2.shardId().id()); } return v; }); for (IndexShardRoutingTable indexShard : ordered) { sb.append("----shard_id [").append(indexShard.shardId().getIndex().getName()) .append("][").append(indexShard.shardId().id()).append("]\n"); for (ShardRouting shard : indexShard) { sb.append("--------").append(shard.shortSummary()).append("\n"); } } return sb.toString(); }
public static void writeTo(IndexShardRoutingTable indexShard, StreamOutput out) throws IOException { indexShard.shardId().getIndex().writeTo(out); writeToThin(indexShard, out); }
/** * Returns shards based on nodeAttributes given such as node name , node attribute, node IP * Supports node specifications in cluster API */ public ShardIterator onlyNodeSelectorActiveInitializingShardsIt(String[] nodeAttributes, DiscoveryNodes discoveryNodes) { ArrayList<ShardRouting> ordered = new ArrayList<>(activeShards.size() + allInitializingShards.size()); Set<String> selectedNodes = Sets.newHashSet(discoveryNodes.resolveNodes(nodeAttributes)); int seed = shuffler.nextSeed(); for (ShardRouting shardRouting : shuffler.shuffle(activeShards, seed)) { if (selectedNodes.contains(shardRouting.currentNodeId())) { ordered.add(shardRouting); } } for (ShardRouting shardRouting : shuffler.shuffle(allInitializingShards, seed)) { if (selectedNodes.contains(shardRouting.currentNodeId())) { ordered.add(shardRouting); } } if (ordered.isEmpty()) { final String message = String.format( Locale.ROOT, "no data nodes with %s [%s] found for shard: %s", nodeAttributes.length == 1 ? "criteria" : "criterion", String.join(",", nodeAttributes), shardId()); throw new IllegalArgumentException(message); } return new PlainShardIterator(shardId, ordered); }
/** * @return all shard ids the request should run on */ protected List<ShardId> shards(Request request, ClusterState clusterState) { List<ShardId> shardIds = new ArrayList<>(); String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(clusterState, request); for (String index : concreteIndices) { IndexMetaData indexMetaData = clusterState.metaData().getIndices().get(index); if (indexMetaData != null) { for (IntObjectCursor<IndexShardRoutingTable> shardRouting : clusterState.getRoutingTable().indicesRouting().get(index).getShards()) { shardIds.add(shardRouting.value.shardId()); } } } return shardIds; }
@Override protected void masterOperation(IndicesShardStoresRequest request, ClusterState state, ActionListener<IndicesShardStoresResponse> listener) { final RoutingTable routingTables = state.routingTable(); final RoutingNodes routingNodes = state.getRoutingNodes(); final String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(state, request); final Set<ShardId> shardIdsToFetch = new HashSet<>(); logger.trace("using cluster state version [{}] to determine shards", state.version()); // collect relevant shard ids of the requested indices for fetching store infos for (String index : concreteIndices) { IndexRoutingTable indexShardRoutingTables = routingTables.index(index); if (indexShardRoutingTables == null) { continue; } for (IndexShardRoutingTable routing : indexShardRoutingTables) { final int shardId = routing.shardId().id(); ClusterShardHealth shardHealth = new ClusterShardHealth(shardId, routing); if (request.shardStatuses().contains(shardHealth.getStatus())) { shardIdsToFetch.add(routing.shardId()); } } } // async fetch store infos from all the nodes // NOTE: instead of fetching shard store info one by one from every node (nShards * nNodes requests) // we could fetch all shard store info from every node once (nNodes requests) // we have to implement a TransportNodesAction instead of using TransportNodesListGatewayStartedShards // for fetching shard stores info, that operates on a list of shards instead of a single shard new AsyncShardStoresInfoFetches(state.nodes(), routingNodes, shardIdsToFetch, listener).start(); }
private void deleteShardIfExistElseWhere(ClusterState state, IndexShardRoutingTable indexShardRoutingTable) { List<Tuple<DiscoveryNode, ShardActiveRequest>> requests = new ArrayList<>(indexShardRoutingTable.size()); String indexUUID = indexShardRoutingTable.shardId().getIndex().getUUID(); ClusterName clusterName = state.getClusterName(); for (ShardRouting shardRouting : indexShardRoutingTable) { assert shardRouting.started() : "expected started shard but was " + shardRouting; DiscoveryNode currentNode = state.nodes().get(shardRouting.currentNodeId()); requests.add(new Tuple<>(currentNode, new ShardActiveRequest(clusterName, indexUUID, shardRouting.shardId(), deleteShardTimeout))); } ShardActiveResponseHandler responseHandler = new ShardActiveResponseHandler(indexShardRoutingTable.shardId(), state.getVersion(), requests.size()); for (Tuple<DiscoveryNode, ShardActiveRequest> request : requests) { logger.trace("{} sending shard active check to {}", request.v2().shardId, request.v1()); transportService.sendRequest(request.v1(), ACTION_SHARD_EXISTS, request.v2(), responseHandler); } }
expected.remove(indexShardRoutingTable.shardId().id()); int routingNumberOfReplicas = indexShardRoutingTable.size() - 1; if (routingNumberOfReplicas != indexMetaData.getNumberOfReplicas()) { throw new IllegalStateException("Shard [" + indexShardRoutingTable.shardId().id() + "] routing table has wrong number of replicas, expected [" + indexMetaData.getNumberOfReplicas() + "], got [" + routingNumberOfReplicas + "]");
boolean found = false; for (String id : ids) { if (Integer.parseInt(id) == indexShard.shardId().id()) { found = true; break;
int shardId = shardRoutingTable.shardId().id(); shards.put(shardId, new ClusterShardHealth(shardId, shardRoutingTable));
builder.startObject("shards"); for (IndexShardRoutingTable indexShardRoutingTable : indexRoutingTable) { builder.startArray(Integer.toString(indexShardRoutingTable.shardId().id())); for (ShardRouting shardRouting : indexShardRoutingTable) { shardRouting.toXContent(builder, params);
/** * Returns the shards id * * @return id of the shard */ public ShardId getShardId() { return shardId(); }
/** * Returns the shards id * * @return id of the shard */ public ShardId getShardId() { return shardId(); }
/** * Returns the shards id * * @return id of the shard */ public ShardId getShardId() { return shardId(); }
/** * Adds a new shard routing (makes a copy of it), with reference data used from the index shard routing table * if it needs to be created. */ public Builder addShard(ShardRouting shard) { IndexShardRoutingTable indexShard = shards.get(shard.id()); if (indexShard == null) { indexShard = new IndexShardRoutingTable.Builder(shard.shardId()).addShard(shard).build(); } else { indexShard = new IndexShardRoutingTable.Builder(indexShard).addShard(shard).build(); } shards.put(indexShard.shardId().id(), indexShard); return this; }
IndexShardRoutingTable.Builder builder = new IndexShardRoutingTable.Builder(indexShard.shardId()); for (ShardRouting shardRouting : indexShard) { builder.addShard(shardRouting);
ShardId shardId = indexShardRoutingTable.shardId(); if (folderNotFoundCache.contains(shardId) == false && shardCanBeDeleted(localNodeId, indexShardRoutingTable)) { IndexService indexService = indicesService.indexService(indexRoutingTable.getIndex());
/** * Returns the expected shard size for the given shard or the default value provided if not enough information are available * to estimate the shards size. */ public static long getExpectedShardSize(ShardRouting shard, RoutingAllocation allocation, long defaultValue) { final IndexMetaData metaData = allocation.metaData().getIndexSafe(shard.index()); final ClusterInfo info = allocation.clusterInfo(); if (metaData.getResizeSourceIndex() != null && shard.active() == false && shard.recoverySource().getType() == RecoverySource.Type.LOCAL_SHARDS) { // in the shrink index case we sum up the source index shards since we basically make a copy of the shard in // the worst case long targetShardSize = 0; final Index mergeSourceIndex = metaData.getResizeSourceIndex(); final IndexMetaData sourceIndexMeta = allocation.metaData().index(mergeSourceIndex); if (sourceIndexMeta != null) { final Set<ShardId> shardIds = IndexMetaData.selectRecoverFromShards(shard.id(), sourceIndexMeta, metaData.getNumberOfShards()); for (IndexShardRoutingTable shardRoutingTable : allocation.routingTable().index(mergeSourceIndex.getName())) { if (shardIds.contains(shardRoutingTable.shardId())) { targetShardSize += info.getShardSize(shardRoutingTable.primaryShard(), 0); } } } return targetShardSize == 0 ? defaultValue : targetShardSize; } else { return info.getShardSize(shard, defaultValue); } } }