001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.cache;
003
004import java.io.File;
005import java.io.IOException;
006import java.nio.channels.FileChannel;
007import java.nio.channels.FileLock;
008import java.nio.file.StandardOpenOption;
009import java.util.Arrays;
010import java.util.Properties;
011import java.util.logging.Handler;
012import java.util.logging.Level;
013import java.util.logging.LogRecord;
014import java.util.logging.Logger;
015import java.util.logging.SimpleFormatter;
016
017import org.apache.commons.jcs.access.CacheAccess;
018import org.apache.commons.jcs.auxiliary.AuxiliaryCache;
019import org.apache.commons.jcs.auxiliary.AuxiliaryCacheFactory;
020import org.apache.commons.jcs.auxiliary.disk.behavior.IDiskCacheAttributes;
021import org.apache.commons.jcs.auxiliary.disk.block.BlockDiskCacheAttributes;
022import org.apache.commons.jcs.auxiliary.disk.block.BlockDiskCacheFactory;
023import org.apache.commons.jcs.auxiliary.disk.indexed.IndexedDiskCacheAttributes;
024import org.apache.commons.jcs.auxiliary.disk.indexed.IndexedDiskCacheFactory;
025import org.apache.commons.jcs.engine.CompositeCacheAttributes;
026import org.apache.commons.jcs.engine.behavior.ICompositeCacheAttributes.DiskUsagePattern;
027import org.apache.commons.jcs.engine.control.CompositeCache;
028import org.apache.commons.jcs.engine.control.CompositeCacheManager;
029import org.apache.commons.jcs.utils.serialization.StandardSerializer;
030import org.openstreetmap.josm.data.preferences.BooleanProperty;
031import org.openstreetmap.josm.data.preferences.IntegerProperty;
032import org.openstreetmap.josm.spi.preferences.Config;
033import org.openstreetmap.josm.tools.Logging;
034import org.openstreetmap.josm.tools.Utils;
035
036/**
037 * Wrapper class for JCS Cache. Sets some sane environment and returns instances of cache objects.
038 * Static configuration for now assumes some small LRU cache in memory and larger LRU cache on disk
039 *
040 * @author Wiktor Niesiobędzki
041 * @since 8168
042 */
043public final class JCSCacheManager {
044    private static volatile CompositeCacheManager cacheManager;
045    private static final long maxObjectTTL = -1;
046    private static final String PREFERENCE_PREFIX = "jcs.cache";
047    public static final BooleanProperty USE_BLOCK_CACHE = new BooleanProperty(PREFERENCE_PREFIX + ".use_block_cache", true);
048
049    private static final AuxiliaryCacheFactory DISK_CACHE_FACTORY =
050            USE_BLOCK_CACHE.get() ? new BlockDiskCacheFactory() : new IndexedDiskCacheFactory();
051    private static FileLock cacheDirLock;
052
053    /**
054     * default objects to be held in memory by JCS caches (per region)
055     */
056    public static final IntegerProperty DEFAULT_MAX_OBJECTS_IN_MEMORY = new IntegerProperty(PREFERENCE_PREFIX + ".max_objects_in_memory", 1000);
057
058    private static final Logger jcsLog;
059
060    static {
061        // raising logging level gives ~500x performance gain
062        // http://westsworld.dk/blog/2008/01/jcs-and-performance/
063        jcsLog = Logger.getLogger("org.apache.commons.jcs");
064        try {
065            jcsLog.setLevel(Level.INFO);
066            jcsLog.setUseParentHandlers(false);
067            // we need a separate handler from Main's, as we downgrade LEVEL.INFO to DEBUG level
068            Arrays.stream(jcsLog.getHandlers()).forEach(jcsLog::removeHandler);
069            jcsLog.addHandler(new Handler() {
070                final SimpleFormatter formatter = new SimpleFormatter();
071
072                @Override
073                public void publish(LogRecord record) {
074                    String msg = formatter.formatMessage(record);
075                    if (record.getLevel().intValue() >= Level.SEVERE.intValue()) {
076                        Logging.error(msg);
077                    } else if (record.getLevel().intValue() >= Level.WARNING.intValue()) {
078                        Logging.warn(msg);
079                        // downgrade INFO level to debug, as JCS is too verbose at INFO level
080                    } else if (record.getLevel().intValue() >= Level.INFO.intValue()) {
081                        Logging.debug(msg);
082                    } else {
083                        Logging.trace(msg);
084                    }
085                }
086
087                @Override
088                public void flush() {
089                    // nothing to be done on flush
090                }
091
092                @Override
093                public void close() {
094                    // nothing to be done on close
095                }
096            });
097        } catch (SecurityException e) {
098            Logging.log(Logging.LEVEL_ERROR, "Unable to configure JCS logs", e);
099        }
100    }
101
102    private JCSCacheManager() {
103        // Hide implicit public constructor for utility classes
104    }
105
106    @SuppressWarnings("resource")
107    private static void initialize() {
108        File cacheDir = new File(Config.getDirs().getCacheDirectory(true), "jcs");
109
110        try {
111            if (!cacheDir.exists() && !cacheDir.mkdirs()) {
112                Logging.warn("Cache directory " + cacheDir.toString() + " does not exists and could not create it");
113            } else {
114                File cacheDirLockPath = new File(cacheDir, ".lock");
115                try {
116                    if (!cacheDirLockPath.exists() && !cacheDirLockPath.createNewFile()) {
117                        Logging.warn("Cannot create cache dir lock file");
118                    }
119                    cacheDirLock = FileChannel.open(cacheDirLockPath.toPath(), StandardOpenOption.WRITE).tryLock();
120
121                    if (cacheDirLock == null)
122                        Logging.warn("Cannot lock cache directory. Will not use disk cache");
123                } catch (IOException e) {
124                    Logging.log(Logging.LEVEL_WARN, "Cannot create cache dir \"" + cacheDirLockPath + "\" lock file:", e);
125                    Logging.warn("Will not use disk cache");
126                }
127            }
128        } catch (SecurityException e) {
129            Logging.log(Logging.LEVEL_WARN, "Unable to configure disk cache. Will not use it", e);
130        }
131
132        // this could be moved to external file
133        Properties props = new Properties();
134        // these are default common to all cache regions
135        // use of auxiliary cache and sizing of the caches is done with giving proper getCache(...) params
136        // CHECKSTYLE.OFF: SingleSpaceSeparator
137        props.setProperty("jcs.default.cacheattributes",                      CompositeCacheAttributes.class.getCanonicalName());
138        props.setProperty("jcs.default.cacheattributes.MaxObjects",           DEFAULT_MAX_OBJECTS_IN_MEMORY.get().toString());
139        props.setProperty("jcs.default.cacheattributes.UseMemoryShrinker",    "true");
140        props.setProperty("jcs.default.cacheattributes.DiskUsagePatternName", "UPDATE"); // store elements on disk on put
141        props.setProperty("jcs.default.elementattributes",                    CacheEntryAttributes.class.getCanonicalName());
142        props.setProperty("jcs.default.elementattributes.IsEternal",          "false");
143        props.setProperty("jcs.default.elementattributes.MaxLife",            Long.toString(maxObjectTTL));
144        props.setProperty("jcs.default.elementattributes.IdleTime",           Long.toString(maxObjectTTL));
145        props.setProperty("jcs.default.elementattributes.IsSpool",            "true");
146        // CHECKSTYLE.ON: SingleSpaceSeparator
147        try {
148            CompositeCacheManager cm = CompositeCacheManager.getUnconfiguredInstance();
149            cm.configure(props);
150            cacheManager = cm;
151        } catch (SecurityException e) {
152            Logging.log(Logging.LEVEL_WARN, "Unable to initialize JCS", e);
153        }
154    }
155
156    /**
157     * Returns configured cache object for named cache region
158     * @param <K> key type
159     * @param <V> value type
160     * @param cacheName region name
161     * @return cache access object
162     */
163    public static <K, V> CacheAccess<K, V> getCache(String cacheName) {
164        return getCache(cacheName, DEFAULT_MAX_OBJECTS_IN_MEMORY.get().intValue(), 0, null);
165    }
166
167    /**
168     * Returns configured cache object with defined limits of memory cache and disk cache
169     * @param <K> key type
170     * @param <V> value type
171     * @param cacheName         region name
172     * @param maxMemoryObjects  number of objects to keep in memory
173     * @param maxDiskObjects    maximum size of the objects stored on disk in kB
174     * @param cachePath         path to disk cache. if null, no disk cache will be created
175     * @return cache access object
176     */
177    public static <K, V> CacheAccess<K, V> getCache(String cacheName, int maxMemoryObjects, int maxDiskObjects, String cachePath) {
178        if (cacheManager != null)
179            return getCacheInner(cacheName, maxMemoryObjects, maxDiskObjects, cachePath);
180
181        synchronized (JCSCacheManager.class) {
182            if (cacheManager == null)
183                initialize();
184            return cacheManager != null ? getCacheInner(cacheName, maxMemoryObjects, maxDiskObjects, cachePath) : null;
185        }
186    }
187
188    @SuppressWarnings("unchecked")
189    private static <K, V> CacheAccess<K, V> getCacheInner(String cacheName, int maxMemoryObjects, int maxDiskObjects, String cachePath) {
190        CompositeCache<K, V> cc = cacheManager.getCache(cacheName, getCacheAttributes(maxMemoryObjects));
191
192        if (cachePath != null && cacheDirLock != null) {
193            IDiskCacheAttributes diskAttributes = getDiskCacheAttributes(maxDiskObjects, cachePath, cacheName);
194            try {
195                if (cc.getAuxCaches().length == 0) {
196                    cc.setAuxCaches(new AuxiliaryCache[]{DISK_CACHE_FACTORY.createCache(
197                            diskAttributes, cacheManager, null, new StandardSerializer())});
198                }
199            } catch (Exception e) { // NOPMD
200                // in case any error in setting auxiliary cache, do not use disk cache at all - only memory
201                cc.setAuxCaches(new AuxiliaryCache[0]);
202                Logging.debug(e);
203            }
204        }
205        return new CacheAccess<>(cc);
206    }
207
208    /**
209     * Close all files to ensure, that all indexes and data are properly written
210     */
211    public static void shutdown() {
212        // use volatile semantics to get consistent object
213        CompositeCacheManager localCacheManager = cacheManager;
214        if (localCacheManager != null) {
215            localCacheManager.shutDown();
216        }
217    }
218
219    private static IDiskCacheAttributes getDiskCacheAttributes(int maxDiskObjects, String cachePath, String cacheName) {
220        IDiskCacheAttributes ret;
221        removeStaleFiles(cachePath + File.separator + cacheName, USE_BLOCK_CACHE.get() ? "_INDEX_v2" : "_BLOCK_v2");
222        String newCacheName = cacheName + (USE_BLOCK_CACHE.get() ? "_BLOCK_v2" : "_INDEX_v2");
223
224        if (USE_BLOCK_CACHE.get()) {
225            BlockDiskCacheAttributes blockAttr = new BlockDiskCacheAttributes();
226            /*
227             * BlockDiskCache never optimizes the file, so when file size is reduced, it will never be truncated to desired size.
228             *
229             * If for some mysterious reason, file size is greater than the value set in preferences, just use the whole file. If the user
230             * wants to reduce the file size, (s)he may just go to preferences and there it should be handled (by removing old file)
231             */
232            File diskCacheFile = new File(cachePath + File.separator + newCacheName + ".data");
233            if (diskCacheFile.exists()) {
234                blockAttr.setMaxKeySize((int) Math.max(maxDiskObjects, diskCacheFile.length()/1024));
235            } else {
236                blockAttr.setMaxKeySize(maxDiskObjects);
237            }
238            blockAttr.setBlockSizeBytes(4096); // use 4k blocks
239            ret = blockAttr;
240        } else {
241            IndexedDiskCacheAttributes indexAttr = new IndexedDiskCacheAttributes();
242            indexAttr.setMaxKeySize(maxDiskObjects);
243            ret = indexAttr;
244        }
245        ret.setDiskLimitType(IDiskCacheAttributes.DiskLimitType.SIZE);
246        File path = new File(cachePath);
247        if (!path.exists() && !path.mkdirs()) {
248            Logging.warn("Failed to create cache path: {0}", cachePath);
249        } else {
250            ret.setDiskPath(cachePath);
251        }
252        ret.setCacheName(newCacheName);
253
254        return ret;
255    }
256
257    private static void removeStaleFiles(String basePathPart, String suffix) {
258        deleteCacheFiles(basePathPart + suffix);
259    }
260
261    private static void deleteCacheFiles(String basePathPart) {
262        Utils.deleteFileIfExists(new File(basePathPart + ".key"));
263        Utils.deleteFileIfExists(new File(basePathPart + ".data"));
264    }
265
266    private static CompositeCacheAttributes getCacheAttributes(int maxMemoryElements) {
267        CompositeCacheAttributes ret = new CompositeCacheAttributes();
268        ret.setMaxObjects(maxMemoryElements);
269        ret.setDiskUsagePattern(DiskUsagePattern.UPDATE);
270        return ret;
271    }
272}