Skip to content

Commit

Permalink
Fix cluster-wide cache clearance (MID-6440)
Browse files Browse the repository at this point in the history
  • Loading branch information
mederly committed Sep 16, 2020
1 parent fe8a7eb commit f8fbbfa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Expand Up @@ -9,6 +9,7 @@
import javax.annotation.PostConstruct;
import javax.ws.rs.core.Response;

import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
Expand Down Expand Up @@ -57,8 +58,7 @@ public <O extends ObjectType> void invalidate(Class<O> type, String oid, boolean
// Regular cache invalidation can be skipped for nodes not checking in. Cache entries will expire on such nodes
// eventually. (We can revisit this design decision if needed.)
clusterExecutionHelper.execute((client, node, result1) -> {
client.path(ClusterServiceConsts.EVENT_INVALIDATION +
ObjectTypes.getRestTypeFromClass(type) + (oid != null ? "/" + oid : ""));
client.path(getInvalidationRestPath(type, oid));
Response response = client.post(null);
Response.StatusType statusInfo = response.getStatusInfo();
if (statusInfo.getFamily() != Response.Status.Family.SUCCESSFUL) {
Expand All @@ -72,6 +72,22 @@ public <O extends ObjectType> void invalidate(Class<O> type, String oid, boolean
}, null, "cache invalidation", result);
}

@NotNull
private <O extends ObjectType> String getInvalidationRestPath(Class<O> type, String oid) {
StringBuilder sb = new StringBuilder(ClusterServiceConsts.EVENT_INVALIDATION);
if (type != null) {
sb.append(ObjectTypes.getRestTypeFromClass(type));
if (oid != null) {
sb.append("/").append(oid);
}
} else {
if (oid != null) {
LOGGER.warn("Cannot invalidate object type null with specific OID. Converting to global invalidation (type=null, oid=null).");
}
}
return sb.toString();
}

private <O extends ObjectType> boolean canExecute(Class<O> type, String oid, boolean clusterwide, CacheInvalidationContext context) {
if (!clusterwide) {
LOGGER.trace("Ignoring invalidate() call for type {} (oid={}) because clusterwide=false", type, oid);
Expand Down
Expand Up @@ -72,6 +72,11 @@ public ClusterRestController() {
// nothing to do
}

@PostMapping(ClusterServiceConsts.EVENT_INVALIDATION)
public ResponseEntity<?> executeClusterCacheInvalidationEvent() {
return executeClusterCacheInvalidationEvent(null, null);
}

@PostMapping(ClusterServiceConsts.EVENT_INVALIDATION + "{type}")
public ResponseEntity<?> executeClusterCacheInvalidationEvent(
@PathVariable("type") String type) {
Expand Down

0 comments on commit f8fbbfa

Please sign in to comment.