Search in sources :

Example 11 with Deployment

use of com.spotify.helios.common.descriptors.Deployment in project helios by spotify.

the class DeploymentGroupTest method awaitUndeployed.

private void awaitUndeployed(final String host, final JobId jobId) throws Exception {
    // Ensure the deployment is gone.
    final Deployment oldDeployment = defaultClient().deployment(host, jobId).get();
    assertThat(oldDeployment, is(nullValue()));
    // Wait for the task to disappear
    awaitTaskGone(defaultClient(), host, jobId, LONG_WAIT_SECONDS, SECONDS);
}
Also used : Deployment(com.spotify.helios.common.descriptors.Deployment)

Example 12 with Deployment

use of com.spotify.helios.common.descriptors.Deployment in project helios by spotify.

the class OldJobReaper method processItem.

@Override
void processItem(final Job job) {
    final JobId jobId = job.getId();
    try {
        final JobStatus jobStatus = masterModel.getJobStatus(jobId);
        final Map<String, Deployment> deployments = jobStatus.getDeployments();
        final List<TaskStatusEvent> events = masterModel.getJobHistory(jobId);
        boolean reap;
        if (deployments.isEmpty()) {
            if (events.isEmpty()) {
                final Long created = job.getCreated();
                if (created == null) {
                    log.info("Marked job '{}' for reaping (not deployed, no history, no creation date)", jobId);
                    reap = true;
                } else if ((clock.now().getMillis() - created) > retentionMillis) {
                    log.info("Marked job '{}' for reaping (not deployed, no history, creation date " + "of {} before retention time of {} days)", jobId, DATE_FORMATTER.print(created), retentionDays);
                    reap = true;
                } else {
                    log.info("NOT reaping job '{}' (not deployed, no history, creation date of {} after " + "retention time of {} days)", jobId, DATE_FORMATTER.print(created), retentionDays);
                    reap = false;
                }
            } else {
                // Get the last event which is the most recent
                final TaskStatusEvent event = events.get(events.size() - 1);
                final String eventDate = DATE_FORMATTER.print(event.getTimestamp());
                // Calculate the amount of time in milliseconds that has elapsed since the last event
                final long unusedDurationMillis = clock.now().getMillis() - event.getTimestamp();
                // A job not deployed, with history, and last used recently should NOT BE reaped
                if (unusedDurationMillis > retentionMillis) {
                    log.info("Marked job '{}' for reaping (not deployed, has history whose last event " + "on {} was before the retention time of {} days)", jobId, eventDate, retentionDays);
                    reap = true;
                } else {
                    log.info("NOT reaping job '{}' (not deployed, has history whose last event " + "on {} was after the retention time of {} days)", jobId, eventDate, retentionDays);
                    reap = false;
                }
            }
        } else {
            // A job that's deployed should NOT BE reaped regardless of its history or creation date
            reap = false;
        }
        if (reap) {
            try {
                log.info("reaping old job '{}'", job.getId());
                masterModel.removeJob(jobId, job.getToken());
            } catch (Exception e) {
                log.warn("Failed to reap old job '{}'", jobId, e);
            }
        }
    } catch (Exception e) {
        log.warn("Failed to determine if job '{}' should be reaped", jobId, e);
    }
}
Also used : JobStatus(com.spotify.helios.common.descriptors.JobStatus) TaskStatusEvent(com.spotify.helios.common.descriptors.TaskStatusEvent) Deployment(com.spotify.helios.common.descriptors.Deployment) JobId(com.spotify.helios.common.descriptors.JobId)

Example 13 with Deployment

use of com.spotify.helios.common.descriptors.Deployment in project helios by spotify.

the class DeploymentGroupResource method getDeploymentGroupStatus.

@GET
@Path("/{name}/status")
@Produces(APPLICATION_JSON)
@Timed
@ExceptionMetered
public Response getDeploymentGroupStatus(@PathParam("name") @Valid final String name) {
    try {
        final DeploymentGroup deploymentGroup = model.getDeploymentGroup(name);
        final DeploymentGroupStatus deploymentGroupStatus = model.getDeploymentGroupStatus(name);
        final List<String> hosts = model.getDeploymentGroupHosts(name);
        final List<DeploymentGroupStatusResponse.HostStatus> result = Lists.newArrayList();
        for (final String host : hosts) {
            final HostStatus hostStatus = model.getHostStatus(host);
            JobId deployedJobId = null;
            TaskStatus.State state = null;
            if (hostStatus != null && hostStatus.getStatus().equals(HostStatus.Status.UP)) {
                for (final Map.Entry<JobId, Deployment> entry : hostStatus.getJobs().entrySet()) {
                    if (name.equals(entry.getValue().getDeploymentGroupName())) {
                        deployedJobId = entry.getKey();
                        final TaskStatus taskStatus = hostStatus.getStatuses().get(deployedJobId);
                        if (taskStatus != null) {
                            state = taskStatus.getState();
                        }
                        break;
                    }
                }
                result.add(new DeploymentGroupStatusResponse.HostStatus(host, deployedJobId, state));
            }
        }
        final DeploymentGroupStatusResponse.Status status;
        if (deploymentGroupStatus == null) {
            status = DeploymentGroupStatusResponse.Status.IDLE;
        } else if (deploymentGroupStatus.getState() == DeploymentGroupStatus.State.FAILED) {
            status = DeploymentGroupStatusResponse.Status.FAILED;
        } else if (deploymentGroupStatus.getState() == DeploymentGroupStatus.State.ROLLING_OUT) {
            status = DeploymentGroupStatusResponse.Status.ROLLING_OUT;
        } else {
            status = DeploymentGroupStatusResponse.Status.ACTIVE;
        }
        final String error = deploymentGroupStatus == null ? "" : deploymentGroupStatus.getError();
        return Response.ok(new DeploymentGroupStatusResponse(deploymentGroup, status, error, result, deploymentGroupStatus)).build();
    } catch (final DeploymentGroupDoesNotExistException e) {
        return Response.status(Response.Status.NOT_FOUND).build();
    }
}
Also used : Deployment(com.spotify.helios.common.descriptors.Deployment) DeploymentGroupStatus(com.spotify.helios.common.descriptors.DeploymentGroupStatus) DeploymentGroupDoesNotExistException(com.spotify.helios.master.DeploymentGroupDoesNotExistException) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) DeploymentGroupStatusResponse(com.spotify.helios.common.protocol.DeploymentGroupStatusResponse) HostStatus(com.spotify.helios.common.descriptors.HostStatus) Map(java.util.Map) DeploymentGroup(com.spotify.helios.common.descriptors.DeploymentGroup) JobId(com.spotify.helios.common.descriptors.JobId) 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 14 with Deployment

use of com.spotify.helios.common.descriptors.Deployment in project helios by spotify.

the class HostsResource method jobPut.

/**
   * Sets the deployment of the job identified by its {@link JobId} on the host named by
   * {@code host} to {@code deployment}.
   * @param host The host to deploy to.
   * @param jobId The job to deploy.
   * @param deployment Deployment information.
   * @param username The user deploying.
   * @param token The authorization token for this deployment.
   * @return The response.
   */
@PUT
@Path("/{host}/jobs/{job}")
@Produces(APPLICATION_JSON)
@Timed
@ExceptionMetered
public JobDeployResponse jobPut(@PathParam("host") final String host, @PathParam("job") final JobId jobId, @Valid final Deployment deployment, @RequestUser final String username, @QueryParam("token") @DefaultValue(EMPTY_TOKEN) final String token) {
    if (!jobId.isFullyQualified()) {
        throw badRequest(new JobDeployResponse(JobDeployResponse.Status.INVALID_ID, host, jobId));
    }
    try {
        final Deployment actualDeployment = deployment.toBuilder().setDeployerUser(username).build();
        model.deployJob(host, actualDeployment, token);
        return new JobDeployResponse(JobDeployResponse.Status.OK, host, jobId);
    } catch (JobAlreadyDeployedException e) {
        throw badRequest(new JobDeployResponse(JobDeployResponse.Status.JOB_ALREADY_DEPLOYED, host, jobId));
    } catch (HostNotFoundException e) {
        throw badRequest(new JobDeployResponse(JobDeployResponse.Status.HOST_NOT_FOUND, host, jobId));
    } catch (JobDoesNotExistException e) {
        throw badRequest(new JobDeployResponse(JobDeployResponse.Status.JOB_NOT_FOUND, host, jobId));
    } catch (JobPortAllocationConflictException e) {
        throw badRequest(new JobDeployResponse(JobDeployResponse.Status.PORT_CONFLICT, host, jobId));
    } catch (TokenVerificationException e) {
        throw forbidden(new JobDeployResponse(JobDeployResponse.Status.FORBIDDEN, host, jobId));
    }
}
Also used : JobPortAllocationConflictException(com.spotify.helios.master.JobPortAllocationConflictException) JobDoesNotExistException(com.spotify.helios.master.JobDoesNotExistException) HostNotFoundException(com.spotify.helios.master.HostNotFoundException) TokenVerificationException(com.spotify.helios.master.TokenVerificationException) Deployment(com.spotify.helios.common.descriptors.Deployment) JobAlreadyDeployedException(com.spotify.helios.master.JobAlreadyDeployedException) JobDeployResponse(com.spotify.helios.common.protocol.JobDeployResponse) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) ExceptionMetered(com.codahale.metrics.annotation.ExceptionMetered) PUT(javax.ws.rs.PUT)

Example 15 with Deployment

use of com.spotify.helios.common.descriptors.Deployment in project helios by spotify.

the class ZooKeeperMasterModel method getJobStatus.

/**
   * Returns the current job status as a {@link JobStatus} object.
   */
@Override
public JobStatus getJobStatus(final JobId jobId) {
    final ZooKeeperClient client = provider.get("getJobStatus");
    final Job job = getJob(client, jobId);
    if (job == null) {
        return null;
    }
    final List<String> hosts;
    try {
        hosts = listJobHosts(client, jobId);
    } catch (JobDoesNotExistException e) {
        return null;
    }
    final ImmutableMap.Builder<String, Deployment> deployments = ImmutableMap.builder();
    final ImmutableMap.Builder<String, TaskStatus> taskStatuses = ImmutableMap.builder();
    for (final String host : hosts) {
        final TaskStatus taskStatus = getTaskStatus(client, host, jobId);
        if (taskStatus != null) {
            taskStatuses.put(host, taskStatus);
        }
        final Deployment deployment = getDeployment(host, jobId);
        if (deployment != null) {
            deployments.put(host, deployment);
        }
    }
    final Map<String, Deployment> deploymentsMap = deployments.build();
    return JobStatus.newBuilder().setJob(job).setDeployments(deploymentsMap).setTaskStatuses(taskStatuses.build()).build();
}
Also used : ZooKeeperClient(com.spotify.helios.servicescommon.coordination.ZooKeeperClient) Deployment(com.spotify.helios.common.descriptors.Deployment) Job(com.spotify.helios.common.descriptors.Job) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

Deployment (com.spotify.helios.common.descriptors.Deployment)44 JobId (com.spotify.helios.common.descriptors.JobId)30 Test (org.junit.Test)24 Job (com.spotify.helios.common.descriptors.Job)21 HeliosClient (com.spotify.helios.client.HeliosClient)19 JobDeployResponse (com.spotify.helios.common.protocol.JobDeployResponse)18 TaskStatus (com.spotify.helios.common.descriptors.TaskStatus)16 CreateJobResponse (com.spotify.helios.common.protocol.CreateJobResponse)14 DockerClient (com.spotify.docker.client.DockerClient)8 HostStatus (com.spotify.helios.common.descriptors.HostStatus)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6 AgentMain (com.spotify.helios.agent.AgentMain)5 HeliosRuntimeException (com.spotify.helios.common.HeliosRuntimeException)5 JobStatus (com.spotify.helios.common.descriptors.JobStatus)5 ZooKeeperClient (com.spotify.helios.servicescommon.coordination.ZooKeeperClient)5 KeeperException (org.apache.zookeeper.KeeperException)5 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)5 RolloutTask (com.spotify.helios.common.descriptors.RolloutTask)3 Task (com.spotify.helios.common.descriptors.Task)3 JobUndeployResponse (com.spotify.helios.common.protocol.JobUndeployResponse)3