Search in sources :

Example 11 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(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);
    }
}
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 12 with BundlesData

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

the class BrokerServiceTest method testCreateNamespacePolicy.

/**
 * It verifies that policiesCache() copies global-policy data into local-policy data and returns combined result
 *
 * @throws Exception
 */
@Test
public void testCreateNamespacePolicy() throws Exception {
    final String namespace = "prop/use/testPolicy";
    final int totalBundle = 3;
    admin.namespaces().createNamespace(namespace, new BundlesData(totalBundle));
    String globalPath = joinPath(LOCAL_POLICIES_ROOT, namespace);
    pulsar.getLocalZkCacheService().policiesCache().clear();
    Optional<LocalPolicies> policy = pulsar.getLocalZkCacheService().policiesCache().get(globalPath);
    assertTrue(policy.isPresent());
    assertEquals(policy.get().bundles.numBundles, totalBundle);
}
Also used : BundlesData(org.apache.pulsar.common.policies.data.BundlesData) LocalPolicies(org.apache.pulsar.common.policies.data.LocalPolicies) Test(org.testng.annotations.Test)

Example 13 with BundlesData

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

the class NamespacesTest method setup.

@Override
@BeforeMethod
public void setup() throws Exception {
    super.internalSetup();
    namespaces = spy(new Namespaces());
    namespaces.setServletContext(new MockServletContext());
    namespaces.setPulsar(pulsar);
    doReturn(mockZookKeeper).when(namespaces).globalZk();
    doReturn(mockZookKeeper).when(namespaces).localZk();
    doReturn(pulsar.getConfigurationCache().propertiesCache()).when(namespaces).propertiesCache();
    doReturn(pulsar.getConfigurationCache().policiesCache()).when(namespaces).policiesCache();
    doReturn(false).when(namespaces).isRequestHttps();
    doReturn("test").when(namespaces).clientAppId();
    doReturn(Sets.newTreeSet(Lists.newArrayList("use", "usw", "usc", "global"))).when(namespaces).clusters();
    doNothing().when(namespaces).validateAdminAccessOnProperty("my-property");
    doNothing().when(namespaces).validateAdminAccessOnProperty("other-property");
    doNothing().when(namespaces).validateAdminAccessOnProperty("new-property");
    admin.clusters().createCluster("use", new ClusterData("http://broker-use.com:" + BROKER_WEBSERVICE_PORT));
    admin.clusters().createCluster("usw", new ClusterData("http://broker-usw.com:" + BROKER_WEBSERVICE_PORT));
    admin.clusters().createCluster("usc", new ClusterData("http://broker-usc.com:" + BROKER_WEBSERVICE_PORT));
    admin.properties().createProperty(this.testProperty, new PropertyAdmin(Lists.newArrayList("role1", "role2"), Sets.newHashSet("use", "usc", "usw")));
    createTestNamespaces(this.testProperty, this.testLocalNamespaces, new BundlesData());
    createGlobalTestNamespaces(this.testProperty, this.testGlobalNamespaces.get(0).getLocalName(), new BundlesData());
    nsSvc = pulsar.getNamespaceService();
}
Also used : Namespaces(org.apache.pulsar.broker.admin.v1.Namespaces) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) BundlesData(org.apache.pulsar.common.policies.data.BundlesData) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 14 with BundlesData

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

the class NamespacesTest method testUnloadNamespaceWithBundles.

@Test
public void testUnloadNamespaceWithBundles() throws Exception {
    URL localWebServiceUrl = new URL(pulsar.getWebServiceAddress());
    String bundledNsLocal = "test-bundled-namespace-1";
    BundlesData bundleData = new BundlesData(Lists.newArrayList("0x00000000", "0x80000000", "0xffffffff"));
    createBundledTestNamespaces(this.testProperty, this.testLocalCluster, bundledNsLocal, bundleData);
    final NamespaceName testNs = NamespaceName.get(this.testProperty, this.testLocalCluster, bundledNsLocal);
    doReturn(Optional.of(localWebServiceUrl)).when(nsSvc).getWebServiceUrl(Mockito.argThat(new Matcher<NamespaceBundle>() {

        @Override
        public void describeTo(Description description) {
        // TODO Auto-generated method stub
        }

        @Override
        public boolean matches(Object item) {
            if (item instanceof NamespaceBundle) {
                NamespaceBundle bundle = (NamespaceBundle) item;
                return bundle.getNamespaceObject().equals(testNs);
            }
            return false;
        }

        @Override
        public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
        // TODO Auto-generated method stub
        }
    }), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyBoolean());
    doReturn(true).when(nsSvc).isServiceUnitOwned(Mockito.argThat(new Matcher<NamespaceBundle>() {

        @Override
        public void describeTo(Description description) {
        // TODO Auto-generated method stub
        }

        @Override
        public boolean matches(Object item) {
            if (item instanceof NamespaceBundle) {
                NamespaceBundle bundle = (NamespaceBundle) item;
                return bundle.getNamespaceObject().equals(testNs);
            }
            return false;
        }

        @Override
        public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
        // TODO Auto-generated method stub
        }
    }));
    NamespaceBundles nsBundles = nsSvc.getNamespaceBundleFactory().getBundles(testNs, bundleData);
    NamespaceBundle testBundle = nsBundles.getBundles().get(0);
    // make one bundle owned
    doReturn(Optional.of(localWebServiceUrl)).when(nsSvc).getWebServiceUrl(testBundle, false, true, false);
    doReturn(true).when(nsSvc).isServiceUnitOwned(testBundle);
    doNothing().when(nsSvc).unloadNamespaceBundle(testBundle);
    namespaces.unloadNamespaceBundle(testProperty, testLocalCluster, bundledNsLocal, "0x00000000_0x80000000", false);
    verify(nsSvc, times(1)).unloadNamespaceBundle(testBundle);
    try {
        namespaces.unloadNamespaceBundle(testProperty, testLocalCluster, bundledNsLocal, "0x00000000_0x88000000", false);
        fail("should have failed");
    } catch (RestException re) {
    // ok
    }
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) Description(org.hamcrest.Description) Matcher(org.hamcrest.Matcher) NamespaceBundles(org.apache.pulsar.common.naming.NamespaceBundles) 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)

Example 15 with BundlesData

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

the class NamespacesTest method testCreateNamespaces.

@Test
public void testCreateNamespaces() throws Exception {
    try {
        namespaces.createNamespace("my-property", "other-colo", "my-namespace", new BundlesData());
        fail("should have failed");
    } catch (RestException e) {
    // Ok, cluster doesn't exist
    }
    List<NamespaceName> nsnames = Lists.newArrayList();
    nsnames.add(NamespaceName.get("my-property", "use", "create-namespace-1"));
    nsnames.add(NamespaceName.get("my-property", "use", "create-namespace-2"));
    nsnames.add(NamespaceName.get("my-property", "usc", "create-other-namespace-1"));
    createTestNamespaces("my-property", nsnames, new BundlesData());
    try {
        namespaces.createNamespace("my-property", "use", "create-namespace-1", new BundlesData());
        fail("should have failed");
    } catch (RestException e) {
    // Ok, namespace already exists
    }
    try {
        namespaces.createNamespace("other-property", "use", "create-namespace-1", new BundlesData());
        fail("should have failed");
    } catch (RestException e) {
    // Ok, property doesn't exist
    }
    try {
        namespaces.createNamespace("my-property", "use", "create-namespace-#", new BundlesData());
        fail("should have failed");
    } catch (RestException e) {
        // Ok, invalid namespace name
        assertEquals(e.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
    }
    mockZookKeeper.failNow(Code.SESSIONEXPIRED);
    try {
        namespaces.createNamespace("my-property", "use", "my-namespace-3", new BundlesData());
        fail("should have failed");
    } catch (RestException e) {
    // Ok
    }
}
Also used : NamespaceName(org.apache.pulsar.common.naming.NamespaceName) BundlesData(org.apache.pulsar.common.policies.data.BundlesData) RestException(org.apache.pulsar.broker.web.RestException) 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