use of com.hubspot.singularity.SingularityTaskId in project Singularity by HubSpot.
the class SingularityMesosOfferScheduler method checkOffers.
public Collection<SingularityOfferHolder> checkOffers(final Collection<Offer> offers) {
for (SingularityPendingTaskId taskId : taskManager.getPendingTasksMarkedForDeletion()) {
lock.runWithRequestLock(() -> taskManager.deletePendingTask(taskId), taskId.getRequestId(), String.format("%s#%s", getClass().getSimpleName(), "checkOffers -> pendingTaskDeletes"));
}
scheduler.checkForDecomissions();
scheduler.drainPendingQueue();
if (offers.isEmpty()) {
LOG.debug("No offers to check");
return Collections.emptyList();
}
final List<SingularityTaskRequestHolder> sortedTaskRequestHolders = getSortedDueTaskRequests();
final int numDueTasks = sortedTaskRequestHolders.size();
final Map<String, SingularityOfferHolder> offerHolders = offers.stream().collect(Collectors.groupingBy((o) -> o.getAgentId().getValue())).entrySet().stream().filter((e) -> e.getValue().size() > 0).map((e) -> {
List<Offer> offersList = e.getValue();
String slaveId = e.getKey();
return new SingularityOfferHolder(offersList, numDueTasks, slaveAndRackHelper.getRackIdOrDefault(offersList.get(0)), slaveId, offersList.get(0).getHostname(), slaveAndRackHelper.getTextAttributes(offersList.get(0)), slaveAndRackHelper.getReservedSlaveAttributes(offersList.get(0)));
}).collect(Collectors.toMap(SingularityOfferHolder::getSlaveId, Function.identity()));
if (sortedTaskRequestHolders.isEmpty()) {
return offerHolders.values();
}
final AtomicInteger tasksScheduled = new AtomicInteger(0);
Map<String, RequestUtilization> requestUtilizations = usageManager.getRequestUtilizations();
List<SingularityTaskId> activeTaskIds = taskManager.getActiveTaskIds();
final Map<String, SingularitySlaveUsageWithCalculatedScores> currentSlaveUsagesBySlaveId = usageManager.getCurrentSlaveUsages(offerHolders.values().stream().map(SingularityOfferHolder::getSlaveId).collect(Collectors.toList())).parallelStream().collect(Collectors.toMap(SingularitySlaveUsageWithId::getSlaveId, (usageWithId) -> new SingularitySlaveUsageWithCalculatedScores(usageWithId, configuration.getMesosConfiguration().getScoringStrategy(), configuration.getMesosConfiguration().getScoreUsingSystemLoad(), getMaxProbableUsageForSlave(activeTaskIds, requestUtilizations, offerHolders.get(usageWithId.getSlaveId()).getSanitizedHost()))));
LOG.trace("Found slave usages {}", currentSlaveUsagesBySlaveId);
Map<String, Integer> tasksPerOfferHost = new ConcurrentHashMap<>();
for (SingularityTaskRequestHolder taskRequestHolder : sortedTaskRequestHolders) {
lock.runWithRequestLock(() -> {
Map<String, Double> scorePerOffer = new ConcurrentHashMap<>();
List<SingularityTaskId> activeTaskIdsForRequest = leaderCache.getActiveTaskIdsForRequest(taskRequestHolder.getTaskRequest().getRequest().getId());
List<CompletableFuture<Void>> scoringFutures = new ArrayList<>();
AtomicReference<Throwable> scoringException = new AtomicReference<>(null);
for (SingularityOfferHolder offerHolder : offerHolders.values()) {
if (!isOfferFull(offerHolder)) {
scoringFutures.add(offerScoringSemaphore.call(() -> CompletableFuture.runAsync(() -> {
try {
double score = calculateScore(offerHolder, currentSlaveUsagesBySlaveId, tasksPerOfferHost, taskRequestHolder, activeTaskIdsForRequest);
if (score != 0) {
scorePerOffer.put(offerHolder.getSlaveId(), score);
}
} catch (Throwable t) {
LOG.error("Uncaught exception while scoring offers", t);
scoringException.set(t);
}
}, offerScoringExecutor)));
}
}
CompletableFutures.allOf(scoringFutures).join();
if (scoringException.get() != null) {
LOG.warn("Exception caught in offer scoring futures, semaphore info: (concurrentRequests: {}, queueSize: {})", offerScoringSemaphore.getConcurrentRequests(), offerScoringSemaphore.getQueueSize());
// This will be caught by either the LeaderOnlyPoller or resourceOffers uncaught exception code, causing an abort
throw new RuntimeException(scoringException.get());
}
if (!scorePerOffer.isEmpty()) {
SingularityOfferHolder bestOffer = offerHolders.get(Collections.max(scorePerOffer.entrySet(), Map.Entry.comparingByValue()).getKey());
LOG.info("Best offer {}/1 is on {}", scorePerOffer.get(bestOffer.getSlaveId()), bestOffer.getSanitizedHost());
SingularityMesosTaskHolder taskHolder = acceptTask(bestOffer, tasksPerOfferHost, taskRequestHolder);
tasksScheduled.getAndIncrement();
bestOffer.addMatchedTask(taskHolder);
updateSlaveUsageScores(taskRequestHolder, currentSlaveUsagesBySlaveId, bestOffer.getSlaveId(), requestUtilizations);
}
}, taskRequestHolder.getTaskRequest().getRequest().getId(), String.format("%s#%s", getClass().getSimpleName(), "checkOffers"));
}
LOG.info("{} tasks scheduled, {} tasks remaining after examining {} offers", tasksScheduled, numDueTasks - tasksScheduled.get(), offers.size());
return offerHolders.values();
}
use of com.hubspot.singularity.SingularityTaskId in project Singularity by HubSpot.
the class LastTaskStatusMigration method applyMigration.
@Override
public void applyMigration() {
final long start = System.currentTimeMillis();
final List<SingularityTaskId> taskIds = taskManager.getActiveTaskIds();
for (SingularityTaskId taskId : taskIds) {
List<SingularityTaskHistoryUpdate> updates = Lists.reverse(taskManager.getTaskHistoryUpdates(taskId));
Optional<MesosTaskStatusObject> taskStatus = Optional.absent();
for (SingularityTaskHistoryUpdate update : updates) {
if (update.getTaskState().toTaskState().isPresent()) {
Optional<SingularityTask> task = taskManager.getTask(taskId);
taskStatus = Optional.of(mesosProtosUtils.taskStatusFromProtos(TaskStatus.newBuilder().setTaskId(TaskID.newBuilder().setValue(taskId.getId())).setAgentId(MesosProtosUtils.toAgentId(task.get().getAgentId())).setState(MesosProtosUtils.toTaskState(update.getTaskState())).build()));
break;
}
}
SingularityTaskStatusHolder taskStatusHolder = new SingularityTaskStatusHolder(taskId, taskStatus, start, serverId, Optional.absent());
taskManager.saveLastActiveTaskStatus(taskStatusHolder);
}
}
use of com.hubspot.singularity.SingularityTaskId in project Singularity by HubSpot.
the class RequestHelper method getMostRecentTask.
public Optional<SingularityTaskIdHistory> getMostRecentTask(SingularityRequest request) {
List<SingularityTaskId> activeTaskIds = taskManager.getActiveTaskIdsForRequest(request.getId());
if (!activeTaskIds.isEmpty()) {
SingularityTaskId lastTaskId = activeTaskIds.get(0);
List<SingularityTaskHistoryUpdate> historyUpdates = taskManager.getTaskHistoryUpdates(lastTaskId);
if (!historyUpdates.isEmpty()) {
SingularityTaskHistoryUpdate lastUpdate = historyUpdates.get(historyUpdates.size() - 1);
return Optional.of(new SingularityTaskIdHistory(lastTaskId, lastUpdate.getTimestamp(), Optional.of(lastUpdate.getTaskState()), // runId not currently provided here, grabbing the full task data for this is a more expensive call
Optional.absent()));
}
}
List<SingularityTaskIdHistory> maybeRecentTasks = taskHistoryHelper.getBlendedHistory(new SingularityTaskHistoryQuery(request.getId()), 0, 1);
if (!maybeRecentTasks.isEmpty()) {
return Optional.of(maybeRecentTasks.get(0));
}
return Optional.absent();
}
use of com.hubspot.singularity.SingularityTaskId in project Singularity by HubSpot.
the class BlendedHistoryHelper method getTaskHistoriesFor.
public List<SingularityTaskIdHistory> getTaskHistoriesFor(TaskManager taskManager, Collection<SingularityTaskId> taskIds) {
Map<SingularityTaskId, SingularityTask> tasks = taskManager.getTasks(taskIds);
Map<SingularityTaskId, List<SingularityTaskHistoryUpdate>> map = taskManager.getTaskHistoryUpdates(taskIds);
List<SingularityTaskIdHistory> histories = Lists.newArrayListWithCapacity(taskIds.size());
for (SingularityTaskId taskId : taskIds) {
List<SingularityTaskHistoryUpdate> historyUpdates = map.get(taskId);
SingularityTask task = tasks.get(taskId);
if (task != null) {
histories.add(SingularityTaskIdHistory.fromTaskIdAndTaskAndUpdates(taskId, task, historyUpdates));
}
}
Collections.sort(histories);
return histories;
}
use of com.hubspot.singularity.SingularityTaskId in project Singularity by HubSpot.
the class SingularityAuthorizationHelper method checkForAuthorizationByTaskId.
public void checkForAuthorizationByTaskId(String taskId, SingularityUser user, SingularityAuthorizationScope scope) {
if (authEnabled) {
checkForbidden(user.isAuthenticated(), "Not Authenticated!");
try {
final SingularityTaskId taskIdObj = SingularityTaskId.valueOf(taskId);
final Optional<SingularityRequestWithState> maybeRequest = requestManager.getRequest(taskIdObj.getRequestId());
if (maybeRequest.isPresent()) {
checkForAuthorization(maybeRequest.get().getRequest(), user, scope);
}
} catch (InvalidSingularityTaskIdException e) {
badRequest(e.getMessage());
}
}
}
Aggregations