source: josm/trunk/src/org/openstreetmap/josm/data/cache/JCSCacheManager.java@ 13818

Last change on this file since 13818 was 13672, checked in by Don-vip, 6 years ago

fix recent SonarQube issues

  • Property svn:eol-style set to native
File size: 12.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.cache;
3
4import java.io.File;
5import java.io.IOException;
6import java.nio.channels.FileChannel;
7import java.nio.channels.FileLock;
8import java.nio.file.StandardOpenOption;
9import java.util.Arrays;
10import java.util.Properties;
11import java.util.logging.Handler;
12import java.util.logging.Level;
13import java.util.logging.LogRecord;
14import java.util.logging.Logger;
15import java.util.logging.SimpleFormatter;
16
17import org.apache.commons.jcs.access.CacheAccess;
18import org.apache.commons.jcs.auxiliary.AuxiliaryCache;
19import org.apache.commons.jcs.auxiliary.AuxiliaryCacheFactory;
20import org.apache.commons.jcs.auxiliary.disk.behavior.IDiskCacheAttributes;
21import org.apache.commons.jcs.auxiliary.disk.block.BlockDiskCacheAttributes;
22import org.apache.commons.jcs.auxiliary.disk.block.BlockDiskCacheFactory;
23import org.apache.commons.jcs.auxiliary.disk.indexed.IndexedDiskCacheAttributes;
24import org.apache.commons.jcs.auxiliary.disk.indexed.IndexedDiskCacheFactory;
25import org.apache.commons.jcs.engine.CompositeCacheAttributes;
26import org.apache.commons.jcs.engine.behavior.ICompositeCacheAttributes.DiskUsagePattern;
27import org.apache.commons.jcs.engine.control.CompositeCache;
28import org.apache.commons.jcs.engine.control.CompositeCacheManager;
29import org.apache.commons.jcs.utils.serialization.StandardSerializer;
30import org.openstreetmap.josm.data.preferences.BooleanProperty;
31import org.openstreetmap.josm.data.preferences.IntegerProperty;
32import org.openstreetmap.josm.spi.preferences.Config;
33import org.openstreetmap.josm.tools.Logging;
34import org.openstreetmap.josm.tools.Utils;
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
39 *
40 * @author Wiktor Niesiobędzki
41 * @since 8168
42 */
43public final class JCSCacheManager {
44 private static volatile CompositeCacheManager cacheManager;
45 private static final long maxObjectTTL = -1;
46 private static final String PREFERENCE_PREFIX = "jcs.cache";
47 public static final BooleanProperty USE_BLOCK_CACHE = new BooleanProperty(PREFERENCE_PREFIX + ".use_block_cache", true);
48
49 private static final AuxiliaryCacheFactory DISK_CACHE_FACTORY =
50 USE_BLOCK_CACHE.get() ? new BlockDiskCacheFactory() : new IndexedDiskCacheFactory();
51 private static FileLock cacheDirLock;
52
53 /**
54 * default objects to be held in memory by JCS caches (per region)
55 */
56 public static final IntegerProperty DEFAULT_MAX_OBJECTS_IN_MEMORY = new IntegerProperty(PREFERENCE_PREFIX + ".max_objects_in_memory", 1000);
57
58 private static final Logger jcsLog;
59
60 static {
61 // raising logging level gives ~500x performance gain
62 // http://westsworld.dk/blog/2008/01/jcs-and-performance/
63 jcsLog = Logger.getLogger("org.apache.commons.jcs");
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();
71
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 }
85 }
86
87 @Override
88 public void flush() {
89 // nothing to be done on flush
90 }
91
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 }
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}
Note: See TracBrowser for help on using the repository browser.