use of com.mesosphere.sdk.specification.TaskSpec 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.TaskSpec 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.TaskSpec in project dcos-commons by mesosphere.
the class DefaultPlanScheduler method killTasks.
private void killTasks(PodInstanceRequirement podInstanceRequirement) {
Map<String, TaskInfo> taskInfoMap = new HashMap<>();
stateStore.fetchTasks().forEach(taskInfo -> taskInfoMap.put(taskInfo.getName(), taskInfo));
LOGGER.info("Killing tasks for pod instance requirement: {}:{}", podInstanceRequirement.getPodInstance().getName(), podInstanceRequirement.getTasksToLaunch());
List<TaskSpec> taskSpecsToLaunch = podInstanceRequirement.getPodInstance().getPod().getTasks().stream().filter(taskSpec -> podInstanceRequirement.getTasksToLaunch().contains(taskSpec.getName())).collect(Collectors.toList());
LOGGER.info("TaskSpecs to launch: {}", taskSpecsToLaunch.stream().map(taskSpec -> taskSpec.getName()).collect(Collectors.toList()));
List<String> resourceSetsToConsume = taskSpecsToLaunch.stream().map(taskSpec -> taskSpec.getResourceSet()).map(resourceSet -> resourceSet.getId()).collect(Collectors.toList());
LOGGER.info("Resource sets to consume: {}", podInstanceRequirement.getPodInstance().getName(), resourceSetsToConsume);
List<String> tasksToKill = podInstanceRequirement.getPodInstance().getPod().getTasks().stream().filter(taskSpec -> resourceSetsToConsume.contains(taskSpec.getResourceSet().getId())).map(taskSpec -> TaskSpec.getInstanceName(podInstanceRequirement.getPodInstance(), taskSpec)).collect(Collectors.toList());
LOGGER.info("Tasks to kill: {}", tasksToKill);
for (String taskName : tasksToKill) {
TaskInfo taskInfo = taskInfoMap.get(taskName);
if (taskInfo != null) {
Optional<Protos.TaskStatus> taskStatusOptional = stateStore.fetchStatus(taskInfo.getName());
Protos.TaskState state = Protos.TaskState.TASK_RUNNING;
if (taskStatusOptional.isPresent()) {
state = taskStatusOptional.get().getState();
}
if (!TaskUtils.isTerminal(state)) {
TaskKiller.killTask(taskInfo.getTaskId());
}
}
}
}
use of com.mesosphere.sdk.specification.TaskSpec 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;
}
use of com.mesosphere.sdk.specification.TaskSpec in project dcos-commons by mesosphere.
the class TLSEvaluationStage method evaluate.
@Override
public EvaluationOutcome evaluate(MesosResourcePool mesosResourcePool, PodInfoBuilder podInfoBuilder) {
TaskSpec taskSpec = podInfoBuilder.getPodInstance().getPod().getTasks().stream().filter(task -> task.getName().equals(taskName)).findFirst().get();
if (taskSpec.getTransportEncryption().isEmpty()) {
return EvaluationOutcome.pass(this, "No TLS specs found for task").build();
}
CertificateNamesGenerator certificateNamesGenerator = new CertificateNamesGenerator(serviceName, taskSpec, podInfoBuilder.getPodInstance(), schedulerConfig);
TLSArtifactPaths tlsArtifactPaths = new TLSArtifactPaths(namespace, TaskSpec.getInstanceName(podInfoBuilder.getPodInstance(), taskName), certificateNamesGenerator.getSANsHash());
for (TransportEncryptionSpec transportEncryptionSpec : taskSpec.getTransportEncryption()) {
try {
tlsArtifactsUpdater.update(tlsArtifactPaths, certificateNamesGenerator, transportEncryptionSpec.getName());
} catch (Exception e) {
logger.error(String.format("Failed to process certificates for %s", taskName), e);
return EvaluationOutcome.fail(this, "Failed to store TLS artifacts for task %s because of exception: %s", taskName, e).build();
}
// Share keys to the task container
podInfoBuilder.getTaskBuilder(taskName).getContainerBuilder().addAllVolumes(getExecutorInfoSecretVolumes(transportEncryptionSpec, tlsArtifactPaths));
}
return EvaluationOutcome.pass(this, "TLS certificate created and added to the task").build();
}
Aggregations