Search in sources :

Example 31 with TransactionException

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

the class PartitionedRegion method sendMsgByBucket.

/*
   * If failed after retries, it will throw PartitionedRegionStorageException, no need for return
   * value
   */
private VersionedObjectList sendMsgByBucket(final Integer bucketId, RemoveAllPRMessage prMsg) {
    // retry the put remotely until it finds the right node managing the bucket
    @Released EntryEventImpl event = prMsg.getFirstEvent(this);
    try {
        RetryTimeKeeper retryTime = null;
        InternalDistributedMember currentTarget = getNodeForBucketWrite(bucketId, null);
        if (logger.isDebugEnabled()) {
            logger.debug("PR.sendMsgByBucket:bucket {}'s currentTarget is {}", bucketId, currentTarget);
        }
        long timeOut = 0;
        int count = 0;
        for (; ; ) {
            switch(count) {
                case 0:
                    // First time. Assume success, keep going.
                    break;
                case 1:
                    this.cache.getCancelCriterion().checkCancelInProgress(null);
                    // Second time (first failure). Calculate timeout and keep going.
                    timeOut = System.currentTimeMillis() + this.retryTimeout;
                    break;
                default:
                    this.cache.getCancelCriterion().checkCancelInProgress(null);
                    // test for timeout
                    long timeLeft = timeOut - System.currentTimeMillis();
                    if (timeLeft < 0) {
                        PRHARedundancyProvider.timedOut(this, null, null, "update an entry", this.retryTimeout);
                    // NOTREACHED
                    }
                    // Didn't time out. Sleep a bit and then continue
                    boolean interrupted = Thread.interrupted();
                    try {
                        Thread.sleep(PartitionedRegionHelper.DEFAULT_WAIT_PER_RETRY_ITERATION);
                    } catch (InterruptedException ignore) {
                        interrupted = true;
                    } finally {
                        if (interrupted) {
                            Thread.currentThread().interrupt();
                        }
                    }
                    break;
            }
            // switch
            count++;
            if (currentTarget == null) {
                // pick target
                checkReadiness();
                if (retryTime == null) {
                    retryTime = new RetryTimeKeeper(this.retryTimeout);
                }
                currentTarget = waitForNodeOrCreateBucket(retryTime, event, bucketId);
                if (logger.isDebugEnabled()) {
                    logger.debug("PR.sendMsgByBucket: event size is {}, new currentTarget is {}", getEntrySize(event), currentTarget);
                }
                // It's possible this is a GemFire thread e.g. ServerConnection
                // which got to this point because of a distributed system shutdown or
                // region closure which uses interrupt to break any sleep() or wait() calls
                // e.g. waitForPrimary or waitForBucketRecovery in which case throw exception
                checkShutdown();
                continue;
            }
            try {
                return tryToSendOneRemoveAllMessage(prMsg, currentTarget);
            } catch (ForceReattemptException prce) {
                checkReadiness();
                InternalDistributedMember lastTarget = currentTarget;
                if (retryTime == null) {
                    retryTime = new RetryTimeKeeper(this.retryTimeout);
                }
                currentTarget = getNodeForBucketWrite(bucketId, retryTime);
                if (logger.isTraceEnabled()) {
                    logger.trace("PR.sendMsgByBucket: Old target was {}, Retrying {}", lastTarget, currentTarget);
                }
                if (lastTarget.equals(currentTarget)) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("PR.sendMsgByBucket: Retrying at the same node:{} due to {}", currentTarget, prce.getMessage());
                    }
                    if (retryTime.overMaximum()) {
                        PRHARedundancyProvider.timedOut(this, null, null, "update an entry", this.retryTimeout);
                    // NOTREACHED
                    }
                    retryTime.waitToRetryNode();
                }
                event.setPossibleDuplicate(true);
                if (prMsg != null) {
                    prMsg.setPossibleDuplicate(true);
                }
            } catch (PrimaryBucketException notPrimary) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Bucket {} on Node {} not primary", notPrimary.getLocalizedMessage(), currentTarget);
                }
                getRegionAdvisor().notPrimary(bucketId, currentTarget);
                if (retryTime == null) {
                    retryTime = new RetryTimeKeeper(this.retryTimeout);
                }
                currentTarget = getNodeForBucketWrite(bucketId, retryTime);
            } catch (DataLocationException dle) {
                if (logger.isDebugEnabled()) {
                    logger.debug("DataLocationException processing putAll", dle);
                }
                throw new TransactionException(dle);
            }
            // It's possible this is a GemFire thread e.g. ServerConnection
            // which got to this point because of a distributed system shutdown or
            // region closure which uses interrupt to break any sleep() or wait()
            // calls
            // e.g. waitForPrimary or waitForBucketRecovery in which case throw
            // exception
            checkShutdown();
            // If we get here, the attempt failed...
            if (count == 1) {
                this.prStats.incRemoveAllMsgsRetried();
            }
            this.prStats.incRemoveAllRetries();
        }
    // for
    // NOTREACHED
    } finally {
        event.release();
    }
}
Also used : Released(org.apache.geode.internal.offheap.annotations.Released) TransactionException(org.apache.geode.cache.TransactionException) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember)

Aggregations

TransactionException (org.apache.geode.cache.TransactionException)31 TransactionDataRebalancedException (org.apache.geode.cache.TransactionDataRebalancedException)12 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)11 TransactionDataNotColocatedException (org.apache.geode.cache.TransactionDataNotColocatedException)9 ForceReattemptException (org.apache.geode.internal.cache.ForceReattemptException)9 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)9 PrimaryBucketException (org.apache.geode.internal.cache.PrimaryBucketException)9 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)7 RegionDestroyedException (org.apache.geode.cache.RegionDestroyedException)7 LowMemoryException (org.apache.geode.cache.LowMemoryException)6 DistributedMember (org.apache.geode.distributed.DistributedMember)6 InternalCache (org.apache.geode.internal.cache.InternalCache)6 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)5 CacheWriterException (org.apache.geode.cache.CacheWriterException)5 UnsupportedOperationInTransactionException (org.apache.geode.cache.UnsupportedOperationInTransactionException)5 IOException (java.io.IOException)4 RollbackException (javax.transaction.RollbackException)4 CancelException (org.apache.geode.CancelException)4 CacheClosedException (org.apache.geode.cache.CacheClosedException)4 CommitConflictException (org.apache.geode.cache.CommitConflictException)4