gpt4 book ai didi

org.lilyproject.util.zookeeper.ZooKeeperItf.retryOperation()方法的使用及代码示例

转载 作者:知者 更新时间:2024-03-14 20:58:49 27 4
gpt4 key购买 nike

本文整理了Java中org.lilyproject.util.zookeeper.ZooKeeperItf.retryOperation()方法的一些代码示例,展示了ZooKeeperItf.retryOperation()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZooKeeperItf.retryOperation()方法的具体详情如下:
包路径:org.lilyproject.util.zookeeper.ZooKeeperItf
类名称:ZooKeeperItf
方法名:retryOperation

ZooKeeperItf.retryOperation介绍

[英]Perform the given operation, retrying in case of connection loss.

Note that in case of connection loss, you are never sure if the operation succeeded or not, so it might be executed twice. Therefore:

  • in case of a delete operation, be prepared to deal with a NoNode exception
  • in case of a create operation, be prepared to deal with a NodeExists exception
  • in case of creation of a sequential node, two nodes might have been created. If they are ephemeral, you can use Stat.ephemeralOwner to find out the ones that belong to the current session. Otherwise, embed the necessary identification into the name or data.

Do not call this method from within a ZooKeeper watcher callback, as it might block for a longer time and hence block the delivery of other events, including the Disconnected event.

This is a Lily-specific method.
[中]执行给定的操作,在连接丢失时重试。
请注意,在连接丢失的情况下,您永远无法确定操作是否成功,因此可能会执行两次。因此:
*如果是删除操作,请准备好处理非节点异常
*如果是创建操作,请准备好处理NodeExists异常
*在创建顺序节点的情况下,可能已经创建了两个节点。如果它们是短暂的,可以使用Stat.ephemeralOwner查找属于当前会话的。否则,在名称或数据中嵌入必要的标识。
不要在ZooKeeper watcher回调中调用此方法,因为它可能会阻塞更长的时间,从而阻塞其他事件的传递,包括断开连接的事件。
这是一种百合特有的方法。

代码示例

代码示例来源:origin: NGDATA/lilyproject

/**
 * 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;
    }
  });
}

代码示例来源:origin: NGDATA/lilyproject

/**
 * Deletes a path (non-recursively) in ZooKeeper, if it exists.
 * <p>
 * If the path doesn't exist, the delete will fail silently. The delete operation is retried until it succeeds, or
 * until it fails with a non-recoverable error.
 * <p>
 * If the path has children, the operation will fail with the underlying {@link NotEmptyException}.
 *
 * @param zk Handle to the ZooKeeper where the delete will occur
 * @param path The path to be deleted
 */
public static void deleteNode(final ZooKeeperItf zk, final String path) throws InterruptedException,
    KeeperException {
  zk.retryOperation(new ZooKeeperOperation<Boolean>() {
    @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;
    }
  });
}

代码示例来源:origin: NGDATA/lilyproject

/**
   * Gets data from a zookeeper node.
   * <p>
   * This operation is retried until it succeeds.
   */
  public static byte[] getData(final ZooKeeperItf zk, final String path, final Watcher watcher, final Stat stat)
      throws InterruptedException, KeeperException {
    final List<byte[]> data = new ArrayList<byte[]>(1);
    zk.retryOperation(new ZooKeeperOperation<Boolean>() {
      @Override
      public Boolean execute() throws KeeperException, InterruptedException {
        data.add(zk.getData(path, watcher, stat));
        return null;
      }
    });
    return data.get(0);
  }
}

代码示例来源:origin: NGDATA/lilyproject

@Override
public <T> T retryOperation(com.ngdata.sep.util.zookeeper.ZooKeeperOperation<T> operation) throws InterruptedException, KeeperException {
  return wrapped.retryOperation(new ZooKeeperOperationAdapter<T>(operation));
}

代码示例来源:origin: NGDATA/lilyproject

created = zk.retryOperation(new ZooKeeperOperation<Boolean>() {
  @Override
  public Boolean execute() throws KeeperException, InterruptedException {
zk.retryOperation(new ZooKeeperOperation<Boolean>() {
  @Override
  public Boolean execute() throws KeeperException, InterruptedException {

代码示例来源:origin: NGDATA/lilyproject

/**
 * 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);
  }
}

代码示例来源:origin: NGDATA/lilyproject

@PostConstruct
  public void start() throws IOException, InterruptedException, KeeperException {
    // Publish our address
    ZkUtil.createPath(zk, nodesPath);
    final String repoAddressAndPort = hostAddress + ":" + port;
    zk.retryOperation(new ZooKeeperOperation<Object>() {
      @Override
      public Object execute() throws KeeperException, InterruptedException {
        zk.create(nodesPath + "/" + repoAddressAndPort, null, ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.EPHEMERAL);
        return null;
      }
    });

    // Publish HBase configuration for LilyClient use
    // Translate HBase conf into json
    ObjectNode propertiesNode = JsonNodeFactory.instance.objectNode();
    for (Map.Entry<String, String> propertyEntry : hbaseConf) {
      if (!propertyEntry.getKey().equals(HConstants.HBASE_CLIENT_INSTANCE_ID)) {
        propertiesNode.put(propertyEntry.getKey(), propertyEntry.getValue());
      }
    }
    // TODO we could compare with current state and log a warn if its different
    ZkUtil.createPath(zk, hbaseConfPath, JsonFormat.serializeAsBytes(propertiesNode));
  }
}

代码示例来源:origin: NGDATA/lilyproject

@Override
public int run(CommandLine cmd) throws Exception {
  int result = super.run(cmd);
  if (result != 0) {
    return result;
  }
  zk = new StateWatchingZooKeeper(zkConnectionString, zkSessionTimeout);
  boolean lilyNodeExists = zk.retryOperation(new ZooKeeperOperation<Boolean>() {
    @Override
    public Boolean execute() throws KeeperException, InterruptedException {
      return zk.exists("/lily", false) != null;
    }
  });
  if (!lilyNodeExists) {
    if (!cmd.hasOption(forceOption.getOpt())) {
      System.out.println("No /lily node found in ZooKeeper. Are you sure you are connecting to the right");
      System.out.println("ZooKeeper? If so, use the option --" + forceOption.getLongOpt() +
          " to bypass this check.");
      return 1;
    } else {
      System.out.println("No /lily node found in ZooKeeper. Will continue anyway since you supplied --" +
          forceOption.getLongOpt());
      System.out.println();
    }
  }
  repositoryModel = new RepositoryModelImpl(zk);
  return 0;
}

代码示例来源:origin: NGDATA/lilyproject

private void proposeAsLeader() throws LeaderElectionSetupException, InterruptedException, KeeperException {
  ZkUtil.createPath(zk, electionPath);
  try {
    // In case of connection loss, a node might have been created for us (we do not know it). Therefore,
    // retrying upon connection loss is important, so that we can continue with watching the leaders.
    // Later on, we do not look at the name of the node we created here, but at the owner.
    zk.retryOperation(new ZooKeeperOperation<String>() {
      @Override
      public String execute() throws KeeperException, InterruptedException {
        return zk.create(electionPath + "/n_", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
      }
    });
  } catch (KeeperException e) {
    throw new LeaderElectionSetupException("Error creating leader election zookeeper node below " +
        electionPath, e);
  }
  watchLeaders();
}

代码示例来源:origin: NGDATA/lilyproject

/**
 * Releases a lock.
 *
 * @param lockId the string returned by {@link ZkLock#lock}.
 * @param ignoreMissing if true, do not throw an exception if the lock does not exist
 */
public static void unlock(final ZooKeeperItf zk, final String lockId, boolean ignoreMissing) throws ZkLockException {
  if (zk.isCurrentThreadEventThread()) {
    throw new RuntimeException("ZkLock should not be used from within the ZooKeeper event thread.");
  }
  try {
    zk.retryOperation(new ZooKeeperOperation<Object>() {
      @Override
      public Object execute() throws KeeperException, InterruptedException {
        zk.delete(lockId, -1);
        return null;
      }
    });
  } catch (KeeperException.NoNodeException e) {
    if (!ignoreMissing) {
      throw new ZkLockException("Error releasing lock: the lock does not exist. Path: " + lockId, e);
    }
  } catch (Throwable t) {
    throw new ZkLockException("Error releasing lock, path: " + lockId, t);
  }
}

代码示例来源:origin: NGDATA/lilyproject

zk.retryOperation(new ZooKeeperOperation<String>() {
  @Override
  public String execute() throws KeeperException, InterruptedException {
  List<ZkLockNode> children = parseChildren(zk.retryOperation(new ZooKeeperOperation<List<String>>() {
    @Override
    public List<String> execute() throws KeeperException, InterruptedException {
      Stat stat = zk.retryOperation(new ZooKeeperOperation<Stat>() {
        @Override
        public Stat execute() throws KeeperException, InterruptedException {
          zk.retryOperation(new ZooKeeperOperation<Object>() {
            @Override
            public Object execute() throws KeeperException, InterruptedException {
  final MyWatcher watcher = new MyWatcher(pathToWatch, condition);
  Stat stat = zk.retryOperation(new ZooKeeperOperation<Stat>() {
    @Override
    public Stat execute() throws KeeperException, InterruptedException {

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com