Search in sources :

Example 26 with TimeoutException

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

the class PartitionedRegion method updateEntryVersionInBucket.

public void updateEntryVersionInBucket(EntryEventImpl event) {
    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) {
                this.dataStore.updateEntryVersionLocally(bucketId, event);
            } else {
                updateEntryVersionRemotely(retryNode, bucketId, event);
            }
            return;
        } catch (ConcurrentCacheModificationException e) {
            if (isDebugEnabled) {
                logger.debug("updateEntryVersionInBucket: caught concurrent cache modification exception", e);
            }
            event.isConcurrencyConflict(true);
            if (isDebugEnabled) {
                logger.debug("ConcurrentCacheModificationException received for updateEntryVersionInBucket 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("updateEntryVersionInBucket: 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();
            }
        } catch (PrimaryBucketException notPrimary) {
            if (isDebugEnabled) {
                logger.debug("updateEntryVersionInBucket {} on Node {} not primary", notPrimary.getLocalizedMessage(), retryNode);
            }
            getRegionAdvisor().notPrimary(bucketId, retryNode);
            retryNode = getOrCreateNodeForBucketWrite(bucketId, retryTime);
        }
        count++;
        if (isDebugEnabled) {
            logger.debug("updateEntryVersionInBucket: Attempting to resend update version to node {} after {} failed attempts", retryNode, count);
        }
    }
    // while
    // No target was found
    PartitionedRegionDistributionException e = new PartitionedRegionDistributionException(LocalizedStrings.PartitionedRegion_NO_VM_AVAILABLE_FOR_UPDATE_ENTRY_VERSION_IN_0_ATTEMPTS.toLocalizedString(// Fix for bug 36014
    count));
    if (!isDebugEnabled) {
        logger.warn(LocalizedMessage.create(LocalizedStrings.PartitionedRegion_NO_VM_AVAILABLE_FOR_UPDATE_ENTRY_VERSION_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 27 with TimeoutException

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

the class PartitionedRegion method getBucketKeys.

/**
   * Fetch the keys for the given bucket identifier, if the bucket is local or remote. This version
   * of the method allows you to retrieve Tombstone entries as well as undestroyed entries.
   * 
   * @param allowTombstones whether to include destroyed entries in the result
   * @return A set of keys from bucketNum or {@link Collections#EMPTY_SET}if no keys can be found.
   */
public Set getBucketKeys(int bucketNum, boolean allowTombstones) {
    Integer buck = bucketNum;
    final int retryAttempts = calcRetry();
    Set ret = null;
    int count = 0;
    InternalDistributedMember nod = getOrCreateNodeForBucketRead(bucketNum);
    RetryTimeKeeper snoozer = null;
    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 (nod == null) {
            if (snoozer == null) {
                snoozer = new RetryTimeKeeper(this.retryTimeout);
            }
            nod = getOrCreateNodeForBucketRead(bucketNum);
            // No storage found for bucket, early out preventing hot loop, bug 36819
            if (nod == null) {
                checkShutdown();
                break;
            }
            count++;
            continue;
        }
        try {
            if (nod.equals(getMyId())) {
                ret = this.dataStore.getKeysLocally(buck, allowTombstones);
            } else {
                FetchKeysResponse r = FetchKeysMessage.send(nod, this, buck, allowTombstones);
                ret = r.waitForKeys();
            }
            if (ret != null) {
                return ret;
            }
        } catch (PRLocallyDestroyedException ignore) {
            if (logger.isDebugEnabled()) {
                logger.debug("getBucketKeys: Encountered PRLocallyDestroyedException");
            }
            checkReadiness();
        } catch (ForceReattemptException prce) {
            if (logger.isDebugEnabled()) {
                logger.debug("getBucketKeys: attempt:{}", (count + 1), prce);
            }
            checkReadiness();
            if (snoozer == null) {
                snoozer = new RetryTimeKeeper(this.retryTimeout);
            }
            InternalDistributedMember oldNode = nod;
            nod = getNodeForBucketRead(buck);
            if (nod != null && nod.equals(oldNode)) {
                if (snoozer.overMaximum()) {
                    checkReadiness();
                    throw new TimeoutException(LocalizedStrings.PartitionedRegion_ATTEMPT_TO_ACQUIRE_PRIMARY_NODE_FOR_READ_ON_BUCKET_0_TIMED_OUT_IN_1_MS.toLocalizedString(new Object[] { getBucketName(buck), snoozer.getRetryTime() }));
                }
                snoozer.waitToRetryNode();
            }
        }
        count++;
    }
    if (logger.isDebugEnabled()) {
        logger.debug("getBucketKeys: no keys found returning empty set");
    }
    return Collections.emptySet();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashSet(java.util.HashSet) Set(java.util.Set) ResultsSet(org.apache.geode.cache.query.internal.ResultsSet) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) PRLocallyDestroyedException(org.apache.geode.internal.cache.partitioned.PRLocallyDestroyedException) FetchKeysResponse(org.apache.geode.internal.cache.partitioned.FetchKeysMessage.FetchKeysResponse) TimeoutException(org.apache.geode.cache.TimeoutException)

Example 28 with TimeoutException

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

the class GlobalRegionDUnitTest method testPutGetTimeout.

/**
   * Tests that {@link Region#put} and {@link Region#get} timeout when another VM holds the
   * distributed lock on the entry in question.
   */
@Test
public void testPutGetTimeout() {
    assertEquals(Scope.GLOBAL, getRegionAttributes().getScope());
    final String name = this.getUniqueName();
    final Object key = "KEY";
    final Object value = "VALUE";
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    SerializableRunnable create = new CacheSerializableRunnable("Create Region") {

        public void run2() throws CacheException {
            createRegion(name);
        }
    };
    vm0.invoke(create);
    vm1.invoke(create);
    vm0.invoke(new CacheSerializableRunnable("Lock entry") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            Lock lock = region.getDistributedLock(key);
            lock.lock();
        }
    });
    vm1.invoke(new CacheSerializableRunnable("Attempt get/put") {

        public void run2() throws CacheException {
            Cache cache = getCache();
            cache.setLockTimeout(1);
            cache.setSearchTimeout(1);
            Region region = getRootRegion().getSubregion(name);
            try {
                region.put(key, value);
                fail("Should have thrown a TimeoutException on put");
            } catch (TimeoutException ex) {
            // pass..
            }
            // With a loader, should try to lock and time out
            region.getAttributesMutator().setCacheLoader(new TestCacheLoader() {

                public Object load2(LoaderHelper helper) {
                    return null;
                }
            });
            try {
                region.get(key);
                fail("Should have thrown a TimeoutException on get");
            } catch (TimeoutException ex) {
            // pass..
            }
            // Without a loader, should succeed
            region.getAttributesMutator().setCacheLoader(null);
            region.get(key);
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Unlock entry") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            Lock lock = region.getDistributedLock(key);
            lock.unlock();
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) Lock(java.util.concurrent.locks.Lock) LoaderHelper(org.apache.geode.cache.LoaderHelper) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache) TimeoutException(org.apache.geode.cache.TimeoutException) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 29 with TimeoutException

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

the class GlobalLockingDUnitTest method testPutLockTimeout.

/**
   * get the lock in one VM, try to put() in other
   */
@Test
public void testPutLockTimeout() {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    final String name = this.getUniqueName();
    final Object key = new Integer(5);
    vm0.invoke(new CacheSerializableRunnable("Get lock") {

        public void run2() throws CacheException {
            Region r = getOrCreateRootRegion().createSubregion(name, getGlobalAttrs());
            Lock lock = r.getDistributedLock(key);
            lock.lock();
        }
    });
    vm1.invoke(new CacheSerializableRunnable("Lock timeout putting entry") {

        public void run2() throws CacheException {
            getOrCreateRootRegion().getCache().setLockTimeout(2);
            Region r = getOrCreateRootRegion().createSubregion(name, getGlobalAttrs());
            try {
                r.put(key, "the value");
                fail("put() should have thrown TimeoutException");
            } catch (TimeoutException ex) {
            // pass
            }
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) Lock(java.util.concurrent.locks.Lock) TimeoutException(org.apache.geode.cache.TimeoutException) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) DLockTest(org.apache.geode.test.junit.categories.DLockTest)

Example 30 with TimeoutException

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

the class GlobalLockingDUnitTest method testInvalidateLockTimeout.

/**
   * get lock in one VM, try to invalidate in other
   */
@Test
public void testInvalidateLockTimeout() {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    final String name = this.getUniqueName();
    final Object key = new Integer(5);
    vm0.invoke(new CacheSerializableRunnable("Get lock") {

        public void run2() throws CacheException {
            Region r = getOrCreateRootRegion().createSubregion(name, getGlobalAttrs());
            Lock lock = r.getDistributedLock(key);
            lock.lock();
        }
    });
    vm1.invoke(new CacheSerializableRunnable("Lock timeout invalidating entry") {

        public void run2() throws CacheException {
            getOrCreateRootRegion().getCache().setLockTimeout(2);
            Region r = getOrCreateRootRegion().createSubregion(name, getGlobalAttrs());
            try {
                r.invalidate(key);
                fail("invalidate() should have thrown TimeoutException");
            } catch (TimeoutException ex) {
            // pass
            }
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) Lock(java.util.concurrent.locks.Lock) TimeoutException(org.apache.geode.cache.TimeoutException) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) DLockTest(org.apache.geode.test.junit.categories.DLockTest)

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