public class GridCacheWriteBehindStore<K,V> extends Object implements GridCacheStore<K,V>, GridLifecycleAware
GridCacheStore that 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.| Modifier and Type | Field and Description |
|---|---|
static float |
CACHE_OVERFLOW_RATIO
Overflow ratio for critical cache size calculation.
|
static int |
DFLT_CONCUR_LVL
Default concurrency level of write cache.
|
static int |
DFLT_INITIAL_CAPACITY
Default write cache initial capacity.
|
| Constructor and Description |
|---|
GridCacheWriteBehindStore(String gridName,
String cacheName,
GridLogger log,
GridCacheStore<K,V> store)
Creates a write-behind cache store for the given store and cache name.
|
| Modifier and Type | Method and Description |
|---|---|
void |
forceFlush()
Forces all entries collected to be flushed to the underlying store.
|
int |
getWriteBehindBufferSize()
Gets count of entries that were processed by the write-behind store and have not been
flushed to the underlying store yet.
|
int |
getWriteBehindCriticalOverflowCount()
Gets count of write buffer overflow events in progress at the moment.
|
int |
getWriteBehindErrorRetryCount()
Gets count of cache entries that are in a store-retry state.
|
long |
getWriteBehindFlushFrequency()
Gets the cache flush frequency.
|
int |
getWriteBehindFlushSize()
Gets the maximum size of the write-behind buffer.
|
int |
getWriteBehindFlushThreadCount()
Gets the number of flush threads that will perform store update operations.
|
int |
getWriteBehindStoreBatchSize()
Gets the maximum count of similar (put or remove) operations that can be grouped to a single batch.
|
int |
getWriteBehindTotalCriticalOverflowCount()
Gets count of write buffer overflow events since initialization.
|
V |
load(GridCacheTx tx,
K key)
Loads value for the key from underlying persistent storage.
|
void |
loadAll(GridCacheTx tx,
Collection<? extends K> keys,
GridBiInClosure<K,V> c)
Loads all values for given keys and passes every value to the provided closure.
|
void |
loadCache(GridBiInClosure<K,V> clo,
Object... args)
Default empty implementation.
|
void |
put(GridCacheTx tx,
K key,
V val)
Stores a given value in persistent storage.
|
void |
putAll(GridCacheTx tx,
Map<? extends K,? extends V> map)
Stores given key value pairs in persistent storage.
|
void |
remove(GridCacheTx tx,
K key)
Removes the value identified by given key from persistent storage.
|
void |
removeAll(GridCacheTx tx,
Collection<? extends K> keys)
Removes all vales identified by given keys from persistent storage.
|
void |
setBatchSize(int batchSize)
Sets the maximum count of similar operations that can be grouped to a single batch.
|
void |
setConcurrencyLevel(int concurLvl)
Sets concurrency level for the write cache.
|
void |
setFlushFrequency(long cacheFlushFreq)
Sets the cache flush frequency.
|
void |
setFlushSize(int cacheMaxSize)
Sets the maximum size of the write cache.
|
void |
setFlushThreadCount(int flushThreadCnt)
Sets the number of threads that will perform store update operations.
|
void |
setInitialCapacity(int initCap)
Sets initial capacity for the write cache.
|
void |
start()
Performs all the initialization logic for write-behind cache store.
|
void |
stop()
Performs shutdown logic for store.
|
GridCacheStore<K,V> |
store() |
String |
toString() |
void |
txEnd(GridCacheTx tx,
boolean commit)
Tells store to commit or rollback a transaction depending on the value of the
'commit'
parameter. |
public static final int DFLT_INITIAL_CAPACITY
public static final float CACHE_OVERFLOW_RATIO
public static final int DFLT_CONCUR_LVL
public GridCacheWriteBehindStore(String gridName, String cacheName, GridLogger log, GridCacheStore<K,V> store)
gridName - Grid name.cacheName - Cache name.log - Grid logger.store - GridCacheStore that need to be wrapped.public void setInitialCapacity(int initCap)
initCap - Initial capacity.public void setConcurrencyLevel(int concurLvl)
concurLvl - Concurrency level.public void setFlushSize(int cacheMaxSize)
cacheMaxSize - Max cache size.public int getWriteBehindFlushSize()
0, then flush is performed only on time-elapsing basis. However,
when this value is 0, the cache critical size is set to
GridCacheConfiguration.DFLT_WRITE_BEHIND_CRITICAL_SIZEpublic void setFlushThreadCount(int flushThreadCnt)
flushThreadCnt - Count of worker threads.public int getWriteBehindFlushThreadCount()
public void setFlushFrequency(long cacheFlushFreq)
cacheFlushFreq - Time interval value in milliseconds.public long getWriteBehindFlushFrequency()
0, then flush is performed only when buffer size exceeds flush size.public void setBatchSize(int batchSize)
batchSize - Maximum count of batch.public int getWriteBehindStoreBatchSize()
public int getWriteBehindBufferSize()
public GridCacheStore<K,V> store()
public void start()
throws GridException
start in interface GridLifecycleAwareGridException - If cache cannot be started due to some reasons.public int getWriteBehindTotalCriticalOverflowCount()
public int getWriteBehindCriticalOverflowCount()
public int getWriteBehindErrorRetryCount()
public void stop()
throws GridException
stop in interface GridLifecycleAwareGridException - If shutdown failed for some reason.public void forceFlush()
throws GridException
GridException - If failed.public void loadCache(GridBiInClosure<K,V> clo, @Nullable Object... args) throws GridException
GridCache.loadCache(GridBiPredicate, long, Object...) method
is explicitly called.loadCache in interface GridCacheStore<K,V>clo - Closure for loaded values.args - Arguments passes into
GridCache.loadCache(GridBiPredicate, long, Object...) method.GridException - If loading failed.public void loadAll(@Nullable GridCacheTx tx, @Nullable Collection<? extends K> keys, GridBiInClosure<K,V> c) throws GridException
For every loaded value method GridInClosure.apply(Object) should be called on
the passed in closure. The closure will then make sure that the loaded value is stored
in cache.
loadAll in interface GridCacheStore<K,V>tx - Cache transaction.keys - Collection of keys to load.c - Closure to call for every loaded element.GridException - If load failed.public V load(@Nullable GridCacheTx tx, K key) throws GridException
load in interface GridCacheStore<K,V>tx - Cache transaction.key - Key to load.null if value was not found.GridException - If load failed.public void putAll(@Nullable GridCacheTx tx, @Nullable Map<? extends K,? extends V> map) throws GridException
null.putAll in interface GridCacheStore<K,V>tx - Cache transaction, if write-behind is not enabled, null otherwise.map - Values to store.GridException - If store failed.public void put(@Nullable GridCacheTx tx, K key, V val) throws GridException
null.put in interface GridCacheStore<K,V>tx - Cache transaction, if write-behind is not enabled, null otherwise.key - Key to put.val - Value to put.GridException - If put failed.public void removeAll(@Nullable GridCacheTx tx, @Nullable Collection<? extends K> keys) throws GridException
null.removeAll in interface GridCacheStore<K,V>tx - Cache transaction, if write-behind is not enabled, null otherwise.keys - Keys to remove.GridException - If remove failed.public void remove(@Nullable GridCacheTx tx, K key) throws GridException
null.remove in interface GridCacheStore<K,V>tx - Cache transaction, if write-behind is not enabled, null otherwise.key - Key to remove.GridException - If remove failed.public void txEnd(GridCacheTx tx, boolean commit) throws GridException
'commit'
parameter.
Note that if explicit transactions are not used in code, then it is possible
to commit or rollback transactions directly in 'put(..)', or 'remove(..)'
methods. In that case, this method should be left empty (GridCacheStoreAdapter provides
empty implementation of this method).
txEnd in interface GridCacheStore<K,V>tx - Cache transaction being ended.commit - True if transaction should commit, false for rollback.GridException - If commit or rollback failed. Note that commit failure in some cases
may bring cache transaction into GridCacheTxState.UNKNOWN which will
consequently cause all transacted entries to be invalidated.Copyright © 2014. All rights reserved.