Search in sources :

Example 16 with ThreadIdentifier

use of org.apache.geode.internal.cache.ha.ThreadIdentifier in project geode by apache.

the class RemoveAllPRMessage method doLocalRemoveAll.

/**
   * This method is called by both operateOnPartitionedRegion() when processing a remote msg or by
   * sendMsgByBucket() when processing a msg targeted to local Jvm. PartitionedRegion 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
   * @param cacheWrite if true invoke cacheWriter before desrtoy
   * @return If succeeds, return true, otherwise, throw exception
   */
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "IMSE_DONT_CATCH_IMSE")
public boolean doLocalRemoveAll(PartitionedRegion r, InternalDistributedMember eventSender, boolean cacheWrite) throws EntryExistsException, ForceReattemptException, DataLocationException {
    boolean didRemove = false;
    long clientReadTimeOut = PoolFactory.DEFAULT_READ_TIMEOUT;
    if (r.hasServerProxy()) {
        clientReadTimeOut = r.getServerProxy().getPool().getReadTimeout();
        if (logger.isDebugEnabled()) {
            logger.debug("RemoveAllPRMessage: doLocalRemoveAll: clientReadTimeOut is {}", clientReadTimeOut);
        }
    }
    DistributedRemoveAllOperation op = null;
    @Released EntryEventImpl baseEvent = null;
    BucketRegion bucketRegion = null;
    PartitionedRegionDataStore ds = r.getDataStore();
    InternalDistributedMember myId = r.getDistributionManager().getDistributionManagerId();
    try {
        if (!notificationOnly) {
            // bucketRegion is not null only when !notificationOnly
            bucketRegion = ds.getInitializedBucketForId(null, bucketId);
            this.versions = new VersionedObjectList(this.removeAllPRDataSize, true, bucketRegion.getAttributes().getConcurrencyChecksEnabled());
            // create a base event and a DPAO for RemoveAllMessage distributed btw redundant buckets
            baseEvent = EntryEventImpl.create(bucketRegion, Operation.REMOVEALL_DESTROY, null, null, this.callbackArg, true, eventSender, !skipCallbacks, true);
            // set baseEventId to the first entry's event id. We need the thread id for DACE
            baseEvent.setEventId(removeAllPRData[0].getEventID());
            if (this.bridgeContext != null) {
                baseEvent.setContext(this.bridgeContext);
            }
            baseEvent.setPossibleDuplicate(this.posDup);
            if (logger.isDebugEnabled()) {
                logger.debug("RemoveAllPRMessage.doLocalRemoveAll: eventSender is {}, baseEvent is {}, msg is {}", eventSender, baseEvent, this);
            }
            op = new DistributedRemoveAllOperation(baseEvent, removeAllPRDataSize, false);
        }
        // Fix the updateMsg misorder issue
        // Lock the keys when doing postRemoveAll
        Object[] keys = new Object[removeAllPRDataSize];
        for (int i = 0; i < removeAllPRDataSize; ++i) {
            keys[i] = removeAllPRData[i].getKey();
        }
        if (!notificationOnly) {
            try {
                if (removeAllPRData.length > 0) {
                    if (this.posDup && bucketRegion.getConcurrencyChecksEnabled()) {
                        if (logger.isDebugEnabled()) {
                            logger.debug("attempting to locate version tags for retried event");
                        }
                        // of the previous attempt
                        for (int i = 0; i < removeAllPRDataSize; i++) {
                            if (removeAllPRData[i].versionTag == null) {
                                removeAllPRData[i].versionTag = bucketRegion.findVersionTagForClientBulkOp(removeAllPRData[i].getEventID());
                                if (removeAllPRData[i].versionTag != null) {
                                    removeAllPRData[i].versionTag.replaceNullIDs(bucketRegion.getVersionMember());
                                }
                            }
                        }
                    }
                    EventID eventID = removeAllPRData[0].getEventID();
                    ThreadIdentifier membershipID = new ThreadIdentifier(eventID.getMembershipID(), eventID.getThreadID());
                    bucketRegion.recordBulkOpStart(membershipID, eventID);
                }
                bucketRegion.waitUntilLocked(keys);
                boolean lockedForPrimary = false;
                final ArrayList<Object> succeeded = new ArrayList<Object>();
                PutAllPartialResult partialKeys = new PutAllPartialResult(removeAllPRDataSize);
                Object key = keys[0];
                try {
                    bucketRegion.doLockForPrimary(false);
                    lockedForPrimary = true;
                    /*
             * The real work to be synchronized, it will take long time. We don't worry about
             * another thread to send any msg which has the same key in this request, because these
             * request will be blocked by foundKey
             */
                    for (int i = 0; i < removeAllPRDataSize; i++) {
                        @Released EntryEventImpl ev = getEventFromEntry(r, myId, eventSender, i, removeAllPRData, notificationOnly, bridgeContext, posDup, skipCallbacks);
                        try {
                            key = ev.getKey();
                            ev.setRemoveAllOperation(op);
                            // then in basicPutPart3(), the ev is added into op
                            try {
                                r.getDataView().destroyOnRemote(ev, cacheWrite, null);
                                didRemove = true;
                                if (logger.isDebugEnabled()) {
                                    logger.debug("RemoveAllPRMessage.doLocalRemoveAll:removeLocally success for " + ev);
                                }
                            } catch (EntryNotFoundException ignore) {
                                didRemove = true;
                                if (ev.getVersionTag() == null) {
                                    if (logger.isDebugEnabled()) {
                                        logger.debug("doLocalRemoveAll:RemoveAll encoutered EntryNotFoundException: event={}", ev);
                                    }
                                }
                            } catch (ConcurrentCacheModificationException e) {
                                didRemove = true;
                                if (logger.isDebugEnabled()) {
                                    logger.debug("RemoveAllPRMessage.doLocalRemoveAll:removeLocally encountered concurrent cache modification for " + ev);
                                }
                            }
                            removeAllPRData[i].setTailKey(ev.getTailKey());
                            if (!didRemove) {
                                // make sure the region hasn't gone away
                                r.checkReadiness();
                                ForceReattemptException fre = new ForceReattemptException("unable to perform remove in RemoveAllPR, but operation should not fail");
                                fre.setHash(ev.getKey().hashCode());
                                throw fre;
                            } else {
                                succeeded.add(removeAllPRData[i].getKey());
                                this.versions.addKeyAndVersion(removeAllPRData[i].getKey(), ev.getVersionTag());
                            }
                        } finally {
                            ev.release();
                        }
                    }
                // for
                } catch (IllegalMonitorStateException ex) {
                    ForceReattemptException fre = new ForceReattemptException("unable to get lock for primary, retrying... ");
                    throw fre;
                } catch (CacheWriterException cwe) {
                    // encounter cacheWriter exception
                    partialKeys.saveFailedKey(key, cwe);
                } finally {
                    try {
                        // Only RemoveAllPRMessage knows if the thread id is fake. Event has no idea.
                        // So we have to manually set useFakeEventId for this op
                        op.setUseFakeEventId(true);
                        r.checkReadiness();
                        bucketRegion.getDataView().postRemoveAll(op, this.versions, bucketRegion);
                    } finally {
                        if (lockedForPrimary) {
                            bucketRegion.doUnlockForPrimary();
                        }
                    }
                }
                if (partialKeys.hasFailure()) {
                    partialKeys.addKeysAndVersions(this.versions);
                    if (logger.isDebugEnabled()) {
                        logger.debug("RemoveAllPRMessage: partial keys applied, map to bucket {}'s keys:{}. Applied {}", bucketId, Arrays.toString(keys), succeeded);
                    }
                    throw new PutAllPartialResultException(partialKeys);
                }
            } catch (RegionDestroyedException e) {
                ds.checkRegionDestroyedOnBucket(bucketRegion, true, e);
            } finally {
                bucketRegion.removeAndNotifyKeys(keys);
            }
        } else {
            for (int i = 0; i < removeAllPRDataSize; i++) {
                EntryEventImpl ev = getEventFromEntry(r, myId, eventSender, i, removeAllPRData, notificationOnly, bridgeContext, posDup, skipCallbacks);
                try {
                    ev.setOriginRemote(true);
                    if (this.callbackArg != null) {
                        ev.setCallbackArgument(this.callbackArg);
                    }
                    r.invokeDestroyCallbacks(EnumListenerEvent.AFTER_DESTROY, ev, r.isInitialized(), true);
                } finally {
                    ev.release();
                }
            }
        }
    } finally {
        if (baseEvent != null)
            baseEvent.release();
        if (op != null)
            op.freeOffHeapResources();
    }
    return true;
}
Also used : Released(org.apache.geode.internal.offheap.annotations.Released) EntryEventImpl(org.apache.geode.internal.cache.EntryEventImpl) ThreadIdentifier(org.apache.geode.internal.cache.ha.ThreadIdentifier) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) ArrayList(java.util.ArrayList) VersionedObjectList(org.apache.geode.internal.cache.tier.sockets.VersionedObjectList) PartitionedRegionDataStore(org.apache.geode.internal.cache.PartitionedRegionDataStore) PutAllPartialResult(org.apache.geode.internal.cache.PutAllPartialResultException.PutAllPartialResult) ConcurrentCacheModificationException(org.apache.geode.internal.cache.versions.ConcurrentCacheModificationException) PutAllPartialResultException(org.apache.geode.internal.cache.PutAllPartialResultException) ForceReattemptException(org.apache.geode.internal.cache.ForceReattemptException) DistributedRemoveAllOperation(org.apache.geode.internal.cache.DistributedRemoveAllOperation) BucketRegion(org.apache.geode.internal.cache.BucketRegion) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) EventID(org.apache.geode.internal.cache.EventID) CacheWriterException(org.apache.geode.cache.CacheWriterException)

Example 17 with ThreadIdentifier

use of org.apache.geode.internal.cache.ha.ThreadIdentifier in project geode by apache.

the class EventStateHelper method fromData.

/**
   * Post 7.1, if changes are made to this method make sure that it is backwards compatible by
   * creating fromDataPreXX methods. Also make sure that the callers to this method are backwards
   * compatible by creating fromDataPreXX methods for them even if they are not changed. <br>
   * Callers for this method are: <br>
   * {@link CreateRegionReplyMessage#fromData(DataInput)} <br>
   * {@link RegionStateMessage#fromData(DataInput)} <br>
   */
public static Map fromData(DataInput dip, boolean isHARegion) throws IOException, ClassNotFoundException {
    InternalDistributedMember senderId = InternalDistributedMember.readEssentialData(dip);
    int numIds = dip.readInt();
    Map<Integer, byte[]> numberToMember = new HashMap();
    for (int i = 0; i < numIds; i++) {
        numberToMember.put(Integer.valueOf(i), DataSerializer.readByteArray(dip));
    }
    int size = dip.readInt();
    HashMap eventState = new HashMap(size);
    for (int i = 0; i < size; i++) {
        int idNumber = dip.readInt();
        int subMapSize = dip.readInt();
        for (int j = 0; j < subMapSize; j++) {
            long threadId = dip.readLong();
            ThreadIdentifier key = new ThreadIdentifier(numberToMember.get(idNumber), threadId);
            if (isHARegion) {
                DispatchedAndCurrentEvents value = new DispatchedAndCurrentEvents();
                InternalDataSerializer.invokeFromData(value, dip);
                eventState.put(key, value);
            } else {
                EventSeqnoHolder value = new EventSeqnoHolder();
                InternalDataSerializer.invokeFromData(value, dip);
                eventState.put(key, value);
                if (value.versionTag != null) {
                    value.versionTag.replaceNullIDs(senderId);
                }
            }
        }
    }
    return eventState;
}
Also used : InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) HashMap(java.util.HashMap) ThreadIdentifier(org.apache.geode.internal.cache.ha.ThreadIdentifier) EventSeqnoHolder(org.apache.geode.internal.cache.EventTracker.EventSeqnoHolder) DispatchedAndCurrentEvents(org.apache.geode.internal.cache.ha.HARegionQueue.DispatchedAndCurrentEvents)

Example 18 with ThreadIdentifier

use of org.apache.geode.internal.cache.ha.ThreadIdentifier in project geode by apache.

the class EventTrackerTest method retriedBulkOpDoesNotRemoveRecordedBulkOpVersionTags.

@Test
public void retriedBulkOpDoesNotRemoveRecordedBulkOpVersionTags() {
    byte[] memId = { 1, 2, 3 };
    long threadId = 1;
    long retrySeqId = 1;
    ThreadIdentifier tid = new ThreadIdentifier(memId, threadId);
    EventID retryEventID = new EventID(memId, threadId, retrySeqId);
    boolean skipCallbacks = true;
    int size = 5;
    recordPutAllEvents(memId, threadId, skipCallbacks, size);
    ConcurrentMap<ThreadIdentifier, BulkOpHolder> map = eventTracker.getRecordedBulkOpVersionTags();
    BulkOpHolder holder = map.get(tid);
    int beforeSize = holder.entryVersionTags.size();
    eventTracker.recordBulkOpStart(tid, retryEventID);
    map = eventTracker.getRecordedBulkOpVersionTags();
    holder = map.get(tid);
    // Retried bulk op should not remove exiting BulkOpVersionTags
    assertTrue(holder.entryVersionTags.size() == beforeSize);
}
Also used : BulkOpHolder(org.apache.geode.internal.cache.EventTracker.BulkOpHolder) ThreadIdentifier(org.apache.geode.internal.cache.ha.ThreadIdentifier) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Example 19 with ThreadIdentifier

use of org.apache.geode.internal.cache.ha.ThreadIdentifier in project geode by apache.

the class ReliableMessagingDUnitTest method setTidAndSeq.

public static void setTidAndSeq(byte[] membershipId, Long threadId, Long sequenceId) {
    tid = new ThreadIdentifier(membershipId, threadId.longValue());
    seqid = sequenceId;
}
Also used : ThreadIdentifier(org.apache.geode.internal.cache.ha.ThreadIdentifier)

Example 20 with ThreadIdentifier

use of org.apache.geode.internal.cache.ha.ThreadIdentifier in project geode by apache.

the class ReliableMessagingDUnitTest method setCreationTimeTidAndSeq.

public static void setCreationTimeTidAndSeq() {
    final Map map = pool.getThreadIdToSequenceIdMap();
    WaitCriterion ev = new WaitCriterion() {

        public boolean done() {
            synchronized (map) {
                return map.entrySet().size() > 0;
            }
        }

        public String description() {
            return null;
        }
    };
    Wait.waitForCriterion(ev, 10 * 1000, 200, true);
    Map.Entry entry;
    synchronized (map) {
        Iterator iter = map.entrySet().iterator();
        entry = (Map.Entry) iter.next();
    }
    SequenceIdAndExpirationObject seo = (SequenceIdAndExpirationObject) entry.getValue();
    assertFalse(seo.getAckSend());
    creationTime = seo.getCreationTime();
    LogWriterUtils.getLogWriter().info("seo is " + seo.toString());
    assertTrue("Creation time not set", creationTime != 0);
    Object[] args = new Object[] { ((ThreadIdentifier) entry.getKey()).getMembershipID(), new Long(((ThreadIdentifier) entry.getKey()).getThreadID()), new Long(seo.getSequenceId()) };
    server1.invoke(ReliableMessagingDUnitTest.class, "setTidAndSeq", args);
    server2.invoke(ReliableMessagingDUnitTest.class, "setTidAndSeq", args);
}
Also used : SequenceIdAndExpirationObject(org.apache.geode.cache.client.internal.QueueStateImpl.SequenceIdAndExpirationObject) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) ThreadIdentifier(org.apache.geode.internal.cache.ha.ThreadIdentifier) Iterator(java.util.Iterator) SequenceIdAndExpirationObject(org.apache.geode.cache.client.internal.QueueStateImpl.SequenceIdAndExpirationObject) Map(java.util.Map)

Aggregations

ThreadIdentifier (org.apache.geode.internal.cache.ha.ThreadIdentifier)20 HashMap (java.util.HashMap)4 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)4 EventID (org.apache.geode.internal.cache.EventID)4 Test (org.junit.Test)4 Map (java.util.Map)3 BulkOpHolder (org.apache.geode.internal.cache.EventTracker.BulkOpHolder)3 CacheWriterException (org.apache.geode.cache.CacheWriterException)2 RegionDestroyedException (org.apache.geode.cache.RegionDestroyedException)2 PoolFactory (org.apache.geode.cache.client.PoolFactory)2 CacheServer (org.apache.geode.cache.server.CacheServer)2 BucketRegion (org.apache.geode.internal.cache.BucketRegion)2 EntryEventImpl (org.apache.geode.internal.cache.EntryEventImpl)2 EventSeqnoHolder (org.apache.geode.internal.cache.EventTracker.EventSeqnoHolder)2 ForceReattemptException (org.apache.geode.internal.cache.ForceReattemptException)2 PartitionedRegionDataStore (org.apache.geode.internal.cache.PartitionedRegionDataStore)2 PutAllPartialResultException (org.apache.geode.internal.cache.PutAllPartialResultException)2 PutAllPartialResult (org.apache.geode.internal.cache.PutAllPartialResultException.PutAllPartialResult)2 DispatchedAndCurrentEvents (org.apache.geode.internal.cache.ha.HARegionQueue.DispatchedAndCurrentEvents)2 VersionedObjectList (org.apache.geode.internal.cache.tier.sockets.VersionedObjectList)2