并发环境中的Ehcache ReentrantReadWriteLock

时间:2017-02-19 20:55:08

标签: java spring multithreading caching ehcache

我在使用ehcache 2.1.0库的Websphere 7.x的高效环境中遇到了问题。 Web容器的线程都在等待协商或插入缓存。

这是所有webcontainer线程待处理时的转储:

NULL
3XMTHREADINFO      "WebContainer : 11" J9VMThread:0x00000000C7C10300, j9thread_t:0x0000010043913FB0, java/lang/Thread:0x00000000507623F0, state:P, prio=5
sun.misc.Unsafe.park(Native Method)
java.util.concurrent.locks.LockSupport.park(LockSupport.java:182)
java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:822) < 2 >
java.util.concurrent.locks.ReentrantReadWriteLock$WriteLock.lock(ReentrantReadWriteLock.java:907)
net.sf.ehcache.store.compound.Segment.put(Segment.java:402)
net.sf.ehcache.store.compound.CompoundStore.put(CompoundStore.java:132)
net.sf.ehcache.Cache.putInternal(Cache.java:1247) < 2 >
org.springframework.cache.ehcache.EhCacheCache.put(EhCacheCache.java:70)
xxx.yyy.fac.security.userdetails.GaiaLdapAuthoritiesPopulator.putElementCache(GaiaLdapAuthoritiesPopulator.java:466) < 4 >
xxx.yyy.dgtp.gaiafrontend.core.filters.preauth.GaiaGrantedAuthoritiesWebAuthenticationDetails.buildDetails(GaiaGrantedAuthoritiesWebAuthenticationDetails.java:32) < 1 >
org.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter.doAuthenticate(AbstractPreAuthenticatedProcessingFilter.java:114) < 1 >
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:184) < 1 >
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) < 1 >
com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:190)
com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:125)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:322)
xxx.yyy.dgtp.gaiafrontend.core.web.filters.JsonDeserializerFilter.doFilterChain(JsonDeserializerFilter.java:109) < 1 >
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
xxx.yyy.dgtp.gaiafrontend.core.filters.userinfo.UserInfoFilter.followWithTheRequestChain(UserInfoFilter.java:106) < 1 >
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
xxx.yyy.dgtp.gaia.commons.web.filters.RequestResponseWrapperFilter.doFilter(RequestResponseWrapperFilter.java:69)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:83)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
xxx.yyy.dgtp.gaiafrontend.core.web.filters.ResponseHeadersFilter.doFilter(ResponseHeadersFilter.java:55)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:184) < 1 >
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) < 1 >
com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:190)
com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:125)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:190)
com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:125) < 1 >
com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:908)
com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:939) < 1 >
com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:181)
com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3994)
com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:276)
com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:945)
com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1592)
com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:191)
com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:453) < 3 >
com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214) < 1 >
com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175)
com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
com.ibm.io.async.AsyncChannelFuture$1.run(AsyncChannelFuture.java:205)
com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1660)     

我在ehcache上使用spring作为抽象层,以编程方式获取/放入缓存的代码没什么特别的。

缓存的配置如下:

    @Bean
    public EhCacheManagerFactoryBean cacheFactoryBean() {

        EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
        ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource("gaia-cache-ldap.xml"));
        return ehCacheManagerFactoryBean;

    }

    @Bean
    public CacheManager cacheManagerLdap() {
        CacheManager cacheManager = new EhCacheCacheManager(cacheFactoryBean().getObject());
        return cacheManager;

    }

可以访问缓存的代码:

 private void getMemberOfRecursive(String group, ConcurrentMap<String, String> groupsCollector) {

        if (group != null) {
            if (existInCache(group)) {
                log.debug("Group: {} exist in cache. No query executing", group);
                groupsCollector.put(group, group);
                Set<String> groups = (Set<String>) getElementCache(group).get();
                for (String newGroup : groups) {
                    getMemberOfRecursive(newGroup, groupsCollector);
                }
            } else {
                String cn = getCnFromDn(group);
                String filter = MessageFormat.format(getFilterGroupRecursive(), cn);
                String baseDN = group.substring(group.indexOf(",") + 1);
                groupsCollector.put(group, group);

                log.debug("Executing recursive  query with baseDN: {} " +
                        " and filter {}: ", baseDN, filter);
                Set<String> groups =
                        getLdapTemplate().searchForSingleAttributeValues(
                                baseDN, filter, new String[]{}, getRetrievesAttributes());
                putElementCache(group, groups);
                for (String newGroup : groups) {
                    getMemberOfRecursive(newGroup, groupsCollector);
                }
            }

        }

Ehcache配置文件:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false"    monitoring="autodetect"       dynamicConfig="true">
    <diskStore path="java.io.tmpdir" />
    <defaultCache maxElementsInMemory="3000" eternal="false" timeToIdleSeconds="1200"
                  timeToLiveSeconds="1200" overflowToDisk="true" maxElementsOnDisk="10000"
                  diskPersistent="false" diskExpiryThreadIntervalSeconds="120"
                  memoryStoreEvictionPolicy="LRU"/>
    <cache name="groupsldap" maxElementsInMemory="3000" eternal="true" overflowToDisk="false"
           memoryStoreEvictionPolicy="LRU"/>

</ehcache>

1 个答案:

答案 0 :(得分:0)

最后,可以重现生产错误。此错误似乎与至少存在于Active Directory组用户中的交叉组有关。在上周未启用缓存的情况下也会跳过此错误,并且能够更密切地分析它,发现存在stackoverflower错误,在这种情况下会导致ldap连接池在处于等待状态的线程中耗尽。

一旦发现它是由于这个问题而且包含的修复程序消除了以前访问过的组,那么这次使用缓存已经在本地测试中再现了我们必须使用类似的行为来填充Web容器的线程池在生产中检测到的。