Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[improve][broker]Make SubscriptionExpirationTime method async #16328

Merged
merged 2 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -857,17 +857,19 @@ protected CompletableFuture<Void> internalSetNamespaceMessageTTLAsync(Integer me
}));
}

protected void internalSetSubscriptionExpirationTime(Integer expirationTime) {
validateNamespacePolicyOperation(namespaceName, PolicyName.SUBSCRIPTION_EXPIRATION_TIME, PolicyOperation.WRITE);
validatePoliciesReadOnlyAccess();

if (expirationTime != null && expirationTime < 0) {
throw new RestException(Status.PRECONDITION_FAILED, "Invalid value for subscription expiration time");
}
updatePolicies(namespaceName, policies -> {
policies.subscription_expiration_time_minutes = expirationTime;
return policies;
});
protected CompletableFuture<Void> internalSetSubscriptionExpirationTimeAsync(Integer expirationTime) {
return validateNamespacePolicyOperationAsync(namespaceName, PolicyName.SUBSCRIPTION_EXPIRATION_TIME,
PolicyOperation.WRITE)
.thenCompose(__ -> validatePoliciesReadOnlyAccessAsync())
.thenAccept(__ -> {
if (expirationTime != null && expirationTime < 0) {
throw new RestException(Status.PRECONDITION_FAILED,
"Invalid value for subscription expiration time");
}
}).thenCompose(__ -> updatePoliciesAsync(namespaceName, policies -> {
policies.subscription_expiration_time_minutes = expirationTime;
return policies;
}));
}

protected CompletableFuture<AutoTopicCreationOverride> internalGetAutoTopicCreationAsync() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,13 +470,19 @@ public void removeNamespaceMessageTTL(@Suspended AsyncResponse asyncResponse,
@ApiOperation(hidden = true, value = "Get the subscription expiration time for the namespace")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"),
@ApiResponse(code = 404, message = "Property or cluster or namespace doesn't exist") })
public Integer getSubscriptionExpirationTime(@PathParam("property") String property,
@PathParam("cluster") String cluster, @PathParam("namespace") String namespace) {
validateAdminAccessForTenant(property);
public void getSubscriptionExpirationTime(@Suspended AsyncResponse asyncResponse,
@PathParam("property") String property,
@PathParam("cluster") String cluster, @PathParam("namespace") String namespace) {
validateNamespaceName(property, cluster, namespace);

Policies policies = getNamespacePolicies(namespaceName);
return policies.subscription_expiration_time_minutes;
validateAdminAccessForTenantAsync(property)
.thenCompose(__ -> getNamespacePoliciesAsync(namespaceName))
.thenAccept(policies -> asyncResponse.resume(policies.subscription_expiration_time_minutes))
.exceptionally(ex -> {
log.error("[{}] Failed to get subscription expiration time for namespace {}: {} ", clientAppId(),
namespaceName, ex.getCause().getMessage(), ex);
resumeAsyncResponseExceptionally(asyncResponse, ex);
return null;
});
}

@POST
Expand All @@ -485,21 +491,37 @@ public Integer getSubscriptionExpirationTime(@PathParam("property") String prope
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"),
@ApiResponse(code = 404, message = "Property or cluster or namespace doesn't exist"),
@ApiResponse(code = 412, message = "Invalid expiration time") })
public void setSubscriptionExpirationTime(@PathParam("property") String property,
public void setSubscriptionExpirationTime(@Suspended AsyncResponse asyncResponse,
@PathParam("property") String property,
@PathParam("cluster") String cluster, @PathParam("namespace") String namespace, int expirationTime) {
validateNamespaceName(property, cluster, namespace);
internalSetSubscriptionExpirationTime(expirationTime);
internalSetSubscriptionExpirationTimeAsync(expirationTime)
.thenAccept(__ -> asyncResponse.resume(Response.noContent().build()))
.exceptionally(ex -> {
log.error("[{}] Failed to set subscription expiration time for namespace {}: {} ", clientAppId(),
namespaceName, ex.getCause().getMessage(), ex);
resumeAsyncResponseExceptionally(asyncResponse, ex);
return null;
});
}

@DELETE
@Path("/{property}/{cluster}/{namespace}/subscriptionExpirationTime")
@ApiOperation(hidden = true, value = "Remove subscription expiration time for namespace")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"),
@ApiResponse(code = 404, message = "Property or cluster or namespace doesn't exist") })
public void removeSubscriptionExpirationTime(@PathParam("property") String property,
public void removeSubscriptionExpirationTime(@Suspended AsyncResponse asyncResponse,
@PathParam("property") String property,
@PathParam("cluster") String cluster, @PathParam("namespace") String namespace) {
validateNamespaceName(property, cluster, namespace);
internalSetSubscriptionExpirationTime(null);
internalSetSubscriptionExpirationTimeAsync(null)
.thenAccept(__ -> asyncResponse.resume(Response.noContent().build()))
.exceptionally(ex -> {
log.error("[{}] Failed to remove subscription expiration time for namespace {}: {} ", clientAppId(),
namespaceName, ex.getCause().getMessage(), ex);
resumeAsyncResponseExceptionally(asyncResponse, ex);
return null;
});
}

@POST
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,13 +410,19 @@ public void removeNamespaceMessageTTL(@Suspended AsyncResponse asyncResponse,
@ApiOperation(value = "Get the subscription expiration time for the namespace")
@ApiResponses(value = { @ApiResponse(code = 403, message = "Don't have admin permission"),
@ApiResponse(code = 404, message = "Tenant or cluster or namespace doesn't exist") })
public Integer getSubscriptionExpirationTime(@PathParam("tenant") String tenant,
@PathParam("namespace") String namespace) {
validateAdminAccessForTenant(tenant);
public void getSubscriptionExpirationTime(@Suspended AsyncResponse asyncResponse,
@PathParam("tenant") String tenant,
@PathParam("namespace") String namespace) {
validateNamespaceName(tenant, namespace);

Policies policies = getNamespacePolicies(namespaceName);
return policies.subscription_expiration_time_minutes;
validateAdminAccessForTenantAsync(tenant)
.thenCompose(__ -> getNamespacePoliciesAsync(namespaceName))
.thenAccept(policies -> asyncResponse.resume(policies.subscription_expiration_time_minutes))
.exceptionally(ex -> {
log.error("[{}] Failed to get subscription expiration time for namespace {}: {} ", clientAppId(),
namespaceName, ex.getCause().getMessage(), ex);
resumeAsyncResponseExceptionally(asyncResponse, ex);
return null;
});
}

@POST
Expand All @@ -425,24 +431,40 @@ public Integer getSubscriptionExpirationTime(@PathParam("tenant") String tenant,
@ApiResponses(value = {@ApiResponse(code = 403, message = "Don't have admin permission"),
@ApiResponse(code = 404, message = "Tenant or cluster or namespace doesn't exist"),
@ApiResponse(code = 412, message = "Invalid expiration time")})
public void setSubscriptionExpirationTime(@PathParam("tenant") String tenant,
public void setSubscriptionExpirationTime(@Suspended AsyncResponse asyncResponse,
@PathParam("tenant") String tenant,
@PathParam("namespace") String namespace,
@ApiParam(value =
"Expiration time in minutes for the specified namespace",
required = true) int expirationTime) {
validateNamespaceName(tenant, namespace);
internalSetSubscriptionExpirationTime(expirationTime);
internalSetSubscriptionExpirationTimeAsync(expirationTime)
.thenAccept(__ -> asyncResponse.resume(Response.noContent().build()))
.exceptionally(ex -> {
log.error("[{}] Failed to set subscription expiration time for namespace {}: {} ", clientAppId(),
namespaceName, ex.getCause().getMessage(), ex);
resumeAsyncResponseExceptionally(asyncResponse, ex);
return null;
});
}

@DELETE
@Path("/{tenant}/{namespace}/subscriptionExpirationTime")
@ApiOperation(value = "Remove subscription expiration time for namespace")
@ApiResponses(value = {@ApiResponse(code = 403, message = "Don't have admin permission"),
@ApiResponse(code = 404, message = "Tenant or cluster or namespace doesn't exist")})
public void removeSubscriptionExpirationTime(@PathParam("tenant") String tenant,
public void removeSubscriptionExpirationTime(@Suspended AsyncResponse asyncResponse,
@PathParam("tenant") String tenant,
@PathParam("namespace") String namespace) {
validateNamespaceName(tenant, namespace);
internalSetSubscriptionExpirationTime(null);
internalSetSubscriptionExpirationTimeAsync(null)
.thenAccept(__ -> asyncResponse.resume(Response.noContent().build()))
.exceptionally(ex -> {
log.error("[{}] Failed to remove subscription expiration time for namespace {}: {} ", clientAppId(),
namespaceName, ex.getCause().getMessage(), ex);
resumeAsyncResponseExceptionally(asyncResponse, ex);
return null;
});
}

@GET
Expand Down