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;
}
}
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");
}
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;
}
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;
}
}
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();
}
}
Aggregations