Search in sources :

Example 1 with CheckResult

use of com.hubspot.singularity.mesos.SingularitySlaveAndRackManager.CheckResult in project Singularity by HubSpot.

the class SingularityMesosSchedulerImpl method resourceOffers.

@Timed
@Override
public void resourceOffers(List<Offer> offers) {
    if (!isRunning()) {
        LOG.info("Scheduler is in state {}, declining {} offer(s)", state.name(), offers.size());
        mesosSchedulerClient.decline(offers.stream().map(Offer::getId).collect(Collectors.toList()));
        return;
    }
    callWithOffersLock(() -> {
        final long start = System.currentTimeMillis();
        lastOfferTimestamp = Optional.of(start);
        LOG.info("Received {} offer(s)", offers.size());
        boolean delclineImmediately = false;
        if (disasterManager.isDisabled(SingularityAction.PROCESS_OFFERS)) {
            LOG.info("Processing offers is currently disabled, declining {} offers", offers.size());
            delclineImmediately = true;
        }
        if (delayWhenStatusUpdateDeltaTooLarge && statusUpdateDeltaAvg.get() > delayWhenDeltaOverMs) {
            LOG.info("Status update delta is too large ({}), declining offers while status updates catch up", statusUpdateDeltaAvg.get());
            delclineImmediately = true;
        }
        if (delclineImmediately) {
            mesosSchedulerClient.decline(offers.stream().map(Offer::getId).collect(Collectors.toList()));
            return;
        }
        if (offerCacheEnabled) {
            if (disasterManager.isDisabled(SingularityAction.CACHE_OFFERS)) {
                offerCache.disableOfferCache();
            } else {
                offerCache.enableOfferCache();
            }
        }
        List<Offer> offersToCheck = new ArrayList<>(offers);
        List<CachedOffer> cachedOfferList = offerCache.checkoutOffers();
        Map<String, CachedOffer> cachedOffers = new HashMap<>();
        for (CachedOffer cachedOffer : cachedOfferList) {
            cachedOffers.put(cachedOffer.getOfferId(), cachedOffer);
            offersToCheck.add(cachedOffer.getOffer());
        }
        offers.parallelStream().forEach((offer) -> {
            String rolesInfo = MesosUtils.getRoles(offer).toString();
            LOG.debug("Received offer ID {} with roles {} from {} ({}) for {} cpu(s), {} memory, {} ports, and {} disk", offer.getId().getValue(), rolesInfo, offer.getHostname(), offer.getAgentId().getValue(), MesosUtils.getNumCpus(offer), MesosUtils.getMemory(offer), MesosUtils.getNumPorts(offer), MesosUtils.getDisk(offer));
            CheckResult checkResult = slaveAndRackManager.checkOffer(offer);
            if (checkResult == CheckResult.NOT_ACCEPTING_TASKS) {
                mesosSchedulerClient.decline(Collections.singletonList(offer.getId()));
                offersToCheck.remove(offer);
                LOG.debug("Will decline offer {}, slave {} is not currently in a state to launch tasks", offer.getId().getValue(), offer.getHostname());
            }
        });
        final Set<OfferID> acceptedOffers = Sets.newHashSetWithExpectedSize(offersToCheck.size());
        try {
            Collection<SingularityOfferHolder> offerHolders = offerScheduler.checkOffers(offersToCheck);
            for (SingularityOfferHolder offerHolder : offerHolders) {
                if (!offerHolder.getAcceptedTasks().isEmpty()) {
                    List<Offer> leftoverOffers = offerHolder.launchTasksAndGetUnusedOffers(mesosSchedulerClient);
                    leftoverOffers.forEach((o) -> {
                        if (cachedOffers.containsKey(o.getId().getValue())) {
                            offerCache.returnOffer(cachedOffers.remove(o.getId().getValue()));
                        } else {
                            offerCache.cacheOffer(start, o);
                        }
                    });
                    List<Offer> offersAcceptedFromSlave = offerHolder.getOffers();
                    offersAcceptedFromSlave.removeAll(leftoverOffers);
                    offersAcceptedFromSlave.stream().filter((offer) -> cachedOffers.containsKey(offer.getId().getValue())).map((o) -> cachedOffers.remove(o.getId().getValue())).forEach(offerCache::useOffer);
                    acceptedOffers.addAll(offersAcceptedFromSlave.stream().map(Offer::getId).collect(Collectors.toList()));
                } else {
                    offerHolder.getOffers().forEach((o) -> {
                        if (cachedOffers.containsKey(o.getId().getValue())) {
                            offerCache.returnOffer(cachedOffers.remove(o.getId().getValue()));
                        } else {
                            offerCache.cacheOffer(start, o);
                        }
                    });
                }
            }
            LOG.info("{} remaining offers not accounted for in offer check", cachedOffers.size());
            cachedOffers.values().forEach(offerCache::returnOffer);
        } catch (Throwable t) {
            LOG.error("Received fatal error while handling offers - will decline all available offers", t);
            mesosSchedulerClient.decline(offersToCheck.stream().filter((o) -> !acceptedOffers.contains(o.getId()) && !cachedOffers.containsKey(o.getId().getValue())).map(Offer::getId).collect(Collectors.toList()));
            offersToCheck.forEach((o) -> {
                if (cachedOffers.containsKey(o.getId().getValue())) {
                    offerCache.returnOffer(cachedOffers.get(o.getId().getValue()));
                }
            });
            throw t;
        }
        LOG.info("Finished handling {} new offer(s) ({}), {} accepted, {} declined/cached", offers.size(), JavaUtils.duration(start), acceptedOffers.size(), offers.size() - acceptedOffers.size());
    }, "resourceOffers");
}
Also used : CachedOffer(com.hubspot.singularity.mesos.SingularityOfferCache.CachedOffer) SingularityTask(com.hubspot.singularity.SingularityTask) Arrays(java.util.Arrays) Subscribed(org.apache.mesos.v1.scheduler.Protos.Event.Subscribed) Inject(com.google.inject.Inject) PrematureChannelClosureException(io.netty.handler.codec.PrematureChannelClosureException) LoggerFactory(org.slf4j.LoggerFactory) Random(java.util.Random) Offer(org.apache.mesos.v1.Protos.Offer) Optional(com.google.common.base.Optional) Map(java.util.Map) MesosProtosUtils(com.hubspot.singularity.helpers.MesosProtosUtils) CheckResult(com.hubspot.singularity.mesos.SingularitySlaveAndRackManager.CheckResult) TaskManager(com.hubspot.singularity.data.TaskManager) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) AbortReason(com.hubspot.singularity.SingularityAbort.AbortReason) Collection(java.util.Collection) SingularityKilledTaskIdRecord(com.hubspot.singularity.SingularityKilledTaskIdRecord) Failure(org.apache.mesos.v1.scheduler.Protos.Event.Failure) Set(java.util.Set) Transcoder(com.hubspot.singularity.data.transcoders.Transcoder) Protos(org.apache.mesos.v1.Protos) Collectors(java.util.stream.Collectors) ExecutorID(org.apache.mesos.v1.Protos.ExecutorID) Sets(com.google.common.collect.Sets) Timed(com.codahale.metrics.annotation.Timed) List(java.util.List) JavaUtils(com.hubspot.mesos.JavaUtils) MesosUtils(com.hubspot.singularity.helpers.MesosUtils) SingularityLeaderCacheCoordinator(com.hubspot.singularity.scheduler.SingularityLeaderCacheCoordinator) RequestCleanupType(com.hubspot.singularity.RequestCleanupType) DisasterManager(com.hubspot.singularity.data.DisasterManager) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Event(org.apache.mesos.v1.scheduler.Protos.Event) Singleton(javax.inject.Singleton) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) InverseOffer(org.apache.mesos.v1.Protos.InverseOffer) SingularityExceptionNotifier(com.hubspot.singularity.sentry.SingularityExceptionNotifier) SingularityConfiguration(com.hubspot.singularity.config.SingularityConfiguration) TaskStatus(org.apache.mesos.v1.Protos.TaskStatus) SingularityAction(com.hubspot.singularity.SingularityAction) Logger(org.slf4j.Logger) SingularityTaskDestroyFrameworkMessage(com.hubspot.singularity.SingularityTaskDestroyFrameworkMessage) AgentID(org.apache.mesos.v1.Protos.AgentID) OfferID(org.apache.mesos.v1.Protos.OfferID) AtomicLong(java.util.concurrent.atomic.AtomicLong) TaskCleanupType(com.hubspot.singularity.TaskCleanupType) MasterInfo(org.apache.mesos.v1.Protos.MasterInfo) TaskID(org.apache.mesos.v1.Protos.TaskID) SingularityAbort(com.hubspot.singularity.SingularityAbort) Message(org.apache.mesos.v1.scheduler.Protos.Event.Message) Preconditions(com.google.common.base.Preconditions) Named(com.google.inject.name.Named) Collections(java.util.Collections) MesosConfiguration(com.hubspot.singularity.config.MesosConfiguration) SingularityMainModule(com.hubspot.singularity.SingularityMainModule) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) OfferID(org.apache.mesos.v1.Protos.OfferID) CachedOffer(com.hubspot.singularity.mesos.SingularityOfferCache.CachedOffer) Offer(org.apache.mesos.v1.Protos.Offer) InverseOffer(org.apache.mesos.v1.Protos.InverseOffer) CheckResult(com.hubspot.singularity.mesos.SingularitySlaveAndRackManager.CheckResult) CachedOffer(com.hubspot.singularity.mesos.SingularityOfferCache.CachedOffer) Timed(com.codahale.metrics.annotation.Timed)

Aggregations

Timed (com.codahale.metrics.annotation.Timed)1 Optional (com.google.common.base.Optional)1 Preconditions (com.google.common.base.Preconditions)1 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1 Inject (com.google.inject.Inject)1 Named (com.google.inject.name.Named)1 JavaUtils (com.hubspot.mesos.JavaUtils)1 RequestCleanupType (com.hubspot.singularity.RequestCleanupType)1 SingularityAbort (com.hubspot.singularity.SingularityAbort)1 AbortReason (com.hubspot.singularity.SingularityAbort.AbortReason)1 SingularityAction (com.hubspot.singularity.SingularityAction)1 SingularityKilledTaskIdRecord (com.hubspot.singularity.SingularityKilledTaskIdRecord)1 SingularityMainModule (com.hubspot.singularity.SingularityMainModule)1 SingularityTask (com.hubspot.singularity.SingularityTask)1 SingularityTaskDestroyFrameworkMessage (com.hubspot.singularity.SingularityTaskDestroyFrameworkMessage)1 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)1 TaskCleanupType (com.hubspot.singularity.TaskCleanupType)1 MesosConfiguration (com.hubspot.singularity.config.MesosConfiguration)1 SingularityConfiguration (com.hubspot.singularity.config.SingularityConfiguration)1