double[] cpus = new double[numOffers];
double[] mem = new double[numOffers];
// Count up the amount of free CPUs and memory on each node
for (int i = 0; i < numOffers; i++) {
Offer offer = offers.get(i);
LOG.info("Got resource offer " + offer.getId());
cpus[i] = getResource(offer, "cpus");
mem[i] = getResource(offer, "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>();
List<List<TaskInfo>> replies =
new ArrayList<List<TaskInfo>>(numOffers);
for (int i = 0; i < numOffers; i++) {
indices.add(i);
replies.add(new ArrayList<TaskInfo>());
}
while (indices.size() > 0) {
for (Iterator<Integer> it = indices.iterator(); it.hasNext();) {
int i = it.next();
Offer offer = offers.get(i);
TaskInfo task = findTask(
offer.getSlaveId(), offer.getHostname(), cpus[i], mem[i]);
if (task != null) {
cpus[i] -= getResource(task, "cpus");
mem[i] -= getResource(task, "mem");
replies.get(i).add(task);
} else {