Search in sources :

Example 51 with LoaderHelper

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

the class MultiVMRegionTestCase method testRemoteCacheLoader.

/**
   * Tests that a {@link CacheLoader} is invoked in a remote VM. This essentially tests
   * <code>netLoad</code>.
   */
@Test
public void testRemoteCacheLoader() throws Exception {
    assumeTrue(supportsNetLoad());
    assertTrue(getRegionAttributes().getScope().isDistributed());
    final String name = this.getUniqueName();
    final Object key = "KEY";
    final Object value = "VALUE";
    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());
                    return value;
                }
            };
            region.getAttributesMutator().setCacheLoader(loader);
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Remote load") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            assertEquals(value, region.get(key));
        }
    });
    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 52 with LoaderHelper

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

Example 53 with LoaderHelper

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

the class MultiVMRegionTestCase method testNoLoaderWithInvalidEntry.

/**
   * Tests that a remote <code>CacheLoader</code> is not invoked if the remote region has an invalid
   * entry (that is, a key, but no value).
   */
@Test
public void testNoLoaderWithInvalidEntry() throws Exception {
    assumeTrue(supportsNetLoad());
    final String name = this.getUniqueName();
    final Object key = "KEY";
    final Object value = "VALUE";
    SerializableRunnable create = new CacheSerializableRunnable("Create Region") {

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

                @Override
                public Object load2(LoaderHelper helper) throws CacheLoaderException {
                    return value;
                }
            };
            region.getAttributesMutator().setCacheLoader(loader);
        }
    };
    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("Create invalid entry") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            region.create(key, null);
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Remote get") {

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

        @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 54 with LoaderHelper

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

the class MultiVMRegionTestCase method versionTestConcurrentEventsOnNonReplicatedRegion.

/**
   * This tests the concurrency versioning system to ensure that event conflation happens correctly
   * and that the statistic is being updated properly
   */
public void versionTestConcurrentEventsOnNonReplicatedRegion() {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    VM vm2 = host.getVM(2);
    // this VM, but treat as a remote for uniformity
    VM vm3 = host.getVM(3);
    final boolean noAck = !getRegionAttributes().getScope().isAck();
    // create an empty region in vm0 and replicated regions in VM 1 and 3,
    // then perform concurrent ops
    // on the same key while creating the region in VM2. Afterward make
    // sure that all three regions are consistent
    final String name = this.getUniqueName() + "-CC";
    SerializableRunnable createRegion = new SerializableRunnable("Create Region") {

        @Override
        public void run() {
            try {
                final RegionFactory f;
                if (VM.getCurrentVMNum() == 0) {
                    f = getCache().createRegionFactory(getRegionAttributes(RegionShortcut.LOCAL.toString()));
                    f.setScope(getRegionAttributes().getScope());
                } else {
                    f = getCache().createRegionFactory(getRegionAttributes());
                }
                CCRegion = (LocalRegion) f.create(name);
            } catch (CacheException ex) {
                fail("While creating region", ex);
            }
        }
    };
    vm0.invoke(createRegion);
    vm1.invoke(createRegion);
    vm3.invoke(createRegion);
    SerializableRunnable performOps = new SerializableRunnable("perform concurrent ops") {

        @Override
        public void run() {
            try {
                doOpsLoop(5000, false);
                sendSerialMessageToAll();
                if (CCRegion.getAttributes().getDataPolicy().withReplication()) {
                    long events = CCRegion.getCachePerfStats().getConflatedEventsCount();
                    assertTrue("expected some event conflation", events > 0);
                }
            } catch (CacheException e) {
                fail("while performing concurrent operations", e);
            }
        }
    };
    AsyncInvocation a0 = vm0.invokeAsync(performOps);
    AsyncInvocation a1 = vm1.invokeAsync(performOps);
    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        fail("sleep was interrupted");
    }
    vm2.invoke(createRegion);
    boolean a0failed = waitForAsyncProcessing(a0, "expected some event conflation");
    boolean a1failed = waitForAsyncProcessing(a1, "expected some event conflation");
    if (a0failed && a1failed) {
        fail("neither member saw event conflation - check stats for " + name);
    }
    // check consistency of the regions
    Map r0Contents = (Map) vm0.invoke(() -> this.getCCRegionContents());
    Map r1Contents = (Map) vm1.invoke(() -> this.getCCRegionContents());
    Map r2Contents = (Map) vm2.invoke(() -> this.getCCRegionContents());
    Map r3Contents = (Map) vm3.invoke(() -> this.getCCRegionContents());
    for (int i = 0; i < 10; i++) {
        String key = "cckey" + i;
        assertEquals("region contents are not consistent", r1Contents.get(key), r2Contents.get(key));
        assertEquals("region contents are not consistent", r2Contents.get(key), r3Contents.get(key));
        for (int subi = 1; subi < 3; subi++) {
            String subkey = key + "-" + subi;
            if (r1Contents.containsKey(subkey)) {
                assertEquals("region contents are not consistent", r1Contents.get(subkey), r2Contents.get(subkey));
                assertEquals("region contents are not consistent", r2Contents.get(subkey), r3Contents.get(subkey));
                if (r0Contents.containsKey(subkey)) {
                    assertEquals("region contents are not consistent", r1Contents.get(subkey), r0Contents.get(subkey));
                }
            } else {
                assertTrue(!r2Contents.containsKey(subkey));
                assertTrue(!r3Contents.containsKey(subkey));
                assertTrue(!r0Contents.containsKey(subkey));
            }
        }
    }
    // a non-replicate region should not carry tombstones, so we'll check for that.
    // then we'll use a cache loader and make sure that 1-hop is used to put the
    // entry into the cache in a replicate. The new entry should have a version
    // of 1 when it's first created, the tombstone should have version 2 and
    // the loaded value should have version 3
    final String loadKey = "loadKey";
    vm0.invoke(new SerializableRunnable("add cache loader and create destroyed entry") {

        @Override
        public void run() {
            CCRegion.getAttributesMutator().setCacheLoader(new CacheLoader() {

                @Override
                public void close() {
                }

                @Override
                public Object load(LoaderHelper helper) throws CacheLoaderException {
                    org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("The test CacheLoader has been invoked for key '" + helper.getKey() + "'");
                    return "loadedValue";
                }
            });
            CCRegion.put(loadKey, "willbeLoadedInitialValue");
            CCRegion.destroy(loadKey);
            if (noAck) {
                // flush for validation to work
                sendSerialMessageToAll();
            }
        // this assertion guarantees that non-replicated regions do not create tombstones.
        // this is currently not the case but is an open issue
        // assertTrue(CCRegion.getRegionEntry(loadKey) == null);
        }
    });
    vm1.invoke(new SerializableRunnable("confirm tombstone") {

        @Override
        public void run() {
            assertTrue(Token.TOMBSTONE == CCRegion.getRegionEntry(loadKey).getValueInVM(CCRegion));
        }
    });
    vm0.invoke(new SerializableRunnable("use cache loader") {

        @Override
        public void run() {
            assertEquals("loadedValue", CCRegion.get(loadKey));
            assertEquals(3, (CCRegion.getRegionEntry(loadKey)).getVersionStamp().getEntryVersion());
        }
    });
    if (!noAck) {
        // might be 3 or 4 with no-ack
        vm1.invoke(new SerializableRunnable("verify version number") {

            @Override
            public void run() {
                assertEquals("loadedValue", CCRegion.get(loadKey));
                assertEquals(3, (CCRegion.getRegionEntry(loadKey)).getVersionStamp().getEntryVersion());
            }
        });
    }
}
Also used : CacheException(org.apache.geode.cache.CacheException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) LoaderHelper(org.apache.geode.cache.LoaderHelper) RegionFactory(org.apache.geode.cache.RegionFactory) VM(org.apache.geode.test.dunit.VM) CacheLoader(org.apache.geode.cache.CacheLoader) Map(java.util.Map) HashMap(java.util.HashMap)

Example 55 with LoaderHelper

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

the class ProxyDUnitTest method distributedOps.

////////////////////// Test Methods //////////////////////
/**
   * check distributed ops that originate in a PROXY are correctly distributed to non-proxy regions.
   */
private void distributedOps(DataPolicy dp, InterestPolicy ip) throws CacheException {
    initOtherId();
    AttributesFactory af = new AttributesFactory();
    af.setDataPolicy(dp);
    af.setSubscriptionAttributes(new SubscriptionAttributes(ip));
    af.setScope(Scope.DISTRIBUTED_ACK);
    Region r = createRootRegion("ProxyDUnitTest", af.create());
    doCreateOtherVm();
    r.put("putkey", "putvalue1");
    getOtherVm().invoke(new CacheSerializableRunnable("check put") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(true, r.containsKey("putkey"));
            assertEquals("putvalue1", r.getEntry("putkey").getValue());
            r.put("putkey", "putvalue2");
        }
    });
    assertEquals(false, r.containsKey("putkey"));
    // netsearch
    assertEquals("putvalue2", r.get("putkey"));
    r.invalidate("putkey");
    getOtherVm().invoke(new CacheSerializableRunnable("check invalidate") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(true, r.containsKey("putkey"));
            assertEquals(null, r.getEntry("putkey").getValue());
        }
    });
    // invalid so total miss
    assertEquals(null, r.get("putkey"));
    r.destroy("putkey");
    getOtherVm().invoke(new CacheSerializableRunnable("check destroy") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(false, r.containsKey("putkey"));
        }
    });
    // total miss
    assertEquals(null, r.get("putkey"));
    r.create("createKey", "createValue1");
    getOtherVm().invoke(new CacheSerializableRunnable("check create") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(true, r.containsKey("createKey"));
            assertEquals("createValue1", r.getEntry("createKey").getValue());
        }
    });
    {
        Map m = new HashMap();
        m.put("putAllKey1", "putAllValue1");
        m.put("putAllKey2", "putAllValue2");
        r.putAll(m, "putAllCallback");
    }
    getOtherVm().invoke(new CacheSerializableRunnable("check putAll") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(true, r.containsKey("putAllKey1"));
            assertEquals("putAllValue1", r.getEntry("putAllKey1").getValue());
            assertEquals(true, r.containsKey("putAllKey2"));
            assertEquals("putAllValue2", r.getEntry("putAllKey2").getValue());
        }
    });
    r.clear();
    getOtherVm().invoke(new CacheSerializableRunnable("check clear") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(0, r.size());
        }
    });
    getOtherVm().invoke(new CacheSerializableRunnable("install CacheWriter") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            AttributesMutator am = r.getAttributesMutator();
            CacheWriter cw = new CacheWriterAdapter() {

                public void beforeCreate(EntryEvent event) throws CacheWriterException {
                    throw new CacheWriterException("expected");
                }
            };
            am.setCacheWriter(cw);
        }
    });
    try {
        r.put("putkey", "putvalue");
        fail("expected CacheWriterException");
    } catch (CacheWriterException expected) {
    }
    getOtherVm().invoke(new CacheSerializableRunnable("check clear") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(0, r.size());
        }
    });
    // total miss
    assertEquals(null, r.get("loadkey"));
    getOtherVm().invoke(new CacheSerializableRunnable("install CacheLoader") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            AttributesMutator am = r.getAttributesMutator();
            // clear csche writer
            am.setCacheWriter(null);
            CacheLoader cl = new CacheLoader() {

                public Object load(LoaderHelper helper) throws CacheLoaderException {
                    if (helper.getKey().equals("loadkey")) {
                        return "loadvalue";
                    } else if (helper.getKey().equals("loadexception")) {
                        throw new CacheLoaderException("expected");
                    } else {
                        return null;
                    }
                }

                public void close() {
                }
            };
            am.setCacheLoader(cl);
        }
    });
    // net load
    assertEquals("loadvalue", r.get("loadkey"));
    // total miss
    assertEquals(null, r.get("foobar"));
    try {
        r.get("loadexception");
        fail("expected CacheLoaderException");
    } catch (CacheLoaderException expected) {
    }
    r.destroyRegion();
    getOtherVm().invoke(new CacheSerializableRunnable("check clear") {

        public void run2() throws CacheException {
            Region r = getRootRegion("ProxyDUnitTest");
            assertEquals(null, r);
        }
    });
}
Also used : CacheWriterAdapter(org.apache.geode.cache.util.CacheWriterAdapter) CacheException(org.apache.geode.cache.CacheException) HashMap(java.util.HashMap) LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) EntryEvent(org.apache.geode.cache.EntryEvent) CacheWriter(org.apache.geode.cache.CacheWriter) Region(org.apache.geode.cache.Region) CacheLoader(org.apache.geode.cache.CacheLoader) HashMap(java.util.HashMap) Map(java.util.Map) SubscriptionAttributes(org.apache.geode.cache.SubscriptionAttributes) AttributesMutator(org.apache.geode.cache.AttributesMutator) CacheWriterException(org.apache.geode.cache.CacheWriterException)

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