Java Code Examples for org.apache.mesos.Protos#Offer

The following examples show how to use org.apache.mesos.Protos#Offer . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MesosNimbus.java    From storm with Apache License 2.0 6 votes vote down vote up
public void resourceOffers(SchedulerDriver driver, List<Protos.Offer> offers) {
  synchronized (_offersLock) {
    if (_offers == null) {
      return;
    }
    LOG.debug("resourceOffers: Currently have {} offers buffered {}",
              _offers.size(), (_offers.size() > 0 ? (":" + offerMapToString(_offers)) : ""));

    for (Protos.Offer offer : offers) {
      if (isHostAccepted(offer.getHostname())) {
        // TODO(ksoundararaj): Should we record the following as info instead of debug
        LOG.info("resourceOffers: Recording offer: {}", offerToString(offer));
        _offers.put(offer.getId(), offer);
      } else {
        LOG.info("resourceOffers: Declining offer: {}", offerToString(offer));
        driver.declineOffer(offer.getId());
      }
    }
    LOG.info("resourceOffers: After processing offers, now have {} offers buffered: {}",
              _offers.size(), offerMapToString(_offers));
  }
}
 
Example 2
Source File: CassandraSchedulerTest.java    From dcos-cassandra-service with Apache License 2.0 6 votes vote down vote up
private void runReconcile(QueuedSchedulerDriver driver) {
    while (!scheduler.getReconciler().isReconciled()) {

        final Protos.Offer offer = TestUtils.generateOffer(frameworkId.getValue(), 4, 10240, 10240);
        scheduler.resourceOffers(driver, Arrays.asList(offer));
        final Collection<Protos.TaskStatus> taskStatuses = driver.drainReconciling();
        if (taskStatuses.isEmpty()) {
            // All reconciled
            cassandraState.getTaskStatuses().forEach(status -> scheduler.statusUpdate(driver, status));
        } else {
            taskStatuses.forEach(status -> scheduler.statusUpdate(driver, status));
        }

        if (!scheduler.getReconciler().isReconciled()) {
            final Collection<Protos.OfferID> declined = driver.drainDeclined();
            assertEquals(1, declined.size());
            assertEquals(declined.iterator().next(), offer.getId());
        }
    }
}
 
Example 3
Source File: LaunchOfferRecommendation.java    From dcos-commons with Apache License 2.0 6 votes vote down vote up
public LaunchOfferRecommendation(
    Protos.Offer offer,
    Protos.TaskInfo taskInfo,
    Protos.ExecutorInfo executorInfo)
{
  this.offer = offer;

  Protos.Offer.Operation.Builder builder = Protos.Offer.Operation.newBuilder();
  // For the LAUNCH_GROUP command, we put the ExecutorInfo in the operation, not the task itself.
  builder.setType(Protos.Offer.Operation.Type.LAUNCH_GROUP)
      .getLaunchGroupBuilder()
      .setExecutor(executorInfo)
      .getTaskGroupBuilder()
      .addTasks(taskInfo);
  this.operation = builder.build();
}
 
Example 4
Source File: UninstallScheduler.java    From dcos-commons with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the resources which are not expected by this service. When uninstalling, all resources are unexpected.
 * The {@link UninstallScheduler} just keeps track of them on its 'checklist' as they are removed.
 */
@Override
public UnexpectedResourcesResponse getUnexpectedResources(Collection<Protos.Offer> unusedOffers) {

  Collection<OfferResources> unexpected = unusedOffers.stream()
      .map(offer -> new OfferResources(offer).addAll(offer.getResourcesList().stream()
          // Omit any unreserved resources, and any unrefined pre-reserved resources.
          // In addition, checking for a valid resource_id label is a good sanity check to avoid
          // potentially unreserving any resources that weren't originally created by the SDK.
          // This is in addition to separate filtering in FrameworkScheduler of reserved Marathon volumes.
          .filter(resource -> ResourceUtils.hasResourceId(resource))
          .collect(Collectors.toList())))
      .collect(Collectors.toList());
  try {
    recorder.recordCleanupOrUninstall(unexpected);
  } catch (Exception e) {
    // Failed to record the upcoming dereservation. Don't return the resources as unexpected until we can record
    // the dereservation.
    logger.error("Failed to record unexpected resources", e);
    return UnexpectedResourcesResponse.failed(Collections.emptyList());
  }
  return UnexpectedResourcesResponse.processed(unexpected);
}
 
Example 5
Source File: OfferProcessorTest.java    From dcos-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testAcceptedAndUnexpectedResources() throws InterruptedException {
    List<Protos.Offer> sentOffers = Arrays.asList(getOffer(), getOffer(), getOffer());
    List<Protos.OfferID> sentOfferIds = sentOffers.stream().map(o -> o.getId()).collect(Collectors.toList());

    Protos.OfferID offerToConsume = sentOfferIds.get(0);
    Protos.OfferID offerToUnreserve = sentOfferIds.get(2);

    when(mockMesosEventClient.offers(any())).thenAnswer(consumeOffer(offerToConsume));
    when(mockMesosEventClient.getUnexpectedResources(any())).thenAnswer(unexpectedOffer(offerToUnreserve));

    processor.setOfferQueueSize(0).start(); // unlimited queue size
    processor.enqueue(sentOffers);
    processor.awaitOffersProcessed();

    // One declined offer, one reserved offer, one unreserved offer:
    verify(mockSchedulerDriver, times(1)).declineOffer(sentOfferIds.get(1), LONG_INTERVAL);
    verify(mockSchedulerDriver, times(1)).acceptOffers(offerIdCaptor.capture(), operationCaptor.capture(), any());

    Assert.assertEquals(new HashSet<>(Arrays.asList(offerToConsume, offerToUnreserve)), offerIdCaptor.getValue());

    List<Protos.Offer.Operation> operations = operationCaptor.getValue();
    Assert.assertEquals(2, operations.size());
    Assert.assertEquals(Protos.Offer.Operation.Type.RESERVE, operations.get(0).getType());
    Assert.assertEquals(Protos.Offer.Operation.Type.UNRESERVE, operations.get(1).getType());
}
 
Example 6
Source File: CassandraSchedulerTest.java    From dcos-cassandra-service with Apache License 2.0 5 votes vote down vote up
@Test
public void installAndRecover() throws Exception {
    install();

    Collection<? extends Step> incompleteSteps = getIncompleteSteps();
    assertTrue(incompleteSteps.isEmpty());

    final CassandraDaemonTask task = cassandraState.getDaemons().get("node-0");
    scheduler.statusUpdate(driver,
            TestUtils.generateStatus(task.getTaskInfo().getTaskId(), Protos.TaskState.TASK_KILLED));
    Set<Protos.TaskStatus> taskStatuses = cassandraState.getTaskStatuses();
    final Optional<Protos.TaskStatus> first = taskStatuses.stream().filter(status -> status.getTaskId().equals(task.getTaskInfo().getTaskId())).findFirst();

    assertEquals(Protos.TaskState.TASK_KILLED, first.get().getState());

    final CassandraTask templateTask = cassandraState.get("node-0-task-template").get();
    final Protos.Offer offer = TestUtils.generateReplacementOffer(frameworkId.getValue(),
            task.getTaskInfo(), templateTask.getTaskInfo());
    scheduler.resourceOffers(driver, Arrays.asList(offer));
    Collection<QueuedSchedulerDriver.OfferOperations> offerOps = driver.drainAccepted();
    assertEquals(String.format("expected accepted offer: %s", offer), 1, offerOps.size());
    Collection<Protos.Offer.Operation> ops = offerOps.iterator().next().getOperations();
    launchAll(ops, scheduler, driver);
    taskStatuses = cassandraState.getTaskStatuses();
    final Optional<Protos.TaskStatus> node0Status = taskStatuses.stream().filter(status -> {
        try {
            return org.apache.mesos.offer.TaskUtils.toTaskName(status.getTaskId()).equals(task.getTaskInfo().getName());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }).findFirst();

    assertEquals(Protos.TaskState.TASK_RUNNING, node0Status.get().getState());
}
 
Example 7
Source File: OfferEvaluatorTest.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecated")
@Test
public void testResourceRefinementFailsForDifferentPreReservation() throws Exception {
    ResourceRefinementCapabilityContext context = new ResourceRefinementCapabilityContext(Capabilities.getInstance());
    try {
        ServiceSpec serviceSpec = getServiceSpec("resource-refinement.yml");
        Assert.assertEquals(TestConstants.PRE_RESERVED_ROLE, serviceSpec.getPods().get(0).getPreReservedRole());

        Protos.Offer badOffer = OfferTestUtils.getOffer(
                Arrays.asList(
                        ResourceTestUtils.getUnreservedCpus(3.0).toBuilder()
                                .setRole(Constants.ANY_ROLE)
                                .addReservations(
                                        Protos.Resource.ReservationInfo.newBuilder()
                                                .setRole("different-role")
                                                .setType(Protos.Resource.ReservationInfo.Type.STATIC))
                                .build()));

        PodSpec podSpec = serviceSpec.getPods().get(0);
        PodInstance podInstance = new DefaultPodInstance(podSpec, 0);
        List<String> tasksToLaunch = Arrays.asList("test-task");
        PodInstanceRequirement podInstanceRequirement = PodInstanceRequirement.newBuilder(podInstance, tasksToLaunch)
                .build();

        List<OfferRecommendation> recommendations = evaluator.evaluate(
                podInstanceRequirement,
                Arrays.asList(badOffer));

        Assert.assertEquals(0, recommendations.size());
    } finally {
        context.reset();
    }
}
 
Example 8
Source File: OfferTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private static Protos.Offer offer(List<Protos.Resource> resources, List<Protos.Attribute> attributes) {
	return Protos.Offer.newBuilder()
		.setId(OFFER_ID)
		.setFrameworkId(FRAMEWORK_ID)
		.setHostname(HOSTNAME)
		.setSlaveId(AGENT_ID)
		.addAllAttributes(attributes)
		.addAllResources(resources)
		.build();
}
 
Example 9
Source File: AbstractCassandraSchedulerTest.java    From cassandra-mesos-deprecated with Apache License 2.0 5 votes vote down vote up
protected void killTask(final Tuple2<Protos.SlaveID, String> slave, final String taskID) {
    final Protos.Offer offer = createOffer(slave);

    scheduler.resourceOffers(driver, Collections.singletonList(offer));

    assertThat(driver.declinedOffers())
        .hasSize(1);
    assertTrue(driver.launchTasks()._2.isEmpty());
    assertTrue(driver.submitTasks().isEmpty());
    assertThat(driver.killTasks())
        .hasSize(1)
        .contains(Protos.TaskID.newBuilder().setValue(taskID).build());
}
 
Example 10
Source File: UninstallSchedulerTest.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testUninstallStepsComplete() throws Exception {
    Protos.Offer offer = OfferTestUtils.getOffer(Arrays.asList(RESERVED_RESOURCE_1, RESERVED_RESOURCE_2));
    UninstallScheduler uninstallScheduler = getUninstallScheduler();
    // Invoke getClientStatus so that plan status is correctly processed before offers are passed:
    Assert.assertEquals(ClientStatusResponse.launching(true), uninstallScheduler.getClientStatus());
    uninstallScheduler.offers(Collections.singletonList(offer));

    // Verify that scheduler doesn't expect _1 and _2. It then expects that they have been cleaned:
    UnexpectedResourcesResponse response =
            uninstallScheduler.getUnexpectedResources(Collections.singletonList(offer));
    Assert.assertEquals(UnexpectedResourcesResponse.Result.PROCESSED, response.result);
    Assert.assertEquals(1, response.offerResources.size());
    Assert.assertEquals(offer.getResourcesList(), response.offerResources.iterator().next().getResources());

    // Check that _1 and _2 are now marked as complete, while _3 is still prepared:
    Plan plan = getUninstallPlan(uninstallScheduler);
    List<Status> expected = Arrays.asList(Status.COMPLETE, Status.COMPLETE, Status.COMPLETE, Status.PREPARED, Status.PENDING);
    Assert.assertEquals(plan.toString(), expected, getStepStatuses(plan));

    // Invoke getClientStatus so that plan status is correctly processed before offers are passed.
    // Shouldn't see any new work since all the uninstall steps were considered candidates up-front.
    Assert.assertEquals(ClientStatusResponse.launching(false), uninstallScheduler.getClientStatus());
    offer = OfferTestUtils.getOffer(Collections.singletonList(RESERVED_RESOURCE_3));
    uninstallScheduler.offers(Collections.singletonList(offer));

    // Verify that scheduler doesn't expect _3. It then expects that it has been cleaned:
    response = uninstallScheduler.getUnexpectedResources(Collections.singletonList(offer));
    Assert.assertEquals(UnexpectedResourcesResponse.Result.PROCESSED, response.result);
    Assert.assertEquals(1, response.offerResources.size());
    Assert.assertEquals(offer.getResourcesList(), response.offerResources.iterator().next().getResources());

    expected = Arrays.asList(Status.COMPLETE, Status.COMPLETE, Status.COMPLETE, Status.COMPLETE, Status.PENDING);
    Assert.assertEquals(expected, getStepStatuses(plan));
}
 
Example 11
Source File: CassandraFrameworkProtosUtils.java    From cassandra-mesos-deprecated with Apache License 2.0 5 votes vote down vote up
public static String attributeValue(@NotNull final Protos.Offer offer, @NotNull final String name) {
    for (final Protos.Attribute attribute : offer.getAttributesList()) {
        if (name.equals(attribute.getName())) {
            return attribute.hasText() ? attribute.getText().getValue() : null;
        }
    }

    return null;
}
 
Example 12
Source File: ZoneRule.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<String> getKeys(Protos.Offer offer) {
  if (offer.hasDomain() && offer.getDomain().hasFaultDomain()) {
    return Arrays.asList(offer.getDomain().getFaultDomain().getZone().getName());
  }

  return Collections.emptyList();
}
 
Example 13
Source File: KubeApiServerIntegrator.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
private VirtualMachineLease nodeToLease(V1Node node) {
    Protos.Offer offer = nodeToOffer(node);
    if (offer == null) {
        return null;
    }
    return new VMLeaseObject(offer);
}
 
Example 14
Source File: YarnNodeCapacityManager.java    From incubator-myriad with Apache License 2.0 4 votes vote down vote up
/**
 * Checks if any containers were allocated in the current scheduler run and
 * launches the corresponding Mesos tasks. It also updates the node
 * capacity depending on what portion of the consumed offers were actually
 * used.
 */
@VisibleForTesting
protected void handleContainerAllocation(RMNode rmNode) {
  String host = rmNode.getNodeID().getHost();

  ConsumedOffer consumedOffer = offerLifecycleMgr.drainConsumedOffer(host);
  if (consumedOffer == null) {
    LOGGER.debug("No offer consumed for {}", host);
    return;
  }

  Node node = nodeStore.getNode(host);
  Set<RMContainer> containersBeforeSched = node.getContainerSnapshot();
  Set<RMContainer> containersAfterSched = new HashSet<>(node.getNode().getRunningContainers());

  Set<RMContainer> containersAllocatedByMesosOffer = (containersBeforeSched == null) ? containersAfterSched : Sets.difference(
      containersAfterSched, containersBeforeSched);

  if (containersAllocatedByMesosOffer.isEmpty()) {
    LOGGER.debug("No containers allocated using Mesos offers for host: {}", host);
    for (Protos.Offer offer : consumedOffer.getOffers()) {
      offerLifecycleMgr.declineOffer(offer);
    }
    decrementNodeCapacity(rmNode, OfferUtils.getYarnResourcesFromMesosOffers(consumedOffer.getOffers()));
  } else {
    LOGGER.debug("Containers allocated using Mesos offers for host: {} count: {}", host, containersAllocatedByMesosOffer.size());

    // Identify the Mesos tasks that need to be launched
    List<Protos.TaskInfo> tasks = Lists.newArrayList();
    Resource resUsed = Resource.newInstance(0, 0);

    for (RMContainer newContainer : containersAllocatedByMesosOffer) {
      tasks.add(getTaskInfoForContainer(newContainer, consumedOffer, node));
      resUsed = Resources.add(resUsed, newContainer.getAllocatedResource());
    }

    // Reduce node capacity to account for unused offers
    Resource resOffered = OfferUtils.getYarnResourcesFromMesosOffers(consumedOffer.getOffers());
    Resource resUnused = Resources.subtract(resOffered, resUsed);
    decrementNodeCapacity(rmNode, resUnused);
    myriadDriver.getDriver().launchTasks(consumedOffer.getOfferIds(), tasks);
  }

  // No need to hold on to the snapshot anymore
  node.removeContainerSnapshot();
}
 
Example 15
Source File: OfferTestUtils.java    From dcos-commons with Apache License 2.0 4 votes vote down vote up
public static Protos.Offer getOffer(Protos.Resource resource) {
    return getOffer(Collections.singletonList(resource));
}
 
Example 16
Source File: SchedulerProxy.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Override
public void resourceOffers(SchedulerDriver driver, List<Protos.Offer> offers) {
	mesosActor.tell(new ResourceOffers(offers), ActorRef.noSender());
}
 
Example 17
Source File: LaunchTask.java    From storm with Apache License 2.0 4 votes vote down vote up
public LaunchTask(final Protos.TaskInfo task, final Protos.Offer offer) {
  this.task = task;
  this.offer = offer;
}
 
Example 18
Source File: ResourceOffers.java    From flink with Apache License 2.0 4 votes vote down vote up
public ResourceOffers(List<Protos.Offer> offers) {
	requireNonNull(offers);
	this.offers = offers;
}
 
Example 19
Source File: IMesosStormScheduler.java    From storm with Apache License 2.0 2 votes vote down vote up
/**
 * This method is invoked by Nimbus when it wants to get a list of worker slots that are available for assigning the
 * topology workers. In Nimbus's view, a "WorkerSlot" is a host and port that it can use to assign a worker.
 */
public List<WorkerSlot> allSlotsAvailableForScheduling(Map<Protos.OfferID, Protos.Offer> offers,
                                                       Collection<SupervisorDetails> existingSupervisors,
                                                       Topologies topologies, Set<String> topologiesMissingAssignments);
 
Example 20
Source File: OfferProcessor.java    From dcos-commons with Apache License 2.0 2 votes vote down vote up
/**
 * Declines the provided offers for a short time. This is used for special cases when the
 * scheduler hasn't fully initialized and otherwise wasn't able to actually look at the offers
 * in question. At least not yet.
 */
static void declineShort(Collection<Protos.Offer> unusedOffers) {
  declineOffers(unusedOffers, Constants.SHORT_DECLINE_SECONDS);
  Metrics.incrementDeclinesShort(unusedOffers.size());
}