schedulerDriver.acceptOffers(msg.offerIds(), msg.operations(), msg.filters()); } catch (Exception ex) { onFatalError(new ResourceManagerException("unable to accept offers", ex));
public void accept(List<OfferRecommendation> recommendations) { if (CollectionUtils.isEmpty(recommendations)) { return; } // Group recommendations by agent: Mesos requires that acceptOffers() only applies to a single agent at a time. // Note that ORDERING IS IMPORTANT: // The resource lifecycle is RESERVE -> CREATE -> DESTROY -> UNRESERVE // Therefore we must preserve ordering within each per-agent set of operations. final Map<String, List<OfferRecommendation>> recsByAgent = groupByAgent(recommendations); for (Map.Entry<String, List<OfferRecommendation>> agentRecs : recsByAgent.entrySet()) { Collection<Protos.Offer.Operation> operations = new ArrayList<>(); Collection<Protos.OfferID> offerIds = new HashSet<>(); for (OfferRecommendation rec : agentRecs.getValue()) { rec.getOperation().ifPresent(operation -> { // Note: We ensure that we only include the offerids for recommendations with operations to perform: operations.add(rec.getOperation().get()); offerIds.add(rec.getOfferId()); }); } int skippedOperations = operations.size() - agentRecs.getValue().size(); if (skippedOperations != 0) { LOGGER.info("Skipping {} recommendations with no operation", skippedOperations); } logOperations(agentRecs.getKey(), offerIds, operations); Driver.getInstance().acceptOffers(offerIds, operations, FILTERS); } } }
.acceptOffers(offerIdsCaptor.capture(), operationsCaptor.capture(), any());
.acceptOffers(offerIdsCaptor.capture(), operationsCaptor.capture(), any());
@Test public void testEmptyOffers() throws Exception { UninstallScheduler uninstallScheduler = getUninstallScheduler(); Assert.assertEquals(ClientStatusResponse.launching(true), uninstallScheduler.getClientStatus()); Assert.assertEquals(OfferResponse.Result.PROCESSED, uninstallScheduler.offers(Collections.emptyList()).result); verify(mockSchedulerDriver, times(0)).acceptOffers(any(), any(), any()); verify(mockSchedulerDriver, times(0)).declineOffer(any(), any()); }
@Test public void testAcceptRecommendationsByAgent() { Driver.setDriver(mockDriver); ACCEPTER.accept(ALL_RECOMMENDATIONS); // Separate calls for each agent: verify(mockDriver, times(2)).acceptOffers(offerIdCaptor.capture(), operationCaptor.capture(), any()); // Offer ids should be deduped within each agent. Also the ordering should be alphabetical based on agent id. List<Collection<Protos.OfferID>> offerIdCalls = offerIdCaptor.getAllValues(); Assert.assertEquals(2, offerIdCalls.size()); Assert.assertEquals(Collections.singleton(OFFER_A.getId()), offerIdCalls.get(0)); Assert.assertEquals(Collections.singleton(OFFER_B.getId()), offerIdCalls.get(1)); List<Collection<Protos.Offer.Operation>> operationCalls = operationCaptor.getAllValues(); Assert.assertEquals(2, operationCalls.size()); Assert.assertEquals(Arrays.asList(DESTROY_A.getOperation().get(), UNRESERVE_A.getOperation().get()), operationCalls.get(0)); Assert.assertEquals(Arrays.asList(DESTROY_B.getOperation().get(), UNRESERVE_B.getOperation().get()), operationCalls.get(1)); } }
@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()); }