Search in sources :

Example 1 with BrokerNamespaceIsolationData

use of org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationData in project pulsar by apache.

the class ClustersBase method getBrokersWithNamespaceIsolationPolicy.

@GET
@Path("/{cluster}/namespaceIsolationPolicies/brokers")
@ApiOperation(value = "Get list of brokers with namespace-isolation policies attached to them.", response = BrokerNamespaceIsolationDataImpl.class, responseContainer = "set", notes = "This operation requires Pulsar superuser privileges.")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission."), @ApiResponse(code = 404, message = "Namespace-isolation policies not found."), @ApiResponse(code = 412, message = "Cluster doesn't exist."), @ApiResponse(code = 500, message = "Internal server error.") })
public List<BrokerNamespaceIsolationData> getBrokersWithNamespaceIsolationPolicy(@ApiParam(value = "The cluster name", required = true) @PathParam("cluster") String cluster) {
    validateSuperUserAccess();
    validateClusterExists(cluster);
    Set<String> availableBrokers;
    Map<String, ? extends NamespaceIsolationData> nsPolicies;
    try {
        availableBrokers = pulsar().getLoadManager().get().getAvailableBrokers();
    } catch (Exception e) {
        log.error("[{}] Failed to get list of brokers in cluster {}", clientAppId(), cluster, e);
        throw new RestException(e);
    }
    try {
        Optional<NamespaceIsolationPolicies> nsPoliciesResult = namespaceIsolationPolicies().getIsolationDataPolicies(cluster);
        if (!nsPoliciesResult.isPresent()) {
            throw new RestException(Status.NOT_FOUND, "namespace-isolation policies not found for " + cluster);
        }
        nsPolicies = nsPoliciesResult.get().getPolicies();
    } catch (Exception e) {
        log.error("[{}] Failed to get namespace isolation-policies {}", clientAppId(), cluster, e);
        throw new RestException(e);
    }
    return availableBrokers.stream().map(broker -> {
        BrokerNamespaceIsolationData.Builder brokerIsolationData = BrokerNamespaceIsolationData.builder().brokerName(broker);
        if (nsPolicies != null) {
            List<String> namespaceRegexes = new ArrayList<>();
            nsPolicies.forEach((name, policyData) -> {
                NamespaceIsolationPolicyImpl nsPolicyImpl = new NamespaceIsolationPolicyImpl(policyData);
                if (nsPolicyImpl.isPrimaryBroker(broker) || nsPolicyImpl.isSecondaryBroker(broker)) {
                    namespaceRegexes.addAll(policyData.getNamespaces());
                    if (nsPolicyImpl.isPrimaryBroker(broker)) {
                        brokerIsolationData.primary(true);
                    }
                }
            });
            brokerIsolationData.namespaceRegex(namespaceRegexes);
        }
        return brokerIsolationData.build();
    }).collect(Collectors.toList());
}
Also used : Constants(org.apache.pulsar.common.naming.Constants) ClusterDataImpl(org.apache.pulsar.common.policies.data.ClusterDataImpl) ObjectMapperFactory(org.apache.pulsar.common.util.ObjectMapperFactory) Path(javax.ws.rs.Path) LoggerFactory(org.slf4j.LoggerFactory) ApiParam(io.swagger.annotations.ApiParam) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) ApiOperation(io.swagger.annotations.ApiOperation) NamespaceIsolationPolicies(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies) MediaType(javax.ws.rs.core.MediaType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Example(io.swagger.annotations.Example) Map(java.util.Map) RestException(org.apache.pulsar.broker.web.RestException) NamedEntity(org.apache.pulsar.common.naming.NamedEntity) DELETE(javax.ws.rs.DELETE) BrokerNamespaceIsolationDataImpl(org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationDataImpl) AsyncResponse(javax.ws.rs.container.AsyncResponse) Set(java.util.Set) Collectors(java.util.stream.Collectors) Suspended(javax.ws.rs.container.Suspended) List(java.util.List) FutureUtil(org.apache.pulsar.common.util.FutureUtil) Response(javax.ws.rs.core.Response) NotFoundException(org.apache.pulsar.metadata.api.MetadataStoreException.NotFoundException) Optional(java.util.Optional) PathParam(javax.ws.rs.PathParam) NamespaceIsolationDataImpl(org.apache.pulsar.common.policies.data.NamespaceIsolationDataImpl) GET(javax.ws.rs.GET) FailureDomainResources(org.apache.pulsar.broker.resources.ClusterResources.FailureDomainResources) CompletableFuture(java.util.concurrent.CompletableFuture) ApiResponses(io.swagger.annotations.ApiResponses) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) Namespaces(org.apache.pulsar.client.admin.Namespaces) NamespaceIsolationPolicyImpl(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicyImpl) Status(javax.ws.rs.core.Response.Status) LinkedHashSet(java.util.LinkedHashSet) BrokerNamespaceIsolationData(org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationData) POST(javax.ws.rs.POST) Logger(org.slf4j.Logger) PulsarWebResource(org.apache.pulsar.broker.web.PulsarWebResource) Maps(com.google.common.collect.Maps) ApiResponse(io.swagger.annotations.ApiResponse) ExampleProperty(io.swagger.annotations.ExampleProperty) PUT(javax.ws.rs.PUT) FailureDomainImpl(org.apache.pulsar.common.policies.data.FailureDomainImpl) Collections(java.util.Collections) NamespaceIsolationData(org.apache.pulsar.common.policies.data.NamespaceIsolationData) NamespaceIsolationPolicies(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicies) NamespaceIsolationPolicyImpl(org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicyImpl) RestException(org.apache.pulsar.broker.web.RestException) List(java.util.List) ArrayList(java.util.ArrayList) RestException(org.apache.pulsar.broker.web.RestException) NotFoundException(org.apache.pulsar.metadata.api.MetadataStoreException.NotFoundException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with BrokerNamespaceIsolationData

use of org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationData in project pulsar by apache.

the class V1_AdminApiTest method clusterNamespaceIsolationPolicies.

@Test
public void clusterNamespaceIsolationPolicies() throws PulsarAdminException {
    try {
        // create
        String policyName1 = "policy-1";
        Map<String, String> parameters1 = new HashMap<>();
        parameters1.put("min_limit", "1");
        parameters1.put("usage_threshold", "100");
        NamespaceIsolationData nsPolicyData1 = NamespaceIsolationData.builder().namespaces(Lists.newArrayList("other/use/other.*")).primary(// match all broker. make it easy to verify `getBrokersWithNamespaceIsolationPolicy` later
        Lists.newArrayList(".*")).secondary(Lists.newArrayList("prod1-broker.*.messaging.use.example.com")).autoFailoverPolicy(AutoFailoverPolicyData.builder().policyType(AutoFailoverPolicyType.min_available).parameters(parameters1).build()).build();
        admin.clusters().createNamespaceIsolationPolicy("use", policyName1, nsPolicyData1);
        String policyName2 = "policy-2";
        Map<String, String> parameters2 = new HashMap<>();
        parameters2.put("min_limit", "1");
        parameters2.put("usage_threshold", "100");
        NamespaceIsolationData nsPolicyData2 = NamespaceIsolationData.builder().namespaces(Lists.newArrayList("other/use/other.*")).primary(Lists.newArrayList("prod1-broker[4-6].messaging.use.example.com")).secondary(Lists.newArrayList("prod1-broker.*.messaging.use.example.com")).autoFailoverPolicy(AutoFailoverPolicyData.builder().policyType(AutoFailoverPolicyType.min_available).parameters(parameters1).build()).build();
        admin.clusters().createNamespaceIsolationPolicy("use", policyName2, nsPolicyData2);
        // verify create indirectly with get
        Map<String, ? extends NamespaceIsolationData> policiesMap = admin.clusters().getNamespaceIsolationPolicies("use");
        assertEquals(policiesMap.get(policyName1), nsPolicyData1);
        assertEquals(policiesMap.get(policyName2), nsPolicyData2);
        // verify local broker get matched.
        List<BrokerNamespaceIsolationData> isoList = admin.clusters().getBrokersWithNamespaceIsolationPolicy("use");
        assertEquals(isoList.size(), 1);
        assertTrue(isoList.get(0).isPrimary());
        // verify update of primary
        nsPolicyData1.getPrimary().remove(0);
        nsPolicyData1.getPrimary().add("prod1-broker[1-2].messaging.use.example.com");
        admin.clusters().updateNamespaceIsolationPolicy("use", policyName1, nsPolicyData1);
        // verify primary change
        policiesMap = admin.clusters().getNamespaceIsolationPolicies("use");
        assertEquals(policiesMap.get(policyName1), nsPolicyData1);
        // verify update of secondary
        nsPolicyData1.getSecondary().remove(0);
        nsPolicyData1.getSecondary().add("prod1-broker[3-4].messaging.use.example.com");
        admin.clusters().updateNamespaceIsolationPolicy("use", policyName1, nsPolicyData1);
        // verify secondary change
        policiesMap = admin.clusters().getNamespaceIsolationPolicies("use");
        assertEquals(policiesMap.get(policyName1), nsPolicyData1);
        // verify update of failover policy limit
        nsPolicyData1.getAutoFailoverPolicy().getParameters().put("min_limit", "10");
        admin.clusters().updateNamespaceIsolationPolicy("use", policyName1, nsPolicyData1);
        // verify min_limit change
        policiesMap = admin.clusters().getNamespaceIsolationPolicies("use");
        assertEquals(policiesMap.get(policyName1), nsPolicyData1);
        // verify update of failover usage_threshold limit
        nsPolicyData1.getAutoFailoverPolicy().getParameters().put("usage_threshold", "80");
        admin.clusters().updateNamespaceIsolationPolicy("use", policyName1, nsPolicyData1);
        // verify usage_threshold change
        policiesMap = admin.clusters().getNamespaceIsolationPolicies("use");
        assertEquals(policiesMap.get(policyName1), nsPolicyData1);
        // verify single get
        NamespaceIsolationDataImpl policy1Data = (NamespaceIsolationDataImpl) admin.clusters().getNamespaceIsolationPolicy("use", policyName1);
        assertEquals(policy1Data, nsPolicyData1);
        // verify creation of more than one policy
        admin.clusters().createNamespaceIsolationPolicy("use", policyName2, nsPolicyData1);
        try {
            admin.clusters().getNamespaceIsolationPolicy("use", "no-such-policy");
            fail("should have raised exception");
        } catch (PulsarAdminException e) {
            assertTrue(e instanceof NotFoundException);
        }
        // verify delete cluster failed
        try {
            admin.clusters().deleteCluster("use");
            fail("should have raised exception");
        } catch (PulsarAdminException e) {
            assertTrue(e instanceof PreconditionFailedException);
        }
        // verify delete
        admin.clusters().deleteNamespaceIsolationPolicy("use", policyName1);
        admin.clusters().deleteNamespaceIsolationPolicy("use", policyName2);
        try {
            admin.clusters().getNamespaceIsolationPolicy("use", policyName1);
            fail("should have raised exception");
        } catch (PulsarAdminException e) {
            assertTrue(e instanceof NotFoundException);
        }
        try {
            admin.clusters().getNamespaceIsolationPolicy("use", policyName2);
            fail("should have raised exception");
        } catch (PulsarAdminException e) {
            assertTrue(e instanceof NotFoundException);
        }
        try {
            admin.clusters().getNamespaceIsolationPolicies("usc");
            fail("should have raised exception");
        } catch (PulsarAdminException e) {
            assertTrue(e instanceof NotFoundException);
        }
        try {
            admin.clusters().getNamespaceIsolationPolicy("usc", "no-such-cluster");
            fail("should have raised exception");
        } catch (PulsarAdminException e) {
            assertTrue(e instanceof PreconditionFailedException);
        }
        try {
            admin.clusters().createNamespaceIsolationPolicy("usc", "no-such-cluster", nsPolicyData1);
            fail("should have raised exception");
        } catch (PulsarAdminException e) {
            assertTrue(e instanceof PreconditionFailedException);
        }
        try {
            admin.clusters().updateNamespaceIsolationPolicy("usc", "no-such-cluster", policy1Data);
            fail("should have raised exception");
        } catch (PulsarAdminException e) {
            assertTrue(e instanceof PreconditionFailedException);
        }
    } catch (PulsarAdminException e) {
        LOG.warn("TEST FAILED [{}]", e.getMessage());
        throw e;
    }
}
Also used : BrokerNamespaceIsolationData(org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationData) HashMap(java.util.HashMap) BrokerNamespaceIsolationData(org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationData) NamespaceIsolationData(org.apache.pulsar.common.policies.data.NamespaceIsolationData) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) NamespaceIsolationDataImpl(org.apache.pulsar.common.policies.data.NamespaceIsolationDataImpl) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 3 with BrokerNamespaceIsolationData

use of org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationData in project pulsar by apache.

the class ClustersImpl method getBrokerWithNamespaceIsolationPolicyAsync.

@Override
public CompletableFuture<BrokerNamespaceIsolationData> getBrokerWithNamespaceIsolationPolicyAsync(String cluster, String broker) {
    WebTarget path = adminClusters.path(cluster).path("namespaceIsolationPolicies").path("brokers").path(broker);
    final CompletableFuture<BrokerNamespaceIsolationData> future = new CompletableFuture<>();
    asyncGetRequest(path, new InvocationCallback<BrokerNamespaceIsolationDataImpl>() {

        @Override
        public void completed(BrokerNamespaceIsolationDataImpl brokerNamespaceIsolationData) {
            future.complete(brokerNamespaceIsolationData);
        }

        @Override
        public void failed(Throwable throwable) {
            future.completeExceptionally(getApiException(throwable.getCause()));
        }
    });
    return future;
}
Also used : BrokerNamespaceIsolationData(org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationData) CompletableFuture(java.util.concurrent.CompletableFuture) BrokerNamespaceIsolationDataImpl(org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationDataImpl) WebTarget(javax.ws.rs.client.WebTarget)

Example 4 with BrokerNamespaceIsolationData

use of org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationData in project pulsar by yahoo.

the class ClustersImpl method getBrokerWithNamespaceIsolationPolicyAsync.

@Override
public CompletableFuture<BrokerNamespaceIsolationData> getBrokerWithNamespaceIsolationPolicyAsync(String cluster, String broker) {
    WebTarget path = adminClusters.path(cluster).path("namespaceIsolationPolicies").path("brokers").path(broker);
    final CompletableFuture<BrokerNamespaceIsolationData> future = new CompletableFuture<>();
    asyncGetRequest(path, new InvocationCallback<BrokerNamespaceIsolationDataImpl>() {

        @Override
        public void completed(BrokerNamespaceIsolationDataImpl brokerNamespaceIsolationData) {
            future.complete(brokerNamespaceIsolationData);
        }

        @Override
        public void failed(Throwable throwable) {
            future.completeExceptionally(getApiException(throwable.getCause()));
        }
    });
    return future;
}
Also used : BrokerNamespaceIsolationData(org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationData) CompletableFuture(java.util.concurrent.CompletableFuture) BrokerNamespaceIsolationDataImpl(org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationDataImpl) WebTarget(javax.ws.rs.client.WebTarget)

Example 5 with BrokerNamespaceIsolationData

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

the class AdminApi2Test method brokerNamespaceIsolationPolicies.

@Test
public void brokerNamespaceIsolationPolicies() throws Exception {
    // create
    String policyName1 = "policy-1";
    String cluster = pulsar.getConfiguration().getClusterName();
    String namespaceRegex = "other/" + cluster + "/other.*";
    String brokerName = pulsar.getAdvertisedAddress();
    String brokerAddress = brokerName + ":" + pulsar.getConfiguration().getWebServicePort().get();
    Map<String, String> parameters1 = new HashMap<>();
    parameters1.put("min_limit", "1");
    parameters1.put("usage_threshold", "100");
    NamespaceIsolationData nsPolicyData1 = NamespaceIsolationData.builder().namespaces(Collections.singletonList(namespaceRegex)).primary(Collections.singletonList(brokerName + ":[0-9]*")).secondary(Collections.singletonList(brokerName + ".*")).autoFailoverPolicy(AutoFailoverPolicyData.builder().policyType(AutoFailoverPolicyType.min_available).parameters(parameters1).build()).build();
    admin.clusters().createNamespaceIsolationPolicy(cluster, policyName1, nsPolicyData1);
    List<BrokerNamespaceIsolationData> brokerIsolationDataList = admin.clusters().getBrokersWithNamespaceIsolationPolicy(cluster);
    assertEquals(brokerIsolationDataList.size(), 1);
    assertEquals(brokerIsolationDataList.get(0).getBrokerName(), brokerAddress);
    assertEquals(brokerIsolationDataList.get(0).getNamespaceRegex().size(), 1);
    assertEquals(brokerIsolationDataList.get(0).getNamespaceRegex().get(0), namespaceRegex);
    assertEquals(brokerIsolationDataList.get(0).getPolicyName(), policyName1);
    BrokerNamespaceIsolationDataImpl brokerIsolationData = (BrokerNamespaceIsolationDataImpl) admin.clusters().getBrokerWithNamespaceIsolationPolicy(cluster, brokerAddress);
    assertEquals(brokerIsolationData.getBrokerName(), brokerAddress);
    assertEquals(brokerIsolationData.getNamespaceRegex().size(), 1);
    assertEquals(brokerIsolationData.getNamespaceRegex().get(0), namespaceRegex);
    BrokerNamespaceIsolationDataImpl isolationData = (BrokerNamespaceIsolationDataImpl) admin.clusters().getBrokerWithNamespaceIsolationPolicy(cluster, "invalid-broker");
    assertFalse(isolationData.isPrimary());
}
Also used : BrokerNamespaceIsolationData(org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationData) BrokerNamespaceIsolationDataImpl(org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationDataImpl) HashMap(java.util.HashMap) BrokerNamespaceIsolationData(org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationData) NamespaceIsolationData(org.apache.pulsar.common.policies.data.NamespaceIsolationData) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Aggregations

BrokerNamespaceIsolationData (org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationData)13 BrokerNamespaceIsolationDataImpl (org.apache.pulsar.common.policies.data.BrokerNamespaceIsolationDataImpl)7 NamespaceIsolationData (org.apache.pulsar.common.policies.data.NamespaceIsolationData)7 HashMap (java.util.HashMap)6 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)6 Test (org.testng.annotations.Test)6 ArrayList (java.util.ArrayList)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 NamespaceIsolationDataImpl (org.apache.pulsar.common.policies.data.NamespaceIsolationDataImpl)4 WebTarget (javax.ws.rs.client.WebTarget)3 NamespaceIsolationPolicyImpl (org.apache.pulsar.common.policies.impl.NamespaceIsolationPolicyImpl)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 RestException (org.apache.pulsar.broker.web.RestException)2 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)2 NotFoundException (org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException)2 PreconditionFailedException (org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException)2 Lists (com.google.common.collect.Lists)1