int[] cpus = new int[numOffers];
int[] mem = new int[numOffers];
// Count up the amount of free CPUs and memory on each node
for (int i = 0; i < numOffers; i++) {
SlaveOffer offer = offers.get(i);
cpus[i] = Integer.parseInt(offer.getParams().get("cpus"));
mem[i] = Integer.parseInt(offer.getParams().get("mem"));
}
// Assign tasks to the nodes in a round-robin manner, and stop when we
// are unable to assign a task to any node.
// We do this by keeping a linked list of indices of nodes for which
// we are still considering assigning tasks. Whenever we can't find a
// new task for a node, we remove it from the list. When the list is
// empty, no further assignments can be made. This algorithm was chosen
// because it minimizing the amount of scanning we need to do if we
// get a large set of offered nodes.
List<Integer> indices = new LinkedList<Integer>();
for (int i = 0; i < numOffers; i++) {
indices.add(i);
}
Collections.shuffle(indices);
while (indices.size() > 0) {
for (Iterator<Integer> it = indices.iterator(); it.hasNext();) {
int i = it.next();
SlaveOffer offer = offers.get(i);
TaskDescription task = findTask(
offer.getSlaveId(), offer.getHost(), cpus[i], mem[i]);
if (task != null) {
cpus[i] -= Integer.parseInt(task.getParams().get("cpus"));
mem[i] -= Integer.parseInt(task.getParams().get("mem"));
tasks.add(task);
} else {