001package org.apache.commons.jcs.auxiliary.lateral;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.Map;
023import java.util.concurrent.ConcurrentHashMap;
024
025import org.apache.commons.jcs.auxiliary.AbstractAuxiliaryCacheMonitor;
026import org.apache.commons.jcs.auxiliary.lateral.socket.tcp.LateralTCPCacheFactory;
027import org.apache.commons.jcs.auxiliary.lateral.socket.tcp.behavior.ITCPLateralCacheAttributes;
028import org.apache.commons.jcs.engine.CacheStatus;
029import org.apache.commons.jcs.engine.ZombieCacheServiceNonLocal;
030import org.apache.commons.jcs.engine.behavior.ICacheServiceNonLocal;
031
032/**
033 * Used to monitor and repair any failed connection for the lateral cache service. By default the
034 * monitor operates in a failure driven mode. That is, it goes into a wait state until there is an
035 * error. Upon the notification of a connection error, the monitor changes to operate in a time
036 * driven mode. That is, it attempts to recover the connections on a periodic basis. When all failed
037 * connections are restored, it changes back to the failure driven mode.
038 */
039public class LateralCacheMonitor extends AbstractAuxiliaryCacheMonitor
040{
041    /**
042     * Map of caches to monitor
043     */
044    private ConcurrentHashMap<String, LateralCacheNoWait<?, ?>> caches;
045
046    /**
047     * Reference to the factory
048     */
049    private LateralTCPCacheFactory factory;
050
051    /**
052     * Allows close classes, ie testers to set the idle period to something testable.
053     * <p>
054     * @param idlePeriod
055     */
056    protected static void forceShortIdlePeriod( long idlePeriod )
057    {
058        LateralCacheMonitor.idlePeriod = idlePeriod;
059    }
060
061    /**
062     * Constructor for the LateralCacheMonitor object
063     * <p>
064     * It's the clients responsibility to decide how many of these there will be.
065     *
066     * @param factory a reference to the factory that manages the service instances
067     */
068    public LateralCacheMonitor(LateralTCPCacheFactory factory)
069    {
070        super("JCS-LateralCacheMonitor");
071        this.factory = factory;
072        this.caches = new ConcurrentHashMap<String, LateralCacheNoWait<?,?>>();
073        setIdlePeriod(20000L);
074    }
075
076    /**
077     * Add a cache to be monitored
078     *
079     * @param cache the cache
080     */
081    public void addCache(LateralCacheNoWait<?, ?> cache)
082    {
083        this.caches.put(cache.getCacheName(), cache);
084
085        // if not yet started, go ahead
086        if (this.getState() == Thread.State.NEW)
087        {
088            this.start();
089        }
090    }
091
092    /**
093     * Clean up all resources before shutdown
094     */
095    @Override
096    public void dispose()
097    {
098        this.caches.clear();
099    }
100
101    /**
102     * Main processing method for the LateralCacheMonitor object
103     */
104    @Override
105    public void doWork()
106    {
107        // Monitor each cache instance one after the other.
108        log.info( "Number of caches to monitor = " + caches.size() );
109        //for
110        for (Map.Entry<String, LateralCacheNoWait<?, ?>> entry : caches.entrySet())
111        {
112            String cacheName = entry.getKey();
113
114            @SuppressWarnings("unchecked") // Downcast to match service
115            LateralCacheNoWait<Object, Object> c = (LateralCacheNoWait<Object, Object>) entry.getValue();
116            if ( c.getStatus() == CacheStatus.ERROR )
117            {
118                log.info( "Found LateralCacheNoWait in error, " + cacheName );
119
120                ITCPLateralCacheAttributes lca = (ITCPLateralCacheAttributes)c.getAuxiliaryCacheAttributes();
121
122                // Get service instance
123                ICacheServiceNonLocal<Object, Object> cacheService = factory.getCSNLInstance(lca);
124
125                // If we can't fix them, just skip and re-try in the
126                // next round.
127                if (cacheService instanceof ZombieCacheServiceNonLocal)
128                {
129                    continue;
130                }
131
132                c.fixCache(cacheService);
133            }
134        }
135    }
136}