How do I make the algorithm wait, so that all the machines handle the
requests they are entitled to handle?
Following is a method that allocates machines in a round-robin order. A
machine is allocated(or returned) if it is not busy and
current-allocations for that machine doesn't exceed maximum-allocations
allowed for the machine.
A list named busyList is maintained that tells if the machine is busy or
is available.This list is automatically updated when a machine is freed or
is booked.
There is a bug in the following method. If at any point of time all the
machines come busy,instead of waiting for a machine to be available it
starts the round-robin from 0. It means it resets the allocation counters
for all the machines.This is wrong ! Next circle of round robin should
only start when, in the current circle all the machines have handled the
requests they are meant to handle.
@Override
public int getNextAvailableVm() {
synchronized(this) {
int machineID;
VmAllocation allocation;
for(Map.Entry<Integer,VmAllocation> entry : allMap.entrySet()) {
machineID = (Integer)(entry.getKey());
allocation = (VmAllocation)(entry.getValue());
if(!busyList.contains(machineID) &&
allocation.getCurrAlloc() < allocation.getMaxAlloc()) {
allocation.setCurrAlloc(allocation.getCurrAlloc() + 1);
return machineID;
}
}
for(Map.Entry<Integer, VmAllocation> entry: allMap.entrySet()) {
allocation = (VmAllocation)(entry.getValue());
allocation.setCurrAlloc(0);
}
allocation = (VmAllocation)allMap.get(0);
allocation.setCurrAlloc(1);
}
return 0;
}
How do I implement this logic ? I want that every machine should handle
the tasks they are entitled to handle before the starting the round-robin
circle again
Note : This method is called multiple times from another class.
No comments:
Post a Comment