Search in sources :

Example 1 with NodeExistsException

use of org.apache.zookeeper.KeeperException.NodeExistsException in project zookeeper by apache.

the class InstanceManager method assignInstance.

public synchronized String assignInstance(String name, Class<? extends Instance> clazz, String params, int weight) throws NoAvailableContainers, DuplicateNameException, InterruptedException, KeeperException {
    if (weight < 1) {
        // if the weights are not above zero, things will get messed up
        weight = 1;
    }
    String instanceSpec = clazz.getName() + ' ' + params;
    if (instanceToAssignment.get(name) != null) {
        throw new DuplicateNameException(name + " already exists");
    }
    // find most idle node
    String mostIdle = null;
    int mostIdleWeight = Integer.MAX_VALUE;
    for (String preferred : preferredList) {
        HashSet<Assigned> assignmentList = assignments.get(preferred);
        int w = 0;
        if (assignmentList != null) {
            for (Assigned a : assignmentList) {
                w += a.weight;
            }
            if (w < mostIdleWeight) {
                mostIdleWeight = w;
                mostIdle = preferred;
            }
        }
    }
    for (Entry<String, HashSet<Assigned>> e : assignments.entrySet()) {
        int w = 0;
        for (Assigned a : e.getValue()) {
            w += a.weight;
        }
        if (w < mostIdleWeight) {
            mostIdleWeight = w;
            mostIdle = e.getKey();
        }
    }
    if (mostIdle == null) {
        throw new NoAvailableContainers("No available containers");
    }
    Assigned a = new Assigned(mostIdle, weight);
    instanceToAssignment.put(name, a);
    HashSet<Assigned> as = assignments.get(mostIdle);
    if (as == null) {
        as = new HashSet<Assigned>();
        assignments.put(mostIdle, as);
    }
    as.add(a);
    KeeperException lastException = null;
    for (int i = 0; i < maxTries; i++) {
        try {
            zk.create(assignmentsNode + '/' + mostIdle + '/' + name, instanceSpec.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
            return mostIdle;
        } catch (NodeExistsException e) {
            return mostIdle;
        } catch (KeeperException e) {
            lastException = e;
        }
    }
    throw lastException;
}
Also used : NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException) KeeperException(org.apache.zookeeper.KeeperException) HashSet(java.util.HashSet)

Example 2 with NodeExistsException

use of org.apache.zookeeper.KeeperException.NodeExistsException in project pulsar by yahoo.

the class LeaderElectionService method elect.

/**
     * We try to get the data in the ELECTION_ROOT node. If the node is present (i.e. leader is present), we store it in
     * the currentLeader and keep a watch on the election node. If we lose the leader, then watch gets triggered and we
     * do the election again. If the node does not exist while getting the data, we get NoNodeException. This means,
     * there is no leader and we create the node at ELECTION_ROOT and write the leader broker's service URL in the node.
     * Once the leader is known, we call the listener method so that leader can take further actions.
     */
private void elect() {
    try {
        byte[] data = zkClient.getData(ELECTION_ROOT, new Watcher() {

            @Override
            public void process(WatchedEvent event) {
                log.warn("Type of the event is [{}] and path is [{}]", event.getType(), event.getPath());
                switch(event.getType()) {
                    case NodeDeleted:
                        log.warn("Election node {} is deleted, attempting re-election...", event.getPath());
                        if (event.getPath().equals(ELECTION_ROOT)) {
                            log.info("This should call elect again...");
                            executor.execute(new Runnable() {

                                @Override
                                public void run() {
                                    // If the node is deleted, attempt the re-election
                                    log.info("Broker [{}] is calling re-election from the thread", pulsar.getWebServiceAddress());
                                    elect();
                                }
                            });
                        }
                        break;
                    default:
                        log.warn("Got something wrong on watch: {}", event);
                        break;
                }
            }
        }, null);
        LeaderBroker leaderBroker = jsonMapper.readValue(data, LeaderBroker.class);
        currentLeader.set(leaderBroker);
        isLeader.set(false);
        leaderListener.brokerIsAFollowerNow();
        // If broker comes here it is a follower. Do nothing, wait for the watch to trigger
        log.info("Broker [{}] is the follower now. Waiting for the watch to trigger...", pulsar.getWebServiceAddress());
    } catch (NoNodeException nne) {
        // There's no leader yet... try to become the leader
        try {
            // Create the root node and add current broker's URL as its contents
            LeaderBroker leaderBroker = new LeaderBroker(pulsar.getWebServiceAddress());
            ZkUtils.createFullPathOptimistic(pulsar.getLocalZkCache().getZooKeeper(), ELECTION_ROOT, jsonMapper.writeValueAsBytes(leaderBroker), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
            // Update the current leader and set the flag to true
            currentLeader.set(new LeaderBroker(leaderBroker.getServiceUrl()));
            isLeader.set(true);
            // Notify the listener that this broker is now the leader so that it can collect usage and start load
            // manager.
            log.info("Broker [{}] is the leader now, notifying the listener...", pulsar.getWebServiceAddress());
            leaderListener.brokerIsTheLeaderNow();
        } catch (NodeExistsException nee) {
            // Re-elect the new leader
            log.warn("Got exception [{}] while creating election node because it already exists. Attempting re-election...", nee.getMessage());
            executor.execute(new Runnable() {

                @Override
                public void run() {
                    elect();
                }
            });
        } catch (Exception e) {
            // Kill the broker because this broker's session with zookeeper might be stale. Killing the broker will
            // make sure that we get the fresh zookeeper session.
            log.error("Got exception [{}] while creating the election node", e.getMessage());
            pulsar.getShutdownService().shutdown(-1);
        }
    } catch (Exception e) {
        // Kill the broker
        log.error("Could not get the content of [{}], got exception [{}]. Shutting down the broker...", ELECTION_ROOT, e);
        pulsar.getShutdownService().shutdown(-1);
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException) Watcher(org.apache.zookeeper.Watcher) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException)

Example 3 with NodeExistsException

use of org.apache.zookeeper.KeeperException.NodeExistsException in project pulsar by yahoo.

the class LocalZooKeeperConnectionService method createIfAbsent.

public static String createIfAbsent(ZooKeeper zk, String path, byte[] data, CreateMode createMode, boolean gc) throws KeeperException, InterruptedException {
    String pathCreated = null;
    try {
        pathCreated = zk.create(path, data, Ids.OPEN_ACL_UNSAFE, createMode);
    } catch (NodeExistsException e) {
        // OK
        LOG.debug("Create skipped for existing znode: path={}", path);
    }
    // reset if what exists is the ephemeral garbage.
    if (gc && (pathCreated == null) && CreateMode.EPHEMERAL.equals(createMode)) {
        Stat stat = zk.exists(path, false);
        if (stat != null && zk.getSessionId() != stat.getEphemeralOwner()) {
            deleteIfExists(zk, path, -1);
            pathCreated = zk.create(path, data, Ids.OPEN_ACL_UNSAFE, createMode);
        }
    }
    return pathCreated;
}
Also used : Stat(org.apache.zookeeper.data.Stat) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException)

Example 4 with NodeExistsException

use of org.apache.zookeeper.KeeperException.NodeExistsException in project drill by apache.

the class ZookeeperClient method putIfAbsent.

/**
   * Puts the given byte sequence into the given path if path is does not exist.
   *
   * @param path  target path
   * @param data  data to store
   * @return null if path was created, else data stored for the given path
   */
public byte[] putIfAbsent(final String path, final byte[] data) {
    Preconditions.checkNotNull(path, "path is required");
    Preconditions.checkNotNull(data, "data is required");
    final String target = PathUtils.join(root, path);
    try {
        try {
            curator.create().withMode(mode).forPath(target, data);
            getCache().rebuildNode(target);
            return null;
        } catch (NodeExistsException e) {
        // do nothing
        }
        return curator.getData().forPath(target);
    } catch (final Exception e) {
        throw new DrillRuntimeException("unable to put ", e);
    }
}
Also used : NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) KeeperException(org.apache.zookeeper.KeeperException) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) VersionMismatchException(org.apache.drill.exec.exception.VersionMismatchException) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException)

Example 5 with NodeExistsException

use of org.apache.zookeeper.KeeperException.NodeExistsException in project drill by apache.

the class ZookeeperClient method put.

/**
   * Puts the given byte sequence into the given path.
   *
   * If path does not exists, this call creates it.
   *
   * If version holder is not null and path already exists, passes given version for comparison.
   * Zookeeper maintains stat structure that holds version number which increases each time znode data change is performed.
   * If we pass version that doesn't match the actual version of the data,
   * the update will fail {@link org.apache.zookeeper.KeeperException.BadVersionException}.
   * We catch such exception and re-throw it as {@link VersionMismatchException}.
   * Link to documentation - https://zookeeper.apache.org/doc/r3.2.2/zookeeperProgrammers.html#sc_zkDataModel_znodes
   *
   * @param path  target path
   * @param data  data to store
   * @param version version holder
   */
public void put(final String path, final byte[] data, DataChangeVersion version) {
    Preconditions.checkNotNull(path, "path is required");
    Preconditions.checkNotNull(data, "data is required");
    final String target = PathUtils.join(root, path);
    try {
        // we make a consistent read to ensure this call won't fail upon consecutive calls on the same path
        // before cache is updated
        boolean hasNode = hasPath(path, true);
        if (!hasNode) {
            try {
                curator.create().withMode(mode).forPath(target, data);
            } catch (NodeExistsException e) {
                // Handle race conditions since Drill is distributed and other
                // drillbits may have just created the node. This assumes that we do want to
                // override the new node. Makes sense here, because if the node had existed,
                // we'd have updated it.
                hasNode = true;
            }
        }
        if (hasNode) {
            if (version != null) {
                try {
                    curator.setData().withVersion(version.getVersion()).forPath(target, data);
                } catch (final KeeperException.BadVersionException e) {
                    throw new VersionMismatchException("Unable to put data. Version mismatch is detected.", version.getVersion(), e);
                }
            } else {
                curator.setData().forPath(target, data);
            }
        }
        getCache().rebuildNode(target);
    } catch (final VersionMismatchException e) {
        throw e;
    } catch (final Exception e) {
        throw new DrillRuntimeException("unable to put ", e);
    }
}
Also used : NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) VersionMismatchException(org.apache.drill.exec.exception.VersionMismatchException) KeeperException(org.apache.zookeeper.KeeperException) KeeperException(org.apache.zookeeper.KeeperException) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) VersionMismatchException(org.apache.drill.exec.exception.VersionMismatchException) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException)

Aggregations

NodeExistsException (org.apache.zookeeper.KeeperException.NodeExistsException)15 KeeperException (org.apache.zookeeper.KeeperException)8 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)4 HeliosRuntimeException (com.spotify.helios.common.HeliosRuntimeException)3 JobId (com.spotify.helios.common.descriptors.JobId)3 ZooKeeperClient (com.spotify.helios.servicescommon.coordination.ZooKeeperClient)2 UUID (java.util.UUID)2 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)2 VersionMismatchException (org.apache.drill.exec.exception.VersionMismatchException)2 CreateMode (org.apache.zookeeper.CreateMode)2 JCommander (com.beust.jcommander.JCommander)1 Job (com.spotify.helios.common.descriptors.Job)1 RolloutTask (com.spotify.helios.common.descriptors.RolloutTask)1 Task (com.spotify.helios.common.descriptors.Task)1 TaskStatusEvent (com.spotify.helios.common.descriptors.TaskStatusEvent)1 ZooKeeperOperation (com.spotify.helios.servicescommon.coordination.ZooKeeperOperation)1 ClusterData (com.yahoo.pulsar.common.policies.data.ClusterData)1 ZooKeeperClientFactory (com.yahoo.pulsar.zookeeper.ZooKeeperClientFactory)1 ZookeeperClientFactoryImpl (com.yahoo.pulsar.zookeeper.ZookeeperClientFactoryImpl)1 IOException (java.io.IOException)1