Class CacheManager

java.lang.Object
org.keycloak.models.cache.infinispan.CacheManager
Direct Known Subclasses:
RealmCacheManager, StoreFactoryCacheManager, UserCacheManager

public abstract class CacheManager extends Object
Some notes on how this works: This implementation manages optimistic locking and version checks itself. The reason is Infinispan just does behave the way we need it to. Not saying Infinispan is bad, just that we have specific caching requirements! This is an invalidation cache implementation and requires to caches: Cache 1 is an Invalidation Cache Cache 2 is a local-only revision number cache. Each node in the cluster maintains its own revision number cache for each entry in the main invalidation cache. This revision cache holds the version counter for each cached entity. Cache listeners do not receive a @CacheEntryInvalidated event if that node does not have an entry for that item. So, consider the following. 1. Node 1 gets current counter for user. There currently isn't one as this user isn't cached. 2. Node 1 reads user from DB 3. Node 2 updates user 4. Node 2 calls cache.remove(user). This does not result in an invalidation listener event to node 1! 5. node 1 checks version counter, checks pass. Stale entry is cached. The issue is that Node 1 doesn't have an entry for the user, so it never receives an invalidation listener event from Node 2 thus it can't bump the version. So, when node 1 goes to cache the user it is stale as the version number was never bumped. How is this issue fixed with the current implementation? 1. Node 1 starts a transaction and records the current revision of the user (getCurrentRevision(); if no revision exists yet, it uses the current value of the node's local counter). 2. Node 1 reads the user from the DB. 3. Node 2 updates the user in the DB. 4. Node 2 calls invalidateObject(userId). This removes the entry from the local cache. 5. Node 2 then bumps the user's revision in its local revision cache. 6. Node 2 sends an InvalidationEvent to the cluster. 7. Node 1 receives the InvalidationEvent and calls invalidateObject(userId). This removes the entry from its local cache if one exists. It also bumps the user's revision in its local revision cache, even if there was no entry: the revision is set to the next value of the node's local counter, which is higher than any value that node has used before. Revisions are only compared on the same node, so they don't need to match across nodes. 8. Node 1 tries to cache the user it read in step 2. addRevisioned() skips caching if the user's current revision is newer than the revision of the transaction start, or newer than the revision recorded in step 1. The stale entry is therefore not stored. The revision cache is local and bounded, so a user's revision entry can be evicted at any time. In that case addRevisioned() falls back to the node's current counter. That counter is newer than the transaction start if any invalidation happened on this node since then, so caching is skipped. This can skip caching unnecessarily, but it never caches a stale entry. No extra marker key is added to the invalidation cache. The revision bump on every node, whether or not it cached the entry, prevents the stale entry from being cached.
Version:
$Revision: 1 $
Author:
Bill Burke
  • Field Details

  • Constructor Details

  • Method Details

    • getLogger

      protected abstract org.jboss.logging.Logger getLogger()
    • getCache

      public org.infinispan.Cache<String,Revisioned> getCache()
    • getCurrentCounter

      public long getCurrentCounter()
    • getCurrentRevision

      public long getCurrentRevision(String id)
    • endRevisionBatch

      public void endRevisionBatch()
    • get

      public <T extends Revisioned> T get(String id, Class<T> type)
    • invalidateObject

      public Object invalidateObject(String id)
    • bumpVersion

      protected void bumpVersion(String id)
    • addRevisioned

      public void addRevisioned(Revisioned object, long startupRevision)
    • addRevisioned

      public void addRevisioned(Revisioned object, long startupRevision, long lifespan)
    • clear

      public void clear()
    • addInvalidations

      public void addInvalidations(Predicate<Map.Entry<String,Revisioned>> predicate, Set<String> invalidations)
    • sendInvalidationEvents

      public void sendInvalidationEvents(KeycloakSession session, Collection<InvalidationEvent> invalidationEvents, String eventKey)
    • invalidationEventReceived

      public void invalidationEventReceived(InvalidationEvent event)
    • addInvalidationsFromEvent

      protected abstract void addInvalidationsFromEvent(InvalidationEvent event, Set<String> invalidations)
    • invalidateCacheKey

      public void invalidateCacheKey(String key, Set<String> invalidations)
    • onClearEvent

      public void onClearEvent(ClusterEvent ignored)
      Handles a cluster-wide cache clear event by clearing all entries from this cache manager.
      Parameters:
      ignored - The cluster event (unused).
    • onInvalidateEvent

      public void onInvalidateEvent(ClusterEvent event)
      Handles a cluster-wide invalidation event by delegating to invalidationEventReceived(InvalidationEvent).
      Parameters:
      event - The cluster event, which must be an InvalidationEvent.