Search in sources :

Example 1 with TaskStatusEvents

use of com.spotify.helios.common.protocol.TaskStatusEvents in project helios by spotify.

the class JobHistoryCommand method run.

@Override
int run(final Namespace options, final HeliosClient client, final PrintStream out, final boolean json, final BufferedReader stdin) throws ExecutionException, InterruptedException {
    final String jobIdString = options.getString(jobIdArg.getDest());
    final Map<JobId, Job> jobs = client.jobs(jobIdString).get();
    if (jobs.size() == 0) {
        out.printf("Unknown job: %s%n", jobIdString);
        return 1;
    } else if (jobs.size() > 1) {
        out.printf("Ambiguous job id: %s%n", jobIdString);
        return 1;
    }
    final JobId jobId = getLast(jobs.keySet());
    final TaskStatusEvents result = client.jobHistory(jobId).get();
    if (json) {
        out.println(Json.asPrettyStringUnchecked(result));
        return 0;
    }
    final Table table = table(out);
    table.row("HOST", "TIMESTAMP", "STATE", "THROTTLED", "CONTAINERID");
    final List<TaskStatusEvent> events = result.getEvents();
    final DateTimeFormatter format = DateTimeFormat.forPattern("YYYY-MM-dd HH:mm:ss.SSS");
    for (final TaskStatusEvent event : events) {
        final String host = checkNotNull(event.getHost());
        final long timestamp = checkNotNull(event.getTimestamp());
        final TaskStatus status = checkNotNull(event.getStatus());
        final State state = checkNotNull(status.getState());
        String containerId = status.getContainerId();
        containerId = containerId == null ? "<none>" : containerId;
        table.row(host, format.print(timestamp), state, status.getThrottled(), containerId);
    }
    table.print();
    return 0;
}
Also used : TaskStatusEvent(com.spotify.helios.common.descriptors.TaskStatusEvent) Table(com.spotify.helios.cli.Table) State(com.spotify.helios.common.descriptors.TaskStatus.State) TaskStatusEvents(com.spotify.helios.common.protocol.TaskStatusEvents) Job(com.spotify.helios.common.descriptors.Job) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) JobId(com.spotify.helios.common.descriptors.JobId)

Example 2 with TaskStatusEvents

use of com.spotify.helios.common.protocol.TaskStatusEvents in project helios by spotify.

the class HistoryResource method jobHistory.

/**
   * Returns the {@link TaskStatusEvents} for the specified job.
   * @param jobId The ID of the job.
   * @return The history of the jobs.
   * @throws HeliosException If an unexpected error occurs.
   */
@GET
@Produces(APPLICATION_JSON)
@Path("jobs/{id}")
@Timed
@ExceptionMetered
public TaskStatusEvents jobHistory(@PathParam("id") @Valid final JobId jobId) throws HeliosException {
    if (!jobId.isFullyQualified()) {
        throw badRequest("Invalid id");
    }
    try {
        final List<TaskStatusEvent> events = model.getJobHistory(jobId);
        metrics.jobsHistoryEventSize(events.size());
        final TaskStatusEvents result = new TaskStatusEvents(events, OK);
        return result;
    } catch (JobDoesNotExistException e) {
        return new TaskStatusEvents(ImmutableList.<TaskStatusEvent>of(), JOB_ID_NOT_FOUND);
    }
}
Also used : TaskStatusEvent(com.spotify.helios.common.descriptors.TaskStatusEvent) JobDoesNotExistException(com.spotify.helios.master.JobDoesNotExistException) TaskStatusEvents(com.spotify.helios.common.protocol.TaskStatusEvents) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered)

Example 3 with TaskStatusEvents

use of com.spotify.helios.common.protocol.TaskStatusEvents in project helios by spotify.

the class HealthCheckTest method testContainerDiesDuringHealthcheck.

@Test
public void testContainerDiesDuringHealthcheck() throws Exception {
    startDefaultMaster();
    final HeliosClient client = defaultClient();
    startDefaultAgent(testHost(), "--service-registry=" + registryAddress);
    awaitHostStatus(client, testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
    final HealthCheck healthCheck = TcpHealthCheck.of("health");
    final Job job = pokeJob(healthCheck);
    final JobId jobId = createJob(job);
    deployJob(jobId, testHost());
    awaitTaskState(jobId, testHost(), HEALTHCHECKING);
    // kill the underlying container
    final JobStatus jobStatus = getOrNull(client.jobStatus(jobId));
    final TaskStatus taskStatus = jobStatus.getTaskStatuses().get(testHost());
    getNewDockerClient().killContainer(taskStatus.getContainerId());
    // ensure the job is marked as failed
    final int timeout = WAIT_TIMEOUT_SECONDS;
    Polling.await(timeout, SECONDS, new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            final TaskStatusEvents jobHistory = getOrNull(client.jobHistory(jobId));
            for (final TaskStatusEvent event : jobHistory.getEvents()) {
                if (event.getStatus().getState() == FAILED) {
                    return true;
                }
            }
            return null;
        }
    });
    // wait for the job to come back up and start healthchecking again
    awaitTaskState(jobId, testHost(), HEALTHCHECKING);
    pokeAndVerifyRegistration(client, jobId, timeout);
}
Also used : TaskStatusEvent(com.spotify.helios.common.descriptors.TaskStatusEvent) HttpHealthCheck(com.spotify.helios.common.descriptors.HttpHealthCheck) HealthCheck(com.spotify.helios.common.descriptors.HealthCheck) ExecHealthCheck(com.spotify.helios.common.descriptors.ExecHealthCheck) TcpHealthCheck(com.spotify.helios.common.descriptors.TcpHealthCheck) TaskStatusEvents(com.spotify.helios.common.protocol.TaskStatusEvents) HeliosClient(com.spotify.helios.client.HeliosClient) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) ServiceEndpoint(com.spotify.helios.common.descriptors.ServiceEndpoint) Endpoint(com.spotify.helios.serviceregistration.ServiceRegistration.Endpoint) IOException(java.io.IOException) JobStatus(com.spotify.helios.common.descriptors.JobStatus) Job(com.spotify.helios.common.descriptors.Job) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Example 4 with TaskStatusEvents

use of com.spotify.helios.common.protocol.TaskStatusEvents in project helios by spotify.

the class JobHistoryTest method testJobHistory.

@Test
public void testJobHistory() throws Exception {
    startDefaultMaster();
    final HeliosClient client = defaultClient();
    startDefaultAgent(testHost());
    awaitHostStatus(testHost(), Status.UP, LONG_WAIT_SECONDS, SECONDS);
    final JobId jobId = createJob(testJobName, testJobVersion, BUSYBOX, IDLE_COMMAND);
    deployJob(jobId, testHost());
    awaitJobState(client, testHost(), jobId, RUNNING, LONG_WAIT_SECONDS, SECONDS);
    undeployJob(jobId, testHost());
    awaitTaskGone(client, testHost(), jobId, LONG_WAIT_SECONDS, SECONDS);
    final TaskStatusEvents events = Polling.await(WAIT_TIMEOUT_SECONDS, SECONDS, new Callable<TaskStatusEvents>() {

        @Override
        public TaskStatusEvents call() throws Exception {
            final TaskStatusEvents events = client.jobHistory(jobId).get();
            final int size = events.getEvents().size();
            if (size == 0) {
                return null;
            }
            // We sometimes get more than one PULLING_IMAGE in the history if a pull tempfails.
            int requiredEventCount = -1;
            for (int i = 0; i < size; i++) {
                if (events.getEvents().get(i).getStatus().getState() != State.PULLING_IMAGE) {
                    requiredEventCount = i + 5;
                    break;
                }
            }
            if (requiredEventCount == -1) {
                return null;
            }
            if (size < requiredEventCount) {
                return null;
            }
            return events;
        }
    });
    final ListIterator<TaskStatusEvent> it = events.getEvents().listIterator();
    while (true) {
        final TaskStatusEvent event = it.next();
        if (event.getStatus().getState() != State.PULLING_IMAGE) {
            //rewind so that this event is the one returned by the next call to it.next() below
            it.previous();
            break;
        }
        assertThat(event, not(hasContainerId()));
    }
    assertThat(it.next(), allOf(hasState(State.CREATING), not(hasContainerId())));
    assertThat(it.next(), allOf(hasState(State.STARTING), hasContainerId()));
    assertThat(it.next(), hasState(State.RUNNING));
    assertThat(it.next(), hasState(State.STOPPING));
    assertThat(it.next(), hasState(State.EXITED, State.STOPPED));
}
Also used : TaskStatusEvent(com.spotify.helios.common.descriptors.TaskStatusEvent) TaskStatusEvents(com.spotify.helios.common.protocol.TaskStatusEvents) HeliosClient(com.spotify.helios.client.HeliosClient) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Aggregations

TaskStatusEvent (com.spotify.helios.common.descriptors.TaskStatusEvent)4 TaskStatusEvents (com.spotify.helios.common.protocol.TaskStatusEvents)4 JobId (com.spotify.helios.common.descriptors.JobId)3 HeliosClient (com.spotify.helios.client.HeliosClient)2 Job (com.spotify.helios.common.descriptors.Job)2 TaskStatus (com.spotify.helios.common.descriptors.TaskStatus)2 Test (org.junit.Test)2 ExceptionMetered (com.codahale.metrics.annotation.ExceptionMetered)1 Timed (com.codahale.metrics.annotation.Timed)1 Table (com.spotify.helios.cli.Table)1 ExecHealthCheck (com.spotify.helios.common.descriptors.ExecHealthCheck)1 HealthCheck (com.spotify.helios.common.descriptors.HealthCheck)1 HttpHealthCheck (com.spotify.helios.common.descriptors.HttpHealthCheck)1 JobStatus (com.spotify.helios.common.descriptors.JobStatus)1 ServiceEndpoint (com.spotify.helios.common.descriptors.ServiceEndpoint)1 State (com.spotify.helios.common.descriptors.TaskStatus.State)1 TcpHealthCheck (com.spotify.helios.common.descriptors.TcpHealthCheck)1 JobDoesNotExistException (com.spotify.helios.master.JobDoesNotExistException)1 Endpoint (com.spotify.helios.serviceregistration.ServiceRegistration.Endpoint)1 IOException (java.io.IOException)1