Search in sources :

Example 1 with BundlesData

use of org.apache.pulsar.common.policies.data.BundlesData in project incubator-pulsar by apache.

the class AdminResource method getNamespacePolicies.

protected Policies getNamespacePolicies(String property, String cluster, String namespace) {
    try {
        Policies policies = policiesCache().get(AdminResource.path(POLICIES, property, cluster, namespace)).orElseThrow(() -> new RestException(Status.NOT_FOUND, "Namespace does not exist"));
        // fetch bundles from LocalZK-policies
        NamespaceBundles bundles = pulsar().getNamespaceService().getNamespaceBundleFactory().getBundles(NamespaceName.get(property, cluster, namespace));
        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(), property, cluster, namespace, e);
        throw new RestException(e);
    }
}
Also used : NamespaceIsolationPolicies(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies) LocalPolicies(org.apache.pulsar.common.policies.data.LocalPolicies) Policies(org.apache.pulsar.common.policies.data.Policies) NamespaceBundles(org.apache.pulsar.common.naming.NamespaceBundles) RestException(org.apache.pulsar.broker.web.RestException) BundlesData(org.apache.pulsar.common.policies.data.BundlesData) RestException(org.apache.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) MalformedURLException(java.net.MalformedURLException) WebApplicationException(javax.ws.rs.WebApplicationException)

Example 2 with BundlesData

use of org.apache.pulsar.common.policies.data.BundlesData in project incubator-pulsar by apache.

the class DiscoveryServiceWebTest method testRedirectUrlWithServerStarted.

/**
 * 1. Start : Broker and Discovery service. 2. Provide started broker server as active broker to Discovery service
 * 3. Call GET, PUT, POST request to discovery service that redirects to Broker service and receives response
 *
 * @throws Exception
 */
@Test
public void testRedirectUrlWithServerStarted() throws Exception {
    // 1. start server
    int port = PortManager.nextFreePort();
    ServiceConfig config = new ServiceConfig();
    config.setWebServicePort(port);
    ServerManager server = new ServerManager(config);
    DiscoveryZooKeeperClientFactoryImpl.zk = mockZookKeeper;
    Map<String, String> params = new TreeMap<>();
    params.put("zookeeperServers", "");
    params.put("zookeeperClientFactoryClass", DiscoveryZooKeeperClientFactoryImpl.class.getName());
    server.addServlet("/", DiscoveryServiceServlet.class, params);
    server.start();
    String serviceUrl = server.getServiceUri().toString();
    String putRequestUrl = serviceUrl + "admin/namespaces/p1/c1/n1";
    String postRequestUrl = serviceUrl + "admin/namespaces/p1/c1/n1/replication";
    String getRequestUrl = serviceUrl + "admin/namespaces/p1";
    /**
     * verify : every time when vip receives a request: it redirects to above brokers sequentially and broker
     * returns appropriate response which must not be null.
     */
    assertEquals(hitBrokerService(HttpMethod.POST, postRequestUrl, Lists.newArrayList("use")), "Cannot set replication on a non-global namespace");
    assertEquals(hitBrokerService(HttpMethod.PUT, putRequestUrl, new BundlesData(1)), "Property does not exist");
    assertEquals(hitBrokerService(HttpMethod.GET, getRequestUrl, null), "Property does not exist");
    server.stop();
}
Also used : ServerManager(org.apache.pulsar.discovery.service.server.ServerManager) ServiceConfig(org.apache.pulsar.discovery.service.server.ServiceConfig) BundlesData(org.apache.pulsar.common.policies.data.BundlesData) TreeMap(java.util.TreeMap) Test(org.testng.annotations.Test)

Example 3 with BundlesData

use of org.apache.pulsar.common.policies.data.BundlesData in project incubator-pulsar by apache.

the class NamespacesBase method validateBundlesData.

protected BundlesData validateBundlesData(BundlesData initialBundles) {
    SortedSet<String> partitions = new TreeSet<String>();
    for (String partition : initialBundles.getBoundaries()) {
        Long partBoundary = Long.decode(partition);
        partitions.add(String.format("0x%08x", partBoundary));
    }
    if (partitions.size() != initialBundles.getBoundaries().size()) {
        log.debug("Input bundles included repeated partition points. Ignored.");
    }
    try {
        NamespaceBundleFactory.validateFullRange(partitions);
    } catch (IllegalArgumentException iae) {
        throw new RestException(Status.BAD_REQUEST, "Input bundles do not cover the whole hash range. first:" + partitions.first() + ", last:" + partitions.last());
    }
    List<String> bundles = Lists.newArrayList();
    bundles.addAll(partitions);
    return new BundlesData(bundles);
}
Also used : TreeSet(java.util.TreeSet) RestException(org.apache.pulsar.broker.web.RestException) BundlesData(org.apache.pulsar.common.policies.data.BundlesData)

Example 4 with BundlesData

use of org.apache.pulsar.common.policies.data.BundlesData in project incubator-pulsar by apache.

the class AdminTest method properties.

@Test
void properties() throws Exception {
    assertEquals(properties.getProperties(), Lists.newArrayList());
    verify(properties, times(1)).validateSuperUserAccess();
    Set<String> allowedClusters = Sets.newHashSet();
    PropertyAdmin propertyAdmin = new PropertyAdmin(Lists.newArrayList("role1", "role2"), allowedClusters);
    properties.createProperty("test-property", propertyAdmin);
    verify(properties, times(2)).validateSuperUserAccess();
    assertEquals(properties.getProperties(), Lists.newArrayList("test-property"));
    verify(properties, times(3)).validateSuperUserAccess();
    assertEquals(properties.getPropertyAdmin("test-property"), propertyAdmin);
    verify(properties, times(4)).validateSuperUserAccess();
    PropertyAdmin newPropertyAdmin = new PropertyAdmin(Lists.newArrayList("role1", "other-role"), allowedClusters);
    properties.updateProperty("test-property", newPropertyAdmin);
    verify(properties, times(5)).validateSuperUserAccess();
    // Wait for updateProperty to take effect
    Thread.sleep(100);
    assertEquals(properties.getPropertyAdmin("test-property"), newPropertyAdmin);
    assertNotSame(properties.getPropertyAdmin("test-property"), propertyAdmin);
    verify(properties, times(7)).validateSuperUserAccess();
    // Check creating existing property
    try {
        properties.createProperty("test-property", propertyAdmin);
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.CONFLICT.getStatusCode());
    }
    // Check non-existing property
    try {
        properties.getPropertyAdmin("non-existing");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.NOT_FOUND.getStatusCode());
    }
    try {
        properties.updateProperty("xxx-non-existing", newPropertyAdmin);
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.NOT_FOUND.getStatusCode());
    }
    // Check deleting non-existing property
    try {
        properties.deleteProperty("non-existing");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.NOT_FOUND.getStatusCode());
    }
    // Test zk failures
    mockZookKeeper.failNow(Code.SESSIONEXPIRED);
    try {
        properties.getProperties();
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    mockZookKeeper.failNow(Code.SESSIONEXPIRED);
    try {
        properties.getPropertyAdmin("my-property");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    mockZookKeeper.failNow(Code.SESSIONEXPIRED);
    try {
        properties.updateProperty("my-property", newPropertyAdmin);
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    mockZookKeeper.failNow(Code.SESSIONEXPIRED);
    try {
        properties.createProperty("test", propertyAdmin);
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    mockZookKeeper.failNow(Code.SESSIONEXPIRED);
    try {
        properties.deleteProperty("my-property");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    properties.createProperty("error-property", propertyAdmin);
    mockZookKeeper.failAfter(2, Code.SESSIONEXPIRED);
    try {
        properties.deleteProperty("error-property");
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
    properties.deleteProperty("test-property");
    properties.deleteProperty("error-property");
    assertEquals(properties.getProperties(), Lists.newArrayList());
    // Create a namespace to test deleting a non-empty property
    clusters.createCluster("use", new ClusterData());
    newPropertyAdmin = new PropertyAdmin(Lists.newArrayList("role1", "other-role"), Sets.newHashSet("use"));
    properties.createProperty("my-property", newPropertyAdmin);
    namespaces.createNamespace("my-property", "use", "my-namespace", new BundlesData());
    try {
        properties.deleteProperty("my-property");
        fail("should have failed");
    } catch (RestException e) {
    // Ok
    }
    // Check name validation
    try {
        properties.createProperty("test&", propertyAdmin);
        fail("should have failed");
    } catch (RestException e) {
        assertEquals(e.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
    }
    namespaces.deleteNamespace("my-property", "use", "my-namespace", false);
    properties.deleteProperty("my-property");
}
Also used : ClusterData(org.apache.pulsar.common.policies.data.ClusterData) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) RestException(org.apache.pulsar.broker.web.RestException) BundlesData(org.apache.pulsar.common.policies.data.BundlesData) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 5 with BundlesData

use of org.apache.pulsar.common.policies.data.BundlesData in project incubator-pulsar by apache.

the class NamespacesTest method testSplitBundles.

@Test
public void testSplitBundles() throws Exception {
    URL localWebServiceUrl = new URL(pulsar.getWebServiceAddress());
    String bundledNsLocal = "test-bundled-namespace-1";
    BundlesData bundleData = new BundlesData(Lists.newArrayList("0x00000000", "0xffffffff"));
    createBundledTestNamespaces(this.testProperty, this.testLocalCluster, bundledNsLocal, bundleData);
    final NamespaceName testNs = NamespaceName.get(this.testProperty, this.testLocalCluster, bundledNsLocal);
    OwnershipCache MockOwnershipCache = spy(pulsar.getNamespaceService().getOwnershipCache());
    doNothing().when(MockOwnershipCache).disableOwnership(any(NamespaceBundle.class));
    Field ownership = NamespaceService.class.getDeclaredField("ownershipCache");
    ownership.setAccessible(true);
    ownership.set(pulsar.getNamespaceService(), MockOwnershipCache);
    mockWebUrl(localWebServiceUrl, testNs);
    // split bundles
    try {
        namespaces.splitNamespaceBundle(testProperty, testLocalCluster, bundledNsLocal, "0x00000000_0xffffffff", false, true);
        // verify split bundles
        BundlesData bundlesData = namespaces.getBundlesData(testProperty, testLocalCluster, bundledNsLocal);
        assertNotNull(bundlesData);
        assertTrue(bundlesData.boundaries.size() == 3);
        assertTrue(bundlesData.boundaries.get(0).equals("0x00000000"));
        assertTrue(bundlesData.boundaries.get(1).equals("0x7fffffff"));
        assertTrue(bundlesData.boundaries.get(2).equals("0xffffffff"));
    } catch (RestException re) {
        assertEquals(re.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
    }
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) Field(java.lang.reflect.Field) OwnershipCache(org.apache.pulsar.broker.namespace.OwnershipCache) BundlesData(org.apache.pulsar.common.policies.data.BundlesData) RestException(org.apache.pulsar.broker.web.RestException) URL(java.net.URL) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Aggregations

BundlesData (org.apache.pulsar.common.policies.data.BundlesData)18 Test (org.testng.annotations.Test)14 RestException (org.apache.pulsar.broker.web.RestException)12 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)10 NamespaceName (org.apache.pulsar.common.naming.NamespaceName)8 URL (java.net.URL)7 NamespaceBundle (org.apache.pulsar.common.naming.NamespaceBundle)7 Field (java.lang.reflect.Field)5 OwnershipCache (org.apache.pulsar.broker.namespace.OwnershipCache)5 NamespaceBundles (org.apache.pulsar.common.naming.NamespaceBundles)4 LocalPolicies (org.apache.pulsar.common.policies.data.LocalPolicies)3 Policies (org.apache.pulsar.common.policies.data.Policies)3 KeeperException (org.apache.zookeeper.KeeperException)3 MalformedURLException (java.net.MalformedURLException)2 TreeMap (java.util.TreeMap)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 ClusterData (org.apache.pulsar.common.policies.data.ClusterData)2 PropertyAdmin (org.apache.pulsar.common.policies.data.PropertyAdmin)2 NamespaceIsolationPolicies (org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies)2 ServerManager (org.apache.pulsar.discovery.service.server.ServerManager)2