use of com.mesosphere.sdk.specification.PodSpec in project dcos-commons by mesosphere.
the class OfferEvaluatorPlacementTest method testColocateAgents.
@Test
public void testColocateAgents() throws Exception {
Protos.Resource offeredCpu = ResourceTestUtils.getUnreservedCpus(2.0);
// Don't launch
PlacementRule placementRule = PlacementUtils.getAgentPlacementRule(Collections.emptyList(), Arrays.asList("some-random-agent")).get();
PodSpec podSpec = PodInstanceRequirementTestUtils.getCpuRequirement(1.0).getPodInstance().getPod();
podSpec = DefaultPodSpec.newBuilder(podSpec).placementRule(placementRule).build();
PodInstance podInstance = new DefaultPodInstance(podSpec, 0);
PodInstanceRequirement podInstanceRequirement = PodInstanceRequirement.newBuilder(podInstance, Arrays.asList(TestConstants.TASK_NAME)).build();
List<OfferRecommendation> recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(OfferTestUtils.getCompleteOffer(offeredCpu)));
Assert.assertEquals(0, recommendations.size());
// Launch
placementRule = PlacementUtils.getAgentPlacementRule(Collections.emptyList(), Arrays.asList(TestConstants.AGENT_ID.getValue())).get();
podSpec = DefaultPodSpec.newBuilder(podSpec).placementRule(placementRule).build();
podInstance = new DefaultPodInstance(podSpec, 0);
podInstanceRequirement = PodInstanceRequirement.newBuilder(podInstance, Arrays.asList(TestConstants.TASK_NAME)).build();
recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(OfferTestUtils.getCompleteOffer(offeredCpu)));
Assert.assertEquals(5, recommendations.size());
}
use of com.mesosphere.sdk.specification.PodSpec in project dcos-commons by mesosphere.
the class OfferEvaluatorPlacementTest method testAvoidAgents.
@Test
public void testAvoidAgents() throws Exception {
Protos.Resource offeredCpu = ResourceTestUtils.getUnreservedCpus(2.0);
// Don't launch
PlacementRule placementRule = PlacementUtils.getAgentPlacementRule(Arrays.asList(TestConstants.AGENT_ID.getValue()), Collections.emptyList()).get();
PodSpec podSpec = PodInstanceRequirementTestUtils.getCpuRequirement(1.0).getPodInstance().getPod();
podSpec = DefaultPodSpec.newBuilder(podSpec).placementRule(placementRule).build();
PodInstance podInstance = new DefaultPodInstance(podSpec, 0);
PodInstanceRequirement podInstanceRequirement = PodInstanceRequirement.newBuilder(podInstance, Arrays.asList(TestConstants.TASK_NAME)).build();
List<OfferRecommendation> recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(OfferTestUtils.getCompleteOffer(offeredCpu)));
Assert.assertEquals(0, recommendations.size());
// Launch
placementRule = PlacementUtils.getAgentPlacementRule(Arrays.asList("some-random-agent"), Collections.emptyList()).get();
podSpec = DefaultPodSpec.newBuilder(podSpec).placementRule(placementRule).build();
podInstance = new DefaultPodInstance(podSpec, 0);
podInstanceRequirement = PodInstanceRequirement.newBuilder(podInstance, Arrays.asList(TestConstants.TASK_NAME)).build();
recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(OfferTestUtils.getCompleteOffer(offeredCpu)));
Assert.assertEquals(5, recommendations.size());
}
use of com.mesosphere.sdk.specification.PodSpec 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.PodSpec in project dcos-commons by mesosphere.
the class CassandraRecoveryPlanOverrider method getNodeRecoveryPhase.
private Phase getNodeRecoveryPhase(Plan inputPlan, int index) {
Phase inputPhase = inputPlan.getChildren().get(0);
Step inputLaunchStep = inputPhase.getChildren().get(index);
// Dig all the way down into the command, so we can append the replace_address option to it.
PodInstance podInstance = inputLaunchStep.start().get().getPodInstance();
PodSpec podSpec = podInstance.getPod();
TaskSpec taskSpec = podSpec.getTasks().stream().filter(t -> t.getName().equals("server")).findFirst().get();
CommandSpec command = taskSpec.getCommand().get();
// Get IP address for the pre-existing node.
Optional<Protos.TaskStatus> status = StateStoreUtils.getTaskStatusFromProperty(stateStore, TaskSpec.getInstanceName(podInstance, taskSpec));
if (!status.isPresent()) {
logger.error("No previously stored TaskStatus to pull IP address from in Cassandra recovery");
return null;
}
String replaceIp = status.get().getContainerStatus().getNetworkInfos(0).getIpAddresses(0).getIpAddress();
DefaultCommandSpec.Builder builder = DefaultCommandSpec.newBuilder(command);
builder.value(String.format("%s -Dcassandra.replace_address=%s -Dcassandra.consistent.rangemovement=false%n", command.getValue().trim(), replaceIp));
// Rebuild a new PodSpec with the modified command, and add it to the phase we return.
TaskSpec newTaskSpec = DefaultTaskSpec.newBuilder(taskSpec).commandSpec(builder.build()).build();
List<TaskSpec> tasks = podSpec.getTasks().stream().map(t -> {
if (t.getName().equals(newTaskSpec.getName())) {
return newTaskSpec;
}
return t;
}).collect(Collectors.toList());
PodSpec newPodSpec = DefaultPodSpec.newBuilder(podSpec).tasks(tasks).build();
PodInstance newPodInstance = new DefaultPodInstance(newPodSpec, index);
PodInstanceRequirement replacePodInstanceRequirement = PodInstanceRequirement.newBuilder(newPodInstance, inputLaunchStep.getPodInstanceRequirement().get().getTasksToLaunch()).recoveryType(RecoveryType.PERMANENT).build();
Step replaceStep = new RecoveryStep(inputLaunchStep.getName(), replacePodInstanceRequirement, new UnconstrainedLaunchConstrainer(), stateStore);
List<Step> steps = new ArrayList<>();
steps.add(replaceStep);
// Restart all other nodes if replacing a seed node to refresh IP resolution
int replaceIndex = replaceStep.getPodInstanceRequirement().get().getPodInstance().getIndex();
if (CassandraSeedUtils.isSeedNode(replaceIndex)) {
logger.info("Scheduling restart of all nodes other than 'node-{}' to refresh seed node address.", replaceIndex);
List<Step> restartSteps = inputPhase.getChildren().stream().filter(step -> step.getPodInstanceRequirement().get().getPodInstance().getIndex() != replaceIndex).map(step -> {
PodInstanceRequirement restartPodInstanceRequirement = PodInstanceRequirement.newBuilder(step.getPodInstanceRequirement().get().getPodInstance(), step.getPodInstanceRequirement().get().getTasksToLaunch()).recoveryType(RecoveryType.TRANSIENT).build();
return new RecoveryStep(step.getName(), restartPodInstanceRequirement, new UnconstrainedLaunchConstrainer(), stateStore);
}).collect(Collectors.toList());
steps.addAll(restartSteps);
}
return new DefaultPhase(RECOVERY_PHASE_NAME, steps, new SerialStrategy<>(), Collections.emptyList());
}
use of com.mesosphere.sdk.specification.PodSpec in project dcos-commons by mesosphere.
the class ConfigurationUpdaterTest method testPodsUsersUnsetInPreviousServiceSpecButSetToRootInNewServiceSpec.
@Test
public void testPodsUsersUnsetInPreviousServiceSpecButSetToRootInNewServiceSpec() throws ConfigStoreException {
final ConfigurationUpdater<ServiceSpec> configurationUpdater = new DefaultConfigurationUpdater(mockStateStore, mockConfigStore, DefaultServiceSpec.getComparatorInstance(), DefaultConfigValidators.getValidators(SchedulerConfigTestUtils.getTestSchedulerConfig()));
when(mockConfigStore.getTargetConfig()).thenReturn(TARGET_ID);
DefaultServiceSpec.Builder serviceSpecWithUser = DefaultServiceSpec.newBuilder(ORIGINAL_SERVICE_SPECIFICATION).user(DcosConstants.DEFAULT_SERVICE_USER);
List<PodSpec> podsWithoutUsers = new ArrayList<>();
for (PodSpec podSpec : ORIGINAL_SERVICE_SPECIFICATION.getPods()) {
podsWithoutUsers.add(DefaultPodSpec.newBuilder(podSpec).user(null).build());
}
serviceSpecWithUser.pods(podsWithoutUsers);
when(mockConfigStore.fetch(TARGET_ID)).thenReturn(serviceSpecWithUser.build());
// set the pod users for the new service spec to "root" to match the default of the non-set user from
// previous service spec
List<PodSpec> podsWithUsers = new ArrayList<>();
for (PodSpec podSpec : ORIGINAL_SERVICE_SPECIFICATION_WITH_USER.getPods()) {
podsWithUsers.add(DefaultPodSpec.newBuilder(podSpec).user(DcosConstants.DEFAULT_SERVICE_USER).build());
}
ServiceSpec SERVICE_SPECIFICATION_WITH_USER = DefaultServiceSpec.newBuilder(ORIGINAL_SERVICE_SPECIFICATION_WITH_USER).pods(podsWithUsers).build();
ConfigurationUpdater.UpdateResult result = configurationUpdater.updateConfiguration(SERVICE_SPECIFICATION_WITH_USER);
Assert.assertEquals(TARGET_ID, result.getTargetId());
Assert.assertEquals(0, result.getErrors().size());
}
Aggregations