@Override public Object execute() throws KeeperException, InterruptedException { byte[] data = zk.getData(indexerPath, false, null); String trashPath = indexerTrashPath + "/" + indexerName; // An indexer with the same name might have existed before and hence already exist // in the indexer trash, handle this by appending a sequence number until a unique // name is found. String baseTrashpath = trashPath; int count = 0; while (zk.exists(trashPath, false) != null) { count++; trashPath = baseTrashpath + "." + count; } zk.create(trashPath, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); return null; } });
try { if (zk.exists(indexerLockPath, false) != null) { List<String> children = Collections.emptyList(); try { children = zk.getChildren(indexerLockPath, false); } catch (KeeperException.NoNodeException e) { zk.delete(indexerLockPath + "/" + child, -1); } catch (KeeperException.NoNodeException e) { zk.delete(indexerLockPath, -1); } catch (KeeperException.NoNodeException e) { zk.delete(indexerPath, -1);
/** * Updates data on a zookeeper node. * * <p> * The supplied data is used for the last node in the path. The path must * already exist. It is not checked if the data is changed or not. This will * cause the version of the node to be increased. * <p> * This operation is retried until it succeeds. */ public static void update(final ZooKeeperItf zk, final String path, final byte[] data, final int version) throws InterruptedException, KeeperException { zk.retryOperation(new ZooKeeperOperation<Boolean>() { @Override public Boolean execute() throws KeeperException, InterruptedException { zk.setData(path, data, version); return null; } }); }
@Override public Boolean execute() throws KeeperException, InterruptedException { if (zk.exists(subPath.toString(), false) == null) { try { zk.create(subPath.toString(), newData, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); return true; } catch (KeeperException.NodeExistsException e) { return false; } } return false; } });
if (zk.exists(regionServerPath, false) == null) { return new ReplicationStatus(statusByPeerAndServer); List<String> regionServers = zk.getChildren(regionServerPath, false); peers = zk.getChildren(peersPath, false); } catch (KeeperException.NoNodeException e) { logs.addAll(zk.getChildren(hlogsPath, false)); } catch (KeeperException.NoNodeException e) { byte[] data = zk.getData(hlogsPath + "/" + log, false, stat);
@Override public Stat execute() throws KeeperException, InterruptedException { return zk.exists(childPath, false); } });
List<String> children = zk.getChildren(electionPath, watcher); Stat stat = zk.exists(electionPath + "/" + leader, false); if (stat.getEphemeralOwner() == zk.getSessionId() && !elected) { elected = true; log.info("Elected as leader for the position of " + position);
@Override public Boolean execute() throws KeeperException, InterruptedException { Stat stat = zk.exists(path, false); if (stat != null) { try { zk.delete(path, stat.getVersion()); } catch (KeeperException.NoNodeException nne) { // This is ok, the node is already gone } // We don't catch BadVersion or NotEmpty as these are probably signs that there is something // unexpected going on with the node that is to be deleted } return true; } });
@Override public String execute() throws KeeperException, InterruptedException { return zk.create(electionPath + "/n_", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); } });
@Override public List<IndexerProcess> execute() throws KeeperException, InterruptedException { List<IndexerProcess> indexerProcesses = Lists.newArrayList(); for (String childNode : zk.getChildren(zkBaseNode, false)) { List<String> nodeNameParts = Lists.newArrayList(Splitter.on(',').split(childNode)); if (indexerName.equals(nodeNameParts.get(0))) { byte[] errorBytes = zk.getData(zkBaseNode + "/" + childNode, false, null); IndexerProcess indexerProcess = new IndexerProcess(indexerName, nodeNameParts.get(1), errorBytes == null || errorBytes.length == 0? null : Bytes.toString(errorBytes)); indexerProcesses.add(indexerProcess); } } return indexerProcesses; }}); } catch (Exception e) {
private IndexerDefinition loadIndexer(String indexerName, boolean forCache) throws InterruptedException, KeeperException, IndexerNotFoundException { final String childPath = indexerCollectionPath + "/" + indexerName; final Stat stat = new Stat(); byte[] data; try { if (forCache) { // do not retry, install watcher data = zk.getData(childPath, watcher, stat); } else { // do retry, do not install watcher data = zk.retryOperation(new ZooKeeperOperation<byte[]>() { @Override public byte[] execute() throws KeeperException, InterruptedException { return zk.getData(childPath, false, stat); } }); } } catch (KeeperException.NoNodeException e) { throw new IndexerNotFoundException(indexerName); } IndexerDefinitionBuilder builder = IndexerDefinitionJsonSerDeser.INSTANCE.fromJsonBytes(data); builder.name(indexerName); builder.occVersion(stat.getVersion()); return builder.build(); }
/** * Verifies that the specified lockId is the owner of the lock. */ public static boolean ownsLock(final ZooKeeperItf zk, final String lockId) throws ZkLockException { if (zk.isCurrentThreadEventThread()) { throw new RuntimeException("ZkLock should not be used from within the ZooKeeper event thread."); } try { int lastSlashPos = lockId.lastIndexOf('/'); final String lockPath = lockId.substring(0, lastSlashPos); String lockName = lockId.substring(lastSlashPos + 1); List<String> children = zk.retryOperation(new ZooKeeperOperation<List<String>>() { @Override public List<String> execute() throws KeeperException, InterruptedException { return zk.getChildren(lockPath, null); } }); if (children.isEmpty()) return false; SortedSet<String> sortedChildren = new TreeSet<String>(children); return sortedChildren.first().equals(lockName); } catch (Throwable t) { throw new ZkLockException("Error checking lock, path: " + lockId, t); } }
@Override public Object execute() throws KeeperException, InterruptedException { try { zk.delete(childPath, -1); } catch (KeeperException.NoNodeException e) { // ignore } return null; } });
@Override public List<String> execute() throws KeeperException, InterruptedException { return zk.getChildren(lockPath, null); } }));
@Override public byte[] execute() throws KeeperException, InterruptedException { return zk.getData(childPath, false, stat); } });
@Override public Boolean execute() throws KeeperException, InterruptedException { zk.setData(path, data, version); return null; } });
@PreDestroy public void stop() throws InterruptedException { stopped = true; zk.removeDefaultWatcher(connectStateWatcher); indexerCacheRefresher.shutdown(); }
/** * * @param position a name for what position this leader election is about, used in informational messages. * @param electionPath path under which the ephemeral leader election nodes should be created. The path * will be created if it does not exist. The path should not end on a slash. */ public LeaderElection(ZooKeeperItf zk, String position, String electionPath, LeaderElectionCallback callback) throws LeaderElectionSetupException, InterruptedException, KeeperException { this.zk = zk; this.position = position; this.electionPath = electionPath; this.callback = callback; proposeAsLeader(); zk.addDefaultWatcher(connectStateWatcher); leaderProvisioner.start(); }
@Override public Stat execute() throws KeeperException, InterruptedException { return zk.exists(lockPath, null); } });