public class PutFromLoadValidator extends Object
InvalidationCacheAccessDelegate
to determine
whether a InvalidationCacheAccessDelegate.putFromLoad(org.hibernate.engine.spi.SessionImplementor, 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:
registerPendingPut(SessionImplementor, Object, long)
acquirePutFromLoadLock(SessionImplementor, Object, long)
null
, the thread should not cache the data;
only if above returns instance of AcquiredLock
, put data in the cache and...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)beginInvalidatingRegion()
followed by endInvalidatingRegion()
(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(SessionImplementor, Object, long)
without a preceding registerPendingPut(SessionImplementor, Object, long)
.
Besides not acquiring lock in registerPendingPut(SessionImplementor, 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.
Modifier and Type | Class and Description |
---|---|
static class |
PutFromLoadValidator.Lock
Marker for lock acquired in
acquirePutFromLoadLock(SessionImplementor, Object, long) |
Constructor and Description |
---|
PutFromLoadValidator(org.infinispan.AdvancedCache cache)
Creates a new put from load validator instance.
|
PutFromLoadValidator(org.infinispan.AdvancedCache cache,
org.infinispan.manager.EmbeddedCacheManager cacheManager)
Creates a new put from load validator instance.
|
Modifier and Type | Method and Description |
---|---|
PutFromLoadValidator.Lock |
acquirePutFromLoadLock(SessionImplementor session,
Object key,
long txTimestamp)
Acquire a lock giving the calling thread the right to put data in the
cache for the given key.
|
boolean |
beginInvalidatingKey(Object lockOwner,
Object key)
Invalidates any
previously registered pending puts
and disables further registrations ensuring a subsequent call to acquirePutFromLoadLock(SessionImplementor, Object, long)
will return false . |
boolean |
beginInvalidatingRegion()
Invalidates all
previously registered pending puts ensuring a subsequent call to
acquirePutFromLoadLock(SessionImplementor, Object, long) will return false . |
boolean |
beginInvalidatingWithPFER(Object lockOwner,
Object key,
Object valueForPFER) |
boolean |
endInvalidatingKey(Object lockOwner,
Object key) |
boolean |
endInvalidatingKey(Object lockOwner,
Object key,
boolean doPFER)
Called after the transaction completes, allowing caching of entries.
|
void |
endInvalidatingRegion()
Called when the region invalidation is finished.
|
void |
registerPendingPut(SessionImplementor session,
Object key,
long txTimestamp)
Notifies this validator that it is expected that a database read followed by a subsequent
acquirePutFromLoadLock(SessionImplementor, Object, long) call will occur. |
Object |
registerRemoteInvalidations(Object[] keys) |
void |
releasePutFromLoadLock(Object key,
PutFromLoadValidator.Lock lock)
Releases the lock previously obtained by a call to
acquirePutFromLoadLock(SessionImplementor, Object, long) . |
static void |
removeFromCache(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 |
resetCurrentSession() |
void |
setCurrentSession(SessionImplementor session) |
public PutFromLoadValidator(org.infinispan.AdvancedCache cache)
cache
- Cache instance on which to store pending put information.public PutFromLoadValidator(org.infinispan.AdvancedCache cache, org.infinispan.manager.EmbeddedCacheManager cacheManager)
cache
- Cache instance on which to store pending put information.cacheManager
- where to find a cache to store pending put informationpublic static void removeFromCache(org.infinispan.AdvancedCache cache)
cache
- public void setCurrentSession(SessionImplementor session)
public void resetCurrentSession()
public PutFromLoadValidator.Lock acquirePutFromLoadLock(SessionImplementor session, Object key, long txTimestamp)
NOTE: A call to this method that returns true
should always be matched with a call to releasePutFromLoadLock(Object, Lock)
.
session
- key
- the keytxTimestamp
- AcquiredLock
if the lock is acquired and the cache put
can proceed; null
if the data should not be cachedpublic void releasePutFromLoadLock(Object key, PutFromLoadValidator.Lock lock)
acquirePutFromLoadLock(SessionImplementor, Object, long)
.key
- the keypublic boolean beginInvalidatingRegion()
previously registered pending puts
ensuring a subsequent call to
acquirePutFromLoadLock(SessionImplementor, Object, long)
will return false
. This method will block until any
concurrent thread that has acquired the putFromLoad lock
for 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.
true
if the invalidation was successful; false
if a problem occured (which the
caller should treat as an exception condition)public void endInvalidatingRegion()
public void registerPendingPut(SessionImplementor session, Object key, long txTimestamp)
acquirePutFromLoadLock(SessionImplementor, 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 subsequent acquirePutFromLoadLock
as 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.session
- key
- key that will be used for subsequent cache puttxTimestamp
- public boolean beginInvalidatingKey(Object lockOwner, Object key)
previously registered pending puts
and disables further registrations ensuring a subsequent call to acquirePutFromLoadLock(SessionImplementor, Object, long)
will return false
. This method will block until any concurrent thread that has
acquired the putFromLoad lock
for 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 }key
- key identifying data whose pending puts should be invalidatedtrue
if the invalidation was successful; false
if a problem occured (which the
caller should treat as an exception condition)public boolean beginInvalidatingWithPFER(Object lockOwner, Object key, Object valueForPFER)
public boolean endInvalidatingKey(Object lockOwner, Object key, boolean doPFER)
beginInvalidatingKey(Object, Object)
, then it should be a no-op.lockOwner
- owner of the invalidation - transaction or threadkey
- Copyright © 2001-2017 Red Hat, Inc. All Rights Reserved.