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

Last change on this file since 16392 was 16392, checked in by simon04, 4 years ago

see #19173, see #19113 - JCSCacheManager: log all initialization errors

  • Property svn:eol-style set to native
File size: 12.3 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.JCS;
18import org.apache.commons.jcs.access.CacheAccess;
19import org.apache.commons.jcs.auxiliary.AuxiliaryCache;
20import org.apache.commons.jcs.auxiliary.AuxiliaryCacheFactory;
21import org.apache.commons.jcs.auxiliary.disk.behavior.IDiskCacheAttributes;
22import org.apache.commons.jcs.auxiliary.disk.block.BlockDiskCacheAttributes;
23import org.apache.commons.jcs.auxiliary.disk.block.BlockDiskCacheFactory;
24import org.apache.commons.jcs.auxiliary.disk.indexed.IndexedDiskCacheAttributes;
25import org.apache.commons.jcs.auxiliary.disk.indexed.IndexedDiskCacheFactory;
26import org.apache.commons.jcs.engine.CompositeCacheAttributes;
27import org.apache.commons.jcs.engine.behavior.ICompositeCacheAttributes.DiskUsagePattern;
28import org.apache.commons.jcs.engine.control.CompositeCache;
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 final long maxObjectTTL = -1;
45 private static final String PREFERENCE_PREFIX = "jcs.cache";
46 public static final BooleanProperty USE_BLOCK_CACHE = new BooleanProperty(PREFERENCE_PREFIX + ".use_block_cache", true);
47
48 private static final AuxiliaryCacheFactory DISK_CACHE_FACTORY =
49 USE_BLOCK_CACHE.get() ? new BlockDiskCacheFactory() : new IndexedDiskCacheFactory();
50 private static FileLock cacheDirLock;
51
52 /**
53 * default objects to be held in memory by JCS caches (per region)
54 */
55 public static final IntegerProperty DEFAULT_MAX_OBJECTS_IN_MEMORY = new IntegerProperty(PREFERENCE_PREFIX + ".max_objects_in_memory", 1000);
56
57 private static final Logger jcsLog;
58
59 static {
60 // raising logging level gives ~500x performance gain
61 // http://westsworld.dk/blog/2008/01/jcs-and-performance/
62 jcsLog = Logger.getLogger("org.apache.commons.jcs");
63 try {
64 jcsLog.setLevel(Level.INFO);
65 jcsLog.setUseParentHandlers(false);
66 // we need a separate handler from Main's, as we downgrade LEVEL.INFO to DEBUG level
67 Arrays.stream(jcsLog.getHandlers()).forEach(jcsLog::removeHandler);
68 jcsLog.addHandler(new Handler() {
69 final SimpleFormatter formatter = new SimpleFormatter();
70
71 @Override
72 public void publish(LogRecord record) {
73 String msg = formatter.formatMessage(record);
74 if (record.getLevel().intValue() >= Level.SEVERE.intValue()) {
75 Logging.error(msg);
76 } else if (record.getLevel().intValue() >= Level.WARNING.intValue()) {
77 // Get rid of useless JCS warnings, see #18644
78 // Pending upstream patch: https://issues.apache.org/jira/projects/JCS/issues/JCS-200
79 // Pending upstream patch: https://issues.apache.org/jira/projects/JCS/issues/JCS-201
80 if ("No configuration settings found. Using hardcoded default values for all pools.".equals(msg)
81 || (msg.startsWith("Region") && msg.endsWith("Resetting cache"))) { // "Region [TMS_BLOCK_v2] Resetting cache"
82 Logging.debug(msg);
83 } else {
84 Logging.warn(msg);
85 }
86 // downgrade INFO level to debug, as JCS is too verbose at INFO level
87 } else if (record.getLevel().intValue() >= Level.INFO.intValue()) {
88 Logging.debug(msg);
89 } else {
90 Logging.trace(msg);
91 }
92 }
93
94 @Override
95 public void flush() {
96 // nothing to be done on flush
97 }
98
99 @Override
100 public void close() {
101 // nothing to be done on close
102 }
103 });
104 } catch (Exception e) {
105 Logging.log(Logging.LEVEL_ERROR, "Unable to configure JCS logs", e);
106 }
107 }
108
109 private JCSCacheManager() {
110 // Hide implicit public constructor for utility classes
111 }
112
113 static {
114 File cacheDir = new File(Config.getDirs().getCacheDirectory(true), "jcs");
115
116 try {
117 if (!cacheDir.exists() && !cacheDir.mkdirs()) {
118 Logging.warn("Cache directory " + cacheDir.toString() + " does not exists and could not create it");
119 } else {
120 File cacheDirLockPath = new File(cacheDir, ".lock");
121 try {
122 if (!cacheDirLockPath.exists() && !cacheDirLockPath.createNewFile()) {
123 Logging.warn("Cannot create cache dir lock file");
124 }
125 cacheDirLock = FileChannel.open(cacheDirLockPath.toPath(), StandardOpenOption.WRITE).tryLock();
126
127 if (cacheDirLock == null)
128 Logging.warn("Cannot lock cache directory. Will not use disk cache");
129 } catch (IOException e) {
130 Logging.log(Logging.LEVEL_WARN, "Cannot create cache dir \"" + cacheDirLockPath + "\" lock file:", e);
131 Logging.warn("Will not use disk cache");
132 }
133 }
134 } catch (Exception e) {
135 Logging.log(Logging.LEVEL_WARN, "Unable to configure disk cache. Will not use it", e);
136 }
137
138 // this could be moved to external file
139 Properties props = new Properties();
140 // these are default common to all cache regions
141 // use of auxiliary cache and sizing of the caches is done with giving proper getCache(...) params
142 // CHECKSTYLE.OFF: SingleSpaceSeparator
143 props.setProperty("jcs.default.cacheattributes", CompositeCacheAttributes.class.getCanonicalName());
144 props.setProperty("jcs.default.cacheattributes.MaxObjects", DEFAULT_MAX_OBJECTS_IN_MEMORY.get().toString());
145 props.setProperty("jcs.default.cacheattributes.UseMemoryShrinker", "true");
146 props.setProperty("jcs.default.cacheattributes.DiskUsagePatternName", "UPDATE"); // store elements on disk on put
147 props.setProperty("jcs.default.elementattributes", CacheEntryAttributes.class.getCanonicalName());
148 props.setProperty("jcs.default.elementattributes.IsEternal", "false");
149 props.setProperty("jcs.default.elementattributes.MaxLife", Long.toString(maxObjectTTL));
150 props.setProperty("jcs.default.elementattributes.IdleTime", Long.toString(maxObjectTTL));
151 props.setProperty("jcs.default.elementattributes.IsSpool", "true");
152 // CHECKSTYLE.ON: SingleSpaceSeparator
153 try {
154 JCS.setConfigProperties(props);
155 } catch (Exception e) {
156 Logging.log(Logging.LEVEL_WARN, "Unable to initialize JCS", e);
157 }
158 }
159
160 /**
161 * Returns configured cache object for named cache region
162 * @param <K> key type
163 * @param <V> value type
164 * @param cacheName region name
165 * @return cache access object
166 */
167 public static <K, V> CacheAccess<K, V> getCache(String cacheName) {
168 return getCache(cacheName, DEFAULT_MAX_OBJECTS_IN_MEMORY.get().intValue(), 0, null);
169 }
170
171 /**
172 * Returns configured cache object with defined limits of memory cache and disk cache
173 * @param <K> key type
174 * @param <V> value type
175 * @param cacheName region name
176 * @param maxMemoryObjects number of objects to keep in memory
177 * @param maxDiskObjects maximum size of the objects stored on disk in kB
178 * @param cachePath path to disk cache. if null, no disk cache will be created
179 * @return cache access object
180 */
181 @SuppressWarnings("unchecked")
182 public static <K, V> CacheAccess<K, V> getCache(String cacheName, int maxMemoryObjects, int maxDiskObjects, String cachePath) {
183 CacheAccess<K, V> cacheAccess = JCS.getInstance(cacheName, getCacheAttributes(maxMemoryObjects));
184 CompositeCache<K, V> cc = cacheAccess.getCacheControl();
185
186 if (cachePath != null && cacheDirLock != null) {
187 IDiskCacheAttributes diskAttributes = getDiskCacheAttributes(maxDiskObjects, cachePath, cacheName);
188 try {
189 if (cc.getAuxCaches().length == 0) {
190 cc.setAuxCaches(new AuxiliaryCache[]{DISK_CACHE_FACTORY.createCache(
191 diskAttributes, null, null, new StandardSerializer())});
192 }
193 } catch (Exception e) { // NOPMD
194 // in case any error in setting auxiliary cache, do not use disk cache at all - only memory
195 cc.setAuxCaches(new AuxiliaryCache[0]);
196 Logging.debug(e);
197 }
198 }
199 return cacheAccess;
200 }
201
202 /**
203 * Close all files to ensure, that all indexes and data are properly written
204 */
205 public static void shutdown() {
206 JCS.shutdown();
207 }
208
209 private static IDiskCacheAttributes getDiskCacheAttributes(int maxDiskObjects, String cachePath, String cacheName) {
210 IDiskCacheAttributes ret;
211 removeStaleFiles(cachePath + File.separator + cacheName, USE_BLOCK_CACHE.get() ? "_INDEX_v2" : "_BLOCK_v2");
212 String newCacheName = cacheName + (USE_BLOCK_CACHE.get() ? "_BLOCK_v2" : "_INDEX_v2");
213
214 if (USE_BLOCK_CACHE.get()) {
215 BlockDiskCacheAttributes blockAttr = new BlockDiskCacheAttributes();
216 /*
217 * BlockDiskCache never optimizes the file, so when file size is reduced, it will never be truncated to desired size.
218 *
219 * If for some mysterious reason, file size is greater than the value set in preferences, just use the whole file. If the user
220 * wants to reduce the file size, (s)he may just go to preferences and there it should be handled (by removing old file)
221 */
222 File diskCacheFile = new File(cachePath + File.separator + newCacheName + ".data");
223 if (diskCacheFile.exists()) {
224 blockAttr.setMaxKeySize((int) Math.max(maxDiskObjects, diskCacheFile.length()/1024));
225 } else {
226 blockAttr.setMaxKeySize(maxDiskObjects);
227 }
228 blockAttr.setBlockSizeBytes(4096); // use 4k blocks
229 ret = blockAttr;
230 } else {
231 IndexedDiskCacheAttributes indexAttr = new IndexedDiskCacheAttributes();
232 indexAttr.setMaxKeySize(maxDiskObjects);
233 ret = indexAttr;
234 }
235 ret.setDiskLimitType(IDiskCacheAttributes.DiskLimitType.SIZE);
236 File path = new File(cachePath);
237 if (!path.exists() && !path.mkdirs()) {
238 Logging.warn("Failed to create cache path: {0}", cachePath);
239 } else {
240 ret.setDiskPath(cachePath);
241 }
242 ret.setCacheName(newCacheName);
243
244 return ret;
245 }
246
247 private static void removeStaleFiles(String basePathPart, String suffix) {
248 deleteCacheFiles(basePathPart + suffix);
249 }
250
251 private static void deleteCacheFiles(String basePathPart) {
252 Utils.deleteFileIfExists(new File(basePathPart + ".key"));
253 Utils.deleteFileIfExists(new File(basePathPart + ".data"));
254 }
255
256 private static CompositeCacheAttributes getCacheAttributes(int maxMemoryElements) {
257 CompositeCacheAttributes ret = new CompositeCacheAttributes();
258 ret.setMaxObjects(maxMemoryElements);
259 ret.setDiskUsagePattern(DiskUsagePattern.UPDATE);
260 return ret;
261 }
262}
Note: See TracBrowser for help on using the repository browser.