use of org.apache.mesos.v1.Protos.TaskInfo in project Singularity by HubSpot.
the class SingularityMesosTaskBuilder method buildTask.
public SingularityMesosTaskHolder buildTask(SingularityOfferHolder offerHolder, List<Resource> availableResources, SingularityTaskRequest taskRequest, Resources desiredTaskResources, Resources desiredExecutorResources) {
final String sanitizedRackId = offerHolder.getSanitizedRackId();
final String sanitizedHost = offerHolder.getSanitizedHost();
final SingularityTaskId taskId = new SingularityTaskId(taskRequest.getPendingTask().getPendingTaskId().getRequestId(), taskRequest.getDeploy().getId(), System.currentTimeMillis(), taskRequest.getPendingTask().getPendingTaskId().getInstanceNo(), sanitizedHost, sanitizedRackId);
final TaskInfo.Builder bldr = TaskInfo.newBuilder().setTaskId(TaskID.newBuilder().setValue(taskId.toString()));
Optional<long[]> ports = Optional.absent();
Optional<Resource> portsResource = Optional.absent();
final Optional<SingularityContainerInfo> containerInfo = taskRequest.getDeploy().getContainerInfo();
if (desiredTaskResources.getNumPorts() > 0 || hasLiteralPortMapping(containerInfo)) {
List<Long> requestedPorts = new ArrayList<>();
if (hasLiteralPortMapping(containerInfo)) {
requestedPorts.addAll(containerInfo.get().getDocker().get().getLiteralHostPorts());
}
portsResource = Optional.of(MesosUtils.getPortsResource(desiredTaskResources.getNumPorts(), availableResources, requestedPorts));
ports = Optional.of(MesosUtils.getPorts(portsResource.get(), desiredTaskResources.getNumPorts()));
}
if (containerInfo.isPresent()) {
prepareContainerInfo(offerHolder, taskId, bldr, containerInfo.get(), ports);
}
if (taskRequest.getDeploy().getCustomExecutorCmd().isPresent()) {
prepareCustomExecutor(bldr, taskId, taskRequest, offerHolder, ports, desiredExecutorResources);
} else {
prepareCommand(bldr, taskId, taskRequest, offerHolder, ports);
}
if (portsResource.isPresent()) {
bldr.addResources(portsResource.get());
}
Optional<String> requiredRole = taskRequest.getRequest().getRequiredRole();
bldr.addResources(MesosUtils.getCpuResource(desiredTaskResources.getCpus(), requiredRole));
bldr.addResources(MesosUtils.getMemoryResource(desiredTaskResources.getMemoryMb(), requiredRole));
bldr.addResources(MesosUtils.getDiskResource(desiredTaskResources.getDiskMb(), requiredRole));
bldr.setAgentId(offerHolder.getOffers().get(0).getAgentId());
bldr.setName(taskRequest.getRequest().getId());
final Builder labelsBuilder = Labels.newBuilder();
// apply request-specific labels, if any
if (taskRequest.getDeploy().getMesosLabels().isPresent() && !taskRequest.getDeploy().getMesosLabels().get().isEmpty()) {
for (SingularityMesosTaskLabel label : taskRequest.getDeploy().getMesosLabels().get()) {
org.apache.mesos.v1.Protos.Label.Builder labelBuilder = Label.newBuilder();
labelBuilder.setKey(label.getKey());
if ((label.getValue().isPresent())) {
labelBuilder.setValue(label.getValue().get());
}
labelsBuilder.addLabels(labelBuilder.build());
}
}
// apply task-specific labels, if any
final int taskInstanceNo = taskRequest.getPendingTask().getPendingTaskId().getInstanceNo();
if (taskRequest.getDeploy().getMesosTaskLabels().isPresent() && taskRequest.getDeploy().getMesosTaskLabels().get().containsKey(taskInstanceNo) && !taskRequest.getDeploy().getMesosTaskLabels().get().get(taskInstanceNo).isEmpty()) {
for (SingularityMesosTaskLabel label : taskRequest.getDeploy().getMesosTaskLabels().get().get(taskInstanceNo)) {
org.apache.mesos.v1.Protos.Label.Builder labelBuilder = Label.newBuilder();
labelBuilder.setKey(label.getKey());
if ((label.getValue().isPresent())) {
labelBuilder.setValue(label.getValue().get());
}
labelsBuilder.addLabels(labelBuilder.build());
}
}
bldr.setLabels(labelsBuilder);
TaskInfo task = bldr.build();
return new SingularityMesosTaskHolder(new SingularityTask(taskRequest, taskId, offerHolder.getOffers().stream().map((o) -> mesosProtosUtils.offerFromProtos(o)).collect(Collectors.toList()), mesosProtosUtils.taskFromProtos(task), Optional.of(offerHolder.getRackId())), task);
}
use of org.apache.mesos.v1.Protos.TaskInfo in project Singularity by HubSpot.
the class SingularitySchedulerTestBase method prepTask.
protected SingularityTask prepTask(SingularityRequest request, SingularityDeploy deploy, long launchTime, int instanceNo, boolean separateHosts, Optional<String> runId) {
SingularityPendingTask pendingTask = buildPendingTask(request, deploy, launchTime, instanceNo, runId);
SingularityTaskRequest taskRequest = new SingularityTaskRequest(request, deploy, pendingTask);
Offer offer;
if (separateHosts) {
offer = createOffer(125, 1024, 2048, String.format("slave%s", instanceNo), String.format("host%s", instanceNo));
} else {
offer = createOffer(125, 1024, 2048);
}
SingularityTaskId taskId = new SingularityTaskId(request.getId(), deploy.getId(), launchTime, instanceNo, offer.getHostname(), "rack1");
TaskID taskIdProto = TaskID.newBuilder().setValue(taskId.toString()).build();
TaskInfo taskInfo = TaskInfo.newBuilder().setAgentId(offer.getAgentId()).setExecutor(ExecutorInfo.newBuilder().setExecutorId(ExecutorID.newBuilder().setValue("executorID"))).setTaskId(taskIdProto).setName("name").build();
SingularityTask task = new SingularityTask(taskRequest, taskId, Collections.singletonList(mesosProtosUtils.offerFromProtos(offer)), mesosProtosUtils.taskFromProtos(taskInfo), Optional.of("rack1"));
taskManager.savePendingTask(pendingTask);
return task;
}
use of org.apache.mesos.v1.Protos.TaskInfo in project Singularity by HubSpot.
the class SingularityMesosTaskBuilderTest method testEnvironmentVariableOverrides.
@Test
public void testEnvironmentVariableOverrides() {
Map<String, String> overrideVariables = new HashMap<>();
overrideVariables.put("MY_NEW_ENV_VAR", "test");
overrideVariables.put("STARTED_BY_USER", "notTestUser");
final SingularityRequest request = new SingularityRequestBuilder("test", RequestType.WORKER).build();
final SingularityDeploy deploy = new SingularityDeployBuilder("test", "1").setCommand(Optional.of("/bin/echo hi")).build();
final SingularityPendingTask pendingTask = new SingularityPendingTaskBuilder().setPendingTaskId(new SingularityPendingTaskId("test", "1", 0, 1, PendingType.IMMEDIATE, 0)).setUser(user).setEnvOverrides(overrideVariables).build();
final SingularityTaskRequest taskRequest = new SingularityTaskRequest(request, deploy, pendingTask);
final TaskInfo task = builder.buildTask(offerHolder, null, taskRequest, taskResources, executorResources).getMesosTask();
Map<String, String> environmentVariables = task.getCommand().getEnvironment().getVariablesList().stream().collect(Collectors.toMap(Variable::getName, Variable::getValue));
for (String key : overrideVariables.keySet()) {
assertEquals("Environment variable " + key + " not overridden.", environmentVariables.get(key), overrideVariables.get(key));
}
}
use of org.apache.mesos.v1.Protos.TaskInfo in project Singularity by HubSpot.
the class SingularityMesosTaskBuilder method prepareCustomExecutor.
/**
* Prepares the Mesos TaskInfo object when using our custom SingularityExecutor.
*/
private void prepareCustomExecutor(final TaskInfo.Builder bldr, final SingularityTaskId taskId, final SingularityTaskRequest task, final SingularityOfferHolder offerHolder, final Optional<long[]> ports, final Resources desiredExecutorResources) {
CommandInfo.Builder commandBuilder = CommandInfo.newBuilder().setValue(task.getDeploy().getCustomExecutorCmd().get());
prepareEnvironment(task, taskId, commandBuilder, offerHolder, ports);
if (task.getDeploy().getUser().isPresent()) {
commandBuilder.setUser(task.getDeploy().getUser().get());
}
prepareMesosUriDownloads(task.getPendingTask().getExtraArtifacts(), commandBuilder);
bldr.setExecutor(ExecutorInfo.newBuilder().setCommand(commandBuilder.build()).setExecutorId(ExecutorID.newBuilder().setValue(task.getDeploy().getCustomExecutorId().or(idGenerator.getNextExecutorId()))).setSource(// set source to taskId for use in statistics endpoint, TODO: remove
task.getDeploy().getCustomExecutorSource().or(taskId.getId())).setLabels(Labels.newBuilder().addLabels(Label.newBuilder().setKey("taskId").setValue(taskId.getId()))).addAllResources(buildMesosResources(desiredExecutorResources, task.getRequest().getRequiredRole())).build());
if (task.getDeploy().getExecutorData().isPresent()) {
final ExecutorDataBuilder executorDataBldr = task.getDeploy().getExecutorData().get().toBuilder();
String defaultS3Bucket = "";
String s3UploaderKeyPattern = "";
if (configuration.getS3ConfigurationOptional().isPresent()) {
if (task.getRequest().getGroup().isPresent() && configuration.getS3ConfigurationOptional().get().getGroupOverrides().containsKey(task.getRequest().getGroup().get())) {
defaultS3Bucket = configuration.getS3ConfigurationOptional().get().getGroupOverrides().get(task.getRequest().getGroup().get()).getS3Bucket();
LOG.trace("Setting defaultS3Bucket to {} for task {} executorData", defaultS3Bucket, taskId.getId());
} else {
defaultS3Bucket = configuration.getS3ConfigurationOptional().get().getS3Bucket();
}
s3UploaderKeyPattern = configuration.getS3ConfigurationOptional().get().getS3KeyFormat();
}
if (task.getPendingTask().getCmdLineArgsList().isPresent() && !task.getPendingTask().getCmdLineArgsList().get().isEmpty()) {
LOG.trace("Adding cmd line args {} to task {} executorData", task.getPendingTask().getCmdLineArgsList(), taskId.getId());
final ImmutableList.Builder<String> extraCmdLineArgsBuilder = ImmutableList.builder();
if (executorDataBldr.getExtraCmdLineArgs() != null && !executorDataBldr.getExtraCmdLineArgs().isEmpty()) {
extraCmdLineArgsBuilder.addAll(executorDataBldr.getExtraCmdLineArgs());
}
extraCmdLineArgsBuilder.addAll(task.getPendingTask().getCmdLineArgsList().get());
executorDataBldr.setExtraCmdLineArgs(extraCmdLineArgsBuilder.build());
}
List<SingularityS3UploaderFile> uploaderAdditionalFiles = new ArrayList<>();
if (configuration.getS3ConfigurationOptional().isPresent()) {
uploaderAdditionalFiles.addAll(configuration.getS3ConfigurationOptional().get().getS3UploaderAdditionalFiles());
}
uploaderAdditionalFiles.addAll(task.getPendingTask().getS3UploaderAdditionalFiles());
Optional<String> maybeS3StorageClass = configuration.getS3ConfigurationOptional().isPresent() ? configuration.getS3ConfigurationOptional().get().getS3StorageClass() : Optional.<String>absent();
Optional<Long> maybeApplyAfterBytes = configuration.getS3ConfigurationOptional().isPresent() ? configuration.getS3ConfigurationOptional().get().getApplyS3StorageClassAfterBytes() : Optional.<Long>absent();
if (task.getPendingTask().getRunAsUserOverride().isPresent()) {
executorDataBldr.setUser(task.getPendingTask().getRunAsUserOverride());
}
final SingularityTaskExecutorData executorData = new SingularityTaskExecutorData(executorDataBldr.build(), uploaderAdditionalFiles, defaultS3Bucket, s3UploaderKeyPattern, configuration.getCustomExecutorConfiguration().getServiceLog(), configuration.getCustomExecutorConfiguration().getServiceFinishedTailLog(), task.getRequest().getGroup(), maybeS3StorageClass, maybeApplyAfterBytes);
try {
bldr.setData(ByteString.copyFromUtf8(objectMapper.writeValueAsString(executorData)));
} catch (JsonProcessingException e) {
LOG.warn("Unable to process executor data {} for task {} as json (trying as string)", executorData, taskId.getId(), e);
bldr.setData(ByteString.copyFromUtf8(executorData.toString()));
}
} else if (task.getDeploy().getCommand().isPresent()) {
bldr.setData(ByteString.copyFromUtf8(task.getDeploy().getCommand().get()));
}
}
use of org.apache.mesos.v1.Protos.TaskInfo in project Singularity by HubSpot.
the class SingularityOfferHolder method launchTasksAndGetUnusedOffers.
public List<Offer> launchTasksAndGetUnusedOffers(SingularityMesosSchedulerClient schedulerClient) {
final List<TaskInfo> toLaunch = Lists.newArrayListWithCapacity(acceptedTasks.size());
final List<SingularityTaskId> taskIds = Lists.newArrayListWithCapacity(acceptedTasks.size());
for (SingularityMesosTaskHolder taskHolder : acceptedTasks) {
taskIds.add(taskHolder.getTask().getTaskId());
toLaunch.add(taskHolder.getMesosTask());
LOG.debug("Launching {} with offer {}", taskHolder.getTask().getTaskId(), offers.get(0).getId());
LOG.trace("Launching {} mesos task: {}", taskHolder.getTask().getTaskId(), MesosUtils.formatForLogging(taskHolder.getMesosTask()));
}
// At this point, `currentResources` contains a list of unused resources, because we subtracted out the required resources of every task we accepted.
// Let's try and reclaim offers by trying to pull each offer's list of resources out of the combined pool of leftover resources.
// n.b., This is currently not optimal. We just look through the offers in this instance and try to reclaim them with no particular priority or order.
Map<Boolean, List<Offer>> partitionedOffers = offers.stream().collect(Collectors.partitioningBy(offer -> {
List<Long> ports = MesosUtils.getAllPorts(offer.getResourcesList());
boolean offerCanBeReclaimedFromUnusedResources = offer.getResourcesList().stream().collect(Collectors.groupingBy(Resource::getRole)).entrySet().stream().map((entry) -> {
// Now, for each set of offer Resources grouped by role...
String role = entry.getKey();
List<Resource> offerResources = entry.getValue();
Optional<String> maybeRole = (!role.equals("") && !role.equals("*")) ? Optional.of(role) : Optional.absent();
// ...Check if we can pull the Resources belonging to this offer out of the pool of `currentResources`.
return MesosUtils.doesOfferMatchResources(maybeRole, MesosUtils.buildResourcesFromMesosResourceList(offerResources, maybeRole), currentResources, ports);
}).reduce(true, (x, y) -> x && y);
if (offerCanBeReclaimedFromUnusedResources) {
// We can reclaim this offer in its entirety! Pull all of its resources out of the combined pool for this SingularityOfferHolder instance.
LOG.trace("Able to reclaim offer {} from unused resources in OfferHolder from host {}. cpu: {}, mem: {}, disk: {}", offer.getId().getValue(), offer.getHostname(), MesosUtils.getNumCpus(offer), MesosUtils.getMemory(offer), MesosUtils.getDisk(offer));
currentResources = MesosUtils.subtractResources(currentResources, offer.getResourcesList());
}
return offerCanBeReclaimedFromUnusedResources;
}));
List<Offer> leftoverOffers = partitionedOffers.get(true);
List<Offer> neededOffers = partitionedOffers.get(false);
schedulerClient.accept(neededOffers.stream().map(Offer::getId).collect(Collectors.toList()), Collections.singletonList(Operation.newBuilder().setType(Type.LAUNCH).setLaunch(Launch.newBuilder().addAllTaskInfos(toLaunch).build()).build()));
LOG.info("{} tasks ({}) launched", taskIds.size(), taskIds);
return leftoverOffers;
}
Aggregations