Search in sources :

Example 1 with Resource

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;
}
Also used : Resource(org.apache.mesos.v1.Protos.Resource)

Example 2 with Resource

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;
}
Also used : Resource(org.apache.mesos.v1.Protos.Resource) ArrayList(java.util.ArrayList)

Example 3 with Resource

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)));
}
Also used : Resource(org.apache.mesos.v1.Protos.Resource)

Example 4 with 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);
}
Also used : Resource(org.apache.mesos.v1.Protos.Resource) Test(org.junit.Test)

Example 5 with Resource

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);
}
Also used : SingularityTask(com.hubspot.singularity.SingularityTask) ExecutorInfo(org.apache.mesos.v1.Protos.ExecutorInfo) Parameter(org.apache.mesos.v1.Protos.Parameter) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) Labels(org.apache.mesos.v1.Protos.Labels) SingularityContainerInfo(com.hubspot.mesos.SingularityContainerInfo) SingularityMesosImage(com.hubspot.mesos.SingularityMesosImage) CommandInfo(org.apache.mesos.v1.Protos.CommandInfo) Optional(com.google.common.base.Optional) Map(java.util.Map) NetworkInfo(org.apache.mesos.v1.Protos.NetworkInfo) SingularityDockerInfo(com.hubspot.mesos.SingularityDockerInfo) MesosProtosUtils(com.hubspot.singularity.helpers.MesosProtosUtils) Variable(org.apache.mesos.v1.Protos.Environment.Variable) Resources(com.hubspot.mesos.Resources) SingularityDockerNetworkType(com.hubspot.mesos.SingularityDockerNetworkType) SingularityPortMapping(com.hubspot.mesos.SingularityPortMapping) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) SingularityDockerVolume(com.hubspot.mesos.SingularityDockerVolume) SingularityVolumeSource(com.hubspot.mesos.SingularityVolumeSource) Collectors(java.util.stream.Collectors) MesosInfo(org.apache.mesos.v1.Protos.ContainerInfo.MesosInfo) ExecutorID(org.apache.mesos.v1.Protos.ExecutorID) ExecutorDataBuilder(com.hubspot.deploy.ExecutorDataBuilder) ByteString(com.google.protobuf.ByteString) List(java.util.List) MesosUtils(com.hubspot.singularity.helpers.MesosUtils) SingularityDockerImage(com.hubspot.mesos.SingularityDockerImage) SingularityMesosInfo(com.hubspot.mesos.SingularityMesosInfo) Entry(java.util.Map.Entry) ContainerInfo(org.apache.mesos.v1.Protos.ContainerInfo) Image(org.apache.mesos.v1.Protos.Image) DockerInfo(org.apache.mesos.v1.Protos.ContainerInfo.DockerInfo) Supplier(com.google.common.base.Supplier) HashMap(java.util.HashMap) SingularityTaskExecutorData(com.hubspot.singularity.SingularityTaskExecutorData) Singleton(javax.inject.Singleton) ArrayList(java.util.ArrayList) Resource(org.apache.mesos.v1.Protos.Resource) Strings(com.google.common.base.Strings) SingularityDockerPortMapping(com.hubspot.mesos.SingularityDockerPortMapping) SingularityS3UploaderFile(com.hubspot.singularity.SingularityS3UploaderFile) ImmutableList(com.google.common.collect.ImmutableList) ExecutorIdGenerator(com.hubspot.singularity.data.ExecutorIdGenerator) Volume(org.apache.mesos.v1.Protos.Volume) URI(org.apache.mesos.v1.Protos.CommandInfo.URI) SingularityAppcImage(com.hubspot.mesos.SingularityAppcImage) SingularityConfiguration(com.hubspot.singularity.config.SingularityConfiguration) SingularityNetworkInfo(com.hubspot.mesos.SingularityNetworkInfo) Logger(org.slf4j.Logger) Parameters(org.apache.mesos.v1.Protos.Parameters) SingularityDockerParameter(com.hubspot.mesos.SingularityDockerParameter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Builder(org.apache.mesos.v1.Protos.Labels.Builder) Ints(com.google.common.primitives.Ints) SingularityMesosTaskHolder(com.hubspot.singularity.helpers.SingularityMesosTaskHolder) Environment(org.apache.mesos.v1.Protos.Environment) SingularityMesosArtifact(com.hubspot.mesos.SingularityMesosArtifact) SingularityTaskRequest(com.hubspot.singularity.SingularityTaskRequest) SingularityVolume(com.hubspot.mesos.SingularityVolume) TaskID(org.apache.mesos.v1.Protos.TaskID) TaskInfo(org.apache.mesos.v1.Protos.TaskInfo) Collections(java.util.Collections) SingularityMesosTaskLabel(com.hubspot.mesos.SingularityMesosTaskLabel) Label(org.apache.mesos.v1.Protos.Label) ExecutorDataBuilder(com.hubspot.deploy.ExecutorDataBuilder) Builder(org.apache.mesos.v1.Protos.Labels.Builder) Resource(org.apache.mesos.v1.Protos.Resource) ArrayList(java.util.ArrayList) SingularityMesosTaskLabel(com.hubspot.mesos.SingularityMesosTaskLabel) Label(org.apache.mesos.v1.Protos.Label) SingularityMesosTaskHolder(com.hubspot.singularity.helpers.SingularityMesosTaskHolder) ByteString(com.google.protobuf.ByteString) TaskInfo(org.apache.mesos.v1.Protos.TaskInfo) SingularityContainerInfo(com.hubspot.mesos.SingularityContainerInfo) SingularityTask(com.hubspot.singularity.SingularityTask) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) SingularityMesosTaskLabel(com.hubspot.mesos.SingularityMesosTaskLabel)

Aggregations

Resource (org.apache.mesos.v1.Protos.Resource)7 ArrayList (java.util.ArrayList)3 Range (org.apache.mesos.v1.Protos.Value.Range)3 Optional (com.google.common.base.Optional)2 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)2 MesosUtils (com.hubspot.singularity.helpers.MesosUtils)2 SingularityMesosTaskHolder (com.hubspot.singularity.helpers.SingularityMesosTaskHolder)2 Collections (java.util.Collections)2 Ranges (org.apache.mesos.v1.Protos.Value.Ranges)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Strings (com.google.common.base.Strings)1 Supplier (com.google.common.base.Supplier)1 ImmutableList (com.google.common.collect.ImmutableList)1 Lists (com.google.common.collect.Lists)1 Ints (com.google.common.primitives.Ints)1 Inject (com.google.inject.Inject)1 ByteString (com.google.protobuf.ByteString)1 ExecutorDataBuilder (com.hubspot.deploy.ExecutorDataBuilder)1 JavaUtils (com.hubspot.mesos.JavaUtils)1