Search in sources :

Example 41 with LoaderHelper

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

the class TXOrderDUnitTest method testFarSideOpForLoad.

/**
   * Tests fix for #40870 Remote CacheListeners invoke afterCreate with Operation.LOCAL_LOAD_CREATE
   * when create executed transactionally"
   */
@Ignore("TODO: test is disabled")
@Test
public void testFarSideOpForLoad() throws Exception {
    Host host = Host.getHost(0);
    VM vm1 = host.getVM(0);
    VM vm2 = host.getVM(1);
    vm1.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            AttributesFactory af = new AttributesFactory();
            af.setDataPolicy(DataPolicy.REPLICATE);
            af.setScope(Scope.DISTRIBUTED_ACK);
            CacheListener cl1 = new CacheListenerAdapter() {

                public void afterCreate(EntryEvent e) {
                    assertTrue(e.getOperation().isLocalLoad());
                }
            };
            af.addCacheListener(cl1);
            CacheLoader cl = new CacheLoader() {

                public Object load(LoaderHelper helper) throws CacheLoaderException {
                    LogWriterUtils.getLogWriter().info("Loading value:" + helper.getKey() + "_value");
                    return helper.getKey() + "_value";
                }

                public void close() {
                }
            };
            af.setCacheLoader(cl);
            createRootRegion("r1", af.create());
            return null;
        }
    });
    vm2.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            AttributesFactory af = new AttributesFactory();
            af.setDataPolicy(DataPolicy.REPLICATE);
            af.setScope(Scope.DISTRIBUTED_ACK);
            CacheListener cl1 = new CacheListenerAdapter() {

                public void afterCreate(EntryEvent e) {
                    LogWriterUtils.getLogWriter().info("op:" + e.getOperation().toString());
                    assertTrue(!e.getOperation().isLocalLoad());
                }
            };
            af.addCacheListener(cl1);
            createRootRegion("r1", af.create());
            return null;
        }
    });
    vm1.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region r = getRootRegion("r1");
            getCache().getCacheTransactionManager().begin();
            r.get("obj_2");
            getCache().getCacheTransactionManager().commit();
            return null;
        }
    });
}
Also used : Host(org.apache.geode.test.dunit.Host) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) CacheException(org.apache.geode.cache.CacheException) CacheListener(org.apache.geode.cache.CacheListener) LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) EntryEvent(org.apache.geode.cache.EntryEvent) Region(org.apache.geode.cache.Region) CacheLoader(org.apache.geode.cache.CacheLoader) Ignore(org.junit.Ignore) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 42 with LoaderHelper

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

the class SearchAndLoadDUnitTest method testConcurrentLoad.

/**
   * This test is for a bug in which a cache loader threw an exception that caused the wrong value
   * to be put in a Future in nonTxnFindObject. This in turn caused a concurrent search for the
   * object to not invoke the loader a second time.
   * 
   * VM0 is used to create a cache and a region having a loader that simulates the conditions that
   * caused the bug. One async thread then does a get() which invokes the loader. Another async
   * thread does a get() which reaches nonTxnFindObject and blocks waiting for the first thread's
   * load to complete. The loader then throws an exception that is sent back to the first thread.
   * The second thread should then cause the loader to be invoked again, and this time the loader
   * will return a value. Both threads then validate that they received the expected result.
   */
@Test
public void testConcurrentLoad() throws Throwable {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    final String name = this.getUniqueName() + "Region";
    final String objectName = "theKey";
    final Integer value = new Integer(44);
    final String exceptionString = "causing first cache-load to fail";
    remoteLoaderInvoked = false;
    loaderInvoked = false;
    vm0.invoke(new CacheSerializableRunnable("create region " + name + " in vm0") {

        public void run2() {
            remoteLoaderInvoked = false;
            loaderInvoked = false;
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.DISTRIBUTED_ACK);
            factory.setConcurrencyChecksEnabled(true);
            factory.setCacheLoader(new CacheLoader() {

                boolean firstInvocation = true;

                public synchronized Object load(LoaderHelper helper) {
                    System.out.println("invoked cache loader for " + helper.getKey());
                    loaderInvoked = true;
                    loaderInvokedLatch.countDown();
                    if (firstInvocation) {
                        firstInvocation = false;
                        try {
                            // wait for both threads to be ready for the exception to be thrown
                            System.out.println("waiting for vm0t2 to be ready before throwing exception");
                            readyForExceptionLatch.await(30, TimeUnit.SECONDS);
                            // give the second thread time to get into loader code
                            Thread.sleep(5000);
                        } catch (InterruptedException e) {
                            fail("interrupted");
                        }
                        System.out.println("throwing exception");
                        exceptionThrown = true;
                        throw new RuntimeException(exceptionString);
                    }
                    System.out.println("returning value=" + value);
                    return value;
                }

                public void close() {
                }
            });
            Region region = createRegion(name, factory.create());
            region.create(objectName, null);
            IgnoredException.addIgnoredException(exceptionString);
        }
    });
    AsyncInvocation async1 = null;
    try {
        async1 = vm0.invokeAsync(new CacheSerializableRunnable("Concurrently invoke the remote loader on the same key - t1") {

            public void run2() {
                Region region = getCache().getRegion("root/" + name);
                LogWriterUtils.getLogWriter().info("t1 is invoking get(" + objectName + ")");
                try {
                    LogWriterUtils.getLogWriter().info("t1 retrieved value " + region.get(objectName));
                    fail("first load should have triggered an exception");
                } catch (RuntimeException e) {
                    if (!e.getMessage().contains(exceptionString)) {
                        throw e;
                    }
                }
            }
        });
        vm0.invoke(new CacheSerializableRunnable("Concurrently invoke the loader on the same key - t2") {

            public void run2() {
                final Region region = getCache().getRegion("root/" + name);
                final Object[] valueHolder = new Object[1];
                // wait for vm1 to cause the loader to be invoked
                LogWriterUtils.getLogWriter().info("t2 is waiting for loader to be invoked by t1");
                try {
                    loaderInvokedLatch.await(30, TimeUnit.SECONDS);
                } catch (InterruptedException e) {
                    fail("interrupted");
                }
                assertTrue(loaderInvoked);
                Thread t = new Thread("invoke get()") {

                    public void run() {
                        try {
                            valueHolder[0] = region.get(objectName);
                        } catch (RuntimeException e) {
                            valueHolder[0] = e;
                        }
                    }
                };
                t.setDaemon(true);
                t.start();
                try {
                    // let the thread get to the point of blocking on vm1's Future
                    // in LocalRegion.nonTxnFindObject()
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    fail("interrupted");
                }
                readyForExceptionLatch.countDown();
                try {
                    t.join(30000);
                } catch (InterruptedException e) {
                    fail("interrupted");
                }
                if (t.isAlive()) {
                    t.interrupt();
                    fail("get() operation blocked for too long - test needs some work");
                }
                LogWriterUtils.getLogWriter().info("t2 is invoking get(" + objectName + ")");
                Object value = valueHolder[0];
                if (value instanceof RuntimeException) {
                    if (((Exception) value).getMessage().contains(exceptionString)) {
                        fail("second load should not have thrown an exception");
                    } else {
                        throw (RuntimeException) value;
                    }
                } else {
                    LogWriterUtils.getLogWriter().info("t2 retrieved value " + value);
                    assertNotNull(value);
                }
            }
        });
    } finally {
        if (async1 != null) {
            async1.join();
            if (async1.exceptionOccurred()) {
                throw async1.getException();
            }
        }
    }
}
Also used : Host(org.apache.geode.test.dunit.Host) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) TimeoutException(org.apache.geode.cache.TimeoutException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) CacheException(org.apache.geode.cache.CacheException) CacheWriterException(org.apache.geode.cache.CacheWriterException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) CacheLoader(org.apache.geode.cache.CacheLoader) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 43 with LoaderHelper

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

the class MultiVMRegionTestCase method testRemoteCacheLoaderArg.

/**
   * Tests that the parameter passed to a remote {@link CacheLoader} is actually passed.
   */
@Test
public void testRemoteCacheLoaderArg() throws Exception {
    assumeTrue(supportsNetLoad());
    assertTrue(getRegionAttributes().getScope().isDistributed());
    final String name = this.getUniqueName();
    final Object key = "KEY";
    final Object value = "VALUE";
    final String arg = "ARG";
    SerializableRunnable create = new CacheSerializableRunnable("Create Region") {

        @Override
        public void run2() throws CacheException {
            createRegion(name);
        // Can't test non-Serializable callback argument here
        // because netLoad will not occur because there are no
        // other members with the region defined when it is
        // created. Hooray for intelligent messaging.
        }
    };
    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());
                    assertEquals(arg, helper.getArgument());
                    return value;
                }
            };
            region.getAttributesMutator().setCacheLoader(loader);
            flushIfNecessary(region);
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Remote load") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            try {
                // Use a non-serializable arg object
                region.get(key, new Object() {
                });
                fail("Should have thrown an IllegalArgumentException");
            } catch (IllegalArgumentException ex) {
            // pass...
            }
            assertNull(region.getEntry(key));
            try {
                assertEquals(value, region.get(key, arg));
            } catch (IllegalArgumentException e) {
            }
        }
    });
    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) 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 44 with LoaderHelper

use of org.apache.geode.cache.LoaderHelper 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 45 with LoaderHelper

use of org.apache.geode.cache.LoaderHelper 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)

Aggregations

LoaderHelper (org.apache.geode.cache.LoaderHelper)56 Test (org.junit.Test)47 Region (org.apache.geode.cache.Region)45 AttributesFactory (org.apache.geode.cache.AttributesFactory)33 CacheException (org.apache.geode.cache.CacheException)32 CacheLoader (org.apache.geode.cache.CacheLoader)32 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)32 VM (org.apache.geode.test.dunit.VM)29 Host (org.apache.geode.test.dunit.Host)26 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)26 LocalRegion (org.apache.geode.internal.cache.LocalRegion)22 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)19 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)18 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)15 TimeoutException (org.apache.geode.cache.TimeoutException)13 StoredObject (org.apache.geode.internal.offheap.StoredObject)13 CacheWriterException (org.apache.geode.cache.CacheWriterException)11 EntryEvent (org.apache.geode.cache.EntryEvent)10 RegionAttributes (org.apache.geode.cache.RegionAttributes)9 AttributesMutator (org.apache.geode.cache.AttributesMutator)7