Search in sources :

Example 11 with LoaderHelper

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

the class MultiVMRegionTestCase method testRemoteLoaderNetSearch.

/**
   * Tests that invoking <code>netSearch</code> in a remote loader returns <code>null</code> instead
   * of causing infinite recursion.
   */
@Test
public void testRemoteLoaderNetSearch() throws Exception {
    assumeTrue(supportsNetLoad());
    assertTrue(getRegionAttributes().getScope().isDistributed());
    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("Get value") {

        @Override
        public void run2() throws CacheException {
            Region region = createRegion(name);
            assertEquals(value, region.get(key));
        }
    };
    vm0.invoke(new CacheSerializableRunnable("Create Region") {

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

                @Override
                public Object load2(LoaderHelper helper) throws CacheLoaderException {
                    try {
                        assertNull(helper.netSearch(true));
                    } catch (TimeoutException ex) {
                        fail("Why did I time out?", ex);
                    }
                    return value;
                }
            });
        }
    });
    vm1.invoke(create);
}
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) TimeoutException(org.apache.geode.cache.TimeoutException) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest)

Example 12 with LoaderHelper

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

the class LRUEvictionControllerDUnitTest method testSizeOne.

/**
   * Tests an <code>LRUCapacityController</code> of size 1.
   */
@Test
public void testSizeOne() throws CacheException {
    int threshold = 1;
    final String name = this.getUniqueName();
    AttributesFactory factory = new AttributesFactory();
    factory.setOffHeap(isOffHeapEnabled());
    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 <= 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 : 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 13 with LoaderHelper

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

the class LocalRegionDUnitTest method testLocalUpdateModifiedCallbackArgument.

/**
   * Tests that a local writer receives a modified version of the callback argument on an update.
   */
@Test
public void testLocalUpdateModifiedCallbackArgument() throws CacheException {
    final String name = this.getUniqueName();
    final Object key = "KEY";
    final Object value = "VALUE";
    final Object one = "ONE";
    final Object two = "TWO";
    TestCacheLoader loader = new TestCacheLoader() {

        public Object load2(LoaderHelper helper) throws CacheLoaderException {
            Object[] array = (Object[]) helper.getArgument();
            assertEquals(one, array[0]);
            array[0] = two;
            return value;
        }
    };
    TestCacheWriter writer = new TestCacheWriter() {

        public void beforeCreate2(EntryEvent event) throws CacheWriterException {
        }

        public void beforeUpdate2(EntryEvent event) throws CacheWriterException {
            Object[] array = (Object[]) event.getCallbackArgument();
            assertEquals(two, array[0]);
        }
    };
    AttributesFactory factory = new AttributesFactory(getRegionAttributes());
    factory.setCacheLoader(loader);
    factory.setCacheWriter(writer);
    Region region = createRegion(name, factory.create());
    region.create(key, null);
    assertFalse(loader.wasInvoked());
    assertTrue(writer.wasInvoked());
    Object[] array = new Object[] { one };
    assertEquals(value, region.get(key, array));
    assertTrue(loader.wasInvoked());
    assertTrue(writer.wasInvoked());
}
Also used : LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) EntryEvent(org.apache.geode.cache.EntryEvent) Region(org.apache.geode.cache.Region) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 14 with LoaderHelper

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

the class GlobalRegionDUnitTest method testRemoteFetch.

/**
   * Tests that a value in a remote cache will be fetched by <code>netSearch</code> and that no
   * loaders are invoked.
   */
@Test
public void testRemoteFetch() throws CacheException {
    assertTrue(getRegionAttributes().getScope().isDistributed());
    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 {
            Region region = createRegion(name);
            loader = new TestCacheLoader() {

                public Object load2(LoaderHelper helper) throws CacheLoaderException {
                    fail("Should not be invoked");
                    return null;
                }
            };
            region.getAttributesMutator().setCacheLoader(loader);
        }
    };
    vm0.invoke(create);
    vm0.invoke(new CacheSerializableRunnable("Put") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            region.put(key, value);
            assertFalse(loader.wasInvoked());
        }
    });
    vm1.invoke(create);
    vm1.invoke(new CacheSerializableRunnable("Get") {

        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            assertEquals(value, region.get(key));
            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) Region(org.apache.geode.cache.Region) Host(org.apache.geode.test.dunit.Host) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 15 with LoaderHelper

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

the class RebalanceOperationDUnitTest method runTestWaitForOperation.

/**
   * Test to ensure that we wait for in progress write operations before moving a primary.
   * 
   * @throws Exception
   */
public void runTestWaitForOperation(final Operation op) throws Exception {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    SerializableRunnable createPrRegion = new SerializableRunnable("createRegion") {

        public void run() {
            Cache cache = getCache();
            AttributesFactory attr = new AttributesFactory();
            attr.setCacheLoader(new CacheLoader() {

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

                public void close() {
                }
            });
            PartitionAttributesFactory paf = new PartitionAttributesFactory();
            paf.setRedundantCopies(1);
            paf.setRecoveryDelay(-1);
            paf.setStartupRecoveryDelay(-1);
            paf.setLocalMaxMemory(100);
            PartitionAttributes prAttr = paf.create();
            attr.setPartitionAttributes(prAttr);
            cache.createRegion("region1", attr.create());
        }
    };
    // Create a region in this VM with a cache writer
    // and cache loader
    Cache cache = getCache();
    AttributesFactory attr = new AttributesFactory();
    attr.setCacheLoader(new CacheLoader() {

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

        public void close() {
        }
    });
    PartitionAttributesFactory paf = new PartitionAttributesFactory();
    paf.setRedundantCopies(1);
    paf.setRecoveryDelay(-1);
    paf.setStartupRecoveryDelay(-1);
    paf.setLocalMaxMemory(100);
    PartitionAttributes prAttr = paf.create();
    attr.setPartitionAttributes(prAttr);
    final Region region = cache.createRegion("region1", attr.create());
    // create some buckets
    region.put(Integer.valueOf(1), "A");
    region.put(Integer.valueOf(2), "A");
    BlockingCacheListener cacheWriter = new BlockingCacheListener(2);
    region.getAttributesMutator().addCacheListener(cacheWriter);
    // start two threads doing operations, one on each bucket
    // the threads will block on the cache writer. The rebalance operation
    // will try to move one of these buckets, but it shouldn't
    // be able to because of the in progress operation.
    Thread thread1 = new Thread() {

        public void run() {
            op.execute(region, Integer.valueOf(1));
        }
    };
    thread1.start();
    Thread thread2 = new Thread() {

        public void run() {
            op.execute(region, Integer.valueOf(2));
        }
    };
    thread2.start();
    cacheWriter.waitForOperationsStarted();
    SerializableRunnable checkLowRedundancy = new SerializableRunnable("checkLowRedundancy") {

        public void run() {
            Cache cache = getCache();
            Region region = cache.getRegion("region1");
            PartitionRegionInfo details = PartitionRegionHelper.getPartitionRegionInfo(region);
            assertEquals(2, details.getCreatedBucketCount());
            assertEquals(0, details.getActualRedundantCopies());
            assertEquals(2, details.getLowRedundancyBucketCount());
        }
    };
    // make sure we can tell that the buckets have low redundancy
    checkLowRedundancy.run();
    // Create the region in the other VM (should have no effect)
    vm1.invoke(createPrRegion);
    // Make sure we still have low redundancy
    checkLowRedundancy.run();
    ResourceManager manager = cache.getResourceManager();
    RebalanceOperation rebalance = manager.createRebalanceFactory().start();
    try {
        rebalance.getResults(5, TimeUnit.SECONDS);
        fail("Operation should not have completed");
    } catch (TimeoutException expected) {
    // do nothing
    }
    cacheWriter.release();
    LogWriterUtils.getLogWriter().info("starting wait for rebalance.  Will wait for " + MAX_WAIT + " seconds");
    RebalanceResults results = rebalance.getResults(MAX_WAIT, TimeUnit.SECONDS);
    assertEquals(2, results.getTotalBucketCreatesCompleted());
    assertEquals(1, results.getTotalPrimaryTransfersCompleted());
    assertEquals(0, results.getTotalBucketTransferBytes());
    assertEquals(0, results.getTotalBucketTransfersCompleted());
    Set<PartitionRebalanceInfo> detailSet = results.getPartitionRebalanceDetails();
    assertEquals(1, detailSet.size());
    PartitionRebalanceInfo details = detailSet.iterator().next();
    assertEquals(2, details.getBucketCreatesCompleted());
    assertEquals(1, details.getPrimaryTransfersCompleted());
    assertEquals(0, details.getBucketTransferBytes());
    assertEquals(0, details.getBucketTransfersCompleted());
    Set<PartitionMemberInfo> afterDetails = details.getPartitionMemberDetailsAfter();
    assertEquals(2, afterDetails.size());
    for (PartitionMemberInfo memberDetails : afterDetails) {
        assertEquals(2, memberDetails.getBucketCount());
        assertEquals(1, memberDetails.getPrimaryCount());
        SerializableRunnable checkRedundancyFixed = new SerializableRunnable("checkRedundancyFixed") {

            public void run() {
                Cache cache = getCache();
                Region region = cache.getRegion("region1");
                PartitionRegionInfo details = PartitionRegionHelper.getPartitionRegionInfo(region);
                assertEquals(2, details.getCreatedBucketCount());
                assertEquals(1, details.getActualRedundantCopies());
                assertEquals(0, details.getLowRedundancyBucketCount());
            }
        };
        checkRedundancyFixed.run();
        vm1.invoke(checkRedundancyFixed);
    }
}
Also used : RebalanceOperation(org.apache.geode.cache.control.RebalanceOperation) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) PartitionAttributes(org.apache.geode.cache.PartitionAttributes) PartitionRebalanceInfo(org.apache.geode.cache.partition.PartitionRebalanceInfo) Host(org.apache.geode.test.dunit.Host) ResourceManager(org.apache.geode.cache.control.ResourceManager) LoaderHelper(org.apache.geode.cache.LoaderHelper) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) PartitionMemberInfo(org.apache.geode.cache.partition.PartitionMemberInfo) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) VM(org.apache.geode.test.dunit.VM) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) BucketRegion(org.apache.geode.internal.cache.BucketRegion) Region(org.apache.geode.cache.Region) CacheLoader(org.apache.geode.cache.CacheLoader) RebalanceResults(org.apache.geode.cache.control.RebalanceResults) PartitionRegionInfo(org.apache.geode.cache.partition.PartitionRegionInfo) Cache(org.apache.geode.cache.Cache) TimeoutException(java.util.concurrent.TimeoutException)

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