/** * Retrieve zookeeper working folder. */ private static String getZooKeeperWorkingDir() { return getUserDir() + File.separator + "zookeeper"; }
@Override public void run() { readLines(); }
/** * Return the stat of the node of the given path. Return null if no such a node exists. * <p> * If the watch is true and the call is successful (no exception is thrown), a watch will be left on the node with * the given path. The watch will be triggered by a successful operation that creates/delete the node or sets the * data on the node. * * @param path * the node path * @param watch * whether need to watch this node * @return the stat of the node of the given path; return null if no such a node exists. * @throws KeeperException * If the server signals an error * @throws InterruptedException * If the server transaction is interrupted. */ public Stat exists(final String path, final boolean watch) throws KeeperException, InterruptedException { return retryOperation(new GuaguaZooKeeperOperation<Stat>() { @Override public Stat execute() throws KeeperException, InterruptedException { return getZooKeeper().exists(path, watch); } }); }
/** * Start zookeeper server in thread of master node. */ private String startZookeeperServer(String localHostName) { int embedZkClientPort = 0; try { embedZkClientPort = ZooKeeperUtils.startEmbedZooKeeper(); } catch (IOException e) { throw new RuntimeException(e); } // 2. check if it is started. ZooKeeperUtils.checkIfEmbedZooKeeperStarted(embedZkClientPort); return localHostName + ":" + embedZkClientPort; }
/** * Perform the given operation, retrying if the connection fails * * @return object. it needs to be cast to the callee's expected return type. */ protected <T> T retryOperation(GuaguaZooKeeperOperation<T> operation) throws KeeperException, InterruptedException { KeeperException exception = null; for(int i = 0; i < this.getMaxRetryAttempts(); i++) { try { return operation.execute(); } catch (KeeperException.SessionExpiredException e) { LOG.warn("Session expired so reconnecting due to:", e); throw e; } catch (KeeperException.ConnectionLossException e) { if(exception == null) { exception = e; } LOG.debug("Attempt {} failed with connection loss so attempting to reconnect. Exception is: {} ", i, e); retryDelay(i); } } throw exception; }
/** * Create a znode if there is no other znode there * * @param path * path to create * @param data * data to set on the final znode * @param acl * acls on each znode created * @param createMode * only affects the final znode * @param recursive * if true, creates all ancestors * @return Path of created znode or Stat of set znode * @throws InterruptedException * @throws KeeperException * Both KeeperException InterruptedException are thrown from {@link ZooKeeper} methods. */ public PathStat createOnceExt(final String path, byte[] data, List<ACL> acl, CreateMode createMode, boolean recursive) throws KeeperException, InterruptedException { try { return new PathStat(createExt(path, data, acl, createMode, recursive), null); } catch (KeeperException.NodeExistsException e) { LOG.warn("createOnceExt: Node already exists on path {}", path); } return null; }
/** * Close this client object. Once the client is closed, its session becomes invalid. All the ephemeral nodes in the * ZooKeeper server associated with the session will be removed. The watches left on those nodes (and on their * parents) will be triggered. * * @throws InterruptedException * in case of InterruptedException from {@code ZooKeeper#close()}; */ public void close() throws InterruptedException { getZooKeeper().close(); }
/** * Performs a retry delay if this is not the first attempt * * @param attemptCount * the number of the attempts performed so far */ protected void retryDelay(int attemptCount) { if(attemptCount > 0) { try { Thread.sleep(attemptCount * getRetryWaitMsecs()); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } }
@Override public BytesPair call() throws Exception { byte[] data = zookeeper.getData(znode, null, null); return new BytesPair(index, data); }
/** * Start zookeeper server in thread of master node. */ private String startZookeeperServer(String localHostName) { int embedZkClientPort = 0; try { embedZkClientPort = ZooKeeperUtils.startEmbedZooKeeper(); } catch (IOException e) { throw new RuntimeException(e); } // 2. check if it is started. ZooKeeperUtils.checkIfEmbedZooKeeperStarted(embedZkClientPort); return localHostName + ":" + embedZkClientPort; }
@Override public Stat execute() throws KeeperException, InterruptedException { return getZooKeeper().exists(path, watch); } });
/** * Return the stat of the node of the given path. Return null if no such a node exists. * <p> * If the watch is non-null and the call is successful (no exception is thrown), a watch will be left on the node * with the given path. The watch will be triggered by a successful operation that creates/delete the node or sets * the data on the node. * * @param path * the node path * @param watcher * explicit watcher * @return the stat of the node of the given path; return null if no such a * node exists. * @throws KeeperException * If the server signals an error * @throws InterruptedException * If the server transaction is interrupted. * @throws IllegalArgumentException * if an invalid path is specified */ public Stat exists(final String path, final Watcher watcher) throws KeeperException, InterruptedException { return retryOperation(new GuaguaZooKeeperOperation<Stat>() { @Override public Stat execute() throws KeeperException, InterruptedException { return getZooKeeper().exists(path, watcher); } }); }
/** * Retrieve zookeeper working folder. */ private static String getZooKeeperWorkingDir(String subFolder) { return getUserDir() + File.separator + subFolder; }
/** * Dump the last n lines of the collector. Likely used in the case of failure. * * @param level * Log level to dump with */ @SuppressWarnings("unused") public synchronized void dumpLastLines() { // Get any remaining lines readLines(); // Dump the lines to the screen for(String line: lastLines) { LOG.info(line); } }
/** * Start zookeeper server in thread of master node. */ private String startZookeeperServer(String localHostName) { int embedZkClientPort = 0; try { embedZkClientPort = ZooKeeperUtils.startEmbedZooKeeper(); } catch (IOException e) { throw new RuntimeException(e); } // 2. check if it is started. ZooKeeperUtils.checkIfEmbedZooKeeperStarted(embedZkClientPort); return localHostName + ":" + embedZkClientPort; }
@Override public Stat execute() throws KeeperException, InterruptedException { return getZooKeeper().exists(path, watcher); } });
/** * Return the data and the stat of the node of the given path. * <p> * If the watch is true and the call is successful (no exception is thrown), a watch will be left on the node with * the given path. The watch will be triggered by a successful operation that sets data on the node, or deletes the * node. * <p> * A KeeperException with error code KeeperException.NoNode will be thrown if no node with the given path exists. * * @param path * the given path * @param watch * whether need to watch this node * @param stat * the stat of the node * @return the data of the node * @throws KeeperException * If the server signals an error with a non-zero error code * @throws InterruptedException * If the server transaction is interrupted. */ public byte[] getData(final String path, final boolean watch, final Stat stat) throws KeeperException, InterruptedException { return retryOperation(new GuaguaZooKeeperOperation<byte[]>() { @Override public byte[] execute() throws KeeperException, InterruptedException { return getZooKeeper().getData(path, watch, stat); } }); }
/** * Start zookeeper server in thread of master node. */ private String startZookeeperServer(String localHostName) { int embedZkClientPort = 0; try { embedZkClientPort = ZooKeeperUtils.startEmbedZooKeeper(); } catch (IOException e) { throw new RuntimeException(e); } // 2. check if it is started. ZooKeeperUtils.checkIfEmbedZooKeeperStarted(embedZkClientPort); return localHostName + ":" + embedZkClientPort; }
@Override public byte[] execute() throws KeeperException, InterruptedException { return getZooKeeper().getData(path, watcher, stat); } });
@Override public byte[] execute() throws KeeperException, InterruptedException { return getZooKeeper().getData(path, watch, stat); } });