Search in sources :

Example 46 with Released

use of org.apache.geode.internal.offheap.annotations.Released in project geode by apache.

the class EntryEventImpl method release.

@Override
@Released({ ENTRY_EVENT_NEW_VALUE, ENTRY_EVENT_OLD_VALUE })
public void release() {
    // noop if already freed or values can not be off-heap
    if (!this.offHeapOk)
        return;
    if (!mayHaveOffHeapReferences()) {
        return;
    }
    synchronized (this.offHeapLock) {
        // Note that this method does not set the old/new values to null but
        // leaves them set to the off-heap value so that future calls to getOld/NewValue
        // will fail with an exception.
        testHookReleaseInProgress();
        Object ov = basicGetOldValue();
        Object nv = basicGetNewValue();
        this.offHeapOk = false;
        if (ov instanceof StoredObject) {
            // System.identityHashCode(ov));
            if (ReferenceCountHelper.trackReferenceCounts()) {
                ReferenceCountHelper.setReferenceCountOwner(new OldValueOwner());
                ((Releasable) ov).release();
                ReferenceCountHelper.setReferenceCountOwner(null);
            } else {
                ((Releasable) ov).release();
            }
        }
        OffHeapHelper.releaseAndTrackOwner(nv, this);
    }
}
Also used : StoredObject(org.apache.geode.internal.offheap.StoredObject) StoredObject(org.apache.geode.internal.offheap.StoredObject) Releasable(org.apache.geode.internal.offheap.Releasable) Released(org.apache.geode.internal.offheap.annotations.Released)

Example 47 with Released

use of org.apache.geode.internal.offheap.annotations.Released in project geode by apache.

the class RemoteRemoveAllMessage method doLocalRemoveAll.

/* we need a event with content for waitForNodeOrCreateBucket() */
/**
   * This method is called by both operateOnLocalRegion() when processing a remote msg or by
   * sendMsgByBucket() when processing a msg targeted to local Jvm. LocalRegion Note: It is very
   * important that this message does NOT cause any deadlocks as the sender will wait indefinitely
   * for the acknowledgment
   * 
   * @param r partitioned region
   * @param eventSender the endpoint server who received request from client
   * @return If succeeds, return true, otherwise, throw exception
   */
public boolean doLocalRemoveAll(final LocalRegion r, final InternalDistributedMember eventSender) throws EntryExistsException, RemoteOperationException {
    final DistributedRegion dr = (DistributedRegion) r;
    // create a base event and a op for RemoveAllMessage distributed btw redundant buckets
    @Released EntryEventImpl baseEvent = EntryEventImpl.create(r, Operation.REMOVEALL_DESTROY, null, null, this.callbackArg, false, eventSender, true);
    try {
        baseEvent.setCausedByMessage(this);
        // set baseEventId to the first entry's event id. We need the thread id for DACE
        baseEvent.setEventId(this.eventId);
        if (this.bridgeContext != null) {
            baseEvent.setContext(this.bridgeContext);
        }
        baseEvent.setPossibleDuplicate(this.posDup);
        if (logger.isDebugEnabled()) {
            logger.debug("RemoteRemoveAllMessage.doLocalRemoveAll: eventSender is {}, baseEvent is {}, msg is {}", eventSender, baseEvent, this);
        }
        final DistributedRemoveAllOperation op = new DistributedRemoveAllOperation(baseEvent, removeAllDataCount, false);
        try {
            final VersionedObjectList versions = new VersionedObjectList(removeAllDataCount, true, dr.concurrencyChecksEnabled);
            dr.syncBulkOp(new Runnable() {

                @SuppressWarnings("synthetic-access")
                public void run() {
                    InternalDistributedMember myId = r.getDistributionManager().getDistributionManagerId();
                    for (int i = 0; i < removeAllDataCount; ++i) {
                        @Released EntryEventImpl ev = RemoveAllPRMessage.getEventFromEntry(r, myId, eventSender, i, removeAllData, false, bridgeContext, posDup, false);
                        try {
                            ev.setRemoveAllOperation(op);
                            if (logger.isDebugEnabled()) {
                                logger.debug("invoking basicDestroy with {}", ev);
                            }
                            try {
                                dr.basicDestroy(ev, true, null);
                            } catch (EntryNotFoundException ignore) {
                            }
                            removeAllData[i].versionTag = ev.getVersionTag();
                            versions.addKeyAndVersion(removeAllData[i].key, ev.getVersionTag());
                        } finally {
                            ev.release();
                        }
                    }
                }
            }, baseEvent.getEventId());
            if (getTXUniqId() != TXManagerImpl.NOTX || dr.getConcurrencyChecksEnabled()) {
                dr.getDataView().postRemoveAll(op, versions, dr);
            }
            RemoveAllReplyMessage.send(getSender(), this.processorId, getReplySender(r.getDistributionManager()), versions, this.removeAllData, this.removeAllDataCount);
            return false;
        } finally {
            op.freeOffHeapResources();
        }
    } finally {
        baseEvent.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) VersionedObjectList(org.apache.geode.internal.cache.tier.sockets.VersionedObjectList)

Example 48 with Released

use of org.apache.geode.internal.offheap.annotations.Released in project geode by apache.

the class SearchLoadAndWriteProcessor method doLocalWrite.

private boolean doLocalWrite(CacheWriter writer, CacheEvent pevent, int paction) throws CacheWriterException {
    // Return if the inhibit all notifications flag is set
    if (pevent instanceof EntryEventImpl) {
        if (((EntryEventImpl) pevent).inhibitAllNotifications()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Notification inhibited for key {}", pevent);
            }
            return false;
        }
    }
    @Released CacheEvent event = getEventForListener(pevent);
    int action = paction;
    if (event.getOperation().isCreate() && action == BEFOREUPDATE) {
        action = BEFORECREATE;
    }
    try {
        switch(action) {
            case BEFORECREATE:
                writer.beforeCreate((EntryEvent) event);
                break;
            case BEFOREDESTROY:
                writer.beforeDestroy((EntryEvent) event);
                break;
            case BEFOREUPDATE:
                writer.beforeUpdate((EntryEvent) event);
                break;
            case BEFOREREGIONDESTROY:
                writer.beforeRegionDestroy((RegionEvent) event);
                break;
            case BEFOREREGIONCLEAR:
                writer.beforeRegionClear((RegionEvent) event);
                break;
            default:
                break;
        }
    } finally {
        if (event != pevent) {
            if (event instanceof EntryEventImpl) {
                ((Releasable) event).release();
            }
        }
    }
    this.localWrite = true;
    return true;
}
Also used : Released(org.apache.geode.internal.offheap.annotations.Released) CacheEvent(org.apache.geode.cache.CacheEvent) Releasable(org.apache.geode.internal.offheap.Releasable)

Example 49 with Released

use of org.apache.geode.internal.offheap.annotations.Released in project geode by apache.

the class AbstractRegionMap method initialImagePut.

public boolean initialImagePut(final Object key, final long lastModified, Object newValue, final boolean wasRecovered, boolean deferLRUCallback, VersionTag entryVersion, InternalDistributedMember sender, boolean isSynchronizing) {
    boolean result = false;
    boolean done = false;
    boolean cleared = false;
    final LocalRegion owner = _getOwner();
    if (newValue == Token.TOMBSTONE && !owner.getConcurrencyChecksEnabled()) {
        return false;
    }
    if (owner instanceof HARegion && newValue instanceof CachedDeserializable) {
        Object actualVal = ((CachedDeserializable) newValue).getDeserializedValue(null, null);
        if (actualVal instanceof HAEventWrapper) {
            HAEventWrapper haEventWrapper = (HAEventWrapper) actualVal;
            // Key was removed at sender side so not putting it into the HARegion
            if (haEventWrapper.getClientUpdateMessage() == null) {
                return false;
            }
            // Getting the instance from singleton CCN..This assumes only one bridge
            // server in the VM
            HAContainerWrapper haContainer = (HAContainerWrapper) CacheClientNotifier.getInstance().getHaContainer();
            if (haContainer == null) {
                return false;
            }
            HAEventWrapper original = null;
            // synchronized (haContainer) {
            do {
                ClientUpdateMessageImpl oldMsg = (ClientUpdateMessageImpl) haContainer.putIfAbsent(haEventWrapper, haEventWrapper.getClientUpdateMessage());
                if (oldMsg != null) {
                    original = (HAEventWrapper) haContainer.getKey(haEventWrapper);
                    if (original == null) {
                        continue;
                    }
                    synchronized (original) {
                        if ((HAEventWrapper) haContainer.getKey(original) != null) {
                            original.incAndGetReferenceCount();
                            HARegionQueue.addClientCQsAndInterestList(oldMsg, haEventWrapper, haContainer, owner.getName());
                            haEventWrapper.setClientUpdateMessage(null);
                            newValue = CachedDeserializableFactory.create(original, ((CachedDeserializable) newValue).getSizeInBytes());
                        } else {
                            original = null;
                        }
                    }
                } else {
                    // putIfAbsent successful
                    synchronized (haEventWrapper) {
                        haEventWrapper.incAndGetReferenceCount();
                        haEventWrapper.setHAContainer(haContainer);
                        haEventWrapper.setClientUpdateMessage(null);
                        haEventWrapper.setIsRefFromHAContainer(true);
                    }
                    break;
                }
            // try until we either get a reference to HAEventWrapper from
            // HAContainer or successfully put one into it.
            } while (original == null);
        /*
         * entry = (Map.Entry)haContainer.getEntry(haEventWrapper); if (entry != null) { original =
         * (HAEventWrapper)entry.getKey(); original.incAndGetReferenceCount(); } else {
         * haEventWrapper.incAndGetReferenceCount(); haEventWrapper.setHAContainer(haContainer);
         * haContainer.put(haEventWrapper, haEventWrapper .getClientUpdateMessage());
         * haEventWrapper.setClientUpdateMessage(null);
         * haEventWrapper.setIsRefFromHAContainer(true); } } if (entry != null) {
         * HARegionQueue.addClientCQsAndInterestList(entry, haEventWrapper, haContainer,
         * owner.getName()); haEventWrapper.setClientUpdateMessage(null); newValue =
         * CachedDeserializableFactory.create(original,
         * ((CachedDeserializable)newValue).getSizeInBytes()); }
         */
        }
    }
    try {
        RegionEntry newRe = getEntryFactory().createEntry(owner, key, Token.REMOVED_PHASE1);
        EntryEventImpl event = null;
        @Retained @Released Object oldValue = null;
        try {
            RegionEntry oldRe = null;
            synchronized (newRe) {
                try {
                    oldRe = putEntryIfAbsent(key, newRe);
                    while (!done && oldRe != null) {
                        synchronized (oldRe) {
                            if (oldRe.isRemovedPhase2()) {
                                owner.getCachePerfStats().incRetries();
                                _getMap().remove(key, oldRe);
                                oldRe = putEntryIfAbsent(key, newRe);
                            } else {
                                boolean acceptedVersionTag = false;
                                if (entryVersion != null && owner.concurrencyChecksEnabled) {
                                    Assert.assertTrue(entryVersion.getMemberID() != null, "GII entry versions must have identifiers");
                                    try {
                                        boolean isTombstone = (newValue == Token.TOMBSTONE);
                                        // don't reschedule the tombstone if it hasn't changed
                                        boolean isSameTombstone = oldRe.isTombstone() && isTombstone && oldRe.getVersionStamp().asVersionTag().equals(entryVersion);
                                        if (isSameTombstone) {
                                            return true;
                                        }
                                        processVersionTagForGII(oldRe, owner, entryVersion, isTombstone, sender, !wasRecovered || isSynchronizing);
                                        acceptedVersionTag = true;
                                    } catch (ConcurrentCacheModificationException e) {
                                        return false;
                                    }
                                }
                                final boolean oldIsTombstone = oldRe.isTombstone();
                                final int oldSize = owner.calculateRegionEntryValueSize(oldRe);
                                try {
                                    result = oldRe.initialImagePut(owner, lastModified, newValue, wasRecovered, acceptedVersionTag);
                                    if (result) {
                                        if (oldIsTombstone) {
                                            owner.unscheduleTombstone(oldRe);
                                            if (newValue != Token.TOMBSTONE) {
                                                lruEntryCreate(oldRe);
                                            } else {
                                                lruEntryUpdate(oldRe);
                                            }
                                        }
                                        if (newValue == Token.TOMBSTONE) {
                                            owner.updateSizeOnRemove(key, oldSize);
                                            if (owner.getServerProxy() == null && owner.getVersionVector().isTombstoneTooOld(entryVersion.getMemberID(), entryVersion.getRegionVersion())) {
                                                // the received tombstone has already been reaped, so don't retain it
                                                removeTombstone(oldRe, entryVersion, false, false);
                                                return false;
                                            } else {
                                                owner.scheduleTombstone(oldRe, entryVersion);
                                                lruEntryDestroy(oldRe);
                                            }
                                        } else {
                                            int newSize = owner.calculateRegionEntryValueSize(oldRe);
                                            if (!oldIsTombstone) {
                                                owner.updateSizeOnPut(key, oldSize, newSize);
                                            } else {
                                                owner.updateSizeOnCreate(key, newSize);
                                            }
                                            EntryLogger.logInitialImagePut(_getOwnerObject(), key, newValue);
                                        }
                                    }
                                    if (owner.getIndexManager() != null) {
                                        // as the update could not locate the old key
                                        if (!oldRe.isRemoved()) {
                                            owner.getIndexManager().updateIndexes(oldRe, IndexManager.REMOVE_ENTRY, IndexProtocol.BEFORE_UPDATE_OP);
                                        }
                                        owner.getIndexManager().updateIndexes(oldRe, oldRe.isRemoved() ? IndexManager.ADD_ENTRY : IndexManager.UPDATE_ENTRY, oldRe.isRemoved() ? IndexProtocol.OTHER_OP : IndexProtocol.AFTER_UPDATE_OP);
                                    }
                                    done = true;
                                } finally {
                                    if (event != null) {
                                        event.release();
                                        event = null;
                                    }
                                }
                            }
                        }
                    }
                    if (!done) {
                        boolean versionTagAccepted = false;
                        if (entryVersion != null && owner.concurrencyChecksEnabled) {
                            Assert.assertTrue(entryVersion.getMemberID() != null, "GII entry versions must have identifiers");
                            try {
                                boolean isTombstone = (newValue == Token.TOMBSTONE);
                                processVersionTagForGII(newRe, owner, entryVersion, isTombstone, sender, !wasRecovered || isSynchronizing);
                                versionTagAccepted = true;
                            } catch (ConcurrentCacheModificationException e) {
                                return false;
                            }
                        }
                        result = newRe.initialImageInit(owner, lastModified, newValue, true, wasRecovered, versionTagAccepted);
                        try {
                            if (result) {
                                if (newValue == Token.TOMBSTONE) {
                                    owner.scheduleTombstone(newRe, entryVersion);
                                } else {
                                    owner.updateSizeOnCreate(key, owner.calculateRegionEntryValueSize(newRe));
                                    EntryLogger.logInitialImagePut(_getOwnerObject(), key, newValue);
                                    lruEntryCreate(newRe);
                                }
                                incEntryCount(1);
                            }
                            // Update local indexes
                            if (owner.getIndexManager() != null) {
                                // the update could not locate the old key
                                if (oldRe != null && !oldRe.isRemoved()) {
                                    owner.getIndexManager().updateIndexes(oldRe, IndexManager.REMOVE_ENTRY, IndexProtocol.BEFORE_UPDATE_OP);
                                }
                                owner.getIndexManager().updateIndexes(newRe, newRe.isRemoved() ? IndexManager.REMOVE_ENTRY : IndexManager.UPDATE_ENTRY, newRe.isRemoved() ? IndexProtocol.OTHER_OP : IndexProtocol.AFTER_UPDATE_OP);
                            }
                            done = true;
                        } finally {
                            if (event != null) {
                                event.release();
                                event = null;
                            }
                        }
                    }
                } finally {
                    if (done && result) {
                        initialImagePutEntry(newRe);
                    }
                    if (!done) {
                        removeEntry(key, newRe, false);
                        if (owner.getIndexManager() != null) {
                            owner.getIndexManager().updateIndexes(newRe, IndexManager.REMOVE_ENTRY, IndexProtocol.OTHER_OP);
                        }
                    }
                }
            }
        // synchronized
        } finally {
            if (event != null)
                event.release();
            OffHeapHelper.release(oldValue);
        }
    } catch (RegionClearedException rce) {
        // Asif: do not issue any sort of callbacks
        done = false;
        cleared = true;
    } catch (QueryException qe) {
        done = false;
        cleared = true;
    } finally {
        if (done && !deferLRUCallback) {
            lruUpdateCallback();
        } else if (!cleared) {
            resetThreadLocals();
        }
    }
    return result;
}
Also used : ClientUpdateMessageImpl(org.apache.geode.internal.cache.tier.sockets.ClientUpdateMessageImpl) HAContainerWrapper(org.apache.geode.internal.cache.ha.HAContainerWrapper) Released(org.apache.geode.internal.offheap.annotations.Released) ConcurrentCacheModificationException(org.apache.geode.internal.cache.versions.ConcurrentCacheModificationException) QueryException(org.apache.geode.cache.query.QueryException) Retained(org.apache.geode.internal.offheap.annotations.Retained) StoredObject(org.apache.geode.internal.offheap.StoredObject) HAEventWrapper(org.apache.geode.internal.cache.tier.sockets.HAEventWrapper)

Example 50 with Released

use of org.apache.geode.internal.offheap.annotations.Released in project geode by apache.

the class AbstractRegionMap method setOldValueInEvent.

// PRECONDITION: caller must be synced on re
private void setOldValueInEvent(EntryEventImpl event, RegionEntry re, boolean cacheWrite, boolean requireOldValue) {
    boolean needToSetOldValue = cacheWrite || requireOldValue || event.getOperation().guaranteesOldValue();
    if (needToSetOldValue) {
        if (event.getOperation().guaranteesOldValue()) {
            // In these cases we want to even get the old value from disk if it is not in memory
            ReferenceCountHelper.skipRefCountTracking();
            @Released Object oldValueInVMOrDisk = re.getValueOffHeapOrDiskWithoutFaultIn(event.getLocalRegion());
            ReferenceCountHelper.unskipRefCountTracking();
            try {
                event.setOldValue(oldValueInVMOrDisk, needToSetOldValue);
            } finally {
                OffHeapHelper.releaseWithNoTracking(oldValueInVMOrDisk);
            }
        } else {
            // In these cases only need the old value if it is in memory
            ReferenceCountHelper.skipRefCountTracking();
            @Retained @Released Object // OFFHEAP: re
            oldValueInVM = re._getValueRetain(event.getLocalRegion(), true);
            // synced so can use
            // its ref.
            ReferenceCountHelper.unskipRefCountTracking();
            try {
                event.setOldValue(oldValueInVM, needToSetOldValue);
            } finally {
                OffHeapHelper.releaseWithNoTracking(oldValueInVM);
            }
        }
    } else {
        // if the old value is in memory then if it is a GatewaySenderEventImpl then
        // we want to set the old value.
        @Unretained Object // OFFHEAP _getValue is ok since re is synced and we only use it
        ov = re._getValue();
        // we don't need to worry about ov being compressed.
        if (ov instanceof GatewaySenderEventImpl) {
            event.setOldValue(ov, true);
        }
    }
}
Also used : Released(org.apache.geode.internal.offheap.annotations.Released) Retained(org.apache.geode.internal.offheap.annotations.Retained) GatewaySenderEventImpl(org.apache.geode.internal.cache.wan.GatewaySenderEventImpl) StoredObject(org.apache.geode.internal.offheap.StoredObject) Unretained(org.apache.geode.internal.offheap.annotations.Unretained)

Aggregations

Released (org.apache.geode.internal.offheap.annotations.Released)57 StoredObject (org.apache.geode.internal.offheap.StoredObject)29 CacheWriterException (org.apache.geode.cache.CacheWriterException)13 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)13 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)12 ConcurrentCacheModificationException (org.apache.geode.internal.cache.versions.ConcurrentCacheModificationException)9 GatewaySenderEventCallbackArgument (org.apache.geode.internal.cache.wan.GatewaySenderEventCallbackArgument)9 Retained (org.apache.geode.internal.offheap.annotations.Retained)9 EntryEventImpl (org.apache.geode.internal.cache.EntryEventImpl)8 VersionedObjectList (org.apache.geode.internal.cache.tier.sockets.VersionedObjectList)8 DiskAccessException (org.apache.geode.cache.DiskAccessException)6 Operation (org.apache.geode.cache.Operation)6 ReplyException (org.apache.geode.distributed.internal.ReplyException)6 RegionDestroyedException (org.apache.geode.cache.RegionDestroyedException)5 IndexManager (org.apache.geode.cache.query.internal.index.IndexManager)5 PartitionedRegionDataStore (org.apache.geode.internal.cache.PartitionedRegionDataStore)5 TimeoutException (org.apache.geode.cache.TimeoutException)4 EventID (org.apache.geode.internal.cache.EventID)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3