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();
}
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);
}
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);
}
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"));
}
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());
}
Aggregations