Search in sources :

Example 46 with Policies

use of com.yahoo.pulsar.common.policies.data.Policies in project pulsar by yahoo.

the class Namespaces method setNamespaceMessageTTL.

@POST
@Path("/{property}/{cluster}/{namespace}/messageTTL")
@ApiOperation(value = "Set message TTL in seconds for namespace")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Property or cluster or namespace doesn't exist"), @ApiResponse(code = 412, message = "Invalid TTL") })
public void setNamespaceMessageTTL(@PathParam("property") String property, @PathParam("cluster") String cluster, @PathParam("namespace") String namespace, int messageTTL) {
    validateAdminAccessOnProperty(property);
    validatePoliciesReadOnlyAccess();
    if (messageTTL < 0) {
        throw new RestException(Status.PRECONDITION_FAILED, "Invalid value for message TTL");
    }
    NamespaceName nsName = new NamespaceName(property, cluster, namespace);
    Entry<Policies, Stat> policiesNode = null;
    try {
        // Force to read the data s.t. the watch to the cache content is setup.
        policiesNode = policiesCache().getWithStat(path("policies", property, cluster, namespace)).orElseThrow(() -> new RestException(Status.NOT_FOUND, "Namespace " + nsName + " does not exist"));
        policiesNode.getKey().message_ttl_in_seconds = messageTTL;
        // Write back the new policies into zookeeper
        globalZk().setData(path("policies", property, cluster, namespace), jsonMapper().writeValueAsBytes(policiesNode.getKey()), policiesNode.getValue().getVersion());
        policiesCache().invalidate(path("policies", property, cluster, namespace));
        log.info("[{}] Successfully updated the message TTL on namespace {}/{}/{}", clientAppId(), property, cluster, namespace);
    } catch (KeeperException.NoNodeException e) {
        log.warn("[{}] Failed to update the message TTL for namespace {}/{}/{}: does not exist", clientAppId(), property, cluster, namespace);
        throw new RestException(Status.NOT_FOUND, "Namespace does not exist");
    } catch (KeeperException.BadVersionException e) {
        log.warn("[{}] Failed to update the message TTL on namespace {}/{}/{} expected policy node version={} : concurrent modification", clientAppId(), property, cluster, namespace, policiesNode.getValue().getVersion());
        throw new RestException(Status.CONFLICT, "Concurrent modification");
    } catch (Exception e) {
        log.error("[{}] Failed to update the message TTL on namespace {}/{}/{}", clientAppId(), property, cluster, namespace, e);
        throw new RestException(e);
    }
}
Also used : NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) Policies(com.yahoo.pulsar.common.policies.data.Policies) PersistencePolicies(com.yahoo.pulsar.common.policies.data.PersistencePolicies) RetentionPolicies(com.yahoo.pulsar.common.policies.data.RetentionPolicies) Stat(org.apache.zookeeper.data.Stat) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) RestException(com.yahoo.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) RestException(com.yahoo.pulsar.broker.web.RestException) WebApplicationException(javax.ws.rs.WebApplicationException) SubscriptionBusyException(com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 47 with Policies

use of com.yahoo.pulsar.common.policies.data.Policies in project pulsar by yahoo.

the class Namespaces method clearNamespaceBundleBacklog.

@POST
@Path("/{property}/{cluster}/{namespace}/{bundle}/clearBacklog")
@ApiOperation(value = "Clear backlog for all destinations on a namespace bundle.")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Namespace does not exist") })
public void clearNamespaceBundleBacklog(@PathParam("property") String property, @PathParam("cluster") String cluster, @PathParam("namespace") String namespace, @PathParam("bundle") String bundleRange, @QueryParam("authoritative") @DefaultValue("false") boolean authoritative) {
    validateAdminAccessOnProperty(property);
    Policies policies = getNamespacePolicies(property, cluster, namespace);
    if (!cluster.equals(Namespaces.GLOBAL_CLUSTER)) {
        validateClusterOwnership(cluster);
        validateClusterForProperty(property, cluster);
    }
    NamespaceName nsName = new NamespaceName(property, cluster, namespace);
    validateNamespaceBundleOwnership(nsName, policies.bundles, bundleRange, authoritative, true);
    clearBacklog(nsName, bundleRange, null);
    log.info("[{}] Successfully cleared backlog on namespace bundle {}/{}", clientAppId(), nsName.toString(), bundleRange);
}
Also used : NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) Policies(com.yahoo.pulsar.common.policies.data.Policies) PersistencePolicies(com.yahoo.pulsar.common.policies.data.PersistencePolicies) RetentionPolicies(com.yahoo.pulsar.common.policies.data.RetentionPolicies) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 48 with Policies

use of com.yahoo.pulsar.common.policies.data.Policies in project pulsar by yahoo.

the class Namespaces method createNamespace.

@PUT
@Path("/{property}/{cluster}/{namespace}")
@ApiOperation(value = "Creates a new empty namespace with no policies attached.")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Property or cluster or namespace doesn't exist"), @ApiResponse(code = 409, message = "Namespace already exists"), @ApiResponse(code = 412, message = "Namespace name is not valid") })
public void createNamespace(@PathParam("property") String property, @PathParam("cluster") String cluster, @PathParam("namespace") String namespace, BundlesData initialBundles) {
    validateAdminAccessOnProperty(property);
    validatePoliciesReadOnlyAccess();
    // check is made at the time of setting replication.
    if (!cluster.equals(GLOBAL_CLUSTER)) {
        validateClusterForProperty(property, cluster);
    }
    if (!clusters().contains(cluster)) {
        log.warn("[{}] Failed to create namespace. Cluster {} does not exist", clientAppId(), cluster);
        throw new RestException(Status.NOT_FOUND, "Cluster does not exist");
    }
    try {
        checkNotNull(propertiesCache().get(path("policies", property)));
    } catch (NoNodeException nne) {
        log.warn("[{}] Failed to create namespace. Property {} does not exist", clientAppId(), property);
        throw new RestException(Status.NOT_FOUND, "Property does not exist");
    } catch (RestException e) {
        throw e;
    } catch (Exception e) {
        throw new RestException(e);
    }
    try {
        NamedEntity.checkName(namespace);
        policiesCache().invalidate(path("policies", property, cluster, namespace));
        Policies policies = new Policies();
        if (initialBundles != null && initialBundles.getNumBundles() > 0) {
            if (initialBundles.getBoundaries() == null || initialBundles.getBoundaries().size() == 0) {
                policies.bundles = getBundles(initialBundles.getNumBundles());
            } else {
                policies.bundles = validateBundlesData(initialBundles);
            }
        }
        zkCreateOptimistic(path("policies", property, cluster, namespace), jsonMapper().writeValueAsBytes(policies));
        log.info("[{}] Created namespace {}/{}/{}", clientAppId(), property, cluster, namespace);
    } catch (KeeperException.NodeExistsException e) {
        log.warn("[{}] Failed to create namespace {}/{}/{} - already exists", clientAppId(), property, cluster, namespace);
        throw new RestException(Status.CONFLICT, "Namespace already exists");
    } catch (IllegalArgumentException e) {
        log.warn("[{}] Failed to create namespace with invalid name {}", clientAppId(), property, e);
        throw new RestException(Status.PRECONDITION_FAILED, "Namespace name is not valid");
    } catch (Exception e) {
        log.error("[{}] Failed to create namespace {}/{}/{}", clientAppId(), property, cluster, namespace, e);
        throw new RestException(e);
    }
}
Also used : Policies(com.yahoo.pulsar.common.policies.data.Policies) PersistencePolicies(com.yahoo.pulsar.common.policies.data.PersistencePolicies) RetentionPolicies(com.yahoo.pulsar.common.policies.data.RetentionPolicies) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) RestException(com.yahoo.pulsar.broker.web.RestException) RestException(com.yahoo.pulsar.broker.web.RestException) WebApplicationException(javax.ws.rs.WebApplicationException) SubscriptionBusyException(com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) KeeperException(org.apache.zookeeper.KeeperException) Path(javax.ws.rs.Path) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Example 49 with Policies

use of com.yahoo.pulsar.common.policies.data.Policies in project pulsar by yahoo.

the class Namespaces method clearNamespaceBundleBacklogForSubscription.

@POST
@Path("/{property}/{cluster}/{namespace}/{bundle}/clearBacklog/{subscription}")
@ApiOperation(value = "Clear backlog for a given subscription on all destinations on a namespace bundle.")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Namespace does not exist") })
public void clearNamespaceBundleBacklogForSubscription(@PathParam("property") String property, @PathParam("cluster") String cluster, @PathParam("namespace") String namespace, @PathParam("subscription") String subscription, @PathParam("bundle") String bundleRange, @QueryParam("authoritative") @DefaultValue("false") boolean authoritative) {
    validateAdminAccessOnProperty(property);
    Policies policies = getNamespacePolicies(property, cluster, namespace);
    if (!cluster.equals(Namespaces.GLOBAL_CLUSTER)) {
        validateClusterOwnership(cluster);
        validateClusterForProperty(property, cluster);
    }
    NamespaceName nsName = new NamespaceName(property, cluster, namespace);
    validateNamespaceBundleOwnership(nsName, policies.bundles, bundleRange, authoritative, true);
    clearBacklog(nsName, bundleRange, subscription);
    log.info("[{}] Successfully cleared backlog for subscription {} on namespace bundle {}/{}", clientAppId(), subscription, nsName.toString(), bundleRange);
}
Also used : NamespaceName(com.yahoo.pulsar.common.naming.NamespaceName) Policies(com.yahoo.pulsar.common.policies.data.Policies) PersistencePolicies(com.yahoo.pulsar.common.policies.data.PersistencePolicies) RetentionPolicies(com.yahoo.pulsar.common.policies.data.RetentionPolicies) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

Policies (com.yahoo.pulsar.common.policies.data.Policies)49 ApiOperation (io.swagger.annotations.ApiOperation)30 ApiResponses (io.swagger.annotations.ApiResponses)30 Path (javax.ws.rs.Path)30 RetentionPolicies (com.yahoo.pulsar.common.policies.data.RetentionPolicies)27 PersistencePolicies (com.yahoo.pulsar.common.policies.data.PersistencePolicies)26 KeeperException (org.apache.zookeeper.KeeperException)25 RestException (com.yahoo.pulsar.broker.web.RestException)24 SubscriptionBusyException (com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)19 NamespaceName (com.yahoo.pulsar.common.naming.NamespaceName)18 WebApplicationException (javax.ws.rs.WebApplicationException)17 PulsarServerException (com.yahoo.pulsar.broker.PulsarServerException)16 PulsarAdminException (com.yahoo.pulsar.client.admin.PulsarAdminException)15 NamespaceBundle (com.yahoo.pulsar.common.naming.NamespaceBundle)13 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)13 Stat (org.apache.zookeeper.data.Stat)13 POST (javax.ws.rs.POST)11 GET (javax.ws.rs.GET)9 Test (org.testng.annotations.Test)9 DestinationName (com.yahoo.pulsar.common.naming.DestinationName)8