Search in sources :

Example 1 with SingularityKilledTaskIdRecord

use of com.hubspot.singularity.SingularityKilledTaskIdRecord in project Singularity by HubSpot.

the class SingularityCleaner method checkKilledTaskIdRecords.

private void checkKilledTaskIdRecords() {
    final long start = System.currentTimeMillis();
    final List<SingularityKilledTaskIdRecord> killedTaskIdRecords = taskManager.getKilledTaskIdRecords();
    if (killedTaskIdRecords.isEmpty()) {
        LOG.trace("No killed taskId records");
        return;
    }
    AtomicInteger obsolete = new AtomicInteger(0);
    AtomicInteger waiting = new AtomicInteger(0);
    AtomicInteger rekilled = new AtomicInteger(0);
    CompletableFutures.allOf(killedTaskIdRecords.stream().collect(Collectors.groupingBy(record -> record.getTaskId().getRequestId())).entrySet().stream().map(killedTaskIdRecordsForRequest -> CompletableFuture.runAsync(() -> lock.runWithRequestLock(() -> {
        for (SingularityKilledTaskIdRecord killedTaskIdRecord : killedTaskIdRecordsForRequest.getValue()) {
            if (!taskManager.isActiveTask(killedTaskIdRecord.getTaskId())) {
                SingularityDeleteResult deleteResult = taskManager.deleteKilledRecord(killedTaskIdRecord.getTaskId());
                LOG.debug("Deleting obsolete {} - {}", killedTaskIdRecord, deleteResult);
                obsolete.getAndIncrement();
                continue;
            }
            long duration = start - killedTaskIdRecord.getTimestamp();
            if (duration > configuration.getAskDriverToKillTasksAgainAfterMillis()) {
                LOG.info("{} is still active, and time since last kill {} is greater than configured (askDriverToKillTasksAgainAfterMillis) {} - asking driver to kill again", killedTaskIdRecord, JavaUtils.durationFromMillis(duration), JavaUtils.durationFromMillis(configuration.getAskDriverToKillTasksAgainAfterMillis()));
                scheduler.killAndRecord(killedTaskIdRecord.getTaskId(), killedTaskIdRecord.getRequestCleanupType(), killedTaskIdRecord.getTaskCleanupType(), Optional.of(killedTaskIdRecord.getOriginalTimestamp()), Optional.of(killedTaskIdRecord.getRetries()), Optional.empty());
                rekilled.getAndIncrement();
            } else {
                LOG.trace("Ignoring {}, because duration {} is less than configured (askDriverToKillTasksAgainAfterMillis) {}", killedTaskIdRecord, JavaUtils.durationFromMillis(duration), JavaUtils.durationFromMillis(configuration.getAskDriverToKillTasksAgainAfterMillis()));
                waiting.getAndIncrement();
            }
        }
    }, killedTaskIdRecordsForRequest.getKey(), String.format("%s#%s", getClass().getSimpleName(), "checkKilledTaskIdRecords")), cleanerExecutor)).collect(Collectors.toList())).join();
    LOG.info("{} obsolete, {} waiting, {} rekilled tasks based on {} killedTaskIdRecords", obsolete, waiting, rekilled, killedTaskIdRecords.size());
}
Also used : SingularityTask(com.hubspot.singularity.SingularityTask) DeployManager(com.hubspot.singularity.data.DeployManager) SingularityLoadBalancerUpdate(com.hubspot.singularity.SingularityLoadBalancerUpdate) Inject(com.google.inject.Inject) SingularityRequestCleanup(com.hubspot.singularity.SingularityRequestCleanup) RequestManager(com.hubspot.singularity.data.RequestManager) LoggerFactory(org.slf4j.LoggerFactory) SingularityRequestLbCleanup(com.hubspot.singularity.SingularityRequestLbCleanup) SingularityTaskShellCommandRequestId(com.hubspot.singularity.SingularityTaskShellCommandRequestId) SingularityExpiringBounce(com.hubspot.singularity.expiring.SingularityExpiringBounce) HashMultiset(com.google.common.collect.HashMultiset) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) TaskManager(com.hubspot.singularity.data.TaskManager) SingularityPendingTaskId(com.hubspot.singularity.SingularityPendingTaskId) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) ImmutableMap(com.google.common.collect.ImmutableMap) SingularityKilledTaskIdRecord(com.hubspot.singularity.SingularityKilledTaskIdRecord) CompletableFutures(com.hubspot.singularity.async.CompletableFutures) Set(java.util.Set) Collectors(java.util.stream.Collectors) RequestState(com.hubspot.singularity.RequestState) PendingType(com.hubspot.singularity.SingularityPendingRequest.PendingType) DeployHealth(com.hubspot.singularity.scheduler.SingularityDeployHealthHelper.DeployHealth) JavaUtils(com.hubspot.mesos.JavaUtils) List(java.util.List) SingularityTaskShellCommandRequest(com.hubspot.singularity.SingularityTaskShellCommandRequest) SingularityTaskShellCommandUpdate(com.hubspot.singularity.SingularityTaskShellCommandUpdate) LoadBalancerClient(com.hubspot.singularity.hooks.LoadBalancerClient) UsageManager(com.hubspot.singularity.data.usage.UsageManager) SingularitySchedulerLock(com.hubspot.singularity.mesos.SingularitySchedulerLock) Optional(java.util.Optional) RequestCleanupType(com.hubspot.singularity.RequestCleanupType) SingularityMesosScheduler(com.hubspot.singularity.mesos.SingularityMesosScheduler) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityRequest(com.hubspot.singularity.SingularityRequest) Iterables(com.google.common.collect.Iterables) SingularityRequestHistory(com.hubspot.singularity.SingularityRequestHistory) Multiset(com.google.common.collect.Multiset) SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) CompletableFuture(java.util.concurrent.CompletableFuture) Singleton(javax.inject.Singleton) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LoadBalancerRequestId(com.hubspot.singularity.LoadBalancerRequestType.LoadBalancerRequestId) SingularityDeleteResult(com.hubspot.singularity.SingularityDeleteResult) SingularityDeployKey(com.hubspot.singularity.SingularityDeployKey) LoadBalancerRequestState(com.hubspot.singularity.LoadBalancerRequestState) SingularityExceptionNotifier(com.hubspot.singularity.sentry.SingularityExceptionNotifier) ExecutorService(java.util.concurrent.ExecutorService) SingularityConfiguration(com.hubspot.singularity.config.SingularityConfiguration) SingularityTaskCleanup(com.hubspot.singularity.SingularityTaskCleanup) Logger(org.slf4j.Logger) SingularityDeploy(com.hubspot.singularity.SingularityDeploy) RequestGroupManager(com.hubspot.singularity.data.RequestGroupManager) TimeUnit(java.util.concurrent.TimeUnit) SingularityRequestDeployState(com.hubspot.singularity.SingularityRequestDeployState) TaskCleanupType(com.hubspot.singularity.TaskCleanupType) LoadBalancerRequestType(com.hubspot.singularity.LoadBalancerRequestType) SingularityManagedThreadPoolFactory(com.hubspot.singularity.SingularityManagedThreadPoolFactory) RequestHistoryHelper(com.hubspot.singularity.data.history.RequestHistoryHelper) Collections(java.util.Collections) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SingularityKilledTaskIdRecord(com.hubspot.singularity.SingularityKilledTaskIdRecord) SingularityDeleteResult(com.hubspot.singularity.SingularityDeleteResult)

Example 2 with SingularityKilledTaskIdRecord

use of com.hubspot.singularity.SingularityKilledTaskIdRecord in project Singularity by HubSpot.

the class SingularityMesosSchedulerImpl method killAndRecord.

public void killAndRecord(SingularityTaskId taskId, Optional<RequestCleanupType> requestCleanupType, Optional<TaskCleanupType> taskCleanupType, Optional<Long> originalTimestamp, Optional<Integer> retries, Optional<String> user) {
    Preconditions.checkState(isRunning());
    Optional<TaskCleanupType> maybeCleanupFromRequestAndTask = getTaskCleanupType(requestCleanupType, taskCleanupType);
    if (maybeCleanupFromRequestAndTask.isPresent() && (maybeCleanupFromRequestAndTask.get() == TaskCleanupType.USER_REQUESTED_DESTROY || maybeCleanupFromRequestAndTask.get() == TaskCleanupType.REQUEST_DELETING)) {
        Optional<SingularityTask> task = taskManager.getTask(taskId);
        if (task.isPresent()) {
            if (task.get().getTaskRequest().getDeploy().getCustomExecutorCmd().isPresent()) {
                byte[] messageBytes = transcoder.toBytes(new SingularityTaskDestroyFrameworkMessage(taskId, user));
                mesosSchedulerClient.frameworkMessage(MesosProtosUtils.toExecutorId(task.get().getMesosTask().getExecutor().getExecutorId()), MesosProtosUtils.toAgentId(task.get().getMesosTask().getAgentId()), messageBytes);
            } else {
                LOG.warn("Not using custom executor, will not send framework message to destroy task");
            }
        } else {
            String message = String.format("No task data available to build kill task framework message for task %s", taskId);
            exceptionNotifier.notify(message);
            LOG.error(message);
        }
    }
    mesosSchedulerClient.kill(TaskID.newBuilder().setValue(taskId.toString()).build());
    taskManager.saveKilledRecord(new SingularityKilledTaskIdRecord(taskId, System.currentTimeMillis(), originalTimestamp.orElse(System.currentTimeMillis()), requestCleanupType, taskCleanupType, retries.orElse(-1) + 1));
}
Also used : SingularityTask(com.hubspot.singularity.SingularityTask) SingularityKilledTaskIdRecord(com.hubspot.singularity.SingularityKilledTaskIdRecord) TaskCleanupType(com.hubspot.singularity.TaskCleanupType) SingularityTaskDestroyFrameworkMessage(com.hubspot.singularity.SingularityTaskDestroyFrameworkMessage)

Example 3 with SingularityKilledTaskIdRecord

use of com.hubspot.singularity.SingularityKilledTaskIdRecord in project Singularity by HubSpot.

the class SingularitySchedulerTest method testTaskLaunchesInRackSensitiveWithKillingTask.

@Test
public void testTaskLaunchesInRackSensitiveWithKillingTask() {
    try {
        configuration.setExpectedRacksCount(Optional.of(3));
        // Set up hosts + racks
        sms.resourceOffers(Arrays.asList(createOffer(1, 1, 1, "agent1", "host1", Optional.of("rack1")), createOffer(1, 1, 1, "agent2", "host2", Optional.of("rack2")), createOffer(1, 1, 1, "agent3", "host3", Optional.of("rack3")))).join();
        initRequest();
        initFirstDeploy();
        requestResource.postRequest(request.toBuilder().setInstances(Optional.of(3)).setRackSensitive(Optional.of(true)).setAgentPlacement(Optional.of(AgentPlacement.SEPARATE)).build(), SingularityUser.DEFAULT_USER);
        schedulerPoller.runActionOnPoll();
        Assertions.assertEquals(3, taskManager.getPendingTaskIds().size());
        sms.resourceOffers(Arrays.asList(createOffer(2, 2000, 2000, "agent1", "host1", Optional.of("rack1")), createOffer(2, 2000, 2000, "agent2", "host2", Optional.of("rack2")), createOffer(2, 2000, 2000, "agent3", "host3", Optional.of("rack3")))).join();
        Assertions.assertEquals(3, taskManager.getActiveTaskIds().size());
        SingularityTaskId taskTwo = taskManager.getActiveTaskIds().stream().filter(t -> t.getInstanceNo() == 2).findFirst().get();
        SingularityTaskId taskThree = taskManager.getActiveTaskIds().stream().filter(t -> t.getInstanceNo() == 3).findFirst().get();
        requestResource.postRequest(request.toBuilder().setInstances(Optional.of(2)).setRackSensitive(Optional.of(true)).setAgentPlacement(Optional.of(AgentPlacement.SEPARATE)).build(), SingularityUser.DEFAULT_USER);
        taskManager.createTaskCleanup(new SingularityTaskCleanup(Optional.empty(), TaskCleanupType.USER_REQUESTED, System.currentTimeMillis(), taskTwo, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()));
        taskManager.createTaskCleanup(new SingularityTaskCleanup(Optional.empty(), TaskCleanupType.USER_REQUESTED, System.currentTimeMillis(), taskThree, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()));
        cleaner.drainCleanupQueue();
        schedulerPoller.runActionOnPoll();
        // Mimic tasks that have been killed (and their cleanup removed) but are slow to shut down
        taskManager.deleteCleanupTask(taskTwo.toString());
        taskManager.deleteCleanupTask(taskThree.toString());
        taskManager.saveKilledRecord(new SingularityKilledTaskIdRecord(taskTwo, System.currentTimeMillis(), System.currentTimeMillis(), Optional.empty(), Optional.empty(), 1));
        taskManager.saveKilledRecord(new SingularityKilledTaskIdRecord(taskTwo, System.currentTimeMillis(), System.currentTimeMillis(), Optional.empty(), Optional.empty(), 1));
        Assertions.assertEquals(3, taskManager.getActiveTaskIds().size());
        Assertions.assertEquals(1, taskManager.getPendingTaskIds().size());
        sms.resourceOffers(Arrays.asList(createOffer(2, 2000, 2000, "agent4", "host4", Optional.of("rack1")), createOffer(2, 2000, 2000, "agent5", "host5", Optional.of("rack2")), createOffer(2, 2000, 2000, "agent6", "host6", Optional.of("rack3")))).join();
        Assertions.assertEquals(4, taskManager.getActiveTaskIds().size());
    } finally {
        configuration.setExpectedRacksCount(Optional.empty());
    }
}
Also used : SingularityKilledTaskIdRecord(com.hubspot.singularity.SingularityKilledTaskIdRecord) SingularityTaskCleanup(com.hubspot.singularity.SingularityTaskCleanup) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) Test(org.junit.jupiter.api.Test)

Example 4 with SingularityKilledTaskIdRecord

use of com.hubspot.singularity.SingularityKilledTaskIdRecord in project Singularity by HubSpot.

the class SingularitySchedulerTest method testScaleDownTakesHighestInstances.

@Test
public void testScaleDownTakesHighestInstances() {
    initRequest();
    initFirstDeploy();
    saveAndSchedule(request.toBuilder().setInstances(Optional.of(5)));
    resourceOffers();
    Assertions.assertEquals(5, taskManager.getActiveTaskIds().size());
    requestResource.scale(requestId, new SingularityScaleRequest(Optional.of(2), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()), singularityUser);
    resourceOffers();
    cleaner.drainCleanupQueue();
    Assertions.assertEquals(3, taskManager.getKilledTaskIdRecords().size());
    for (SingularityKilledTaskIdRecord taskId : taskManager.getKilledTaskIdRecords()) {
        Assertions.assertTrue(taskId.getTaskId().getInstanceNo() > 2);
        scheduler.drainPendingQueue();
    }
}
Also used : SingularityKilledTaskIdRecord(com.hubspot.singularity.SingularityKilledTaskIdRecord) SingularityScaleRequest(com.hubspot.singularity.api.SingularityScaleRequest) Test(org.junit.jupiter.api.Test)

Aggregations

SingularityKilledTaskIdRecord (com.hubspot.singularity.SingularityKilledTaskIdRecord)4 SingularityTask (com.hubspot.singularity.SingularityTask)2 SingularityTaskCleanup (com.hubspot.singularity.SingularityTaskCleanup)2 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)2 TaskCleanupType (com.hubspot.singularity.TaskCleanupType)2 HashMultiset (com.google.common.collect.HashMultiset)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Iterables (com.google.common.collect.Iterables)1 Multiset (com.google.common.collect.Multiset)1 Inject (com.google.inject.Inject)1 JavaUtils (com.hubspot.mesos.JavaUtils)1 LoadBalancerRequestState (com.hubspot.singularity.LoadBalancerRequestState)1 LoadBalancerRequestType (com.hubspot.singularity.LoadBalancerRequestType)1 LoadBalancerRequestId (com.hubspot.singularity.LoadBalancerRequestType.LoadBalancerRequestId)1 RequestCleanupType (com.hubspot.singularity.RequestCleanupType)1 RequestState (com.hubspot.singularity.RequestState)1 SingularityDeleteResult (com.hubspot.singularity.SingularityDeleteResult)1 SingularityDeploy (com.hubspot.singularity.SingularityDeploy)1 SingularityDeployKey (com.hubspot.singularity.SingularityDeployKey)1 SingularityLoadBalancerUpdate (com.hubspot.singularity.SingularityLoadBalancerUpdate)1