Collection<String> liveNodes, List<MasterOperation> runningOperations) throws IndexDeployException {
if (liveNodes.isEmpty()) {
throw new IndexDeployException(ErrorType.NO_NODES_AVAILIBLE, "no nodes availible");
}
InteractionProtocol protocol = context.getProtocol();
Set<Shard> shards = indexMD.getShards();
// now distribute shards
final Map<String, List<String>> currentIndexShard2NodesMap = protocol
.getShard2NodesMap(Shard.getShardNames(shards));
Map<String, List<String>> currentGlobalNode2ShardsMap = getCurrentNode2ShardMap(liveNodes, protocol
.getShard2NodesMap(protocol.getShard2NodeShards()));
addRunningDeployments(currentGlobalNode2ShardsMap, runningOperations);
final Map<String, List<String>> newNode2ShardMap = context.getDeployPolicy().createDistributionPlan(
currentIndexShard2NodesMap, cloneMap(currentGlobalNode2ShardsMap), new ArrayList<String>(liveNodes),
indexMD.getReplicationLevel());
// System.out.println(distributionMap);// node to shards
Set<String> nodes = newNode2ShardMap.keySet();
List<OperationId> operationIds = new ArrayList<OperationId>(nodes.size());
One2ManyListMap<String, String> newShardsByNode = new One2ManyListMap<String, String>();
for (String node : nodes) {
List<String> nodeShards = newNode2ShardMap.get(node);
List<String> listOfAdded = CollectionUtil.getListOfAdded(currentGlobalNode2ShardsMap.get(node), nodeShards);
if (!listOfAdded.isEmpty()) {
ShardDeployOperation deployInstruction = new ShardDeployOperation();
for (String shard : listOfAdded) {
deployInstruction.addShard(shard, indexMD.getShardPath(shard));
newShardsByNode.add(node, shard);
}
OperationId operationId = protocol.addNodeOperation(node, deployInstruction);
operationIds.add(operationId);
}
List<String> listOfRemoved = CollectionUtil.getListOfRemoved(currentGlobalNode2ShardsMap.get(node), nodeShards);
if (!listOfRemoved.isEmpty()) {
ShardUndeployOperation undeployInstruction = new ShardUndeployOperation(listOfRemoved);
OperationId operationId = protocol.addNodeOperation(node, undeployInstruction);
operationIds.add(operationId);
}
}
_newShardsByNodeMap = newShardsByNode.asMap();
return operationIds;