Search in sources :

Example 21 with LoaderHelper

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

the class GemcachedBinaryClientJUnitTest method testCacheLoaderException.

@SuppressWarnings("unchecked")
public void testCacheLoaderException() throws Exception {
    MemcachedClient client = createMemcachedClient();
    assertTrue(client.set("key", 0, "value").get());
    GemFireCacheImpl cache = GemFireCacheImpl.getInstance();
    Region region = cache.getRegion(GemFireMemcachedServer.REGION_NAME);
    region.getAttributesMutator().setCacheLoader(new CacheLoader() {

        @Override
        public void close() {
        }

        @Override
        public Object load(LoaderHelper helper) throws CacheLoaderException {
            if (helper.getKey().equals(KeyWrapper.getWrappedKey("exceptionkey".getBytes()))) {
                throw new RuntimeException("ExpectedStrings: Cache loader exception");
            }
            return null;
        }
    });
    long start = System.nanoTime();
    try {
        client.get("exceptionkey");
        throw new RuntimeException("expected exception not thrown");
    } catch (Exception e) {
    // expected
    }
    assertEquals("value", client.get("key"));
}
Also used : LoaderHelper(org.apache.geode.cache.LoaderHelper) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) MemcachedClient(net.spy.memcached.MemcachedClient) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) Region(org.apache.geode.cache.Region) CacheLoader(org.apache.geode.cache.CacheLoader) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) CacheWriterException(org.apache.geode.cache.CacheWriterException)

Example 22 with LoaderHelper

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

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

the class GlobalRegionDUnitTest method testPutGetTimeout.

/**
   * Tests that {@link Region#put} and {@link Region#get} timeout when another VM holds the
   * distributed lock on the entry in question.
   */
@Test
public void testPutGetTimeout() {
    assertEquals(Scope.GLOBAL, getRegionAttributes().getScope());
    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 Region") {

        public void run2() throws CacheException {
            createRegion(name);
        }
    };
    vm0.invoke(create);
    vm1.invoke(create);
    vm0.invoke(new CacheSerializableRunnable("Lock entry") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            Lock lock = region.getDistributedLock(key);
            lock.lock();
        }
    });
    vm1.invoke(new CacheSerializableRunnable("Attempt get/put") {

        public void run2() throws CacheException {
            Cache cache = getCache();
            cache.setLockTimeout(1);
            cache.setSearchTimeout(1);
            Region region = getRootRegion().getSubregion(name);
            try {
                region.put(key, value);
                fail("Should have thrown a TimeoutException on put");
            } catch (TimeoutException ex) {
            // pass..
            }
            // With a loader, should try to lock and time out
            region.getAttributesMutator().setCacheLoader(new TestCacheLoader() {

                public Object load2(LoaderHelper helper) {
                    return null;
                }
            });
            try {
                region.get(key);
                fail("Should have thrown a TimeoutException on get");
            } catch (TimeoutException ex) {
            // pass..
            }
            // Without a loader, should succeed
            region.getAttributesMutator().setCacheLoader(null);
            region.get(key);
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Unlock entry") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            Lock lock = region.getDistributedLock(key);
            lock.unlock();
        }
    });
}
Also used : CacheException(org.apache.geode.cache.CacheException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) Lock(java.util.concurrent.locks.Lock) LoaderHelper(org.apache.geode.cache.LoaderHelper) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache) TimeoutException(org.apache.geode.cache.TimeoutException) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 24 with LoaderHelper

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

the class LRUEvictionControllerDUnitTest method testCacheLoader.

/**
   * Carefully verifies that region operations effect the {@link LRUStatistics} as expected in the
   * presense of a {@link CacheLoader}.
   */
@Test
public void testCacheLoader() throws CacheException {
    int threshold = 10;
    final String name = this.getUniqueName();
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.LOCAL);
    factory.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(threshold));
    factory.setCacheLoader(new CacheLoader() {

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

        public void close() {
        }
    });
    Region region;
    if (usingMain) {
        DistributedSystem system = DistributedSystem.connect(new Properties());
        Cache cache = CacheFactory.create(system);
        region = cache.createRegion("Test", factory.create());
    } else {
        region = createRegion(name, factory.create());
    }
    LRUStatistics lruStats = getLRUStats(region);
    assertNotNull(lruStats);
    for (int i = 1; i <= 10; i++) {
        Object key = new Integer(i);
        Object value = String.valueOf(i);
        region.put(key, value);
        assertEquals(i, lruStats.getCounter());
        assertEquals(0, 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(10, lruStats.getCounter());
        assertEquals(i - 10, lruStats.getEvictions());
    }
}
Also used : Properties(java.util.Properties) DistributedSystem(org.apache.geode.distributed.DistributedSystem) LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) LRUStatistics(org.apache.geode.internal.cache.lru.LRUStatistics) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) CacheLoader(org.apache.geode.cache.CacheLoader) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 25 with LoaderHelper

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

the class LocalRegionDUnitTest method testLocalLoaderNetSearch.

/**
   * Tests that if a <code>CacheLoader</code> for a local region invokes
   * {@link LoaderHelper#netSearch}, a {@link CacheLoaderException} is thrown.
   */
@Test
public void testLocalLoaderNetSearch() throws CacheException {
    assertEquals(Scope.LOCAL, getRegionAttributes().getScope());
    final String name = this.getUniqueName();
    final Object key = this.getUniqueName();
    TestCacheLoader loader = new TestCacheLoader() {

        public Object load2(LoaderHelper helper) throws CacheLoaderException {
            try {
                helper.netSearch(true);
            } catch (TimeoutException ex) {
                Assert.fail("Why did I timeout?", ex);
            }
            return null;
        }
    };
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    factory.setCacheLoader(loader);
    Region region = createRegion(name, factory.create());
    assertEquals(Scope.LOCAL, region.getAttributes().getScope());
    try {
        region.get(key);
        fail("Should have thrown a CacheLoaderException");
    } catch (CacheLoaderException ex) {
        String expected = org.apache.geode.internal.cache.LoaderHelperImpl.NET_SEARCH_LOCAL.toLocalizedString();
        String message = ex.getMessage();
        assertTrue("Unexpected message \"" + message + "\"", message.indexOf(expected) != -1);
    }
}
Also used : LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) Region(org.apache.geode.cache.Region) TimeoutException(org.apache.geode.cache.TimeoutException) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

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