use of org.apache.geode.cache.EntryNotFoundException in project geode by apache.
the class CacheListenerTestCase method testCacheListenerAfterInvalidateWithForce.
@Test
public void testCacheListenerAfterInvalidateWithForce() throws CacheException {
AbstractRegionMap.FORCE_INVALIDATE_EVENT = true;
try {
String name = this.getUniqueName();
final Object key = this.getUniqueName();
final Object value = new Integer(42);
TestCacheListener listener = new TestCacheListener() {
int invalidateCount = 0;
public void afterCreate2(EntryEvent event) {
// This method will get invoked when the region is populated
}
public void afterInvalidate2(EntryEvent event) {
invalidateCount++;
assertEquals(key, event.getKey());
if (invalidateCount == 2) {
assertEquals(value, event.getOldValue());
} else {
assertNull(event.getOldValue());
}
assertNull(event.getNewValue());
verifyEventProps(event);
}
};
AttributesFactory factory = new AttributesFactory(getRegionAttributes());
factory.setCacheListener(listener);
Region region = createRegion(name, factory.create());
// Does not exist but should still invoke listener
try {
region.invalidate(key);
fail("expected EntryNotFoundException");
} catch (EntryNotFoundException expected) {
}
assertTrue(listener.wasInvoked());
region.create(key, value);
assertTrue(listener.wasInvoked());
region.invalidate(key);
assertTrue(listener.wasInvoked());
// already invalid but should still invoke listener
region.invalidate(key);
assertTrue(listener.wasInvoked());
} finally {
AbstractRegionMap.FORCE_INVALIDATE_EVENT = false;
}
}
use of org.apache.geode.cache.EntryNotFoundException in project geode by apache.
the class LocalRegion method remove.
/**
* Same as {@link #remove(Object, Object)} except a callback argument is supplied to be passed on
* to <tt>CacheListener</tt>s and/or <tt>CacheWriter</tt>s.
*/
public boolean remove(Object key, Object value, Object callbackArg) {
checkIfConcurrentMapOpsAllowed();
validateKey(key);
validateCallbackArg(callbackArg);
checkReadiness();
checkForLimitedOrNoAccess();
if (value == null) {
value = Token.INVALID;
}
@Released EntryEventImpl event = EntryEventImpl.create(this, Operation.REMOVE, key, null, callbackArg, false, getMyId());
try {
if (generateEventID() && event.getEventId() == null) {
event.setNewEventId(this.cache.getDistributedSystem());
}
discoverJTA();
getDataView().destroyExistingEntry(event, true, value);
} catch (EntryNotFoundException ignore) {
return false;
} catch (RegionDestroyedException rde) {
if (!rde.getRegionFullPath().equals(getFullPath())) {
// Handle when a bucket is destroyed
throw new RegionDestroyedException(toString(), getFullPath(), rde);
} else {
throw rde;
}
} finally {
event.release();
}
return true;
}
use of org.apache.geode.cache.EntryNotFoundException in project geode by apache.
the class LocalRegion method putIfAbsent.
/**
* If the specified key is not already associated with a value, associate it with the given value.
* This is equivalent to
*
* <pre>
* if (!region.containsKey(key))
* return region.put(key, value);
* else
* return region.get(key);
* </pre>
*
* Except that the action is performed atomically.
*
* <i>Note that if this method returns null then there is no way to determine definitely whether
* this operation succeeded and modified the region, or if the entry is in an invalidated state
* and no modification occurred.</i>
*
* If this method does not modify the region then no listeners or other callbacks are executed. If
* a modification does occur, then the behavior with respect to callbacks is the same as
* {@link Region#create(Object, Object)}.
*
* @param key key with which the specified value is to be associated.
* @param value the value for the new entry, which may be null meaning the new entry starts as if
* it had been locally invalidated.
* @return previous value associated with specified key, or <tt>null</tt> if there was no mapping
* for key. A <tt>null</tt> return can also indicate that the entry in the region was
* previously in an invalidated state.
*
* @throws ClassCastException if key does not satisfy the keyConstraint
* @throws IllegalArgumentException if the key or value is not serializable and this is a
* distributed region
* @throws TimeoutException if timed out getting distributed lock for {@code Scope.GLOBAL}
* @throws NullPointerException if key is <tt>null</tt>
* @throws PartitionedRegionStorageException if the operation could not be completed.
*/
public Object putIfAbsent(Object key, Object value, Object callbackArgument) {
long startPut = CachePerfStats.getStatTime();
checkIfConcurrentMapOpsAllowed();
validateArguments(key, value, callbackArgument);
// TODO ConcurrentMap.putIfAbsent() treats null as an invalidation operation
// BUT we need to return the old value, which Invalidate isn't currently doing
checkReadiness();
checkForLimitedOrNoAccess();
discoverJTA();
// This used to call the constructor which took the old value. It
// was modified to call the other EntryEventImpl constructor so that
// an id will be generated by default. Null was passed in anyway.
// generate EventID
@Released EntryEventImpl event = EntryEventImpl.create(this, Operation.PUT_IF_ABSENT, key, value, callbackArgument, false, getMyId());
try {
if (generateEventID()) {
event.setNewEventId(this.cache.getDistributedSystem());
}
final Object oldValue = null;
final boolean ifNew = true;
final boolean ifOld = false;
final boolean requireOldValue = true;
if (!basicPut(event, ifNew, ifOld, oldValue, requireOldValue)) {
return event.getOldValue();
} else {
if (!getDataView().isDeferredStats()) {
getCachePerfStats().endPut(startPut, false);
}
return null;
}
} catch (EntryNotFoundException ignore) {
return event.getOldValue();
} finally {
event.release();
}
}
use of org.apache.geode.cache.EntryNotFoundException in project geode by apache.
the class LocalRegion method localDestroyNoCallbacks.
/**
* WARNING: this method is overridden in subclasses.
*/
protected void localDestroyNoCallbacks(Object key) {
if (logger.isDebugEnabled()) {
logger.debug("localDestroyNoCallbacks key={}", key);
}
checkReadiness();
validateKey(key);
@Released EntryEventImpl event = EntryEventImpl.create(this, Operation.LOCAL_DESTROY, key, false, getMyId(), false, /* generateCallbacks */
true);
try {
// expectedOldValue
basicDestroy(event, false, null);
} catch (CacheWriterException e) {
// cache writer not called
throw new Error(LocalizedStrings.LocalRegion_CACHE_WRITER_SHOULD_NOT_HAVE_BEEN_CALLED_FOR_LOCALDESTROY.toLocalizedString(), e);
} catch (TimeoutException e) {
// no distributed lock
throw new Error(LocalizedStrings.LocalRegion_NO_DISTRIBUTED_LOCK_SHOULD_HAVE_BEEN_ATTEMPTED_FOR_LOCALDESTROY.toLocalizedString(), e);
} catch (EntryNotFoundException ignore) {
// not a problem
} finally {
event.release();
}
}
use of org.apache.geode.cache.EntryNotFoundException in project geode by apache.
the class RemoteInvalidateMessage method operateOnRegion.
/**
* This method is called upon receipt and make the desired changes to the PartitionedRegion Note:
* It is very important that this message does NOT cause any deadlocks as the sender will wait
* indefinitely for the acknowledgement
*
* @throws EntryExistsException
*/
@Override
protected boolean operateOnRegion(DistributionManager dm, LocalRegion r, long startTime) throws EntryExistsException, RemoteOperationException {
InternalDistributedMember eventSender = originalSender;
if (eventSender == null) {
eventSender = getSender();
}
final Object key = getKey();
@Released final EntryEventImpl event = EntryEventImpl.create(r, getOperation(), key, null, /* newValue */
getCallbackArg(), this.useOriginRemote, /* originRemote - false to force distribution in buckets */
eventSender, true, /* generateCallbacks */
false);
try {
if (this.bridgeContext != null) {
event.setContext(this.bridgeContext);
}
event.setCausedByMessage(this);
if (this.versionTag != null) {
this.versionTag.replaceNullIDs(getSender());
event.setVersionTag(this.versionTag);
}
Assert.assertTrue(eventId != null);
event.setEventId(eventId);
event.setPossibleDuplicate(this.possibleDuplicate);
// for cqs, which needs old value based on old value being sent on wire.
boolean eventShouldHaveOldValue = getHasOldValue();
if (eventShouldHaveOldValue) {
if (getOldValueIsSerialized()) {
event.setSerializedOldValue(getOldValueBytes());
} else {
event.setOldValue(getOldValueBytes());
}
}
boolean sendReply = true;
try {
r.checkReadiness();
r.checkForLimitedOrNoAccess();
r.basicInvalidate(event);
if (logger.isTraceEnabled(LogMarker.DM)) {
logger.trace(LogMarker.DM, "remoteInvalidated key: {}", key);
}
sendReply(getSender(), this.processorId, dm, /* ex */
null, event.getRegion(), event.getVersionTag(), startTime);
sendReply = false;
} catch (EntryNotFoundException eee) {
// failed = true;
if (logger.isDebugEnabled()) {
logger.debug("operateOnRegion caught EntryNotFoundException");
}
sendReply(getSender(), getProcessorId(), dm, new ReplyException(eee), r, null, startTime);
// this prevents us from acking later
sendReply = false;
} catch (PrimaryBucketException pbe) {
sendReply(getSender(), getProcessorId(), dm, new ReplyException(pbe), r, startTime);
return false;
}
return sendReply;
} finally {
event.release();
}
}
Aggregations