Search in sources :

Example 16 with SchedulerDriver

use of org.apache.mesos.SchedulerDriver in project incubator-myriad by apache.

the class ResourceOffersEventHandler method onEvent.

@Override
public void onEvent(ResourceOffersEvent event, long sequence, boolean endOfBatch) throws Exception {
    SchedulerDriver driver = event.getDriver();
    List<Offer> offers = event.getOffers();
    // to not process any offers unless Myriad receives a "framework registered" notification.
    if (schedulerState.getFrameworkID() == null) {
        LOGGER.warn("Received {} offers, but declining them since Framework ID is not yet set", offers.size());
        for (Offer offer : offers) {
            driver.declineOffer(offer.getId());
        }
        return;
    }
    LOGGER.debug("Received offers {}", offers.size());
    LOGGER.debug("Pending tasks: {}", this.schedulerState.getPendingTaskIds());
    // Let Myriad use reserved resources firstly.
    Collections.sort(offers, new Comparator<Offer>() {

        boolean isReserved(Offer o) {
            for (Protos.Resource resource : o.getResourcesList()) {
                if (resource.hasRole() && !Objects.equals(resource.getRole(), DEFAULT_ROLE)) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public int compare(Offer o1, Offer o2) {
            boolean reserved1 = isReserved(o1);
            boolean reserved2 = isReserved(o2);
            if (reserved1 == reserved2) {
                return 0;
            }
            return reserved1 ? -1 : 1;
        }
    });
    driverOperationLock.lock();
    try {
        for (Iterator<Offer> iterator = offers.iterator(); iterator.hasNext(); ) {
            Offer offer = iterator.next();
            Set<NodeTask> nodeTasks = schedulerState.getNodeTasks(offer.getSlaveId());
            for (NodeTask nodeTask : nodeTasks) {
                nodeTask.setSlaveAttributes(offer.getAttributesList());
            }
            // keep this in case SchedulerState gets out of sync. This should not happen with
            // synchronizing addNodes method in SchedulerState
            // but to keep it safe
            final Set<Protos.TaskID> missingTasks = Sets.newHashSet();
            Set<Protos.TaskID> pendingTasks = schedulerState.getPendingTaskIds();
            if (CollectionUtils.isNotEmpty(pendingTasks)) {
                for (Protos.TaskID pendingTaskId : pendingTasks) {
                    NodeTask taskToLaunch = schedulerState.getTask(pendingTaskId);
                    if (taskToLaunch == null) {
                        missingTasks.add(pendingTaskId);
                        LOGGER.warn("Node task for TaskID: {} does not exist", pendingTaskId);
                        continue;
                    }
                    String taskPrefix = taskToLaunch.getTaskPrefix();
                    ServiceResourceProfile profile = taskToLaunch.getProfile();
                    Constraint constraint = taskToLaunch.getConstraint();
                    Set<NodeTask> launchedTasks = new HashSet<>();
                    launchedTasks.addAll(schedulerState.getActiveTasksByType(taskPrefix));
                    launchedTasks.addAll(schedulerState.getStagingTasksByType(taskPrefix));
                    ResourceOfferContainer resourceOfferContainer = new ResourceOfferContainer(offer, taskToLaunch.getProfile(), role);
                    if (SchedulerUtils.isUniqueHostname(offer, taskToLaunch, launchedTasks) && resourceOfferContainer.satisfies(taskToLaunch.getProfile(), constraint)) {
                        try {
                            final TaskInfo task = taskFactoryMap.get(taskPrefix).createTask(resourceOfferContainer, schedulerState.getFrameworkID().get(), pendingTaskId, taskToLaunch);
                            LOGGER.info("Launching task: {} using offer: {}", task.getTaskId().getValue(), offer.getId());
                            LOGGER.debug("Launching task: {} with profile: {} using offer: {}", task, profile, offer);
                            driver.launchTasks(Collections.singleton(offer.getId()), Collections.singleton(task));
                            schedulerState.makeTaskStaging(pendingTaskId);
                            // For every NM Task that we launch, we currently
                            // need to backup the ExecutorInfo for that NM Task in the State Store.
                            // Without this, we will not be able to launch tasks corresponding to yarn
                            // containers. This is specially important in case the RM restarts.
                            taskToLaunch.setExecutorInfo(task.getExecutor());
                            taskToLaunch.setHostname(offer.getHostname());
                            taskToLaunch.setSlaveId(offer.getSlaveId());
                            schedulerState.addTask(pendingTaskId, taskToLaunch);
                            // remove the used offer from offers list
                            iterator.remove();
                            break;
                        } catch (Throwable t) {
                            LOGGER.error("Exception thrown while trying to create a task for {}", taskPrefix, t);
                        }
                    }
                }
                for (Protos.TaskID taskId : missingTasks) {
                    schedulerState.removeTask(taskId);
                }
            }
        }
        for (Offer offer : offers) {
            if (SchedulerUtils.isEligibleForFineGrainedScaling(offer.getHostname(), schedulerState)) {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Picking an offer from slave with hostname {} for fine grained scaling.", offer.getHostname());
                }
                offerLifecycleMgr.addOffers(offer);
            } else {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Declining offer {} from slave {}.", offer, offer.getHostname());
                }
                driver.declineOffer(offer.getId());
            }
        }
    } finally {
        driverOperationLock.unlock();
    }
}
Also used : Constraint(org.apache.myriad.scheduler.constraints.Constraint) NodeTask(org.apache.myriad.state.NodeTask) ResourceOfferContainer(org.apache.myriad.scheduler.resource.ResourceOfferContainer) TaskInfo(org.apache.mesos.Protos.TaskInfo) Offer(org.apache.mesos.Protos.Offer) Protos(org.apache.mesos.Protos) ServiceResourceProfile(org.apache.myriad.scheduler.ServiceResourceProfile) SchedulerDriver(org.apache.mesos.SchedulerDriver)

Example 17 with SchedulerDriver

use of org.apache.mesos.SchedulerDriver in project dcos-commons by mesosphere.

the class Expect method stepStatus.

/**
 * Verifies that the indicated plan.phase.step has the expected status.
 */
public static Expect stepStatus(String planName, String phaseName, String stepName, Status expectedStatus) {
    return new Expect() {

        @Override
        public void expect(ClusterState state, SchedulerDriver mockDriver) {
            Plan recoveryPlan = state.getPlans().stream().filter(plan -> plan.getName().equals(planName)).findAny().get();
            Phase phase = recoveryPlan.getChildren().stream().filter(p -> p.getName().equals(phaseName)).findAny().get();
            Step step = phase.getChildren().stream().filter(s -> s.getName().equals(stepName)).findAny().get();
            Assert.assertEquals(expectedStatus, step.getStatus());
        }

        @Override
        public String getDescription() {
            return String.format("For Phase: %s, Step: %s, Status is %s", phaseName, stepName, expectedStatus);
        }
    };
}
Also used : Phase(com.mesosphere.sdk.scheduler.plan.Phase) Step(com.mesosphere.sdk.scheduler.plan.Step) Plan(com.mesosphere.sdk.scheduler.plan.Plan) SchedulerDriver(org.apache.mesos.SchedulerDriver)

Example 18 with SchedulerDriver

use of org.apache.mesos.SchedulerDriver in project dcos-commons by mesosphere.

the class Expect method planStatus.

public static Expect planStatus(String planName, Status status) {
    return new Expect() {

        @Override
        public void expect(ClusterState state, SchedulerDriver mockDriver) throws AssertionError {
            Plan plan = state.getPlans().stream().filter(p -> p.getName().equals(planName)).findFirst().get();
            Assert.assertEquals(status, plan.getStatus());
        }

        @Override
        public String getDescription() {
            return String.format("Plan %s has status %s", planName, status);
        }
    };
}
Also used : Plan(com.mesosphere.sdk.scheduler.plan.Plan) SchedulerDriver(org.apache.mesos.SchedulerDriver)

Example 19 with SchedulerDriver

use of org.apache.mesos.SchedulerDriver in project dcos-commons by mesosphere.

the class Expect method knownTasks.

/**
 * Verifies that the scheduler's list of tasks in the state store matches the provided set.
 */
public static Expect knownTasks(Persister persisterWithTasks, String... taskNames) {
    return new Expect() {

        @Override
        public void expect(ClusterState state, SchedulerDriver mockDriver) {
            Set<String> expectedTasks = new HashSet<>(Arrays.asList(taskNames));
            Set<String> tasks = new StateStore(persisterWithTasks).fetchTasks().stream().map(Protos.TaskInfo::getName).collect(Collectors.toSet());
            Assert.assertEquals(expectedTasks, tasks);
        }

        @Override
        public String getDescription() {
            return String.format("State store task names: %s", new StateStore(persisterWithTasks).fetchTasks().stream().map(Protos.TaskInfo::getName).collect(Collectors.toList()));
        }
    };
}
Also used : Protos(org.apache.mesos.Protos) StateStore(com.mesosphere.sdk.state.StateStore) SchedulerDriver(org.apache.mesos.SchedulerDriver)

Example 20 with SchedulerDriver

use of org.apache.mesos.SchedulerDriver in project dcos-commons by mesosphere.

the class Expect method reconciledExplicitly.

/**
 * Verifies that an explicit task reconciliation for the task statuses in the provided persister was invoked.
 */
public static Expect reconciledExplicitly(Persister persisterWithStatuses) {
    // Use a custom comparator for sorting: Protos don't implement Comparable
    final Comparator<Protos.TaskStatus> statusComparator = new Comparator<Protos.TaskStatus>() {

        @Override
        public int compare(TaskStatus o1, TaskStatus o2) {
            return o1.getTaskId().getValue().compareTo(o2.getTaskId().getValue());
        }
    };
    return new Expect() {

        // Use this form instead of using ArgumentCaptor.forClass() to avoid problems with typecasting generics:
        @Captor
        private ArgumentCaptor<Collection<Protos.TaskStatus>> statusCaptor;

        @Override
        public void expect(ClusterState state, SchedulerDriver mockDriver) {
            MockitoAnnotations.initMocks(this);
            verify(mockDriver, atLeastOnce()).reconcileTasks(statusCaptor.capture());
            Set<Protos.TaskStatus> expected = new TreeSet<>(statusComparator);
            expected.addAll(new StateStore(persisterWithStatuses).fetchStatuses());
            // We do this arg ourselves, since the in-mock comparison never matches.
            for (Collection<Protos.TaskStatus> reconcileArgs : statusCaptor.getAllValues()) {
                Set<Protos.TaskStatus> got = new TreeSet<>(statusComparator);
                got.addAll(reconcileArgs);
                if (expected.equals(got)) {
                    // Found matching call
                    return;
                }
            }
            Assert.fail(String.format("Expected a task reconcile with arguments: %s, but actual calls were: %s", expected, statusCaptor.getAllValues()));
        }

        @Override
        public String getDescription() {
            return String.format("Explicit task reconcile call for statuses: %s", new StateStore(persisterWithStatuses).fetchStatuses().stream().map(status -> String.format("%s=%s", status.getTaskId().getValue(), status.getState())).collect(Collectors.toList()));
        }
    };
}
Also used : ArgumentCaptor(org.mockito.ArgumentCaptor) Protos(org.apache.mesos.Protos) StateStore(com.mesosphere.sdk.state.StateStore) TaskStatus(org.apache.mesos.Protos.TaskStatus) SchedulerDriver(org.apache.mesos.SchedulerDriver)

Aggregations

SchedulerDriver (org.apache.mesos.SchedulerDriver)26 Scheduler (org.apache.mesos.Scheduler)10 Subscribe (com.google.common.eventbus.Subscribe)8 Protos (org.apache.mesos.Protos)8 UPID (com.groupon.mesos.util.UPID)6 Properties (java.util.Properties)6 Before (org.junit.Before)6 Config (com.twitter.heron.spi.common.Config)4 StateStore (com.mesosphere.sdk.state.StateStore)3 FrameworkID (org.apache.mesos.Protos.FrameworkID)3 Test (org.junit.Test)3 Plan (com.mesosphere.sdk.scheduler.plan.Plan)2 BaseContainer (com.twitter.heron.scheduler.mesos.framework.BaseContainer)2 MesosFramework (com.twitter.heron.scheduler.mesos.framework.MesosFramework)2 Config (org.apache.heron.spi.common.Config)2 Offer (org.apache.mesos.Protos.Offer)2 TaskStatus (org.apache.mesos.Protos.TaskStatus)2 Etcd (com.chinaunicom.etcd.v2.Etcd)1 RestfulServerConfiguration (com.dangdang.ddframe.job.cloud.scheduler.env.RestfulServerConfiguration)1 ProducerManager (com.dangdang.ddframe.job.cloud.scheduler.producer.ProducerManager)1