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;
}
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;
}
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));
}
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;
}
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;
}
Aggregations