use of org.apache.pulsar.common.naming.NamespaceBundles in project incubator-pulsar by apache.
the class NamespacesBase method internalClearNamespaceBacklogForSubscription.
protected void internalClearNamespaceBacklogForSubscription(String subscription, boolean authoritative) {
validateAdminAccessOnProperty(namespaceName.getProperty());
try {
NamespaceBundles bundles = pulsar().getNamespaceService().getNamespaceBundleFactory().getBundles(namespaceName);
Exception exception = null;
for (NamespaceBundle nsBundle : bundles.getBundles()) {
try {
// clear
if (pulsar().getNamespaceService().getOwner(nsBundle).isPresent()) {
// TODO: make this admin call asynchronous
pulsar().getAdminClient().namespaces().clearNamespaceBundleBacklogForSubscription(namespaceName.toString(), nsBundle.getBundleRange(), subscription);
}
} catch (Exception e) {
if (exception == null) {
exception = e;
}
}
}
if (exception != null) {
if (exception instanceof PulsarAdminException) {
throw new RestException((PulsarAdminException) exception);
} else {
throw new RestException(exception.getCause());
}
}
} catch (WebApplicationException wae) {
throw wae;
} catch (Exception e) {
throw new RestException(e);
}
log.info("[{}] Successfully cleared backlog for subscription {} on all the bundles for namespace {}", clientAppId(), subscription, namespaceName);
}
use of org.apache.pulsar.common.naming.NamespaceBundles in project incubator-pulsar by apache.
the class NamespacesBase method internalClearNamespaceBacklog.
protected void internalClearNamespaceBacklog(boolean authoritative) {
validateAdminAccessOnProperty(namespaceName.getProperty());
try {
NamespaceBundles bundles = pulsar().getNamespaceService().getNamespaceBundleFactory().getBundles(namespaceName);
Exception exception = null;
for (NamespaceBundle nsBundle : bundles.getBundles()) {
try {
// clear
if (pulsar().getNamespaceService().getOwner(nsBundle).isPresent()) {
// TODO: make this admin call asynchronous
pulsar().getAdminClient().namespaces().clearNamespaceBundleBacklog(namespaceName.toString(), nsBundle.getBundleRange());
}
} catch (Exception e) {
if (exception == null) {
exception = e;
}
}
}
if (exception != null) {
if (exception instanceof PulsarAdminException) {
throw new RestException((PulsarAdminException) exception);
} else {
throw new RestException(exception.getCause());
}
}
} catch (WebApplicationException wae) {
throw wae;
} catch (Exception e) {
throw new RestException(e);
}
log.info("[{}] Successfully cleared backlog on all the bundles for namespace {}", clientAppId(), namespaceName);
}
use of org.apache.pulsar.common.naming.NamespaceBundles in project incubator-pulsar by apache.
the class NamespacesBase method internalDeleteNamespace.
@SuppressWarnings("deprecation")
protected void internalDeleteNamespace(boolean authoritative) {
validateAdminAccessOnProperty(namespaceName.getProperty());
validatePoliciesReadOnlyAccess();
// ensure that non-global namespace is directed to the correct cluster
if (!namespaceName.isGlobal()) {
validateClusterOwnership(namespaceName.getCluster());
}
Entry<Policies, Stat> policiesNode = null;
Policies policies = null;
// ensure the local cluster is the only cluster for the global namespace configuration
try {
policiesNode = policiesCache().getWithStat(path(POLICIES, namespaceName.toString())).orElseThrow(() -> new RestException(Status.NOT_FOUND, "Namespace " + namespaceName + " does not exist."));
policies = policiesNode.getKey();
if (namespaceName.isGlobal()) {
if (policies.replication_clusters.size() > 1) {
// There are still more than one clusters configured for the global namespace
throw new RestException(Status.PRECONDITION_FAILED, "Cannot delete the global namespace " + namespaceName + ". There are still more than one replication clusters configured.");
}
if (policies.replication_clusters.size() == 1 && !policies.replication_clusters.contains(config().getClusterName())) {
// the only replication cluster is other cluster, redirect
String replCluster = policies.replication_clusters.get(0);
ClusterData replClusterData = clustersCache().get(AdminResource.path("clusters", replCluster)).orElseThrow(() -> new RestException(Status.NOT_FOUND, "Cluster " + replCluster + " does not exist"));
URL replClusterUrl;
if (!config().isTlsEnabled() || !isRequestHttps()) {
replClusterUrl = new URL(replClusterData.getServiceUrl());
} else if (StringUtils.isNotBlank(replClusterData.getServiceUrlTls())) {
replClusterUrl = new URL(replClusterData.getServiceUrlTls());
} else {
throw new RestException(Status.PRECONDITION_FAILED, "The replication cluster does not provide TLS encrypted service");
}
URI redirect = UriBuilder.fromUri(uri.getRequestUri()).host(replClusterUrl.getHost()).port(replClusterUrl.getPort()).replaceQueryParam("authoritative", false).build();
if (log.isDebugEnabled()) {
log.debug("[{}] Redirecting the rest call to {}: cluster={}", clientAppId(), redirect, replCluster);
}
throw new WebApplicationException(Response.temporaryRedirect(redirect).build());
}
}
} catch (WebApplicationException wae) {
throw wae;
} catch (Exception e) {
throw new RestException(e);
}
boolean isEmpty;
try {
isEmpty = pulsar().getNamespaceService().getListOfTopics(namespaceName).isEmpty();
} catch (Exception e) {
throw new RestException(e);
}
if (!isEmpty) {
log.debug("Found topics on namespace {}", namespaceName);
throw new RestException(Status.CONFLICT, "Cannot delete non empty namespace");
}
// set the policies to deleted so that somebody else cannot acquire this namespace
try {
policies.deleted = true;
globalZk().setData(path(POLICIES, namespaceName.toString()), jsonMapper().writeValueAsBytes(policies), policiesNode.getValue().getVersion());
policiesCache().invalidate(path(POLICIES, namespaceName.toString()));
} catch (Exception e) {
log.error("[{}] Failed to delete namespace on global ZK {}", clientAppId(), namespaceName, e);
throw new RestException(e);
}
// remove from owned namespace map and ephemeral node from ZK
try {
NamespaceBundles bundles = pulsar().getNamespaceService().getNamespaceBundleFactory().getBundles(namespaceName);
for (NamespaceBundle bundle : bundles.getBundles()) {
// check if the bundle is owned by any broker, if not then we do not need to delete the bundle
if (pulsar().getNamespaceService().getOwner(bundle).isPresent()) {
pulsar().getAdminClient().namespaces().deleteNamespaceBundle(namespaceName.toString(), bundle.getBundleRange());
}
}
// we have successfully removed all the ownership for the namespace, the policies znode can be deleted now
final String globalZkPolicyPath = path(POLICIES, namespaceName.toString());
final String lcaolZkPolicyPath = joinPath(LOCAL_POLICIES_ROOT, namespaceName.toString());
globalZk().delete(globalZkPolicyPath, -1);
localZk().delete(lcaolZkPolicyPath, -1);
policiesCache().invalidate(globalZkPolicyPath);
localCacheService().policiesCache().invalidate(lcaolZkPolicyPath);
} catch (PulsarAdminException cae) {
throw new RestException(cae);
} catch (Exception e) {
log.error("[{}] Failed to remove owned namespace {}", clientAppId(), namespaceName, e);
// avoid throwing exception in case of the second failure
}
}
use of org.apache.pulsar.common.naming.NamespaceBundles in project incubator-pulsar by apache.
the class AdminResource method getNamespacePolicies.
protected Policies getNamespacePolicies(NamespaceName namespaceName) {
try {
Policies policies = policiesCache().get(AdminResource.path(POLICIES, namespaceName.toString())).orElseThrow(() -> new RestException(Status.NOT_FOUND, "Namespace does not exist"));
// fetch bundles from LocalZK-policies
NamespaceBundles bundles = pulsar().getNamespaceService().getNamespaceBundleFactory().getBundles(namespaceName);
BundlesData bundleData = NamespaceBundleFactory.getBundlesData(bundles);
policies.bundles = bundleData != null ? bundleData : policies.bundles;
return policies;
} catch (RestException re) {
throw re;
} catch (Exception e) {
log.error("[{}] Failed to get namespace policies {}", clientAppId(), namespaceName, e);
throw new RestException(e);
}
}
use of org.apache.pulsar.common.naming.NamespaceBundles in project incubator-pulsar by apache.
the class NamespaceBundlesTest method testFindBundle.
@Test
public void testFindBundle() throws Exception {
SortedSet<Long> partitions = Sets.newTreeSet();
partitions.add(0l);
partitions.add(0x40000000l);
partitions.add(0xa0000000l);
partitions.add(0xb0000000l);
partitions.add(0xc0000000l);
partitions.add(0xffffffffl);
NamespaceBundles bundles = new NamespaceBundles(NamespaceName.get("pulsar/global/ns1"), partitions, factory);
TopicName topicName = TopicName.get("persistent://pulsar/global/ns1/topic-1");
NamespaceBundle bundle = bundles.findBundle(topicName);
assertTrue(bundle.includes(topicName));
topicName = TopicName.get("persistent://pulsar/use/ns2/topic-2");
try {
bundles.findBundle(topicName);
fail("Should have failed due to mismatched namespace name");
} catch (IllegalArgumentException iae) {
// OK, expected
}
Long hashKey = factory.getLongHashCode(topicName.toString());
// The following code guarantees that we have at least two ranges after the hashKey till the end
SortedSet<Long> tailSet = partitions.tailSet(hashKey);
tailSet.add(hashKey);
// Now, remove the first range to ensure the hashKey is not included in <code>newPar</code>
Iterator<Long> iter = tailSet.iterator();
iter.next();
SortedSet<Long> newPar = tailSet.tailSet(iter.next());
try {
bundles = new NamespaceBundles(topicName.getNamespaceObject(), newPar, factory);
bundles.findBundle(topicName);
fail("Should have failed due to out-of-range");
} catch (ArrayIndexOutOfBoundsException iae) {
// OK, expected
}
}
Aggregations