Search in sources :

Example 1 with TaskSpec

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();
}
Also used : Protos(org.apache.mesos.Protos) Arrays(java.util.Arrays) SchedulerDriver(org.apache.mesos.SchedulerDriver) TestConstants(com.mesosphere.sdk.testutils.TestConstants) TaskSpec(com.mesosphere.sdk.specification.TaskSpec) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) PodSpec(com.mesosphere.sdk.specification.PodSpec) List(java.util.List) VolumeSpec(com.mesosphere.sdk.specification.VolumeSpec) Scheduler(org.apache.mesos.Scheduler) ResourceSpec(com.mesosphere.sdk.specification.ResourceSpec) Optional(java.util.Optional) Constants(com.mesosphere.sdk.offer.Constants) VolumeSpec(com.mesosphere.sdk.specification.VolumeSpec) PodSpec(com.mesosphere.sdk.specification.PodSpec) Protos(org.apache.mesos.Protos) TaskSpec(com.mesosphere.sdk.specification.TaskSpec) ResourceSpec(com.mesosphere.sdk.specification.ResourceSpec)

Example 2 with TaskSpec

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());
}
Also used : Protos(org.apache.mesos.Protos) java.util(java.util) Logger(org.slf4j.Logger) TaskSpec(com.mesosphere.sdk.specification.TaskSpec) LoggerFactory(org.slf4j.LoggerFactory) StateStoreUtils(com.mesosphere.sdk.state.StateStoreUtils) RecoveryType(com.mesosphere.sdk.scheduler.recovery.RecoveryType) DefaultPodSpec(com.mesosphere.sdk.specification.DefaultPodSpec) RecoveryPlanOverrider(com.mesosphere.sdk.scheduler.recovery.RecoveryPlanOverrider) DefaultTaskSpec(com.mesosphere.sdk.specification.DefaultTaskSpec) Collectors(java.util.stream.Collectors) PodSpec(com.mesosphere.sdk.specification.PodSpec) CommandSpec(com.mesosphere.sdk.specification.CommandSpec) SerialStrategy(com.mesosphere.sdk.scheduler.plan.strategy.SerialStrategy) UnconstrainedLaunchConstrainer(com.mesosphere.sdk.scheduler.recovery.constrain.UnconstrainedLaunchConstrainer) StateStore(com.mesosphere.sdk.state.StateStore) com.mesosphere.sdk.scheduler.plan(com.mesosphere.sdk.scheduler.plan) RecoveryStep(com.mesosphere.sdk.scheduler.recovery.RecoveryStep) PodInstance(com.mesosphere.sdk.specification.PodInstance) DefaultCommandSpec(com.mesosphere.sdk.specification.DefaultCommandSpec) DefaultPodSpec(com.mesosphere.sdk.specification.DefaultPodSpec) PodSpec(com.mesosphere.sdk.specification.PodSpec) PodInstance(com.mesosphere.sdk.specification.PodInstance) TaskSpec(com.mesosphere.sdk.specification.TaskSpec) DefaultTaskSpec(com.mesosphere.sdk.specification.DefaultTaskSpec) RecoveryStep(com.mesosphere.sdk.scheduler.recovery.RecoveryStep) RecoveryStep(com.mesosphere.sdk.scheduler.recovery.RecoveryStep) CommandSpec(com.mesosphere.sdk.specification.CommandSpec) DefaultCommandSpec(com.mesosphere.sdk.specification.DefaultCommandSpec) UnconstrainedLaunchConstrainer(com.mesosphere.sdk.scheduler.recovery.constrain.UnconstrainedLaunchConstrainer) DefaultCommandSpec(com.mesosphere.sdk.specification.DefaultCommandSpec)

Example 3 with TaskSpec

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());
            }
        }
    }
}
Also used : TaskInfo(org.apache.mesos.Protos.TaskInfo) Protos(org.apache.mesos.Protos) java.util(java.util) com.mesosphere.sdk.offer(com.mesosphere.sdk.offer) Logger(org.slf4j.Logger) OfferEvaluator(com.mesosphere.sdk.offer.evaluate.OfferEvaluator) TaskSpec(com.mesosphere.sdk.specification.TaskSpec) TaskInfo(org.apache.mesos.Protos.TaskInfo) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) TaskKiller(com.mesosphere.sdk.framework.TaskKiller) StateStore(com.mesosphere.sdk.state.StateStore) OfferID(org.apache.mesos.Protos.OfferID) Offer(org.apache.mesos.Protos.Offer) Protos(org.apache.mesos.Protos) TaskSpec(com.mesosphere.sdk.specification.TaskSpec)

Example 4 with TaskSpec

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;
}
Also used : Protos(org.apache.mesos.Protos) IntStream(java.util.stream.IntStream) java.util(java.util) com.mesosphere.sdk.offer(com.mesosphere.sdk.offer) Logger(org.slf4j.Logger) TaskSpec(com.mesosphere.sdk.specification.TaskSpec) ResourceSpec(com.mesosphere.sdk.specification.ResourceSpec) com.mesosphere.sdk.offer.taskdata(com.mesosphere.sdk.offer.taskdata) TextFormat(com.google.protobuf.TextFormat) Collectors(java.util.stream.Collectors) DcosConstants(com.mesosphere.sdk.dcos.DcosConstants) PortSpec(com.mesosphere.sdk.specification.PortSpec) Protos(org.apache.mesos.Protos) TaskSpec(com.mesosphere.sdk.specification.TaskSpec) ResourceSpec(com.mesosphere.sdk.specification.ResourceSpec) PortSpec(com.mesosphere.sdk.specification.PortSpec)

Example 5 with TaskSpec

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();
}
Also used : TransportEncryptionSpec(com.mesosphere.sdk.specification.TransportEncryptionSpec) TaskSpec(com.mesosphere.sdk.specification.TaskSpec) IOException(java.io.IOException)

Aggregations

TaskSpec (com.mesosphere.sdk.specification.TaskSpec)5 Collectors (java.util.stream.Collectors)4 Protos (org.apache.mesos.Protos)4 java.util (java.util)3 Logger (org.slf4j.Logger)3 com.mesosphere.sdk.offer (com.mesosphere.sdk.offer)2 PodSpec (com.mesosphere.sdk.specification.PodSpec)2 ResourceSpec (com.mesosphere.sdk.specification.ResourceSpec)2 StateStore (com.mesosphere.sdk.state.StateStore)2 IOException (java.io.IOException)2 TextFormat (com.google.protobuf.TextFormat)1 DcosConstants (com.mesosphere.sdk.dcos.DcosConstants)1 TaskKiller (com.mesosphere.sdk.framework.TaskKiller)1 Constants (com.mesosphere.sdk.offer.Constants)1 OfferEvaluator (com.mesosphere.sdk.offer.evaluate.OfferEvaluator)1 com.mesosphere.sdk.offer.taskdata (com.mesosphere.sdk.offer.taskdata)1 com.mesosphere.sdk.scheduler.plan (com.mesosphere.sdk.scheduler.plan)1 SerialStrategy (com.mesosphere.sdk.scheduler.plan.strategy.SerialStrategy)1 RecoveryPlanOverrider (com.mesosphere.sdk.scheduler.recovery.RecoveryPlanOverrider)1 RecoveryStep (com.mesosphere.sdk.scheduler.recovery.RecoveryStep)1