Class CacheManager
- java.lang.Object
-
- org.apache.iotdb.db.metadata.mtree.store.disk.cache.CacheManager
-
- All Implemented Interfaces:
ICacheManager
- Direct Known Subclasses:
LRUCacheManager,PlainCacheManager
public abstract class CacheManager extends java.lang.Object implements ICacheManager
This class implemented the cache management, involving the cache status management on per MNode and cache eviction. All the nodes in memory are still basically organized as a trie via the container and parent reference in each node. Some extra data structure is used to help manage the node cached in memory.The cache eviction on node is actually evicting a subtree from the MTree.
All the cached nodes are divided into two parts by their cache status, evictable nodes and none evictable nodes.
- Evictable nodes are all placed in nodeCache, which prepares the nodes be selected by cache eviction.
- None evictable nodes takes two parts:
- The volatile nodes, new added or updated, which means the data has not been synced to disk.
- The ancestors of the volatile nodes. If a node is not volatile but not in nodeCache, it means this node is an ancestor of some volatile node. Any volatile node is contained in the CachedContainer of its parent. The parent will be placed in nodeBuffer. If the parent is volatile as well, then the parent of the parent will be placed in nodeBuffer instead, which means the root node of a maximum volatile subtree will be placed in node buffer.
-
-
Constructor Summary
Constructors Constructor Description CacheManager()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract voidaddToNodeCache(CacheEntry cacheEntry, IMNode node)voidclear(IMNode root)protected abstract voidclearNodeCache()java.util.List<IMNode>collectVolatileMNodes()Collect nodes in all volatile subtrees.booleanevict()Choose an evictable node from nodeCache and evicted all the cached node in the subtree it represented.protected CacheEntrygetCacheEntry(IMNode node)protected abstract IMNodegetPotentialNodeTobeEvicted()protected voidinitCacheEntryForNode(IMNode node)voidinitRootStatus(IMNode root)protected abstract booleanisInNodeCache(CacheEntry cacheEntry)voidpinMNode(IMNode node)Pin a node in memory, and it will not be evicted.voidremove(IMNode node)protected abstract voidremoveFromNodeCache(CacheEntry cacheEntry)booleanunPinMNode(IMNode node)Unpin a node, and if the lock mayer on this node is zero, it will be evictable.protected abstract voidupdateCacheStatusAfterAccess(CacheEntry cacheEntry)voidupdateCacheStatusAfterAppend(IMNode node)The new appended node should be cached and the volatile subtree it belonged should be added to nodeBuffer.voidupdateCacheStatusAfterDiskRead(IMNode node)The node read from disk should be cached and added to nodeCache and the cache of its belonging container.voidupdateCacheStatusAfterMemoryRead(IMNode node)Some cache status of the given node may be updated based on concrete cache strategy after being read in memory.voidupdateCacheStatusAfterPersist(IMNode node)After flush the given node's container to disk, the cache status of the nodes in container and their ancestors should be updated.voidupdateCacheStatusAfterUpdate(IMNode node)The updated node should be marked volatile and removed from nodeCache if necessary and the volatile subtree it belonged should be added to nodeBuffer.protected abstract voidupdateCacheStatusAfterUpdate(CacheEntry cacheEntry, IMNode node)
-
-
-
Method Detail
-
initRootStatus
public void initRootStatus(IMNode root)
- Specified by:
initRootStatusin interfaceICacheManager
-
updateCacheStatusAfterMemoryRead
public void updateCacheStatusAfterMemoryRead(IMNode node) throws MNodeNotCachedException
Some cache status of the given node may be updated based on concrete cache strategy after being read in memory.- Specified by:
updateCacheStatusAfterMemoryReadin interfaceICacheManager- Parameters:
node-- Throws:
MNodeNotCachedException
-
updateCacheStatusAfterDiskRead
public void updateCacheStatusAfterDiskRead(IMNode node)
The node read from disk should be cached and added to nodeCache and the cache of its belonging container.- Specified by:
updateCacheStatusAfterDiskReadin interfaceICacheManager- Parameters:
node-
-
updateCacheStatusAfterAppend
public void updateCacheStatusAfterAppend(IMNode node)
The new appended node should be cached and the volatile subtree it belonged should be added to nodeBuffer.- Specified by:
updateCacheStatusAfterAppendin interfaceICacheManager- Parameters:
node-
-
updateCacheStatusAfterUpdate
public void updateCacheStatusAfterUpdate(IMNode node)
The updated node should be marked volatile and removed from nodeCache if necessary and the volatile subtree it belonged should be added to nodeBuffer.- Specified by:
updateCacheStatusAfterUpdatein interfaceICacheManager- Parameters:
node-
-
updateCacheStatusAfterPersist
public void updateCacheStatusAfterPersist(IMNode node)
After flush the given node's container to disk, the cache status of the nodes in container and their ancestors should be updated. All the node should be added to nodeCache and the nodeBuffer should be cleared after finishing a MTree flush task.- Specified by:
updateCacheStatusAfterPersistin interfaceICacheManager- Parameters:
node-
-
collectVolatileMNodes
public java.util.List<IMNode> collectVolatileMNodes()
Collect nodes in all volatile subtrees. All volatile nodes' parents will be collected. The ancestor will be in front of the descendent in returned list.- Specified by:
collectVolatileMNodesin interfaceICacheManager- Returns:
-
remove
public void remove(IMNode node)
- Specified by:
removein interfaceICacheManager
-
evict
public boolean evict()
Choose an evictable node from nodeCache and evicted all the cached node in the subtree it represented.- Specified by:
evictin interfaceICacheManager- Returns:
- whether evicted any MNode successfully
-
pinMNode
public void pinMNode(IMNode node) throws MNodeNotPinnedException
Pin a node in memory, and it will not be evicted. The pin is implemented as a multi mayer lock. When a thread/task pin the node, one mayer lock will be added to the node and its parent. This help guarantees the cache assumption that if a node is cached/pinned, its parent should be cached/pinned.- Specified by:
pinMNodein interfaceICacheManager- Parameters:
node-- Throws:
MNodeNotPinnedException
-
unPinMNode
public boolean unPinMNode(IMNode node)
Unpin a node, and if the lock mayer on this node is zero, it will be evictable. Unpin a node means decrease one mayer lock from the node. If the lock mayer reaches zero, the mayer lock of its parent should decrease.- Specified by:
unPinMNodein interfaceICacheManager- Parameters:
node-- Returns:
-
clear
public void clear(IMNode root)
- Specified by:
clearin interfaceICacheManager
-
getCacheEntry
protected CacheEntry getCacheEntry(IMNode node)
-
initCacheEntryForNode
protected void initCacheEntryForNode(IMNode node)
-
updateCacheStatusAfterAccess
protected abstract void updateCacheStatusAfterAccess(CacheEntry cacheEntry)
-
updateCacheStatusAfterUpdate
protected abstract void updateCacheStatusAfterUpdate(CacheEntry cacheEntry, IMNode node)
-
isInNodeCache
protected abstract boolean isInNodeCache(CacheEntry cacheEntry)
-
addToNodeCache
protected abstract void addToNodeCache(CacheEntry cacheEntry, IMNode node)
-
removeFromNodeCache
protected abstract void removeFromNodeCache(CacheEntry cacheEntry)
-
getPotentialNodeTobeEvicted
protected abstract IMNode getPotentialNodeTobeEvicted()
-
clearNodeCache
protected abstract void clearNodeCache()
-
-