gpt4 book ai didi

com.twitter.finagle.common.zookeeper.ZooKeeperClient类的使用及代码示例

转载 作者:知者 更新时间:2024-03-13 11:07:24 27 4
gpt4 key购买 nike

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

ZooKeeperClient介绍

[英]Manages a connection to a ZooKeeper cluster.
[中]管理与ZooKeeper群集的连接。

代码示例

代码示例来源:origin: com.twitter/finagle-serversets

/**
 * Checks to see if the client might reasonably re-try an operation given the exception thrown
 * while attempting it.  If the ZooKeeper session should be expired to enable the re-try to
 * succeed this method will expire it as a side-effect.
 *
 * @param e the exception to test
 * @return true if a retry can be attempted
 */
public boolean shouldRetry(KeeperException e) {
 if (e instanceof SessionExpiredException) {
  close();
 }
 return ZooKeeperUtils.isRetryable(e);
}

代码示例来源:origin: com.twitter/finagle-serversets_2.11

/**
 * Creates an unconnected client that will lazily attempt to connect on the first call to
 * {@link #get()}.
 *
 * @param sessionTimeout the ZK session timeout
 * @param zooKeeperServer the first, required ZK server
 * @param zooKeeperServers any additional servers forming the ZK cluster
 */
public ZooKeeperClient(Duration sessionTimeout, InetSocketAddress zooKeeperServer,
  InetSocketAddress... zooKeeperServers) {
 this(sessionTimeout, combine(zooKeeperServer, zooKeeperServers));
}

代码示例来源:origin: com.twitter/finagle-serversets

private void watchGroup()
  throws ZooKeeperConnectionException, InterruptedException, KeeperException {
 if (stopped) {
  return;
 }
 List<String> children = zkClient.get().getChildren(path, groupWatcher);
 setMembers(children.stream()
  .filter(nodeNameFilter)
  .collect(Collectors.toList())
 );
}

代码示例来源:origin: com.twitter/finagle-serversets_2.11

public Runnable watch() throws WatchException, InterruptedException {
 Watcher onExpirationWatcher = zkClient.registerExpirationHandler(new Runnable() {
  @Override public void run() {
   // Servers may have changed Status while we were disconnected from ZooKeeper, check and
   // re-register our node watches.
   rebuildServerSet();
  }
 });
 try {
  return group.watch(new GroupChangeListener() {
   @Override public void onGroupChange(Iterable<String> memberIds) {
    notifyGroupChange(memberIds);
   }
  });
 } catch (WatchException e) {
  zkClient.unregister(onExpirationWatcher);
  throw e;
 } catch (InterruptedException e) {
  zkClient.unregister(onExpirationWatcher);
  throw e;
 }
}

代码示例来源:origin: com.twitter/finagle-serversets_2.11

@Override public Boolean get() throws JoinException {
  try {
   zkClient.get().delete(nodePath, ZooKeeperUtils.ANY_VERSION);
   return true;
  } catch (InterruptedException e) {
   Thread.currentThread().interrupt();
   throw new JoinException("Interrupted trying to cancel membership: " + nodePath, e);
  } catch (ZooKeeperConnectionException e) {
   LOG.log(Level.WARNING, "Problem connecting to ZooKeeper, retrying", e);
   return false;
  } catch (NoNodeException e) {
   LOG.info("Membership already cancelled, node at path: " + nodePath
        + " has been deleted");
   return true;
  } catch (KeeperException e) {
   if (zkClient.shouldRetry(e)) {
    LOG.log(Level.WARNING, "Temporary error cancelling membership: " + nodePath, e);
    return false;
   } else {
    throw new JoinException("Problem cancelling membership: " + nodePath, e);
   }
  }
 }
});

代码示例来源:origin: com.twitter/finagle-serversets_2.11

@Override public Boolean get() throws InterruptedException {
  try {
   watchGroup();
   return true;
  } catch (ZooKeeperConnectionException e) {
   LOG.log(Level.WARNING, "Problem connecting to ZooKeeper, retrying", e);
   return false;
  } catch (KeeperException e) {
   if (zkClient.shouldRetry(e)) {
    LOG.log(Level.WARNING, "Temporary error re-watching group: " + path, e);
    return false;
   } else {
    throw new IllegalStateException("Permanent problem re-watching group: " + path, e);
   }
  }
 }
};

代码示例来源:origin: com.twitter/finagle-serversets_2.11

zkClient.registerExpirationHandler(new Runnable() {
  @Override public void run() {
   tryJoin();
  ? CreateMode.EPHEMERAL_SEQUENTIAL
  : CreateMode.EPHEMERAL;
nodePath = zkClient.get().create(
 path + "/" + nodeName, updatedMembershipData, acl, createMode);
memberId = Group.this.getMemberId(nodePath);
zkClient.get().exists(nodePath, new Watcher() {
 @Override public void process(WatchedEvent event) {
  if (event.getType() == EventType.NodeDeleted) {

代码示例来源:origin: com.twitter/finagle-serversets

/**
 * Creates a set of credentials for the zoo keeper digest authentication mechanism.
 *
 * @param username the username to authenticate with
 * @param password the password to authenticate with
 * @return a set of credentials that can be used to authenticate the zoo keeper client
 */
public static Credentials digestCredentials(String username, String password) {
 MorePreconditions.checkNotBlank(username);
 Objects.requireNonNull(password);
 // TODO(John Sirois): DigestAuthenticationProvider is broken - uses platform default charset
 // (on server) and so we just have to hope here that clients are deployed in compatible jvms.
 // Consider writing and installing a version of DigestAuthenticationProvider that controls its
 // Charset explicitly.
 return credentials("digest", (username + ":" + password).getBytes());
}

代码示例来源:origin: com.twitter/finagle-serversets

/**
 * Clients that need to re-establish state after session expiration can register an
 * {@code onExpired} command to execute.
 *
 * @param onExpired the {@code Runnable} to register
 * @return the new {@link Watcher} which can later be passed to {@link #unregister} for
 *     removal.
 */
public Watcher registerExpirationHandler(final Runnable onExpired) {
 Watcher watcher = new Watcher() {
  @Override public void process(WatchedEvent event) {
   if (event.getType() == EventType.None && event.getState() == KeeperState.Expired) {
    onExpired.run();
   }
  }
 };
 register(watcher);
 return watcher;
}

代码示例来源:origin: com.twitter/finagle-serversets

synchronized void setMembers(Iterable<String> members) {
  if (stopped) {
   LOG.info("Suppressing membership update, no longer watching " + this);
   return;
  }
  if (this.members == null) {
   // Reset our watch on the group if session expires - only needs to be registered once.
   zkClient.registerExpirationHandler(new Runnable() {
    @Override public void run() {
     tryWatchGroup();
    }
   });
  }
  Set<String> membership = new HashSet<>();
  members.iterator().forEachRemaining(membership::add);
  if (!membership.equals(this.members)) {
   groupChangeListener.onGroupChange(members);
   this.members = Collections.unmodifiableSet(membership);
  }
 }
}

代码示例来源:origin: com.twitter/finagle-serversets

@Override public Boolean get() throws JoinException {
  try {
   zkClient.get().delete(nodePath, ZooKeeperUtils.ANY_VERSION);
   return true;
  } catch (InterruptedException e) {
   Thread.currentThread().interrupt();
   throw new JoinException("Interrupted trying to cancel membership: " + nodePath, e);
  } catch (ZooKeeperConnectionException e) {
   LOG.log(Level.WARNING, "Problem connecting to ZooKeeper, retrying", e);
   return false;
  } catch (NoNodeException e) {
   LOG.info("Membership already cancelled, node at path: " + nodePath
        + " has been deleted");
   return true;
  } catch (KeeperException e) {
   if (zkClient.shouldRetry(e)) {
    LOG.log(Level.WARNING, "Temporary error cancelling membership: " + nodePath, e);
    return false;
   } else {
    throw new JoinException("Problem cancelling membership: " + nodePath, e);
   }
  }
 }
});

代码示例来源:origin: com.twitter/finagle-serversets

@Override public Boolean get() throws InterruptedException {
  try {
   watchGroup();
   return true;
  } catch (ZooKeeperConnectionException e) {
   LOG.log(Level.WARNING, "Problem connecting to ZooKeeper, retrying", e);
   return false;
  } catch (KeeperException e) {
   if (zkClient.shouldRetry(e)) {
    LOG.log(Level.WARNING, "Temporary error re-watching group: " + path, e);
    return false;
   } else {
    throw new IllegalStateException("Permanent problem re-watching group: " + path, e);
   }
  }
 }
};

代码示例来源:origin: com.twitter/finagle-serversets

public Runnable watch() throws WatchException, InterruptedException {
 Watcher onExpirationWatcher = zkClient.registerExpirationHandler(new Runnable() {
  @Override public void run() {
   // Servers may have changed Status while we were disconnected from ZooKeeper, check and
   // re-register our node watches.
   rebuildServerSet();
  }
 });
 try {
  return group.watch(new GroupChangeListener() {
   @Override public void onGroupChange(Iterable<String> memberIds) {
    notifyGroupChange(memberIds);
   }
  });
 } catch (WatchException e) {
  zkClient.unregister(onExpirationWatcher);
  throw e;
 } catch (InterruptedException e) {
  zkClient.unregister(onExpirationWatcher);
  throw e;
 }
}

代码示例来源:origin: com.twitter/finagle-serversets

zkClient.registerExpirationHandler(new Runnable() {
  @Override public void run() {
   tryJoin();
  ? CreateMode.EPHEMERAL_SEQUENTIAL
  : CreateMode.EPHEMERAL;
nodePath = zkClient.get().create(
 path + "/" + nodeName, updatedMembershipData, acl, createMode);
memberId = Group.this.getMemberId(nodePath);
zkClient.get().exists(nodePath, new Watcher() {
 @Override public void process(WatchedEvent event) {
  if (event.getType() == EventType.NodeDeleted) {

代码示例来源:origin: com.twitter/finagle-serversets_2.11

/**
 * Creates a set of credentials for the zoo keeper digest authentication mechanism.
 *
 * @param username the username to authenticate with
 * @param password the password to authenticate with
 * @return a set of credentials that can be used to authenticate the zoo keeper client
 */
public static Credentials digestCredentials(String username, String password) {
 MorePreconditions.checkNotBlank(username);
 Objects.requireNonNull(password);
 // TODO(John Sirois): DigestAuthenticationProvider is broken - uses platform default charset
 // (on server) and so we just have to hope here that clients are deployed in compatible jvms.
 // Consider writing and installing a version of DigestAuthenticationProvider that controls its
 // Charset explicitly.
 return credentials("digest", (username + ":" + password).getBytes());
}

代码示例来源:origin: com.twitter/finagle-serversets_2.11

/**
 * Clients that need to re-establish state after session expiration can register an
 * {@code onExpired} command to execute.
 *
 * @param onExpired the {@code Runnable} to register
 * @return the new {@link Watcher} which can later be passed to {@link #unregister} for
 *     removal.
 */
public Watcher registerExpirationHandler(final Runnable onExpired) {
 Watcher watcher = new Watcher() {
  @Override public void process(WatchedEvent event) {
   if (event.getType() == EventType.None && event.getState() == KeeperState.Expired) {
    onExpired.run();
   }
  }
 };
 register(watcher);
 return watcher;
}

代码示例来源:origin: com.twitter/finagle-serversets_2.11

synchronized void setMembers(Iterable<String> members) {
  if (stopped) {
   LOG.info("Suppressing membership update, no longer watching " + this);
   return;
  }
  if (this.members == null) {
   // Reset our watch on the group if session expires - only needs to be registered once.
   zkClient.registerExpirationHandler(new Runnable() {
    @Override public void run() {
     tryWatchGroup();
    }
   });
  }
  Set<String> membership = new HashSet<>();
  members.iterator().forEachRemaining(membership::add);
  if (!membership.equals(this.members)) {
   groupChangeListener.onGroupChange(members);
   this.members = Collections.unmodifiableSet(membership);
  }
 }
}

代码示例来源:origin: com.twitter/finagle-serversets_2.11

@Override public ServiceInstance get() {
   try {
    byte[] data = zkClient.get().getData(nodePath, false, null);
    return ServerSets.deserializeServiceInstance(data, codec);
   } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
    throw new ServiceInstanceFetchException(
      "Interrupted updating service data for: " + nodePath, e);
   } catch (ZooKeeperConnectionException e) {
    LOG.log(Level.WARNING,
      "Temporary error trying to updating service data for: " + nodePath, e);
    return null;
   } catch (NoNodeException e) {
    invalidateNodePath(nodePath);
    throw new ServiceInstanceDeletedException(nodePath);
   } catch (KeeperException e) {
    if (zkClient.shouldRetry(e)) {
     LOG.log(Level.WARNING,
       "Temporary error trying to update service data for: " + nodePath, e);
     return null;
    } else {
     throw new ServiceInstanceFetchException(
       "Failed to update service data for: " + nodePath, e);
    }
   } catch (IOException e) {
    throw new ServiceInstanceFetchException(
      "Failed to deserialize the ServiceInstance data for: " + nodePath, e);
   }
  }
});

代码示例来源:origin: com.twitter/finagle-serversets_2.11

private void watchGroup()
  throws ZooKeeperConnectionException, InterruptedException, KeeperException {
 if (stopped) {
  return;
 }
 List<String> children = zkClient.get().getChildren(path, groupWatcher);
 setMembers(children.stream()
  .filter(nodeNameFilter)
  .collect(Collectors.toList())
 );
}

代码示例来源:origin: com.twitter/finagle-serversets_2.11

@Override public Boolean get() throws InterruptedException {
  try {
   join();
   return true;
  } catch (CancelledException e) {
   // Lost a cancel race - that's ok.
   return true;
  } catch (ZooKeeperConnectionException e) {
   LOG.log(Level.WARNING, "Problem connecting to ZooKeeper, retrying", e);
   return false;
  } catch (KeeperException e) {
   if (zkClient.shouldRetry(e)) {
    LOG.log(Level.WARNING, "Temporary error re-joining group: " + path, e);
    return false;
   } else {
    throw new IllegalStateException("Permanent problem re-joining group: " + path, e);
   }
  }
 }
};

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