org.apache.ignite.internal.processors.cache.store

## 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 a `CacheStore` 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.

### Field Summary

Fields
`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 Summary

Constructors
Constructor and Description
`GridCacheWriteBehindStore(CacheStoreManager storeMgr, String gridName, String cacheName, IgniteLogger log, CacheStore<K,V> store)` Creates a write-behind cache store for the given store.

### Method Summary

Methods
`void` `delete(Object key)`
`void` `deleteAll(Collection<?> keys)`
`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(K key)`
`Map<K,V>` `loadAll(Iterable<? extends K> keys)`
`void` `loadCache(IgniteBiInClosure<K,V> clo, Object... args)` Loads all values from underlying persistent storage.
`void` `sessionEnd(boolean commit)` Tells store to commit or rollback a transaction depending on the value of the `'commit'` parameter.
`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.
`CacheStore<K,V>` `store()`
`String` `toString()`
`void` `write(javax.cache.Cache.Entry<? extends K,? extends V> entry)`
`void` `writeAll(Collection<javax.cache.Cache.Entry<? extends K,? extends V>> entries)`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### 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 gridName,
                             String cacheName,
                             IgniteLogger log,
                             CacheStore<K,V> store)

Creates a write-behind cache store for the given store.

Parameters:
  `storeMgr` - Store manager.
  `gridName` - Grid name.
  `cacheName` - Cache name.
  `log` - Grid logger.
  `store` - `GridCacheStore` that 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 is `0`, then flush is performed only on time-elapsing basis. However, when this value is `0`, the cache critical size is set to `CacheConfiguration.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.

#### 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 is `0`, 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:
  `start` in interface `LifecycleAware`

#### 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:
  `stop` in interface `LifecycleAware`

#### forceFlush

    public void forceFlush()
                    throws IgniteCheckedException

Forces all entries collected to be flushed to the underlying store.

Throws:
  `IgniteCheckedException` - If failed.

#### loadCache

    public void loadCache(IgniteBiInClosure<K,V> clo,
                 @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 whenever `IgniteCache.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 `CacheStoreAdapter` does 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:
  `loadCache` in interface `CacheStore<K,V>`
Parameters:
  `clo` - Closure for loaded values.
  `args` - Arguments passes into `IgniteCache.loadCache(IgniteBiPredicate, Object...)` method.

#### loadAll

    public Map<K,V> loadAll(Iterable<? extends K> keys)

Specified by:
  `loadAll` in interface `javax.cache.integration.CacheLoader<K,V>`

#### load

    public V load(K key)

Specified by:
  `load` in interface `javax.cache.integration.CacheLoader<K,V>`

#### writeAll

    public void writeAll(Collection<javax.cache.Cache.Entry<? extends K,? extends V>> entries)

Specified by:
  `writeAll` in interface `javax.cache.integration.CacheWriter<K,V>`

#### write

    public void write(javax.cache.Cache.Entry<? extends K,? extends V> entry)

Specified by:
  `write` in interface `javax.cache.integration.CacheWriter<K,V>`

#### deleteAll

    public void deleteAll(Collection<?> keys)

Specified by:
  `deleteAll` in interface `javax.cache.integration.CacheWriter<K,V>`

#### delete

    public void delete(Object key)

Specified by:
  `delete` in interface `javax.cache.integration.CacheWriter<K,V>`

#### sessionEnd

    public void sessionEnd(boolean commit)

Tells store to commit or rollback a transaction depending on the value of the `'commit'` parameter.

Specified by:
  `sessionEnd` in interface `CacheStore<K,V>`
Parameters:
  `commit` - `True` if transaction should commit, `false` for rollback.

#### toString

    public String toString()

Overrides:
  `toString` in class `Object`

2015 Copyright © Apache Software Foundation

Follow @ApacheIgnite

Ignite Fabric | : ver. 1.3.0-incubating
Release Date | : July 10 2015