}
List<VirtualNode> vNodes = new ArrayList<VirtualNode>();
for (int i = 0; i < vNodeNum; i++) {
VirtualNode vNode = new VirtualNode(vGroup);
int count = 0;
boolean primaryExisted = false;
String primaryGroup = null;
for (NodeGroupCreate nodeGroup : vGroup.getNodeGroups()) {
/*
* by default, if there are no instance_per_host policy, one virtual node contains
* one base node
*/
int baseNodeNum = 1;
if (nodeGroup.instancePerHost() != null) {
// multiple base nodes inside a virtual node
baseNodeNum = nodeGroup.instancePerHost();
}
/*
* imagine a dc-split case, where a virtual group contains a data group (4 nodes), and a
* compute group (3 nodes). Apparently, the last virtual node should contain only one data
* node.
*/
if (i * baseNodeNum >= nodeGroup.getInstanceNum()) {
baseNodeNum = 0;
}
for (int j = 0; j < baseNodeNum; j++) {
BaseNode newGuy =
getBaseNode(cluster, nodeGroup, i * baseNodeNum + j);
boolean existed = false;
if (existedNodes != null && existedNodes.size() > 0) {
// existedNodes -> Map<NodeName>
for (BaseNode node : existedNodes) {
if (newGuy.getVmName().equalsIgnoreCase(node.getVmName())) {
existed = true;
// handle the special data node existence case here
if (mixed
&& newGuy.getNodeGroup().getReferredGroup() == null) {
primaryExisted = true;
primaryGroup = newGuy.getNodeGroup().getName();
}
break;
}
}
}
if (!existed) {
vNode.addNode(newGuy);
count++;
}
}
}
if (count == 0)
continue;
logger.info("put " + count + " base nodes into one virtual node");
if (primaryExisted) {
vNode.setReferToGroup(primaryGroup);
vNode.setStrictAssociated(true);
}
vNodes.add(vNode);
}
return vNodes;