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. :)
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.