Search in sources :

Example 1 with AlreadyExistsException

use of org.apache.pulsar.metadata.api.MetadataStoreException.AlreadyExistsException in project pulsar by apache.

the class NamespacesBase method internalCreateNamespace.

protected void internalCreateNamespace(Policies policies) {
    validateTenantOperation(namespaceName.getTenant(), TenantOperation.CREATE_NAMESPACE);
    validatePoliciesReadOnlyAccess();
    validatePolicies(namespaceName, policies);
    try {
        int maxNamespacesPerTenant = pulsar().getConfiguration().getMaxNamespacesPerTenant();
        // no distributed locks are added here.In a concurrent scenario, the threshold will be exceeded.
        if (maxNamespacesPerTenant > 0) {
            List<String> namespaces = tenantResources().getListOfNamespaces(namespaceName.getTenant());
            if (namespaces != null && namespaces.size() > maxNamespacesPerTenant) {
                throw new RestException(Status.PRECONDITION_FAILED, "Exceed the maximum number of namespace in tenant :" + namespaceName.getTenant());
            }
        }
        namespaceResources().createPolicies(namespaceName, policies);
        log.info("[{}] Created namespace {}", clientAppId(), namespaceName);
    } catch (AlreadyExistsException e) {
        log.warn("[{}] Failed to create namespace {} - already exists", clientAppId(), namespaceName);
        throw new RestException(Status.CONFLICT, "Namespace already exists");
    } catch (Exception e) {
        log.error("[{}] Failed to create namespace {}", clientAppId(), namespaceName, e);
        throw new RestException(e);
    }
}
Also used : AlreadyExistsException(org.apache.pulsar.metadata.api.MetadataStoreException.AlreadyExistsException) RestException(org.apache.pulsar.broker.web.RestException) RestException(org.apache.pulsar.broker.web.RestException) MetadataStoreException(org.apache.pulsar.metadata.api.MetadataStoreException) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) NotFoundException(org.apache.pulsar.metadata.api.MetadataStoreException.NotFoundException) WebApplicationException(javax.ws.rs.WebApplicationException) AlreadyExistsException(org.apache.pulsar.metadata.api.MetadataStoreException.AlreadyExistsException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) ExecutionException(java.util.concurrent.ExecutionException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) BadVersionException(org.apache.pulsar.metadata.api.MetadataStoreException.BadVersionException)

Example 2 with AlreadyExistsException

use of org.apache.pulsar.metadata.api.MetadataStoreException.AlreadyExistsException in project pulsar by apache.

the class PulsarWorkerService method initInBroker.

@Override
public void initInBroker(ServiceConfiguration brokerConfig, WorkerConfig workerConfig, PulsarResources pulsarResources, InternalConfigurationData internalConf) throws Exception {
    String namespace = workerConfig.getPulsarFunctionsNamespace();
    String[] a = workerConfig.getPulsarFunctionsNamespace().split("/");
    String tenant = a[0];
    String cluster = workerConfig.getPulsarFunctionsCluster();
    // create tenant for function worker service
    try {
        NamedEntity.checkName(tenant);
        pulsarResources.getTenantResources().createTenant(tenant, new TenantInfoImpl(Sets.newHashSet(workerConfig.getSuperUserRoles()), Sets.newHashSet(cluster)));
        LOG.info("Created tenant {} for function worker", tenant);
    } catch (AlreadyExistsException e) {
        LOG.debug("Failed to create already existing property {} for function worker service", cluster, e);
    } catch (IllegalArgumentException e) {
        LOG.error("Failed to create property with invalid name {} for function worker service", cluster, e);
        throw e;
    } catch (Exception e) {
        LOG.error("Failed to create property {} for function worker", cluster, e);
        throw e;
    }
    // create cluster for function worker service
    try {
        NamedEntity.checkName(cluster);
        ClusterDataImpl clusterData = ClusterDataImpl.builder().serviceUrl(workerConfig.getPulsarWebServiceUrl()).brokerServiceUrl(workerConfig.getPulsarServiceUrl()).build();
        pulsarResources.getClusterResources().createCluster(cluster, clusterData);
        LOG.info("Created cluster {} for function worker", cluster);
    } catch (AlreadyExistsException e) {
        LOG.debug("Failed to create already existing cluster {} for function worker service", cluster, e);
    } catch (IllegalArgumentException e) {
        LOG.error("Failed to create cluster with invalid name {} for function worker service", cluster, e);
        throw e;
    } catch (Exception e) {
        LOG.error("Failed to create cluster {} for function worker service", cluster, e);
        throw e;
    }
    // create namespace for function worker service
    try {
        Policies policies = createFunctionsNamespacePolicies(workerConfig.getPulsarFunctionsCluster());
        policies.bundles = getBundles(brokerConfig.getDefaultNumberOfNamespaceBundles());
        pulsarResources.getNamespaceResources().createPolicies(NamespaceName.get(namespace), policies);
        LOG.info("Created namespace {} for function worker service", namespace);
    } catch (AlreadyExistsException e) {
        LOG.debug("Failed to create already existing namespace {} for function worker service", namespace);
    } catch (Exception e) {
        LOG.error("Failed to create namespace {}", namespace, e);
        throw e;
    }
    URI dlogURI;
    try {
        // initializing dlog namespace for function worker
        if (workerConfig.isInitializedDlogMetadata()) {
            String metadataStoreUrl = removeIdentifierFromMetadataURL(internalConf.getMetadataStoreUrl());
            dlogURI = WorkerUtils.newDlogNamespaceURI(metadataStoreUrl);
        } else {
            dlogURI = WorkerUtils.initializeDlogNamespace(internalConf);
        }
    } catch (IOException ioe) {
        LOG.error("Failed to initialize dlog namespace with zookeeper {} at at metadata service uri {} for " + "storing function packages", internalConf.getMetadataStoreUrl(), internalConf.getBookkeeperMetadataServiceUri(), ioe);
        throw ioe;
    }
    init(workerConfig, dlogURI, false);
    LOG.info("Function worker service setup completed");
}
Also used : InactiveTopicPolicies(org.apache.pulsar.common.policies.data.InactiveTopicPolicies) RetentionPolicies(org.apache.pulsar.common.policies.data.RetentionPolicies) Policies(org.apache.pulsar.common.policies.data.Policies) AlreadyExistsException(org.apache.pulsar.metadata.api.MetadataStoreException.AlreadyExistsException) IOException(java.io.IOException) ClusterDataImpl(org.apache.pulsar.common.policies.data.ClusterDataImpl) URI(java.net.URI) TenantInfoImpl(org.apache.pulsar.common.policies.data.TenantInfoImpl) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) AlreadyExistsException(org.apache.pulsar.metadata.api.MetadataStoreException.AlreadyExistsException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException)

Example 3 with AlreadyExistsException

use of org.apache.pulsar.metadata.api.MetadataStoreException.AlreadyExistsException in project pulsar by apache.

the class MetadataCacheImpl method create.

@Override
public CompletableFuture<Void> create(String path, T value) {
    byte[] content;
    try {
        content = serde.serialize(path, value);
    } catch (Throwable t) {
        return FutureUtils.exception(t);
    }
    CompletableFuture<Void> future = new CompletableFuture<>();
    store.put(path, content, Optional.of(-1L)).thenAccept(stat -> {
        // Make sure we have the value cached before the operation is completed
        // In addition to caching the value, we need to add a watch on the path,
        // so when/if it changes on any other node, we are notified and we can
        // update the cache
        objCache.get(path).whenComplete((stat2, ex) -> {
            if (ex == null) {
                future.complete(null);
            } else {
                log.error("Exception while getting path {}", path, ex);
                future.completeExceptionally(ex.getCause());
            }
        });
    }).exceptionally(ex -> {
        if (ex.getCause() instanceof BadVersionException) {
            // Use already exists exception to provide more self-explanatory error message
            future.completeExceptionally(new AlreadyExistsException(ex.getCause()));
        } else {
            future.completeExceptionally(ex.getCause());
        }
        return null;
    });
    return future;
}
Also used : Getter(lombok.Getter) MetadataStore(org.apache.pulsar.metadata.api.MetadataStore) CompletableFuture(java.util.concurrent.CompletableFuture) AbstractMetadataStore(org.apache.pulsar.metadata.impl.AbstractMetadataStore) CacheGetResult(org.apache.pulsar.metadata.api.CacheGetResult) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Notification(org.apache.pulsar.metadata.api.Notification) MetadataCache(org.apache.pulsar.metadata.api.MetadataCache) GetResult(org.apache.pulsar.metadata.api.GetResult) MetadataSerde(org.apache.pulsar.metadata.api.MetadataSerde) JavaType(com.fasterxml.jackson.databind.JavaType) AsyncCacheLoader(com.github.benmanes.caffeine.cache.AsyncCacheLoader) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) Executor(java.util.concurrent.Executor) AlreadyExistsException(org.apache.pulsar.metadata.api.MetadataStoreException.AlreadyExistsException) IOException(java.io.IOException) FutureUtils(org.apache.bookkeeper.common.concurrent.FutureUtils) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ContentDeserializationException(org.apache.pulsar.metadata.api.MetadataStoreException.ContentDeserializationException) AsyncLoadingCache(com.github.benmanes.caffeine.cache.AsyncLoadingCache) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) NotFoundException(org.apache.pulsar.metadata.api.MetadataStoreException.NotFoundException) BadVersionException(org.apache.pulsar.metadata.api.MetadataStoreException.BadVersionException) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) CompletableFuture(java.util.concurrent.CompletableFuture) AlreadyExistsException(org.apache.pulsar.metadata.api.MetadataStoreException.AlreadyExistsException) BadVersionException(org.apache.pulsar.metadata.api.MetadataStoreException.BadVersionException)

Example 4 with AlreadyExistsException

use of org.apache.pulsar.metadata.api.MetadataStoreException.AlreadyExistsException in project pulsar by yahoo.

the class PulsarWorkerService method initInBroker.

@Override
public void initInBroker(ServiceConfiguration brokerConfig, WorkerConfig workerConfig, PulsarResources pulsarResources, InternalConfigurationData internalConf) throws Exception {
    String namespace = workerConfig.getPulsarFunctionsNamespace();
    String[] a = workerConfig.getPulsarFunctionsNamespace().split("/");
    String tenant = a[0];
    String cluster = workerConfig.getPulsarFunctionsCluster();
    // create tenant for function worker service
    try {
        NamedEntity.checkName(tenant);
        pulsarResources.getTenantResources().createTenant(tenant, new TenantInfoImpl(Sets.newHashSet(workerConfig.getSuperUserRoles()), Sets.newHashSet(cluster)));
        LOG.info("Created tenant {} for function worker", tenant);
    } catch (AlreadyExistsException e) {
        LOG.debug("Failed to create already existing property {} for function worker service", cluster, e);
    } catch (IllegalArgumentException e) {
        LOG.error("Failed to create property with invalid name {} for function worker service", cluster, e);
        throw e;
    } catch (Exception e) {
        LOG.error("Failed to create property {} for function worker", cluster, e);
        throw e;
    }
    // create cluster for function worker service
    try {
        NamedEntity.checkName(cluster);
        ClusterDataImpl clusterData = ClusterDataImpl.builder().serviceUrl(workerConfig.getPulsarWebServiceUrl()).brokerServiceUrl(workerConfig.getPulsarServiceUrl()).build();
        pulsarResources.getClusterResources().createCluster(cluster, clusterData);
        LOG.info("Created cluster {} for function worker", cluster);
    } catch (AlreadyExistsException e) {
        LOG.debug("Failed to create already existing cluster {} for function worker service", cluster, e);
    } catch (IllegalArgumentException e) {
        LOG.error("Failed to create cluster with invalid name {} for function worker service", cluster, e);
        throw e;
    } catch (Exception e) {
        LOG.error("Failed to create cluster {} for function worker service", cluster, e);
        throw e;
    }
    // create namespace for function worker service
    try {
        Policies policies = createFunctionsNamespacePolicies(workerConfig.getPulsarFunctionsCluster());
        policies.bundles = getBundles(brokerConfig.getDefaultNumberOfNamespaceBundles());
        pulsarResources.getNamespaceResources().createPolicies(NamespaceName.get(namespace), policies);
        LOG.info("Created namespace {} for function worker service", namespace);
    } catch (AlreadyExistsException e) {
        LOG.debug("Failed to create already existing namespace {} for function worker service", namespace);
    } catch (Exception e) {
        LOG.error("Failed to create namespace {}", namespace, e);
        throw e;
    }
    URI dlogURI = null;
    if (brokerConfig.isMetadataStoreBackedByZookeeper()) {
        try {
            // initializing dlog namespace for function worker
            if (workerConfig.isInitializedDlogMetadata()) {
                String metadataStoreUrl = removeIdentifierFromMetadataURL(internalConf.getMetadataStoreUrl());
                dlogURI = WorkerUtils.newDlogNamespaceURI(metadataStoreUrl);
            } else {
                dlogURI = WorkerUtils.initializeDlogNamespace(internalConf);
            }
        } catch (IOException ioe) {
            LOG.error("Failed to initialize dlog namespace with zookeeper {} at at metadata service uri {} for " + "storing function packages", internalConf.getMetadataStoreUrl(), internalConf.getBookkeeperMetadataServiceUri(), ioe);
            throw ioe;
        }
    }
    init(workerConfig, dlogURI, false);
    LOG.info("Function worker service setup completed");
}
Also used : InactiveTopicPolicies(org.apache.pulsar.common.policies.data.InactiveTopicPolicies) RetentionPolicies(org.apache.pulsar.common.policies.data.RetentionPolicies) Policies(org.apache.pulsar.common.policies.data.Policies) AlreadyExistsException(org.apache.pulsar.metadata.api.MetadataStoreException.AlreadyExistsException) IOException(java.io.IOException) ClusterDataImpl(org.apache.pulsar.common.policies.data.ClusterDataImpl) URI(java.net.URI) TenantInfoImpl(org.apache.pulsar.common.policies.data.TenantInfoImpl) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) AlreadyExistsException(org.apache.pulsar.metadata.api.MetadataStoreException.AlreadyExistsException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) IOException(java.io.IOException)

Example 5 with AlreadyExistsException

use of org.apache.pulsar.metadata.api.MetadataStoreException.AlreadyExistsException in project pulsar by yahoo.

the class MetadataCacheImpl method create.

@Override
public CompletableFuture<Void> create(String path, T value) {
    byte[] content;
    try {
        content = serde.serialize(path, value);
    } catch (Throwable t) {
        return FutureUtils.exception(t);
    }
    CompletableFuture<Void> future = new CompletableFuture<>();
    store.put(path, content, Optional.of(-1L)).thenAccept(stat -> {
        // Make sure we have the value cached before the operation is completed
        // In addition to caching the value, we need to add a watch on the path,
        // so when/if it changes on any other node, we are notified and we can
        // update the cache
        objCache.get(path).whenComplete((stat2, ex) -> {
            if (ex == null) {
                future.complete(null);
            } else {
                log.error("Exception while getting path {}", path, ex);
                future.completeExceptionally(ex.getCause());
            }
        });
    }).exceptionally(ex -> {
        if (ex.getCause() instanceof BadVersionException) {
            // Use already exists exception to provide more self-explanatory error message
            future.completeExceptionally(new AlreadyExistsException(ex.getCause()));
        } else {
            future.completeExceptionally(ex.getCause());
        }
        return null;
    });
    return future;
}
Also used : Getter(lombok.Getter) MetadataStore(org.apache.pulsar.metadata.api.MetadataStore) CompletableFuture(java.util.concurrent.CompletableFuture) AbstractMetadataStore(org.apache.pulsar.metadata.impl.AbstractMetadataStore) CacheGetResult(org.apache.pulsar.metadata.api.CacheGetResult) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Notification(org.apache.pulsar.metadata.api.Notification) MetadataCache(org.apache.pulsar.metadata.api.MetadataCache) GetResult(org.apache.pulsar.metadata.api.GetResult) MetadataSerde(org.apache.pulsar.metadata.api.MetadataSerde) JavaType(com.fasterxml.jackson.databind.JavaType) AsyncCacheLoader(com.github.benmanes.caffeine.cache.AsyncCacheLoader) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) Executor(java.util.concurrent.Executor) AlreadyExistsException(org.apache.pulsar.metadata.api.MetadataStoreException.AlreadyExistsException) IOException(java.io.IOException) FutureUtils(org.apache.bookkeeper.common.concurrent.FutureUtils) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ContentDeserializationException(org.apache.pulsar.metadata.api.MetadataStoreException.ContentDeserializationException) AsyncLoadingCache(com.github.benmanes.caffeine.cache.AsyncLoadingCache) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) NotFoundException(org.apache.pulsar.metadata.api.MetadataStoreException.NotFoundException) BadVersionException(org.apache.pulsar.metadata.api.MetadataStoreException.BadVersionException) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) CompletableFuture(java.util.concurrent.CompletableFuture) AlreadyExistsException(org.apache.pulsar.metadata.api.MetadataStoreException.AlreadyExistsException) BadVersionException(org.apache.pulsar.metadata.api.MetadataStoreException.BadVersionException)

Aggregations

AlreadyExistsException (org.apache.pulsar.metadata.api.MetadataStoreException.AlreadyExistsException)10 BadVersionException (org.apache.pulsar.metadata.api.MetadataStoreException.BadVersionException)7 IOException (java.io.IOException)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)4 NotFoundException (org.apache.pulsar.metadata.api.MetadataStoreException.NotFoundException)4 TypeReference (com.fasterxml.jackson.core.type.TypeReference)3 JavaType (com.fasterxml.jackson.databind.JavaType)3 AsyncCacheLoader (com.github.benmanes.caffeine.cache.AsyncCacheLoader)3 AsyncLoadingCache (com.github.benmanes.caffeine.cache.AsyncLoadingCache)3 Caffeine (com.github.benmanes.caffeine.cache.Caffeine)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 URI (java.net.URI)3 List (java.util.List)3 Optional (java.util.Optional)3 Executor (java.util.concurrent.Executor)3 TimeUnit (java.util.concurrent.TimeUnit)3 Consumer (java.util.function.Consumer)3 Function (java.util.function.Function)3 Supplier (java.util.function.Supplier)3