Search in sources :

Example 36 with CacheLoaderException

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

the class MemoryThresholdsOffHeapDUnitTest method createPR.

private CacheSerializableRunnable createPR(final String rName, final boolean accessor) {
    return new CacheSerializableRunnable("create PR accessor") {

        @Override
        public void run2() throws CacheException {
            // Assert some level of connectivity
            getSystem(getOffHeapProperties());
            InternalResourceManager irm = (InternalResourceManager) getCache().getResourceManager();
            irm.setCriticalOffHeapPercentage(90f);
            AttributesFactory<Integer, String> af = new AttributesFactory<Integer, String>();
            if (!accessor) {
                af.setCacheLoader(new CacheLoader<Integer, String>() {

                    final AtomicInteger numLoaderInvocations = new AtomicInteger(0);

                    public String load(LoaderHelper<Integer, String> helper) throws CacheLoaderException {
                        Integer expectedInvocations = (Integer) helper.getArgument();
                        final int actualInvocations = this.numLoaderInvocations.getAndIncrement();
                        if (expectedInvocations.intValue() != actualInvocations) {
                            throw new CacheLoaderException("Expected " + expectedInvocations + " invocations, actual is " + actualInvocations);
                        }
                        return helper.getKey().toString();
                    }

                    public void close() {
                    }
                });
                af.setPartitionAttributes(new PartitionAttributesFactory().create());
            } else {
                af.setPartitionAttributes(new PartitionAttributesFactory().setLocalMaxMemory(0).create());
            }
            af.setOffHeap(true);
            getCache().createRegion(rName, af.create());
        }
    };
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) InternalResourceManager(org.apache.geode.internal.cache.control.InternalResourceManager)

Example 37 with CacheLoaderException

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

the class MemoryThresholdsOffHeapDUnitTest method testDRLoadRejection.

/**
   * Test that DistributedRegion cacheLoade and netLoad are passed through to the calling thread if
   * the local VM is in a critical state. Once the VM has moved to a safe state then test that they
   * are allowed.
   */
// GEODE-438: test pollution, async actions, time sensitive,
@Category(FlakyTest.class)
// waitForCriterion, TODO: consider disconnect DS in setup
@Test
public void testDRLoadRejection() throws Exception {
    final Host host = Host.getHost(0);
    final VM replicate1 = host.getVM(1);
    final VM replicate2 = host.getVM(2);
    final String rName = getUniqueName();
    // Make sure the desired VMs will have a fresh DS.
    AsyncInvocation d1 = replicate1.invokeAsync(() -> disconnectFromDS());
    AsyncInvocation d2 = replicate2.invokeAsync(() -> disconnectFromDS());
    d1.join();
    assertFalse(d1.exceptionOccurred());
    d2.join();
    assertFalse(d2.exceptionOccurred());
    CacheSerializableRunnable establishConnectivity = new CacheSerializableRunnable("establishcConnectivity") {

        @SuppressWarnings("synthetic-access")
        @Override
        public void run2() throws CacheException {
            getSystem(getOffHeapProperties());
        }
    };
    replicate1.invoke(establishConnectivity);
    replicate2.invoke(establishConnectivity);
    CacheSerializableRunnable createRegion = new CacheSerializableRunnable("create DistributedRegion") {

        @Override
        public void run2() throws CacheException {
            // Assert some level of connectivity
            InternalDistributedSystem ds = getSystem(getOffHeapProperties());
            assertTrue(ds.getDistributionManager().getNormalDistributionManagerIds().size() >= 1);
            InternalResourceManager irm = (InternalResourceManager) getCache().getResourceManager();
            irm.setCriticalOffHeapPercentage(90f);
            AttributesFactory af = new AttributesFactory();
            af.setScope(Scope.DISTRIBUTED_ACK);
            af.setDataPolicy(DataPolicy.REPLICATE);
            af.setOffHeap(true);
            Region region = getCache().createRegion(rName, af.create());
        }
    };
    replicate1.invoke(createRegion);
    replicate2.invoke(createRegion);
    replicate1.invoke(addExpectedException);
    replicate2.invoke(addExpectedException);
    final Integer expected = (Integer) replicate1.invoke(new SerializableCallable("test Local DistributedRegion Load") {

        public Object call() throws Exception {
            final DistributedRegion r = (DistributedRegion) getCache().getRegion(rName);
            AttributesMutator<Integer, String> am = r.getAttributesMutator();
            am.setCacheLoader(new CacheLoader<Integer, String>() {

                final AtomicInteger numLoaderInvocations = new AtomicInteger(0);

                public String load(LoaderHelper<Integer, String> helper) throws CacheLoaderException {
                    Integer expectedInvocations = (Integer) helper.getArgument();
                    final int actualInvocations = this.numLoaderInvocations.getAndIncrement();
                    if (expectedInvocations.intValue() != actualInvocations) {
                        throw new CacheLoaderException("Expected " + expectedInvocations + " invocations, actual is " + actualInvocations);
                    }
                    return helper.getKey().toString();
                }

                public void close() {
                }
            });
            int expectedInvocations = 0;
            final OffHeapMemoryMonitor ohmm = ((InternalResourceManager) getCache().getResourceManager()).getOffHeapMonitor();
            assertFalse(ohmm.getState().isCritical());
            {
                Integer k = new Integer(1);
                assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
            }
            r.put("oh1", new byte[838860]);
            r.put("oh3", new byte[157287]);
            WaitCriterion wc = new WaitCriterion() {

                public String description() {
                    return "expected region " + r + " to set memoryThreshold";
                }

                public boolean done() {
                    return r.memoryThresholdReached.get();
                }
            };
            Wait.waitForCriterion(wc, 30 * 1000, 10, true);
            {
                Integer k = new Integer(2);
                assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
            }
            r.destroy("oh3");
            wc = new WaitCriterion() {

                public String description() {
                    return "expected region " + r + " to unset memoryThreshold";
                }

                public boolean done() {
                    return !r.memoryThresholdReached.get();
                }
            };
            Wait.waitForCriterion(wc, 30 * 1000, 10, true);
            {
                Integer k = new Integer(3);
                assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
            }
            return new Integer(expectedInvocations);
        }
    });
    final CacheSerializableRunnable validateData1 = new CacheSerializableRunnable("Validate data 1") {

        @Override
        public void run2() throws CacheException {
            Region r = getCache().getRegion(rName);
            Integer i1 = new Integer(1);
            assertTrue(r.containsKey(i1));
            assertNotNull(r.getEntry(i1));
            Integer i2 = new Integer(2);
            assertFalse(r.containsKey(i2));
            assertNull(r.getEntry(i2));
            Integer i3 = new Integer(3);
            assertTrue(r.containsKey(i3));
            assertNotNull(r.getEntry(i3));
        }
    };
    replicate1.invoke(validateData1);
    replicate2.invoke(validateData1);
    replicate2.invoke(new SerializableCallable("test DistributedRegion netLoad") {

        public Object call() throws Exception {
            final DistributedRegion r = (DistributedRegion) getCache().getRegion(rName);
            final OffHeapMemoryMonitor ohmm = ((InternalResourceManager) getCache().getResourceManager()).getOffHeapMonitor();
            assertFalse(ohmm.getState().isCritical());
            int expectedInvocations = expected.intValue();
            {
                Integer k = new Integer(4);
                assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
            }
            // Place in a critical state for the next test
            r.put("oh3", new byte[157287]);
            WaitCriterion wc = new WaitCriterion() {

                public String description() {
                    return "expected region " + r + " to set memoryThreshold";
                }

                public boolean done() {
                    return r.memoryThresholdReached.get();
                }
            };
            Wait.waitForCriterion(wc, 30 * 1000, 10, true);
            {
                Integer k = new Integer(5);
                assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
            }
            r.destroy("oh3");
            wc = new WaitCriterion() {

                public String description() {
                    return "expected region " + r + " to unset memoryThreshold";
                }

                public boolean done() {
                    return !r.memoryThresholdReached.get();
                }
            };
            Wait.waitForCriterion(wc, 30 * 1000, 10, true);
            {
                Integer k = new Integer(6);
                assertEquals(k.toString(), r.get(k, new Integer(expectedInvocations++)));
            }
            return new Integer(expectedInvocations);
        }
    });
    replicate1.invoke(removeExpectedException);
    replicate2.invoke(removeExpectedException);
    final CacheSerializableRunnable validateData2 = new CacheSerializableRunnable("Validate data 2") {

        @Override
        public void run2() throws CacheException {
            Region<Integer, String> r = getCache().getRegion(rName);
            Integer i4 = new Integer(4);
            assertTrue(r.containsKey(i4));
            assertNotNull(r.getEntry(i4));
            Integer i5 = new Integer(5);
            assertFalse(r.containsKey(i5));
            assertNull(r.getEntry(i5));
            Integer i6 = new Integer(6);
            assertTrue(r.containsKey(i6));
            assertNotNull(r.getEntry(i6));
        }
    };
    replicate1.invoke(validateData2);
    replicate2.invoke(validateData2);
}
Also used : OffHeapMemoryMonitor(org.apache.geode.internal.cache.control.OffHeapMemoryMonitor) Host(org.apache.geode.test.dunit.Host) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) IgnoredException(org.apache.geode.test.dunit.IgnoredException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) LowMemoryException(org.apache.geode.cache.LowMemoryException) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) CacheException(org.apache.geode.cache.CacheException) InternalResourceManager(org.apache.geode.internal.cache.control.InternalResourceManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) LocalRegion(org.apache.geode.internal.cache.LocalRegion) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) ProxyBucketRegion(org.apache.geode.internal.cache.ProxyBucketRegion) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) Category(org.junit.experimental.categories.Category) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 38 with CacheLoaderException

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

the class MultiVMRegionTestCase method testLocalCacheLoader.

/**
   * Tests that a local loader is preferred to a remote one
   */
@Test
public void testLocalCacheLoader() throws Exception {
    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 \"remote\" region") {

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

                @Override
                public Object load2(LoaderHelper helper) throws CacheLoaderException {
                    if (helper.getRegion().getAttributes().getPartitionAttributes() == null) {
                        fail("Should not be invoked");
                        return null;
                    } else {
                        return value;
                    }
                }
            };
            region.getAttributesMutator().setCacheLoader(loader);
        }
    };
    vm0.invoke(new CacheSerializableRunnable("Create \"local\" region") {

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

                @Override
                public Object load2(LoaderHelper helper) throws CacheLoaderException {
                    return value;
                }
            });
        }
    });
    vm1.invoke(create);
    vm0.invoke(new CacheSerializableRunnable("Get") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            assertEquals(value, region.get(key));
        }
    });
    vm1.invoke(new SerializableRunnable("Verify loader not invoked") {

        @Override
        public void run() {
            assertFalse(loader.wasInvoked());
        }
    });
}
Also used : LoaderHelper(org.apache.geode.cache.LoaderHelper) CacheException(org.apache.geode.cache.CacheException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) 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) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest)

Example 39 with CacheLoaderException

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

the class MultiVMRegionTestCase method testRemoteCacheLoaderException.

/**
   * Tests that a remote {@link CacheLoader} that throws a {@link CacheLoaderException} results is
   * propagated back to the caller.
   */
@Test
public void testRemoteCacheLoaderException() throws Exception {
    assumeTrue(supportsNetLoad());
    assertTrue(getRegionAttributes().getScope().isDistributed());
    final String name = this.getUniqueName();
    final Object key = "KEY";
    SerializableRunnable create = new CacheSerializableRunnable("Create Region") {

        @Override
        public void run2() throws CacheException {
            createRegion(name);
        }
    };
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    vm0.invoke(create);
    vm1.invoke(create);
    vm1.invoke(new CacheSerializableRunnable("Set CacheLoader") {

        @Override
        public void run2() throws CacheException {
            final Region region = getRootRegion().getSubregion(name);
            loader = new TestCacheLoader() {

                @Override
                public Object load2(LoaderHelper helper) throws CacheLoaderException {
                    assertEquals(region, helper.getRegion());
                    assertEquals(key, helper.getKey());
                    assertNull(helper.getArgument());
                    String s = "Test Exception";
                    throw new CacheLoaderException(s);
                }
            };
            region.getAttributesMutator().setCacheLoader(loader);
            flushIfNecessary(region);
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Remote load") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            try {
                region.get(key);
                fail("Should have thrown a CacheLoaderException");
            } catch (CacheLoaderException ex) {
            // pass...
            }
        }
    });
    vm1.invoke(new SerializableRunnable("Verify loader") {

        @Override
        public void run() {
            assertTrue(loader.wasInvoked());
        }
    });
}
Also used : LoaderHelper(org.apache.geode.cache.LoaderHelper) CacheException(org.apache.geode.cache.CacheException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) 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) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest)

Example 40 with CacheLoaderException

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

the class MultiVMRegionTestCase method testMirroredLocalLoad.

/**
   * Tests that a local load occurs, even with mirroring
   */
@Test
public void testMirroredLocalLoad() throws Exception {
    assumeTrue(supportsReplication());
    final String name = this.getUniqueName();
    final Object key = "KEY";
    final Object value = "VALUE";
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    // use VMs on different gemfire systems
    VM vm2 = host.getVM(2);
    vm0.invoke(new CacheSerializableRunnable("Create region with loader") {

        @Override
        public void run2() throws CacheException {
            RegionAttributes ra = getRegionAttributes();
            AttributesFactory factory = new AttributesFactory(ra);
            if (ra.getEvictionAttributes() == null || !ra.getEvictionAttributes().getAction().isOverflowToDisk()) {
                factory.setDiskStoreName(null);
            }
            factory.setDataPolicy(DataPolicy.REPLICATE);
            factory.setCacheLoader(new TestCacheLoader() {

                @Override
                public Object load2(LoaderHelper helper) throws CacheLoaderException {
                    return value;
                }
            });
            createRegion(name, factory.create());
        }
    });
    SerializableRunnable create = new CacheSerializableRunnable("Create region with bad loader") {

        @Override
        public void run2() throws CacheException {
            RegionAttributes ra = getRegionAttributes();
            AttributesFactory factory = new AttributesFactory(ra);
            if (ra.getEvictionAttributes() == null || !ra.getEvictionAttributes().getAction().isOverflowToDisk()) {
                factory.setDiskStoreName(null);
            }
            factory.setDataPolicy(DataPolicy.REPLICATE);
            loader = new TestCacheLoader() {

                @Override
                public Object load2(LoaderHelper helper) throws CacheLoaderException {
                    fail("Should not be invoked");
                    return null;
                }
            };
            factory.setCacheLoader(loader);
            createRegion(name, factory.create());
        }
    };
    vm2.invoke(create);
    vm0.invoke(new CacheSerializableRunnable("Get") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            assertEquals(value, region.get(key));
        }
    });
    vm2.invoke(new CacheSerializableRunnable("Verify no load") {

        @Override
        public void run2() throws CacheException {
            assertFalse(loader.wasInvoked());
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) RegionAttributes(org.apache.geode.cache.RegionAttributes) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) 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) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest)

Aggregations

CacheLoaderException (org.apache.geode.cache.CacheLoaderException)45 Test (org.junit.Test)32 LoaderHelper (org.apache.geode.cache.LoaderHelper)31 Region (org.apache.geode.cache.Region)31 AttributesFactory (org.apache.geode.cache.AttributesFactory)25 VM (org.apache.geode.test.dunit.VM)23 CacheException (org.apache.geode.cache.CacheException)22 Host (org.apache.geode.test.dunit.Host)22 CacheLoader (org.apache.geode.cache.CacheLoader)19 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)19 TimeoutException (org.apache.geode.cache.TimeoutException)15 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)15 LocalRegion (org.apache.geode.internal.cache.LocalRegion)14 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)11 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)10 CacheWriterException (org.apache.geode.cache.CacheWriterException)9 StoredObject (org.apache.geode.internal.offheap.StoredObject)8 EntryEvent (org.apache.geode.cache.EntryEvent)7 AttributesMutator (org.apache.geode.cache.AttributesMutator)6 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)6