|
| 1 | +package com.wesley.blobfs; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Map.Entry; |
| 5 | +import java.util.concurrent.ConcurrentHashMap; |
| 6 | +import java.util.concurrent.ExecutorService; |
| 7 | +import java.util.concurrent.Executors; |
| 8 | +import java.util.concurrent.ThreadFactory; |
| 9 | + |
| 10 | +import org.slf4j.Logger; |
| 11 | +import org.slf4j.LoggerFactory; |
| 12 | + |
| 13 | +import ru.serce.jnrfuse.struct.FileStat; |
| 14 | + |
| 15 | +public class BfsFilesCache extends BfsCacheBase { |
| 16 | + private static BfsFilesCache instance; |
| 17 | + private static ExecutorService cacheAutoCleanupES; |
| 18 | + private static boolean clusterEnabled = Constants.BFS_CLUSTER_ENABLED; |
| 19 | + |
| 20 | + private BfsFilesCache(){ |
| 21 | + cacheStore = new ConcurrentHashMap<String, CachedObject>(Constants.BFS_FILES_CACHE_INIT_CAPACITY, 0.9f, 1); |
| 22 | + capacity = Constants.BFS_FILES_CACHE_MAX_CAPACITY; |
| 23 | + expireTime = Constants.BFS_FILES_CACHE_EXPIRE_TIME; |
| 24 | + /* start the lease auto cleanup service */ |
| 25 | + if (clusterEnabled){ |
| 26 | + startCacheAutoCleanupService(); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + private final void startCacheAutoCleanupService(){ |
| 31 | + /* Make it a daemon */ |
| 32 | + cacheAutoCleanupES = Executors.newSingleThreadExecutor(new ThreadFactory(){ |
| 33 | + public Thread newThread(Runnable r) { |
| 34 | + Thread t = new Thread(r); |
| 35 | + t.setDaemon(true); |
| 36 | + return t; |
| 37 | + } |
| 38 | + }); |
| 39 | + cacheAutoCleanupES.submit(new cacheAutoCleaner()); |
| 40 | + } |
| 41 | + |
| 42 | + public final boolean getFileStat(String path, FileStat stat){ |
| 43 | + BfsPath bfsPath = new BfsPath(path); |
| 44 | + PathProperties pathProperties = (PathProperties) get(path); |
| 45 | + if (!bfsPath.fillFileStat(pathProperties, stat)){ |
| 46 | + return false; |
| 47 | + } |
| 48 | + return true; |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void finalize() { |
| 54 | + cacheAutoCleanupES.shutdown(); |
| 55 | + } |
| 56 | + |
| 57 | + public static BfsFilesCache getInstance(){ |
| 58 | + if(instance == null){ |
| 59 | + synchronized (BfsFilesCache.class) { |
| 60 | + if(instance == null){ |
| 61 | + instance = new BfsFilesCache(); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + return instance; |
| 66 | + } |
| 67 | + |
| 68 | + private class cacheAutoCleaner implements Runnable{ |
| 69 | + private Logger logger = LoggerFactory.getLogger("cacheAutoCleaner.class"); |
| 70 | + @Override |
| 71 | + public void run() { |
| 72 | + try { |
| 73 | + /* start the auto cleanup process */ |
| 74 | + while (true){ |
| 75 | + BfsFilesCache bfsFilesCache = BfsFilesCache.getInstance(); |
| 76 | + /* Retrieve the msgs from the service bus topic */ |
| 77 | + ArrayList<String> msgs = new ArrayList<>(); |
| 78 | + msgs = MessageService.sbReceiveMessages(); |
| 79 | + for (String msg: msgs){ |
| 80 | + logger.trace("delete {} from cache", msg); |
| 81 | + BfsPath msgPath = new BfsPath(msg); |
| 82 | + BfsPathType msgPathType = msgPath.getBfsPathProperties().getBfsPathType(); |
| 83 | + if ("ROOT".equals(msgPathType.toString())){ |
| 84 | + bfsFilesCache.clear(); |
| 85 | + } else if ("CONTAINER".equals(msgPathType.toString()) || "SUBDIR".equals(msgPathType.toString())){ |
| 86 | + for (Entry<String, CachedObject> entry : bfsFilesCache.cacheStore.entrySet()) { |
| 87 | + if (entry.getKey().startsWith(msg)) { |
| 88 | + bfsFilesCache.delete(entry.getKey()); |
| 89 | + } |
| 90 | + } |
| 91 | + } else if ("BLOB".equals(msgPathType.toString()) || "LINK".equals(msgPathType.toString())){ |
| 92 | + if (bfsFilesCache.has(msg)) { |
| 93 | + bfsFilesCache.delete(msg); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + Thread.sleep(Constants.DEFAULT_BFC_THREAD_SLEEP_MILLS); |
| 98 | + } |
| 99 | + } catch (Exception ex) { |
| 100 | + logger.error(ex.getMessage()); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments