Search in sources :

Example 31 with ZooKeeperClient

use of com.spotify.helios.servicescommon.coordination.ZooKeeperClient 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)

Example 32 with ZooKeeperClient

use of com.spotify.helios.servicescommon.coordination.ZooKeeperClient in project helios by spotify.

the class ZooKeeperMasterModel method deployJob.

/**
 * Creates a config entry within the specified agent to un/deploy a job, or more generally, change
 * the deployment status according to the {@code Goal} value in {@link Deployment}.
 */
@Override
public void deployJob(final String host, final Deployment deployment, final String token) throws JobDoesNotExistException, JobAlreadyDeployedException, HostNotFoundException, JobPortAllocationConflictException, TokenVerificationException {
    final ZooKeeperClient client = provider.get("deployJob");
    deployJobRetry(client, host, deployment, 0, token);
}
Also used : ZooKeeperClient(com.spotify.helios.servicescommon.coordination.ZooKeeperClient)

Example 33 with ZooKeeperClient

use of com.spotify.helios.servicescommon.coordination.ZooKeeperClient in project helios by spotify.

the class ZooKeeperMasterModel method getHostLabels.

@Override
public Map<String, String> getHostLabels(final String host) {
    final ZooKeeperClient client = provider.get("getHostStatus");
    if (!ZooKeeperRegistrarUtil.isHostRegistered(client, host)) {
        return emptyMap();
    }
    final Map<String, String> labels = getLabels(client, host);
    return labels == null ? emptyMap() : labels;
}
Also used : ZooKeeperClient(com.spotify.helios.servicescommon.coordination.ZooKeeperClient)

Example 34 with ZooKeeperClient

use of com.spotify.helios.servicescommon.coordination.ZooKeeperClient in project helios by spotify.

the class ZooKeeperMasterModel method getInitRollingUpdateOps.

private RollingUpdateOp getInitRollingUpdateOps(final DeploymentGroup deploymentGroup, final List<String> updateHosts, final List<String> undeployHosts, final ZooKeeperClient zooKeeperClient) throws KeeperException {
    final List<RolloutTask> rolloutTasks = new ArrayList<>();
    // give precedence to the updateHosts list so we don't end up in a state where we updated a host
    // and then removed the job from it (because of buggy logic in the calling method)
    final List<String> updateHostsCopy = new ArrayList<>(updateHosts);
    final List<String> undeployHostsCopy = new ArrayList<>(undeployHosts);
    undeployHostsCopy.removeAll(updateHostsCopy);
    // we only care about hosts that are UP
    final List<String> upHostsToUndeploy = undeployHostsCopy.stream().filter(host -> checkHostUp(zooKeeperClient, host)).collect(Collectors.toList());
    final List<String> upHostsToDeploy = updateHostsCopy.stream().filter(host -> checkHostUp(zooKeeperClient, host)).collect(Collectors.toList());
    rolloutTasks.addAll(RollingUndeployPlanner.of(deploymentGroup).plan(upHostsToUndeploy));
    rolloutTasks.addAll(RollingUpdatePlanner.of(deploymentGroup).plan(upHostsToDeploy));
    log.info("generated rolloutTasks for deployment-group name={} " + "updateHosts={} undeployHosts={}: {}", deploymentGroup.getName(), updateHosts, undeployHosts, rolloutTasks);
    final DeploymentGroupTasks tasks = DeploymentGroupTasks.newBuilder().setRolloutTasks(rolloutTasks).setTaskIndex(0).setDeploymentGroup(deploymentGroup).build();
    return new RollingUpdateOpFactory(tasks, DEPLOYMENT_GROUP_EVENT_FACTORY).start(deploymentGroup, zooKeeperClient);
}
Also used : Descriptor.parse(com.spotify.helios.common.descriptors.Descriptor.parse) HeliosRuntimeException(com.spotify.helios.common.HeliosRuntimeException) ZooKeeperClient(com.spotify.helios.servicescommon.coordination.ZooKeeperClient) LoggerFactory(org.slf4j.LoggerFactory) Stat(org.apache.zookeeper.data.Stat) ThrottleState(com.spotify.helios.common.descriptors.ThrottleState) FAILED(com.spotify.helios.common.descriptors.DeploymentGroupStatus.State.FAILED) ZooKeeperOperations.create(com.spotify.helios.servicescommon.coordination.ZooKeeperOperations.create) Collections.singletonList(java.util.Collections.singletonList) Optional.fromNullable(com.google.common.base.Optional.fromNullable) Json(com.spotify.helios.common.Json) RolloutOptions(com.spotify.helios.common.descriptors.RolloutOptions) RollingUndeployPlanner(com.spotify.helios.rollingupdate.RollingUndeployPlanner) ZooKeeperOperations.set(com.spotify.helios.servicescommon.coordination.ZooKeeperOperations.set) VersionedValue(com.spotify.helios.servicescommon.VersionedValue) Map(java.util.Map) Deployment(com.spotify.helios.common.descriptors.Deployment) TypeReference(com.fasterxml.jackson.core.type.TypeReference) JsonParseException(com.fasterxml.jackson.core.JsonParseException) HostInfo(com.spotify.helios.common.descriptors.HostInfo) Function(com.google.common.base.Function) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Collections.emptyList(java.util.Collections.emptyList) DeploymentGroup(com.spotify.helios.common.descriptors.DeploymentGroup) Predicate(java.util.function.Predicate) EventSender(com.spotify.helios.servicescommon.EventSender) Set(java.util.Set) PortMapping(com.spotify.helios.common.descriptors.PortMapping) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) RollingUpdateOp(com.spotify.helios.rollingupdate.RollingUpdateOp) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) Nullable(org.jetbrains.annotations.Nullable) HOSTS_CHANGED(com.spotify.helios.common.descriptors.DeploymentGroup.RollingUpdateReason.HOSTS_CHANGED) TaskStatusEvent(com.spotify.helios.common.descriptors.TaskStatusEvent) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) NotEmptyException(org.apache.zookeeper.KeeperException.NotEmptyException) MANUAL(com.spotify.helios.common.descriptors.DeploymentGroup.RollingUpdateReason.MANUAL) UP(com.spotify.helios.common.descriptors.HostStatus.Status.UP) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) RollingUpdateOpFactory(com.spotify.helios.rollingupdate.RollingUpdateOpFactory) Pattern(java.util.regex.Pattern) Lists.reverse(com.google.common.collect.Lists.reverse) TRUE(java.lang.Boolean.TRUE) RollingUpdatePlanner(com.spotify.helios.rollingupdate.RollingUpdatePlanner) ZooKeeperOperations.delete(com.spotify.helios.servicescommon.coordination.ZooKeeperOperations.delete) Joiner(com.google.common.base.Joiner) JobId(com.spotify.helios.common.descriptors.JobId) RollingUpdateError(com.spotify.helios.rollingupdate.RollingUpdateError) Goal(com.spotify.helios.common.descriptors.Goal) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) Paths(com.spotify.helios.servicescommon.coordination.Paths) ZooKeeperRegistrarUtil(com.spotify.helios.servicescommon.ZooKeeperRegistrarUtil) ArrayList(java.util.ArrayList) ROLLING_OUT(com.spotify.helios.common.descriptors.DeploymentGroupStatus.State.ROLLING_OUT) Strings(com.google.common.base.Strings) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) DeploymentGroupTasks(com.spotify.helios.common.descriptors.DeploymentGroupTasks) HostStatus(com.spotify.helios.common.descriptors.HostStatus) DOWN(com.spotify.helios.common.descriptors.HostStatus.Status.DOWN) OpResult(org.apache.zookeeper.OpResult) Task(com.spotify.helios.common.descriptors.Task) TaskStatus(com.spotify.helios.common.descriptors.TaskStatus) BadVersionException(org.apache.zookeeper.KeeperException.BadVersionException) Collections.emptyMap(java.util.Collections.emptyMap) Logger(org.slf4j.Logger) Job(com.spotify.helios.common.descriptors.Job) KeeperException(org.apache.zookeeper.KeeperException) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) MoreObjects(com.google.common.base.MoreObjects) IOException(java.io.IOException) ZooKeeperOperation(com.spotify.helios.servicescommon.coordination.ZooKeeperOperation) Maps(com.google.common.collect.Maps) DeploymentGroupEventFactory(com.spotify.helios.rollingupdate.DeploymentGroupEventFactory) RolloutTask(com.spotify.helios.common.descriptors.RolloutTask) Node(com.spotify.helios.servicescommon.coordination.Node) Ordering(com.google.common.collect.Ordering) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) JobStatus(com.spotify.helios.common.descriptors.JobStatus) ZooKeeperClientProvider(com.spotify.helios.servicescommon.coordination.ZooKeeperClientProvider) DeploymentGroupStatus(com.spotify.helios.common.descriptors.DeploymentGroupStatus) ZooKeeperOperations.check(com.spotify.helios.servicescommon.coordination.ZooKeeperOperations.check) Preconditions(com.google.common.base.Preconditions) AgentInfo(com.spotify.helios.common.descriptors.AgentInfo) Comparator(java.util.Comparator) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException) Collections(java.util.Collections) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) DeploymentGroupTasks(com.spotify.helios.common.descriptors.DeploymentGroupTasks) RolloutTask(com.spotify.helios.common.descriptors.RolloutTask) RollingUpdateOpFactory(com.spotify.helios.rollingupdate.RollingUpdateOpFactory)

Example 35 with ZooKeeperClient

use of com.spotify.helios.servicescommon.coordination.ZooKeeperClient in project helios by spotify.

the class ZooKeeperMasterModel method deregisterHost.

/**
 * Undoes the effect of {@link ZooKeeperMasterModel#registerHost(String, String)}.  Cleans up
 * any leftover host-related things.
 */
@Override
public void deregisterHost(final String host) throws HostNotFoundException, HostStillInUseException {
    final ZooKeeperClient client = provider.get("deregisterHost");
    ZooKeeperRegistrarUtil.deregisterHost(client, host);
}
Also used : ZooKeeperClient(com.spotify.helios.servicescommon.coordination.ZooKeeperClient)

Aggregations

ZooKeeperClient (com.spotify.helios.servicescommon.coordination.ZooKeeperClient)43 HeliosRuntimeException (com.spotify.helios.common.HeliosRuntimeException)20 KeeperException (org.apache.zookeeper.KeeperException)18 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)15 DefaultZooKeeperClient (com.spotify.helios.servicescommon.coordination.DefaultZooKeeperClient)13 Job (com.spotify.helios.common.descriptors.Job)12 Test (org.junit.Test)12 DeploymentGroup (com.spotify.helios.common.descriptors.DeploymentGroup)11 ZooKeeperOperation (com.spotify.helios.servicescommon.coordination.ZooKeeperOperation)11 IOException (java.io.IOException)10 RolloutTask (com.spotify.helios.common.descriptors.RolloutTask)8 DeploymentGroupTasks (com.spotify.helios.common.descriptors.DeploymentGroupTasks)6 JobId (com.spotify.helios.common.descriptors.JobId)6 Deployment (com.spotify.helios.common.descriptors.Deployment)5 CuratorFramework (org.apache.curator.framework.CuratorFramework)5 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)5 DeploymentGroupStatus (com.spotify.helios.common.descriptors.DeploymentGroupStatus)4 SetData (com.spotify.helios.servicescommon.coordination.SetData)4 RetryPolicy (org.apache.curator.RetryPolicy)4 BadVersionException (org.apache.zookeeper.KeeperException.BadVersionException)4