You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/05/10 05:30:41 UTC
[GitHub] [pulsar] Technoboy- opened a new pull request, #15518: [improve][broker] Make some methods in NamespacesBase async.
Technoboy- opened a new pull request, #15518:
URL: https://github.com/apache/pulsar/pull/15518
### Motivation
See PIP #14365 and change tracker #15043.
Make `NamespacesBase` `getTenantNamespaces / createNamespace / getTopics / getPolicies / getPermissions` methods to pure async.
### Documentation
- [x] `no-need-doc`
(Please explain why)
--
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: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] nodece commented on a diff in pull request #15518: [improve][broker] Make some methods in NamespacesBase async.
Posted by GitBox <gi...@apache.org>.
nodece commented on code in PR #15518:
URL: https://github.com/apache/pulsar/pull/15518#discussion_r870442423
##########
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 was addressed 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: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Technoboy- commented on a diff in pull request #15518: [improve][broker] Make some methods in NamespacesBase async.
Posted by GitBox <gi...@apache.org>.
Technoboy- commented on code in PR #15518:
URL: https://github.com/apache/pulsar/pull/15518#discussion_r872116745
##########
pulsar-broker-common/src/main/java/org/apache/pulsar/broker/resources/TenantResources.java:
##########
@@ -110,6 +111,41 @@ public List<String> getListOfNamespaces(String tenant) throws MetadataStoreExcep
return namespaces;
}
+ public CompletableFuture<List<String>> getListOfNamespacesAsync(String tenant) {
+ // this will return a cluster in v1 and a namespace in v2
+ return getChildrenAsync(joinPath(BASE_POLICIES_PATH, tenant))
+ .thenCompose(clusterOrNamespaces -> clusterOrNamespaces.stream().map(key ->
+ getChildrenAsync(joinPath(BASE_POLICIES_PATH, tenant, key))
+ .thenCompose(children -> {
+ if (children == null || children.isEmpty()) {
+ String namespace = NamespaceName.get(tenant, key).toString();
+ // if the length is 0 then this is probably a leftover cluster from namespace
+ // created with the v1 admin format (prop/cluster/ns) and then deleted, so no
+ // need to add it to the list
+ return getAsync(joinPath(BASE_POLICIES_PATH, namespace))
+ .thenApply(opt -> opt.isPresent() ? Collections.singletonList(namespace)
+ : new ArrayList<String>())
+ .exceptionally(ex -> {
+ Throwable cause = FutureUtil.unwrapCompletionException(ex);
+ if (cause instanceof MetadataStoreException
+ .ContentDeserializationException) {
+ return new ArrayList<>();
+ }
+ throw FutureUtil.wrapToCompletionException(ex);
+ });
+ } else {
+ CompletableFuture<List<String>> ret = new CompletableFuture();
+ ret.complete(children.stream().map(ns -> NamespaceName.get(tenant, key, ns)
+ .toString()).collect(Collectors.toList()));
+ return ret;
+ }
+ })).reduce(CompletableFuture.completedFuture(new ArrayList<>()),
+ (accumulator, n) -> accumulator.thenCompose(namespaces -> n.thenCompose(m -> {
+ namespaces.addAll(m);
+ return CompletableFuture.completedFuture(namespaces);
Review Comment:
fixed
##########
pulsar-broker-common/src/main/java/org/apache/pulsar/broker/resources/TenantResources.java:
##########
@@ -110,6 +111,41 @@ public List<String> getListOfNamespaces(String tenant) throws MetadataStoreExcep
return namespaces;
}
+ public CompletableFuture<List<String>> getListOfNamespacesAsync(String tenant) {
+ // this will return a cluster in v1 and a namespace in v2
+ return getChildrenAsync(joinPath(BASE_POLICIES_PATH, tenant))
+ .thenCompose(clusterOrNamespaces -> clusterOrNamespaces.stream().map(key ->
+ getChildrenAsync(joinPath(BASE_POLICIES_PATH, tenant, key))
+ .thenCompose(children -> {
+ if (children == null || children.isEmpty()) {
+ String namespace = NamespaceName.get(tenant, key).toString();
+ // if the length is 0 then this is probably a leftover cluster from namespace
+ // created with the v1 admin format (prop/cluster/ns) and then deleted, so no
+ // need to add it to the list
+ return getAsync(joinPath(BASE_POLICIES_PATH, namespace))
+ .thenApply(opt -> opt.isPresent() ? Collections.singletonList(namespace)
+ : new ArrayList<String>())
+ .exceptionally(ex -> {
+ Throwable cause = FutureUtil.unwrapCompletionException(ex);
+ if (cause instanceof MetadataStoreException
+ .ContentDeserializationException) {
+ return new ArrayList<>();
+ }
+ throw FutureUtil.wrapToCompletionException(ex);
+ });
+ } else {
+ CompletableFuture<List<String>> ret = new CompletableFuture();
+ ret.complete(children.stream().map(ns -> NamespaceName.get(tenant, key, ns)
+ .toString()).collect(Collectors.toList()));
+ return ret;
+ }
+ })).reduce(CompletableFuture.completedFuture(new ArrayList<>()),
+ (accumulator, n) -> accumulator.thenCompose(namespaces -> n.thenCompose(m -> {
Review Comment:
fixed
--
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: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] mattisonchao commented on a diff in pull request #15518: [improve][broker] Make some methods in NamespacesBase async.
Posted by GitBox <gi...@apache.org>.
mattisonchao commented on code in PR #15518:
URL: https://github.com/apache/pulsar/pull/15518#discussion_r872030642
##########
pulsar-broker-common/src/main/java/org/apache/pulsar/broker/resources/TenantResources.java:
##########
@@ -110,6 +111,41 @@ public List<String> getListOfNamespaces(String tenant) throws MetadataStoreExcep
return namespaces;
}
+ public CompletableFuture<List<String>> getListOfNamespacesAsync(String tenant) {
+ // this will return a cluster in v1 and a namespace in v2
+ return getChildrenAsync(joinPath(BASE_POLICIES_PATH, tenant))
+ .thenCompose(clusterOrNamespaces -> clusterOrNamespaces.stream().map(key ->
+ getChildrenAsync(joinPath(BASE_POLICIES_PATH, tenant, key))
+ .thenCompose(children -> {
+ if (children == null || children.isEmpty()) {
+ String namespace = NamespaceName.get(tenant, key).toString();
+ // if the length is 0 then this is probably a leftover cluster from namespace
+ // created with the v1 admin format (prop/cluster/ns) and then deleted, so no
+ // need to add it to the list
+ return getAsync(joinPath(BASE_POLICIES_PATH, namespace))
+ .thenApply(opt -> opt.isPresent() ? Collections.singletonList(namespace)
+ : new ArrayList<String>())
+ .exceptionally(ex -> {
+ Throwable cause = FutureUtil.unwrapCompletionException(ex);
+ if (cause instanceof MetadataStoreException
+ .ContentDeserializationException) {
+ return new ArrayList<>();
+ }
+ throw FutureUtil.wrapToCompletionException(ex);
+ });
+ } else {
+ CompletableFuture<List<String>> ret = new CompletableFuture();
+ ret.complete(children.stream().map(ns -> NamespaceName.get(tenant, key, ns)
+ .toString()).collect(Collectors.toList()));
+ return ret;
+ }
+ })).reduce(CompletableFuture.completedFuture(new ArrayList<>()),
+ (accumulator, n) -> accumulator.thenCompose(namespaces -> n.thenCompose(m -> {
+ namespaces.addAll(m);
+ return CompletableFuture.completedFuture(namespaces);
Review Comment:
```suggestion
return namespaces;
```
##########
pulsar-broker-common/src/main/java/org/apache/pulsar/broker/resources/TenantResources.java:
##########
@@ -110,6 +111,41 @@ public List<String> getListOfNamespaces(String tenant) throws MetadataStoreExcep
return namespaces;
}
+ public CompletableFuture<List<String>> getListOfNamespacesAsync(String tenant) {
+ // this will return a cluster in v1 and a namespace in v2
+ return getChildrenAsync(joinPath(BASE_POLICIES_PATH, tenant))
+ .thenCompose(clusterOrNamespaces -> clusterOrNamespaces.stream().map(key ->
+ getChildrenAsync(joinPath(BASE_POLICIES_PATH, tenant, key))
+ .thenCompose(children -> {
+ if (children == null || children.isEmpty()) {
+ String namespace = NamespaceName.get(tenant, key).toString();
+ // if the length is 0 then this is probably a leftover cluster from namespace
+ // created with the v1 admin format (prop/cluster/ns) and then deleted, so no
+ // need to add it to the list
+ return getAsync(joinPath(BASE_POLICIES_PATH, namespace))
+ .thenApply(opt -> opt.isPresent() ? Collections.singletonList(namespace)
+ : new ArrayList<String>())
+ .exceptionally(ex -> {
+ Throwable cause = FutureUtil.unwrapCompletionException(ex);
+ if (cause instanceof MetadataStoreException
+ .ContentDeserializationException) {
+ return new ArrayList<>();
+ }
+ throw FutureUtil.wrapToCompletionException(ex);
+ });
+ } else {
+ CompletableFuture<List<String>> ret = new CompletableFuture();
+ ret.complete(children.stream().map(ns -> NamespaceName.get(tenant, key, ns)
+ .toString()).collect(Collectors.toList()));
+ return ret;
+ }
+ })).reduce(CompletableFuture.completedFuture(new ArrayList<>()),
+ (accumulator, n) -> accumulator.thenCompose(namespaces -> n.thenCompose(m -> {
Review Comment:
```suggestion
(accumulator, n) -> accumulator.thenApply(namespaces -> n.thenCompose(m -> {
```
##########
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())
Review Comment:
Directly return future to avoid creating one more future?
--
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: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] codelipenghui merged pull request #15518: [improve][broker] Make some methods in NamespacesBase async.
Posted by GitBox <gi...@apache.org>.
codelipenghui merged PR #15518:
URL: https://github.com/apache/pulsar/pull/15518
--
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: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Technoboy- commented on a diff in pull request #15518: [improve][broker] Make some methods in NamespacesBase async.
Posted by GitBox <gi...@apache.org>.
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: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] nodece commented on a diff in pull request #15518: [improve][broker] Make some methods in NamespacesBase async.
Posted by GitBox <gi...@apache.org>.
nodece commented on code in PR #15518:
URL: https://github.com/apache/pulsar/pull/15518#discussion_r870426460
##########
pulsar-broker-common/src/main/java/org/apache/pulsar/broker/resources/TenantResources.java:
##########
@@ -110,6 +111,45 @@ public List<String> getListOfNamespaces(String tenant) throws MetadataStoreExcep
return namespaces;
}
+ public CompletableFuture<List<String>> getListOfNamespacesAsync(String tenant) {
+ // this will return a cluster in v1 and a namespace in v2
+ return getChildrenAsync(joinPath(BASE_POLICIES_PATH, tenant))
+ .thenApply(clusterOrNamespaces -> clusterOrNamespaces.stream().map(key ->
+ getChildrenAsync(joinPath(BASE_POLICIES_PATH, tenant, key))
+ .thenCompose(children -> {
+ CompletableFuture<List<String>> ret = new CompletableFuture();
+ if (children == null || children.isEmpty()) {
+ String namespace = NamespaceName.get(tenant, key).toString();
+ // if the length is 0 then this is probably a leftover cluster from namespace
+ // created with the v1 admin format (prop/cluster/ns) and then deleted, so no
+ // need to add it to the list
+ ret = getAsync(joinPath(BASE_POLICIES_PATH, namespace))
+ .thenApply(opt -> opt.isPresent() ? Collections.singletonList(namespace)
+ : new ArrayList<String>())
+ .exceptionally(ex -> {
+ Throwable cause = FutureUtil.unwrapCompletionException(ex);
+ if (cause instanceof MetadataStoreException
+ .ContentDeserializationException) {
+ return null;
+ }
+ throw FutureUtil.wrapToCompletionException(ex);
+ });
+ } else {
+ ret.complete(children.stream().map(ns -> NamespaceName.get(tenant, key, ns)
+ .toString()).collect(Collectors.toList()));
+ }
+ return ret;
+ })).collect(Collectors.toList())
Review Comment:
use `reduce` looks better here, so like:
```suggestion
})).reduce(CompletableFuture.completedFuture(new ArrayList<>()),
(accumulator, n) -> accumulator.thenCompose(namespaces -> n.thenCompose(m -> {
namespaces.addAll(m);
return CompletableFuture.completedFuture(namespaces);
}))));
```
then use `thenCompose` instead of `thenApply` in line 117.
We also can avoid the `waitForAll` and `join`.
##########
pulsar-broker-common/src/main/java/org/apache/pulsar/broker/resources/TenantResources.java:
##########
@@ -110,6 +111,45 @@ public List<String> getListOfNamespaces(String tenant) throws MetadataStoreExcep
return namespaces;
}
+ public CompletableFuture<List<String>> getListOfNamespacesAsync(String tenant) {
+ // this will return a cluster in v1 and a namespace in v2
+ return getChildrenAsync(joinPath(BASE_POLICIES_PATH, tenant))
+ .thenApply(clusterOrNamespaces -> clusterOrNamespaces.stream().map(key ->
+ getChildrenAsync(joinPath(BASE_POLICIES_PATH, tenant, key))
+ .thenCompose(children -> {
+ CompletableFuture<List<String>> ret = new CompletableFuture();
+ if (children == null || children.isEmpty()) {
+ String namespace = NamespaceName.get(tenant, key).toString();
+ // if the length is 0 then this is probably a leftover cluster from namespace
+ // created with the v1 admin format (prop/cluster/ns) and then deleted, so no
+ // need to add it to the list
+ ret = getAsync(joinPath(BASE_POLICIES_PATH, namespace))
+ .thenApply(opt -> opt.isPresent() ? Collections.singletonList(namespace)
+ : new ArrayList<String>())
+ .exceptionally(ex -> {
+ Throwable cause = FutureUtil.unwrapCompletionException(ex);
+ if (cause instanceof MetadataStoreException
+ .ContentDeserializationException) {
+ return null;
+ }
+ throw FutureUtil.wrapToCompletionException(ex);
+ });
+ } else {
+ ret.complete(children.stream().map(ns -> NamespaceName.get(tenant, key, ns)
+ .toString()).collect(Collectors.toList()));
+ }
+ return ret;
+ })).collect(Collectors.toList())
Review Comment:
Use `reduce` looks better here, so like:
```suggestion
})).reduce(CompletableFuture.completedFuture(new ArrayList<>()),
(accumulator, n) -> accumulator.thenCompose(namespaces -> n.thenCompose(m -> {
namespaces.addAll(m);
return CompletableFuture.completedFuture(namespaces);
}))));
```
then use `thenCompose` instead of `thenApply` in line 117.
We also can avoid the `waitForAll` and `join`.
--
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: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] shibd commented on a diff in pull request #15518: [improve][broker] Make some methods in NamespacesBase async.
Posted by GitBox <gi...@apache.org>.
shibd commented on code in PR #15518:
URL: https://github.com/apache/pulsar/pull/15518#discussion_r872082744
##########
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())
Review Comment:
+1
--
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: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Technoboy- commented on a diff in pull request #15518: [improve][broker] Make some methods in NamespacesBase async.
Posted by GitBox <gi...@apache.org>.
Technoboy- commented on code in PR #15518:
URL: https://github.com/apache/pulsar/pull/15518#discussion_r869273841
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v1/Namespaces.java:
##########
@@ -187,7 +208,17 @@ public void createNamespace(@PathParam("property") String property, @PathParam("
policies.bundles = getBundles(defaultNumberOfBundles);
}
- internalCreateNamespace(policies);
+ internalCreateNamespace(policies)
Review Comment:
yes, fixed.
--
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: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] nodece commented on a diff in pull request #15518: [improve][broker] Make some methods in NamespacesBase async.
Posted by GitBox <gi...@apache.org>.
nodece commented on code in PR #15518:
URL: https://github.com/apache/pulsar/pull/15518#discussion_r870436161
##########
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:
Then we need to check `AlreadyExistsException`.
--
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: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Technoboy- commented on a diff in pull request #15518: [improve][broker] Make some methods in NamespacesBase async.
Posted by GitBox <gi...@apache.org>.
Technoboy- commented on code in PR #15518:
URL: https://github.com/apache/pulsar/pull/15518#discussion_r869271211
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v1/Namespaces.java:
##########
@@ -187,7 +208,17 @@ public void createNamespace(@PathParam("property") String property, @PathParam("
policies.bundles = getBundles(defaultNumberOfBundles);
}
- internalCreateNamespace(policies);
+ internalCreateNamespace(policies)
+ .thenAccept(__ -> response.resume(Response.noContent().build()))
+ .exceptionally(ex -> {
+ Throwable root = FutureUtil.unwrapCompletionException(ex);
Review Comment:
right.
fixed
--
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: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Technoboy- commented on a diff in pull request #15518: [improve][broker] Make some methods in NamespacesBase async.
Posted by GitBox <gi...@apache.org>.
Technoboy- commented on code in PR #15518:
URL: https://github.com/apache/pulsar/pull/15518#discussion_r869254291
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/AdminResource.java:
##########
@@ -323,6 +323,11 @@ protected CompletableFuture<Policies> getNamespacePoliciesAsync(NamespaceName na
return FutureUtil.failedFuture(new RestException(e));
}
policies.get().bundles = bundleData != null ? bundleData : policies.get().bundles;
+ if (policies.get().is_allow_auto_update_schema == null) {
Review Comment:
https://github.com/apache/pulsar/pull/14409 only adds to the `getNamespacePolicies` method, forget to add to `getNamespacePoliciesAsync `, and causes the test not to pass.
--
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: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] mattisonchao commented on a diff in pull request #15518: [improve][broker] Make some methods in NamespacesBase async.
Posted by GitBox <gi...@apache.org>.
mattisonchao commented on code in PR #15518:
URL: https://github.com/apache/pulsar/pull/15518#discussion_r869259506
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/AdminResource.java:
##########
@@ -323,6 +323,11 @@ protected CompletableFuture<Policies> getNamespacePoliciesAsync(NamespaceName na
return FutureUtil.failedFuture(new RestException(e));
}
policies.get().bundles = bundleData != null ? bundleData : policies.get().bundles;
+ if (policies.get().is_allow_auto_update_schema == null) {
Review Comment:
Oh, I see. thanks!
--
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: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Technoboy- commented on a diff in pull request #15518: [improve][broker] Make some methods in NamespacesBase async.
Posted by GitBox <gi...@apache.org>.
Technoboy- commented on code in PR #15518:
URL: https://github.com/apache/pulsar/pull/15518#discussion_r872116545
##########
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())
Review Comment:
fiexed
--
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: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] lhotari commented on pull request #15518: [improve][broker] Make some methods in NamespacesBase async.
Posted by GitBox <gi...@apache.org>.
lhotari commented on PR #15518:
URL: https://github.com/apache/pulsar/pull/15518#issuecomment-1129725779
there seem to be a new type of failure in tests causing flakiness and it could be related to this PR. Please check #15652
--
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: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Technoboy- commented on a diff in pull request #15518: [improve][broker] Make some methods in NamespacesBase async.
Posted by GitBox <gi...@apache.org>.
Technoboy- commented on code in PR #15518:
URL: https://github.com/apache/pulsar/pull/15518#discussion_r869372040
##########
pulsar-broker-common/src/main/java/org/apache/pulsar/broker/resources/TenantResources.java:
##########
@@ -110,6 +110,40 @@ public List<String> getListOfNamespaces(String tenant) throws MetadataStoreExcep
return namespaces;
}
+ public CompletableFuture<List<String>> getListOfNamespacesAsync(String tenant) {
+ List<String> namespaces = new ArrayList<>();
+ // this will return a cluster in v1 and a namespace in v2
+ CompletableFuture<List<CompletableFuture<Void>>> result = getChildrenAsync(joinPath(BASE_POLICIES_PATH, tenant))
+ .thenApply(clusterOrNamespaces -> clusterOrNamespaces.stream().map(key ->
+ getChildrenAsync(joinPath(BASE_POLICIES_PATH, tenant, key))
+ .thenCompose(children -> {
+ CompletableFuture<Void> ret = CompletableFuture.completedFuture(null);
+ if (children == null || children.isEmpty()) {
+ String namespace = NamespaceName.get(tenant, key).toString();
+ // if the length is 0 then this is probably a leftover cluster from namespace
+ // created with the v1 admin format (prop/cluster/ns) and then deleted, so no
+ // need to add it to the list
+ ret = ret.thenCompose(__ -> getAsync(joinPath(BASE_POLICIES_PATH, namespace))
+ .thenAccept(opt -> opt.map(k -> namespaces.add(namespace)))
+ .exceptionally(ex -> {
+ Throwable cause = FutureUtil.unwrapCompletionException(ex);
+ if (cause instanceof MetadataStoreException
+ .ContentDeserializationException) {
+ return null;
+ }
+ throw FutureUtil.wrapToCompletionException(ex);
+ }));
+ } else {
+ children.forEach(ns -> {
Review Comment:
Fixed, but seems harder to read ...
--
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: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] mattisonchao commented on a diff in pull request #15518: [improve][broker] Make some methods in NamespacesBase async.
Posted by GitBox <gi...@apache.org>.
mattisonchao commented on code in PR #15518:
URL: https://github.com/apache/pulsar/pull/15518#discussion_r869232194
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v1/Namespaces.java:
##########
@@ -187,7 +208,17 @@ public void createNamespace(@PathParam("property") String property, @PathParam("
policies.bundles = getBundles(defaultNumberOfBundles);
}
- internalCreateNamespace(policies);
+ internalCreateNamespace(policies)
+ .thenAccept(__ -> response.resume(Response.noContent().build()))
+ .exceptionally(ex -> {
+ Throwable root = FutureUtil.unwrapCompletionException(ex);
Review Comment:
Should we add an error log here?
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v2/Namespaces.java:
##########
@@ -139,11 +155,23 @@ public Policies getPolicies(@PathParam("tenant") String tenant, @PathParam("name
@ApiResponse(code = 404, message = "Tenant or cluster doesn't exist"),
@ApiResponse(code = 409, message = "Namespace already exists"),
@ApiResponse(code = 412, message = "Namespace name is not valid") })
- public void createNamespace(@PathParam("tenant") String tenant, @PathParam("namespace") String namespace,
- @ApiParam(value = "Policies for the namespace") Policies policies) {
+ public void createNamespace(@Suspended AsyncResponse response,
+ @PathParam("tenant") String tenant,
+ @PathParam("namespace") String namespace,
+ @ApiParam(value = "Policies for the namespace") Policies policies) {
validateNamespaceName(tenant, namespace);
policies = getDefaultPolicesIfNull(policies);
- internalCreateNamespace(policies);
+ internalCreateNamespace(policies)
+ .thenAccept(__ -> response.resume(Response.noContent().build()))
+ .exceptionally(ex -> {
+ Throwable root = FutureUtil.unwrapCompletionException(ex);
Review Comment:
Should we add an error log here?
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v1/Namespaces.java:
##########
@@ -187,7 +208,17 @@ public void createNamespace(@PathParam("property") String property, @PathParam("
policies.bundles = getBundles(defaultNumberOfBundles);
}
- internalCreateNamespace(policies);
+ internalCreateNamespace(policies)
Review Comment:
line 196 `validateClusterForTenant(namespaceName.getTenant(), namespaceName.getCluster())` looks like a sync method that can be async.
##########
pulsar-broker-common/src/main/java/org/apache/pulsar/broker/resources/TenantResources.java:
##########
@@ -110,6 +110,40 @@ public List<String> getListOfNamespaces(String tenant) throws MetadataStoreExcep
return namespaces;
}
+ public CompletableFuture<List<String>> getListOfNamespacesAsync(String tenant) {
+ List<String> namespaces = new ArrayList<>();
+ // this will return a cluster in v1 and a namespace in v2
+ CompletableFuture<List<CompletableFuture<Void>>> result = getChildrenAsync(joinPath(BASE_POLICIES_PATH, tenant))
+ .thenApply(clusterOrNamespaces -> clusterOrNamespaces.stream().map(key ->
+ getChildrenAsync(joinPath(BASE_POLICIES_PATH, tenant, key))
+ .thenCompose(children -> {
+ CompletableFuture<Void> ret = CompletableFuture.completedFuture(null);
+ if (children == null || children.isEmpty()) {
+ String namespace = NamespaceName.get(tenant, key).toString();
+ // if the length is 0 then this is probably a leftover cluster from namespace
+ // created with the v1 admin format (prop/cluster/ns) and then deleted, so no
+ // need to add it to the list
+ ret = ret.thenCompose(__ -> getAsync(joinPath(BASE_POLICIES_PATH, namespace))
+ .thenAccept(opt -> opt.map(k -> namespaces.add(namespace)))
+ .exceptionally(ex -> {
+ Throwable cause = FutureUtil.unwrapCompletionException(ex);
+ if (cause instanceof MetadataStoreException
+ .ContentDeserializationException) {
+ return null;
+ }
+ throw FutureUtil.wrapToCompletionException(ex);
+ }));
+ } else {
+ children.forEach(ns -> {
Review Comment:
Maybe we can use `Collectors.toList()` to avoid the below code and improve readability.
`List<String> namespaces = new ArrayList<>();`
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/AdminResource.java:
##########
@@ -323,6 +323,11 @@ protected CompletableFuture<Policies> getNamespacePoliciesAsync(NamespaceName na
return FutureUtil.failedFuture(new RestException(e));
}
policies.get().bundles = bundleData != null ? bundleData : policies.get().bundles;
+ if (policies.get().is_allow_auto_update_schema == null) {
Review Comment:
Question:
Why add this part of the code?
--
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: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Technoboy- commented on a diff in pull request #15518: [improve][broker] Make some methods in NamespacesBase async.
Posted by GitBox <gi...@apache.org>.
Technoboy- commented on code in PR #15518:
URL: https://github.com/apache/pulsar/pull/15518#discussion_r869271819
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v2/Namespaces.java:
##########
@@ -139,11 +155,23 @@ public Policies getPolicies(@PathParam("tenant") String tenant, @PathParam("name
@ApiResponse(code = 404, message = "Tenant or cluster doesn't exist"),
@ApiResponse(code = 409, message = "Namespace already exists"),
@ApiResponse(code = 412, message = "Namespace name is not valid") })
- public void createNamespace(@PathParam("tenant") String tenant, @PathParam("namespace") String namespace,
- @ApiParam(value = "Policies for the namespace") Policies policies) {
+ public void createNamespace(@Suspended AsyncResponse response,
+ @PathParam("tenant") String tenant,
+ @PathParam("namespace") String namespace,
+ @ApiParam(value = "Policies for the namespace") Policies policies) {
validateNamespaceName(tenant, namespace);
policies = getDefaultPolicesIfNull(policies);
- internalCreateNamespace(policies);
+ internalCreateNamespace(policies)
+ .thenAccept(__ -> response.resume(Response.noContent().build()))
+ .exceptionally(ex -> {
+ Throwable root = FutureUtil.unwrapCompletionException(ex);
Review Comment:
fixed
--
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: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Technoboy- commented on a diff in pull request #15518: [improve][broker] Make some methods in NamespacesBase async.
Posted by GitBox <gi...@apache.org>.
Technoboy- commented on code in PR #15518:
URL: https://github.com/apache/pulsar/pull/15518#discussion_r870943028
##########
pulsar-broker-common/src/main/java/org/apache/pulsar/broker/resources/TenantResources.java:
##########
@@ -110,6 +111,45 @@ public List<String> getListOfNamespaces(String tenant) throws MetadataStoreExcep
return namespaces;
}
+ public CompletableFuture<List<String>> getListOfNamespacesAsync(String tenant) {
+ // this will return a cluster in v1 and a namespace in v2
+ return getChildrenAsync(joinPath(BASE_POLICIES_PATH, tenant))
+ .thenApply(clusterOrNamespaces -> clusterOrNamespaces.stream().map(key ->
+ getChildrenAsync(joinPath(BASE_POLICIES_PATH, tenant, key))
+ .thenCompose(children -> {
+ CompletableFuture<List<String>> ret = new CompletableFuture();
+ if (children == null || children.isEmpty()) {
+ String namespace = NamespaceName.get(tenant, key).toString();
+ // if the length is 0 then this is probably a leftover cluster from namespace
+ // created with the v1 admin format (prop/cluster/ns) and then deleted, so no
+ // need to add it to the list
+ ret = getAsync(joinPath(BASE_POLICIES_PATH, namespace))
+ .thenApply(opt -> opt.isPresent() ? Collections.singletonList(namespace)
+ : new ArrayList<String>())
+ .exceptionally(ex -> {
+ Throwable cause = FutureUtil.unwrapCompletionException(ex);
+ if (cause instanceof MetadataStoreException
+ .ContentDeserializationException) {
+ return null;
+ }
+ throw FutureUtil.wrapToCompletionException(ex);
+ });
+ } else {
+ ret.complete(children.stream().map(ns -> NamespaceName.get(tenant, key, ns)
+ .toString()).collect(Collectors.toList()));
+ }
+ return ret;
+ })).collect(Collectors.toList())
Review Comment:
yes, right .
##########
pulsar-broker-common/src/main/java/org/apache/pulsar/broker/resources/TenantResources.java:
##########
@@ -110,6 +111,45 @@ public List<String> getListOfNamespaces(String tenant) throws MetadataStoreExcep
return namespaces;
}
+ public CompletableFuture<List<String>> getListOfNamespacesAsync(String tenant) {
+ // this will return a cluster in v1 and a namespace in v2
+ return getChildrenAsync(joinPath(BASE_POLICIES_PATH, tenant))
+ .thenApply(clusterOrNamespaces -> clusterOrNamespaces.stream().map(key ->
+ getChildrenAsync(joinPath(BASE_POLICIES_PATH, tenant, key))
+ .thenCompose(children -> {
+ CompletableFuture<List<String>> ret = new CompletableFuture();
+ if (children == null || children.isEmpty()) {
+ String namespace = NamespaceName.get(tenant, key).toString();
+ // if the length is 0 then this is probably a leftover cluster from namespace
+ // created with the v1 admin format (prop/cluster/ns) and then deleted, so no
+ // need to add it to the list
+ ret = getAsync(joinPath(BASE_POLICIES_PATH, namespace))
+ .thenApply(opt -> opt.isPresent() ? Collections.singletonList(namespace)
+ : new ArrayList<String>())
+ .exceptionally(ex -> {
+ Throwable cause = FutureUtil.unwrapCompletionException(ex);
+ if (cause instanceof MetadataStoreException
+ .ContentDeserializationException) {
+ return null;
+ }
+ throw FutureUtil.wrapToCompletionException(ex);
+ });
+ } else {
+ ret.complete(children.stream().map(ns -> NamespaceName.get(tenant, key, ns)
+ .toString()).collect(Collectors.toList()));
+ }
+ return ret;
+ })).collect(Collectors.toList())
Review Comment:
fixed
--
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: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org