Search in sources :

Example 1 with NamespaceIsolationPolicies

use of com.yahoo.pulsar.common.policies.impl.NamespaceIsolationPolicies in project pulsar by yahoo.

the class SimpleResourceAllocationPolicies method getNamespaceIsolationPolicy.

private NamespaceIsolationPolicy getNamespaceIsolationPolicy(NamespaceName namespace) {
    NamespaceIsolationPolicies policies = null;
    NamespaceIsolationPolicy policy = null;
    try {
        policies = getIsolationPolicies(pulsar.getConfiguration().getClusterName());
        policy = policies.getPolicyByNamespace(namespace);
    } catch (Exception e) {
        LOG.warn("Unable to get the namespaceIsolationPolicies [{}]", e);
    }
    return policy;
}
Also used : NamespaceIsolationPolicies(com.yahoo.pulsar.common.policies.impl.NamespaceIsolationPolicies) NamespaceIsolationPolicy(com.yahoo.pulsar.common.policies.NamespaceIsolationPolicy)

Example 2 with NamespaceIsolationPolicies

use of com.yahoo.pulsar.common.policies.impl.NamespaceIsolationPolicies in project pulsar by yahoo.

the class SimpleResourceAllocationPolicies method IsIsolationPoliciesPresent.

public boolean IsIsolationPoliciesPresent(NamespaceName namespace) {
    try {
        NamespaceIsolationPolicies policies = null;
        policies = getIsolationPolicies(pulsar.getConfiguration().getClusterName());
        if (policies != null)
            return policies.getPolicyByNamespace(namespace) != null;
    } catch (Exception e) {
        LOG.warn("IsIsolationPoliciesPresent: Unable to get the namespaceIsolationPolicies [{}]", e);
    }
    return false;
}
Also used : NamespaceIsolationPolicies(com.yahoo.pulsar.common.policies.impl.NamespaceIsolationPolicies)

Example 3 with NamespaceIsolationPolicies

use of com.yahoo.pulsar.common.policies.impl.NamespaceIsolationPolicies in project pulsar by yahoo.

the class Clusters method deleteNamespaceIsolationPolicy.

@DELETE
@Path("/{cluster}/namespaceIsolationPolicies/{policyName}")
@ApiOperation(value = "Delete namespace isolation policy")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission or plicy is read only"), @ApiResponse(code = 412, message = "Cluster doesn't exist") })
public void deleteNamespaceIsolationPolicy(@PathParam("cluster") String cluster, @PathParam("policyName") String policyName) throws Exception {
    validateSuperUserAccess();
    validateClusterExists(cluster);
    validatePoliciesReadOnlyAccess();
    try {
        String nsIsolationPolicyPath = path("clusters", cluster, "namespaceIsolationPolicies");
        NamespaceIsolationPolicies nsIsolationPolicies = namespaceIsolationPoliciesCache().get(nsIsolationPolicyPath).orElseGet(() -> {
            try {
                this.createNamespaceIsolationPolicyNode(nsIsolationPolicyPath);
                return new NamespaceIsolationPolicies();
            } catch (KeeperException | InterruptedException e) {
                throw new RestException(e);
            }
        });
        nsIsolationPolicies.deletePolicy(policyName);
        globalZk().setData(nsIsolationPolicyPath, jsonMapper().writeValueAsBytes(nsIsolationPolicies.getPolicies()), -1);
        // make sure that the cache content will be refreshed for the next read access
        namespaceIsolationPoliciesCache().invalidate(nsIsolationPolicyPath);
    } catch (KeeperException.NoNodeException nne) {
        log.warn("[{}] Failed to update brokers/{}/namespaceIsolationPolicies: Does not exist", clientAppId(), cluster);
        throw new RestException(Status.NOT_FOUND, "NamespaceIsolationPolicies for cluster " + cluster + " does not exist");
    } catch (Exception e) {
        log.error("[{}] Failed to update brokers/{}/namespaceIsolationPolicies/{}", clientAppId(), cluster, policyName, e);
        throw new RestException(e);
    }
}
Also used : NamespaceIsolationPolicies(com.yahoo.pulsar.common.policies.impl.NamespaceIsolationPolicies) RestException(com.yahoo.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) RestException(com.yahoo.pulsar.broker.web.RestException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 4 with NamespaceIsolationPolicies

use of com.yahoo.pulsar.common.policies.impl.NamespaceIsolationPolicies in project pulsar by yahoo.

the class Clusters method getNamespaceIsolationPolicy.

@GET
@Path("/{cluster}/namespaceIsolationPolicies/{policyName}")
@ApiOperation(value = "Get a single namespace isolation policy assigned in the cluster", response = NamespaceIsolationData.class)
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Policy doesn't exist"), @ApiResponse(code = 412, message = "Cluster doesn't exist") })
public NamespaceIsolationData getNamespaceIsolationPolicy(@PathParam("cluster") String cluster, @PathParam("policyName") String policyName) throws Exception {
    validateSuperUserAccess();
    validateClusterExists(cluster);
    try {
        NamespaceIsolationPolicies nsIsolationPolicies = namespaceIsolationPoliciesCache().get(path("clusters", cluster, "namespaceIsolationPolicies")).orElseThrow(() -> new RestException(Status.NOT_FOUND, "NamespaceIsolationPolicies for cluster " + cluster + " does not exist"));
        // construct the response to NamespaceisolationData map
        if (!nsIsolationPolicies.getPolicies().containsKey(policyName)) {
            log.info("[{}] Cannot find NamespaceIsolationPolicy {} for cluster {}", policyName, cluster);
            throw new RestException(Status.NOT_FOUND, "Cannot find NamespaceIsolationPolicy " + policyName + " for cluster " + cluster);
        }
        return nsIsolationPolicies.getPolicies().get(policyName);
    } catch (RestException re) {
        throw re;
    } catch (Exception e) {
        log.error("[{}] Failed to get clusters/{}/namespaceIsolationPolicies/{}", clientAppId(), cluster, e);
        throw new RestException(e);
    }
}
Also used : NamespaceIsolationPolicies(com.yahoo.pulsar.common.policies.impl.NamespaceIsolationPolicies) RestException(com.yahoo.pulsar.broker.web.RestException) RestException(com.yahoo.pulsar.broker.web.RestException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 5 with NamespaceIsolationPolicies

use of com.yahoo.pulsar.common.policies.impl.NamespaceIsolationPolicies in project pulsar by yahoo.

the class LoadBalancerTest method createNamespacePolicies.

private void createNamespacePolicies(PulsarService pulsar) throws Exception {
    // // prepare three policies for the namespace isolation
    NamespaceIsolationPolicies policies = new NamespaceIsolationPolicies();
    // set up policy that use this broker as primary
    NamespaceIsolationData policyData = new NamespaceIsolationData();
    policyData.namespaces = new ArrayList<String>();
    policyData.namespaces.add("pulsar/use/primary-ns.*");
    policyData.primary = new ArrayList<String>();
    for (int i = 0; i < BROKER_COUNT; i++) {
        policyData.primary.add(lookupAddresses[i]);
    }
    policyData.secondary = new ArrayList<String>();
    policyData.auto_failover_policy = new AutoFailoverPolicyData();
    policyData.auto_failover_policy.policy_type = AutoFailoverPolicyType.min_available;
    policyData.auto_failover_policy.parameters = new HashMap<String, String>();
    policyData.auto_failover_policy.parameters.put("min_limit", "1");
    policyData.auto_failover_policy.parameters.put("usage_threshold", "100");
    policies.setPolicy("primaryBrokerPolicy", policyData);
    // set up policy that use this broker as secondary
    policyData = new NamespaceIsolationData();
    policyData.namespaces = new ArrayList<String>();
    policyData.namespaces.add("pulsar/use/secondary-ns.*");
    policyData.primary = new ArrayList<String>();
    policyData.primary.add(lookupAddresses[0]);
    policyData.secondary = new ArrayList<String>();
    for (int i = 1; i < BROKER_COUNT; i++) {
        policyData.secondary.add(lookupAddresses[i]);
    }
    policyData.auto_failover_policy = new AutoFailoverPolicyData();
    policyData.auto_failover_policy.policy_type = AutoFailoverPolicyType.min_available;
    policyData.auto_failover_policy.parameters = new HashMap<String, String>();
    policyData.auto_failover_policy.parameters.put("min_limit", "1");
    policyData.auto_failover_policy.parameters.put("usage_threshold", "100");
    policies.setPolicy("secondaryBrokerPolicy", policyData);
    // set up policy that do not use this broker (neither primary nor secondary)
    policyData = new NamespaceIsolationData();
    policyData.namespaces = new ArrayList<String>();
    policyData.namespaces.add("pulsar/use/shared-ns.*");
    policyData.primary = new ArrayList<String>();
    policyData.primary.add(lookupAddresses[0]);
    policyData.secondary = new ArrayList<String>();
    for (int i = 1; i < BROKER_COUNT; i++) {
        policyData.secondary.add(lookupAddresses[i]);
    }
    policyData.auto_failover_policy = new AutoFailoverPolicyData();
    policyData.auto_failover_policy.policy_type = AutoFailoverPolicyType.min_available;
    policyData.auto_failover_policy.parameters = new HashMap<String, String>();
    policyData.auto_failover_policy.parameters.put("min_limit", "1");
    policyData.auto_failover_policy.parameters.put("usage_threshold", "100");
    policies.setPolicy("otherBrokerPolicy", policyData);
    ObjectMapper jsonMapper = ObjectMapperFactory.create();
    ZooKeeper globalZk = pulsar.getGlobalZkCache().getZooKeeper();
    ZkUtils.createFullPathOptimistic(globalZk, AdminResource.path("clusters", "use", "namespaceIsolationPolicies"), new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    byte[] content = jsonMapper.writeValueAsBytes(policies.getPolicies());
    globalZk.setData(AdminResource.path("clusters", "use", "namespaceIsolationPolicies"), content, -1);
}
Also used : NamespaceIsolationPolicies(com.yahoo.pulsar.common.policies.impl.NamespaceIsolationPolicies) ZooKeeper(org.apache.zookeeper.ZooKeeper) AutoFailoverPolicyData(com.yahoo.pulsar.common.policies.data.AutoFailoverPolicyData) NamespaceIsolationData(com.yahoo.pulsar.common.policies.data.NamespaceIsolationData) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

NamespaceIsolationPolicies (com.yahoo.pulsar.common.policies.impl.NamespaceIsolationPolicies)9 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)4 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)4 RestException (com.yahoo.pulsar.broker.web.RestException)4 ApiOperation (io.swagger.annotations.ApiOperation)4 ApiResponses (io.swagger.annotations.ApiResponses)4 IOException (java.io.IOException)4 Path (javax.ws.rs.Path)4 KeeperException (org.apache.zookeeper.KeeperException)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 AutoFailoverPolicyData (com.yahoo.pulsar.common.policies.data.AutoFailoverPolicyData)2 NamespaceIsolationData (com.yahoo.pulsar.common.policies.data.NamespaceIsolationData)2 DELETE (javax.ws.rs.DELETE)2 ZooKeeper (org.apache.zookeeper.ZooKeeper)2 NamespaceIsolationPolicy (com.yahoo.pulsar.common.policies.NamespaceIsolationPolicy)1 NamespaceOwnershipStatus (com.yahoo.pulsar.common.policies.data.NamespaceOwnershipStatus)1 HashMap (java.util.HashMap)1 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1