Class 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.

    GridCacheWriteBehindStore doesn't support concurrent modifications of the same key.

    • 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 - 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.
      • 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 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
      • 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.