All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hubspot.singularity.mesos.SingularityOfferHolder Maven / Gradle / Ivy

package com.hubspot.singularity.mesos;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.mesos.Protos;
import org.apache.mesos.Protos.Resource;
import org.apache.mesos.Protos.Status;
import org.apache.mesos.Protos.TaskInfo;
import org.apache.mesos.SchedulerDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.hubspot.mesos.JavaUtils;
import com.hubspot.mesos.MesosUtils;
import com.hubspot.singularity.SingularityPendingTaskId;
import com.hubspot.singularity.SingularityTask;
import com.hubspot.singularity.SingularityTaskId;

public class SingularityOfferHolder {

  private static final Logger LOG = LoggerFactory.getLogger(SingularityMesosScheduler.class);

  private final Protos.Offer offer;
  private final List acceptedTasks;
  private final Set rejectedPendingTaskIds;
  private List currentResources;

  private final String rackId;
  private final String sanitizedHost;
  private final String sanitizedRackId;

  private final Map textAttributes;
  private final Map reservedSlaveAttributes;

  public SingularityOfferHolder(Protos.Offer offer, int taskSizeHint, String rackId, Map textAttributes, Map reservedSlaveAttributes) {
    this.rackId = rackId;
    this.offer = offer;
    this.acceptedTasks = Lists.newArrayListWithCapacity(taskSizeHint);
    this.currentResources = offer.getResourcesList();
    this.rejectedPendingTaskIds = new HashSet<>();
    this.sanitizedHost = JavaUtils.getReplaceHyphensWithUnderscores(offer.getHostname());
    this.sanitizedRackId = JavaUtils.getReplaceHyphensWithUnderscores(rackId);
    this.textAttributes = textAttributes;
    this.reservedSlaveAttributes = reservedSlaveAttributes;
  }

  public Map getTextAttributes() {
    return textAttributes;
  }

  public String getRackId() {
    return rackId;
  }

  public boolean hasReservedSlaveAttributes() {
    return !reservedSlaveAttributes.isEmpty();
  }

  public Map getReservedSlaveAttributes() {
    return reservedSlaveAttributes;
  }

  public String getSanitizedHost() {
    return sanitizedHost;
  }

  public String getSanitizedRackId() {
    return sanitizedRackId;
  }

  public void addRejectedTask(SingularityPendingTaskId pendingTaskId) {
    rejectedPendingTaskIds.add(pendingTaskId);
  }

  public boolean hasRejectedPendingTaskAlready(SingularityPendingTaskId pendingTaskId) {
    return rejectedPendingTaskIds.contains(pendingTaskId);
  }

  public void addMatchedTask(SingularityTask task) {
    acceptedTasks.add(task);

    // subtract task resources from offer
    currentResources = MesosUtils.subtractResources(currentResources, task.getMesosTask().getResourcesList());

    // subtract executor resources from offer, if any are defined
    if (task.getMesosTask().hasExecutor() && task.getMesosTask().getExecutor().getResourcesCount() > 0) {
      currentResources = MesosUtils.subtractResources(currentResources, task.getMesosTask().getExecutor().getResourcesList());
    }
  }

  public void launchTasks(SchedulerDriver driver) {
    final List toLaunch = Lists.newArrayListWithCapacity(acceptedTasks.size());
    final List taskIds = Lists.newArrayListWithCapacity(acceptedTasks.size());

    for (SingularityTask task : acceptedTasks) {
      taskIds.add(task.getTaskId());
      toLaunch.add(task.getMesosTask());
      LOG.debug("Launching {} with offer {}", task.getTaskId(), offer.getId());
      LOG.trace("Launching {} mesos task: {}", task.getTaskId(), MesosUtils.formatForLogging(task.getMesosTask()));
    }

    Status initialStatus = driver.launchTasks(ImmutableList.of(offer.getId()), toLaunch);

    LOG.info("{} tasks ({}) launched with status {}", taskIds.size(), taskIds, initialStatus);
  }

  public List getAcceptedTasks() {
    return acceptedTasks;
  }

  public List getCurrentResources() {
    return currentResources;
  }

  public Protos.Offer getOffer() {
    return offer;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy