Search in sources :

Example 31 with CacheLoader

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

the class SearchLoadAndWriteProcessor method searchAndLoad.

/**
   * If we have a local cache loader and the region is not global, then invoke the loader If the
   * region is local, or the result is non-null, then return whatever the loader returned do a
   * netSearch amongst selected peers if netSearch returns a blob, deserialize the blob and return
   * that as the result netSearch failed, so all we can do at this point is do a load return result
   * from load
   */
private void searchAndLoad(EntryEventImpl event, TXStateInterface txState, Object localValue) throws CacheLoaderException, TimeoutException {
    RegionAttributes attrs = region.getAttributes();
    Scope scope = attrs.getScope();
    DataPolicy dataPolicy = attrs.getDataPolicy();
    if (txState != null) {
        TXEntryState tx = txState.txReadEntry(event.getKeyInfo(), region, false, true);
        if (tx != null) {
            if (tx.noValueInSystem()) {
                // If the tx view has it invalid or destroyed everywhere
                // then don't do a netsearch. We want to see the
                // transactional view.
                load(event);
                return;
            }
        }
    }
    // if mirrored then we can optimize by skipping netsearch in some cases,
    // and if also skip netSearch if we find an INVALID token since we
    // know it was distributed. (Otherwise it would be a LOCAL_INVALID token)
    {
        if (localValue == Token.INVALID || dataPolicy.withReplication()) {
            load(event);
            return;
        }
    }
    Object obj = null;
    if (!scope.isGlobal()) {
        // copy into local var to prevent race condition
        CacheLoader loader = ((AbstractRegion) region).basicGetLoader();
        if (loader != null) {
            obj = doLocalLoad(loader, true);
            Assert.assertTrue(obj != Token.INVALID && obj != Token.LOCAL_INVALID);
            event.setNewValue(obj);
            this.isSerialized = false;
            this.result = obj;
            return;
        }
        if (scope.isLocal()) {
            return;
        }
    }
    netSearchForBlob();
    if (this.result != null) {
        Assert.assertTrue(this.result != Token.INVALID && this.result != Token.LOCAL_INVALID);
        if (this.isSerialized) {
            event.setSerializedNewValue((byte[]) this.result);
        } else {
            event.setNewValue(this.result);
        }
        event.setVersionTag(this.versionTag);
        return;
    }
    load(event);
}
Also used : Scope(org.apache.geode.cache.Scope) RegionAttributes(org.apache.geode.cache.RegionAttributes) CacheLoader(org.apache.geode.cache.CacheLoader) DataPolicy(org.apache.geode.cache.DataPolicy)

Example 32 with CacheLoader

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

the class AbstractRegion method setCacheLoader.

@Override
public synchronized CacheLoader setCacheLoader(CacheLoader cacheLoader) {
    checkReadiness();
    CacheLoader oldLoader = this.cacheLoader;
    assignCacheLoader(cacheLoader);
    cacheLoaderChanged(oldLoader);
    return oldLoader;
}
Also used : CacheLoader(org.apache.geode.cache.CacheLoader)

Example 33 with CacheLoader

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

the class DiskRegionDUnitTest method testLRUCCSizeOne.

/**
   * Tests a disk-based region with an {@link LRUCapacityController} with size 1 and an eviction
   * action of "overflow".
   */
@Test
public void testLRUCCSizeOne() throws CacheException {
    int threshold = 1;
    final String name = this.getUniqueName();
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.LOCAL);
    factory.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(threshold, EvictionAction.OVERFLOW_TO_DISK));
    factory.setCacheLoader(new CacheLoader() {

        public Object load(LoaderHelper helper) throws CacheLoaderException {
            return "LOADED VALUE";
        }

        public void close() {
        }
    });
    DiskStoreFactory dsf = getCache().createDiskStoreFactory();
    factory.setDiskSynchronous(true);
    File d = new File("DiskRegions" + OSProcess.getId());
    d.mkdirs();
    dsf.setDiskDirs(new File[] { d });
    DiskStore ds = dsf.create(name);
    factory.setDiskStoreName(ds.getName());
    Region region = createRegion(name, factory.create());
    LRUStatistics lruStats = getLRUStats(region);
    assertNotNull(lruStats);
    for (int i = 1; i <= 1; i++) {
        Object key = new Integer(i);
        Object value = String.valueOf(i);
        region.put(key, value);
        assertEquals(1, lruStats.getCounter());
        assertEquals(0, lruStats.getEvictions());
    }
    for (int i = 2; i <= 10; i++) {
        Object key = new Integer(i);
        Object value = String.valueOf(i);
        region.put(key, value);
        assertEquals(1, lruStats.getCounter());
        assertEquals(i - 1, lruStats.getEvictions());
    }
    for (int i = 11; i <= 20; i++) {
        Object key = new Integer(i);
        // Object value = String.valueOf(i);
        // Invoke loader
        region.get(key);
        assertEquals(1, lruStats.getCounter());
        assertEquals(i - 1, lruStats.getEvictions());
    }
}
Also used : DiskStoreFactory(org.apache.geode.cache.DiskStoreFactory) LoaderHelper(org.apache.geode.cache.LoaderHelper) DiskStore(org.apache.geode.cache.DiskStore) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) LRUStatistics(org.apache.geode.internal.cache.lru.LRUStatistics) DiskRegion(org.apache.geode.internal.cache.DiskRegion) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) CacheLoader(org.apache.geode.cache.CacheLoader) File(java.io.File) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 34 with CacheLoader

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

the class TXDistributedDUnitTest method testDACKLoadedMessage.

/**
   * Test distributed ack transactions that consist only of data from loaded values
   */
@Test
public void testDACKLoadedMessage() throws Exception {
    final CacheTransactionManager txMgr = this.getCache().getCacheTransactionManager();
    final String rgnName = getUniqueName();
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_ACK);
    factory.setEarlyAck(false);
    factory.setCacheLoader(new CacheLoader() {

        public Object load(LoaderHelper helper) {
            return "val" + helper.getArgument();
        }

        public void close() {
        }
    });
    Region rgn = getCache().createRegion(rgnName, factory.create());
    Invoke.invokeInEveryVM(new SerializableRunnable("testDACKLoadedMessage: intial configuration") {

        public void run() {
            try {
                AttributesFactory factory2 = new AttributesFactory();
                factory2.setScope(Scope.DISTRIBUTED_ACK);
                factory2.setEarlyAck(false);
                // factory.setDataPolicy(DataPolicy.REPLICATE);
                factory2.setMirrorType(MirrorType.KEYS);
                getCache().createRegion(rgnName, factory2.create());
            } catch (CacheException e) {
                Assert.fail("While creating region", e);
            }
        }
    });
    // Confirm the standard case
    txMgr.begin();
    rgn.put("key1", "val1");
    txMgr.commit();
    assertEquals("val1", rgn.getEntry("key1").getValue());
    Invoke.invokeInEveryVM(new SerializableRunnable("testDACKLoadedMessage: confirm standard case") {

        public void run() {
            Region rgn1 = getCache().getRegion(rgnName);
            assertEquals("val1", rgn1.getEntry("key1").getValue());
        }
    });
    // Confirm loaded value case
    txMgr.begin();
    rgn.get("key2", new Integer(2));
    txMgr.commit();
    assertEquals("val2", rgn.getEntry("key2").getValue());
    Invoke.invokeInEveryVM(new SerializableRunnable("testDACKLoadedMessage: confirm standard case") {

        public void run() {
            Region rgn1 = getCache().getRegion(rgnName);
            assertEquals("val2", rgn1.getEntry("key2").getValue());
        }
    });
    // This should use the ack w/ the lockid
    txMgr.begin();
    rgn.put("key3", "val3");
    rgn.get("key4", new Integer(4));
    txMgr.commit();
    Invoke.invokeInEveryVM(new SerializableRunnable("testDACKLoadedMessage: confirm standard case") {

        public void run() {
            Region rgn1 = getCache().getRegion(rgnName);
            assertEquals("val3", rgn1.getEntry("key3").getValue());
            assertEquals("val4", rgn1.getEntry("key4").getValue());
        }
    });
}
Also used : LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheException(org.apache.geode.cache.CacheException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) CacheLoader(org.apache.geode.cache.CacheLoader) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 35 with CacheLoader

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

the class SearchAndLoadDUnitTest method testNetLoad.

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

        public void run() {
            try {
                loaderInvoked = false;
                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() {
            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 remote loader") {

        public void run() {
            for (int i = 0; i < 1; i++) {
                try {
                    Object result = getRootRegion().getSubregion(name).get(objectName);
                    assertEquals(value, result);
                    assertEquals(new Boolean(loaderInvoked), Boolean.FALSE);
                // 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);
                }
            }
        }
    });
}
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)

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