ICacheRegistryService and CacheBuilder possible issue [message #1828732] |
Wed, 17 June 2020 17:22  |
Eclipse User |
|
|
|
I was experimenting with concurrent access to the cache including its possible concurrent creation.
To my surprise creating twice the same cache did not caused an error.
I created custom CacheRegisteryService to log what is going on...
It appears that cache is successfuly created twice, but only one of them is referenced in Cache RegisteryService (the last one created).
If I understand it correctly ConcurrentHashMap.put() overwrites previous value if there was any and returns the previous value.
I see three solutions:
1. After building cache do not use that object, get it instead from CacheRegisteryService - the easiest to implement (Possible cached data loss?)
2. make CacheRegisteryService throw exceptio on second create
3. make CacheRegisteryService return existing cache on second create - the most convenient to use. (assuming the remaining cache parameters are the same). But that would require altering interface ICacheRegisteryService, and build() and register() methods in CacheBuilder class.
experiments:
...
ICache<KeyContext, ScoutRole> c1 = createRolesCache();
ICache<KeyContext, ScoutRole> c2 = getRolesCache();
ICache<KeyContext, ScoutRole> c3 = createRolesCache();
ICache<KeyContext, ScoutRole> c4 = getRolesCache();
LOG.debug("c1: {}", c1);
LOG.debug("c2: {}", c2);
LOG.debug("c3: {}", c3);
LOG.debug("c4: {}", c4);
...
private ICache<KeyContext, ScoutRole> getRolesCache() {
LOG.entry();
ICache<KeyContext, ScoutRole> result = null;
CacheRegistryService crs = BEANS.get(CacheRegistryService.class);
result = crs.opt(ROLES_CACHE_ID);
if (result == null) {
result = createRolesCache();
}
return LOG.exit(result);
}
private ICache<KeyContext, ScoutRole> createRolesCache() {
LOG.entry();
ICache<KeyContext, ScoutRole> result = null;
@SuppressWarnings("unchecked")
ICacheBuilder<KeyContext, ScoutRole> cacheBuilder = BEANS.get(ICacheBuilder.class);
result = cacheBuilder
.withCacheId(ROLES_CACHE_ID)
.withValueResolver(new PermissionCacheValueResolver())
.withThreadSafe(true)
.build();
return LOG.exit(result);
}
package io.poc.scout;
import org.eclipse.scout.rt.platform.Replace;
import org.eclipse.scout.rt.platform.cache.CacheRegistryService;
import org.eclipse.scout.rt.platform.cache.ICache;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;
@Replace
public class MyCacheRegistryService extends CacheRegistryService {
private static final XLogger LOG = XLoggerFactory.getXLogger(MyCacheRegistryService.class);
public MyCacheRegistryService() {
LOG.entry();
LOG.exit();
}
@Override
public <K, V> void register(ICache<K, V> cache) {
LOG.entry(cache);
if (getMap().get(cache.getCacheId()) != null) {
LOG.debug("cache already registered: {}", cache.getCacheId());
}
getMap().put(cache.getCacheId(), cache);
LOG.debug("keys: {}", getMap().keySet());
LOG.exit();
}
}
relevant logs:
22:03:54.040 T [qtp440938038-12] io.poc.scout.MyCacheRegistryService register(MyCacheRegistryService.java:23) - entry with (org.eclipse.scout.rt.platform.cache.BasicCache@5c933438)
22:03:54.041 D [qtp440938038-12] io.poc.scout.MyCacheRegistryService register(MyCacheRegistryService.java:31) - keys: [org.eclipse.scout.apps.helloworld.server.security.ServerAccessControlService.CACHE, org.eclipse.scout.rt.shared.services.common.code.CodeService]
22:03:54.042 T [qtp440938038-12] io.poc.scout.MyCacheRegistryService register(MyCacheRegistryService.java:33) - exit
22:03:54.043 T [qtp440938038-12] org.eclipse.scout.apps.helloworld.server.security.ServerAccessControlService createRolesCache(ServerAccessControlService.java:267) - exit with (org.eclipse.scout.rt.platform.cache.BasicCache@5c933438)
22:03:54.045 T [qtp440938038-12] org.eclipse.scout.apps.helloworld.server.security.ServerAccessControlService getRolesCache(ServerAccessControlService.java:242) - entry
22:03:54.048 T [qtp440938038-12] org.eclipse.scout.apps.helloworld.server.security.ServerAccessControlService getRolesCache(ServerAccessControlService.java:252) - exit with (org.eclipse.scout.rt.platform.cache.BasicCache@5c933438)
22:03:54.049 T [qtp440938038-12] org.eclipse.scout.apps.helloworld.server.security.ServerAccessControlService createRolesCache(ServerAccessControlService.java:256) - entry
22:03:54.058 T [qtp440938038-12] io.poc.scout.MyCacheRegistryService register(MyCacheRegistryService.java:23) - entry with (org.eclipse.scout.rt.platform.cache.BasicCache@113fed2c)
22:03:54.059 D [qtp440938038-12] io.poc.scout.MyCacheRegistryService register(MyCacheRegistryService.java:26) - cache already registered: org.eclipse.scout.apps.helloworld.server.security.ServerAccessControlService.CACHE
22:03:54.059 D [qtp440938038-12] io.poc.scout.MyCacheRegistryService register(MyCacheRegistryService.java:31) - keys: [org.eclipse.scout.apps.helloworld.server.security.ServerAccessControlService.CACHE, org.eclipse.scout.rt.shared.services.common.code.CodeService]
22:03:54.064 T [qtp440938038-12] io.poc.scout.MyCacheRegistryService register(MyCacheRegistryService.java:33) - exit
22:03:54.068 T [qtp440938038-12] org.eclipse.scout.apps.helloworld.server.security.ServerAccessControlService createRolesCache(ServerAccessControlService.java:267) - exit with (org.eclipse.scout.rt.platform.cache.BasicCache@113fed2c)
22:03:54.072 T [qtp440938038-12] org.eclipse.scout.apps.helloworld.server.security.ServerAccessControlService getRolesCache(ServerAccessControlService.java:242) - entry
22:03:54.073 T [qtp440938038-12] org.eclipse.scout.apps.helloworld.server.security.ServerAccessControlService getRolesCache(ServerAccessControlService.java:252) - exit with (org.eclipse.scout.rt.platform.cache.BasicCache@113fed2c)
22:03:54.074 D [qtp440938038-12] org.eclipse.scout.apps.helloworld.server.security.ServerAccessControlService <init>(ServerAccessControlService.java:56) - c1: org.eclipse.scout.rt.platform.cache.BasicCache@5c933438
22:03:54.075 D [qtp440938038-12] org.eclipse.scout.apps.helloworld.server.security.ServerAccessControlService <init>(ServerAccessControlService.java:57) - c2: org.eclipse.scout.rt.platform.cache.BasicCache@5c933438
22:03:54.076 D [qtp440938038-12] org.eclipse.scout.apps.helloworld.server.security.ServerAccessControlService <init>(ServerAccessControlService.java:58) - c3: org.eclipse.scout.rt.platform.cache.BasicCache@113fed2c
22:03:54.081 D [qtp440938038-12] org.eclipse.scout.apps.helloworld.server.security.ServerAccessControlService <init>(ServerAccessControlService.java:59) - c4: org.eclipse.scout.rt.platform.cache.BasicCache@113fed2c
22:03:54.083 T [qtp440938038-12] org.eclipse.scout.apps.helloworld.server.security.ServerAccessControlService <init>(ServerAccessControlService.java:61) - exit
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03525 seconds