Search in sources :

Example 1 with TaskInfoAndStatus

use of com.mesosphere.sdk.http.types.TaskInfoAndStatus in project dcos-commons by mesosphere.

the class PodQueries method getPodInstanceStatusJson.

/**
 * Returns a JSON object describing a pod instance and its tasks of the form:
 * <code>{
 *   "name": "pod-0",
 *   "tasks": [ {
 *     "id": "pod-0-server",
 *     "name": "server",
 *     "status": "RUNNING"
 *   }, ... ]
 * }</code>
 */
private static JSONObject getPodInstanceStatusJson(StateStore stateStore, String podInstanceName, Collection<TaskInfoAndStatus> tasks) {
    JSONObject jsonPod = new JSONObject();
    jsonPod.put("name", podInstanceName);
    for (TaskInfoAndStatus task : tasks) {
        JSONObject jsonTask = new JSONObject();
        jsonTask.put("id", task.getInfo().getTaskId().getValue());
        jsonTask.put("name", task.getInfo().getName());
        Optional<String> stateString = getTaskStateString(stateStore, task.getInfo().getName(), task.getStatus());
        if (stateString.isPresent()) {
            jsonTask.put("status", stateString.get());
        }
        jsonPod.append("tasks", jsonTask);
    }
    return jsonPod;
}
Also used : JSONObject(org.json.JSONObject) TaskInfoAndStatus(com.mesosphere.sdk.http.types.TaskInfoAndStatus)

Example 2 with TaskInfoAndStatus

use of com.mesosphere.sdk.http.types.TaskInfoAndStatus in project dcos-commons by mesosphere.

the class PodQueries method killTasks.

private static Response killTasks(String podName, Collection<TaskInfoAndStatus> tasksToKill, RecoveryType recoveryType) {
    for (TaskInfoAndStatus taskToKill : tasksToKill) {
        final Protos.TaskInfo taskInfo = taskToKill.getInfo();
        if (taskToKill.hasStatus()) {
            LOGGER.info("  {} ({}): currently has status {}", taskInfo.getName(), taskInfo.getTaskId().getValue(), taskToKill.getStatus().get().getState());
        } else {
            LOGGER.info("  {} ({}): no status available", taskInfo.getName(), taskInfo.getTaskId().getValue());
        }
        TaskKiller.killTask(taskInfo.getTaskId());
    }
    JSONObject json = new JSONObject();
    json.put("pod", podName);
    json.put("tasks", tasksToKill.stream().map(t -> t.getInfo().getName()).collect(Collectors.toList()));
    return jsonOkResponse(json);
}
Also used : TaskInfoAndStatus(com.mesosphere.sdk.http.types.TaskInfoAndStatus) JSONObject(org.json.JSONObject) Protos(org.apache.mesos.Protos)

Example 3 with TaskInfoAndStatus

use of com.mesosphere.sdk.http.types.TaskInfoAndStatus in project dcos-commons by mesosphere.

the class PodQueries method overrideGoalState.

private static Response overrideGoalState(StateStore stateStore, String podInstanceName, Set<String> taskNameFilter, GoalStateOverride override) {
    Optional<Collection<TaskInfoAndStatus>> allPodTasks = GroupedTasks.create(stateStore).getPodInstanceTasks(podInstanceName);
    if (!allPodTasks.isPresent()) {
        return podNotFoundResponse(podInstanceName);
    }
    Collection<TaskInfoAndStatus> podTasks = RequestUtils.filterPodTasks(podInstanceName, allPodTasks.get(), taskNameFilter);
    if (podTasks.isEmpty() || podTasks.size() < taskNameFilter.size()) {
        // one or more requested tasks were not found.
        LOGGER.error("Request had task filter: {} but pod '{}' tasks are: {} (matching: {})", taskNameFilter, podInstanceName, allPodTasks.get().stream().map(t -> t.getInfo().getName()).collect(Collectors.toList()), podTasks.stream().map(t -> t.getInfo().getName()).collect(Collectors.toList()));
        return podNotFoundResponse(podInstanceName);
    }
    // invoke the restart request itself against ALL tasks. this ensures that they're ALL flagged as failed via
    // FailureUtils, which is then checked by DefaultRecoveryPlanManager.
    LOGGER.info("Performing {} goal state override of {} tasks in pod {}:", override, podTasks.size(), podInstanceName);
    // First pass: Store the desired override for each task
    GoalStateOverride.Status pendingStatus = override.newStatus(GoalStateOverride.Progress.PENDING);
    for (TaskInfoAndStatus taskToOverride : podTasks) {
        stateStore.storeGoalOverrideStatus(taskToOverride.getInfo().getName(), pendingStatus);
    }
    // Second pass: Restart the tasks. They will be updated to IN_PROGRESS once we receive a terminal TaskStatus.
    return killTasks(podInstanceName, podTasks, RecoveryType.TRANSIENT);
}
Also used : GoalStateOverride(com.mesosphere.sdk.state.GoalStateOverride) TaskInfoAndStatus(com.mesosphere.sdk.http.types.TaskInfoAndStatus)

Example 4 with TaskInfoAndStatus

use of com.mesosphere.sdk.http.types.TaskInfoAndStatus in project dcos-commons by mesosphere.

the class PodQueries method getStatuses.

/**
 * Produces the summary statuses of all pod instances.
 */
public static Response getStatuses(StateStore stateStore, String serviceName) {
    try {
        // Group the tasks by pod:
        GroupedTasks groupedTasks = GroupedTasks.create(stateStore);
        // Output statuses for all tasks in each pod:
        JSONObject responseJson = new JSONObject();
        responseJson.put("service", serviceName);
        for (Map.Entry<String, Map<Integer, List<TaskInfoAndStatus>>> podType : groupedTasks.byPodTypeAndIndex.entrySet()) {
            JSONObject podJson = new JSONObject();
            podJson.put("name", podType.getKey());
            for (Map.Entry<Integer, List<TaskInfoAndStatus>> podInstance : podType.getValue().entrySet()) {
                podJson.append("instances", getPodInstanceStatusJson(stateStore, PodInstance.getName(podType.getKey(), podInstance.getKey()), podInstance.getValue()));
            }
            responseJson.append("pods", podJson);
        }
        // Output an 'unknown pod' instance for any tasks which didn't have a resolvable pod:
        if (!groupedTasks.unknownPod.isEmpty()) {
            JSONObject podTypeJson = new JSONObject();
            podTypeJson.put("name", UNKNOWN_POD_LABEL);
            podTypeJson.append("instances", getPodInstanceStatusJson(stateStore, PodInstance.getName(UNKNOWN_POD_LABEL, 0), groupedTasks.unknownPod));
            responseJson.append("pods", podTypeJson);
        }
        return jsonOkResponse(responseJson);
    } catch (Exception e) {
        LOGGER.error("Failed to fetch collated list of task statuses by pod", e);
        return Response.serverError().build();
    }
}
Also used : JSONObject(org.json.JSONObject) TaskInfoAndStatus(com.mesosphere.sdk.http.types.TaskInfoAndStatus) GroupedTasks(com.mesosphere.sdk.http.types.GroupedTasks) JSONException(org.json.JSONException) TaskException(com.mesosphere.sdk.offer.TaskException)

Example 5 with TaskInfoAndStatus

use of com.mesosphere.sdk.http.types.TaskInfoAndStatus in project dcos-commons by mesosphere.

the class PodQueriesTest method testGetPodInfo.

@Test
public void testGetPodInfo() {
    when(mockStateStore.fetchTasks()).thenReturn(TASK_INFOS);
    when(mockStateStore.fetchStatuses()).thenReturn(TASK_STATUSES);
    Response response = PodQueries.getInfo(mockStateStore, "test-1");
    assertEquals(200, response.getStatus());
    @SuppressWarnings("unchecked") List<TaskInfoAndStatus> info = (List<TaskInfoAndStatus>) response.getEntity();
    assertEquals(2, info.size());
    assertEquals(POD_1_TASK_A, info.get(0).getInfo());
    assertEquals(Optional.of(POD_1_STATUS_A), info.get(0).getStatus());
    assertEquals(POD_1_TASK_B, info.get(1).getInfo());
    assertEquals(Optional.of(POD_1_STATUS_B), info.get(1).getStatus());
}
Also used : Response(javax.ws.rs.core.Response) TaskInfoAndStatus(com.mesosphere.sdk.http.types.TaskInfoAndStatus) List(java.util.List) Test(org.junit.Test)

Aggregations

TaskInfoAndStatus (com.mesosphere.sdk.http.types.TaskInfoAndStatus)5 JSONObject (org.json.JSONObject)3 GroupedTasks (com.mesosphere.sdk.http.types.GroupedTasks)1 TaskException (com.mesosphere.sdk.offer.TaskException)1 GoalStateOverride (com.mesosphere.sdk.state.GoalStateOverride)1 List (java.util.List)1 Response (javax.ws.rs.core.Response)1 Protos (org.apache.mesos.Protos)1 JSONException (org.json.JSONException)1 Test (org.junit.Test)1