Search in sources :

Example 1 with Port

use of com.hashicorp.nomad.apimodel.Port in project incubator-heron by apache.

the class NomadScheduler method getTaskSpecRawDriver.

/**
 * Get the task spec for using raw_exec driver in Nomad
 * In raw exec mode, Heron will be run directly on the machine
 */
Task getTaskSpecRawDriver(Task task, String taskName, int containerIndex) {
    String executorBinary = Context.executorBinary(this.clusterConfig);
    // get arguments for heron executor command
    String[] executorArgs = SchedulerUtils.executorCommandArgs(this.clusterConfig, this.runtimeConfig, NomadConstants.EXECUTOR_PORTS, String.valueOf(containerIndex));
    // get complete heron executor command
    String executorCmd = executorBinary + " " + String.join(" ", executorArgs);
    // get heron_downloader command for downloading topology package
    String topologyDownloadCmd = getFetchCommand(this.clusterConfig, this.runtimeConfig);
    // read nomad heron executor start up script from file
    String heronNomadScript = getHeronNomadScript(this.localConfig);
    task.setName(taskName);
    // use raw_exec driver
    task.setDriver(NomadConstants.NomadDriver.RAW_EXEC.getName());
    // call nomad heron start up script
    task.addConfig(NomadConstants.NOMAD_TASK_COMMAND, NomadConstants.SHELL_CMD);
    String[] args = { NomadConstants.NOMAD_HERON_SCRIPT_NAME };
    task.addConfig(NomadConstants.NOMAD_TASK_COMMAND_ARGS, args);
    Template template = new Template();
    template.setEmbeddedTmpl(heronNomadScript);
    template.setDestPath(NomadConstants.NOMAD_HERON_SCRIPT_NAME);
    task.addTemplates(template);
    Resources resourceReqs = new Resources();
    // configure nomad to allocate dynamic ports
    Port[] ports = new Port[NomadConstants.EXECUTOR_PORTS.size()];
    int i = 0;
    for (SchedulerUtils.ExecutorPort port : NomadConstants.EXECUTOR_PORTS.keySet()) {
        ports[i] = new Port().setLabel(port.getName().replace("-", "_"));
        i++;
    }
    // set enviroment variables used int the heron nomad start up script
    Map<String, String> envVars = new HashMap<>();
    envVars.put(NomadConstants.HERON_NOMAD_WORKING_DIR, NomadContext.workingDirectory(this.localConfig) + "/container-" + String.valueOf(containerIndex));
    if (NomadContext.useCorePackageUri(this.localConfig)) {
        envVars.put(NomadConstants.HERON_USE_CORE_PACKAGE_URI, "true");
        envVars.put(NomadConstants.HERON_CORE_PACKAGE_URI, NomadContext.corePackageUri(this.localConfig));
    } else {
        envVars.put(NomadConstants.HERON_USE_CORE_PACKAGE_URI, "false");
        envVars.put(NomadConstants.HERON_CORE_PACKAGE_DIR, NomadContext.corePackageDirectory(this.localConfig));
    }
    envVars.put(NomadConstants.HERON_TOPOLOGY_DOWNLOAD_CMD, topologyDownloadCmd);
    envVars.put(NomadConstants.HERON_EXECUTOR_CMD, executorCmd);
    task.setEnv(envVars);
    return task;
}
Also used : HashMap(java.util.HashMap) Port(com.hashicorp.nomad.apimodel.Port) SchedulerUtils(com.twitter.heron.scheduler.utils.SchedulerUtils) Resources(com.hashicorp.nomad.apimodel.Resources) Template(com.hashicorp.nomad.apimodel.Template)

Example 2 with Port

use of com.hashicorp.nomad.apimodel.Port in project heron by twitter.

the class NomadScheduler method getTask.

Task getTask(String taskName, int containerIndex, Resource containerResource) {
    String nomadDriver = NomadContext.getHeronNomadDriver(this.localConfig);
    Task task = new Task();
    if (nomadDriver.equals(NomadConstants.NomadDriver.RAW_EXEC.getName())) {
        getTaskSpecRawDriver(task, taskName, containerIndex);
    } else if (nomadDriver.equals(NomadConstants.NomadDriver.DOCKER.getName())) {
        getTaskSpecDockerDriver(task, taskName, containerIndex);
    } else {
        throw new IllegalArgumentException("Invalid Nomad driver specified: " + nomadDriver);
    }
    // set resources requests
    Resources resourceReqs = new Resources();
    // configure nomad to allocate dynamic ports
    Port[] ports = new Port[NomadConstants.EXECUTOR_PORTS.size()];
    int i = 0;
    for (SchedulerUtils.ExecutorPort port : NomadConstants.EXECUTOR_PORTS.keySet()) {
        ports[i] = new Port().setLabel(port.getName().replace("-", "_"));
        i++;
    }
    NetworkResource networkResource = new NetworkResource();
    networkResource.addDynamicPorts(ports);
    // set memory requirements
    long memoryReqMb = containerResource.getRam().asMegabytes();
    resourceReqs.setMemoryMb(longToInt(memoryReqMb));
    // set CPU requirements
    double coresReq = containerResource.getCpu();
    double coresReqFreq = NomadContext.getCoreFreqMapping(this.localConfig) * coresReq;
    resourceReqs.setCpu(Integer.valueOf((int) Math.round(coresReqFreq)));
    // set disk requirements
    long diskReqMb = containerResource.getDisk().asMegabytes();
    resourceReqs.setDiskMb(longToInt(diskReqMb));
    // allocate dynamic port for prometheus/websink metrics
    String prometheusPortFile = getPrometheusMetricsFile(this.localConfig);
    if (prometheusPortFile == null) {
        LOG.severe("Failed to find port file for Prometheus metrics. " + "Please check metrics sinks configurations");
    } else {
        networkResource.addDynamicPorts(new Port().setLabel(METRICS_PORT));
        task.addEnv(NomadConstants.METRICS_PORT_FILE, prometheusPortFile);
        if (NomadContext.getHeronNomadMetricsServiceRegister(this.localConfig)) {
            // getting tags for service
            List<String> tags = new LinkedList<>();
            tags.add(String.format("%s-%s", Runtime.topologyName(this.runtimeConfig), containerIndex));
            tags.addAll(Arrays.asList(NomadContext.getHeronNomadMetricsServiceAdditionalTags(this.localConfig)));
            // register metrics service with consul
            Service service = new Service().setName(getMetricsServiceName(Runtime.topologyName(this.runtimeConfig), containerIndex)).setPortLabel(METRICS_PORT).setTags(tags).addChecks(new ServiceCheck().setType(NomadConstants.NOMAD_SERVICE_CHECK_TYPE).setPortLabel(METRICS_PORT).setInterval(TimeUnit.NANOSECONDS.convert(NomadContext.getHeronNomadMetricsServiceCheckIntervalSec(this.localConfig), TimeUnit.SECONDS)).setTimeout(TimeUnit.NANOSECONDS.convert(NomadContext.getHeronNomadMetricsServiceCheckTimeoutSec(this.localConfig), TimeUnit.SECONDS)));
            task.addServices(service);
        }
    }
    resourceReqs.addNetworks(networkResource);
    task.setResources(resourceReqs);
    return task;
}
Also used : Task(com.hashicorp.nomad.apimodel.Task) ServiceCheck(com.hashicorp.nomad.apimodel.ServiceCheck) Port(com.hashicorp.nomad.apimodel.Port) SchedulerUtils(org.apache.heron.scheduler.utils.SchedulerUtils) Service(com.hashicorp.nomad.apimodel.Service) LinkedList(java.util.LinkedList) NetworkResource(com.hashicorp.nomad.apimodel.NetworkResource) Resources(com.hashicorp.nomad.apimodel.Resources)

Example 3 with Port

use of com.hashicorp.nomad.apimodel.Port in project heron by twitter.

the class NomadSchedulerTest method testGetTaskDocker.

@SuppressWarnings("unchecked")
@Test
public void testGetTaskDocker() {
    this.mockRuntime = this.mockRuntime.newBuilder().putAll(this.mockRuntime).put(NomadContext.HERON_NOMAD_DRIVER, NomadConstants.NomadDriver.DOCKER.getName()).build();
    this.mockConfig = this.mockConfig.newBuilder().putAll(this.mockConfig).put(NomadContext.HERON_NOMAD_DRIVER, NomadConstants.NomadDriver.DOCKER.getName()).build();
    Set<PackingPlan.ContainerPlan> containers = new HashSet<>();
    containers.add(Mockito.mock(PackingPlan.ContainerPlan.class));
    PowerMockito.mockStatic(SchedulerUtils.class);
    Resource resource = new Resource(CPU_RESOURCE, MEMORY_RESOURCE, DISK_RESOURCE);
    PowerMockito.when(SchedulerUtils.executorCommandArgs(Mockito.any(), Mockito.any(), Mockito.anyMap(), Mockito.anyString())).thenReturn(EXECUTOR_CMD_ARGS);
    PowerMockito.mockStatic(NomadScheduler.class);
    PowerMockito.when(NomadScheduler.getFetchCommand(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(TOPOLOGY_DOWNLOAD_CMD);
    PowerMockito.when(NomadScheduler.getHeronNomadScript(this.mockConfig)).thenReturn(HERON_NOMAD_SCRIPT);
    PowerMockito.when(NomadScheduler.longToInt(MEMORY_RESOURCE.asMegabytes())).thenReturn((int) MEMORY_RESOURCE.asMegabytes());
    PowerMockito.when(NomadScheduler.longToInt(DISK_RESOURCE.asMegabytes())).thenReturn((int) DISK_RESOURCE.asMegabytes());
    PowerMockito.when(NomadScheduler.getPrometheusMetricsFile(Mockito.any())).thenReturn(PORT_FILE);
    scheduler.initialize(this.mockConfig, this.mockRuntime);
    Task task = scheduler.getTask(TASK_NAME, CONTAINER_INDEX, resource);
    LOG.info("task: " + task);
    Assert.assertEquals(TASK_NAME, task.getName());
    Assert.assertEquals(NomadConstants.NomadDriver.DOCKER.getName(), task.getDriver());
    Assert.assertTrue(task.getConfig().containsKey(NomadConstants.NOMAD_TASK_COMMAND));
    Assert.assertEquals(NomadConstants.SHELL_CMD, task.getConfig().get(NomadConstants.NOMAD_TASK_COMMAND));
    Assert.assertTrue(task.getEnv().containsKey(NomadConstants.METRICS_PORT_FILE));
    Assert.assertEquals((int) CPU_RESOURCE * HERON_NOMAD_CORE_FREQ_MAPPING, task.getResources().getCpu().intValue());
    Assert.assertEquals((int) MEMORY_RESOURCE.asMegabytes(), task.getResources().getMemoryMb().intValue());
    Assert.assertEquals((int) DISK_RESOURCE.asMegabytes(), task.getResources().getDiskMb().intValue());
    Assert.assertTrue(task.getEnv().containsKey(NomadConstants.HOST));
    Assert.assertEquals(PORT_FILE, task.getEnv().get(NomadConstants.METRICS_PORT_FILE));
    Assert.assertEquals("${attr.unique.network.ip-address}", task.getEnv().get(NomadConstants.HOST));
    Assert.assertTrue(task.getConfig().containsKey(NomadConstants.NETWORK_MODE));
    Assert.assertEquals("default", task.getConfig().get(NomadConstants.NETWORK_MODE));
    Assert.assertEquals(task.getResources().getNetworks().size(), 1);
    Set<String> ports = new HashSet<>();
    for (Port entry : task.getResources().getNetworks().get(0).getDynamicPorts()) {
        ports.add(entry.getLabel());
        Assert.assertEquals(entry.getValue(), 0);
    }
    for (SchedulerUtils.ExecutorPort entry : NomadConstants.EXECUTOR_PORTS.keySet()) {
        Assert.assertTrue(ports.contains(entry.getName().replace("-", "_")));
    }
    Assert.assertTrue(ports.contains(NomadConstants.METRICS_PORT));
}
Also used : Task(com.hashicorp.nomad.apimodel.Task) Port(com.hashicorp.nomad.apimodel.Port) SchedulerUtils(org.apache.heron.scheduler.utils.SchedulerUtils) Resource(org.apache.heron.spi.packing.Resource) HashSet(java.util.HashSet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with Port

use of com.hashicorp.nomad.apimodel.Port in project incubator-heron by apache.

the class NomadScheduler method getTask.

Task getTask(String taskName, int containerIndex, Resource containerResource) {
    String nomadDriver = NomadContext.getHeronNomadDriver(this.localConfig);
    Task task = new Task();
    if (nomadDriver.equals(NomadConstants.NomadDriver.RAW_EXEC.getName())) {
        getTaskSpecRawDriver(task, taskName, containerIndex);
    } else if (nomadDriver.equals(NomadConstants.NomadDriver.DOCKER.getName())) {
        getTaskSpecDockerDriver(task, taskName, containerIndex);
    } else {
        throw new IllegalArgumentException("Invalid Nomad driver specified: " + nomadDriver);
    }
    // set resources requests
    Resources resourceReqs = new Resources();
    // configure nomad to allocate dynamic ports
    Port[] ports = new Port[NomadConstants.EXECUTOR_PORTS.size()];
    int i = 0;
    for (SchedulerUtils.ExecutorPort port : NomadConstants.EXECUTOR_PORTS.keySet()) {
        ports[i] = new Port().setLabel(port.getName().replace("-", "_"));
        i++;
    }
    resourceReqs.addNetworks(new NetworkResource().addDynamicPorts(ports));
    // set memory requirements
    long memoryReqMb = containerResource.getRam().asMegabytes();
    resourceReqs.setMemoryMb(longToInt(memoryReqMb));
    // set cpu requirements
    double coresReq = containerResource.getCpu();
    double coresReqFreq = NomadContext.getCoreFreqMapping(this.localConfig) * coresReq;
    resourceReqs.setCpu(Integer.valueOf((int) Math.round(coresReqFreq)));
    // set disk requirements
    long diskReqMb = containerResource.getDisk().asMegabytes();
    resourceReqs.setDiskMb(longToInt(diskReqMb));
    task.setResources(resourceReqs);
    return task;
}
Also used : NetworkResource(com.hashicorp.nomad.apimodel.NetworkResource) Task(com.hashicorp.nomad.apimodel.Task) Port(com.hashicorp.nomad.apimodel.Port) SchedulerUtils(com.twitter.heron.scheduler.utils.SchedulerUtils) Resources(com.hashicorp.nomad.apimodel.Resources)

Example 5 with Port

use of com.hashicorp.nomad.apimodel.Port in project heron by twitter.

the class NomadScheduler method getTaskSpecRawDriver.

/**
 * Get the task spec for using raw_exec driver in Nomad
 * In raw exec mode, Heron will be run directly on the machine
 */
Task getTaskSpecRawDriver(Task task, String taskName, int containerIndex) {
    String executorBinary = Context.executorBinary(this.clusterConfig);
    // get arguments for heron executor command
    String[] executorArgs = SchedulerUtils.executorCommandArgs(this.clusterConfig, this.runtimeConfig, NomadConstants.EXECUTOR_PORTS, String.valueOf(containerIndex));
    // get complete heron executor command
    String executorCmd = executorBinary + " " + String.join(" ", executorArgs);
    // get heron_downloader command for downloading topology package
    String topologyDownloadCmd = getFetchCommand(this.localConfig, this.clusterConfig, this.runtimeConfig);
    // read nomad heron executor start up script from file
    String heronNomadScript = getHeronNomadScript(this.localConfig);
    task.setName(taskName);
    // use raw_exec driver
    task.setDriver(NomadConstants.NomadDriver.RAW_EXEC.getName());
    // call nomad heron start up script
    task.addConfig(NomadConstants.NOMAD_TASK_COMMAND, NomadConstants.SHELL_CMD);
    String[] args = { NomadConstants.NOMAD_HERON_SCRIPT_NAME };
    task.addConfig(NomadConstants.NOMAD_TASK_COMMAND_ARGS, args);
    Template template = new Template();
    template.setEmbeddedTmpl(heronNomadScript);
    template.setDestPath(NomadConstants.NOMAD_HERON_SCRIPT_NAME);
    task.addTemplates(template);
    // configure nomad to allocate dynamic ports
    Port[] ports = new Port[NomadConstants.EXECUTOR_PORTS.size()];
    int i = 0;
    for (SchedulerUtils.ExecutorPort port : NomadConstants.EXECUTOR_PORTS.keySet()) {
        ports[i] = new Port().setLabel(port.getName().replace("-", "_"));
        i++;
    }
    // set enviroment variables used int the heron nomad start up script
    Map<String, String> envVars = new HashMap<>();
    envVars.put(NomadConstants.HERON_NOMAD_WORKING_DIR, NomadContext.workingDirectory(this.localConfig) + "/container-" + String.valueOf(containerIndex));
    if (NomadContext.useCorePackageUri(this.localConfig)) {
        envVars.put(NomadConstants.HERON_USE_CORE_PACKAGE_URI, "true");
        envVars.put(NomadConstants.HERON_CORE_PACKAGE_URI, NomadContext.corePackageUri(this.localConfig));
    } else {
        envVars.put(NomadConstants.HERON_USE_CORE_PACKAGE_URI, "false");
        envVars.put(NomadConstants.HERON_CORE_PACKAGE_DIR, NomadContext.corePackageDirectory(this.localConfig));
    }
    envVars.put(NomadConstants.HERON_TOPOLOGY_DOWNLOAD_CMD, topologyDownloadCmd);
    envVars.put(NomadConstants.HERON_EXECUTOR_CMD, executorCmd);
    task.setEnv(envVars);
    return task;
}
Also used : HashMap(java.util.HashMap) Port(com.hashicorp.nomad.apimodel.Port) SchedulerUtils(org.apache.heron.scheduler.utils.SchedulerUtils) Template(com.hashicorp.nomad.apimodel.Template)

Aggregations

Port (com.hashicorp.nomad.apimodel.Port)6 Resources (com.hashicorp.nomad.apimodel.Resources)4 Task (com.hashicorp.nomad.apimodel.Task)4 NetworkResource (com.hashicorp.nomad.apimodel.NetworkResource)3 Template (com.hashicorp.nomad.apimodel.Template)3 HashMap (java.util.HashMap)3 SchedulerUtils (org.apache.heron.scheduler.utils.SchedulerUtils)3 SchedulerUtils (com.twitter.heron.scheduler.utils.SchedulerUtils)2 Service (com.hashicorp.nomad.apimodel.Service)1 ServiceCheck (com.hashicorp.nomad.apimodel.ServiceCheck)1 JobAPI (edu.iu.dsc.tws.proto.system.job.JobAPI)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Resource (org.apache.heron.spi.packing.Resource)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1