示例#1
0
 /** Allows package private classes to update volatile ZooKeeper. */
 void updateKeeper(SolrZooKeeper keeper) throws InterruptedException {
   SolrZooKeeper oldKeeper = this.keeper;
   this.keeper = keeper;
   if (oldKeeper != null) {
     oldKeeper.close();
   }
   // we might have been closed already
   if (isClosed) this.keeper.close();
 }
示例#2
0
 private void closeKeeper(SolrZooKeeper keeper) {
   if (keeper != null) {
     try {
       keeper.close();
     } catch (InterruptedException e) {
       // Restore the interrupted status
       Thread.currentThread().interrupt();
       log.error("", e);
       throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR, "", e);
     }
   }
 }
示例#3
0
 /** Returns children of the node at the path */
 public List<String> getChildren(final String path, final Watcher watcher, boolean retryOnConnLoss)
     throws KeeperException, InterruptedException {
   if (retryOnConnLoss) {
     return zkCmdExecutor.retryOperation(
         new ZkOperation() {
           @Override
           public List<String> execute() throws KeeperException, InterruptedException {
             return keeper.getChildren(path, watcher);
           }
         });
   } else {
     return keeper.getChildren(path, watcher);
   }
 }
示例#4
0
 /** Returns true if path exists */
 public Boolean exists(final String path, boolean retryOnConnLoss)
     throws KeeperException, InterruptedException {
   if (retryOnConnLoss) {
     return zkCmdExecutor.retryOperation(
         new ZkOperation() {
           @Override
           public Boolean execute() throws KeeperException, InterruptedException {
             return keeper.exists(path, null) != null;
           }
         });
   } else {
     return keeper.exists(path, null) != null;
   }
 }
示例#5
0
 /** Returns path of created node */
 public String create(
     final String path, final byte[] data, final CreateMode createMode, boolean retryOnConnLoss)
     throws KeeperException, InterruptedException {
   if (retryOnConnLoss) {
     return zkCmdExecutor.retryOperation(
         new ZkOperation() {
           @Override
           public String execute() throws KeeperException, InterruptedException {
             return keeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, createMode);
           }
         });
   } else {
     return keeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, createMode);
   }
 }
示例#6
0
 /** Returns node's state */
 public Stat setData(
     final String path, final byte data[], final int version, boolean retryOnConnLoss)
     throws KeeperException, InterruptedException {
   if (retryOnConnLoss) {
     return zkCmdExecutor.retryOperation(
         new ZkOperation() {
           @Override
           public Stat execute() throws KeeperException, InterruptedException {
             return keeper.setData(path, data, version);
           }
         });
   } else {
     return keeper.setData(path, data, version);
   }
 }
示例#7
0
 /** Returns node's data */
 public byte[] getData(
     final String path, final Watcher watcher, final Stat stat, boolean retryOnConnLoss)
     throws KeeperException, InterruptedException {
   if (retryOnConnLoss) {
     return zkCmdExecutor.retryOperation(
         new ZkOperation() {
           @Override
           public byte[] execute() throws KeeperException, InterruptedException {
             return keeper.getData(path, watcher, stat);
           }
         });
   } else {
     return keeper.getData(path, watcher, stat);
   }
 }
示例#8
0
 public void delete(final String path, final int version, boolean retryOnConnLoss)
     throws InterruptedException, KeeperException {
   if (retryOnConnLoss) {
     zkCmdExecutor.retryOperation(
         new ZkOperation() {
           @Override
           public Stat execute() throws KeeperException, InterruptedException {
             keeper.delete(path, version);
             return null;
           }
         });
   } else {
     keeper.delete(path, version);
   }
 }
示例#9
0
 /** Returns path of created node */
 public String create(
     final String path,
     final byte data[],
     final List<ACL> acl,
     final CreateMode createMode,
     boolean retryOnConnLoss)
     throws KeeperException, InterruptedException {
   if (retryOnConnLoss) {
     return zkCmdExecutor.retryOperation(
         new ZkOperation() {
           @Override
           public String execute() throws KeeperException, InterruptedException {
             return keeper.create(path, data, acl, createMode);
           }
         });
   } else {
     return keeper.create(path, data, acl, createMode);
   }
 }
示例#10
0
  /**
   * Creates the path in ZooKeeper, creating each node as necessary.
   *
   * <p>e.g. If <code>path=/solr/group/node</code> and none of the nodes, solr, group, node exist,
   * each will be created.
   *
   * <p>Note: retryOnConnLoss is only respected for the final node - nodes before that are always
   * retried on connection loss.
   */
  public void makePath(
      String path,
      byte[] data,
      CreateMode createMode,
      Watcher watcher,
      boolean failOnExists,
      boolean retryOnConnLoss)
      throws KeeperException, InterruptedException {
    if (log.isInfoEnabled()) {
      log.info("makePath: " + path);
    }
    boolean retry = true;

    if (path.startsWith("/")) {
      path = path.substring(1, path.length());
    }
    String[] paths = path.split("/");
    StringBuilder sbPath = new StringBuilder();
    for (int i = 0; i < paths.length; i++) {
      byte[] bytes = null;
      String pathPiece = paths[i];
      sbPath.append("/" + pathPiece);
      final String currentPath = sbPath.toString();
      Object exists = exists(currentPath, watcher, retryOnConnLoss);
      if (exists == null || ((i == paths.length - 1) && failOnExists)) {
        CreateMode mode = CreateMode.PERSISTENT;
        if (i == paths.length - 1) {
          mode = createMode;
          bytes = data;
          if (!retryOnConnLoss) retry = false;
        }
        try {
          if (retry) {
            final CreateMode finalMode = mode;
            final byte[] finalBytes = bytes;
            zkCmdExecutor.retryOperation(
                new ZkOperation() {
                  @Override
                  public Object execute() throws KeeperException, InterruptedException {
                    keeper.create(currentPath, finalBytes, ZooDefs.Ids.OPEN_ACL_UNSAFE, finalMode);
                    return null;
                  }
                });
          } else {
            keeper.create(currentPath, bytes, ZooDefs.Ids.OPEN_ACL_UNSAFE, mode);
          }
        } catch (NodeExistsException e) {

          if (!failOnExists) {
            // TODO: version ? for now, don't worry about race
            setData(currentPath, data, -1, retryOnConnLoss);
            // set new watch
            exists(currentPath, watcher, retryOnConnLoss);
            return;
          }

          // ignore unless it's the last node in the path
          if (i == paths.length - 1) {
            throw e;
          }
        }
        if (i == paths.length - 1) {
          // set new watch
          exists(currentPath, watcher, retryOnConnLoss);
        }
      } else if (i == paths.length - 1) {
        // TODO: version ? for now, don't worry about race
        setData(currentPath, data, -1, retryOnConnLoss);
        // set new watch
        exists(currentPath, watcher, retryOnConnLoss);
      }
    }
  }
示例#11
0
 /** Returns true if client is connected */
 public boolean isConnected() {
   return keeper != null && keeper.getState() == ZooKeeper.States.CONNECTED;
 }