Skip to content

Commit

Permalink
refactor user cache
Browse files Browse the repository at this point in the history
  • Loading branch information
patriot1burke committed Mar 10, 2016
1 parent cd299c9 commit 82ad261
Show file tree
Hide file tree
Showing 16 changed files with 289 additions and 311 deletions.
Expand Up @@ -23,24 +23,26 @@ public abstract class CacheManager {
protected static final Logger logger = Logger.getLogger(CacheManager.class); protected static final Logger logger = Logger.getLogger(CacheManager.class);
protected final Cache<String, Long> revisions; protected final Cache<String, Long> revisions;
protected final Cache<String, Revisioned> cache; protected final Cache<String, Revisioned> cache;
protected final UpdateCounter counter = new UpdateCounter();


public CacheManager(Cache<String, Revisioned> cache, Cache<String, Long> revisions) { public CacheManager(Cache<String, Revisioned> cache, Cache<String, Long> revisions) {
this.cache = cache; this.cache = cache;
this.revisions = revisions; this.revisions = revisions;
this.cache.addListener(this);
} }


public Cache<String, Revisioned> getCache() { public Cache<String, Revisioned> getCache() {
return cache; return cache;
} }


public Cache<String, Long> getRevisions() { public long getCurrentCounter() {
return revisions; return counter.current();
} }


public Long getCurrentRevision(String id) { public Long getCurrentRevision(String id) {
Long revision = revisions.get(id); Long revision = revisions.get(id);
if (revision == null) { if (revision == null) {
revision = UpdateCounter.current(); revision = counter.current();
} }
// if you do cache.remove() on node 1 and the entry doesn't exist on node 2, node 2 never receives a invalidation event // if you do cache.remove() on node 1 and the entry doesn't exist on node 2, node 2 never receives a invalidation event
// so, we do this to force this. // so, we do this to force this.
Expand Down Expand Up @@ -85,7 +87,7 @@ public Object invalidateObject(String id) {
} }


protected void bumpVersion(String id) { protected void bumpVersion(String id) {
long next = UpdateCounter.next(); long next = counter.next();
Object rev = revisions.put(id, next); Object rev = revisions.put(id, next);
} }


Expand All @@ -97,7 +99,7 @@ public void addRevisioned(Revisioned object, long startupRevision) {
Long rev = revisions.get(id); Long rev = revisions.get(id);
if (rev == null) { if (rev == null) {
if (id.endsWith("realm.clients")) RealmCacheManager.logger.trace("addRevisioned rev == null realm.clients"); if (id.endsWith("realm.clients")) RealmCacheManager.logger.trace("addRevisioned rev == null realm.clients");
rev = UpdateCounter.current(); rev = counter.current();
revisions.put(id, rev); revisions.put(id, rev);
} }
revisions.startBatch(); revisions.startBatch();
Expand Down
Expand Up @@ -29,6 +29,7 @@
import org.keycloak.models.cache.CacheUserProvider; import org.keycloak.models.cache.CacheUserProvider;
import org.keycloak.models.cache.CacheUserProviderFactory; import org.keycloak.models.cache.CacheUserProviderFactory;
import org.keycloak.models.cache.infinispan.entities.CachedUser; import org.keycloak.models.cache.infinispan.entities.CachedUser;
import org.keycloak.models.cache.infinispan.entities.Revisioned;


import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;


Expand All @@ -39,25 +40,23 @@ public class InfinispanCacheUserProviderFactory implements CacheUserProviderFact


private static final Logger log = Logger.getLogger(InfinispanCacheUserProviderFactory.class); private static final Logger log = Logger.getLogger(InfinispanCacheUserProviderFactory.class);


protected volatile InfinispanUserCache userCache; protected volatile UserCacheManager userCache;


protected final RealmLookup usernameLookup = new RealmLookup();


protected final RealmLookup emailLookup = new RealmLookup();


@Override @Override
public CacheUserProvider create(KeycloakSession session) { public CacheUserProvider create(KeycloakSession session) {
lazyInit(session); lazyInit(session);
return new DefaultCacheUserProvider(userCache, session); return new UserCacheSession(userCache, session);
} }


private void lazyInit(KeycloakSession session) { private void lazyInit(KeycloakSession session) {
if (userCache == null) { if (userCache == null) {
synchronized (this) { synchronized (this) {
if (userCache == null) { if (userCache == null) {
Cache<String, CachedUser> cache = session.getProvider(InfinispanConnectionProvider.class).getCache(InfinispanConnectionProvider.USER_CACHE_NAME); Cache<String, Revisioned> cache = session.getProvider(InfinispanConnectionProvider.class).getCache(InfinispanConnectionProvider.USER_CACHE_NAME);
cache.addListener(new CacheListener()); Cache<String, Long> revisions = session.getProvider(InfinispanConnectionProvider.class).getCache(InfinispanConnectionProvider.VERSION_CACHE_NAME);
userCache = new InfinispanUserCache(cache, usernameLookup, emailLookup); userCache = new UserCacheManager(cache, revisions);
} }
} }
} }
Expand All @@ -81,100 +80,5 @@ public String getId() {
return "default"; return "default";
} }


@Listener
public class CacheListener {

@CacheEntryCreated
public void userCreated(CacheEntryCreatedEvent<String, CachedUser> event) {
if (!event.isPre()) {
CachedUser user = event.getValue();
if (user != null) {
String realm = user.getRealm();

usernameLookup.put(realm, user.getUsername(), user.getId());
if (user.getEmail() != null) {
emailLookup.put(realm, user.getEmail(), user.getId());
}

log.tracev("User added realm={0}, id={1}, username={2}", realm, user.getId(), user.getUsername());
}
}
}

@CacheEntryRemoved
public void userRemoved(CacheEntryRemovedEvent<String, CachedUser> event) {
if (event.isPre()) {
CachedUser user = event.getValue();
if (user != null) {
removeUser(user);

log.tracev("User invalidated realm={0}, id={1}, username={2}", user.getRealm(), user.getId(), user.getUsername());
}
}
}

@CacheEntryInvalidated
public void userInvalidated(CacheEntryInvalidatedEvent<String, CachedUser> event) {
if (event.isPre()) {
CachedUser user = event.getValue();
if (user != null) {
removeUser(user);

log.tracev("User invalidated realm={0}, id={1}, username={2}", user.getRealm(), user.getId(), user.getUsername());
}
}
}

@CacheEntriesEvicted
public void userEvicted(CacheEntriesEvictedEvent<String, CachedUser> event) {
for (CachedUser user : event.getEntries().values()) {
removeUser(user);

log.tracev("User evicted realm={0}, id={1}, username={2}", user.getRealm(), user.getId(), user.getUsername());
}
}

private void removeUser(CachedUser cachedUser) {
String realm = cachedUser.getRealm();
usernameLookup.remove(realm, cachedUser.getUsername());
if (cachedUser.getEmail() != null) {
emailLookup.remove(realm, cachedUser.getEmail());
}
}

}

static class RealmLookup {

protected final ConcurrentHashMap<String, ConcurrentHashMap<String, String>> lookup = new ConcurrentHashMap<>();

public void put(String realm, String key, String value) {
ConcurrentHashMap<String, String> map = lookup.get(realm);
if(map == null) {
map = new ConcurrentHashMap<>();
ConcurrentHashMap<String, String> p = lookup.putIfAbsent(realm, map);
if (p != null) {
map = p;
}
}
map.put(key, value);
}

public String get(String realm, String key) {
ConcurrentHashMap<String, String> map = lookup.get(realm);
return map != null ? map.get(key) : null;
}

public void remove(String realm, String key) {
ConcurrentHashMap<String, String> map = lookup.get(realm);
if (map != null) {
map.remove(key);
if (map.isEmpty()) {
lookup.remove(realm);
}
}
}

}


} }

This file was deleted.

Expand Up @@ -48,7 +48,6 @@ public class RealmCacheManager extends CacheManager {


public RealmCacheManager(Cache<String, Revisioned> cache, Cache<String, Long> revisions) { public RealmCacheManager(Cache<String, Revisioned> cache, Cache<String, Long> revisions) {
super(cache, revisions); super(cache, revisions);
this.cache.addListener(this);
} }




Expand Down
Expand Up @@ -131,7 +131,7 @@ public class RealmCacheSession implements CacheRealmProvider {
public RealmCacheSession(RealmCacheManager cache, KeycloakSession session) { public RealmCacheSession(RealmCacheManager cache, KeycloakSession session) {
this.cache = cache; this.cache = cache;
this.session = session; this.session = session;
this.startupRevision = UpdateCounter.current(); this.startupRevision = cache.getCurrentCounter();
session.getTransaction().enlistPrepare(getPrepareTransaction()); session.getTransaction().enlistPrepare(getPrepareTransaction());
session.getTransaction().enlistAfterCompletion(getAfterTransaction()); session.getTransaction().enlistAfterCompletion(getAfterTransaction());
} }
Expand Down Expand Up @@ -380,6 +380,14 @@ public boolean removeRealm(String id) {
return getDelegate().removeRealm(id); return getDelegate().removeRealm(id);
} }


protected void invalidateClient(RealmModel realm, ClientModel client) {
invalidations.add(client.getId());
invalidations.add(getRealmClientsQueryCacheKey(realm.getId()));
invalidations.add(getClientByClientIdCacheKey(client.getClientId(), realm));
listInvalidations.add(realm.getId());
}


@Override @Override
public ClientModel addClient(RealmModel realm, String clientId) { public ClientModel addClient(RealmModel realm, String clientId) {
ClientModel client = getDelegate().addClient(realm, clientId); ClientModel client = getDelegate().addClient(realm, clientId);
Expand All @@ -395,8 +403,7 @@ public ClientModel addClient(RealmModel realm, String id, String clientId) {
private ClientModel addedClient(RealmModel realm, ClientModel client) { private ClientModel addedClient(RealmModel realm, ClientModel client) {
logger.trace("added Client....."); logger.trace("added Client.....");
// need to invalidate realm client query cache every time as it may not be loaded on this node, but loaded on another // need to invalidate realm client query cache every time as it may not be loaded on this node, but loaded on another
invalidations.add(getRealmClientsQueryCacheKey(realm.getId())); invalidateClient(realm, client);
invalidations.add(client.getId());
cache.clientAdded(realm.getId(), client.getId(), invalidations); cache.clientAdded(realm.getId(), client.getId(), invalidations);
// this is needed so that a new client that hasn't been committed isn't cached in a query // this is needed so that a new client that hasn't been committed isn't cached in a query
listInvalidations.add(realm.getId()); listInvalidations.add(realm.getId());
Expand Down Expand Up @@ -464,10 +471,7 @@ public boolean removeClient(String id, RealmModel realm) {
ClientModel client = getClientById(id, realm); ClientModel client = getClientById(id, realm);
if (client == null) return false; if (client == null) return false;
// need to invalidate realm client query cache every time client list is changed // need to invalidate realm client query cache every time client list is changed
invalidations.add(getRealmClientsQueryCacheKey(realm.getId())); invalidateClient(realm, client);
invalidations.add(getClientByClientIdCacheKey(client.getClientId(), realm));
listInvalidations.add(realm.getId());
registerClientInvalidation(id);
cache.clientRemoval(realm.getId(), id, invalidations); cache.clientRemoval(realm.getId(), id, invalidations);
for (RoleModel role : client.getRoles()) { for (RoleModel role : client.getRoles()) {
cache.roleInvalidation(role.getId(), invalidations); cache.roleInvalidation(role.getId(), invalidations);
Expand Down
Expand Up @@ -9,13 +9,13 @@
*/ */
public class UpdateCounter { public class UpdateCounter {


private static final AtomicLong counter = new AtomicLong(); private final AtomicLong counter = new AtomicLong();


public static long current() { public long current() {
return counter.get(); return counter.get();
} }


public static long next() { public long next() {
return counter.incrementAndGet(); return counter.incrementAndGet();
} }


Expand Down
Expand Up @@ -31,11 +31,11 @@
public class UserAdapter implements UserModel { public class UserAdapter implements UserModel {
protected UserModel updated; protected UserModel updated;
protected CachedUser cached; protected CachedUser cached;
protected CacheUserProvider userProviderCache; protected UserCacheSession userProviderCache;
protected KeycloakSession keycloakSession; protected KeycloakSession keycloakSession;
protected RealmModel realm; protected RealmModel realm;


public UserAdapter(CachedUser cached, CacheUserProvider userProvider, KeycloakSession keycloakSession, RealmModel realm) { public UserAdapter(CachedUser cached, UserCacheSession userProvider, KeycloakSession keycloakSession, RealmModel realm) {
this.cached = cached; this.cached = cached;
this.userProviderCache = userProvider; this.userProviderCache = userProvider;
this.keycloakSession = keycloakSession; this.keycloakSession = keycloakSession;
Expand All @@ -44,7 +44,7 @@ public UserAdapter(CachedUser cached, CacheUserProvider userProvider, KeycloakSe


protected void getDelegateForUpdate() { protected void getDelegateForUpdate() {
if (updated == null) { if (updated == null) {
userProviderCache.registerUserInvalidation(realm, getId()); userProviderCache.registerUserInvalidation(realm, cached);
updated = userProviderCache.getDelegate().getUserById(getId(), realm); updated = userProviderCache.getDelegate().getUserById(getId(), realm);
if (updated == null) throw new IllegalStateException("Not found in database"); if (updated == null) throw new IllegalStateException("Not found in database");
} }
Expand Down

0 comments on commit 82ad261

Please sign in to comment.