[8168] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.data.cache;
|
---|
| 3 |
|
---|
| 4 | import java.io.File;
|
---|
| 5 | import java.io.IOException;
|
---|
[13204] | 6 | import java.nio.channels.FileChannel;
|
---|
[8186] | 7 | import java.nio.channels.FileLock;
|
---|
[13204] | 8 | import java.nio.file.StandardOpenOption;
|
---|
[11169] | 9 | import java.util.Arrays;
|
---|
[8168] | 10 | import java.util.Properties;
|
---|
| 11 | import java.util.logging.Handler;
|
---|
| 12 | import java.util.logging.Level;
|
---|
| 13 | import java.util.logging.LogRecord;
|
---|
| 14 | import java.util.logging.Logger;
|
---|
[8991] | 15 | import java.util.logging.SimpleFormatter;
|
---|
[8168] | 16 |
|
---|
| 17 | import org.apache.commons.jcs.access.CacheAccess;
|
---|
| 18 | import org.apache.commons.jcs.auxiliary.AuxiliaryCache;
|
---|
[8598] | 19 | import org.apache.commons.jcs.auxiliary.AuxiliaryCacheFactory;
|
---|
| 20 | import org.apache.commons.jcs.auxiliary.disk.behavior.IDiskCacheAttributes;
|
---|
[10169] | 21 | import org.apache.commons.jcs.auxiliary.disk.block.BlockDiskCacheAttributes;
|
---|
| 22 | import org.apache.commons.jcs.auxiliary.disk.block.BlockDiskCacheFactory;
|
---|
[10323] | 23 | import org.apache.commons.jcs.auxiliary.disk.indexed.IndexedDiskCacheAttributes;
|
---|
| 24 | import org.apache.commons.jcs.auxiliary.disk.indexed.IndexedDiskCacheFactory;
|
---|
[8168] | 25 | import org.apache.commons.jcs.engine.CompositeCacheAttributes;
|
---|
| 26 | import org.apache.commons.jcs.engine.behavior.ICompositeCacheAttributes.DiskUsagePattern;
|
---|
| 27 | import org.apache.commons.jcs.engine.control.CompositeCache;
|
---|
| 28 | import org.apache.commons.jcs.engine.control.CompositeCacheManager;
|
---|
| 29 | import org.apache.commons.jcs.utils.serialization.StandardSerializer;
|
---|
[10323] | 30 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
[8168] | 31 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
[12855] | 32 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
[12620] | 33 | import org.openstreetmap.josm.tools.Logging;
|
---|
[10406] | 34 | import org.openstreetmap.josm.tools.Utils;
|
---|
[8168] | 35 |
|
---|
| 36 | /**
|
---|
| 37 | * Wrapper class for JCS Cache. Sets some sane environment and returns instances of cache objects.
|
---|
| 38 | * Static configuration for now assumes some small LRU cache in memory and larger LRU cache on disk
|
---|
[10723] | 39 | *
|
---|
| 40 | * @author Wiktor Niesiobędzki
|
---|
[8318] | 41 | * @since 8168
|
---|
[8168] | 42 | */
|
---|
[8514] | 43 | public final class JCSCacheManager {
|
---|
[8840] | 44 | private static volatile CompositeCacheManager cacheManager;
|
---|
[12811] | 45 | private static final long maxObjectTTL = -1;
|
---|
[8318] | 46 | private static final String PREFERENCE_PREFIX = "jcs.cache";
|
---|
[10652] | 47 | public static final BooleanProperty USE_BLOCK_CACHE = new BooleanProperty(PREFERENCE_PREFIX + ".use_block_cache", true);
|
---|
[10323] | 48 |
|
---|
[12537] | 49 | private static final AuxiliaryCacheFactory DISK_CACHE_FACTORY =
|
---|
[10323] | 50 | USE_BLOCK_CACHE.get() ? new BlockDiskCacheFactory() : new IndexedDiskCacheFactory();
|
---|
[8840] | 51 | private static FileLock cacheDirLock;
|
---|
[8168] | 52 |
|
---|
| 53 | /**
|
---|
| 54 | * default objects to be held in memory by JCS caches (per region)
|
---|
| 55 | */
|
---|
[10378] | 56 | public static final IntegerProperty DEFAULT_MAX_OBJECTS_IN_MEMORY = new IntegerProperty(PREFERENCE_PREFIX + ".max_objects_in_memory", 1000);
|
---|
[8168] | 57 |
|
---|
[12811] | 58 | private static final Logger jcsLog;
|
---|
[8487] | 59 |
|
---|
[12811] | 60 | static {
|
---|
[8168] | 61 | // raising logging level gives ~500x performance gain
|
---|
| 62 | // http://westsworld.dk/blog/2008/01/jcs-and-performance/
|
---|
[12811] | 63 | jcsLog = Logger.getLogger("org.apache.commons.jcs");
|
---|
[13647] | 64 | try {
|
---|
| 65 | jcsLog.setLevel(Level.INFO);
|
---|
| 66 | jcsLog.setUseParentHandlers(false);
|
---|
| 67 | // we need a separate handler from Main's, as we downgrade LEVEL.INFO to DEBUG level
|
---|
| 68 | Arrays.stream(jcsLog.getHandlers()).forEach(jcsLog::removeHandler);
|
---|
| 69 | jcsLog.addHandler(new Handler() {
|
---|
| 70 | final SimpleFormatter formatter = new SimpleFormatter();
|
---|
[8991] | 71 |
|
---|
[13647] | 72 | @Override
|
---|
| 73 | public void publish(LogRecord record) {
|
---|
| 74 | String msg = formatter.formatMessage(record);
|
---|
| 75 | if (record.getLevel().intValue() >= Level.SEVERE.intValue()) {
|
---|
| 76 | Logging.error(msg);
|
---|
| 77 | } else if (record.getLevel().intValue() >= Level.WARNING.intValue()) {
|
---|
| 78 | Logging.warn(msg);
|
---|
| 79 | // downgrade INFO level to debug, as JCS is too verbose at INFO level
|
---|
| 80 | } else if (record.getLevel().intValue() >= Level.INFO.intValue()) {
|
---|
| 81 | Logging.debug(msg);
|
---|
| 82 | } else {
|
---|
| 83 | Logging.trace(msg);
|
---|
| 84 | }
|
---|
[8168] | 85 | }
|
---|
| 86 |
|
---|
[13647] | 87 | @Override
|
---|
| 88 | public void flush() {
|
---|
| 89 | // nothing to be done on flush
|
---|
| 90 | }
|
---|
[8168] | 91 |
|
---|
[13647] | 92 | @Override
|
---|
| 93 | public void close() {
|
---|
| 94 | // nothing to be done on close
|
---|
| 95 | }
|
---|
| 96 | });
|
---|
| 97 | } catch (SecurityException e) {
|
---|
| 98 | Logging.log(Logging.LEVEL_ERROR, "Unable to configure JCS logs", e);
|
---|
| 99 | }
|
---|
[12813] | 100 | }
|
---|
[8168] | 101 |
|
---|
[12811] | 102 | private JCSCacheManager() {
|
---|
| 103 | // Hide implicit public constructor for utility classes
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | @SuppressWarnings("resource")
|
---|
[13647] | 107 | private static void initialize() {
|
---|
[12856] | 108 | File cacheDir = new File(Config.getDirs().getCacheDirectory(true), "jcs");
|
---|
[12811] | 109 |
|
---|
[13649] | 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.warn("Cannot create cache dir \"" + cacheDirLockPath.toString() + "\" lock file: " + e.toString());
|
---|
| 125 | Logging.warn("Will not use disk cache");
|
---|
[13643] | 126 | }
|
---|
| 127 | }
|
---|
[13649] | 128 | } catch (SecurityException e) {
|
---|
| 129 | Logging.log(Logging.LEVEL_WARN, "Unable to configure disk cache. Will not use it", e);
|
---|
[12811] | 130 | }
|
---|
[13649] | 131 |
|
---|
[8168] | 132 | // this could be moved to external file
|
---|
| 133 | Properties props = new Properties();
|
---|
| 134 | // these are default common to all cache regions
|
---|
[13647] | 135 | // use of auxiliary cache and sizing of the caches is done with giving proper getCache(...) params
|
---|
[10378] | 136 | // CHECKSTYLE.OFF: SingleSpaceSeparator
|
---|
[8509] | 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");
|
---|
[10378] | 146 | // CHECKSTYLE.ON: SingleSpaceSeparator
|
---|
[13649] | 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 | }
|
---|
[8168] | 154 | }
|
---|
| 155 |
|
---|
| 156 | /**
|
---|
| 157 | * Returns configured cache object for named cache region
|
---|
[9246] | 158 | * @param <K> key type
|
---|
| 159 | * @param <V> value type
|
---|
[8168] | 160 | * @param cacheName region name
|
---|
| 161 | * @return cache access object
|
---|
| 162 | */
|
---|
[13643] | 163 | public static <K, V> CacheAccess<K, V> getCache(String cacheName) {
|
---|
[8168] | 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
|
---|
[9246] | 169 | * @param <K> key type
|
---|
| 170 | * @param <V> value type
|
---|
[8168] | 171 | * @param cacheName region name
|
---|
| 172 | * @param maxMemoryObjects number of objects to keep in memory
|
---|
[8598] | 173 | * @param maxDiskObjects maximum size of the objects stored on disk in kB
|
---|
[8168] | 174 | * @param cachePath path to disk cache. if null, no disk cache will be created
|
---|
| 175 | * @return cache access object
|
---|
| 176 | */
|
---|
[13643] | 177 | public static <K, V> CacheAccess<K, V> getCache(String cacheName, int maxMemoryObjects, int maxDiskObjects, String cachePath) {
|
---|
[8168] | 178 | if (cacheManager != null)
|
---|
| 179 | return getCacheInner(cacheName, maxMemoryObjects, maxDiskObjects, cachePath);
|
---|
| 180 |
|
---|
| 181 | synchronized (JCSCacheManager.class) {
|
---|
| 182 | if (cacheManager == null)
|
---|
| 183 | initialize();
|
---|
[13649] | 184 | return cacheManager != null ? getCacheInner(cacheName, maxMemoryObjects, maxDiskObjects, cachePath) : null;
|
---|
[8168] | 185 | }
|
---|
| 186 | }
|
---|
| 187 |
|
---|
| 188 | @SuppressWarnings("unchecked")
|
---|
[13643] | 189 | private static <K, V> CacheAccess<K, V> getCacheInner(String cacheName, int maxMemoryObjects, int maxDiskObjects, String cachePath) {
|
---|
[8168] | 190 | CompositeCache<K, V> cc = cacheManager.getCache(cacheName, getCacheAttributes(maxMemoryObjects));
|
---|
| 191 |
|
---|
[8186] | 192 | if (cachePath != null && cacheDirLock != null) {
|
---|
[10323] | 193 | IDiskCacheAttributes diskAttributes = getDiskCacheAttributes(maxDiskObjects, cachePath, cacheName);
|
---|
[8598] | 194 | try {
|
---|
| 195 | if (cc.getAuxCaches().length == 0) {
|
---|
[12537] | 196 | cc.setAuxCaches(new AuxiliaryCache[]{DISK_CACHE_FACTORY.createCache(
|
---|
| 197 | diskAttributes, cacheManager, null, new StandardSerializer())});
|
---|
[8598] | 198 | }
|
---|
[13644] | 199 | } catch (Exception e) { // NOPMD
|
---|
[13643] | 200 | // in case any error in setting auxiliary cache, do not use disk cache at all - only memory
|
---|
| 201 | cc.setAuxCaches(new AuxiliaryCache[0]);
|
---|
[8598] | 202 | }
|
---|
[8168] | 203 | }
|
---|
[9070] | 204 | return new CacheAccess<>(cc);
|
---|
[8168] | 205 | }
|
---|
| 206 |
|
---|
| 207 | /**
|
---|
| 208 | * Close all files to ensure, that all indexes and data are properly written
|
---|
| 209 | */
|
---|
| 210 | public static void shutdown() {
|
---|
| 211 | // use volatile semantics to get consistent object
|
---|
| 212 | CompositeCacheManager localCacheManager = cacheManager;
|
---|
| 213 | if (localCacheManager != null) {
|
---|
| 214 | localCacheManager.shutDown();
|
---|
| 215 | }
|
---|
| 216 | }
|
---|
| 217 |
|
---|
[10323] | 218 | private static IDiskCacheAttributes getDiskCacheAttributes(int maxDiskObjects, String cachePath, String cacheName) {
|
---|
| 219 | IDiskCacheAttributes ret;
|
---|
[10762] | 220 | removeStaleFiles(cachePath + File.separator + cacheName, USE_BLOCK_CACHE.get() ? "_INDEX_v2" : "_BLOCK_v2");
|
---|
[10965] | 221 | String newCacheName = cacheName + (USE_BLOCK_CACHE.get() ? "_BLOCK_v2" : "_INDEX_v2");
|
---|
[10652] | 222 |
|
---|
[10323] | 223 | if (USE_BLOCK_CACHE.get()) {
|
---|
| 224 | BlockDiskCacheAttributes blockAttr = new BlockDiskCacheAttributes();
|
---|
[10652] | 225 | /*
|
---|
| 226 | * BlockDiskCache never optimizes the file, so when file size is reduced, it will never be truncated to desired size.
|
---|
| 227 | *
|
---|
| 228 | * If for some mysterious reason, file size is greater than the value set in preferences, just use the whole file. If the user
|
---|
| 229 | * wants to reduce the file size, (s)he may just go to preferences and there it should be handled (by removing old file)
|
---|
| 230 | */
|
---|
[10965] | 231 | File diskCacheFile = new File(cachePath + File.separator + newCacheName + ".data");
|
---|
[10652] | 232 | if (diskCacheFile.exists()) {
|
---|
| 233 | blockAttr.setMaxKeySize((int) Math.max(maxDiskObjects, diskCacheFile.length()/1024));
|
---|
| 234 | } else {
|
---|
| 235 | blockAttr.setMaxKeySize(maxDiskObjects);
|
---|
| 236 | }
|
---|
[10417] | 237 | blockAttr.setBlockSizeBytes(4096); // use 4k blocks
|
---|
[10323] | 238 | ret = blockAttr;
|
---|
| 239 | } else {
|
---|
| 240 | IndexedDiskCacheAttributes indexAttr = new IndexedDiskCacheAttributes();
|
---|
| 241 | indexAttr.setMaxKeySize(maxDiskObjects);
|
---|
| 242 | ret = indexAttr;
|
---|
| 243 | }
|
---|
[8598] | 244 | ret.setDiskLimitType(IDiskCacheAttributes.DiskLimitType.SIZE);
|
---|
[10323] | 245 | File path = new File(cachePath);
|
---|
| 246 | if (!path.exists() && !path.mkdirs()) {
|
---|
[12765] | 247 | Logging.warn("Failed to create cache path: {0}", cachePath);
|
---|
[10323] | 248 | } else {
|
---|
| 249 | ret.setDiskPath(cachePath);
|
---|
[8168] | 250 | }
|
---|
[10965] | 251 | ret.setCacheName(newCacheName);
|
---|
[10323] | 252 |
|
---|
[8168] | 253 | return ret;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
[10323] | 256 | private static void removeStaleFiles(String basePathPart, String suffix) {
|
---|
| 257 | deleteCacheFiles(basePathPart + suffix);
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | private static void deleteCacheFiles(String basePathPart) {
|
---|
[10570] | 261 | Utils.deleteFileIfExists(new File(basePathPart + ".key"));
|
---|
| 262 | Utils.deleteFileIfExists(new File(basePathPart + ".data"));
|
---|
[10323] | 263 | }
|
---|
| 264 |
|
---|
[8168] | 265 | private static CompositeCacheAttributes getCacheAttributes(int maxMemoryElements) {
|
---|
| 266 | CompositeCacheAttributes ret = new CompositeCacheAttributes();
|
---|
| 267 | ret.setMaxObjects(maxMemoryElements);
|
---|
| 268 | ret.setDiskUsagePattern(DiskUsagePattern.UPDATE);
|
---|
| 269 | return ret;
|
---|
| 270 | }
|
---|
| 271 | }
|
---|