Search in sources :

Example 1 with SingularityOfferHolder

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

the class SingularitySchedulerPoller method runActionOnPoll.

@Override
public void runActionOnPoll() {
    if (disasterManager.isDisabled(SingularityAction.RUN_SCHEDULER_POLLER)) {
        LOG.warn("Scheduler poller is disabled");
        return;
    }
    lock.runWithOffersLock(() -> {
        List<CachedOffer> cachedOffers = offerCache.checkoutOffers();
        Map<String, CachedOffer> offerIdToCachedOffer = new HashMap<>(cachedOffers.size());
        List<Offer> offers = new ArrayList<>(cachedOffers.size());
        for (CachedOffer cachedOffer : cachedOffers) {
            offerIdToCachedOffer.put(cachedOffer.getOfferId(), cachedOffer);
            offers.add(cachedOffer.getOffer());
        }
        Collection<SingularityOfferHolder> offerHolders = offerScheduler.checkOffers(offers);
        if (offerHolders.isEmpty()) {
            return;
        }
        int acceptedOffers = 0;
        int launchedTasks = 0;
        for (SingularityOfferHolder offerHolder : offerHolders) {
            List<CachedOffer> cachedOffersFromHolder = offerHolder.getOffers().stream().map((o) -> offerIdToCachedOffer.get(o.getId().getValue())).collect(Collectors.toList());
            if (!offerHolder.getAcceptedTasks().isEmpty()) {
                List<Offer> unusedOffers = offerHolder.launchTasksAndGetUnusedOffers(schedulerClient);
                launchedTasks += offerHolder.getAcceptedTasks().size();
                acceptedOffers += cachedOffersFromHolder.size() - unusedOffers.size();
                // Return to the cache those offers which we checked out of the cache, but didn't end up using.
                List<CachedOffer> unusedCachedOffers = unusedOffers.stream().map((o) -> offerIdToCachedOffer.get(o.getId().getValue())).collect(Collectors.toList());
                unusedCachedOffers.forEach((cachedOffer) -> {
                    offerIdToCachedOffer.remove(cachedOffer.getOfferId());
                    offerCache.returnOffer(cachedOffer);
                });
                // Notify the cache of the cached offers that we did use.
                cachedOffersFromHolder.removeAll(unusedCachedOffers);
                cachedOffersFromHolder.forEach((cachedOffer) -> {
                    offerIdToCachedOffer.remove(cachedOffer.getOfferId());
                    offerCache.useOffer(cachedOffer);
                });
            } else {
                cachedOffersFromHolder.forEach((cachedOffer) -> {
                    offerIdToCachedOffer.remove(cachedOffer.getOfferId());
                    offerCache.returnOffer(cachedOffer);
                });
            }
        }
        LOG.info("{} remaining offers not accounted for in offer check", offerIdToCachedOffer.size());
        offerIdToCachedOffer.values().forEach(offerCache::returnOffer);
        LOG.info("Launched {} tasks on {} cached offers (returned {})", launchedTasks, acceptedOffers, offerHolders.size() - acceptedOffers);
    }, getClass().getSimpleName());
}
Also used : SingularityOfferHolder(com.hubspot.singularity.mesos.SingularityOfferHolder) SingularityAction(com.hubspot.singularity.SingularityAction) DisasterManager(com.hubspot.singularity.data.DisasterManager) CachedOffer(com.hubspot.singularity.mesos.SingularityOfferCache.CachedOffer) SingularityMesosOfferScheduler(com.hubspot.singularity.mesos.SingularityMesosOfferScheduler) Logger(org.slf4j.Logger) Collection(java.util.Collection) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Offer(org.apache.mesos.v1.Protos.Offer) Singleton(javax.inject.Singleton) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) OfferCache(com.hubspot.singularity.mesos.OfferCache) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) SingularityMesosSchedulerClient(com.hubspot.singularity.mesos.SingularityMesosSchedulerClient) Map(java.util.Map) SingularityOfferHolder(com.hubspot.singularity.mesos.SingularityOfferHolder) SingularitySchedulerLock(com.hubspot.singularity.mesos.SingularitySchedulerLock) SingularityConfiguration(com.hubspot.singularity.config.SingularityConfiguration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CachedOffer(com.hubspot.singularity.mesos.SingularityOfferCache.CachedOffer) Offer(org.apache.mesos.v1.Protos.Offer) CachedOffer(com.hubspot.singularity.mesos.SingularityOfferCache.CachedOffer)

Aggregations

Inject (com.google.inject.Inject)1 SingularityAction (com.hubspot.singularity.SingularityAction)1 SingularityConfiguration (com.hubspot.singularity.config.SingularityConfiguration)1 DisasterManager (com.hubspot.singularity.data.DisasterManager)1 OfferCache (com.hubspot.singularity.mesos.OfferCache)1 SingularityMesosOfferScheduler (com.hubspot.singularity.mesos.SingularityMesosOfferScheduler)1 SingularityMesosSchedulerClient (com.hubspot.singularity.mesos.SingularityMesosSchedulerClient)1 CachedOffer (com.hubspot.singularity.mesos.SingularityOfferCache.CachedOffer)1 SingularityOfferHolder (com.hubspot.singularity.mesos.SingularityOfferHolder)1 SingularitySchedulerLock (com.hubspot.singularity.mesos.SingularitySchedulerLock)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 Singleton (javax.inject.Singleton)1 Offer (org.apache.mesos.v1.Protos.Offer)1 Logger (org.slf4j.Logger)1