From 9511e22f7858cd4579e7eae6b0f990178002db65 Mon Sep 17 00:00:00 2001 From: Pavol Mederly Date: Wed, 16 Sep 2020 23:32:41 +0200 Subject: [PATCH] Fix cluster-wide cache clearance (MID-6440) (cherry picked from commit f8fbbfa6664f2d9c5f1b453acc997fd5a397bb4e) --- .../model/impl/ClusterCacheListener.java | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/ClusterCacheListener.java b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/ClusterCacheListener.java index 5d5c6e91c2c..7121803de44 100644 --- a/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/ClusterCacheListener.java +++ b/model/model-impl/src/main/java/com/evolveum/midpoint/model/impl/ClusterCacheListener.java @@ -6,6 +6,15 @@ */ package com.evolveum.midpoint.model.impl; +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; +import org.springframework.stereotype.Component; + import com.evolveum.midpoint.CacheInvalidationContext; import com.evolveum.midpoint.TerminateSessionEvent; import com.evolveum.midpoint.model.impl.security.NodeAuthenticationToken; @@ -58,9 +67,8 @@ public void invalidate(Class 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, result1) -> { - client.path(ClusterRestService.EVENT_INVALIDATION + - ObjectTypes.getRestTypeFromClass(type) + (oid != null ? "/" + oid : "")); + clusterExecutionHelper.execute((client, node, result1) -> { + client.path(getInvalidationRestPath(type, oid)); Response response = client.post(null); Response.StatusType statusInfo = response.getStatusInfo(); if (statusInfo.getFamily() != Response.Status.Family.SUCCESSFUL) { @@ -74,6 +82,22 @@ public void invalidate(Class type, String oid, boolean }, null, "cache invalidation", result); } + @NotNull + private String getInvalidationRestPath(Class 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 boolean canExecute(Class type, String oid, boolean clusterwide, CacheInvalidationContext context) { if (!clusterwide) { LOGGER.trace("Ignoring invalidate() call for type {} (oid={}) because clusterwide=false", type, oid);