Search in sources :

Example 1 with ResourceOfferContainer

use of org.apache.myriad.scheduler.resource.ResourceOfferContainer 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 2 with ResourceOfferContainer

use of org.apache.myriad.scheduler.resource.ResourceOfferContainer in project incubator-myriad by apache.

the class TestNMTaskFactory method testNMTaskFactory.

@Test
public void testNMTaskFactory() {
    NMExecutorCommandLineGenerator clGenerator = new NMExecutorCommandLineGenerator(cfgWithDocker);
    TaskUtils taskUtils = new TaskUtils(cfgWithDocker);
    Protos.Offer offer = new OfferBuilder("test.com").addScalarResource("cpus", 10.0).addScalarResource("mem", 16000).addRangeResource("ports", 3500, 3505).build();
    ServiceResourceProfile profile = new ExtendedResourceProfile(new NMProfile("tooMuchCpu", 7L, 8000L), taskUtils.getNodeManagerCpus(), taskUtils.getNodeManagerMemory(), taskUtils.getNodeManagerPorts());
    NodeTask nodeTask = new NodeTask(profile, null);
    ResourceOfferContainer roc = new ResourceOfferContainer(offer, profile, null);
    NMTaskFactory taskFactory = new NMTaskFactory(cfgWithDocker, taskUtils, clGenerator);
    Protos.TaskInfo taskInfo = taskFactory.createTask(roc, frameworkId, makeTaskId("nm.zero"), nodeTask);
    assertFalse("taskInfo should not have a container", taskInfo.hasContainer());
    assertTrue("The container should have an executor", taskInfo.hasExecutor());
    Protos.ExecutorInfo executorInfo = taskInfo.getExecutor();
    assertTrue("executorInfo should have container", executorInfo.hasContainer());
    Protos.ContainerInfo containerInfo = executorInfo.getContainer();
    assertTrue("There should be two volumes", containerInfo.getVolumesCount() == 2);
    assertTrue("The first volume should be read only", containerInfo.getVolumes(0).getMode().equals(Protos.Volume.Mode.RO));
    assertTrue("The first volume should be read write", containerInfo.getVolumes(1).getMode().equals(Protos.Volume.Mode.RW));
    assertTrue("There should be a docker image", containerInfo.getDocker().hasImage());
    assertTrue("The docker image should be mesos/myraid", containerInfo.getDocker().getImage().equals("mesos/myriad"));
    assertTrue("Should be using host networking", containerInfo.getDocker().getNetwork().equals(Protos.ContainerInfo.DockerInfo.Network.HOST));
    assertTrue("There should be two parameters", containerInfo.getDocker().getParametersList().size() == 2);
    assertTrue("Privledged mode should be false", !containerInfo.getDocker().getPrivileged());
}
Also used : OfferBuilder(org.apache.myriad.scheduler.offer.OfferBuilder) NodeTask(org.apache.myriad.state.NodeTask) ResourceOfferContainer(org.apache.myriad.scheduler.resource.ResourceOfferContainer) Protos(org.apache.mesos.Protos) Test(org.junit.Test) BaseConfigurableTest(org.apache.myriad.BaseConfigurableTest)

Example 3 with ResourceOfferContainer

use of org.apache.myriad.scheduler.resource.ResourceOfferContainer in project incubator-myriad by apache.

the class TestServiceTaskFactory method testServiceTaskFactory.

@Test
public void testServiceTaskFactory() {
    ServiceCommandLineGenerator clGenerator = new ServiceCommandLineGenerator(cfgWithDocker);
    TaskUtils taskUtils = new TaskUtils(cfgWithDocker);
    Protos.Offer offer = new OfferBuilder("test.com").addScalarResource("cpus", 10.0).addScalarResource("mem", 16000).addRangeResource("ports", 3400, 3410).build();
    Map<String, ServiceConfiguration> stringServiceConfigurationMap = cfgWithDocker.getServiceConfigurations();
    System.out.print(stringServiceConfigurationMap);
    ServiceConfiguration serviceConfiguration = cfgWithDocker.getServiceConfigurations().get("jobhistory");
    ServiceResourceProfile profile = new ServiceResourceProfile("jobhistory", serviceConfiguration.getCpus(), serviceConfiguration.getJvmMaxMemoryMB(), serviceConfiguration.getPorts());
    NodeTask nodeTask = new NodeTask(profile, null);
    nodeTask.setTaskPrefix("jobhistory");
    ResourceOfferContainer roc = new ResourceOfferContainer(offer, profile, null);
    System.out.print(roc.getPorts());
    ServiceTaskFactory taskFactory = new ServiceTaskFactory(cfgWithDocker, taskUtils, clGenerator);
    Protos.TaskInfo taskInfo = taskFactory.createTask(roc, frameworkId, makeTaskId("jobhistory"), nodeTask);
    assertTrue("taskInfo should have a container", taskInfo.hasContainer());
    assertFalse("The container should not have an executor", taskInfo.hasExecutor());
    Protos.ContainerInfo containerInfo = taskInfo.getContainer();
    assertTrue("There should be two volumes", containerInfo.getVolumesCount() == 2);
    assertTrue("The first volume should be read only", containerInfo.getVolumes(0).getMode().equals(Protos.Volume.Mode.RO));
    assertTrue("The first volume should be read write", containerInfo.getVolumes(1).getMode().equals(Protos.Volume.Mode.RW));
    assertTrue("There should be a docker image", containerInfo.getDocker().hasImage());
    assertTrue("The docker image should be mesos/myraid", containerInfo.getDocker().getImage().equals("mesos/myriad"));
    assertTrue("Should be using host networking", containerInfo.getDocker().getNetwork().equals(Protos.ContainerInfo.DockerInfo.Network.HOST));
    assertTrue("There should be two parameters", containerInfo.getDocker().getParametersList().size() == 2);
    assertTrue("Privledged mode should be false", containerInfo.getDocker().getPrivileged() == false);
}
Also used : OfferBuilder(org.apache.myriad.scheduler.offer.OfferBuilder) NodeTask(org.apache.myriad.state.NodeTask) ResourceOfferContainer(org.apache.myriad.scheduler.resource.ResourceOfferContainer) ServiceConfiguration(org.apache.myriad.configuration.ServiceConfiguration) Protos(org.apache.mesos.Protos) Test(org.junit.Test) BaseConfigurableTest(org.apache.myriad.BaseConfigurableTest)

Aggregations

Protos (org.apache.mesos.Protos)3 ResourceOfferContainer (org.apache.myriad.scheduler.resource.ResourceOfferContainer)3 NodeTask (org.apache.myriad.state.NodeTask)3 BaseConfigurableTest (org.apache.myriad.BaseConfigurableTest)2 OfferBuilder (org.apache.myriad.scheduler.offer.OfferBuilder)2 Test (org.junit.Test)2 Offer (org.apache.mesos.Protos.Offer)1 TaskInfo (org.apache.mesos.Protos.TaskInfo)1 SchedulerDriver (org.apache.mesos.SchedulerDriver)1 ServiceConfiguration (org.apache.myriad.configuration.ServiceConfiguration)1 ServiceResourceProfile (org.apache.myriad.scheduler.ServiceResourceProfile)1 Constraint (org.apache.myriad.scheduler.constraints.Constraint)1