Search in sources :

Example 6 with HeliosRuntimeException

use of com.spotify.helios.common.HeliosRuntimeException in project helios by spotify.

the class ZooKeeperRegistrarUtil method deregisterHost.

public static void deregisterHost(final ZooKeeperClient client, final String host) throws HostNotFoundException, HostStillInUseException {
    log.info("deregistering host: {}", host);
    // TODO (dano): handle retry failures
    try {
        final List<ZooKeeperOperation> operations = Lists.newArrayList();
        if (client.exists(Paths.configHost(host)) == null) {
            throw new HostNotFoundException("host [" + host + "] does not exist");
        }
        // Remove all jobs deployed to this host
        final List<String> jobs = safeGetChildren(client, Paths.configHostJobs(host));
        for (final String jobString : jobs) {
            final JobId job = JobId.fromString(jobString);
            final String hostJobPath = Paths.configHostJob(host, job);
            final List<String> nodes = safeListRecursive(client, hostJobPath);
            for (final String node : reverse(nodes)) {
                operations.add(delete(node));
            }
            if (client.exists(Paths.configJobHost(job, host)) != null) {
                operations.add(delete(Paths.configJobHost(job, host)));
            }
            // Clean out the history for each job
            final List<String> history = safeListRecursive(client, Paths.historyJobHost(job, host));
            for (final String s : reverse(history)) {
                operations.add(delete(s));
            }
        }
        operations.add(delete(Paths.configHostJobs(host)));
        // Remove the host status
        final List<String> nodes = safeListRecursive(client, Paths.statusHost(host));
        for (final String node : reverse(nodes)) {
            operations.add(delete(node));
        }
        // Remove port allocations
        final List<String> ports = safeGetChildren(client, Paths.configHostPorts(host));
        for (final String port : ports) {
            operations.add(delete(Paths.configHostPort(host, Integer.valueOf(port))));
        }
        operations.add(delete(Paths.configHostPorts(host)));
        // Remove host id
        final String idPath = Paths.configHostId(host);
        if (client.exists(idPath) != null) {
            operations.add(delete(idPath));
        }
        // Remove host config root
        operations.add(delete(Paths.configHost(host)));
        client.transaction(operations);
    } catch (NoNodeException e) {
        throw new HostNotFoundException(host);
    } catch (KeeperException e) {
        throw new HeliosRuntimeException(e);
    }
}
Also used : NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) ZooKeeperOperation(com.spotify.helios.servicescommon.coordination.ZooKeeperOperation) HostNotFoundException(com.spotify.helios.master.HostNotFoundException) HeliosRuntimeException(com.spotify.helios.common.HeliosRuntimeException) JobId(com.spotify.helios.common.descriptors.JobId) KeeperException(org.apache.zookeeper.KeeperException)

Example 7 with HeliosRuntimeException

use of com.spotify.helios.common.HeliosRuntimeException in project helios by spotify.

the class ZooKeeperRegistrarUtil method reRegisterHost.

/**
   * Re-register an agent with a different host id. Will remove the existing status of the agent
   * but preserve any jobs deployed to the host and their history.
   * @param client ZooKeeperClient
   * @param host Host
   * @param hostId ID of the host
   * @throws HostNotFoundException If the hostname we are trying to re-register as doesn't exist.
   * @throws KeeperException If an unexpected zookeeper error occurs.
   */
public static void reRegisterHost(final ZooKeeperClient client, final String host, final String hostId) throws HostNotFoundException, KeeperException {
    // * Delete everything in the /status/hosts/<hostname> subtree
    // * Don't delete any history for the job (on the host)
    // * DON'T touch anything in the /config/hosts/<hostname> subtree, except updating the host id
    log.info("re-registering host: {}, new host id: {}", host, hostId);
    try {
        final List<ZooKeeperOperation> operations = Lists.newArrayList();
        // Check that the host exists in ZK
        operations.add(check(Paths.configHost(host)));
        // Remove the host status
        final List<String> nodes = safeListRecursive(client, Paths.statusHost(host));
        for (final String node : reverse(nodes)) {
            operations.add(delete(node));
        }
        // ...and re-create the /status/hosts/<host>/jobs node + parent
        operations.add(create(Paths.statusHost(host)));
        operations.add(create(Paths.statusHostJobs(host)));
        // Update the host ID
        // We don't have WRITE permissions to the node, so delete and re-create it.
        operations.add(delete(Paths.configHostId(host)));
        operations.add(create(Paths.configHostId(host), hostId.getBytes(UTF_8)));
        client.transaction(operations);
    } catch (NoNodeException e) {
        throw new HostNotFoundException(host);
    } catch (KeeperException e) {
        throw new HeliosRuntimeException(e);
    }
}
Also used : NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) ZooKeeperOperation(com.spotify.helios.servicescommon.coordination.ZooKeeperOperation) HostNotFoundException(com.spotify.helios.master.HostNotFoundException) HeliosRuntimeException(com.spotify.helios.common.HeliosRuntimeException) KeeperException(org.apache.zookeeper.KeeperException)

Example 8 with HeliosRuntimeException

use of com.spotify.helios.common.HeliosRuntimeException in project helios by spotify.

the class ZooKeeperMasterModel method getDeploymentGroupStatus.

@Override
public DeploymentGroupStatus getDeploymentGroupStatus(final String name) throws DeploymentGroupDoesNotExistException {
    log.debug("getting deployment group status: {}", name);
    final ZooKeeperClient client = provider.get("getDeploymentGroupStatus");
    final DeploymentGroup deploymentGroup = getDeploymentGroup(client, name);
    if (deploymentGroup == null) {
        return null;
    }
    try {
        final Node node = client.getNode(Paths.statusDeploymentGroup(name));
        final byte[] bytes = node.getBytes();
        if (bytes.length == 0) {
            return null;
        }
        return Json.read(bytes, DeploymentGroupStatus.class);
    } catch (NoNodeException e) {
        return null;
    } catch (KeeperException | IOException e) {
        throw new HeliosRuntimeException("getting deployment group status " + name + " failed", e);
    }
}
Also used : NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) ZooKeeperClient(com.spotify.helios.servicescommon.coordination.ZooKeeperClient) Node(com.spotify.helios.servicescommon.coordination.Node) HeliosRuntimeException(com.spotify.helios.common.HeliosRuntimeException) IOException(java.io.IOException) DeploymentGroup(com.spotify.helios.common.descriptors.DeploymentGroup) KeeperException(org.apache.zookeeper.KeeperException)

Example 9 with HeliosRuntimeException

use of com.spotify.helios.common.HeliosRuntimeException in project helios by spotify.

the class ZooKeeperMasterModel method getDeploymentGroups.

/**
   * Returns a {@link Map} of deployment group name to {@link DeploymentGroup} objects for all of
   * the deployment groups known.
   */
@Override
public Map<String, DeploymentGroup> getDeploymentGroups() {
    log.debug("getting deployment groups");
    final String folder = Paths.configDeploymentGroups();
    final ZooKeeperClient client = provider.get("getDeploymentGroups");
    try {
        final List<String> names;
        try {
            names = client.getChildren(folder);
        } catch (NoNodeException e) {
            return Maps.newHashMap();
        }
        final Map<String, DeploymentGroup> descriptors = Maps.newHashMap();
        for (final String name : names) {
            final String path = Paths.configDeploymentGroup(name);
            try {
                final byte[] data = client.getData(path);
                final DeploymentGroup descriptor = parse(data, DeploymentGroup.class);
                descriptors.put(descriptor.getName(), descriptor);
            } catch (NoNodeException e) {
                // Ignore, the deployment group was deleted before we had a chance to read it.
                log.debug("Ignoring deleted deployment group {}", name);
            }
        }
        return descriptors;
    } catch (KeeperException | IOException e) {
        throw new HeliosRuntimeException("getting deployment groups failed", e);
    }
}
Also used : NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) ZooKeeperClient(com.spotify.helios.servicescommon.coordination.ZooKeeperClient) HeliosRuntimeException(com.spotify.helios.common.HeliosRuntimeException) IOException(java.io.IOException) DeploymentGroup(com.spotify.helios.common.descriptors.DeploymentGroup) KeeperException(org.apache.zookeeper.KeeperException)

Example 10 with HeliosRuntimeException

use of com.spotify.helios.common.HeliosRuntimeException in project helios by spotify.

the class ZooKeeperMasterModel method getRunningMasters.

/**
   * Returns a list of the host names of the currently running masters.
   */
@Override
public List<String> getRunningMasters() {
    final ZooKeeperClient client = provider.get("getRunningMasters");
    try {
        final List<String> masters = client.getChildren(Paths.statusMaster());
        final ImmutableList.Builder<String> upMasters = ImmutableList.builder();
        for (final String master : masters) {
            if (client.exists(Paths.statusMasterUp(master)) != null) {
                upMasters.add(master);
            }
        }
        return upMasters.build();
    } catch (KeeperException e) {
        throw new HeliosRuntimeException("listing masters failed", e);
    }
}
Also used : ZooKeeperClient(com.spotify.helios.servicescommon.coordination.ZooKeeperClient) ImmutableList(com.google.common.collect.ImmutableList) HeliosRuntimeException(com.spotify.helios.common.HeliosRuntimeException) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

HeliosRuntimeException (com.spotify.helios.common.HeliosRuntimeException)27 KeeperException (org.apache.zookeeper.KeeperException)23 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)20 ZooKeeperClient (com.spotify.helios.servicescommon.coordination.ZooKeeperClient)16 JobId (com.spotify.helios.common.descriptors.JobId)10 IOException (java.io.IOException)10 ZooKeeperOperation (com.spotify.helios.servicescommon.coordination.ZooKeeperOperation)9 Job (com.spotify.helios.common.descriptors.Job)7 RolloutTask (com.spotify.helios.common.descriptors.RolloutTask)5 Task (com.spotify.helios.common.descriptors.Task)5 Deployment (com.spotify.helios.common.descriptors.Deployment)4 DeploymentGroup (com.spotify.helios.common.descriptors.DeploymentGroup)4 UUID (java.util.UUID)4 NodeExistsException (org.apache.zookeeper.KeeperException.NodeExistsException)4 ImmutableList (com.google.common.collect.ImmutableList)3 Node (com.spotify.helios.servicescommon.coordination.Node)3 DeploymentGroupStatus (com.spotify.helios.common.descriptors.DeploymentGroupStatus)2 HostNotFoundException (com.spotify.helios.master.HostNotFoundException)2 RollingUpdateOp (com.spotify.helios.rollingupdate.RollingUpdateOp)2 DefaultZooKeeperClient (com.spotify.helios.servicescommon.coordination.DefaultZooKeeperClient)2