Search in sources :

Example 1 with CacheLoader

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

the class SearchAndLoadDUnitTest method testLocalLoad.

@Test
public void testLocalLoad() throws CacheException, InterruptedException {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    final String name = this.getUniqueName() + "-ACK";
    final String objectName = "C";
    final Integer value = new Integer(44);
    remoteLoaderInvoked = false;
    loaderInvoked = false;
    vm0.invoke(new SerializableRunnable("Create ACK Region") {

        public void run() {
            remoteLoaderInvoked = false;
            loaderInvoked = false;
            try {
                AttributesFactory factory = new AttributesFactory();
                factory.setScope(Scope.DISTRIBUTED_ACK);
                factory.setEarlyAck(false);
                factory.setCacheLoader(new CacheLoader() {

                    public Object load(LoaderHelper helper) {
                        loaderInvoked = true;
                        return value;
                    }

                    public void close() {
                    }
                });
                Region region = createRegion(name, factory.create());
                region.create(objectName, null);
            } catch (CacheException ex) {
                Assert.fail("While creating ACK region", ex);
            }
        }
    });
    vm1.invoke(new SerializableRunnable("Create ACK Region") {

        public void run() {
            remoteLoaderInvoked = false;
            loaderInvoked = false;
            try {
                AttributesFactory factory = new AttributesFactory();
                factory.setScope(Scope.DISTRIBUTED_ACK);
                factory.setEarlyAck(false);
                factory.setCacheLoader(new CacheLoader() {

                    public Object load(LoaderHelper helper) {
                        remoteLoaderInvoked = true;
                        return value;
                    }

                    public void close() {
                    }
                });
                createRegion(name, factory.create());
            } catch (CacheException ex) {
                Assert.fail("While creating ACK region", ex);
            }
        }
    });
    vm0.invoke(new SerializableRunnable("Get a value from local loader") {

        public void run() {
            try {
                Object result = getRootRegion().getSubregion(name).get(objectName);
                assertEquals(value, result);
                assertEquals(new Boolean(loaderInvoked), Boolean.TRUE);
                assertEquals(new Boolean(remoteLoaderInvoked), Boolean.FALSE);
            } catch (CacheLoaderException cle) {
            } catch (TimeoutException te) {
            }
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) 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) Region(org.apache.geode.cache.Region) CacheLoader(org.apache.geode.cache.CacheLoader) TimeoutException(org.apache.geode.cache.TimeoutException) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 2 with CacheLoader

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

the class SearchAndLoadDUnitTest method testEmptyNetLoad.

/**
   * Confirm that a netLoad that returns null will NOT allow other netLoad methods to be called.
   */
@Test
public void testEmptyNetLoad() throws CacheException, InterruptedException {
    disconnectAllFromDS();
    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() + "-ACK";
    final String objectName = "B";
    loaderInvoked = false;
    remoteLoaderInvoked = false;
    remoteLoaderInvokedCount = 0;
    vm0.invoke(new SerializableRunnable("Create ACK Region") {

        public void run() {
            loaderInvoked = false;
            remoteLoaderInvoked = false;
            remoteLoaderInvokedCount = 0;
            try {
                AttributesFactory factory = new AttributesFactory();
                factory.setScope(Scope.DISTRIBUTED_ACK);
                factory.setEarlyAck(false);
                // factory.setCacheLoader(new CacheLoader() {
                // public Object load(LoaderHelper helper) {
                /// loaderInvoked = true;
                // return value;
                // }
                //
                // public void close() {
                //
                // }
                // });
                Region region = createRegion(name, factory.create());
                region.create(objectName, null);
            } catch (CacheException ex) {
                Assert.fail("While creating ACK region", ex);
            }
        }
    });
    SerializableRunnable installLoader = new SerializableRunnable("Create ACK Region") {

        public void run() {
            loaderInvoked = false;
            remoteLoaderInvoked = false;
            remoteLoaderInvokedCount = 0;
            try {
                AttributesFactory factory = new AttributesFactory();
                factory.setScope(Scope.DISTRIBUTED_ACK);
                factory.setEarlyAck(false);
                factory.setCacheLoader(new CacheLoader() {

                    public Object load(LoaderHelper helper) {
                        remoteLoaderInvoked = true;
                        remoteLoaderInvokedCount++;
                        return null;
                    }

                    public void close() {
                    }
                });
                createRegion(name, factory.create());
            } catch (CacheException ex) {
                Assert.fail("While creating ACK region", ex);
            }
        }
    };
    vm1.invoke(installLoader);
    vm2.invoke(installLoader);
    vm0.invoke(new SerializableRunnable("Get a value from remote loader") {

        public void run() {
            for (int i = 0; i < 1; i++) {
                try {
                    Object result = getRootRegion().getSubregion(name).get(objectName);
                    assertEquals(null, result);
                    assertEquals(false, loaderInvoked);
                // getRootRegion().getSubregion(name).invalidate(objectName);
                } catch (CacheLoaderException cle) {
                    Assert.fail("While getting value for ACK region", cle);
                }/*
           * catch(EntryNotFoundException enfe) { fail("While getting value for ACK region", enfe);
           * 
           * }
           */
                 catch (TimeoutException te) {
                    Assert.fail("While getting value for ACK region", te);
                }
            }
        }
    });
    // we only invoke one netLoad loader even when they return null.
    boolean xor = vmRemoteLoaderInvoked(vm1) ^ vmRemoteLoaderInvoked(vm2);
    assertEquals("vm1=" + vmRemoteLoaderInvoked(vm1) + " vm2=" + vmRemoteLoaderInvoked(vm2) + " vm1Count=" + vmRemoteLoaderInvokedCount(vm1) + " vm2Count=" + vmRemoteLoaderInvokedCount(vm2), true, xor);
    int total = vmRemoteLoaderInvokedCount(vm1) + vmRemoteLoaderInvokedCount(vm2);
    assertEquals("vm1=" + vmRemoteLoaderInvokedCount(vm1) + " vm2=" + vmRemoteLoaderInvokedCount(vm2), 1, total);
}
Also used : CacheException(org.apache.geode.cache.CacheException) 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) Region(org.apache.geode.cache.Region) CacheLoader(org.apache.geode.cache.CacheLoader) TimeoutException(org.apache.geode.cache.TimeoutException) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 3 with CacheLoader

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

the class GlobalLockingDUnitTest method testLoadLockTimeout.

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

        public void run2() throws CacheException {
            Region r = getOrCreateRootRegion().createSubregion(name, getGlobalAttrs());
            Lock lock = r.getDistributedLock(key);
            lock.lock();
        }
    });
    // In second VM, do a get that tries to invoke a loader
    vm1.invoke(new CacheSerializableRunnable("Lock timeout local loader") {

        public void run2() throws CacheException {
            getOrCreateRootRegion().getCache().setLockTimeout(2);
            Region r = getOrCreateRootRegion().createSubregion(name, getGlobalAttrs());
            r.getAttributesMutator().setCacheLoader(new CacheLoader() {

                public Object load(LoaderHelper helper) throws CacheLoaderException {
                    throw new CacheLoaderException("Made it into the loader!");
                }

                public void close() {
                }
            });
            try {
                r.get(key);
                fail("get() should have thrown TimeoutException");
            } catch (TimeoutException ex) {
            // pass
            }
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) Host(org.apache.geode.test.dunit.Host) Lock(java.util.concurrent.locks.Lock) LoaderHelper(org.apache.geode.cache.LoaderHelper) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) CacheLoader(org.apache.geode.cache.CacheLoader) TimeoutException(org.apache.geode.cache.TimeoutException) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) DLockTest(org.apache.geode.test.junit.categories.DLockTest)

Example 4 with CacheLoader

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

the class RegionReliabilityTestCase method testFullAccess.

/**
   * Tests affect of FULL_ACCESS on region operations.
   */
@Test
public void testFullAccess() throws Exception {
    final String name = this.getUniqueName();
    final String roleA = name + "-A";
    // assign names to 4 vms...
    final String[] requiredRoles = { roleA };
    Set requiredRolesSet = new HashSet();
    for (int i = 0; i < requiredRoles.length; i++) {
        requiredRolesSet.add(InternalRole.getRole(requiredRoles[i]));
    }
    assertEquals(requiredRoles.length, requiredRolesSet.size());
    // connect controller to system...
    Properties config = new Properties();
    config.setProperty(ROLES, "");
    getSystem(config);
    getCache();
    // create region in controller...
    MembershipAttributes ra = new MembershipAttributes(requiredRoles, LossAction.FULL_ACCESS, ResumptionAction.NONE);
    AttributesFactory fac = new AttributesFactory();
    fac.setMembershipAttributes(ra);
    fac.setScope(getRegionScope());
    fac.setStatisticsEnabled(true);
    RegionAttributes attr = fac.create();
    Region region = createRootRegion(name, attr);
    // wait for memberTimeout to expire
    waitForMemberTimeout();
    // use vm0 for netsearch and netload
    Host.getHost(0).getVM(0).invoke(new CacheSerializableRunnable("Create Region") {

        public void run2() throws CacheException {
            createConnection(null);
            AttributesFactory fac = new AttributesFactory();
            fac.setScope(getRegionScope());
            fac.setCacheLoader(new CacheLoader() {

                public Object load(LoaderHelper helper) throws CacheLoaderException {
                    if ("netload".equals(helper.getKey())) {
                        return "netload";
                    } else {
                        return null;
                    }
                }

                public void close() {
                }
            });
            RegionAttributes attr = fac.create();
            Region region = createRootRegion(name, attr);
            Object netsearch = "netsearch";
            region.put(netsearch, netsearch);
        }
    });
    // test ops on Region that should not throw
    assertNoAccessDoesNotThrow(region);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) RegionAttributes(org.apache.geode.cache.RegionAttributes) CacheException(org.apache.geode.cache.CacheException) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) AbstractRegion(org.apache.geode.internal.cache.AbstractRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) CacheLoader(org.apache.geode.cache.CacheLoader) HashSet(java.util.HashSet) MembershipAttributes(org.apache.geode.cache.MembershipAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 5 with CacheLoader

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

the class PartitionedRegionAPIDUnitTest method testCacherLoaderHelper.

@Test
public void testCacherLoaderHelper() throws Exception {
    final String rName = getUniqueName();
    Host host = Host.getHost(0);
    VM vm2 = host.getVM(2);
    VM vm3 = host.getVM(3);
    final int localMaxMemory = 10;
    final String key1 = "key1";
    final String arg = "loaderArg";
    CacheSerializableRunnable createLoaderPR = new CacheSerializableRunnable("createLoaderPR") {

        public void run2() throws CacheException {
            getCache();
            CacheLoader cl = new TestCacheLoader() {

                public Object load2(LoaderHelper helper) throws CacheLoaderException {
                    assertNotNull(helper);
                    assertEquals(key1, helper.getKey());
                    assertEquals(rName, helper.getRegion().getName());
                    assertEquals(arg, helper.getArgument());
                    return helper.getArgument();
                }
            };
            PartitionedRegion pr = (PartitionedRegion) new RegionFactory().setCacheLoader(cl).setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(1).setLocalMaxMemory(localMaxMemory).create()).create(rName);
            assertSame(cl, pr.getDataStore().getCacheLoader());
        }
    };
    vm2.invoke(createLoaderPR);
    vm3.invoke(createLoaderPR);
    // create a "pure" accessor, no data storage
    getCache();
    Region pr = new RegionFactory().setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(1).setLocalMaxMemory(0).create()).create(rName);
    assertEquals(arg, pr.get(key1, arg));
}
Also used : LoaderHelper(org.apache.geode.cache.LoaderHelper) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) RegionFactory(org.apache.geode.cache.RegionFactory) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Host(org.apache.geode.test.dunit.Host) TestCacheLoader(org.apache.geode.cache30.TestCacheLoader) CacheLoader(org.apache.geode.cache.CacheLoader) TestCacheLoader(org.apache.geode.cache30.TestCacheLoader) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

CacheLoader (org.apache.geode.cache.CacheLoader)40 LoaderHelper (org.apache.geode.cache.LoaderHelper)32 Region (org.apache.geode.cache.Region)24 Test (org.junit.Test)24 AttributesFactory (org.apache.geode.cache.AttributesFactory)21 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)20 CacheException (org.apache.geode.cache.CacheException)18 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)17 VM (org.apache.geode.test.dunit.VM)16 Host (org.apache.geode.test.dunit.Host)13 CacheWriterException (org.apache.geode.cache.CacheWriterException)9 LocalRegion (org.apache.geode.internal.cache.LocalRegion)9 AttributesMutator (org.apache.geode.cache.AttributesMutator)8 RegionAttributes (org.apache.geode.cache.RegionAttributes)8 TimeoutException (org.apache.geode.cache.TimeoutException)7 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)7 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)7 HashSet (java.util.HashSet)6 Set (java.util.Set)6 Properties (java.util.Properties)5