use of com.hashicorp.nomad.apimodel.Service 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;
}
Aggregations