001package org.apache.commons.jcs3.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.concurrent.ConcurrentHashMap;
023
024import org.apache.commons.jcs3.auxiliary.AbstractAuxiliaryCacheMonitor;
025import org.apache.commons.jcs3.auxiliary.lateral.socket.tcp.LateralTCPCacheFactory;
026import org.apache.commons.jcs3.auxiliary.lateral.socket.tcp.behavior.ITCPLateralCacheAttributes;
027import org.apache.commons.jcs3.engine.CacheStatus;
028import org.apache.commons.jcs3.engine.ZombieCacheServiceNonLocal;
029import org.apache.commons.jcs3.engine.behavior.ICacheServiceNonLocal;
030
031/**
032 * Used to monitor and repair any failed connection for the lateral cache service. By default the
033 * monitor operates in a failure driven mode. That is, it goes into a wait state until there is an
034 * error. Upon the notification of a connection error, the monitor changes to operate in a time
035 * driven mode. That is, it attempts to recover the connections on a periodic basis. When all failed
036 * connections are restored, it changes back to the failure driven mode.
037 */
038public class LateralCacheMonitor extends AbstractAuxiliaryCacheMonitor
039{
040    /**
041     * Map of caches to monitor
042     */
043    private final ConcurrentHashMap<String, LateralCacheNoWait<Object, Object>> caches;
044
045    /**
046     * Reference to the factory
047     */
048    private final LateralTCPCacheFactory factory;
049
050    /**
051     * Allows close classes, ie testers to set the idle period to something testable.
052     * <p>
053     * @param idlePeriod
054     * 
055     * @deprecated Use setIdlePeriod()
056     */
057    @Deprecated
058    protected static void forceShortIdlePeriod( final long idlePeriod )
059    {
060        LateralCacheMonitor.setIdlePeriod(idlePeriod);
061    }
062
063    /**
064     * Constructor for the LateralCacheMonitor object
065     * <p>
066     * It's the clients responsibility to decide how many of these there will be.
067     *
068     * @param factory a reference to the factory that manages the service instances
069     */
070    public LateralCacheMonitor(final LateralTCPCacheFactory factory)
071    {
072        super("JCS-LateralCacheMonitor");
073        this.factory = factory;
074        this.caches = new ConcurrentHashMap<>();
075        setIdlePeriod(20000L);
076    }
077
078    /**
079     * Add a cache to be monitored
080     *
081     * @param cache the cache
082     */
083    @SuppressWarnings("unchecked") // common map for all caches
084    public void addCache(final LateralCacheNoWait<?, ?> cache)
085    {
086        this.caches.put(cache.getCacheName(), (LateralCacheNoWait<Object, Object>)cache);
087
088        // if not yet started, go ahead
089        if (this.getState() == Thread.State.NEW)
090        {
091            this.start();
092        }
093    }
094
095    /**
096     * Clean up all resources before shutdown
097     */
098    @Override
099    public void dispose()
100    {
101        this.caches.clear();
102    }
103
104    /**
105     * Main processing method for the LateralCacheMonitor object
106     */
107    @Override
108    public void doWork()
109    {
110        // Monitor each cache instance one after the other.
111        log.info( "Number of caches to monitor = " + caches.size() );
112
113        caches.forEach((cacheName, cache) -> {
114
115            if (cache.getStatus() == CacheStatus.ERROR)
116            {
117                log.info( "Found LateralCacheNoWait in error, " + cacheName );
118
119                final ITCPLateralCacheAttributes lca =
120                        (ITCPLateralCacheAttributes) cache.getAuxiliaryCacheAttributes();
121
122                // Get service instance
123                final ICacheServiceNonLocal<Object, Object> cacheService =
124                        factory.getCSNLInstance(lca, cache.getElementSerializer());
125
126                // If we can't fix them, just skip and re-try in the
127                // next round.
128                if (!(cacheService instanceof ZombieCacheServiceNonLocal))
129                {
130                    cache.fixCache(cacheService);
131                }
132            }
133        });
134    }
135}