Search in sources :

Example 66 with JobId

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

the class ZooKeeperClusterIdTest method testAgent.

@Test
public void testAgent() throws Exception {
    startDefaultMaster("--zk-cluster-id=" + zkClusterId);
    startDefaultAgent(testHost(), "--zk-cluster-id=" + zkClusterId);
    awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS);
    // Create job and deploy it
    final JobId jobId = createJob(testJobName, testJobVersion, BUSYBOX, IDLE_COMMAND);
    deployJob(jobId, testHost());
    final TaskStatus runningStatus = awaitTaskState(jobId, testHost(), RUNNING);
    final String containerId = runningStatus.getContainerId();
    // Delete the config node which contains the cluster ID and all the job definitions
    zk().curatorWithSuperAuth().delete().deletingChildrenIfNeeded().forPath("/config");
    // Sleep for a second so agent has a chance to react to deletion
    Thread.sleep(1000);
    // Make sure the agent didn't stop the job
    try (final DockerClient docker = getNewDockerClient()) {
        final List<Container> containers = docker.listContainers();
        final CustomTypeSafeMatcher<Container> containerIdMatcher = new CustomTypeSafeMatcher<Container>("Container with id " + containerId) {

            @Override
            protected boolean matchesSafely(Container container) {
                return container.id().equals(containerId);
            }
        };
        assertContainersMatch(containers, containerIdMatcher);
    }
}
Also used : Container(com.spotify.docker.client.messages.Container) DockerClient(com.spotify.docker.client.DockerClient) CustomTypeSafeMatcher(org.hamcrest.CustomTypeSafeMatcher) Matchers.containsString(org.hamcrest.Matchers.containsString) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) JobId(com.spotify.helios.common.descriptors.JobId) Test(org.junit.Test)

Example 67 with JobId

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

the class ZooKeeperMasterModel method updateDeployment.

/**
   * Used to update the existing deployment of a job.
   */
@Override
public void updateDeployment(final String host, final Deployment deployment, final String token) throws HostNotFoundException, JobNotDeployedException, TokenVerificationException {
    log.info("updating deployment {}: {}", deployment, host);
    final ZooKeeperClient client = provider.get("updateDeployment");
    final JobId jobId = deployment.getJobId();
    final Job job = getJob(client, jobId);
    final Deployment existingDeployment = getDeployment(host, jobId);
    if (job == null) {
        throw new JobNotDeployedException(host, jobId);
    }
    verifyToken(token, job);
    assertHostExists(client, host);
    assertTaskExists(client, host, deployment.getJobId());
    final String path = Paths.configHostJob(host, jobId);
    final Task task = new Task(job, deployment.getGoal(), existingDeployment.getDeployerUser(), existingDeployment.getDeployerMaster(), existingDeployment.getDeploymentGroupName());
    try {
        client.setData(path, task.toJsonBytes());
    } catch (Exception e) {
        throw new HeliosRuntimeException("updating deployment " + deployment + " on host " + host + " failed", e);
    }
}
Also used : Task(com.spotify.helios.common.descriptors.Task) RolloutTask(com.spotify.helios.common.descriptors.RolloutTask) ZooKeeperClient(com.spotify.helios.servicescommon.coordination.ZooKeeperClient) HeliosRuntimeException(com.spotify.helios.common.HeliosRuntimeException) Deployment(com.spotify.helios.common.descriptors.Deployment) Job(com.spotify.helios.common.descriptors.Job) JobId(com.spotify.helios.common.descriptors.JobId) HeliosRuntimeException(com.spotify.helios.common.HeliosRuntimeException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) NotEmptyException(org.apache.zookeeper.KeeperException.NotEmptyException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) BadVersionException(org.apache.zookeeper.KeeperException.BadVersionException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException)

Example 68 with JobId

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

the class ZooKeeperMasterModel method checkForPortConflicts.

private static void checkForPortConflicts(final ZooKeeperClient client, final String host, final int port, final JobId jobId) throws JobPortAllocationConflictException {
    try {
        final String path = Paths.configHostPort(host, port);
        if (client.stat(path) == null) {
            return;
        }
        final byte[] b = client.getData(path);
        final JobId existingJobId = parse(b, JobId.class);
        throw new JobPortAllocationConflictException(jobId, existingJobId, host, port);
    } catch (KeeperException | IOException ex) {
        throw new HeliosRuntimeException("checking port allocations failed", ex);
    }
}
Also used : HeliosRuntimeException(com.spotify.helios.common.HeliosRuntimeException) IOException(java.io.IOException) JobId(com.spotify.helios.common.descriptors.JobId) KeeperException(org.apache.zookeeper.KeeperException)

Example 69 with JobId

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

the class ZooKeeperMasterModel method listHostJobs.

private List<JobId> listHostJobs(final ZooKeeperClient client, final String host) {
    final List<String> jobIdStrings;
    final String folder = Paths.statusHostJobs(host);
    try {
        jobIdStrings = client.getChildren(folder);
    } catch (KeeperException.NoNodeException e) {
        return null;
    } catch (KeeperException e) {
        throw new HeliosRuntimeException("List tasks for host failed: " + host, e);
    }
    final ImmutableList.Builder<JobId> jobIds = ImmutableList.builder();
    for (final String jobIdString : jobIdStrings) {
        jobIds.add(JobId.fromString(jobIdString));
    }
    return jobIds.build();
}
Also used : NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) ImmutableList(com.google.common.collect.ImmutableList) HeliosRuntimeException(com.spotify.helios.common.HeliosRuntimeException) KeeperException(org.apache.zookeeper.KeeperException) JobId(com.spotify.helios.common.descriptors.JobId)

Example 70 with JobId

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

the class ZooKeeperMasterModel method addJob.

/**
   * Adds a job into the configuration.
   */
@Override
public void addJob(final Job job) throws JobExistsException {
    log.info("adding job: {}", job);
    final JobId id = job.getId();
    final UUID operationId = UUID.randomUUID();
    final String creationPath = Paths.configJobCreation(id, operationId);
    final ZooKeeperClient client = provider.get("addJob");
    try {
        try {
            client.ensurePath(Paths.historyJob(id));
            client.transaction(create(Paths.configJob(id), job), create(Paths.configJobRefShort(id), id), create(Paths.configJobHosts(id)), create(creationPath), // change down the tree. Effectively, make it that version == cVersion.
            set(Paths.configJobs(), UUID.randomUUID().toString().getBytes()));
        } catch (final NodeExistsException e) {
            if (client.exists(creationPath) != null) {
                // The job was created, we're done here
                return;
            }
            throw new JobExistsException(id.toString());
        }
    } catch (NoNodeException e) {
        throw new HeliosRuntimeException("adding job " + job + " failed due to missing ZK path: " + e.getPath(), e);
    } catch (final KeeperException e) {
        throw new HeliosRuntimeException("adding job " + job + " failed", e);
    }
}
Also used : NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) ZooKeeperClient(com.spotify.helios.servicescommon.coordination.ZooKeeperClient) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException) HeliosRuntimeException(com.spotify.helios.common.HeliosRuntimeException) UUID(java.util.UUID) JobId(com.spotify.helios.common.descriptors.JobId) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

JobId (com.spotify.helios.common.descriptors.JobId)115 Test (org.junit.Test)68 TaskStatus (com.spotify.helios.common.descriptors.TaskStatus)41 Job (com.spotify.helios.common.descriptors.Job)37 HeliosClient (com.spotify.helios.client.HeliosClient)35 Deployment (com.spotify.helios.common.descriptors.Deployment)29 Matchers.containsString (org.hamcrest.Matchers.containsString)25 DockerClient (com.spotify.docker.client.DockerClient)19 JobStatus (com.spotify.helios.common.descriptors.JobStatus)19 JobDeployResponse (com.spotify.helios.common.protocol.JobDeployResponse)16 CreateJobResponse (com.spotify.helios.common.protocol.CreateJobResponse)13 IOException (java.io.IOException)12 HostStatus (com.spotify.helios.common.descriptors.HostStatus)11 Map (java.util.Map)11 LogStream (com.spotify.docker.client.LogStream)10 HeliosRuntimeException (com.spotify.helios.common.HeliosRuntimeException)10 KeeperException (org.apache.zookeeper.KeeperException)9 TaskStatusEvent (com.spotify.helios.common.descriptors.TaskStatusEvent)8 AgentMain (com.spotify.helios.agent.AgentMain)7 PortMapping (com.spotify.helios.common.descriptors.PortMapping)7