Search in sources :

Example 46 with NamespaceBundle

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

the class PulsarWebResource method validateNamespaceBundleRange.

protected NamespaceBundle validateNamespaceBundleRange(NamespaceName fqnn, BundlesData bundles, String bundleRange) {
    try {
        checkArgument(bundleRange.contains("_"), "Invalid bundle range");
        String[] boundaries = bundleRange.split("_");
        Long lowerEndpoint = Long.decode(boundaries[0]);
        Long upperEndpoint = Long.decode(boundaries[1]);
        Range<Long> hashRange = Range.range(lowerEndpoint, BoundType.CLOSED, upperEndpoint, (upperEndpoint.equals(NamespaceBundles.FULL_UPPER_BOUND)) ? BoundType.CLOSED : BoundType.OPEN);
        NamespaceBundle nsBundle = pulsar().getNamespaceService().getNamespaceBundleFactory().getBundle(fqnn, hashRange);
        NamespaceBundles nsBundles = pulsar().getNamespaceService().getNamespaceBundleFactory().getBundles(fqnn, bundles);
        nsBundles.validateBundle(nsBundle);
        return nsBundle;
    } catch (Exception e) {
        log.error("[{}] Failed to validate namespace bundle {}/{}", clientAppId(), fqnn.toString(), bundleRange, e);
        throw new RestException(e);
    }
}
Also used : NamespaceBundle(com.yahoo.pulsar.common.naming.NamespaceBundle) NamespaceBundles(com.yahoo.pulsar.common.naming.NamespaceBundles) KeeperException(org.apache.zookeeper.KeeperException) ExecutionException(java.util.concurrent.ExecutionException) WebApplicationException(javax.ws.rs.WebApplicationException)

Example 47 with NamespaceBundle

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

the class NamespacesTest method testDeleteNamespaceWithBundles.

@Test
public void testDeleteNamespaceWithBundles() 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 = new NamespaceName(this.testProperty, this.testLocalCluster, bundledNsLocal);
    com.yahoo.pulsar.client.admin.Namespaces namespacesAdmin = mock(com.yahoo.pulsar.client.admin.Namespaces.class);
    doReturn(namespacesAdmin).when(admin).namespaces();
    doReturn(null).when(nsSvc).getWebServiceUrl(Mockito.argThat(new Matcher<NamespaceBundle>() {

        @Override
        public void describeTo(Description description) {
        }

        @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_() {
        }
    }), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyBoolean());
    doReturn(false).when(nsSvc).isServiceUnitOwned(Mockito.argThat(new Matcher<NamespaceBundle>() {

        @Override
        public void describeTo(Description description) {
        }

        @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_() {
        }
    }));
    doReturn(Optional.of(new NamespaceEphemeralData())).when(nsSvc).getOwner(Mockito.argThat(new Matcher<NamespaceBundle>() {

        @Override
        public void describeTo(Description description) {
        }

        @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_() {
        }
    }));
    doThrow(new PulsarAdminException.PreconditionFailedException(new ClientErrorException(Status.PRECONDITION_FAILED))).when(namespacesAdmin).deleteNamespaceBundle(Mockito.anyString(), Mockito.anyString());
    try {
        namespaces.deleteNamespaceBundle(testProperty, testLocalCluster, bundledNsLocal, "0x00000000_0x80000000", false);
        fail("Should have failed");
    } catch (RestException re) {
        assertEquals(re.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
    }
    try {
        namespaces.deleteNamespace(testProperty, testLocalCluster, bundledNsLocal, false);
        fail("Should have failed");
    } catch (RestException re) {
        assertEquals(re.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
    }
    NamespaceBundles nsBundles = nsSvc.getNamespaceBundleFactory().getBundles(testNs, bundleData);
    // make one bundle owned
    doReturn(localWebServiceUrl).when(nsSvc).getWebServiceUrl(nsBundles.getBundles().get(0), false, true, false);
    doReturn(true).when(nsSvc).isServiceUnitOwned(nsBundles.getBundles().get(0));
    doNothing().when(namespacesAdmin).deleteNamespaceBundle(testProperty + "/" + testLocalCluster + "/" + bundledNsLocal, "0x00000000_0x80000000");
    try {
        namespaces.deleteNamespaceBundle(testProperty, testLocalCluster, bundledNsLocal, "0x80000000_0xffffffff", false);
        fail("Should have failed");
    } catch (RestException re) {
        assertEquals(re.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
    }
    try {
        namespaces.deleteNamespace(testProperty, testLocalCluster, bundledNsLocal, false);
        fail("should have failed");
    } catch (RestException re) {
        assertEquals(re.getResponse().getStatus(), Status.PRECONDITION_FAILED.getStatusCode());
    }
    // ensure all three bundles are owned by the local broker
    for (NamespaceBundle bundle : nsBundles.getBundles()) {
        doReturn(localWebServiceUrl).when(nsSvc).getWebServiceUrl(bundle, false, true, false);
        doReturn(true).when(nsSvc).isServiceUnitOwned(bundle);
    }
    doNothing().when(namespacesAdmin).deleteNamespaceBundle(Mockito.anyString(), Mockito.anyString());
}
Also used : NamespaceBundle(com.yahoo.pulsar.common.naming.NamespaceBundle) Description(org.hamcrest.Description) Matcher(org.hamcrest.Matcher) NamespaceBundles(com.yahoo.pulsar.common.naming.NamespaceBundles) BundlesData(com.yahoo.pulsar.common.policies.data.BundlesData) RestException(com.yahoo.pulsar.broker.web.RestException) URL(java.net.URL) NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) ClientErrorException(javax.ws.rs.ClientErrorException) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) NamespaceEphemeralData(com.yahoo.pulsar.broker.namespace.NamespaceEphemeralData) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 48 with NamespaceBundle

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

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 = new NamespaceName(this.testProperty, this.testLocalCluster, bundledNsLocal);
    doReturn(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(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(com.yahoo.pulsar.common.naming.NamespaceBundle) NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) Description(org.hamcrest.Description) Matcher(org.hamcrest.Matcher) NamespaceBundles(com.yahoo.pulsar.common.naming.NamespaceBundles) BundlesData(com.yahoo.pulsar.common.policies.data.BundlesData) RestException(com.yahoo.pulsar.broker.web.RestException) URL(java.net.URL) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 49 with NamespaceBundle

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

the class NamespacesTest method mockWebUrl.

private void mockWebUrl(URL localWebServiceUrl, NamespaceName namespace) throws Exception {
    doReturn(localWebServiceUrl).when(nsSvc).getWebServiceUrl(Mockito.argThat(new Matcher<NamespaceBundle>() {

        @Override
        public void describeTo(Description description) {
        }

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

        @Override
        public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
        }
    }), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyBoolean());
    doReturn(true).when(nsSvc).isServiceUnitOwned(Mockito.argThat(new Matcher<NamespaceBundle>() {

        @Override
        public void describeTo(Description description) {
        }

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

        @Override
        public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
        }
    }));
}
Also used : NamespaceBundle(com.yahoo.pulsar.common.naming.NamespaceBundle) Description(org.hamcrest.Description) Matcher(org.hamcrest.Matcher)

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