VM-Assign负载平衡算法

时间:2016-04-01 17:07:11

标签: java algorithm cloud load-balancing

我有以下java代码的主动监控负载均衡器算法。算法选择负载最小的VM进行请求分配。我必须为这个算法添加一个条件。如果选择的VM在最后一次迭代中使用,则再次搜索最少加载的VM。 else请求被分配给该VM。我如何才能将其添加到算法中。 Flow of algorithm 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);
            }
        }
    }


}

1 个答案:

答案 0 :(得分:0)

这可能会对你有所帮助。 Current Status of VM

您可以调用getTotalUtilizationOfCpu方法传递当前时间,然后您将恢复一个double值,表示此时Vm的CPU使用率百分比。基于此,您可以将VM的状态与先前的迭代进行比较。获得此值可以解决VM状态规则和计划任务。