Search in sources :

Example 21 with CacheWriterException

use of org.apache.geode.cache.CacheWriterException in project geode by apache.

the class DestroyMessage method operateOnPartitionedRegion.

/**
   * 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
   */
@Override
protected boolean operateOnPartitionedRegion(DistributionManager dm, PartitionedRegion r, long startTime) throws EntryExistsException, DataLocationException {
    InternalDistributedMember eventSender = originalSender;
    if (eventSender == null) {
        eventSender = getSender();
    }
    @Released EntryEventImpl event = null;
    try {
        if (this.bridgeContext != null) {
            event = EntryEventImpl.create(r, getOperation(), this.key, null, /* newValue */
            getCallbackArg(), false, /* originRemote */
            eventSender, true);
            event.setContext(this.bridgeContext);
        } else // bridgeContext != null
        {
            event = EntryEventImpl.create(r, getOperation(), this.key, null, /* newValue */
            getCallbackArg(), false, /* originRemote - false to force distribution in buckets */
            eventSender, true, /* generateCallbacks */
            false);
        }
        if (this.versionTag != null) {
            this.versionTag.replaceNullIDs(getSender());
            event.setVersionTag(this.versionTag);
        }
        event.setInvokePRCallbacks(!notificationOnly);
        Assert.assertTrue(eventId != null);
        event.setEventId(eventId);
        event.setPossibleDuplicate(this.posDup);
        PartitionedRegionDataStore ds = r.getDataStore();
        boolean sendReply = true;
        if (!notificationOnly) {
            Assert.assertTrue(ds != null, "This process should have storage for an item in " + this.toString());
            try {
                Integer bucket = Integer.valueOf(PartitionedRegionHelper.getHashKey(r, null, this.key, null, this.cbArg));
                // try {
                // // the event must show its true origin for cachewriter invocation
                // event.setOriginRemote(true);
                // event.setPartitionMessage(this);
                // r.doCacheWriteBeforeDestroy(event);
                // }
                // finally {
                // event.setOriginRemote(false);
                // }
                event.setCausedByMessage(this);
                r.getDataView().destroyOnRemote(event, true, /* cacheWrite */
                this.expectedOldValue);
                if (logger.isTraceEnabled(LogMarker.DM)) {
                    logger.trace(LogMarker.DM, "{} updated bucket: {} with key: {}", getClass().getName(), bucket, this.key);
                }
            } catch (CacheWriterException cwe) {
                sendReply(getSender(), this.processorId, dm, new ReplyException(cwe), r, startTime);
                return false;
            } catch (EntryNotFoundException eee) {
                logger.trace(LogMarker.DM, "{}: operateOnRegion caught EntryNotFoundException", getClass().getName());
                ReplyMessage.send(getSender(), getProcessorId(), new ReplyException(eee), getReplySender(dm), r.isInternalRegion());
                // this prevents us from acking later
                sendReply = false;
            } catch (PrimaryBucketException pbe) {
                sendReply(getSender(), getProcessorId(), dm, new ReplyException(pbe), r, startTime);
                sendReply = false;
            } finally {
                this.versionTag = event.getVersionTag();
            }
        } else {
            @Released EntryEventImpl e2 = createListenerEvent(event, r, dm.getDistributionManagerId());
            try {
                r.invokeDestroyCallbacks(EnumListenerEvent.AFTER_DESTROY, e2, r.isInitialized(), true);
            } finally {
                // if e2 == ev then no need to free it here. The outer finally block will get it.
                if (e2 != event) {
                    e2.release();
                }
            }
        }
        return sendReply;
    } finally {
        if (event != null) {
            event.release();
        }
    }
}
Also used : Released(org.apache.geode.internal.offheap.annotations.Released) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) EntryEventImpl(org.apache.geode.internal.cache.EntryEventImpl) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) PartitionedRegionDataStore(org.apache.geode.internal.cache.PartitionedRegionDataStore) PrimaryBucketException(org.apache.geode.internal.cache.PrimaryBucketException) ReplyException(org.apache.geode.distributed.internal.ReplyException) CacheWriterException(org.apache.geode.cache.CacheWriterException)

Example 22 with CacheWriterException

use of org.apache.geode.cache.CacheWriterException in project geode by apache.

the class CacheLoaderTestCase method testCacheWriterOnLoad.

/**
   * Tests that a <code>CacheWriter</code> gets invoked on a <code>load</code>.
   */
@Test
public void testCacheWriterOnLoad() throws CacheException {
    final String name = this.getUniqueName();
    final Object key = this.getUniqueName();
    final Object oldValue = new Integer(42);
    final Object newValue = new Integer(43);
    TestCacheLoader loader = new TestCacheLoader() {

        public Object load2(LoaderHelper helper) throws CacheLoaderException {
            return oldValue;
        }
    };
    TestCacheWriter writer = new TestCacheWriter() {

        public void beforeCreate2(EntryEvent event) throws CacheWriterException {
            assertEquals(oldValue, event.getNewValue());
            assertTrue(event.getOperation().isLoad());
            assertTrue(event.getOperation().isLocalLoad());
            assertFalse(event.getOperation().isNetLoad());
            assertFalse(event.getOperation().isNetSearch());
        }
    };
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    factory.setCacheLoader(loader);
    factory.setCacheWriter(writer);
    Region region = createRegion(name, factory.create());
    loader.wasInvoked();
    assertEquals(oldValue, region.get(key));
    assertTrue(loader.wasInvoked());
    assertTrue(writer.wasInvoked());
    writer = new TestCacheWriter() {

        public void beforeUpdate2(EntryEvent event) throws CacheWriterException {
            assertEquals(oldValue, event.getOldValue());
            assertEquals(newValue, event.getNewValue());
            assertFalse(event.getOperation().isLoad());
            assertFalse(event.getOperation().isLocalLoad());
            assertFalse(event.getOperation().isNetLoad());
            assertFalse(event.getOperation().isNetSearch());
        }
    };
    region.getAttributesMutator().setCacheWriter(writer);
    region.put(key, newValue);
    assertFalse(loader.wasInvoked());
    assertTrue(writer.wasInvoked());
}
Also used : LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) EntryEvent(org.apache.geode.cache.EntryEvent) Region(org.apache.geode.cache.Region) CacheWriterException(org.apache.geode.cache.CacheWriterException) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 23 with CacheWriterException

use of org.apache.geode.cache.CacheWriterException in project geode by apache.

the class LocalRegion method basicDestroyRegion.

void basicDestroyRegion(RegionEventImpl event, boolean cacheWrite, boolean lock, boolean callbackEvents) throws CacheWriterException, TimeoutException {
    preDestroyChecks();
    final TXStateProxy tx = this.cache.getTXMgr().internalSuspend();
    try {
        boolean acquiredLock = false;
        if (lock) {
            try {
                acquireDestroyLock();
                acquiredLock = true;
            } catch (CancelException ignore) {
                if (logger.isDebugEnabled()) {
                    logger.debug("basicDestroyRegion: acquireDestroyLock failed due to cache closure, region = {}", getFullPath());
                }
            }
        }
        try {
            // maintain destroy lock and TXStateInterface
            // I moved checkRegionDestroyed up out of the following
            // try block because it does not seem correct to deliver
            // a destroy event to the clients of the region was already
            // destroyed on the server.
            checkRegionDestroyed(false);
            // see bug 47736
            boolean cancelledByCacheWriterException = false;
            HashSet eventSet = null;
            try {
                // ensure that destroy events are dispatched
                if (this instanceof PartitionedRegion && !((PartitionedRegion) this).getParallelGatewaySenderIds().isEmpty()) {
                    ((PartitionedRegion) this).destroyParallelGatewaySenderRegion(event.getOperation(), cacheWrite, lock, callbackEvents);
                }
                if (this.parentRegion != null) {
                    // "Bubble up" the cache statistics to parent if this regions are more recent
                    this.parentRegion.updateStats();
                }
                try {
                    eventSet = callbackEvents ? new HashSet() : null;
                    this.destroyedSubregionSerialNumbers = collectSubregionSerialNumbers();
                    recursiveDestroyRegion(eventSet, event, cacheWrite);
                } catch (CancelException e) {
                    // a serious problem.
                    if (!this.cache.forcedDisconnect()) {
                        logger.warn(LocalizedMessage.create(LocalizedStrings.LocalRegion_RECURSIVEDESTROYREGION_RECURSION_FAILED_DUE_TO_CACHE_CLOSURE_REGION_0, getFullPath()), e);
                    }
                } catch (CacheWriterException cwe) {
                    cancelledByCacheWriterException = true;
                    throw cwe;
                }
                // at this point all subregions are destroyed and this region has been marked as destroyed
                // and postDestroyRegion has been called for each region. The only detail left is
                // unhooking this region from the parent subregion map, and sending listener events
                Assert.assertTrue(this.isDestroyed);
                // artifacts From Management Layer
                if (!isInternalRegion()) {
                    InternalDistributedSystem system = this.cache.getInternalDistributedSystem();
                    system.handleResourceEvent(ResourceEvent.REGION_REMOVE, this);
                }
                try {
                    LocalRegion parent = this.parentRegion;
                    if (parent == null) {
                        this.cache.removeRoot(this);
                    } else {
                        parent.subregions.remove(this.regionName, this);
                    }
                } catch (CancelException e) {
                    // I don't think this should ever happens: bulletproofing for bug 39454
                    if (!this.cache.forcedDisconnect()) {
                        logger.warn(LocalizedMessage.create(LocalizedStrings.LocalRegion_BASICDESTROYREGION_PARENT_REMOVAL_FAILED_DUE_TO_CACHE_CLOSURE_REGION_0, getFullPath()), e);
                    }
                }
            } finally {
                // ensure that destroy events are dispatched
                if (!cancelledByCacheWriterException) {
                    // We only need to notify bridgeClients of the top level region destroy
                    // which it will take and do a localRegionDestroy.
                    // So we pass it event and NOT eventSet
                    event.setEventType(EnumListenerEvent.AFTER_REGION_DESTROY);
                    notifyBridgeClients(event);
                }
                // since some of the destroys happened.
                if (eventSet != null && callbackEvents) {
                    try {
                        sendPendingRegionDestroyEvents(eventSet);
                    } catch (CancelException ignore) {
                    // ignore, we're mute.
                    }
                }
            }
        } finally {
            if (acquiredLock) {
                try {
                    releaseDestroyLock();
                } catch (CancelException ignore) {
                // ignore
                }
            }
        }
    } finally {
        this.cache.getTXMgr().internalResume(tx);
    }
}
Also used : CancelException(org.apache.geode.CancelException) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) HashSet(java.util.HashSet) CacheWriterException(org.apache.geode.cache.CacheWriterException)

Example 24 with CacheWriterException

use of org.apache.geode.cache.CacheWriterException 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 25 with CacheWriterException

use of org.apache.geode.cache.CacheWriterException in project geode by apache.

the class RemoteDestroyMessage 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
   */
@Override
protected boolean operateOnRegion(DistributionManager dm, LocalRegion r, long startTime) throws EntryExistsException, RemoteOperationException {
    InternalDistributedMember eventSender = originalSender;
    if (eventSender == null) {
        eventSender = getSender();
    }
    @Released EntryEventImpl event = null;
    try {
        if (this.bridgeContext != null) {
            event = EntryEventImpl.create(r, getOperation(), getKey(), null, /* newValue */
            getCallbackArg(), false, /* originRemote */
            eventSender, true);
            event.setContext(this.bridgeContext);
            // for cq processing and client notification by BS.
            if (this.hasOldValue) {
                if (this.oldValueIsSerialized) {
                    event.setSerializedOldValue(getOldValueBytes());
                } else {
                    event.setOldValue(getOldValueBytes());
                }
            }
        } else // bridgeContext != null
        {
            event = EntryEventImpl.create(r, getOperation(), getKey(), null, /* newValue */
            getCallbackArg(), this.useOriginRemote, eventSender, true, /* generateCallbacks */
            false);
        }
        event.setCausedByMessage(this);
        if (this.versionTag != null) {
            this.versionTag.replaceNullIDs(getSender());
            event.setVersionTag(this.versionTag);
        }
        // for cq processing and client notification by BS.
        if (this.hasOldValue) {
            if (this.oldValueIsSerialized) {
                event.setSerializedOldValue(getOldValueBytes());
            } else {
                event.setOldValue(getOldValueBytes());
            }
        }
        Assert.assertTrue(eventId != null);
        event.setEventId(eventId);
        event.setPossibleDuplicate(this.possibleDuplicate);
        try {
            r.getDataView().destroyOnRemote(event, true, this.expectedOldValue);
            sendReply(dm, event.getVersionTag());
        } catch (CacheWriterException cwe) {
            sendReply(getSender(), this.processorId, dm, new ReplyException(cwe), r, startTime);
            return false;
        } catch (EntryNotFoundException eee) {
            if (logger.isDebugEnabled()) {
                logger.debug("operateOnRegion caught EntryNotFoundException", eee);
            }
            ReplyMessage.send(getSender(), getProcessorId(), new ReplyException(eee), getReplySender(dm), r.isInternalRegion());
        } catch (DataLocationException e) {
            if (logger.isDebugEnabled()) {
                logger.debug("operateOnRegion caught DataLocationException");
            }
            ReplyMessage.send(getSender(), getProcessorId(), new ReplyException(e), getReplySender(dm), r.isInternalRegion());
        }
        return false;
    } finally {
        if (event != null) {
            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) CacheWriterException(org.apache.geode.cache.CacheWriterException)

Aggregations

CacheWriterException (org.apache.geode.cache.CacheWriterException)50 Region (org.apache.geode.cache.Region)22 Test (org.junit.Test)14 EntryEvent (org.apache.geode.cache.EntryEvent)13 Released (org.apache.geode.internal.offheap.annotations.Released)12 AttributesFactory (org.apache.geode.cache.AttributesFactory)11 CacheException (org.apache.geode.cache.CacheException)11 TimeoutException (org.apache.geode.cache.TimeoutException)11 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)10 CacheWriterAdapter (org.apache.geode.cache.util.CacheWriterAdapter)9 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)9 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)8 VM (org.apache.geode.test.dunit.VM)8 Cache (org.apache.geode.cache.Cache)7 RegionEvent (org.apache.geode.cache.RegionEvent)7 Host (org.apache.geode.test.dunit.Host)7 IOException (java.io.IOException)6 CacheWriter (org.apache.geode.cache.CacheWriter)6 EntryEventImpl (org.apache.geode.internal.cache.EntryEventImpl)6 InternalGemFireError (org.apache.geode.InternalGemFireError)5