There is a LinkedList with the name busyList
that keeps track of all the machines that are busy.A machine may be busy or available.The id
of the machine is removed from the list when it gets free.
Following is the method that gets fired whenever the machine gets free/busy
@Override
public void cloudSimEventFired(CloudSimEvent e) {
if(e.getId() == CloudSimEvents.EVENT_CLOUDLET_ALLOCATED_TO_VM) {
int vmId = (Integer) e.getParameter(Constants.PARAM_VM_ID);
busyList.add(vmId);
}else if(e.getId() == CloudSimEvents.EVENT_VM_FINISHED_CLOUDLET) {
int vmId = (Integer) e.getParameter(Constants.PARAM_VM_ID);
busyList.remove(new Integer(vmId));
}
}
A map named allMap
(for allocation map) has id of the machine as its key and an object of class VmAllocation
as its value.The attributes of VmAllocation
are :
maxAlloc (which tells the maximum times a machine can be returned)
currAlloc (which tells the current count)
/*
allMap.put(id, new VmAllocation(o.getMemory()/512, 0));
o.getMemory()/512 => maxAlloc
0 => currAlloc
This map has been initialized earlier.Like for 3 machines id will be 0,1,2
As the machine will be returned currAlloc will be incremented by 1
but it cannot exceed maxAlloc.
*/
I have to return the machines in a round robin fashion while keeping in view, that I do not return the machine that is busy at the moment.If busyList.contains(id)
returns true, it means the machine is busy and the next available machine (in the allMap) is returned also keeping in view that it doesn't get more than maxAlloc
set for it. For example if 1
comes busy, then 2
will be returned if 2
is not busy and currAlloc
has not exceeded maxAlloc
Following is the method that has to return the machine and is called multiple times from outside the enclosing class.
@Override
public int getNextAvailableVm() {
// return the id
}
This is the general way algorithm shall work :
Let the id of the machines inside the allMap be :
0,1,2,3,4
0 has maxAlloc of 4
1 has maxAlloc of 4
2,3 has maxAlloc of 2
4 has maxAlloc of 1
The ideal order of returning will be :
0000 1111 22 33 4 0000 1111 22 33 4 00....
But this happens only when none of the machine is ever busy. But if machines get busy the order could be like :
0 0 1 2 0 1 0 1 ....
Note that the number of times a machine is returned cannot exceed maxAlloc
and if machine is found busy next machine that will be returned, is the one that could be just next to the machine that is currently busy and that itself is not busy (Also currAlloc should not exceed maxAlloc)
If maxAlloc = 5, currAlloc limit is 4. Because its count starts from 0
I am unable to implement this logic. Help me do this.