Search in sources :

Example 11 with TimeoutException

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

the class GemFireCacheImpl method shutDownOnePRGracefully.

private void shutDownOnePRGracefully(PartitionedRegion partitionedRegion) {
    boolean acquiredLock = false;
    try {
        partitionedRegion.acquireDestroyLock();
        acquiredLock = true;
        synchronized (partitionedRegion.getRedundancyProvider()) {
            if (partitionedRegion.isDataStore() && partitionedRegion.getDataStore() != null && partitionedRegion.getDataPolicy() == DataPolicy.PERSISTENT_PARTITION) {
                int numBuckets = partitionedRegion.getTotalNumberOfBuckets();
                Map<InternalDistributedMember, PersistentMemberID>[] bucketMaps = new Map[numBuckets];
                PartitionedRegionDataStore dataStore = partitionedRegion.getDataStore();
                // lock all the primary buckets
                Set<Entry<Integer, BucketRegion>> bucketEntries = dataStore.getAllLocalBuckets();
                for (Entry e : bucketEntries) {
                    BucketRegion bucket = (BucketRegion) e.getValue();
                    if (bucket == null || bucket.isDestroyed) {
                        // bucket region could be destroyed in race condition
                        continue;
                    }
                    bucket.getBucketAdvisor().tryLockIfPrimary();
                    // get map <InternalDistributedMember, persistentID> for this bucket's
                    // remote members
                    bucketMaps[bucket.getId()] = bucket.getBucketAdvisor().adviseInitializedPersistentMembers();
                    if (logger.isDebugEnabled()) {
                        logger.debug("shutDownAll: PR {}: initialized persistent members for {}:{}", partitionedRegion.getName(), bucket.getId(), bucketMaps[bucket.getId()]);
                    }
                }
                if (logger.isDebugEnabled()) {
                    logger.debug("shutDownAll: All buckets for PR {} are locked.", partitionedRegion.getName());
                }
                // send lock profile update to other members
                partitionedRegion.setShutDownAllStatus(PartitionedRegion.PRIMARY_BUCKETS_LOCKED);
                new UpdateAttributesProcessor(partitionedRegion).distribute(false);
                partitionedRegion.getRegionAdvisor().waitForProfileStatus(PartitionedRegion.PRIMARY_BUCKETS_LOCKED);
                if (logger.isDebugEnabled()) {
                    logger.debug("shutDownAll: PR {}: all bucketLock profiles received.", partitionedRegion.getName());
                }
                // if async write, do flush
                if (!partitionedRegion.getAttributes().isDiskSynchronous()) {
                    // several PRs might share the same diskStore, we will only flush once
                    // even flush is called several times.
                    partitionedRegion.getDiskStore().forceFlush();
                    // send flush profile update to other members
                    partitionedRegion.setShutDownAllStatus(PartitionedRegion.DISK_STORE_FLUSHED);
                    new UpdateAttributesProcessor(partitionedRegion).distribute(false);
                    partitionedRegion.getRegionAdvisor().waitForProfileStatus(PartitionedRegion.DISK_STORE_FLUSHED);
                    if (logger.isDebugEnabled()) {
                        logger.debug("shutDownAll: PR {}: all flush profiles received.", partitionedRegion.getName());
                    }
                }
                // async write
                // persist other members to OFFLINE_EQUAL for each bucket region
                // iterate through all the bucketMaps and exclude the items whose
                // idm is no longer online
                Set<InternalDistributedMember> membersToPersistOfflineEqual = partitionedRegion.getRegionAdvisor().adviseDataStore();
                for (Entry e : bucketEntries) {
                    BucketRegion bucket = (BucketRegion) e.getValue();
                    if (bucket == null || bucket.isDestroyed) {
                        // bucket region could be destroyed in race condition
                        continue;
                    }
                    Map<InternalDistributedMember, PersistentMemberID> persistMap = getSubMapForLiveMembers(membersToPersistOfflineEqual, bucketMaps[bucket.getId()]);
                    if (persistMap != null) {
                        bucket.getPersistenceAdvisor().persistMembersOfflineAndEqual(persistMap);
                        if (logger.isDebugEnabled()) {
                            logger.debug("shutDownAll: PR {}: persisting bucket {}:{}", partitionedRegion.getName(), bucket.getId(), persistMap);
                        }
                    }
                }
                // send persisted profile update to other members, let all members to persist
                // before close the region
                partitionedRegion.setShutDownAllStatus(PartitionedRegion.OFFLINE_EQUAL_PERSISTED);
                new UpdateAttributesProcessor(partitionedRegion).distribute(false);
                partitionedRegion.getRegionAdvisor().waitForProfileStatus(PartitionedRegion.OFFLINE_EQUAL_PERSISTED);
                if (logger.isDebugEnabled()) {
                    logger.debug("shutDownAll: PR {}: all offline_equal profiles received.", partitionedRegion.getName());
                }
            }
            // dataStore
            // after done all steps for buckets, close partitionedRegion
            // close accessor directly
            RegionEventImpl event = new RegionEventImpl(partitionedRegion, Operation.REGION_CLOSE, null, false, getMyId(), true);
            try {
                // not to acquire lock
                partitionedRegion.basicDestroyRegion(event, false, false, true);
            } catch (CacheWriterException e) {
                // not possible with local operation, CacheWriter not called
                throw new Error(LocalizedStrings.LocalRegion_CACHEWRITEREXCEPTION_SHOULD_NOT_BE_THROWN_IN_LOCALDESTROYREGION.toLocalizedString(), e);
            } catch (TimeoutException e) {
                // not possible with local operation, no distributed locks possible
                throw new Error(LocalizedStrings.LocalRegion_TIMEOUTEXCEPTION_SHOULD_NOT_BE_THROWN_IN_LOCALDESTROYREGION.toLocalizedString(), e);
            }
        }
    // synchronized
    } catch (CacheClosedException cce) {
        logger.debug("Encounter CacheClosedException when shutDownAll is closing PR: {}:{}", partitionedRegion.getFullPath(), cce.getMessage());
    } catch (CancelException ce) {
        logger.debug("Encounter CancelException when shutDownAll is closing PR: {}:{}", partitionedRegion.getFullPath(), ce.getMessage());
    } catch (RegionDestroyedException rde) {
        logger.debug("Encounter CacheDestroyedException when shutDownAll is closing PR: {}:{}", partitionedRegion.getFullPath(), rde.getMessage());
    } finally {
        if (acquiredLock) {
            partitionedRegion.releaseDestroyLock();
        }
    }
}
Also used : RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) InternalGemFireError(org.apache.geode.InternalGemFireError) CacheClosedException(org.apache.geode.cache.CacheClosedException) SimpleExtensionPoint(org.apache.geode.internal.cache.extension.SimpleExtensionPoint) ExtensionPoint(org.apache.geode.internal.cache.extension.ExtensionPoint) PersistentMemberID(org.apache.geode.internal.cache.persistence.PersistentMemberID) Entry(java.util.Map.Entry) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) CancelException(org.apache.geode.CancelException) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) SortedMap(java.util.SortedMap) HashMap(java.util.HashMap) CacheWriterException(org.apache.geode.cache.CacheWriterException) TimeoutException(org.apache.geode.cache.TimeoutException)

Example 12 with TimeoutException

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

the class MultiVMRegionTestCase method testCacheLoaderWithNetLoad.

@Test
public void testCacheLoaderWithNetLoad() throws Exception {
    // replicated regions and partitioned regions make no sense for this
    // test
    assumeFalse(getRegionAttributes().getDataPolicy().withReplication());
    assumeFalse(getRegionAttributes().getDataPolicy().isPreloaded());
    assumeTrue(getRegionAttributes().getPartitionAttributes() == null);
    final String name = this.getUniqueName();
    final Object key = this.getUniqueName();
    final Object value = new Integer(42);
    Host host = Host.getHost(0);
    // use vm on other gemfire system
    VM vm1 = host.getVM(1);
    vm1.invoke(new CacheSerializableRunnable("set up remote loader") {

        @Override
        public void run2() throws CacheException {
            final TestCacheLoader remoteloader = new TestCacheLoader() {

                @Override
                public Object load2(LoaderHelper helper) throws CacheLoaderException {
                    assertEquals(key, helper.getKey());
                    assertEquals(name, helper.getRegion().getName());
                    return value;
                }
            };
            AttributesFactory factory = new AttributesFactory(getRegionAttributes());
            factory.setCacheLoader(remoteloader);
            createRegion(name, factory.create());
        }
    });
    final TestCacheLoader loader1 = new TestCacheLoader() {

        @Override
        public Object load2(LoaderHelper helper) throws CacheLoaderException {
            assertEquals(key, helper.getKey());
            assertEquals(name, helper.getRegion().getName());
            try {
                helper.getRegion().getAttributes();
                Object result = helper.netSearch(true);
                assertEquals(value, result);
                return result;
            } catch (TimeoutException ex) {
                fail("Why did I time out?", ex);
            }
            return null;
        }
    };
    AttributesFactory f = new AttributesFactory(getRegionAttributes());
    f.setCacheLoader(loader1);
    Region region = createRegion(name, f.create());
    loader1.wasInvoked();
    Region.Entry entry = region.getEntry(key);
    assertNull(entry);
    region.create(key, null);
    entry = region.getEntry(key);
    assertNotNull(entry);
    assertNull(entry.getValue());
    assertEquals(value, region.get(key));
    assertTrue(loader1.wasInvoked());
    assertEquals(value, region.getEntry(key).getValue());
}
Also used : CacheException(org.apache.geode.cache.CacheException) Host(org.apache.geode.test.dunit.Host) LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) VM(org.apache.geode.test.dunit.VM) LocalRegion(org.apache.geode.internal.cache.LocalRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) StoredObject(org.apache.geode.internal.offheap.StoredObject) TimeoutException(org.apache.geode.cache.TimeoutException) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest)

Example 13 with TimeoutException

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

the class MultiVMRegionTestCase method testCacheLoaderWithNetSearch.

@Test
public void testCacheLoaderWithNetSearch() throws Exception {
    assumeTrue(supportsNetLoad());
    // some tests use mirroring by default (e.g. persistBackup regions)
    // if so, then this test won't work right
    assumeFalse(getRegionAttributes().getDataPolicy().withReplication());
    assumeFalse(getRegionAttributes().getDataPolicy().isPreloaded());
    final String name = this.getUniqueName();
    final Object key = this.getUniqueName();
    final Object value = new Integer(42);
    Host host = Host.getHost(0);
    // use vm on other gemfire system
    VM vm1 = host.getVM(1);
    vm1.invoke(new CacheSerializableRunnable("set remote value") {

        @Override
        public void run2() throws CacheException {
            Region rgn = createRegion(name);
            rgn.put(key, value);
            flushIfNecessary(rgn);
        }
    });
    final TestCacheLoader loader1 = new TestCacheLoader() {

        @Override
        public Object load2(LoaderHelper helper) throws CacheLoaderException {
            assertEquals(key, helper.getKey());
            assertEquals(name, helper.getRegion().getName());
            try {
                helper.getRegion().getAttributes();
                Object result = helper.netSearch(false);
                assertEquals(value, result);
                return result;
            } catch (TimeoutException ex) {
                fail("Why did I time out?", ex);
            }
            return null;
        }
    };
    AttributesFactory f = new AttributesFactory(getRegionAttributes());
    f.setCacheLoader(loader1);
    Region region = createRegion(name, f.create());
    loader1.wasInvoked();
    Region.Entry entry = region.getEntry(key);
    assertNull(entry);
    region.create(key, null);
    entry = region.getEntry(key);
    assertNotNull(entry);
    assertNull(entry.getValue());
    // make sure value is still there in vm1
    vm1.invoke(new CacheSerializableRunnable("verify remote value") {

        @Override
        public void run2() throws CacheException {
            Region rgn = getRootRegion().getSubregion(name);
            assertEquals(value, rgn.getEntry(key).getValue());
        }
    });
    assertEquals(value, region.get(key));
    // so we get the value but the loader is never invoked.
    if (region.getAttributes().getScope().isGlobal()) {
        assertTrue(!loader1.wasInvoked());
    } else {
        assertTrue(loader1.wasInvoked());
    }
    assertEquals(value, region.getEntry(key).getValue());
}
Also used : CacheException(org.apache.geode.cache.CacheException) Host(org.apache.geode.test.dunit.Host) LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) VM(org.apache.geode.test.dunit.VM) LocalRegion(org.apache.geode.internal.cache.LocalRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) StoredObject(org.apache.geode.internal.offheap.StoredObject) TimeoutException(org.apache.geode.cache.TimeoutException) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest)

Example 14 with TimeoutException

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

the class MultiVMRegionTestCase method testRemoteLoaderNetSearch.

/**
   * Tests that invoking <code>netSearch</code> in a remote loader returns <code>null</code> instead
   * of causing infinite recursion.
   */
@Test
public void testRemoteLoaderNetSearch() throws Exception {
    assumeTrue(supportsNetLoad());
    assertTrue(getRegionAttributes().getScope().isDistributed());
    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("Get value") {

        @Override
        public void run2() throws CacheException {
            Region region = createRegion(name);
            assertEquals(value, region.get(key));
        }
    };
    vm0.invoke(new CacheSerializableRunnable("Create Region") {

        @Override
        public void run2() throws CacheException {
            Region region = createRegion(name);
            region.getAttributesMutator().setCacheLoader(new TestCacheLoader() {

                @Override
                public Object load2(LoaderHelper helper) throws CacheLoaderException {
                    try {
                        assertNull(helper.netSearch(true));
                    } catch (TimeoutException ex) {
                        fail("Why did I time out?", ex);
                    }
                    return value;
                }
            });
        }
    });
    vm1.invoke(create);
}
Also used : LoaderHelper(org.apache.geode.cache.LoaderHelper) CacheException(org.apache.geode.cache.CacheException) VM(org.apache.geode.test.dunit.VM) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) LocalRegion(org.apache.geode.internal.cache.LocalRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) StoredObject(org.apache.geode.internal.offheap.StoredObject) Host(org.apache.geode.test.dunit.Host) TimeoutException(org.apache.geode.cache.TimeoutException) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest)

Example 15 with TimeoutException

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

the class SearchAndLoadDUnitTest method testOneHopNetWriteRemoteWriter.

/** same as the previous test but the cache writer is in a third, non-replicated, vm */
@Test
public void testOneHopNetWriteRemoteWriter() throws CacheException, InterruptedException {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    VM vm2 = host.getVM(2);
    final String name = this.getUniqueName() + "Region";
    final String objectName = "Object7";
    final Integer value = new Integer(483);
    final Integer updateValue = new Integer(484);
    vm0.invoke(new SerializableRunnable("Create replicate Region") {

        public void run() {
            try {
                AttributesFactory factory = new AttributesFactory();
                factory.setScope(Scope.DISTRIBUTED_ACK);
                factory.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
                createRegion(name, factory.create());
            } catch (CacheException ex) {
                Assert.fail("While creating empty region", ex);
            }
        }
    });
    vm1.invoke(new SerializableRunnable("Create empty Region") {

        public void run() {
            try {
                AttributesFactory factory = new AttributesFactory();
                factory.setScope(Scope.DISTRIBUTED_ACK);
                factory.setDataPolicy(DataPolicy.EMPTY);
                createRegion(name, factory.create());
            } catch (CacheException ex) {
                Assert.fail("While creating empty region", ex);
            }
        }
    });
    vm2.invoke(new SerializableRunnable("Create replicated region with cacheWriter") {

        public void run() {
            netWriteInvoked = false;
            operationWasCreate = false;
            originWasRemote = false;
            writerInvocationCount = 0;
            try {
                AttributesFactory factory = new AttributesFactory();
                factory.setScope(Scope.DISTRIBUTED_ACK);
                factory.setDataPolicy(DataPolicy.EMPTY);
                factory.setCacheWriter(new CacheWriter() {

                    public void beforeCreate(EntryEvent e) throws CacheWriterException {
                        e.getRegion().getCache().getLogger().info("cache writer beforeCreate invoked for " + e);
                        netWriteInvoked = true;
                        operationWasCreate = true;
                        originWasRemote = e.isOriginRemote();
                        writerInvocationCount++;
                        return;
                    }

                    public void beforeUpdate(EntryEvent e) throws CacheWriterException {
                        e.getRegion().getCache().getLogger().info("cache writer beforeUpdate invoked for " + e);
                        netWriteInvoked = true;
                        operationWasCreate = false;
                        originWasRemote = e.isOriginRemote();
                        writerInvocationCount++;
                        return;
                    }

                    public void beforeDestroy(EntryEvent e) throws CacheWriterException {
                    }

                    public void beforeRegionDestroy(RegionEvent e) throws CacheWriterException {
                    }

                    public void beforeRegionClear(RegionEvent e) throws CacheWriterException {
                    }

                    public void close() {
                    }
                });
                createRegion(name, factory.create());
            } catch (CacheException ex) {
                Assert.fail("While creating replicated region", ex);
            }
        }
    });
    vm1.invoke(new SerializableRunnable("do a put that should be proxied in the other vm and invoke its cache writer") {

        public void run() {
            try {
                getRootRegion().getSubregion(name).put(objectName, value);
            } catch (CacheWriterException cwe) {
            } catch (TimeoutException te) {
            }
        }
    });
    vm2.invoke(new SerializableRunnable("ensure that cache writer was invoked with correct settings in event") {

        public void run() {
            assertTrue("expected cache writer to be invoked", netWriteInvoked);
            assertTrue("expected originRemote to be true", originWasRemote);
            assertTrue("expected event to be create", operationWasCreate);
            assertEquals("expected only one cache writer invocation", 1, writerInvocationCount);
            // set flags for the next test - updating the same key
            netWriteInvoked = false;
            writerInvocationCount = 0;
        }
    });
    vm1.invoke(new SerializableRunnable("do an update that should be proxied in the other vm and invoke its cache writer") {

        public void run() {
            try {
                getRootRegion().getSubregion(name).put(objectName, updateValue);
            } catch (CacheWriterException cwe) {
            } catch (TimeoutException te) {
            }
        }
    });
    vm2.invoke(new SerializableRunnable("ensure that cache writer was invoked with correct settings in event") {

        public void run() {
            assertTrue("expected cache writer to be invoked", netWriteInvoked);
            assertTrue("expected originRemote to be true", originWasRemote);
            assertTrue("expected event to be create", operationWasCreate);
            assertEquals("expected only one cache writer invocation", 1, writerInvocationCount);
        }
    });
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheException(org.apache.geode.cache.CacheException) VM(org.apache.geode.test.dunit.VM) CacheWriter(org.apache.geode.cache.CacheWriter) EntryEvent(org.apache.geode.cache.EntryEvent) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) RegionEvent(org.apache.geode.cache.RegionEvent) CacheWriterException(org.apache.geode.cache.CacheWriterException) TimeoutException(org.apache.geode.cache.TimeoutException) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

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