Search in sources :

Example 6 with ServiceResourceProfile

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

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

the class TestResourceOfferContainer method testResouceOfferContainerForNMWithOutRole.

@Test
public void testResouceOfferContainerForNMWithOutRole() {
    Protos.Offer offer = new OfferBuilder("test.com").addScalarResource("cpus", 4.0).addScalarResource("mem", 8000).addRangeResource("ports", 3500, 3600).build();
    Map<String, Long> ports = new HashMap<>(4);
    ports.put("test1.address", 0L);
    ports.put("test2.address", 0L);
    ports.put("test3.address", 0L);
    ports.put("test4.port", 3501L);
    ServiceResourceProfile profile1 = new ExtendedResourceProfile(new NMProfile("small", 2L, 6000L), .2, 1024.0, ports);
    ResourceOfferContainer roc = new ResourceOfferContainer(offer, profile1, "");
    System.out.print(roc.getCpus());
    System.out.print(roc.getMem());
    System.out.print(roc.getPorts());
    assertTrue(roc.getHostName().equals("test.com"));
    assertTrue("Should be satisfied if offer contains request", roc.satisfies(profile1));
    ServiceResourceProfile profile2 = new ExtendedResourceProfile(new NMProfile("tooMuchCpu", 7L, 8000L), .2, 1024.0, ports);
    roc = new ResourceOfferContainer(offer, profile2, "");
    assertFalse("Should be unsatisfied if too much cpu requested", roc.satisfies(profile2));
    ServiceResourceProfile profile3 = new ExtendedResourceProfile(new NMProfile("tooMuchMem", 3L, 50000L), .2, 1024.0, ports);
    roc = new ResourceOfferContainer(offer, profile3, "");
    assertFalse("Should be unsatisfied if too much memory requested", roc.satisfies(profile3));
    ports.put("test.bad.address", 1500L);
    ServiceResourceProfile profile4 = new ExtendedResourceProfile(new NMProfile("portOutOfRange", 3L, 50000L), .2, 1024.0, ports);
    roc = new ResourceOfferContainer(offer, profile4, "");
    assertFalse("Should be unsatisfied if port not in range", roc.satisfies(profile4));
    List<Protos.Resource> resourcesCpu = roc.consumeCpus(3.0);
    assertTrue("Should get a list of resources of size 1", resourcesCpu.size() == 1.0);
    assertTrue("Cpus should be decreased", roc.getCpus() == 1.0);
    List<Protos.Resource> resourcesMem = roc.consumeMem(7000.0);
    assertTrue("Should get a list of resources of size 1", resourcesMem.size() == 1);
    assertTrue("Mem should be decreased", roc.getMem() == 1000.0);
}
Also used : ExtendedResourceProfile(org.apache.myriad.scheduler.ExtendedResourceProfile) HashMap(java.util.HashMap) OfferBuilder(org.apache.myriad.scheduler.offer.OfferBuilder) NMProfile(org.apache.myriad.scheduler.NMProfile) Protos(org.apache.mesos.Protos) ServiceResourceProfile(org.apache.myriad.scheduler.ServiceResourceProfile) Test(org.junit.Test)

Example 8 with ServiceResourceProfile

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

the class TestResourceOfferContainer method testResouceOfferContainerForNMWithRole.

@Test
public void testResouceOfferContainerForNMWithRole() {
    Protos.Offer offer = new OfferBuilder("test.com").addScalarResource("cpus", 2.0).addScalarResource("mem", 8000).addScalarResource("cpus", "test", 4.0).addScalarResource("mem", "test", 32000.0).addRangeResource("ports", 3500, 3600).addRangeResource("ports", "test", 1500, 1600).build();
    Map<String, Long> ports = new HashMap<>(4);
    ports.put("test1.address", 0L);
    ports.put("test2.address", 0L);
    ports.put("test3.address", 1500L);
    ports.put("test4.port", 3502L);
    ServiceResourceProfile profile1 = new ExtendedResourceProfile(new NMProfile("small", 2L, 8000L), .2, 1024.0, ports);
    ResourceOfferContainer roc = new ResourceOfferContainer(offer, profile1, "test");
    assertTrue(roc.getHostName().equals("test.com"));
    assertTrue("Should be satisfied if offer contains request", roc.satisfies(profile1));
    ServiceResourceProfile profile2 = new ExtendedResourceProfile(new NMProfile("tooMuchCpu", 7L, 8000L), .2, 1024.0, ports);
    roc = new ResourceOfferContainer(offer, profile2, "test");
    assertFalse("Should be unsatisfied if too much cpu requested", roc.satisfies(profile2));
    ServiceResourceProfile profile3 = new ExtendedResourceProfile(new NMProfile("tooMuchMem", 3L, 50000L), .2, 1024.0, ports);
    roc = new ResourceOfferContainer(offer, profile3, "test");
    assertFalse("Should be unsatisfied if too much memory requested", roc.satisfies(profile3));
    ports.put("test.bad.address", 32000L);
    ServiceResourceProfile profile4 = new ExtendedResourceProfile(new NMProfile("portOutOfRange", 3L, 50000L), .2, 1024.0, ports);
    roc = new ResourceOfferContainer(offer, profile4, "test");
    assertFalse("Should be unsatisfied if port not in range", roc.satisfies(profile4));
    List<Protos.Resource> resources = roc.consumeCpus(4.5);
    assertTrue("Resource List should be of size to when requesting 4.1 cpus", (resources.size() == 2));
    assertTrue("Cpus should be decreased", roc.getCpus() <= 1.5);
    List<Protos.Resource> resources1 = roc.consumeCpus(1.5);
    assertTrue("Resource List should be of size 1", resources1.size() == 1);
    assertTrue("All cpu resources should be consumed", roc.getCpus() <= 0.0);
}
Also used : ExtendedResourceProfile(org.apache.myriad.scheduler.ExtendedResourceProfile) HashMap(java.util.HashMap) OfferBuilder(org.apache.myriad.scheduler.offer.OfferBuilder) NMProfile(org.apache.myriad.scheduler.NMProfile) Protos(org.apache.mesos.Protos) ServiceResourceProfile(org.apache.myriad.scheduler.ServiceResourceProfile) Test(org.junit.Test)

Example 9 with ServiceResourceProfile

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

the class Main method initServiceConfigurations.

/**
 * Create ServiceProfile for any configured service
 *
 * @param cfg
 * @param injector
 */
private void initServiceConfigurations(MyriadConfiguration cfg, Injector injector) {
    LOGGER.info("Initializing initServiceConfigurations");
    ServiceProfileManager profileManager = injector.getInstance(ServiceProfileManager.class);
    Map<String, ServiceConfiguration> servicesConfigs = injector.getInstance(MyriadConfiguration.class).getServiceConfigurations();
    for (Map.Entry<String, ServiceConfiguration> entry : servicesConfigs.entrySet()) {
        final String taskPrefix = entry.getKey();
        ServiceConfiguration config = entry.getValue();
        final Double cpu = config.getCpus();
        final Double mem = config.getJvmMaxMemoryMB();
        final Map<String, Long> ports = config.getPorts();
        profileManager.add(new ServiceResourceProfile(taskPrefix, cpu, mem, ports));
    }
}
Also used : ServiceProfileManager(org.apache.myriad.scheduler.ServiceProfileManager) ServiceConfiguration(org.apache.myriad.configuration.ServiceConfiguration) MyriadConfiguration(org.apache.myriad.configuration.MyriadConfiguration) ServiceResourceProfile(org.apache.myriad.scheduler.ServiceResourceProfile) Map(java.util.Map)

Example 10 with ServiceResourceProfile

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

the class Main method validateNMInstances.

private void validateNMInstances(Injector injector) {
    LOGGER.info("Validating nmInstances..");
    Map<String, Integer> nmInstances = injector.getInstance(MyriadConfiguration.class).getNmInstances();
    ServiceProfileManager profileManager = injector.getInstance(ServiceProfileManager.class);
    long maxCpu = Long.MIN_VALUE;
    long maxMem = Long.MIN_VALUE;
    for (Map.Entry<String, Integer> entry : nmInstances.entrySet()) {
        String profile = entry.getKey();
        ServiceResourceProfile nodeManager = profileManager.get(profile);
        if (nodeManager == null) {
            throw new IllegalArgumentException("Invalid profile name '" + profile + "' specified in 'nmInstances'");
        }
        if (entry.getValue() > 0) {
            if (nodeManager.getCpus() > maxCpu) {
                // find the profile with largest number of cpus
                maxCpu = nodeManager.getCpus().longValue();
                // use the memory from the same profile
                maxMem = nodeManager.getMemory().longValue();
            }
        }
    }
    if (maxCpu <= 0 || maxMem <= 0) {
        throw new IllegalStateException("Please configure 'nmInstances' with at least one instance/profile " + "with non-zero cpu/mem resources.");
    }
}
Also used : ServiceProfileManager(org.apache.myriad.scheduler.ServiceProfileManager) MyriadConfiguration(org.apache.myriad.configuration.MyriadConfiguration) ServiceResourceProfile(org.apache.myriad.scheduler.ServiceResourceProfile) Map(java.util.Map)

Aggregations

ServiceResourceProfile (org.apache.myriad.scheduler.ServiceResourceProfile)11 Protos (org.apache.mesos.Protos)5 HashMap (java.util.HashMap)4 OfferBuilder (org.apache.myriad.scheduler.offer.OfferBuilder)4 Test (org.junit.Test)4 Map (java.util.Map)3 MyriadConfiguration (org.apache.myriad.configuration.MyriadConfiguration)3 ExtendedResourceProfile (org.apache.myriad.scheduler.ExtendedResourceProfile)3 NMProfile (org.apache.myriad.scheduler.NMProfile)3 ServiceProfileManager (org.apache.myriad.scheduler.ServiceProfileManager)3 LikeConstraint (org.apache.myriad.scheduler.constraints.LikeConstraint)3 NodeTask (org.apache.myriad.state.NodeTask)3 TreeMap (java.util.TreeMap)2 Before (org.junit.Before)2 MyriadFileSystemRMStateStore (org.apache.hadoop.yarn.server.resourcemanager.recovery.MyriadFileSystemRMStateStore)1 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 TaskUtils (org.apache.myriad.scheduler.TaskUtils)1