5

我最近一直在尝试将云计算作为我大学作业的一部分。

我一直在尝试使用 CloudSim 实现一种新的负载平衡算法(已在一些研究论文中提出)。

请帮我解决这个算法,我在实现它时遇到了一些主要问题。

这是代码

/*
* Title: Load Balancing in Cloud Computing
*/
package org.cloudbus.cloudsim;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public final class MyPolicyNew extends VmAllocationPolicy {

    private Map<String, Host> vmTable;
    private Map<String, Integer> usedPes;
    private List<Integer> freePes;

    private int status[] = new int[100];

    public MyPolicyNew(List<? extends Host> list) {
        super(list);
        setFreePes(new ArrayList<Integer>());
        for (Host host : getHostList()) {
            getFreePes().add(host.getPesNumber());

        }

        setVmTable(new HashMap<String, Host>());
        setUsedPes(new HashMap<String, Integer>());
    }

@Override

public boolean allocateHostForVm(Vm vm) {
    int idx = -1;
    int requiredPes;
    requiredPes = vm.getPesNumber();
    boolean result = false;
    int tries = 0; 
    List<Integer> freePesTmp = new ArrayList<Integer>();

    for (Integer freePes : getFreePes()) {
        freePesTmp.add(freePes);
    }
    int tempstatus[] = new int[100];
    for(int j=0; j<100; j++){
        tempstatus[j]= status[j];
    }
    if (!getVmTable().containsKey(vm.getUid())) {
        do {
            int moreFree = Integer.MIN_VALUE;
            for (int i=0; i < freePesTmp.size(); i++) {
                if ((freePesTmp.get(i) > moreFree) && (tempstatus[i]!=1)) {
                    moreFree = freePesTmp.get(i);
                    idx = i;
                }
                tempstatus[idx]=1;
                int flag=0;
                for(int j=0; j< freePesTmp.size(); j++)
                { //
                    if(tempstatus[j]==1)
                        flag=1;
                    else
                        flag=0;
                }
                if(flag==1){
                    moreFree = Integer.MIN_VALUE;
                    for (int k=0; k < freePesTmp.size(); k++) {
                        if (freePesTmp.get(k) > moreFree) {
                            moreFree = freePesTmp.get(k);
                            idx = k;
                        }
                    }
                }
            }
            Host host = getHostList().get(idx);
            result = host.vmCreate(vm);

            if (result) {
                getVmTable().put(vm.getUid(), host);
                getUsedPes().put(vm.getUid(), requiredPes);

                getFreePes().set(idx, getFreePes().get(idx) - requiredPes);
                status[idx]=1;
                result = true;
                break;
            }
            else {
                freePesTmp.set(idx, Integer.MIN_VALUE);
                tempstatus[idx]=0;
            }
            tries++;
        }while(!result && tries < getFreePes().size());
    }
    return result;
}

@Override
public void deallocateHostForVm(Vm vm) {

    Host host = getVmTable().remove(vm.getUid());
    int idx = getHostList().indexOf(host);
    int pes = getUsedPes().remove(vm.getUid());
    if (host != null) {
        host.vmDestroy(vm);
        status[idx]= 0;
    }
}

getFreePes().set(idx, getFreePes().get(idx) + pes);

@Override
public Host getHost(Vm vm) {
    return getVmTable().get(vm.getUid());
}

@Override
public Host getHost(int vmId, int userId) {
    return getVmTable().get(Vm.getUid(userId, vmId));
}

public Map<String, Host> getVmTable() {
    return vmTable;
}

protected void setVmTable(Map<String, Host> vmTable) {
    this.vmTable = vmTable;
}

protected Map<String, Integer> getUsedPes() {
    return usedPes;
}

protected void setUsedPes(Map<String, Integer> usedPes) {
    this.usedPes = usedPes;
}

protected List<Integer> getFreePes() {
    return freePes;
}

protected void setFreePes(List<Integer> freePes) {
    this.freePes = freePes;
}

@Override
public List<Map<String, Object>> optimizeAllocation(List<? extends Vm> vmList) {
    return null;
}

@Override
public boolean allocateHostForVm(Vm vm, Host host) {
    if (host.vmCreate(vm)){
        getVmTable().put(vm.getUid(), host);
        Log.formatLine("%.2f: VM #" + vm.getId() + " has been allocated to the host #" + host.getId(), CloudSim.clock());
        return true;
    }
    return false;
}

}

无论我在哪里使用该函数,getFreePes()我的 NetBeans 编译器都会出错void cannot be deferenced ,并且cannot find symbol getFreePes().

算法背后的基本思想:: 该算法检查数据中心中是否有空闲主机,如果有,则为该主机分配一个进程并减少该主机的空闲处理器数量。如果没有空闲主机,但有一些空闲处理器,则它检查具有最大处理器数的主机并将新传入进程分配给该主机。

请帮我解决这个代码,我也不是很擅长 Java,我更像是一个 C/C++ 编码器,所以,我在处理事情时遇到了一些问题,所以我在处理这个新库时遇到了一些问题,所以我我不习惯它的很多特性和功能,我的一些前辈帮助我想出了这个代码,但现在它不起作用,请帮助我。

感谢您提前提供任何帮助。:)

4

2 回答 2

1

这是由 定义的策略VmAllocationPolicySimple,因此您不需要实现它。这是一个最差策略,它选择使用最少 PE 的主机来运行 VM。

如果您想使用不同的策略,例如 Best Fit、First Fit 或实施您自己的策略,您可以尝试CloudSim Plus,这是一个最先进、功能齐全、积极维护的 CloudSim 分支。要创建自己的策略,您只需要扩展 VmAllocationPolicyAbstract 类并实现该Optional<Host> findHostForVm(final Vm vm)方法。这样,您就不必担心类的所有内部细节。

例如,下面的方法是 CloudSim PlusVmAllocationPolicySimple类中用于实现 Host 最差主机分配策略的所有代码:

public Optional<Host> findHostForVm(final Vm vm) {
    final Map<Host, Long> map = getHostFreePesMap();
    return map.entrySet()
        .stream()
        .filter(e -> e.getKey().isSuitableForVm(vm))
        .max(Comparator.comparingLong(Map.Entry::getValue))
        .map(Map.Entry::getKey);
}

在 CloudSim Plus 中,您甚至不需要创建 VmAllocationPolicy 子类来实现自定义策略。检查RandomVmAllocationPolicyExample.java了解详细信息。

于 2018-12-02T18:19:08.853 回答
0

我不知道您使用的是哪个版本的 cloudsim。但我也在研究 cloudsim 3.0.3 版,并且我还实施了一些负载平衡策略。

This is the policy I have implemented. Code is not written in good optimized way but it is working. You can give it a try. This is mynewVmallocationpolicy. I think you can see what you have mistaken.

public boolean allocateHostForVm(Vm vm) {
        int requiredPes = vm.getNumberOfPes();
    //  int requiredram = vm.getRam();
        int freeramh1=0;
        int freeramh2=0;
        int freepesh1=0;
        int freepesh2=0;
        boolean result = false;
        int tries = 0;
        List<Integer> freePesTmp = new ArrayList<Integer>();
        for (Integer freePes : getFreePes()) {
            freePesTmp.add(freePes);
        }

        List <Host> hostList =getHostList();


        if (!getVmTable().containsKey(vm.getUid())) { // if this vm was not created
            do {// we still trying until we find a host or until we try all of them

                int idx = -1;

                // we want the host with less pes in use
                    Host h1=hostList.get(0);
                    int j=0;
                    idx=0;
                    for (int i = 1; i < freePesTmp.size(); i++) {
                        Host h2=hostList.get(i);
                        freeramh1=h1.getRamProvisioner().getAvailableRam();
                        freeramh2=h2.getRamProvisioner().getAvailableRam();
                        freepesh1=freePesTmp.get(j);
                        freepesh2=freePesTmp.get(i);
                    //  Log.printLine( " freeram "+ freeramh1 + "h2" + freeramh2 + "free pes " + freepesh1 + "h2" + freepesh2);
                        double diffram=0.0,diffpes=0.0;
                        if(freeramh2!=0 || freeramh1!=0){

                            diffram= (1.0*(freeramh2-freeramh1)/(freeramh2+freeramh1));

                        //  Log.printLine( " inside diffram " + diffram);
                        }
                        else
                            Log.printLine( " fault in ram " );
                        if(freepesh2!=0 || freepesh1!=0){
                            diffpes=(1.0*(freepesh1-freepesh2)/(freepesh1+freepesh2)) ;

                            //Log.printLine( " inside diffpes " + diffpes);
                        }
                        else
                            Log.printLine( " fault in pes ");

                        //Log.printLine( " diffram,diffpes "+ diffram + "diff pes " + diffpes );

                        if(diffram==diffpes || diffpes>diffram){
                            idx=j;  
                        }
                        else{
                            h1=h2;
                            j=i;
                            idx=i;
                            break;
                        }

                    }

                Host host = getHostList().get(idx);
                result = host.vmCreate(vm);

                if (result) { // if vm were succesfully created in the host
                    Log.printLine( " vm " +  "created" + "host" + idx);
                    getVmTable().put(vm.getUid(), host);
                    getUsedPes().put(vm.getUid(), requiredPes);
                    getFreePes().set(idx, getFreePes().get(idx) - requiredPes);
                    result = true;
                    break;
                } else {
                    freePesTmp.set(idx, Integer.MIN_VALUE);
                }
                tries++;
            } while (!result && tries < getFreePes().size());

        }

        return result;
    }

这段代码工作正常。你可以检查一下有什么问题。

于 2015-04-24T18:37:05.070 回答