Search in sources :

Example 31 with EntryNotFoundException

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;
    }
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) EntryEvent(org.apache.geode.cache.EntryEvent) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) Region(org.apache.geode.cache.Region) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 32 with EntryNotFoundException

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;
}
Also used : Released(org.apache.geode.internal.offheap.annotations.Released) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException)

Example 33 with EntryNotFoundException

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();
    }
}
Also used : Released(org.apache.geode.internal.offheap.annotations.Released) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) StoredObject(org.apache.geode.internal.offheap.StoredObject)

Example 34 with EntryNotFoundException

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();
    }
}
Also used : Released(org.apache.geode.internal.offheap.annotations.Released) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) InternalGemFireError(org.apache.geode.InternalGemFireError) CacheWriterException(org.apache.geode.cache.CacheWriterException) TimeoutException(org.apache.geode.cache.TimeoutException)

Example 35 with EntryNotFoundException

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();
    }
}
Also used : Released(org.apache.geode.internal.offheap.annotations.Released) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) ReplyException(org.apache.geode.distributed.internal.ReplyException)

Aggregations

EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)78 Region (org.apache.geode.cache.Region)27 LocalRegion (org.apache.geode.internal.cache.LocalRegion)20 Test (org.junit.Test)18 CacheException (org.apache.geode.cache.CacheException)14 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)13 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)13 VersionTag (org.apache.geode.internal.cache.versions.VersionTag)13 Released (org.apache.geode.internal.offheap.annotations.Released)12 RegionDestroyedException (org.apache.geode.cache.RegionDestroyedException)10 StoredObject (org.apache.geode.internal.offheap.StoredObject)10 AttributesFactory (org.apache.geode.cache.AttributesFactory)9 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)9 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8 CacheWriterException (org.apache.geode.cache.CacheWriterException)8 Entry (org.apache.geode.cache.Region.Entry)8 TransactionDataNotColocatedException (org.apache.geode.cache.TransactionDataNotColocatedException)8 ConcurrentCacheModificationException (org.apache.geode.internal.cache.versions.ConcurrentCacheModificationException)8 Host (org.apache.geode.test.dunit.Host)8