Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 虚拟机分配负载平衡算法_Java_Algorithm_Cloud_Load Balancing - Fatal编程技术网

Java 虚拟机分配负载平衡算法

Java 虚拟机分配负载平衡算法,java,algorithm,cloud,load-balancing,Java,Algorithm,Cloud,Load Balancing,我有以下主动监视负载平衡器算法的java代码。算法选择负载最少的VM进行请求分配。我必须给这个算法增加一个条件。若选定的VM正好在最后一次迭代中使用,那个么它将再次搜索负载最少的VM。else请求分配给该VM。如何将此添加到算法中。 JAVA代码: 包cloudsim.ext.datacenter; 导入java.util.Collections; 导入java.util.HashMap; 导入java.util.Map; 导入cloudsim.ext.Constants; 导入cloudsim

我有以下主动监视负载平衡器算法的java代码。算法选择负载最少的VM进行请求分配。我必须给这个算法增加一个条件。若选定的VM正好在最后一次迭代中使用,那个么它将再次搜索负载最少的VM。else请求分配给该VM。如何将此添加到算法中。 JAVA代码:

包cloudsim.ext.datacenter;
导入java.util.Collections;
导入java.util.HashMap;
导入java.util.Map;
导入cloudsim.ext.Constants;
导入cloudsim.ext.event.CloudSimEvent;
导入cloudsim.ext.event.CloudSimEventListener;
导入cloudsim.ext.event.CloudSimEvents;
导入java.util.Set;
公共类ActiveVmLoadBalancer扩展VmLoadBalancer实现CloudSimEventListener{
/**保存每个VM上当前活动AllCoActions的计数*/
私有映射currentAllocationCounts;
私有地图列表;
公共ActiveVmLoadBalancer(数据中心控制器dcb){
dcb.addCloudSimEventListener(此);
this.vmStatesList=dcb.getVmStatesList();
this.currentAllocationCounts=Collections.synchronizedMap(新HashMap());
}
/**
*@返回虚拟机的虚拟机id,以便保留每个虚拟机上活动任务的数量
*在虚拟机之间均匀分布。
*/
@凌驾
public int getNextAvailableVm(){
intvmid=-1;
//查找分配数最少的vm
//如果未分配所有可用的虚拟机,请分配新的虚拟机
如果(currentAllocationCounts.size()
这可能会对您有所帮助


您可以通过当前时间调用GetTotalizationOfcu方法,然后恢复一个双精度值,该值表示当时Vm的CPU使用率百分比。基于此,您可以将VM的状态与以前的迭代进行比较。获取此值可以解决VM状态规则和计划任务。

是否有理由将其标记为
c
,或者您只是觉得标记美观?否,我将其标记为。。bcz只需在代码中循环一次即可。。但我在模拟中遇到了错误。。只是逻辑上的错误,所以你应该把你的问题说得更具体些。您不能仅仅发布代码并说“请接受它并为我实现此修改”。相反,请确切地解释你的疑问是什么,你无法理解的是什么。这样,你肯定会得到更多的帮助。谢谢。我将使用do while循环实现这个条件。当(vmId==assignedVm)。。但模拟(云分析师)dosent显示了任何结果
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);
            }
        }
    }


}