// Task is no longer PENDING no need to preempt
if (pendingTasks.isEmpty()) {
return Optional.absent();
}
final IAssignedTask pendingTask = Iterables.getOnlyElement(pendingTasks);
Multimap<String, IAssignedTask> slavesToActiveTasks = getSlavesToActiveTasks();
if (slavesToActiveTasks.isEmpty()) {
return Optional.absent();
}
attemptedPreemptions.incrementAndGet();
// Group the offers by slave id so they can be paired with active tasks from the same slave.
Multimap<String, HostOffer> slavesToOffers =
Multimaps.index(offerQueue.getOffers(), OFFER_TO_SLAVE_ID);
Set<String> allSlaves = ImmutableSet.<String>builder()
.addAll(slavesToOffers.keySet())
.addAll(slavesToActiveTasks.keySet())
.build();
for (String slaveID : allSlaves) {
final Optional<Set<IAssignedTask>> toPreemptTasks = getTasksToPreempt(
slavesToActiveTasks.get(slaveID),
slavesToOffers.get(slaveID),
pendingTask,
attributeAggregate);
if (toPreemptTasks.isPresent()) {
storage.write(new MutateWork.NoResult.Quiet() {
@Override
protected void execute(MutableStoreProvider storeProvider) {
for (IAssignedTask toPreempt : toPreemptTasks.get()) {
stateManager.changeState(
storeProvider,
toPreempt.getTaskId(),
Optional.<ScheduleStatus>absent(),
PREEMPTING,
Optional.of("Preempting in favor of " + pendingTask.getTaskId()));
tasksPreempted.incrementAndGet();
}
}
});
return Optional.of(slaveID);