Search in sources :

Example 61 with RestException

use of org.apache.pulsar.broker.web.RestException in project incubator-pulsar by apache.

the class NamespacesBase method internalClearNamespaceBacklogForSubscription.

protected void internalClearNamespaceBacklogForSubscription(String subscription, boolean authoritative) {
    validateAdminAccessOnProperty(namespaceName.getProperty());
    try {
        NamespaceBundles bundles = pulsar().getNamespaceService().getNamespaceBundleFactory().getBundles(namespaceName);
        Exception exception = null;
        for (NamespaceBundle nsBundle : bundles.getBundles()) {
            try {
                // clear
                if (pulsar().getNamespaceService().getOwner(nsBundle).isPresent()) {
                    // TODO: make this admin call asynchronous
                    pulsar().getAdminClient().namespaces().clearNamespaceBundleBacklogForSubscription(namespaceName.toString(), nsBundle.getBundleRange(), subscription);
                }
            } catch (Exception e) {
                if (exception == null) {
                    exception = e;
                }
            }
        }
        if (exception != null) {
            if (exception instanceof PulsarAdminException) {
                throw new RestException((PulsarAdminException) exception);
            } else {
                throw new RestException(exception.getCause());
            }
        }
    } catch (WebApplicationException wae) {
        throw wae;
    } catch (Exception e) {
        throw new RestException(e);
    }
    log.info("[{}] Successfully cleared backlog for subscription {} on all the bundles for namespace {}", clientAppId(), subscription, namespaceName);
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) WebApplicationException(javax.ws.rs.WebApplicationException) NamespaceBundles(org.apache.pulsar.common.naming.NamespaceBundles) RestException(org.apache.pulsar.broker.web.RestException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) RestException(org.apache.pulsar.broker.web.RestException) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) WebApplicationException(javax.ws.rs.WebApplicationException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) ExecutionException(java.util.concurrent.ExecutionException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException)

Example 62 with RestException

use of org.apache.pulsar.broker.web.RestException in project incubator-pulsar by apache.

the class NamespacesBase method internalUnloadNamespaceBundle.

@SuppressWarnings("deprecation")
public void internalUnloadNamespaceBundle(String bundleRange, boolean authoritative) {
    log.info("[{}] Unloading namespace bundle {}/{}", clientAppId(), namespaceName, bundleRange);
    validateSuperUserAccess();
    Policies policies = getNamespacePolicies(namespaceName);
    if (namespaceName.isGlobal()) {
        // check cluster ownership for a given global namespace: redirect if peer-cluster owns it
        validateGlobalNamespaceOwnership(namespaceName);
    } else {
        validateClusterOwnership(namespaceName.getCluster());
        validateClusterForProperty(namespaceName.getProperty(), namespaceName.getCluster());
    }
    validatePoliciesReadOnlyAccess();
    if (!isBundleOwnedByAnyBroker(namespaceName, policies.bundles, bundleRange)) {
        log.info("[{}] Namespace bundle is not owned by any broker {}/{}", clientAppId(), namespaceName, bundleRange);
        return;
    }
    NamespaceBundle nsBundle = validateNamespaceBundleOwnership(namespaceName, policies.bundles, bundleRange, authoritative, true);
    try {
        pulsar().getNamespaceService().unloadNamespaceBundle(nsBundle);
        log.info("[{}] Successfully unloaded namespace bundle {}", clientAppId(), nsBundle.toString());
    } catch (Exception e) {
        log.error("[{}] Failed to unload namespace bundle {}/{}", clientAppId(), namespaceName, bundleRange, e);
        throw new RestException(e);
    }
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) PersistencePolicies(org.apache.pulsar.common.policies.data.PersistencePolicies) RetentionPolicies(org.apache.pulsar.common.policies.data.RetentionPolicies) Policies(org.apache.pulsar.common.policies.data.Policies) RestException(org.apache.pulsar.broker.web.RestException) RestException(org.apache.pulsar.broker.web.RestException) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) WebApplicationException(javax.ws.rs.WebApplicationException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) ExecutionException(java.util.concurrent.ExecutionException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException)

Example 63 with RestException

use of org.apache.pulsar.broker.web.RestException in project incubator-pulsar by apache.

the class NamespacesBase method internalSetBacklogQuota.

protected void internalSetBacklogQuota(BacklogQuotaType backlogQuotaType, BacklogQuota backlogQuota) {
    validateAdminAccessOnProperty(namespaceName.getProperty());
    validatePoliciesReadOnlyAccess();
    if (backlogQuotaType == null) {
        backlogQuotaType = BacklogQuotaType.destination_storage;
    }
    try {
        Stat nodeStat = new Stat();
        final String path = path(POLICIES, namespaceName.toString());
        byte[] content = globalZk().getData(path, null, nodeStat);
        Policies policies = jsonMapper().readValue(content, Policies.class);
        RetentionPolicies r = policies.retention_policies;
        if (r != null) {
            Policies p = new Policies();
            p.backlog_quota_map.put(backlogQuotaType, backlogQuota);
            if (!checkQuotas(p, r)) {
                log.warn("[{}] Failed to update backlog configuration for namespace {}: conflicts with retention quota", clientAppId(), namespaceName);
                throw new RestException(Status.PRECONDITION_FAILED, "Backlog Quota exceeds configured retention quota for namespace. Please increase retention quota and retry");
            }
        }
        policies.backlog_quota_map.put(backlogQuotaType, backlogQuota);
        globalZk().setData(path, jsonMapper().writeValueAsBytes(policies), nodeStat.getVersion());
        policiesCache().invalidate(path(POLICIES, namespaceName.toString()));
        log.info("[{}] Successfully updated backlog quota map: namespace={}, map={}", clientAppId(), namespaceName, jsonMapper().writeValueAsString(policies.backlog_quota_map));
    } catch (KeeperException.NoNodeException e) {
        log.warn("[{}] Failed to update backlog quota map for namespace {}: does not exist", clientAppId(), namespaceName);
        throw new RestException(Status.NOT_FOUND, "Namespace does not exist");
    } catch (KeeperException.BadVersionException e) {
        log.warn("[{}] Failed to update backlog quota map for namespace {}: concurrent modification", clientAppId(), namespaceName);
        throw new RestException(Status.CONFLICT, "Concurrent modification");
    } catch (RestException pfe) {
        throw pfe;
    } catch (Exception e) {
        log.error("[{}] Failed to update backlog quota map for namespace {}", clientAppId(), namespaceName, e);
        throw new RestException(e);
    }
}
Also used : RetentionPolicies(org.apache.pulsar.common.policies.data.RetentionPolicies) Stat(org.apache.zookeeper.data.Stat) PersistencePolicies(org.apache.pulsar.common.policies.data.PersistencePolicies) RetentionPolicies(org.apache.pulsar.common.policies.data.RetentionPolicies) Policies(org.apache.pulsar.common.policies.data.Policies) RestException(org.apache.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) RestException(org.apache.pulsar.broker.web.RestException) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) WebApplicationException(javax.ws.rs.WebApplicationException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) ExecutionException(java.util.concurrent.ExecutionException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException)

Example 64 with RestException

use of org.apache.pulsar.broker.web.RestException in project incubator-pulsar by apache.

the class NamespacesBase method validatePeerClusterConflict.

/**
 * It validates that peer-clusters can't coexist in replication-clusters
 *
 * @param clusterName:
 *            given cluster whose peer-clusters can't be present into replication-cluster list
 * @param clusters:
 *            replication-cluster list
 */
private void validatePeerClusterConflict(String clusterName, Set<String> replicationClusters) {
    try {
        ClusterData clusterData = clustersCache().get(path("clusters", clusterName)).orElseThrow(() -> new RestException(Status.PRECONDITION_FAILED, "Invalid replication cluster " + clusterName));
        Set<String> peerClusters = clusterData.getPeerClusterNames();
        if (peerClusters != null && !peerClusters.isEmpty()) {
            SetView<String> conflictPeerClusters = Sets.intersection(peerClusters, replicationClusters);
            if (!conflictPeerClusters.isEmpty()) {
                log.warn("[{}] {}'s peer cluster can't be part of replication clusters {}", clientAppId(), clusterName, conflictPeerClusters);
                throw new RestException(Status.CONFLICT, String.format("%s's peer-clusters %s can't be part of replication-clusters %s", clusterName, conflictPeerClusters, replicationClusters));
            }
        }
    } catch (RestException re) {
        throw re;
    } catch (Exception e) {
        log.warn("[{}] Failed to get cluster-data for {}", clientAppId(), clusterName, e);
    }
}
Also used : ClusterData(org.apache.pulsar.common.policies.data.ClusterData) RestException(org.apache.pulsar.broker.web.RestException) RestException(org.apache.pulsar.broker.web.RestException) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) WebApplicationException(javax.ws.rs.WebApplicationException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) ExecutionException(java.util.concurrent.ExecutionException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException)

Example 65 with RestException

use of org.apache.pulsar.broker.web.RestException in project incubator-pulsar by apache.

the class NamespacesBase method internalSetMaxConsumersPerSubscription.

protected void internalSetMaxConsumersPerSubscription(int maxConsumersPerSubscription) {
    validateSuperUserAccess();
    validatePoliciesReadOnlyAccess();
    try {
        Stat nodeStat = new Stat();
        final String path = path(POLICIES, namespaceName.toString());
        byte[] content = globalZk().getData(path, null, nodeStat);
        Policies policies = jsonMapper().readValue(content, Policies.class);
        if (maxConsumersPerSubscription < 0) {
            throw new RestException(Status.PRECONDITION_FAILED, "maxConsumersPerSubscription must be 0 or more");
        }
        policies.max_consumers_per_subscription = maxConsumersPerSubscription;
        globalZk().setData(path, jsonMapper().writeValueAsBytes(policies), nodeStat.getVersion());
        policiesCache().invalidate(path(POLICIES, namespaceName.toString()));
        log.info("[{}] Successfully updated maxConsumersPerSubscription configuration: namespace={}, value={}", clientAppId(), namespaceName, policies.max_consumers_per_subscription);
    } catch (KeeperException.NoNodeException e) {
        log.warn("[{}] Failed to update maxConsumersPerSubscription configuration for namespace {}: does not exist", clientAppId(), namespaceName);
        throw new RestException(Status.NOT_FOUND, "Namespace does not exist");
    } catch (KeeperException.BadVersionException e) {
        log.warn("[{}] Failed to update maxConsumersPerSubscription configuration for namespace {}: concurrent modification", clientAppId(), namespaceName);
        throw new RestException(Status.CONFLICT, "Concurrent modification");
    } catch (RestException pfe) {
        throw pfe;
    } catch (Exception e) {
        log.error("[{}] Failed to update maxConsumersPerSubscription configuration for namespace {}", clientAppId(), namespaceName, e);
        throw new RestException(e);
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) PersistencePolicies(org.apache.pulsar.common.policies.data.PersistencePolicies) RetentionPolicies(org.apache.pulsar.common.policies.data.RetentionPolicies) Policies(org.apache.pulsar.common.policies.data.Policies) RestException(org.apache.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) RestException(org.apache.pulsar.broker.web.RestException) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) WebApplicationException(javax.ws.rs.WebApplicationException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) ExecutionException(java.util.concurrent.ExecutionException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException)

Aggregations

RestException (org.apache.pulsar.broker.web.RestException)112 KeeperException (org.apache.zookeeper.KeeperException)81 WebApplicationException (javax.ws.rs.WebApplicationException)55 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)55 ExecutionException (java.util.concurrent.ExecutionException)53 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)51 SubscriptionBusyException (org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)49 IOException (java.io.IOException)36 Policies (org.apache.pulsar.common.policies.data.Policies)33 ApiResponses (io.swagger.annotations.ApiResponses)30 Path (javax.ws.rs.Path)29 ApiOperation (io.swagger.annotations.ApiOperation)27 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)25 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)24 NotAllowedException (org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException)24 TopicBusyException (org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException)24 NotFoundException (org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException)24 PreconditionFailedException (org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException)24 RetentionPolicies (org.apache.pulsar.common.policies.data.RetentionPolicies)24 PersistencePolicies (org.apache.pulsar.common.policies.data.PersistencePolicies)22