use of com.mesosphere.sdk.specification.ResourceSpec in project dcos-commons by mesosphere.
the class SendOffer method getOfferForPod.
private Protos.Offer getOfferForPod(ClusterState state) {
Optional<PodSpec> matchingSpec = state.getServiceSpec().getPods().stream().filter(podSpec -> podType.equals(podSpec.getType())).findAny();
if (!matchingSpec.isPresent()) {
throw new IllegalArgumentException(String.format("No PodSpec found with type=%s: types=%s", podType, state.getServiceSpec().getPods().stream().map(podSpec -> podSpec.getType()).collect(Collectors.toList())));
}
PodSpec podSpec = matchingSpec.get();
Protos.Offer.Builder offerBuilder = Protos.Offer.newBuilder().setFrameworkId(TestConstants.FRAMEWORK_ID).setSlaveId(TestConstants.AGENT_ID).setHostname(hostname);
offerBuilder.getIdBuilder().setValue(UUID.randomUUID().toString());
// Include pod/executor-level volumes:
for (VolumeSpec volumeSpec : podSpec.getVolumes()) {
offerBuilder.addResources(toUnreservedResource(volumeSpec));
}
// Include task-level resources (note: resources are not merged, e.g. 1.5cpu+1.0 cpu instead of 2.5cpu):
for (TaskSpec taskSpec : podSpec.getTasks()) {
if (podToReuse.isPresent()) {
// Copy executor id and resources from prior pod launch:
LaunchedPod pod = state.getLastLaunchedPod(podToReuse.get());
offerBuilder.addExecutorIds(pod.getExecutor().getExecutorId()).addAllResources(pod.getExecutor().getResourcesList());
for (Protos.TaskInfo task : pod.getTasks()) {
offerBuilder.addAllResources(task.getResourcesList());
}
} else {
// Create new unreserved resources:
for (ResourceSpec resourceSpec : taskSpec.getResourceSet().getResources()) {
offerBuilder.addResources(toUnreservedResource(resourceSpec));
}
for (VolumeSpec volumeSpec : taskSpec.getResourceSet().getVolumes()) {
offerBuilder.addResources(toUnreservedResource(volumeSpec));
}
}
}
// just ignored.
if (!podToReuse.isPresent()) {
offerBuilder.addAllResources(DEFAULT_EXECUTOR_RESOURCES);
}
return offerBuilder.build();
}
use of com.mesosphere.sdk.specification.ResourceSpec in project dcos-commons by mesosphere.
the class ExecutorResourceMapper method findMatchingDiskSpec.
private Optional<ResourceLabels> findMatchingDiskSpec(Protos.Resource executorResource, Collection<ResourceSpec> resourceSpecs) {
for (ResourceSpec resourceSpec : resourceSpecs) {
if (!(resourceSpec instanceof VolumeSpec)) {
continue;
}
if (executorResource.getDisk().getVolume().getContainerPath().equals(((VolumeSpec) resourceSpec).getContainerPath())) {
Optional<String> resourceId = ResourceUtils.getResourceId(executorResource);
if (!resourceId.isPresent()) {
logger.error("Failed to find resource ID for resource: {}", executorResource);
continue;
}
double diskSize = executorResource.getScalar().getValue();
VolumeSpec updatedSpec = OfferEvaluationUtils.updateVolumeSpec((VolumeSpec) resourceSpec, diskSize);
return Optional.of(new ResourceLabels(resourceSpec, updatedSpec, resourceId.get(), Optional.of(executorResource.getDisk().getPersistence().getId()), ResourceUtils.getSourceRoot(executorResource)));
}
}
return Optional.empty();
}
use of com.mesosphere.sdk.specification.ResourceSpec in project dcos-commons by mesosphere.
the class OfferEvaluationUtilsTest method testResourceDecrease.
private void testResourceDecrease(Optional<String> namespace) {
String resourceId = UUID.randomUUID().toString();
Protos.Value current = getValue(5);
Protos.Value desired = getValue(4);
Protos.Value toSubtract = ValueUtils.subtract(current, desired);
ResourceSpec resourceSpec = getResourceSpec(desired);
when(mockPool.consumeReserved(RESOURCE_NAME, desired, resourceId)).thenReturn(Optional.of(getMesosResource(getResourceSpec(current), resourceId, namespace)));
when(mockPool.consumeReservableMerged(RESOURCE_NAME, desired, Constants.ANY_ROLE)).thenReturn(Optional.of(getMesosResource(toSubtract)));
ReserveEvaluationOutcome outcome = OfferEvaluationUtils.evaluateSimpleResource(mockStage, resourceSpec, Optional.of(resourceId), namespace, mockPool);
Assert.assertTrue(outcome.getEvaluationOutcome().isPassing());
OfferRecommendation recommendation = outcome.getEvaluationOutcome().getOfferRecommendations().get(0);
Assert.assertTrue(recommendation instanceof UnreserveOfferRecommendation);
Assert.assertTrue(outcome.getResourceId().isPresent());
Protos.Resource resource = recommendation.getOperation().getUnreserve().getResources(0);
Assert.assertEquals(toSubtract.getScalar(), resource.getScalar());
if (namespace.isPresent()) {
Assert.assertEquals(namespace.get(), ResourceUtils.getNamespace(resource).get());
} else {
Assert.assertFalse(ResourceUtils.getNamespace(resource).isPresent());
}
}
use of com.mesosphere.sdk.specification.ResourceSpec in project dcos-commons by mesosphere.
the class OfferEvaluationUtilsTest method testResourceIncreaseSufficient.
private void testResourceIncreaseSufficient(Optional<String> namespace) {
String resourceId = UUID.randomUUID().toString();
Protos.Value current = getValue(4);
Protos.Value desired = getValue(5);
Protos.Value toAdd = ValueUtils.subtract(desired, current);
ResourceSpec resourceSpec = getResourceSpec(desired);
when(mockPool.consumeReserved(RESOURCE_NAME, desired, resourceId)).thenReturn(Optional.of(getMesosResource(getResourceSpec(current), resourceId, namespace)));
when(mockPool.consumeReservableMerged(RESOURCE_NAME, toAdd, Constants.ANY_ROLE)).thenReturn(Optional.of(getMesosResource(toAdd)));
ReserveEvaluationOutcome outcome = OfferEvaluationUtils.evaluateSimpleResource(mockStage, resourceSpec, Optional.of(resourceId), namespace, mockPool);
Assert.assertTrue(outcome.getEvaluationOutcome().isPassing());
OfferRecommendation recommendation = outcome.getEvaluationOutcome().getOfferRecommendations().get(0);
Assert.assertTrue(recommendation instanceof ReserveOfferRecommendation);
Assert.assertTrue(outcome.getResourceId().isPresent());
Protos.Resource resource = recommendation.getOperation().getReserve().getResources(0);
Assert.assertEquals(toAdd.getScalar(), resource.getScalar());
if (namespace.isPresent()) {
Assert.assertEquals(namespace.get(), ResourceUtils.getNamespace(resource).get());
} else {
Assert.assertFalse(ResourceUtils.getNamespace(resource).isPresent());
}
}
use of com.mesosphere.sdk.specification.ResourceSpec in project dcos-commons by mesosphere.
the class PortEvaluationStage method selectDynamicPort.
private static Optional<Integer> selectDynamicPort(MesosResourcePool mesosResourcePool, PodInfoBuilder podInfoBuilder) {
Set<Integer> consumedPorts = new HashSet<>();
// compile a list of those to check against the offered ports.
for (TaskSpec task : podInfoBuilder.getPodInstance().getPod().getTasks()) {
for (ResourceSpec resourceSpec : task.getResourceSet().getResources()) {
if (resourceSpec instanceof PortSpec) {
PortSpec portSpec = (PortSpec) resourceSpec;
if (portSpec.getPort() != 0) {
consumedPorts.add((int) portSpec.getPort());
}
}
}
}
// Also check other dynamically allocated ports which had been taken by earlier stages of this evaluation round.
for (Protos.Resource.Builder resourceBuilder : podInfoBuilder.getTaskResourceBuilders()) {
consumedPorts.addAll(getPortsInResource(resourceBuilder.build()));
}
for (Protos.Resource.Builder resourceBuilder : podInfoBuilder.getExecutorResourceBuilders()) {
consumedPorts.addAll(getPortsInResource(resourceBuilder.build()));
}
Protos.Value availablePorts = mesosResourcePool.getUnreservedMergedPool().get(Constants.PORTS_RESOURCE_TYPE);
Optional<Integer> dynamicPort = Optional.empty();
if (availablePorts != null) {
dynamicPort = availablePorts.getRanges().getRangeList().stream().flatMap(r -> IntStream.rangeClosed((int) r.getBegin(), (int) r.getEnd()).boxed()).filter(p -> !consumedPorts.contains(p)).findFirst();
}
return dynamicPort;
}
Aggregations