Class PutFromLoadValidator
InvalidationCacheAccessDelegate to determine
whether a InvalidationCacheAccessDelegate.putFromLoad(Object, Object, Object, long, Object, boolean)
call should be allowed to update the cache. A putFromLoad has
the potential to store stale data, since the data may have been removed from the
database and the cache between the time when the data was read from the database
and the actual call to putFromLoad.
The expected usage of this class by a thread that read the cache and did not find data is:
- Call
registerPendingPut(Object, Object, long) - Read the database
- Call
acquirePutFromLoadLock(Object, Object, long) - if above returns
null, the thread should not cache the data; only if above returns instance ofAcquiredLock, put data in the cache and... - then call
releasePutFromLoadLock(Object, Lock)
The expected usage by a thread that is taking an action such that any pending
putFromLoad may have stale data and should not cache it is to either
call
-
beginInvalidatingKey(Object, Object)(for a single key invalidation) - or
beginInvalidatingRegion()followed byendInvalidatingRegion()(for a general invalidation all pending puts)
endInvalidatingKey(Object, Object) should
be called in order to allow further attempts to cache entry.
This class also supports the concept of "naked puts", which are calls to
acquirePutFromLoadLock(Object, Object, long) without a preceding registerPendingPut(Object, Object, long).
Besides not acquiring lock in registerPendingPut(Object, Object, long) this can happen when collection
elements are loaded after the collection has not been found in the cache, where the elements
don't have their own table but can be listed as 'select ... from Element where collection_id = ...'.
Naked puts are handled according to txTimestamp obtained by calling RegionFactory.nextTimestamp()
before the transaction is started. The timestamp is compared with timestamp of last invalidation end time
and the write to the cache is denied if it is lower or equal.
- Version:
- $Revision: $
- Author:
- Brian Stansberry
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classMarker for lock acquired inacquirePutFromLoadLock(Object, Object, long) -
Constructor Summary
ConstructorsConstructorDescriptionPutFromLoadValidator(org.infinispan.AdvancedCache cache, TimeSource timeSource, org.infinispan.configuration.cache.Configuration pendingPutsConfiguration) Creates a new put from load validator instance.PutFromLoadValidator(org.infinispan.AdvancedCache cache, TimeSource timeSource, org.infinispan.manager.EmbeddedCacheManager cacheManager, org.infinispan.configuration.cache.Configuration pendingPutsConfiguration) Creates a new put from load validator instance. -
Method Summary
Modifier and TypeMethodDescriptionacquirePutFromLoadLock(Object session, Object key, long txTimestamp) Acquire a lock giving the calling thread the right to put data in the cache for the given key.static voidaddToCache(org.infinispan.AdvancedCache cache, PutFromLoadValidator validator) Besides the call from constructor, this should be called only from tests when mocking the validator.booleanbeginInvalidatingKey(Object lockOwner, Object key) Invalidates anypreviously registered pending putsand disables further registrations ensuring a subsequent call toacquirePutFromLoadLock(Object, Object, long)will returnfalse.booleanInvalidates allpreviously registered pending putsensuring a subsequent call toacquirePutFromLoadLock(Object, Object, long)will returnfalse.booleanbeginInvalidatingWithPFER(Object lockOwner, Object key, Object valueForPFER) voiddestroy()booleanendInvalidatingKey(Object lockOwner, Object key) booleanendInvalidatingKey(Object lockOwner, Object key, boolean doPFER) Called after the transaction completes, allowing caching of entries.voidCalled when the region invalidation is finished.voidregisterPendingPut(Object session, Object key, long txTimestamp) Notifies this validator that it is expected that a database read followed by a subsequentacquirePutFromLoadLock(Object, Object, long)call will occur.voidReleases the lock previously obtained by a call toacquirePutFromLoadLock(Object, Object, long).static PutFromLoadValidatorremoveFromCache(org.infinispan.AdvancedCache cache) This methods should be called only from tests; it removes existing validator from the cache structures in order to replace it with new one.void
-
Constructor Details
-
PutFromLoadValidator
public PutFromLoadValidator(org.infinispan.AdvancedCache cache, TimeSource timeSource, org.infinispan.configuration.cache.Configuration pendingPutsConfiguration) Creates a new put from load validator instance.- Parameters:
pendingPutsConfiguration-cache- Cache instance on which to store pending put information.
-
PutFromLoadValidator
public PutFromLoadValidator(org.infinispan.AdvancedCache cache, TimeSource timeSource, org.infinispan.manager.EmbeddedCacheManager cacheManager, org.infinispan.configuration.cache.Configuration pendingPutsConfiguration) Creates a new put from load validator instance.- Parameters:
cache- Cache instance on which to store pending put information.timeSource-cacheManager- where to find a cache to store pending put informationpendingPutsConfiguration-
-
-
Method Details
-
addToCache
Besides the call from constructor, this should be called only from tests when mocking the validator. -
removeFromCache
This methods should be called only from tests; it removes existing validator from the cache structures in order to replace it with new one.- Parameters:
cache-
-
destroy
public void destroy() -
acquirePutFromLoadLock
public PutFromLoadValidator.Lock acquirePutFromLoadLock(Object session, Object key, long txTimestamp) Acquire a lock giving the calling thread the right to put data in the cache for the given key.NOTE: A call to this method that returns
trueshould always be matched with a call toreleasePutFromLoadLock(Object, Lock).- Parameters:
session-key- the keytxTimestamp-- Returns:
AcquiredLockif the lock is acquired and the cache put can proceed;nullif the data should not be cached
-
releasePutFromLoadLock
Releases the lock previously obtained by a call toacquirePutFromLoadLock(Object, Object, long).- Parameters:
key- the key
-
beginInvalidatingRegion
public boolean beginInvalidatingRegion()Invalidates allpreviously registered pending putsensuring a subsequent call toacquirePutFromLoadLock(Object, Object, long)will returnfalse.This method will block until any concurrent thread that has
acquired the putFromLoad lockfor the any key has released the lock. This allows the caller to be certain the putFromLoad will not execute after this method returns, possibly caching stale data.- Returns:
trueif the invalidation was successful;falseif a problem occurred (which the caller should treat as an exception condition)
-
endInvalidatingRegion
public void endInvalidatingRegion()Called when the region invalidation is finished. -
registerPendingPut
Notifies this validator that it is expected that a database read followed by a subsequentacquirePutFromLoadLock(Object, Object, long)call will occur. The intent is this method would be called following a cache miss wherein it is expected that a database read plus cache put will occur. Calling this method allows the validator to treat the subsequentacquirePutFromLoadLockas if the database read occurred when this method was invoked. This allows the validator to compare the timestamp of this call against the timestamp of subsequent removal notifications.- Parameters:
session-key- key that will be used for subsequent cache puttxTimestamp-
-
beginInvalidatingKey
Invalidates anypreviously registered pending putsand disables further registrations ensuring a subsequent call toacquirePutFromLoadLock(Object, Object, long)will returnfalse.This method will block until any concurrent thread that has
After this transaction completes,acquired the putFromLoad lockfor the given key has released the lock. This allows the caller to be certain the putFromLoad will not execute after this method returns, possibly caching stale data.endInvalidatingKey(Object, Object)needs to be called }- Parameters:
key- key identifying data whose pending puts should be invalidated- Returns:
trueif the invalidation was successful;falseif a problem occurred (which the caller should treat as an exception condition)
-
beginInvalidatingWithPFER
-
endInvalidatingKey
-
endInvalidatingKey
Called after the transaction completes, allowing caching of entries. It is possible that this method is called without previous invocation ofbeginInvalidatingKey(Object, Object), then it should be a no-op.- Parameters:
lockOwner- owner of the invalidation - transaction or threadkey-- Returns:
-
removePendingPutsCache
public void removePendingPutsCache()
-