Search in sources :

Example 26 with Job

use of com.spotify.helios.common.descriptors.Job 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)

Example 27 with Job

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

the class ZooKeeperMasterModel method getDeployOperations.

private List<ZooKeeperOperation> getDeployOperations(final ZooKeeperClient client, final String host, final Deployment deployment, final String token) throws JobDoesNotExistException, JobAlreadyDeployedException, TokenVerificationException, HostNotFoundException, JobPortAllocationConflictException {
    assertHostExists(client, host);
    final JobId id = deployment.getJobId();
    final Job job = getJob(id);
    if (job == null) {
        throw new JobDoesNotExistException(id);
    }
    verifyToken(token, job);
    final UUID operationId = UUID.randomUUID();
    final String jobPath = Paths.configJob(id);
    final String taskPath = Paths.configHostJob(host, id);
    final String taskCreationPath = Paths.configHostJobCreation(host, id, operationId);
    final List<Integer> staticPorts = staticPorts(job);
    final Map<String, byte[]> portNodes = Maps.newHashMap();
    final byte[] idJson = id.toJsonBytes();
    for (final int port : staticPorts) {
        final String path = Paths.configHostPort(host, port);
        portNodes.put(path, idJson);
    }
    final Task task = new Task(job, deployment.getGoal(), deployment.getDeployerUser(), deployment.getDeployerMaster(), deployment.getDeploymentGroupName());
    final List<ZooKeeperOperation> operations = Lists.newArrayList(check(jobPath), create(portNodes), create(Paths.configJobHost(id, host)));
    // Attempt to read a task here.
    try {
        client.getNode(taskPath);
        // if we get here the node exists already
        throw new JobAlreadyDeployedException(host, id);
    } catch (NoNodeException e) {
        // if the real reason of the failure is that the job is already deployed.
        for (final int port : staticPorts) {
            checkForPortConflicts(client, host, port, id);
        }
        operations.add(create(taskPath, task));
        operations.add(create(taskCreationPath));
    } catch (KeeperException e) {
        throw new HeliosRuntimeException("reading existing task description failed", e);
    }
    return ImmutableList.copyOf(operations);
}
Also used : Task(com.spotify.helios.common.descriptors.Task) RolloutTask(com.spotify.helios.common.descriptors.RolloutTask) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) ZooKeeperOperation(com.spotify.helios.servicescommon.coordination.ZooKeeperOperation) HeliosRuntimeException(com.spotify.helios.common.HeliosRuntimeException) Job(com.spotify.helios.common.descriptors.Job) UUID(java.util.UUID) JobId(com.spotify.helios.common.descriptors.JobId) KeeperException(org.apache.zookeeper.KeeperException)

Example 28 with Job

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

the class ZooKeeperMasterModel method getJobHistory.

/**
   * Given a jobId and host, returns the N most recent events in its history on that host in the
   * cluster.
   */
@Override
public List<TaskStatusEvent> getJobHistory(final JobId jobId, final String host) throws JobDoesNotExistException {
    final Job descriptor = getJob(jobId);
    if (descriptor == null) {
        throw new JobDoesNotExistException(jobId);
    }
    final ZooKeeperClient client = provider.get("getJobHistory");
    final List<String> hosts;
    try {
        hosts = (!isNullOrEmpty(host)) ? singletonList(host) : client.getChildren(Paths.historyJobHosts(jobId));
    } catch (NoNodeException e) {
        return emptyList();
    } catch (KeeperException e) {
        throw Throwables.propagate(e);
    }
    final List<TaskStatusEvent> jsEvents = Lists.newArrayList();
    for (final String h : hosts) {
        final List<String> events;
        try {
            events = client.getChildren(Paths.historyJobHostEvents(jobId, h));
        } catch (NoNodeException e) {
            continue;
        } catch (KeeperException e) {
            throw Throwables.propagate(e);
        }
        for (final String event : events) {
            try {
                final byte[] data = client.getData(Paths.historyJobHostEventsTimestamp(jobId, h, Long.valueOf(event)));
                final TaskStatus status = Json.read(data, TaskStatus.class);
                jsEvents.add(new TaskStatusEvent(status, Long.valueOf(event), h));
            } catch (NoNodeException e) {
            // ignore, it went away before we read it
            } catch (KeeperException | IOException e) {
                throw Throwables.propagate(e);
            }
        }
    }
    return Ordering.from(EVENT_COMPARATOR).sortedCopy(jsEvents);
}
Also used : TaskStatusEvent(com.spotify.helios.common.descriptors.TaskStatusEvent) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) IOException(java.io.IOException) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) ZooKeeperClient(com.spotify.helios.servicescommon.coordination.ZooKeeperClient) Job(com.spotify.helios.common.descriptors.Job) KeeperException(org.apache.zookeeper.KeeperException)

Example 29 with Job

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

the class JobValidatorTest method testJobFromJsonWithInvalidRegistration.

/**
   * Tests that Jobs deserialized from JSON representation that happen to have malformed
   * "registration" sections are properly handled. This mimics a real life test case where the
   * "volumes" entry was accidentally indented wrong and included within the "registration"
   * section.
   */
@Test
public void testJobFromJsonWithInvalidRegistration() throws Exception {
    final URL resource = getClass().getResource("job-with-bad-registration.json");
    final byte[] bytes = Resources.toByteArray(resource);
    final Job job = Json.read(bytes, Job.class);
    assertThat(validator.validate(job), contains("registration for 'volumes' is malformed: does not have a port mapping"));
}
Also used : Job(com.spotify.helios.common.descriptors.Job) URL(java.net.URL) Test(org.junit.Test)

Example 30 with Job

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

the class JobValidatorTest method testInvalidHealthCheckFail.

@Test
public void testInvalidHealthCheckFail() {
    final Job jobWithNoPorts = Job.newBuilder().setName("foo").setVersion("1").setImage("foobar").setHealthCheck(HEALTH_CHECK).build();
    assertEquals(1, validator.validate(jobWithNoPorts).size());
    final Job jobWithWrongPort = jobWithNoPorts.toBuilder().addPort("a", PortMapping.of(1, 1)).build();
    assertEquals(1, validator.validate(jobWithWrongPort).size());
}
Also used : Job(com.spotify.helios.common.descriptors.Job) Test(org.junit.Test)

Aggregations

Job (com.spotify.helios.common.descriptors.Job)79 Test (org.junit.Test)57 JobId (com.spotify.helios.common.descriptors.JobId)38 HeliosClient (com.spotify.helios.client.HeliosClient)25 Deployment (com.spotify.helios.common.descriptors.Deployment)21 CreateJobResponse (com.spotify.helios.common.protocol.CreateJobResponse)16 TaskStatus (com.spotify.helios.common.descriptors.TaskStatus)15 JobStatus (com.spotify.helios.common.descriptors.JobStatus)12 JobDeployResponse (com.spotify.helios.common.protocol.JobDeployResponse)11 ZooKeeperClient (com.spotify.helios.servicescommon.coordination.ZooKeeperClient)10 KeeperException (org.apache.zookeeper.KeeperException)10 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)10 PortMapping (com.spotify.helios.common.descriptors.PortMapping)9 HeliosRuntimeException (com.spotify.helios.common.HeliosRuntimeException)8 ServiceEndpoint (com.spotify.helios.common.descriptors.ServiceEndpoint)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 DockerClient (com.spotify.docker.client.DockerClient)7 ZooKeeperOperation (com.spotify.helios.servicescommon.coordination.ZooKeeperOperation)7 IOException (java.io.IOException)7 ServicePorts (com.spotify.helios.common.descriptors.ServicePorts)6