Class FileBasedCache
java.lang.Object
org.apache.flink.state.forst.fs.cache.DoubleListLru<String,FileCacheEntry>
org.apache.flink.state.forst.fs.cache.FileBasedCache
- All Implemented Interfaces:
Closeable,AutoCloseable,Iterable<org.apache.flink.api.java.tuple.Tuple2<String,FileCacheEntry>>
A file-granularity LRU cache. Only newly generated SSTs are written to the cache, the file
reading from the remote will not. Newly generated SSTs are written to the original file system
and cache simultaneously, so, the cached file can be directly deleted with persisting when
evicting. The FileBasedCache manages multiple FileCacheEntry instances.
Each FileCacheEntry represents a cached file and can open CachedDataInputStream for reading
the file.
CachedDataInputStream instances are created by FileCacheEntry and can read data from either
the cached file or the original file, depending on the cache entry's state. It has internal
stream status to indicate the current reading source.
CachedDataOutputStream instances are created by FileBasedCache and write data to both the
original and cached files, creating a new cache entry in the cache when the writing is
finished.
FileBasedCache, FileCacheEntry, CachedDataInputStream, and
CachedDataOutputStream classes work together to implement a file-based caching mechanism
in ForSt State Backend.
-
Constructor Summary
ConstructorsConstructorDescriptionFileBasedCache(org.apache.flink.configuration.ReadableConfig configuration, CacheLimitPolicy cacheLimitPolicy, org.apache.flink.core.fs.FileSystem cacheFs, org.apache.flink.core.fs.Path basePath, org.apache.flink.metrics.MetricGroup metricGroup) -
Method Summary
Modifier and TypeMethodDescriptionvoidaddFirst(String key, FileCacheEntry value) Adds a new entry to the front of the cache.voidaddSecond(String key, FileCacheEntry value) Inserts a new entry at the middle of the cache.voidclose()create(org.apache.flink.core.fs.FSDataOutputStream originalOutputStream, org.apache.flink.core.fs.Path path) voiddelete(org.apache.flink.core.fs.Path path) Retrieves the value associated with the specified key.voidvoidstatic booleanChecks if the current thread is a Flink thread.open(org.apache.flink.core.fs.Path path, org.apache.flink.core.fs.FSDataInputStream originalStream) voidregisterInCache(org.apache.flink.core.fs.Path originalPath, long size) Directly insert in cache when restoring.Removes the entry associated with the specified key from the cache.static voidSets the current thread as a Flink thread.Methods inherited from class org.apache.flink.state.forst.fs.cache.DoubleListLru
descendingIterator, getSecondSize, isEmpty, iterator, moveMiddleBack, moveMiddleFront, sizeMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
FileBasedCache
public FileBasedCache(org.apache.flink.configuration.ReadableConfig configuration, CacheLimitPolicy cacheLimitPolicy, org.apache.flink.core.fs.FileSystem cacheFs, org.apache.flink.core.fs.Path basePath, org.apache.flink.metrics.MetricGroup metricGroup)
-
-
Method Details
-
setFlinkThread
public static void setFlinkThread()Sets the current thread as a Flink thread. This method is used to mark the thread as a Flink thread, which can be used to determine whether the file access would affect the LRU cache order, or metrics updates. -
isFlinkThread
public static boolean isFlinkThread()Checks if the current thread is a Flink thread. This method returns a boolean indicating whether the current thread has been marked as a Flink thread using thesetFlinkThread()method.- Returns:
- true if the current thread is a Flink thread, false otherwise.
-
incHitCounter
public void incHitCounter() -
incMissCounter
public void incMissCounter() -
open
public CachedDataInputStream open(org.apache.flink.core.fs.Path path, org.apache.flink.core.fs.FSDataInputStream originalStream) throws IOException - Throws:
IOException
-
create
public CachedDataOutputStream create(org.apache.flink.core.fs.FSDataOutputStream originalOutputStream, org.apache.flink.core.fs.Path path) throws IOException - Throws:
IOException
-
delete
public void delete(org.apache.flink.core.fs.Path path) -
get
Description copied from class:DoubleListLruRetrieves the value associated with the specified key. Optionally affects the order of the entries in the cache.- Overrides:
getin classDoubleListLru<String,FileCacheEntry> - Parameters:
key- the key of the entryaffectOrder- true if the order of the entries should be affected, false otherwise- Returns:
- the value associated with the key, or null if the key is not found
-
addFirst
Description copied from class:DoubleListLruAdds a new entry to the front of the cache.- Overrides:
addFirstin classDoubleListLru<String,FileCacheEntry> - Parameters:
key- the key of the entryvalue- the value of the entry
-
addSecond
Description copied from class:DoubleListLruInserts a new entry at the middle of the cache.- Overrides:
addSecondin classDoubleListLru<String,FileCacheEntry> - Parameters:
key- the key of the entryvalue- the value of the entry
-
remove
Description copied from class:DoubleListLruRemoves the entry associated with the specified key from the cache.- Overrides:
removein classDoubleListLru<String,FileCacheEntry> - Parameters:
key- the key of the entry to be removed- Returns:
- the value of the removed entry, or null if the key is not found
-
registerInCache
public void registerInCache(org.apache.flink.core.fs.Path originalPath, long size) Directly insert in cache when restoring. -
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-