Search in sources :

Example 6 with TaskLabelReader

use of com.mesosphere.sdk.offer.taskdata.TaskLabelReader in project dcos-commons by mesosphere.

the class ClusterState method getLastLaunchedPod.

/**
 * Returns the last pod to be launched with the specified name.
 *
 * @param podName name+index of the pod, of the form "podtype-#"
 * @return a list of tasks which were included in the pod
 * @throws IllegalStateException if no such pod was found
 * @see #getLastLaunchedPod()
 */
public LaunchedPod getLastLaunchedPod(String podName) {
    Set<String> allPodNames = new TreeSet<>();
    LaunchedPod foundPod = null;
    for (LaunchedPod pod : createdPods) {
        // Sample pod info from the first task. All tasks should share the same pod info:
        final Protos.TaskInfo task = pod.getTasks().iterator().next();
        final TaskLabelReader reader = new TaskLabelReader(task);
        final String thisPod;
        try {
            thisPod = PodInstance.getName(reader.getType(), reader.getIndex());
        } catch (TaskException e) {
            throw new IllegalStateException("Unable to extract pod from task " + task.getName(), e);
        }
        allPodNames.add(thisPod);
        if (thisPod.equals(podName)) {
            foundPod = pod;
        // Don't break: want to collect the most recent version
        }
    }
    if (foundPod == null) {
        throw new IllegalStateException(String.format("Unable to find pod named %s. Available pods were: %s", podName, allPodNames));
    }
    return foundPod;
}
Also used : TaskLabelReader(com.mesosphere.sdk.offer.taskdata.TaskLabelReader) TaskException(com.mesosphere.sdk.offer.TaskException) Protos(org.apache.mesos.Protos) TreeSet(java.util.TreeSet)

Example 7 with TaskLabelReader

use of com.mesosphere.sdk.offer.taskdata.TaskLabelReader in project dcos-commons by mesosphere.

the class DecommissionPlanFactory method getPodsToDecommission.

/**
 * Returns a mapping of pods to be decommissioned with affected tasks within those pods. The returned mapping will
 * be in the order that the pods should be decommissioned.
 */
@VisibleForTesting
static SortedMap<PodKey, Collection<Protos.TaskInfo>> getPodsToDecommission(ServiceSpec serviceSpec, Collection<Protos.TaskInfo> tasks) {
    // If multiple pod types are being decommissioned, they should be decommissioned in the reverse of the order
    // that they're declared in the ServiceSpec (opposite direction of default deployment)
    List<String> orderedPodTypes = serviceSpec.getPods().stream().map(PodSpec::getType).collect(Collectors.toList());
    Collections.reverse(orderedPodTypes);
    Map<String, Integer> expectedPodCounts = serviceSpec.getPods().stream().collect(Collectors.toMap(PodSpec::getType, PodSpec::getCount));
    LOGGER.info("Expected pod counts: {}", expectedPodCounts);
    SortedMap<PodKey, Collection<Protos.TaskInfo>> podsToDecommission = new TreeMap<>();
    for (Protos.TaskInfo task : tasks) {
        final PodKey podKey;
        try {
            TaskLabelReader labelReader = new TaskLabelReader(task);
            podKey = new PodKey(labelReader.getType(), labelReader.getIndex(), orderedPodTypes);
        } catch (TaskException e) {
            LOGGER.error(String.format("Failed to retrieve task metadata. Omitting task from decommission: %s", task.getName()), e);
            continue;
        }
        Integer expectedPodCount = expectedPodCounts.get(podKey.podType);
        if (expectedPodCount == null) {
            LOGGER.info("Scheduling '{}' for decommission: '{}' is not present in service spec: {}", task.getName(), podKey.podType, expectedPodCounts.keySet());
        } else if (podKey.podIndex >= expectedPodCount) {
            LOGGER.info("Scheduling '{}' for decommission: '{}' exceeds desired pod count {}", task.getName(), podKey.getPodName(), expectedPodCount);
        } else {
            // Do nothing
            continue;
        }
        Collection<Protos.TaskInfo> podTasks = podsToDecommission.get(podKey);
        if (podTasks == null) {
            podTasks = new ArrayList<>();
            podsToDecommission.put(podKey, podTasks);
        }
        podTasks.add(task);
    }
    LOGGER.info("Pods scheduled for decommission: {}", podsToDecommission.keySet());
    return podsToDecommission;
}
Also used : TaskLabelReader(com.mesosphere.sdk.offer.taskdata.TaskLabelReader) TaskException(com.mesosphere.sdk.offer.TaskException) Protos(org.apache.mesos.Protos) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 8 with TaskLabelReader

use of com.mesosphere.sdk.offer.taskdata.TaskLabelReader in project dcos-commons by mesosphere.

the class DefaultStepFactory method hasReachedGoalState.

@VisibleForTesting
protected boolean hasReachedGoalState(PodInstance podInstance, Protos.TaskInfo taskInfo) throws TaskException {
    GoalState goalState = TaskUtils.getGoalState(podInstance, taskInfo.getName());
    Optional<Protos.TaskStatus> status = stateStore.fetchStatus(taskInfo.getName());
    if (!status.isPresent()) {
        return false;
    }
    if (goalState.equals(GoalState.RUNNING)) {
        switch(status.get().getState()) {
            case TASK_RUNNING:
                if (Capabilities.getInstance().supportsDefaultExecutor()) {
                    return new TaskLabelReader(taskInfo).isReadinessCheckSucceeded(status.get());
                }
                // readiness checks on restart.
                return true;
            default:
                return false;
        }
    } else if (goalState.equals(GoalState.ONCE) || goalState.equals(GoalState.FINISH) || goalState.equals(GoalState.FINISHED)) {
        switch(status.get().getState()) {
            case TASK_FINISHED:
                return true;
            default:
                return false;
        }
    } else {
        throw new TaskException("Unexpected goal state encountered: " + goalState);
    }
}
Also used : GoalState(com.mesosphere.sdk.specification.GoalState) TaskLabelReader(com.mesosphere.sdk.offer.taskdata.TaskLabelReader) TaskException(com.mesosphere.sdk.offer.TaskException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 9 with TaskLabelReader

use of com.mesosphere.sdk.offer.taskdata.TaskLabelReader in project dcos-commons by mesosphere.

the class DeploymentStep method update.

/**
 * Synchronized to ensure consistency between this and {@link #updateOfferStatus(Collection)}.
 */
@Override
public synchronized void update(Protos.TaskStatus status) {
    logger.debug("Step {} received status: {}", getName(), TextFormat.shortDebugString(status));
    if (!tasks.containsKey(status.getTaskId())) {
        logger.debug("Step {} ignoring irrelevant TaskStatus: {}", getName(), TextFormat.shortDebugString(status));
        return;
    }
    if (isComplete()) {
        logger.debug("Step {} ignoring TaskStatus due to being Complete: {}", getName(), TextFormat.shortDebugString(status));
        return;
    }
    GoalState goalState = null;
    try {
        goalState = TaskUtils.getGoalState(podInstanceRequirement.getPodInstance(), CommonIdUtils.toTaskName(status.getTaskId()));
    } catch (TaskException e) {
        logger.error(String.format("Failed to get goal state for step %s with status %s", getName(), getStatus()), e);
        return;
    }
    logger.info("Goal state for: {} is: {}", status.getTaskId().getValue(), goalState.name());
    switch(status.getState()) {
        case TASK_ERROR:
        case TASK_FAILED:
        case TASK_KILLED:
        case TASK_KILLING:
        case TASK_LOST:
            setTaskStatus(status.getTaskId(), Status.PENDING);
            break;
        case TASK_STAGING:
        case TASK_STARTING:
            setTaskStatus(status.getTaskId(), Status.STARTING);
            break;
        case TASK_RUNNING:
            Protos.TaskInfo taskInfo = tasks.get(status.getTaskId()).getTaskInfo();
            if (goalState.equals(GoalState.RUNNING) && new TaskLabelReader(taskInfo).isReadinessCheckSucceeded(status)) {
                setTaskStatus(status.getTaskId(), Status.COMPLETE);
            } else {
                setTaskStatus(status.getTaskId(), Status.STARTED);
            }
            break;
        case TASK_FINISHED:
            if (goalState.equals(GoalState.ONCE) || goalState.equals(GoalState.FINISH) || goalState.equals(GoalState.FINISHED)) {
                setTaskStatus(status.getTaskId(), Status.COMPLETE);
            } else {
                setTaskStatus(status.getTaskId(), Status.PENDING);
            }
            break;
        default:
            logger.error("Failed to process unexpected state: " + status.getState());
    }
    updateStatus();
}
Also used : GoalState(com.mesosphere.sdk.specification.GoalState) TaskLabelReader(com.mesosphere.sdk.offer.taskdata.TaskLabelReader) Protos(org.apache.mesos.Protos) GoalStateOverride(com.mesosphere.sdk.state.GoalStateOverride)

Example 10 with TaskLabelReader

use of com.mesosphere.sdk.offer.taskdata.TaskLabelReader in project dcos-commons by mesosphere.

the class RoundRobinByHostnameRuleTest method getPodInstance.

private static PodInstance getPodInstance(TaskInfo taskInfo) {
    try {
        TaskLabelReader labels = new TaskLabelReader(taskInfo);
        ResourceSet resourceSet = PodInstanceRequirementTestUtils.getCpuResourceSet(1.0);
        PodSpec podSpec = PodInstanceRequirementTestUtils.getRequirement(resourceSet, labels.getType(), labels.getIndex()).getPodInstance().getPod();
        return new DefaultPodInstance(podSpec, labels.getIndex());
    } catch (Exception e) {
        throw new IllegalStateException(e);
    }
}
Also used : TaskLabelReader(com.mesosphere.sdk.offer.taskdata.TaskLabelReader) PodSpec(com.mesosphere.sdk.specification.PodSpec) ResourceSet(com.mesosphere.sdk.specification.ResourceSet) DefaultPodInstance(com.mesosphere.sdk.scheduler.plan.DefaultPodInstance) InvalidRequirementException(com.mesosphere.sdk.offer.InvalidRequirementException) TaskException(com.mesosphere.sdk.offer.TaskException)

Aggregations

TaskLabelReader (com.mesosphere.sdk.offer.taskdata.TaskLabelReader)13 TaskException (com.mesosphere.sdk.offer.TaskException)8 Protos (org.apache.mesos.Protos)5 PodSpec (com.mesosphere.sdk.specification.PodSpec)4 InvalidRequirementException (com.mesosphere.sdk.offer.InvalidRequirementException)3 DefaultPodInstance (com.mesosphere.sdk.scheduler.plan.DefaultPodInstance)3 ResourceSet (com.mesosphere.sdk.specification.ResourceSet)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 DefaultPodSpec (com.mesosphere.sdk.specification.DefaultPodSpec)2 GoalState (com.mesosphere.sdk.specification.GoalState)2 ConfigStoreException (com.mesosphere.sdk.state.ConfigStoreException)2 TaskInfo (org.apache.mesos.Protos.TaskInfo)2 TaskLabelWriter (com.mesosphere.sdk.offer.taskdata.TaskLabelWriter)1 DefaultServiceSpec (com.mesosphere.sdk.specification.DefaultServiceSpec)1 ServiceSpec (com.mesosphere.sdk.specification.ServiceSpec)1 GoalStateOverride (com.mesosphere.sdk.state.GoalStateOverride)1 IOException (java.io.IOException)1 TreeSet (java.util.TreeSet)1 DiscoveryInfo (org.apache.mesos.Protos.DiscoveryInfo)1 Operation (org.apache.mesos.Protos.Offer.Operation)1