Search in sources :

Example 26 with NamespaceBundle

use of com.yahoo.pulsar.common.naming.NamespaceBundle in project pulsar by yahoo.

the class NamespaceBundleTest method testConstructor.

@Test
public void testConstructor() {
    try {
        new NamespaceBundle(null, null, null);
        fail("Should have failed w/ null pointer exception");
    } catch (NullPointerException npe) {
    // OK, expected
    }
    try {
        new NamespaceBundle(new NamespaceName("pulsar.old.ns"), null, null);
        fail("Should have failed w/ illegal argument exception");
    } catch (IllegalArgumentException iae) {
    // OK, expected
    }
    try {
        new NamespaceBundle(new NamespaceName("pulsar/use/ns"), Range.range(0L, BoundType.CLOSED, 0L, BoundType.OPEN), null);
        fail("Should have failed w/ illegal argument exception");
    } catch (IllegalArgumentException iae) {
    // OK, expected
    }
    try {
        new NamespaceBundle(new NamespaceName("pulsar/use/ns"), Range.range(0L, BoundType.OPEN, 1L, BoundType.OPEN), null);
        fail("Should have failed w/ illegal argument exception");
    } catch (IllegalArgumentException iae) {
    // OK, expected
    }
    try {
        new NamespaceBundle(new NamespaceName("pulsar/use/ns"), Range.range(1L, BoundType.CLOSED, 1L, BoundType.OPEN), null);
        fail("Should have failed w/ illegal argument exception");
    } catch (IllegalArgumentException iae) {
    // OK, expected
    }
    try {
        new NamespaceBundle(new NamespaceName("pulsar/use/ns"), Range.range(0L, BoundType.CLOSED, 1L, BoundType.CLOSED), null);
        fail("Should have failed w/ illegal argument exception");
    } catch (IllegalArgumentException iae) {
    // OK, expected
    }
    try {
        new NamespaceBundle(new NamespaceName("pulsar/use/ns"), Range.range(0L, BoundType.CLOSED, NamespaceBundles.FULL_UPPER_BOUND, BoundType.OPEN), null);
        fail("Should have failed w/ illegal argument exception");
    } catch (IllegalArgumentException iae) {
    // OK, expected
    }
    try {
        new NamespaceBundle(new NamespaceName("pulsar/use/ns"), Range.range(0L, BoundType.CLOSED, NamespaceBundles.FULL_UPPER_BOUND, BoundType.CLOSED), null);
        fail("Should have failed w/ null pointer exception");
    } catch (NullPointerException npe) {
    // OK, expected
    }
    NamespaceBundle bundle = new NamespaceBundle(new NamespaceName("pulsar/use/ns"), Range.range(0L, BoundType.CLOSED, 1L, BoundType.OPEN), factory);
    assertTrue(bundle.getKeyRange().lowerEndpoint().equals(0L));
    assertEquals(bundle.getKeyRange().lowerBoundType(), BoundType.CLOSED);
    assertTrue(bundle.getKeyRange().upperEndpoint().equals(1L));
    assertEquals(bundle.getKeyRange().upperBoundType(), BoundType.OPEN);
    assertEquals(bundle.getNamespaceObject().toString(), "pulsar/use/ns");
}
Also used : NamespaceBundle(com.yahoo.pulsar.common.naming.NamespaceBundle) NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) Test(org.testng.annotations.Test)

Example 27 with NamespaceBundle

use of com.yahoo.pulsar.common.naming.NamespaceBundle in project pulsar by yahoo.

the class NamespaceBundleTest method testCompareTo.

@Test
public void testCompareTo() throws Exception {
    NamespaceBundle bundle = factory.getBundle(new NamespaceName("pulsar/use/ns1"), Range.range(0l, BoundType.CLOSED, 0x40000000L, BoundType.OPEN));
    NamespaceBundle bundle2 = factory.getBundle(new NamespaceName("pulsar/use/ns1"), Range.range(0x20000000l, BoundType.CLOSED, 0x40000000L, BoundType.OPEN));
    try {
        bundle.compareTo(bundle2);
        fail("Should have failed due to overlap ranges");
    } catch (IllegalArgumentException iae) {
    // OK, expected
    }
    NamespaceBundle bundle0 = factory.getBundle(new NamespaceName("pulsar/use/ns1"), Range.range(0l, BoundType.CLOSED, 0x10000000L, BoundType.OPEN));
    assertTrue(bundle0.compareTo(bundle2) < 0);
    assertTrue(bundle2.compareTo(bundle0) > 0);
    NamespaceBundle bundle1 = factory.getBundle(new NamespaceName("pulsar/use/ns1"), Range.range(0l, BoundType.CLOSED, 0x20000000L, BoundType.OPEN));
    assertTrue(bundle1.compareTo(bundle2) < 0);
    NamespaceBundle bundle3 = factory.getBundle(new NamespaceName("pulsar/use/ns1"), Range.range(0l, BoundType.CLOSED, 0x40000000L, BoundType.OPEN));
    assertTrue(bundle.compareTo(bundle3) == 0);
    NamespaceBundle otherBundle = factory.getBundle(new NamespaceName("pulsar/use/ns2"), Range.range(0x10000000l, BoundType.CLOSED, 0x30000000L, BoundType.OPEN));
    assertTrue(otherBundle.compareTo(bundle0) > 0);
}
Also used : NamespaceBundle(com.yahoo.pulsar.common.naming.NamespaceBundle) NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) Test(org.testng.annotations.Test)

Example 28 with NamespaceBundle

use of com.yahoo.pulsar.common.naming.NamespaceBundle in project pulsar by yahoo.

the class NamespaceBundleTest method testToString.

@Test
public void testToString() throws Exception {
    NamespaceBundle bundle0 = factory.getBundle(new NamespaceName("pulsar/use/ns1"), Range.range(0l, BoundType.CLOSED, 0x10000000L, BoundType.OPEN));
    assertEquals(bundle0.toString(), "pulsar/use/ns1/0x00000000_0x10000000");
    bundle0 = factory.getBundle(new NamespaceName("pulsar/use/ns1"), Range.range(0x10000000l, BoundType.CLOSED, NamespaceBundles.FULL_UPPER_BOUND, BoundType.CLOSED));
    assertEquals(bundle0.toString(), "pulsar/use/ns1/0x10000000_0xffffffff");
}
Also used : NamespaceBundle(com.yahoo.pulsar.common.naming.NamespaceBundle) NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) Test(org.testng.annotations.Test)

Example 29 with NamespaceBundle

use of com.yahoo.pulsar.common.naming.NamespaceBundle in project pulsar by yahoo.

the class Namespaces method deleteNamespaceBundle.

@DELETE
@Path("/{property}/{cluster}/{namespace}/{bundle}")
@ApiOperation(value = "Delete a namespace bundle and all the destinations under it.")
@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 bundle is not empty") })
public void deleteNamespaceBundle(@PathParam("property") String property, @PathParam("cluster") String cluster, @PathParam("namespace") String namespace, @PathParam("bundle") String bundleRange, @QueryParam("authoritative") @DefaultValue("false") boolean authoritative) {
    NamespaceName nsName = new NamespaceName(property, cluster, namespace);
    validateAdminAccessOnProperty(property);
    validatePoliciesReadOnlyAccess();
    // ensure that non-global namespace is directed to the correct cluster
    validateClusterOwnership(cluster);
    Policies policies = getNamespacePolicies(property, cluster, namespace);
    // ensure the local cluster is the only cluster for the global namespace configuration
    try {
        if (cluster.equals(Namespaces.GLOBAL_CLUSTER)) {
            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 " + nsName + ". 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, "Cluser " + replCluster + " does not exist"));
                URL replClusterUrl;
                if (!config().isTlsEnabled()) {
                    replClusterUrl = new URL(replClusterData.getServiceUrl());
                } else if (!replClusterData.getServiceUrlTls().isEmpty()) {
                    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();
                log.debug("[{}] Redirecting the rest call to {}: cluster={}", clientAppId(), redirect, cluster);
                throw new WebApplicationException(Response.temporaryRedirect(redirect).build());
            }
        }
    } catch (WebApplicationException wae) {
        throw wae;
    } catch (Exception e) {
        throw new RestException(e);
    }
    NamespaceBundle bundle = validateNamespaceBundleOwnership(nsName, policies.bundles, bundleRange, authoritative, true);
    try {
        List<String> destinations = getDestinations(property, cluster, namespace);
        for (String destination : destinations) {
            NamespaceBundle destinationBundle = (NamespaceBundle) pulsar().getNamespaceService().getBundle(DestinationName.get(destination));
            if (bundle.equals(destinationBundle)) {
                throw new RestException(Status.CONFLICT, "Cannot delete non empty bundle");
            }
        }
        // remove from owned namespace map and ephemeral node from ZK
        pulsar().getNamespaceService().removeOwnedServiceUnit(bundle);
    } catch (WebApplicationException wae) {
        throw wae;
    } catch (Exception e) {
        log.error("[{}] Failed to remove namespace bundle {}/{}", clientAppId(), nsName.toString(), bundleRange, e);
        throw new RestException(e);
    }
}
Also used : NamespaceBundle(com.yahoo.pulsar.common.naming.NamespaceBundle) NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) Policies(com.yahoo.pulsar.common.policies.data.Policies) PersistencePolicies(com.yahoo.pulsar.common.policies.data.PersistencePolicies) RetentionPolicies(com.yahoo.pulsar.common.policies.data.RetentionPolicies) ClusterData(com.yahoo.pulsar.common.policies.data.ClusterData) WebApplicationException(javax.ws.rs.WebApplicationException) RestException(com.yahoo.pulsar.broker.web.RestException) URI(java.net.URI) URL(java.net.URL) RestException(com.yahoo.pulsar.broker.web.RestException) WebApplicationException(javax.ws.rs.WebApplicationException) SubscriptionBusyException(com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 30 with NamespaceBundle

use of com.yahoo.pulsar.common.naming.NamespaceBundle in project pulsar by yahoo.

the class ResourceQuotas method getNamespaceBundleResourceQuota.

@GET
@Path("/{property}/{cluster}/{namespace}/{bundle}")
@ApiOperation(value = "Get resource quota of a namespace bundle.")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Namespace does not exist") })
public ResourceQuota getNamespaceBundleResourceQuota(@PathParam("property") String property, @PathParam("cluster") String cluster, @PathParam("namespace") String namespace, @PathParam("bundle") String bundleRange) {
    validateSuperUserAccess();
    Policies policies = getNamespacePolicies(property, cluster, namespace);
    if (!cluster.equals(Namespaces.GLOBAL_CLUSTER)) {
        validateClusterOwnership(cluster);
        validateClusterForProperty(property, cluster);
    }
    NamespaceName fqnn = new NamespaceName(property, cluster, namespace);
    NamespaceBundle nsBundle = validateNamespaceBundleRange(fqnn, policies.bundles, bundleRange);
    try {
        return pulsar().getLocalZkCacheService().getResourceQuotaCache().getQuota(nsBundle);
    } catch (Exception e) {
        log.error("[{}] Failed to get resource quota for namespace bundle {}", clientAppId(), nsBundle.toString());
        throw new RestException(e);
    }
}
Also used : NamespaceBundle(com.yahoo.pulsar.common.naming.NamespaceBundle) NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) Policies(com.yahoo.pulsar.common.policies.data.Policies) RestException(com.yahoo.pulsar.broker.web.RestException) RestException(com.yahoo.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

NamespaceBundle (com.yahoo.pulsar.common.naming.NamespaceBundle)49 NamespaceName (com.yahoo.pulsar.common.naming.NamespaceName)40 Test (org.testng.annotations.Test)27 KeeperException (org.apache.zookeeper.KeeperException)19 NamespaceBundles (com.yahoo.pulsar.common.naming.NamespaceBundles)14 PulsarServerException (com.yahoo.pulsar.broker.PulsarServerException)13 RestException (com.yahoo.pulsar.broker.web.RestException)13 PulsarAdminException (com.yahoo.pulsar.client.admin.PulsarAdminException)11 Policies (com.yahoo.pulsar.common.policies.data.Policies)11 ApiOperation (io.swagger.annotations.ApiOperation)10 ApiResponses (io.swagger.annotations.ApiResponses)10 Path (javax.ws.rs.Path)10 DestinationName (com.yahoo.pulsar.common.naming.DestinationName)9 WebApplicationException (javax.ws.rs.WebApplicationException)9 Field (java.lang.reflect.Field)8 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)8 ServiceUnitNotReadyException (com.yahoo.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException)7 SubscriptionBusyException (com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)7 PersistencePolicies (com.yahoo.pulsar.common.policies.data.PersistencePolicies)7 RetentionPolicies (com.yahoo.pulsar.common.policies.data.RetentionPolicies)7