0
我有以下java代碼的主動監控負載平衡算法。算法選擇最少加載的VM進行請求分配。我必須爲此算法添加一個條件。如果選擇的虛擬機恰好在最後一次迭代中使用,則會再次搜索最小負載的虛擬機。否則請求被分配給該VM。如何將此添加到算法中。 Flow of algorithm Java代碼:https://github.com/suhailgupta03/Cloud_Analyst_In_Progress/blob/master/src/cloudsim/ext/datacenter/ActiveVmLoadBalancer.javaVM-Assign負載平衡算法
package cloudsim.ext.datacenter;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import cloudsim.ext.Constants;
import cloudsim.ext.event.CloudSimEvent;
import cloudsim.ext.event.CloudSimEventListener;
import cloudsim.ext.event.CloudSimEvents;
import java.util.Set;
public class ActiveVmLoadBalancer extends VmLoadBalancer implements CloudSimEventListener {
/** Holds the count current active allcoations on each VM */
private Map<Integer, Integer> currentAllocationCounts;
private Map<Integer, VirtualMachineState> vmStatesList;
public ActiveVmLoadBalancer(DatacenterController dcb){
dcb.addCloudSimEventListener(this);
this.vmStatesList = dcb.getVmStatesList();
this.currentAllocationCounts = Collections.synchronizedMap(new HashMap<Integer, Integer>());
}
/**
* @return The VM id of a VM so that the number of active tasks on each VM is kept
* evenly distributed among the VMs.
*/
@Override
public int getNextAvailableVm(){
int vmId = -1;
//Find the vm with least number of allocations
//If all available vms are not allocated, allocated the new ones
if (currentAllocationCounts.size() < vmStatesList.size()){
for (int availableVmId : vmStatesList.keySet()){
if (!currentAllocationCounts.containsKey(availableVmId)){
vmId = availableVmId;
break;
}
}
} else {
int currCount;
int minCount = Integer.MAX_VALUE;
for (int thisVmId : currentAllocationCounts.keySet()){
currCount = currentAllocationCounts.get(thisVmId);
if (currCount < minCount){
minCount = currCount;
vmId = thisVmId;
}
}
}
allocatedVm(vmId);
return vmId;
}
@Override
public void cloudSimEventFired(CloudSimEvent e) {
if (e.getId() == CloudSimEvents.EVENT_CLOUDLET_ALLOCATED_TO_VM){
int vmId = (Integer) e.getParameter(Constants.PARAM_VM_ID);
Integer currCount = currentAllocationCounts.remove(vmId);
if (currCount == null){
currCount = 1;
} else {
currCount++;
}
currentAllocationCounts.put(vmId, currCount);
} else if (e.getId() == CloudSimEvents.EVENT_VM_FINISHED_CLOUDLET){
int vmId = (Integer) e.getParameter(Constants.PARAM_VM_ID);
Integer currCount = currentAllocationCounts.remove(vmId);
if (currCount != null){
currCount--;
currentAllocationCounts.put(vmId, currCount);
}
}
}
}
是不是有一個你把它標記爲'c'的原因,還是你覺得標籤美觀? – EOF
不,我標記它.. bcz它只需要一個做while循環代碼..但我得到錯誤的模擬..只是錯誤的邏輯,所以.. – Yagnesh
你應該讓你的問題更具體。你不能只發布你的代碼,並說「請採取它併爲我實施這個修改」。相反,請解釋你的疑問到底是什麼,你無法弄清楚是什麼。那樣,你一定會得到更多的幫助。謝謝。 – lrnzcig