use of org.apache.zookeeper.KeeperException.NoNodeException in project pulsar by yahoo.
the class OwnershipCacheTest method testRemoveOwnership.
@Test
public void testRemoveOwnership() throws Exception {
OwnershipCache cache = new OwnershipCache(this.pulsar, bundleFactory);
NamespaceName testNs = new NamespaceName("pulsar/test/ns-7");
NamespaceBundle bundle = bundleFactory.getFullBundle(testNs);
// case 1: no one owns the namespace
assertFalse(cache.getOwnerAsync(bundle).get().isPresent());
cache.removeOwnership(bundle).get();
assertTrue(cache.getOwnedBundles().isEmpty());
// case 2: this broker owns the namespace
NamespaceEphemeralData data1 = cache.tryAcquiringOwnership(bundle).get();
assertEquals(data1.getNativeUrl(), selfBrokerUrl);
assertTrue(!data1.isDisabled());
assertTrue(cache.getOwnedBundles().size() == 1);
cache.removeOwnership(bundle);
Thread.sleep(500);
assertTrue(cache.getOwnedBundles().isEmpty());
Thread.sleep(500);
try {
zkCache.getZooKeeper().getData(ServiceUnitZkUtils.path(bundle), null, null);
fail("Should have failed");
} catch (NoNodeException nne) {
// OK
}
}
use of org.apache.zookeeper.KeeperException.NoNodeException in project pulsar by yahoo.
the class Namespaces method createNamespace.
@PUT
@Path("/{property}/{cluster}/{namespace}")
@ApiOperation(value = "Creates a new empty namespace with no policies attached.")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Property or cluster or namespace doesn't exist"), @ApiResponse(code = 409, message = "Namespace already exists"), @ApiResponse(code = 412, message = "Namespace name is not valid") })
public void createNamespace(@PathParam("property") String property, @PathParam("cluster") String cluster, @PathParam("namespace") String namespace, BundlesData initialBundles) {
validateAdminAccessOnProperty(property);
validatePoliciesReadOnlyAccess();
// check is made at the time of setting replication.
if (!cluster.equals(GLOBAL_CLUSTER)) {
validateClusterForProperty(property, cluster);
}
if (!clusters().contains(cluster)) {
log.warn("[{}] Failed to create namespace. Cluster {} does not exist", clientAppId(), cluster);
throw new RestException(Status.NOT_FOUND, "Cluster does not exist");
}
try {
checkNotNull(propertiesCache().get(path("policies", property)));
} catch (NoNodeException nne) {
log.warn("[{}] Failed to create namespace. Property {} does not exist", clientAppId(), property);
throw new RestException(Status.NOT_FOUND, "Property does not exist");
} catch (RestException e) {
throw e;
} catch (Exception e) {
throw new RestException(e);
}
try {
NamedEntity.checkName(namespace);
policiesCache().invalidate(path("policies", property, cluster, namespace));
Policies policies = new Policies();
if (initialBundles != null && initialBundles.getNumBundles() > 0) {
if (initialBundles.getBoundaries() == null || initialBundles.getBoundaries().size() == 0) {
policies.bundles = getBundles(initialBundles.getNumBundles());
} else {
policies.bundles = validateBundlesData(initialBundles);
}
}
zkCreateOptimistic(path("policies", property, cluster, namespace), jsonMapper().writeValueAsBytes(policies));
log.info("[{}] Created namespace {}/{}/{}", clientAppId(), property, cluster, namespace);
} catch (KeeperException.NodeExistsException e) {
log.warn("[{}] Failed to create namespace {}/{}/{} - already exists", clientAppId(), property, cluster, namespace);
throw new RestException(Status.CONFLICT, "Namespace already exists");
} catch (IllegalArgumentException e) {
log.warn("[{}] Failed to create namespace with invalid name {}", clientAppId(), property, e);
throw new RestException(Status.PRECONDITION_FAILED, "Namespace name is not valid");
} catch (Exception e) {
log.error("[{}] Failed to create namespace {}/{}/{}", clientAppId(), property, cluster, namespace, e);
throw new RestException(e);
}
}
use of org.apache.zookeeper.KeeperException.NoNodeException in project lucene-solr by apache.
the class LeaderElectionTest method getLeaderUrl.
private String getLeaderUrl(final String collection, final String slice) throws KeeperException, InterruptedException {
int iterCount = 60;
while (iterCount-- > 0) {
try {
byte[] data = zkClient.getData(ZkStateReader.getShardLeadersPath(collection, slice), null, null, true);
ZkCoreNodeProps leaderProps = new ZkCoreNodeProps(ZkNodeProps.load(data));
return leaderProps.getCoreUrl();
} catch (NoNodeException | SessionExpiredException e) {
Thread.sleep(500);
}
}
zkClient.printLayoutToStdOut();
throw new RuntimeException("Could not get leader props");
}
use of org.apache.zookeeper.KeeperException.NoNodeException in project lucene-solr by apache.
the class ZkController method persistConfigResourceToZooKeeper.
/**
* Persists a config file to ZooKeeper using optimistic concurrency.
*
* @return true on success
*/
public static int persistConfigResourceToZooKeeper(ZkSolrResourceLoader zkLoader, int znodeVersion, String resourceName, byte[] content, boolean createIfNotExists) {
int latestVersion = znodeVersion;
final ZkController zkController = zkLoader.getZkController();
final SolrZkClient zkClient = zkController.getZkClient();
final String resourceLocation = zkLoader.getConfigSetZkPath() + "/" + resourceName;
String errMsg = "Failed to persist resource at {0} - old {1}";
try {
try {
Stat stat = zkClient.setData(resourceLocation, content, znodeVersion, true);
// if the set succeeded , it should have incremented the version by one always
latestVersion = stat.getVersion();
log.info("Persisted config data to node {} ", resourceLocation);
touchConfDir(zkLoader);
} catch (NoNodeException e) {
if (createIfNotExists) {
try {
zkClient.create(resourceLocation, content, CreateMode.PERSISTENT, true);
//just created so version must be zero
latestVersion = 0;
touchConfDir(zkLoader);
} catch (KeeperException.NodeExistsException nee) {
try {
Stat stat = zkClient.exists(resourceLocation, null, true);
log.debug("failed to set data version in zk is {} and expected version is {} ", stat.getVersion(), znodeVersion);
} catch (Exception e1) {
log.warn("could not get stat");
}
log.info(StrUtils.formatString(errMsg, resourceLocation, znodeVersion));
throw new ResourceModifiedInZkException(ErrorCode.CONFLICT, StrUtils.formatString(errMsg, resourceLocation, znodeVersion) + ", retry.");
}
}
}
} catch (KeeperException.BadVersionException bve) {
int v = -1;
try {
Stat stat = zkClient.exists(resourceLocation, null, true);
v = stat.getVersion();
} catch (Exception e) {
log.error(e.getMessage());
}
log.info(StrUtils.formatString(errMsg + " zkVersion= " + v, resourceLocation, znodeVersion));
throw new ResourceModifiedInZkException(ErrorCode.CONFLICT, StrUtils.formatString(errMsg, resourceLocation, znodeVersion) + ", retry.");
} catch (ResourceModifiedInZkException e) {
throw e;
} catch (Exception e) {
if (e instanceof InterruptedException) {
// Restore the interrupted status
Thread.currentThread().interrupt();
}
final String msg = "Error persisting resource at " + resourceLocation;
log.error(msg, e);
throw new SolrException(ErrorCode.SERVER_ERROR, msg, e);
}
return latestVersion;
}
Aggregations