Search in sources :

Example 91 with RestException

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

the class BrokersBase method updateDynamicConfigurationOnZk.

/**
 * if {@link ServiceConfiguration}-field is allowed to be modified dynamically, update configuration-map into zk, so
 * all other brokers get the watch and can see the change and take appropriate action on the change.
 *
 * @param configName
 *            : configuration key
 * @param configValue
 *            : configuration value
 */
private synchronized void updateDynamicConfigurationOnZk(String configName, String configValue) {
    try {
        if (!BrokerService.validateDynamicConfiguration(configName, configValue)) {
            throw new RestException(Status.PRECONDITION_FAILED, " Invalid dynamic-config value");
        }
        if (BrokerService.isDynamicConfiguration(configName)) {
            ZooKeeperDataCache<Map<String, String>> dynamicConfigurationCache = pulsar().getBrokerService().getDynamicConfigurationCache();
            Map<String, String> configurationMap = dynamicConfigurationCache.get(BROKER_SERVICE_CONFIGURATION_PATH).orElse(null);
            if (configurationMap != null) {
                configurationMap.put(configName, configValue);
                byte[] content = ObjectMapperFactory.getThreadLocal().writeValueAsBytes(configurationMap);
                dynamicConfigurationCache.invalidate(BROKER_SERVICE_CONFIGURATION_PATH);
                serviceConfigZkVersion = localZk().setData(BROKER_SERVICE_CONFIGURATION_PATH, content, serviceConfigZkVersion).getVersion();
            } else {
                configurationMap = Maps.newHashMap();
                configurationMap.put(configName, configValue);
                byte[] content = ObjectMapperFactory.getThreadLocal().writeValueAsBytes(configurationMap);
                ZkUtils.createFullPathOptimistic(localZk(), BROKER_SERVICE_CONFIGURATION_PATH, content, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            }
            LOG.info("[{}] Updated Service configuration {}/{}", clientAppId(), configName, configValue);
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("[{}] Can't update non-dynamic configuration {}/{}", clientAppId(), configName, configValue);
            }
            throw new RestException(Status.PRECONDITION_FAILED, " Can't update non-dynamic configuration");
        }
    } catch (RestException re) {
        throw re;
    } catch (Exception ie) {
        LOG.error("[{}] Failed to update configuration {}/{}, {}", clientAppId(), configName, configValue, ie.getMessage(), ie);
        throw new RestException(ie);
    }
}
Also used : RestException(org.apache.pulsar.broker.web.RestException) Map(java.util.Map) RestException(org.apache.pulsar.broker.web.RestException)

Example 92 with RestException

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

the class BrokersBase method getOwnedNamespaes.

@GET
@Path("/{cluster}/{broker}/ownedNamespaces")
@ApiOperation(value = "Get the list of namespaces served by the specific broker", response = NamespaceOwnershipStatus.class, responseContainer = "Map")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Cluster doesn't exist") })
public Map<String, NamespaceOwnershipStatus> getOwnedNamespaes(@PathParam("cluster") String cluster, @PathParam("broker") String broker) throws Exception {
    validateSuperUserAccess();
    validateClusterOwnership(cluster);
    validateBrokerName(broker);
    try {
        // now we validated that this is the broker specified in the request
        return pulsar().getNamespaceService().getOwnedNameSpacesStatus();
    } catch (Exception e) {
        LOG.error("[{}] Failed to get the namespace ownership status. cluster={}, broker={}", clientAppId(), cluster, broker);
        throw new RestException(e);
    }
}
Also used : RestException(org.apache.pulsar.broker.web.RestException) RestException(org.apache.pulsar.broker.web.RestException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 93 with RestException

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

the class PropertiesBase method createProperty.

@PUT
@Path("/{property}")
@ApiOperation(value = "Create a new property.", notes = "This operation requires Pulsar super-user privileges.")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 409, message = "Property already exist"), @ApiResponse(code = 412, message = "Property name is not valid") })
public void createProperty(@PathParam("property") String property, PropertyAdmin config) {
    validateSuperUserAccess();
    validatePoliciesReadOnlyAccess();
    try {
        NamedEntity.checkName(property);
        zkCreate(path(POLICIES, property), jsonMapper().writeValueAsBytes(config));
        log.info("[{}] Created property {}", clientAppId(), property);
    } catch (KeeperException.NodeExistsException e) {
        log.warn("[{}] Failed to create already existing property {}", clientAppId(), property);
        throw new RestException(Status.CONFLICT, "Property already exist");
    } catch (IllegalArgumentException e) {
        log.warn("[{}] Failed to create property with invalid name {}", clientAppId(), property, e);
        throw new RestException(Status.PRECONDITION_FAILED, "Property name is not valid");
    } catch (Exception e) {
        log.error("[{}] Failed to create property {}", clientAppId(), property, e);
        throw new RestException(e);
    }
}
Also used : RestException(org.apache.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) KeeperException(org.apache.zookeeper.KeeperException) RestException(org.apache.pulsar.broker.web.RestException) Path(javax.ws.rs.Path) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Example 94 with RestException

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

the class PropertiesBase method deleteProperty.

@DELETE
@Path("/{property}")
@ApiOperation(value = "elete a property and all namespaces and topics under it.")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"), @ApiResponse(code = 404, message = "Property does not exist"), @ApiResponse(code = 409, message = "The property still has active namespaces") })
public void deleteProperty(@PathParam("property") String property) {
    validateSuperUserAccess();
    validatePoliciesReadOnlyAccess();
    boolean isPropertyEmpty = false;
    try {
        isPropertyEmpty = getListOfNamespaces(property).isEmpty();
    } catch (KeeperException.NoNodeException e) {
        log.warn("[{}] Failed to delete property {}: does not exist", clientAppId(), property);
        throw new RestException(Status.NOT_FOUND, "The property does not exist");
    } catch (Exception e) {
        log.error("[{}] Failed to get property status {}", clientAppId(), property, e);
        throw new RestException(e);
    }
    if (!isPropertyEmpty) {
        log.warn("[{}] Failed to delete property {}: not empty", clientAppId(), property);
        throw new RestException(Status.CONFLICT, "The property still has active namespaces");
    }
    try {
        // First try to delete every cluster z-node
        for (String cluster : globalZk().getChildren(path(POLICIES, property), false)) {
            globalZk().delete(path(POLICIES, property, cluster), -1);
        }
        globalZk().delete(path(POLICIES, property), -1);
        log.info("[{}] Deleted property {}", clientAppId(), property);
    } catch (Exception e) {
        log.error("[{}] Failed to delete property {}", clientAppId(), property, e);
        throw new RestException(e);
    }
}
Also used : RestException(org.apache.pulsar.broker.web.RestException) KeeperException(org.apache.zookeeper.KeeperException) KeeperException(org.apache.zookeeper.KeeperException) RestException(org.apache.pulsar.broker.web.RestException) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 95 with RestException

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

the class ResourceQuotasBase method internalGetNamespaceBundleResourceQuota.

@SuppressWarnings("deprecation")
protected ResourceQuota internalGetNamespaceBundleResourceQuota(String bundleRange) {
    validateSuperUserAccess();
    Policies policies = getNamespacePolicies(namespaceName);
    if (!namespaceName.isGlobal()) {
        validateClusterOwnership(namespaceName.getCluster());
        validateClusterForProperty(namespaceName.getProperty(), namespaceName.getCluster());
    }
    NamespaceBundle nsBundle = validateNamespaceBundleRange(namespaceName, policies.bundles, bundleRange);
    try {
        return pulsar().getLocalZkCacheService().getResourceQuotaCache().getQuota(nsBundle);
    } catch (Exception e) {
        log.error("[{}] Failed to get resource quota for namespace bundle {}", clientAppId(), nsBundle.toString());
        throw new RestException(e);
    }
}
Also used : NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) 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)

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