use of org.apache.mesos.v1.Protos.Resource in project Singularity by HubSpot.
the class MesosUtils method subtractResources.
public static List<Resource> subtractResources(List<Resource> resources, List<Resource> subtract) {
List<Resource> remaining = Lists.newArrayListWithCapacity(resources.size());
for (Resource resource : resources) {
Optional<Resource> matched = getMatchingResource(resource, subtract);
if (!matched.isPresent()) {
remaining.add(resource.toBuilder().clone().build());
} else {
Resource.Builder resourceBuilder = resource.toBuilder().clone();
if (resource.hasScalar()) {
resourceBuilder.setScalar(resource.toBuilder().getScalarBuilder().setValue(resource.getScalar().getValue() - matched.get().getScalar().getValue()).build());
} else if (resource.hasRanges()) {
resourceBuilder.setRanges(subtractRanges(resource.getRanges(), matched.get().getRanges()));
} else {
throw new IllegalStateException(String.format("Can't subtract non-scalar or range resources %s", formatForLogging(resource)));
}
remaining.add(resourceBuilder.build());
}
}
return remaining;
}
use of org.apache.mesos.v1.Protos.Resource in project Singularity by HubSpot.
the class MesosUtils method combineResources.
public static List<Resource> combineResources(List<List<Resource>> resourcesList) {
List<Resource> resources = new ArrayList<>();
for (List<Resource> resourcesToAdd : resourcesList) {
for (Resource resource : resourcesToAdd) {
Optional<Resource> matched = getMatchingResource(resource, resources);
if (!matched.isPresent()) {
resources.add(resource);
} else {
int index = resources.indexOf(matched.get());
Resource.Builder resourceBuilder = resource.toBuilder().clone();
if (resource.hasScalar()) {
resourceBuilder.setScalar(resource.toBuilder().getScalarBuilder().setValue(resource.getScalar().getValue() + matched.get().getScalar().getValue()).build());
resources.set(index, resourceBuilder.build());
} else if (resource.hasRanges()) {
Ranges.Builder newRanges = Ranges.newBuilder();
resource.getRanges().getRangeList().forEach(newRanges::addRange);
matched.get().getRanges().getRangeList().forEach(newRanges::addRange);
resourceBuilder.setRanges(newRanges);
resources.set(index, resourceBuilder.build());
} else {
throw new IllegalStateException(String.format("Can't subtract non-scalar or range resources %s", formatForLogging(resource)));
}
}
}
}
return resources;
}
use of org.apache.mesos.v1.Protos.Resource in project Singularity by HubSpot.
the class MesosUtilsTest method test.
private void test(int numPorts, String... ranges) {
Resource resource = MesosUtils.getPortsResource(numPorts, buildOffer(ranges));
Assert.assertEquals(numPorts, MesosUtils.getNumPorts(Collections.singletonList(resource)));
}
use of org.apache.mesos.v1.Protos.Resource in project Singularity by HubSpot.
the class MesosUtilsTest method testGetZeroPortsFromResource.
@Test
public void testGetZeroPortsFromResource() {
String[] rangesOverlappingRequestPorts = { "23:28" };
int numPorts = 0;
List<Long> requestedPorts = Arrays.asList(25L, 27L);
Resource resource = MesosUtils.getPortsResource(numPorts, buildOffer(rangesOverlappingRequestPorts).getResourcesList(), requestedPorts);
Assert.assertEquals(0, MesosUtils.getPorts(resource, numPorts).length);
}
use of org.apache.mesos.v1.Protos.Resource 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);
}
Aggregations