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. :)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With