Class GridCacheWriteBehindStore<K,V>
- java.lang.Object
-
- org.apache.ignite.internal.processors.cache.store.GridCacheWriteBehindStore<K,V>
-
- All Implemented Interfaces:
javax.cache.integration.CacheLoader<K,V>,javax.cache.integration.CacheWriter<K,V>,CacheStore<K,V>,LifecycleAware
public class GridCacheWriteBehindStore<K,V> extends Object implements CacheStore<K,V>, LifecycleAware
Internal wrapper for aCacheStorethat enables write-behind logic. The general purpose of this approach is to reduce cache store load under high store update rate. The idea is to cache all write and remove operations in a pending map and delegate these changes to the underlying store either after timeout or if size of a pending map exceeded some pre-configured value. Another performance gain is achieved due to combining a group of similar operations to a single batch update. The essential flush size for the write-behind cache should be at least the estimated count of simultaneously written keys. In case of significantly smaller value there would be triggered a lot of flush events that will result in a high cache store load. Since write operations to the cache store are deferred, transaction support is lost; no transaction objects are passed to the underlying store.GridCacheWriteBehindStoredoesn't support concurrent modifications of the same key.
-
-
Field Summary
Fields Modifier and Type Field Description static floatCACHE_OVERFLOW_RATIOOverflow ratio for critical cache size calculation.static intDFLT_CONCUR_LVLDefault concurrency level of write cache.static intDFLT_INITIAL_CAPACITYDefault write cache initial capacity.
-
Constructor Summary
Constructors Constructor Description GridCacheWriteBehindStore(CacheStoreManager storeMgr, String igniteInstanceName, String cacheName, IgniteLogger log, CacheStore<K,V> store)Creates a write-behind cache store for the given store.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddelete(Object key)voiddeleteAll(Collection<?> keys)voidforceFlush()Forces all entries collected to be flushed to the underlying store.intgetWriteBehindBufferSize()Gets count of entries that were processed by the write-behind store and have not been flushed to the underlying store yet.intgetWriteBehindCriticalOverflowCount()Gets count of write buffer overflow events in progress at the moment.intgetWriteBehindErrorRetryCount()Gets count of cache entries that are in a store-retry state.longgetWriteBehindFlushFrequency()Gets the cache flush frequency.intgetWriteBehindFlushSize()Gets the maximum size of the write-behind buffer.intgetWriteBehindFlushThreadCount()Gets the number of flush threads that will perform store update operations.intgetWriteBehindStoreBatchSize()Gets the maximum count of similar (put or remove) operations that can be grouped to a single batch.intgetWriteBehindTotalCriticalOverflowCount()Gets count of write buffer overflow events since initialization.booleangetWriteCoalescing()Gets the write coalescing flag.Vload(K key)Map<K,V>loadAll(Iterable<? extends K> keys)voidloadCache(IgniteBiInClosure<K,V> clo, @Nullable Object... args)Loads all values from underlying persistent storage.voidsessionEnd(boolean commit)Tells store to commit or rollback a transaction depending on the value of the'commit'parameter.voidsetBatchSize(int batchSize)Sets the maximum count of similar operations that can be grouped to a single batch.voidsetConcurrencyLevel(int concurLvl)Sets concurrency level for the write cache.voidsetFlushFrequency(long cacheFlushFreq)Sets the cache flush frequency.voidsetFlushSize(int cacheMaxSize)Sets the maximum size of the write cache.voidsetFlushThreadCount(int flushThreadCnt)Sets the number of threads that will perform store update operations.voidsetInitialCapacity(int initCap)Sets initial capacity for the write cache.voidsetWriteCoalescing(boolean writeCoalescing)Sets the write coalescing flag.voidstart()Performs all the initialization logic for write-behind cache store.voidstop()Performs shutdown logic for store.CacheStore<K,V>store()StringtoString()voidwrite(javax.cache.Cache.Entry<? extends K,? extends V> entry)voidwriteAll(Collection<javax.cache.Cache.Entry<? extends K,? extends V>> entries)
-
-
-
Field Detail
-
DFLT_INITIAL_CAPACITY
public static final int DFLT_INITIAL_CAPACITY
Default write cache initial capacity.- See Also:
- Constant Field Values
-
CACHE_OVERFLOW_RATIO
public static final float CACHE_OVERFLOW_RATIO
Overflow ratio for critical cache size calculation.- See Also:
- Constant Field Values
-
DFLT_CONCUR_LVL
public static final int DFLT_CONCUR_LVL
Default concurrency level of write cache.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
GridCacheWriteBehindStore
public GridCacheWriteBehindStore(CacheStoreManager storeMgr, String igniteInstanceName, String cacheName, IgniteLogger log, CacheStore<K,V> store)
Creates a write-behind cache store for the given store.- Parameters:
storeMgr- Store manager.igniteInstanceName- Ignite instance name.cacheName- Cache name.log- Grid logger.store-GridCacheStorethat need to be wrapped.
-
-
Method Detail
-
setInitialCapacity
public void setInitialCapacity(int initCap)
Sets initial capacity for the write cache.- Parameters:
initCap- Initial capacity.
-
setConcurrencyLevel
public void setConcurrencyLevel(int concurLvl)
Sets concurrency level for the write cache. Concurrency level is expected count of concurrent threads attempting to update cache.- Parameters:
concurLvl- Concurrency level.
-
setFlushSize
public void setFlushSize(int cacheMaxSize)
Sets the maximum size of the write cache. When the count of unique keys in write cache exceeds this value, the eldest entry in the cache is immediately scheduled for write to the underlying store.- Parameters:
cacheMaxSize- Max cache size.
-
getWriteBehindFlushSize
public int getWriteBehindFlushSize()
Gets the maximum size of the write-behind buffer. When the count of unique keys in write buffer exceeds this value, the buffer is scheduled for write to the underlying store. If this value is0, then flush is performed only on time-elapsing basis. However, when this value is0, the cache critical size is set toCacheConfiguration.DFLT_WRITE_BEHIND_CRITICAL_SIZE.- Returns:
- Buffer size that triggers flush procedure.
-
setFlushThreadCount
public void setFlushThreadCount(int flushThreadCnt)
Sets the number of threads that will perform store update operations.- Parameters:
flushThreadCnt- Count of worker threads.
-
getWriteBehindFlushThreadCount
public int getWriteBehindFlushThreadCount()
Gets the number of flush threads that will perform store update operations.- Returns:
- Count of worker threads.
-
setWriteCoalescing
public void setWriteCoalescing(boolean writeCoalescing)
Sets the write coalescing flag.- Parameters:
writeCoalescing- Write coalescing flag.
-
getWriteCoalescing
public boolean getWriteCoalescing()
Gets the write coalescing flag.- Returns:
- Write coalescing flag.
-
setFlushFrequency
public void setFlushFrequency(long cacheFlushFreq)
Sets the cache flush frequency. All pending operations on the underlying store will be performed within time interval not less then this value.- Parameters:
cacheFlushFreq- Time interval value in milliseconds.
-
getWriteBehindFlushFrequency
public long getWriteBehindFlushFrequency()
Gets the cache flush frequency. All pending operations on the underlying store will be performed within time interval not less then this value. If this value is0, then flush is performed only when buffer size exceeds flush size.- Returns:
- Flush frequency in milliseconds.
-
setBatchSize
public void setBatchSize(int batchSize)
Sets the maximum count of similar operations that can be grouped to a single batch.- Parameters:
batchSize- Maximum count of batch.
-
getWriteBehindStoreBatchSize
public int getWriteBehindStoreBatchSize()
Gets the maximum count of similar (put or remove) operations that can be grouped to a single batch.- Returns:
- Maximum size of batch.
-
getWriteBehindBufferSize
public int getWriteBehindBufferSize()
Gets count of entries that were processed by the write-behind store and have not been flushed to the underlying store yet.- Returns:
- Total count of entries in cache store internal buffer.
-
store
public CacheStore<K,V> store()
- Returns:
- Underlying store.
-
start
public void start()
Performs all the initialization logic for write-behind cache store. This class must not be used until this method returns.- Specified by:
startin interfaceLifecycleAware
-
getWriteBehindTotalCriticalOverflowCount
public int getWriteBehindTotalCriticalOverflowCount()
Gets count of write buffer overflow events since initialization. Each overflow event causes the ongoing flush operation to be performed synchronously.- Returns:
- Count of cache overflow events since start.
-
getWriteBehindCriticalOverflowCount
public int getWriteBehindCriticalOverflowCount()
Gets count of write buffer overflow events in progress at the moment. Each overflow event causes the ongoing flush operation to be performed synchronously.- Returns:
- Count of cache overflow events since start.
-
getWriteBehindErrorRetryCount
public int getWriteBehindErrorRetryCount()
Gets count of cache entries that are in a store-retry state. An entry is assigned a store-retry state when underlying store failed due some reason and cache has enough space to retain this entry till the next try.- Returns:
- Count of entries in store-retry state.
-
stop
public void stop()
Performs shutdown logic for store. No put, get and remove requests will be processed after this method is called.- Specified by:
stopin interfaceLifecycleAware
-
forceFlush
public void forceFlush() throws IgniteCheckedExceptionForces all entries collected to be flushed to the underlying store.- Throws:
IgniteCheckedException- If failed.
-
loadCache
public void loadCache(IgniteBiInClosure<K,V> clo, @Nullable @Nullable Object... args)
Loads all values from underlying persistent storage. Note that keys are not passed, so it is up to implementation to figure out what to load. This method is called wheneverIgniteCache.loadCache(IgniteBiPredicate, Object...)method is invoked which is usually to preload the cache from persistent storage.This method is optional, and cache implementation does not depend on this method to do anything. Default implementation of this method in
CacheStoreAdapterdoes nothing.For every loaded value method
IgniteBiInClosure.apply(Object, Object)should be called on the passed in closure. The closure will then make sure that the loaded value is stored in cache.- Specified by:
loadCachein interfaceCacheStore<K,V>- Parameters:
clo- Closure for loaded values.args- Arguments passes intoIgniteCache.loadCache(IgniteBiPredicate, Object...)method.
-
writeAll
public void writeAll(Collection<javax.cache.Cache.Entry<? extends K,? extends V>> entries)
-
deleteAll
public void deleteAll(Collection<?> keys)
-
delete
public void delete(Object key)
-
sessionEnd
public void sessionEnd(boolean commit)
Tells store to commit or rollback a transaction depending on the value of the'commit'parameter.- Specified by:
sessionEndin interfaceCacheStore<K,V>- Parameters:
commit-Trueif transaction should commit,falsefor rollback.
-
-