下面列出了org.apache.zookeeper.KeeperException#NoNodeException ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void checkRevocableWatcher(String path) throws Exception
{
RevocationSpec entry = revocable.get();
if ( entry != null )
{
try
{
byte[] bytes = client.getData().usingWatcher(revocableWatcher).forPath(path);
if ( Arrays.equals(bytes, REVOKE_MESSAGE) )
{
entry.getExecutor().execute(entry.getRunnable());
}
}
catch ( KeeperException.NoNodeException ignore )
{
// ignore
}
}
}
/**
* Delete a path and all of its sub nodes
* @param filter for node to be deleted
*/
public static void clean(SolrZkClient zkClient, String path, Predicate<String> filter) throws InterruptedException, KeeperException {
if (filter == null) {
clean(zkClient, path);
return;
}
TreeSet<String> paths = new TreeSet<>(Comparator.comparingInt(String::length).reversed());
traverseZkTree(zkClient, path, VISIT_ORDER.VISIT_POST, znode -> {
if (!znode.equals("/") && filter.test(znode)) paths.add(znode);
});
for (String subpath : paths) {
if (!subpath.equals("/")) {
try {
zkClient.delete(subpath, -1, true);
} catch (KeeperException.NotEmptyException | KeeperException.NoNodeException e) {
// expected
}
}
}
}
/**
* Return a list of all valid paths for state handles.
*
* @return List of valid state handle paths in ZooKeeper
* @throws Exception if a ZooKeeper operation fails
*/
public Collection<String> getAllPaths() throws Exception {
final String path = "/";
while (true) {
Stat stat = client.checkExists().forPath(path);
if (stat == null) {
return Collections.emptyList();
} else {
try {
return client.getChildren().forPath(path);
} catch (KeeperException.NoNodeException ignored) {
// Concurrent deletion, retry
}
}
}
}
private DocCollection fetchCollectionState(String coll, Watcher watcher) throws KeeperException, InterruptedException {
String collectionPath = getCollectionPath(coll);
while (true) {
try {
Stat stat = new Stat();
byte[] data = zkClient.getData(collectionPath, watcher, stat, true);
ClusterState state = ClusterState.createFromJson(stat.getVersion(), data, Collections.emptySet());
ClusterState.CollectionRef collectionRef = state.getCollectionStates().get(coll);
return collectionRef == null ? null : collectionRef.get();
} catch (KeeperException.NoNodeException e) {
if (watcher != null) {
// Leave an exists watch in place in case a state.json is created later.
Stat exists = zkClient.exists(collectionPath, watcher, true);
if (exists != null) {
// Rare race condition, we tried to fetch the data and couldn't find it, then we found it exists.
// Loop and try again.
continue;
}
}
return null;
}
}
}
private List<String> getChildrenWatched(String path, Watcher watcher, boolean recurse) throws Exception
{
List<String> instanceIds;
try
{
instanceIds = client.getChildren().usingWatcher(watcher).forPath(path);
}
catch ( KeeperException.NoNodeException e )
{
if ( recurse )
{
try
{
client.create().creatingParentContainersIfNeeded().forPath(path);
}
catch ( KeeperException.NodeExistsException ignore )
{
// ignore
}
instanceIds = getChildrenWatched(path, watcher, false);
}
else
{
throw e;
}
}
return instanceIds;
}
/**
* Sets the location of <code>hbase:meta</code> in ZooKeeper to the specified server address.
* @param zookeeper reference to the {@link ZKWatcher} which also contains configuration and
* operation
* @param serverName the name of the server
* @param replicaId the ID of the replica
* @param state the state of the region
* @throws KeeperException if a ZooKeeper operation fails
*/
public static void setMetaLocation(ZKWatcher zookeeper, ServerName serverName, int replicaId,
RegionState.State state) throws KeeperException {
if (serverName == null) {
LOG.warn("Tried to set null ServerName in hbase:meta; skipping -- ServerName required");
return;
}
LOG.info("Setting hbase:meta (replicaId={}) location in ZooKeeper as {}", replicaId,
serverName);
// Make the MetaRegionServer pb and then get its bytes and save this as
// the znode content.
MetaRegionServer pbrsr = MetaRegionServer.newBuilder()
.setServer(ProtobufUtil.toServerName(serverName))
.setRpcVersion(HConstants.RPC_CURRENT_VERSION)
.setState(state.convert()).build();
byte[] data = ProtobufUtil.prependPBMagic(pbrsr.toByteArray());
try {
ZKUtil.setData(zookeeper,
zookeeper.getZNodePaths().getZNodeForReplica(replicaId), data);
} catch(KeeperException.NoNodeException nne) {
if (replicaId == RegionInfo.DEFAULT_REPLICA_ID) {
LOG.debug("META region location doesn't exist, create it");
} else {
LOG.debug("META region location doesn't exist for replicaId=" + replicaId +
", create it");
}
ZKUtil.createAndWatch(zookeeper, zookeeper.getZNodePaths().getZNodeForReplica(replicaId),
data);
}
}
private void fetchServices(RpcHostAndPort hostAndPort){
String serverKey = this.genServerKey(hostAndPort);
String serviceListKey = this.getServiceListKey(serverKey);
try {
List<String> services = this.zkclient.getChildren().forPath(serviceListKey);
this.getServices(services, hostAndPort);
} catch (Exception e) {
if(e instanceof KeeperException.NoNodeException){
return;
}
this.handleZkException(e);
}
}
public void setCommittedOffsetCount(TopicPartition topicPartition, long count)
throws Exception {
String offsetPath = getCommittedOffsetPartitionPath(topicPartition);
LOG.info("creating missing parents for zookeeper path {}", offsetPath);
createMissingParents(offsetPath);
byte[] data = Long.toString(count).getBytes();
try {
LOG.info("setting zookeeper path {} value {}", offsetPath, count);
// -1 matches any version
mCurator.setData().forPath(offsetPath, data);
} catch (KeeperException.NoNodeException exception) {
LOG.warn("Failed to set value to path " + offsetPath, exception);
}
}
/**
*
* @return leader's data
* @throws Exception
*/
public byte[] getActualMaster() throws Exception {
try {
return zk.getData(leaderpath, false, new Stat());
} catch (KeeperException.NoNodeException noMaster) {
return null;
}
}
@Test
public void testEnsurePath() throws Exception
{
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
client.start();
try
{
PathChildrenCache cache = new PathChildrenCache(client, "/one/two/three", false);
cache.start();
timing.sleepABit();
try
{
client.create().forPath("/one/two/three/four");
}
catch ( KeeperException.NoNodeException e )
{
Assert.fail("Path should exist", e);
}
}
finally
{
CloseableUtils.closeQuietly(client);
}
}
public List<ConsumeRpcObject> getConsumeObjects(String group,String service,String version){
List<ConsumeRpcObject> consumers = new ArrayList<ConsumeRpcObject>();
String serviceDirName = group+"_"+service+"_"+version;
String consumerBase = "/consumers/"+serviceDirName;
List<String> apps = this.getConsumeApplications(group, service, version);
for(String app:apps){
String hostipDir = consumerBase+"/"+app;
try {
List<String> hosts = zkclient.getChildren().forPath(hostipDir);
for(String host:hosts){
ConsumeRpcObject con = new ConsumeRpcObject();
con.setIp(host);
con.setVersion(version);
con.setClassName(service);
con.setGroup(group);
con.setApplication(app);
consumers.add(con);
}
} catch (Exception e) {
if(e instanceof KeeperException.NoNodeException){
continue;
}
throw new RpcException(e);
}
}
return consumers;
}
@Override
public final boolean isSubscriptionCreatedAndInitialized() throws NakadiRuntimeException {
// First step - check that state node was already written
try {
final String state = new String(getCurator().getData().forPath(getSubscriptionPath("/state")), UTF_8);
return state.equals(STATE_INITIALIZED);
} catch (final KeeperException.NoNodeException ex) {
return false;
} catch (final Exception e) {
throw new NakadiRuntimeException(e);
}
}
@Override
public List<org.springframework.cloud.client.ServiceInstance> getInstances(
final String serviceId) {
try {
if (getServiceDiscovery() == null) {
return Collections.EMPTY_LIST;
}
String serviceIdToQuery = getServiceIdToQuery(serviceId);
Collection<ServiceInstance<ZookeeperInstance>> zkInstances = getServiceDiscovery()
.queryForInstances(serviceIdToQuery);
List<org.springframework.cloud.client.ServiceInstance> instances = new ArrayList<>();
for (ServiceInstance<ZookeeperInstance> instance : zkInstances) {
instances.add(createServiceInstance(serviceIdToQuery, instance));
}
return instances;
}
catch (KeeperException.NoNodeException e) {
if (log.isDebugEnabled()) {
log.debug(
"Error getting instances from zookeeper. Possibly, no service has registered.",
e);
}
// this means that nothing has registered as a service yes
return Collections.emptyList();
}
catch (Exception exception) {
rethrowRuntimeException(exception);
}
return new ArrayList<>();
}
/**
* Set the balancer on/off.
*
* @param balancerOn true if the balancher should be on, false otherwise
* @throws KeeperException if a ZooKeeper operation fails
*/
public void setBalancerOn(boolean balancerOn) throws KeeperException {
byte [] upData = toByteArray(balancerOn);
try {
ZKUtil.setData(watcher, watcher.getZNodePaths().balancerZNode, upData);
} catch(KeeperException.NoNodeException nne) {
ZKUtil.createAndWatch(watcher, watcher.getZNodePaths().balancerZNode, upData);
}
super.nodeDataChanged(watcher.getZNodePaths().balancerZNode);
}
@VisibleForTesting
void purgeLockInternal(String lockPath) {
try {
curator.delete().guaranteed().deletingChildrenIfNeeded().forPath(lockPath);
} catch (KeeperException.NoNodeException ex) {
// it's safe to purge a lock when there is no node found in lockPath
logger.warn("No node found when purge lock in Lock path: {}", lockPath, ex);
} catch (Exception e) {
if (ThrowableUtils.isInterruptedException(e))
throw new ZkReleaseLockInterruptException("Purge lock was interrupted at " + lockPath, e);
else
throw new ZkReleaseLockException("Error while " + client + " trying to purge " + lockPath, e);
}
}
/**
* Utility to remove the barrier node
*
* @throws Exception errors
*/
public synchronized void removeBarrier() throws Exception
{
try
{
client.delete().forPath(barrierPath);
}
catch ( KeeperException.NoNodeException ignore )
{
// ignore
}
}
@SuppressWarnings({"unchecked"})
public synchronized void createClusterStateWatchersAndUpdate() throws KeeperException, InterruptedException {
// We need to fetch the current cluster state and the set of live nodes
log.debug("Updating cluster state from ZooKeeper... ");
try {
// on reconnect of SolrZkClient force refresh and re-add watches.
loadClusterProperties();
refreshLiveNodes(new LiveNodeWatcher());
refreshCollections();
refreshCollectionList(new CollectionsChildWatcher());
refreshAliases(aliasesManager);
if (securityNodeListener != null) {
addSecurityNodeWatcher(pair -> {
ConfigData cd = new ConfigData();
cd.data = pair.first() == null || pair.first().length == 0 ? EMPTY_MAP : Utils.getDeepCopy((Map) fromJSON(pair.first()), 4, false);
cd.version = pair.second() == null ? -1 : pair.second().getVersion();
securityData = cd;
securityNodeListener.run();
});
securityData = getSecurityProps(true);
}
collectionPropsObservers.forEach((k, v) -> {
collectionPropsWatchers.computeIfAbsent(k, PropsWatcher::new).refreshAndWatch(true);
});
} catch (KeeperException.NoNodeException nne) {
throw new SolrException(ErrorCode.SERVICE_UNAVAILABLE,
"Cannot connect to cluster at " + zkClient.getZkServerAddress() + ": cluster not found/not ready");
}
}
@Test
public void testConcurrentCreateRoutedAliasMinimal() throws IOException, KeeperException.NoNodeException {
// this is the test where be blow out a bunch of create commands all out at once.
// other tests are more functionality based, and just use a single thread.
// Failure of this test very occasionally due to overseer overload would not be worrisome (just bothersome).
// Any use case creating large numbers of time routed aliases concurrently would be an EXTREMELY odd
// if not fundamentally broken use case. This test method is just here to guard against any race
// conditions in the code that could crop up rarely in lower volume usage.
// That said any failures involving about NPE's or missing parameters or oddities other than overwhelming
// the overseer queue with retry races emanating from this test should be investigated. Also if it fails
// frequently that needs to be investigated of course.
final AtomicReference<Exception> failure = new AtomicReference<>();
// Note: this number of threads seems to work regularly with the up-tweaked number of retries (50) in
// org.apache.solr.common.cloud.ZkStateReader.AliasesManager.applyModificationAndExportToZk()
// with the original 5 retries this wouldn't reliably pass with 10 threads, but with 50 retries it seems
// to handle 50 threads about a dozen times without any failure (on a 32 thread processor)
// it also passed 3/3 at 150 threads and 2/3 with 250 threads on both 1 node and 4 nodes...
// the failure mode seems to be overseer tasks that are not found. I suspect this happens when enough
// threads get into retry races and the spam overwhelms the overseer. (that this can happen might imply
// an issue over there, but I'm not sure, since there is an intentional hard limit on the overseer queue
// and I haven't tried to count the retries up and figure out if the requests are actually exceeding that
// limit or not, but the speed of retries might indicate an effectively hot loop, but again, a separate issue.
// The hope is that the level of concurrency supported by create routed alias and the code it uses is such
// that this test wouldn't spuriously fail more than once a year. If that's true users should never see
// an issue in the wild unless they are doing something we probably don't want to support anyway
final CreateRoutedAliasThread[] threads = new CreateRoutedAliasThread[4];
int numStart = num;
for (; num < threads.length + numStart; num++) {
final String aliasName = "testAlias" + num;
final String baseUrl = solrCluster.getJettySolrRunners().get(0).getBaseUrl().toString();
final SolrClient solrClient = getHttpSolrClient(baseUrl);
int i = num - numStart;
threads[i] = new CreateRoutedAliasThread("create-delete-search-" + i, aliasName, "NOW/HOUR",
solrClient, failure, false);
}
startAll(threads);
joinAll(threads);
assertNull("concurrent alias creation failed " + failure.get(), failure.get());
}
private boolean internalLeave(long startMs, boolean hasMaxWait, long maxWaitMs) throws Exception
{
String ourPathName = ZKPaths.getNodeFromPath(ourPath);
boolean ourNodeShouldExist = true;
boolean result = true;
for(;;)
{
if ( connectionLost.get() )
{
throw new KeeperException.ConnectionLossException();
}
List<String> children;
try
{
children = client.getChildren().forPath(barrierPath);
}
catch ( KeeperException.NoNodeException dummy )
{
children = Lists.newArrayList();
}
children = filterAndSortChildren(children);
if ( (children == null) || (children.size() == 0) )
{
break;
}
int ourIndex = children.indexOf(ourPathName);
if ( (ourIndex < 0) && ourNodeShouldExist )
{
if ( connectionLost.get() )
{
break; // connection was lost but we've reconnected. However, our ephemeral node is gone
}
else
{
throw new IllegalStateException(String.format("Our path (%s) is missing", ourPathName));
}
}
if ( children.size() == 1 )
{
if ( ourNodeShouldExist && !children.get(0).equals(ourPathName) )
{
throw new IllegalStateException(String.format("Last path (%s) is not ours (%s)", children.get(0), ourPathName));
}
checkDeleteOurPath(ourNodeShouldExist);
break;
}
Stat stat;
boolean IsLowestNode = (ourIndex == 0);
if ( IsLowestNode )
{
String highestNodePath = ZKPaths.makePath(barrierPath, children.get(children.size() - 1));
stat = client.checkExists().usingWatcher(watcher).forPath(highestNodePath);
}
else
{
String lowestNodePath = ZKPaths.makePath(barrierPath, children.get(0));
stat = client.checkExists().usingWatcher(watcher).forPath(lowestNodePath);
checkDeleteOurPath(ourNodeShouldExist);
ourNodeShouldExist = false;
}
if ( stat != null )
{
if ( hasMaxWait )
{
long elapsed = System.currentTimeMillis() - startMs;
long thisWaitMs = maxWaitMs - elapsed;
if ( thisWaitMs <= 0 )
{
result = false;
}
else
{
wait(thisWaitMs);
}
}
else
{
wait();
}
}
}
try
{
client.delete().forPath(readyPath);
}
catch ( KeeperException.NoNodeException ignore )
{
// ignore
}
return result;
}
@Override
public Stat statNode(String path, ServerCnxn serverCnxn) throws KeeperException.NoNodeException {
limiter.statLimit.updateForWatch(path, serverCnxn);
return super.statNode(path, serverCnxn);
}