Load Balancing CloudSim

7.2k views Asked by At

I have been recently trying to work on Cloud Computing as a part of my College assignment.

I have been trying to implement a new Load Balancing algorithm, (which has been proposed in some research paper) using CloudSim.

Please help me out with this algorithm, I have some major issues implementing it.

This is the code

/*
* 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;
}

}

Where ever I use the function getFreePes() my NetBeans compiler gives an error void cannot be deferenced and cannot find symbol getFreePes().

Basic idea behind the algorithm:: The algorithm checks if there are any free hosts in the data center and if there are any then it assigns a process to that host and decreases the number of free processors with that host. If there are no free hosts, but have some free processors then it checks the host with the maximum number of processors and assigns the new incoming process to that host..

Please help me out with this code, I am not very good at Java either, I have been more of a C/C++ coder, so, I am having a bit of problem dealing with things and further this a new library to me so I am not used to much of its features and function, a few of my seniors have helped me come up with this code, but now its not working, please help me guys.

Thanks for any help in advance. :)

2

There are 2 answers

0
Akash Mutha On BEST ANSWER

I do not know which version of cloudsim you are using. But I am also working on cloudsim version 3.0.3 and I have implemented also some load balancing policies.

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

This code is working fine. You can check whats wrong.

0
Manoel Campos On

This is the policy defined by the VmAllocationPolicySimple, so you don't need to implement it. It's a worst-fit policy that selects the Host with the fewest PEs in use to run a VM.

If you want to use different policies such as Best Fit, First Fit or implement your own policy, you can try CloudSim Plus, a state-of-the-art, full-featured, actively-maintained CloudSim fork. To create your own policy you just need to extend VmAllocationPolicyAbstract class and implement the Optional<Host> findHostForVm(final Vm vm) method. This way, you don't need to worry about all the internal details of the class.

As an example, the method below is all the code used in CloudSim Plus' VmAllocationPolicySimple class to implement the Host worst-fit host allocation policy:

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

And in CloudSim Plus, you don't even need to create a VmAllocationPolicy subclass to implement a custom policy. Check the RandomVmAllocationPolicyExample.java for details.