Search in sources :

Example 1 with TimeoutException

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

the class DistributedRegion method getDistributedLockIfGlobal.

/**
   * If this region's scope is GLOBAL, get a distributed lock on the given key, and return the Lock.
   * The sender is responsible for unlocking.
   * 
   * @return the acquired Lock if the region is GLOBAL, otherwise null.
   * 
   * @throws NullPointerException if key is null
   */
private Lock getDistributedLockIfGlobal(Object key) throws TimeoutException {
    if (getScope().isGlobal()) {
        if (isLockingSuspendedByCurrentThread())
            return null;
        long start = System.currentTimeMillis();
        long timeLeft = getCache().getLockTimeout();
        long lockTimeout = timeLeft;
        StringId msg = null;
        Object[] msgArgs = null;
        while (timeLeft > 0 || lockTimeout == -1) {
            this.cache.getCancelCriterion().checkCancelInProgress(null);
            boolean interrupted = Thread.interrupted();
            try {
                Lock dlock = getDistributedLock(key);
                if (!dlock.tryLock(timeLeft, TimeUnit.SECONDS)) {
                    msg = LocalizedStrings.DistributedRegion_ATTEMPT_TO_ACQUIRE_DISTRIBUTED_LOCK_FOR_0_FAILED_AFTER_WAITING_1_SECONDS;
                    msgArgs = new Object[] { key, (System.currentTimeMillis() - start) / 1000L };
                    break;
                }
                return dlock;
            } catch (InterruptedException ex) {
                interrupted = true;
                this.cache.getCancelCriterion().checkCancelInProgress(ex);
                // TODO: Why is it OK to keep going?
                if (lockTimeout > -1) {
                    timeLeft = getCache().getLockTimeout() - (System.currentTimeMillis() - start) / 1000L;
                }
            } finally {
                if (interrupted) {
                    Thread.currentThread().interrupt();
                }
            }
        }
        // while
        if (msg == null) {
            msg = LocalizedStrings.DistributedRegion_TIMED_OUT_AFTER_WAITING_0_SECONDS_FOR_THE_DISTRIBUTED_LOCK_FOR_1;
            msgArgs = new Object[] { getCache().getLockTimeout(), key };
        }
        throw new TimeoutException(msg.toLocalizedString(msgArgs));
    } else {
        return null;
    }
}
Also used : StringId(org.apache.geode.i18n.StringId) Lock(java.util.concurrent.locks.Lock) TimeoutException(org.apache.geode.cache.TimeoutException)

Example 2 with TimeoutException

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

the class PartitionedRegion method getNodeForBucketWrite.

/**
   * Fetch the primary Node returning if the redundancy for the Node is satisfied
   * 
   * @param bucketId the identity of the bucket
   * @param snoozer a RetryTimeKeeper to track how long we may wait until the bucket is ready
   * @return the primary member's id or null if there is no storage
   */
public InternalDistributedMember getNodeForBucketWrite(int bucketId, final RetryTimeKeeper snoozer) {
    final boolean isDebugEnabled = logger.isDebugEnabled();
    RetryTimeKeeper localSnoozer = snoozer;
    // prevent writing to a bucket whose redundancy is sub par
    while (minimumWriteRedundancy > 0 && getRegionAdvisor().getBucketRedundancy(bucketId) < this.minimumWriteRedundancy) {
        this.cache.getCancelCriterion().checkCancelInProgress(null);
        // condition
        if (!getRegionAdvisor().isStorageAssignedForBucket(bucketId, this.minimumWriteRedundancy, false)) {
            if (isDebugEnabled) {
                logger.debug("No storage assigned for bucket ({}{}{}) writer", getPRId(), BUCKET_ID_SEPARATOR, bucketId);
            }
            // No bucket for this key
            return null;
        }
        if (localSnoozer == null) {
            localSnoozer = new RetryTimeKeeper(this.retryTimeout);
        }
        if (!localSnoozer.overMaximum()) {
            localSnoozer.waitForBucketsRecovery();
        } else {
            int red = getRegionAdvisor().getBucketRedundancy(bucketId);
            final TimeoutException noTime = new TimeoutException(LocalizedStrings.PartitionedRegion_ATTEMPT_TO_ACQUIRE_PRIMARY_NODE_FOR_WRITE_ON_BUCKET_0_TIMED_OUT_IN_1_MS_CURRENT_REDUNDANCY_2_DOES_NOT_SATISFY_MINIMUM_3.toLocalizedString(new Object[] { bucketStringForLogs(bucketId), localSnoozer.getRetryTime(), red, this.minimumWriteRedundancy }));
            checkReadiness();
            throw noTime;
        }
    }
    // bucket lock for bucket creation.
    return waitForNoStorageOrPrimary(bucketId, "write");
}
Also used : TimeoutException(org.apache.geode.cache.TimeoutException)

Example 3 with TimeoutException

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

the class PartitionedRegion method invalidateInBucket.

/**
   * Invalidate the entry in the bucket identified by the key
   */
void invalidateInBucket(final EntryEventImpl event) throws EntryNotFoundException {
    final boolean isDebugEnabled = logger.isDebugEnabled();
    final Integer bucketId = event.getKeyInfo().getBucketId();
    assert bucketId != KeyInfo.UNKNOWN_BUCKET;
    final InternalDistributedMember targetNode = getOrCreateNodeForBucketWrite(bucketId, null);
    final int retryAttempts = calcRetry();
    int count = 0;
    RetryTimeKeeper retryTime = null;
    InternalDistributedMember retryNode = targetNode;
    while (count <= retryAttempts) {
        // 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
        checkShutdown();
        if (retryNode == null) {
            checkReadiness();
            if (retryTime == null) {
                retryTime = new RetryTimeKeeper(this.retryTimeout);
            }
            try {
                retryNode = getOrCreateNodeForBucketWrite(bucketId, retryTime);
            } catch (TimeoutException ignore) {
                if (getRegionAdvisor().isStorageAssignedForBucket(bucketId)) {
                    // bucket no longer exists
                    throw new EntryNotFoundException(LocalizedStrings.PartitionedRegion_ENTRY_NOT_FOUND_FOR_KEY_0.toLocalizedString(event.getKey()));
                }
                // fall out to failed exception
                break;
            }
            if (retryNode == null) {
                checkEntryNotFound(event.getKey());
            }
            continue;
        }
        final boolean isLocal = (this.localMaxMemory > 0) && retryNode.equals(getMyId());
        try {
            if (isLocal) {
                event.setInvokePRCallbacks(true);
                this.dataStore.invalidateLocally(bucketId, event);
            } else {
                invalidateRemotely(retryNode, bucketId, event);
            }
            return;
        } catch (ConcurrentCacheModificationException e) {
            if (isDebugEnabled) {
                logger.debug("invalidateInBucket: caught concurrent cache modification exception", e);
            }
            event.isConcurrencyConflict(true);
            if (isDebugEnabled) {
                logger.debug("ConcurrentCacheModificationException received for invalidateInBucket for bucketId: {}{}{} for event: {}  No reattampt is done, returning from here", getPRId(), BUCKET_ID_SEPARATOR, bucketId, event);
            }
            return;
        } catch (ForceReattemptException prce) {
            prce.checkKey(event.getKey());
            if (isDebugEnabled) {
                logger.debug("invalidateInBucket: retry attempt:{} of {}", count, retryAttempts, prce);
            }
            checkReadiness();
            InternalDistributedMember lastNode = retryNode;
            retryNode = getOrCreateNodeForBucketWrite(bucketId, retryTime);
            if (lastNode.equals(retryNode)) {
                if (retryTime == null) {
                    retryTime = new RetryTimeKeeper(this.retryTimeout);
                }
                if (retryTime.overMaximum()) {
                    break;
                }
                retryTime.waitToRetryNode();
            }
            event.setPossibleDuplicate(true);
        } catch (PrimaryBucketException notPrimary) {
            if (isDebugEnabled) {
                logger.debug("invalidateInBucket {} on Node {} not primary", notPrimary.getLocalizedMessage(), retryNode);
            }
            getRegionAdvisor().notPrimary(bucketId, retryNode);
            retryNode = getOrCreateNodeForBucketWrite(bucketId, retryTime);
        }
        count++;
        if (count == 1) {
            this.prStats.incInvalidateOpsRetried();
        }
        this.prStats.incInvalidateRetries();
        if (isDebugEnabled) {
            logger.debug("invalidateInBucket: Attempting to resend invalidate to node {} after {} failed attempts", retryNode, count);
        }
    }
    // while
    // No target was found
    PartitionedRegionDistributionException e = new PartitionedRegionDistributionException(LocalizedStrings.PartitionedRegion_NO_VM_AVAILABLE_FOR_INVALIDATE_IN_0_ATTEMPTS.toLocalizedString(// Fix for bug 36014
    count));
    if (!isDebugEnabled) {
        logger.warn(LocalizedMessage.create(LocalizedStrings.PartitionedRegion_NO_VM_AVAILABLE_FOR_INVALIDATE_IN_0_ATTEMPTS, count));
    } else {
        logger.warn(e.getMessage(), e);
    }
    throw e;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) PartitionedRegionDistributionException(org.apache.geode.cache.PartitionedRegionDistributionException) ConcurrentCacheModificationException(org.apache.geode.internal.cache.versions.ConcurrentCacheModificationException) TimeoutException(org.apache.geode.cache.TimeoutException)

Example 4 with TimeoutException

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

the class DumpB2NRegion method process.

@Override
public void process(final DistributionManager dm) {
    PartitionedRegion pr = null;
    // Get the region, or die trying...
    final long finish = System.currentTimeMillis() + 10 * 1000;
    try {
        for (; ; ) {
            dm.getCancelCriterion().checkCancelInProgress(null);
            // pr = null; (redundant assignment)
            pr = PartitionedRegion.getPRFromId(this.regionId);
            if (pr != null) {
                break;
            }
            if (System.currentTimeMillis() > finish) {
                ReplyException rex = new ReplyException(new TimeoutException("Waited too long for region to initialize"));
                sendReply(getSender(), this.processorId, dm, rex, null, 0);
                return;
            }
            // wait a little
            boolean interrupted = Thread.interrupted();
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                interrupted = true;
                pr.checkReadiness();
            } finally {
                if (interrupted)
                    Thread.currentThread().interrupt();
            }
        }
        // Now, wait for the PR to finish initializing
        pr.waitForData();
        // OK, now it's safe to process this.
        super.process(dm);
    } catch (CancelException e) {
        sendReply(this.sender, this.processorId, dm, new ReplyException(e), pr, 0);
    } catch (PRLocallyDestroyedException e) {
        sendReply(this.sender, this.processorId, dm, new ReplyException(e), pr, 0);
        return;
    } catch (RegionDestroyedException rde) {
        sendReply(this.sender, this.processorId, dm, new ReplyException(rde), pr, 0);
        return;
    }
}
Also used : PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) CancelException(org.apache.geode.CancelException) ReplyException(org.apache.geode.distributed.internal.ReplyException) TimeoutException(org.apache.geode.cache.TimeoutException)

Example 5 with TimeoutException

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

the class LocalRegion method localDestroy.

@Override
public void localDestroy(Object key, Object aCallbackArgument) throws EntryNotFoundException {
    validateKey(key);
    checkReadiness();
    checkForNoAccess();
    @Released EntryEventImpl event = EntryEventImpl.create(this, Operation.LOCAL_DESTROY, key, null, aCallbackArgument, false, getMyId());
    if (generateEventID()) {
        event.setNewEventId(this.cache.getDistributedSystem());
    }
    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);
    } finally {
        event.release();
    }
}
Also used : Released(org.apache.geode.internal.offheap.annotations.Released) InternalGemFireError(org.apache.geode.InternalGemFireError) CacheWriterException(org.apache.geode.cache.CacheWriterException) TimeoutException(org.apache.geode.cache.TimeoutException)

Aggregations

TimeoutException (org.apache.geode.cache.TimeoutException)48 Test (org.junit.Test)24 CacheException (org.apache.geode.cache.CacheException)22 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)21 Host (org.apache.geode.test.dunit.Host)20 VM (org.apache.geode.test.dunit.VM)20 Region (org.apache.geode.cache.Region)18 AttributesFactory (org.apache.geode.cache.AttributesFactory)17 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)14 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)13 CacheWriterException (org.apache.geode.cache.CacheWriterException)10 LoaderHelper (org.apache.geode.cache.LoaderHelper)10 Lock (java.util.concurrent.locks.Lock)8 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)6 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)6 DLockTest (org.apache.geode.test.junit.categories.DLockTest)6 IOException (java.io.IOException)5 InternalGemFireError (org.apache.geode.InternalGemFireError)5 CacheWriter (org.apache.geode.cache.CacheWriter)5 StringId (org.apache.geode.i18n.StringId)5