Technoboy- commented on code in PR #15518:
URL: https://github.com/apache/pulsar/pull/15518#discussion_r870486744
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/impl/NamespacesBase.java:
##########
@@ -103,60 +103,55 @@
import org.apache.pulsar.common.util.Codec;
import org.apache.pulsar.common.util.FutureUtil;
import org.apache.pulsar.metadata.api.MetadataStoreException;
-import
org.apache.pulsar.metadata.api.MetadataStoreException.AlreadyExistsException;
import
org.apache.pulsar.metadata.api.MetadataStoreException.BadVersionException;
import org.apache.pulsar.metadata.api.MetadataStoreException.NotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class NamespacesBase extends AdminResource {
- protected List<String> internalGetTenantNamespaces(String tenant) {
- checkNotNull(tenant, "Tenant should not be null");
+ protected CompletableFuture<List<String>>
internalGetTenantNamespaces(String tenant) {
+ if (tenant == null) {
+ return FutureUtil.failedFuture(new
RestException(Status.BAD_REQUEST, "Tenant should not be null"));
+ }
try {
NamedEntity.checkName(tenant);
} catch (IllegalArgumentException e) {
log.warn("[{}] Tenant name is invalid {}", clientAppId(), tenant,
e);
- throw new RestException(Status.PRECONDITION_FAILED, "Tenant name
is not valid");
- }
- validateTenantOperation(tenant, TenantOperation.LIST_NAMESPACES);
-
- try {
- if (!tenantResources().tenantExists(tenant)) {
- throw new RestException(Status.NOT_FOUND, "Tenant not found");
- }
-
- return tenantResources().getListOfNamespaces(tenant);
- } catch (Exception e) {
- log.error("[{}] Failed to get namespaces list: {}", clientAppId(),
e);
- throw new RestException(e);
+ return FutureUtil.failedFuture(new
RestException(Status.PRECONDITION_FAILED, "Tenant name is not valid"));
}
+ return validateTenantOperationAsync(tenant,
TenantOperation.LIST_NAMESPACES)
+ .thenCompose(__ -> tenantResources().tenantExistsAsync(tenant))
+ .thenCompose(existed -> {
+ if (!existed) {
+ throw new RestException(Status.NOT_FOUND, "Tenant not
found");
+ }
+ return tenantResources().getListOfNamespacesAsync(tenant);
+ });
}
- 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);
- }
+ protected CompletableFuture<Void> internalCreateNamespace(Policies
policies) {
+ return validateTenantOperationAsync(namespaceName.getTenant(),
TenantOperation.CREATE_NAMESPACE)
+ .thenCompose(__ -> validatePoliciesReadOnlyAccessAsync())
+ .thenAccept(__ -> validatePolicies(namespaceName, policies))
+ .thenCompose(__ -> {
+ CompletableFuture<Void> ret =
CompletableFuture.completedFuture(null);
+ int maxNamespacesPerTenant =
pulsar().getConfiguration().getMaxNamespacesPerTenant();
+ // no distributed locks are added here.In a concurrent
scenario, the threshold will be exceeded.
+ if (maxNamespacesPerTenant > 0) {
+ ret =
tenantResources().getListOfNamespacesAsync(namespaceName.getTenant())
+ .thenAccept(namespaces -> {
+ if (namespaces != null &&
namespaces.size() > maxNamespacesPerTenant) {
+ throw new
RestException(Status.PRECONDITION_FAILED,
+ "Exceed the maximum number of
namespace in tenant :"
+ +
namespaceName.getTenant());
+ }
+ });
+ }
+ return ret;
+ })
+ .thenCompose(__ ->
namespaceResources().createPoliciesAsync(namespaceName, policies))
+ .thenAccept(__ -> log.info("[{}] Created namespace {}",
clientAppId(), namespaceName));
Review Comment:
It's checked in the controller.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]