VM分配负载平衡算法



我有以下主动监控负载均衡器算法的java代码。算法选择负载最小的VM进行请求分配。我必须给这个算法加一个条件。如果选择的VM正好在上一次迭代中使用,则它再次搜索负载最小的VM。else请求被分配给该VM。我该如何将其添加到算法中。算法流程JAVA代码:https://github.com/suhailgupta03/Cloud_Analyst_In_Progress/blob/master/src/cloudsim/ext/datacenter/ActiveVmLoadBalancer.java

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);
            }
        }
    }

}

这可能会对您有所帮助。VM 的当前状态

您可以通过当前时间调用getTotalUtilizationOfCpu方法,然后您将恢复一个双值,表示Vm在该时刻的CPU使用率百分比。基于此,您可以将VM的状态与以前的迭代进行比较。获得此值后,您可以解决VM状态规则并安排任务。

最新更新