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