Search in sources :

Example 76 with PartitionAttributesFactory

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

the class PartitionedRegionTestUtilsDUnitTest method testGetKeys.

/**
   * Test the {@link PartitionedRegion#getSomeKeys(java.util.Random)} method, making sure it returns
   * keys when there are keys and {@link java.util.Collections#EMPTY_SET} when there are none.
   */
@Test
public void testGetKeys() throws Exception {
    final String r = getUniqueName();
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    VM vm2 = host.getVM(2);
    CacheSerializableRunnable create = new CacheSerializableRunnable("CreatePartitionedRegion") {

        public void run2() throws CacheException {
            Cache cache = getCache();
            AttributesFactory attr = new AttributesFactory();
            attr.setPartitionAttributes(new PartitionAttributesFactory().setTotalNumBuckets(totalNumBuckets).create());
            Region p = cache.createRegion(r, attr.create());
            assertNotNull(p);
            assertTrue(!p.isDestroyed());
            assertNull(p.get("Key"));
        }
    };
    vm0.invoke(create);
    vm1.invoke(create);
    vm2.invoke(create);
    vm0.invoke(new CacheSerializableRunnable("GetSomeKeys") {

        public void run2() throws CacheException {
            PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(r);
            Random rand = new Random(123);
            // Assert that its empty
            for (int i = 0; i < 5; i++) {
                LogWriterUtils.getLogWriter().info("Invocation " + i + " of getSomeKeys");
                try {
                    Set s = null;
                    s = pr.getSomeKeys(rand);
                    assertNotNull(s);
                    assertTrue(s.isEmpty());
                } catch (ClassNotFoundException cnfe) {
                    Assert.fail("GetSomeKeys failed with ClassNotFoundException", cnfe);
                } catch (IOException ioe) {
                    Assert.fail("GetSomeKeys failed with IOException", ioe);
                }
            }
            final int MAXKEYS = 50;
            for (int i = 0; i < MAXKEYS; i++) {
                pr.put("testKey" + i, new Integer(i));
            }
            // Assert not empty and has value in an accepable range
            for (int i = 0; i < 5; i++) {
                LogWriterUtils.getLogWriter().info("Invocation " + i + " of getSomeKeys");
                try {
                    Set s = null;
                    s = pr.getSomeKeys(rand);
                    assertNotNull(s);
                    assertFalse(s.isEmpty());
                    Integer val;
                    LogWriterUtils.getLogWriter().info("Invocation " + i + " got " + s.size() + " keys");
                    for (Iterator it = s.iterator(); it.hasNext(); ) {
                        Object key = it.next();
                        LogWriterUtils.getLogWriter().info("Key: " + key);
                        val = (Integer) pr.get(key);
                        assertNotNull(val);
                        assertTrue(val.intValue() >= 0);
                        assertTrue(val.intValue() < MAXKEYS);
                    }
                } catch (ClassNotFoundException cnfe) {
                    Assert.fail("GetSomeKeys failed with ClassNotFoundException", cnfe);
                } catch (IOException ioe) {
                    Assert.fail("GetSomeKeys failed with IOException", ioe);
                }
            }
        }
    });
}
Also used : Set(java.util.Set) CacheException(org.apache.geode.cache.CacheException) Host(org.apache.geode.test.dunit.Host) IOException(java.io.IOException) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) Random(java.util.Random) VM(org.apache.geode.test.dunit.VM) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 77 with PartitionAttributesFactory

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

the class PartitionedRegionTestUtilsDUnitTest method testLocalCacheOps.

/**
   * Test the test utilities that allow investigation of a PartitionedRegion's local cache.
   */
@Test
public void testLocalCacheOps() throws Exception {
    final String r = getUniqueName();
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm2 = host.getVM(2);
    vm0.invoke(new CacheSerializableRunnable("CreatePR") {

        public void run2() throws CacheException {
            Cache cache = getCache();
            AttributesFactory attr = new AttributesFactory();
            attr.setPartitionAttributes(new PartitionAttributesFactory().setTotalNumBuckets(totalNumBuckets).setLocalMaxMemory(8).create());
            PartitionedRegion p = (PartitionedRegion) cache.createRegion(r, attr.create());
            assertNotNull(p);
        }
    });
    // TODO enable this test when we have the LocalCache properly implemented -- mthomas 2/23/2006
    // vm1.invoke(new CacheSerializableRunnable("CreatePRWithLocalCacheAndTestOps") {
    // public void run2() throws CacheException
    // {
    // Cache cache = getCache();
    // AttributesFactory attr = new AttributesFactory();
    // attr.setScope(Scope.DISTRIBUTED_ACK);
    // Properties lp = new Properties();
    // lp.setProperty(PartitionAttributesFactory.LOCAL_MAX_MEMORY_PROPERTY, "0");
    // attr.setPartitionAttributes(new PartitionAttributesFactory()
    // .setLocalProperties(lp)
    // .createPartitionAttributes());
    //
    // PartitionedRegion p = (PartitionedRegion) cache.createRegion(r, attr.create());
    // assertNotNull(p);
    //
    // final String key1 = "lcKey1"; final String val1 = "lcVal1";
    // final String key2 = "lcKey2"; final String val2 = "lcVal2";
    // // Test localCacheContainsKey
    // assertFalse(p.localCacheContainsKey(key1));
    // assertFalse(p.localCacheContainsKey(key2));
    // p.put(key1, val1);
    // assertFalse(p.localCacheContainsKey(key1));
    // assertFalse(p.localCacheContainsKey(key2));
    // assertIndexDetailsEquals(val1, p.get(key1));
    // assertTrue(p.localCacheContainsKey(key1));
    // assertFalse(p.localCacheContainsKey(key2));
    //
    // // test localCacheKeySet
    // Set lset = p.localCacheKeySet();
    // assertTrue(lset.contains(key1));
    // assertFalse(lset.contains(key2));
    //
    // // test localCacheGet
    // assertIndexDetailsEquals(val1, p.localCacheGet(key1));
    // assertNull(p.localCacheGet(key2));
    // p.put(key2, val2);
    // assertNull(p.localCacheGet(key2));
    // assertIndexDetailsEquals(val2, p.get(key2));
    // assertIndexDetailsEquals(val2, p.localCacheGet(key2));
    // }
    // });
    vm2.invoke(new CacheSerializableRunnable("CreatePRWithNoLocalCacheAndTestOps") {

        public void run2() throws CacheException {
            Cache cache = getCache();
            AttributesFactory attr = new AttributesFactory();
            attr.setPartitionAttributes(new PartitionAttributesFactory().setTotalNumBuckets(totalNumBuckets).setLocalMaxMemory(0).create());
            PartitionedRegion p = (PartitionedRegion) cache.createRegion(r, attr.create());
            assertNotNull(p);
            final String key3 = "lcKey3";
            final String val3 = "lcVal3";
            final String key4 = "lcKey4";
            final String val4 = "lcVal4";
            // Test localCacheContainsKey
            assertFalse(p.localCacheContainsKey(key3));
            assertFalse(p.localCacheContainsKey(key4));
            p.put(key3, val3);
            assertFalse(p.localCacheContainsKey(key3));
            assertFalse(p.localCacheContainsKey(key4));
            assertEquals(val3, p.get(key3));
            assertFalse(p.localCacheContainsKey(key3));
            assertFalse(p.localCacheContainsKey(key4));
            // test localCacheKeySet
            Set lset = p.localCacheKeySet();
            assertFalse(lset.contains(key3));
            assertFalse(lset.contains(key4));
            // test localCacheGet
            assertNull(val3, p.localCacheGet(key3));
            assertNull(p.localCacheGet(key4));
            p.put(key4, val4);
            assertNull(p.localCacheGet(key4));
            assertEquals(val4, p.get(key4));
            assertNull(p.localCacheGet(key4));
        }
    });
}
Also used : PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) Set(java.util.Set) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheException(org.apache.geode.cache.CacheException) VM(org.apache.geode.test.dunit.VM) Host(org.apache.geode.test.dunit.Host) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 78 with PartitionAttributesFactory

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

the class PersistentPartitionedRegionJUnitTest method createRegion.

private Region createRegion(int ttl, boolean isHeapEviction, boolean isEntryEviction) {
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOG_LEVEL, "info");
    // props.setProperty("log-file", "junit.log");
    cache = new CacheFactory(props).create();
    cache.createDiskStoreFactory().setMaxOplogSize(1).setDiskDirs(new File[] { dir }).create("disk");
    RegionFactory<Object, Object> rf = cache.createRegionFactory().setDataPolicy(DataPolicy.PERSISTENT_PARTITION).setDiskStoreName("disk");
    rf.setPartitionAttributes(new PartitionAttributesFactory().setTotalNumBuckets(1).create());
    if (ttl > 0) {
        rf.setEntryTimeToLive(new ExpirationAttributes(ttl, ExpirationAction.DESTROY));
    }
    if (isEntryEviction) {
        rf.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(10, EvictionAction.OVERFLOW_TO_DISK));
    } else if (isHeapEviction) {
        rf.setEvictionAttributes(EvictionAttributes.createLRUHeapAttributes(ObjectSizer.DEFAULT, EvictionAction.OVERFLOW_TO_DISK));
    }
    Region region = rf.create("region");
    return region;
}
Also used : PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) Region(org.apache.geode.cache.Region) Properties(java.util.Properties) CacheFactory(org.apache.geode.cache.CacheFactory) File(java.io.File) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes)

Example 79 with PartitionAttributesFactory

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

the class PartitionedRegionWithSameNameDUnitTest method createRegionAttrsForPR.

/**
   * This function creates Region attributes with provided scope,redundancy and localmaxMemory
   */
public static RegionAttributes createRegionAttrsForPR(int red, int localMaxMem) {
    AttributesFactory attr = new AttributesFactory();
    attr.setMirrorType(MirrorType.NONE);
    PartitionAttributesFactory paf = new PartitionAttributesFactory();
    PartitionAttributes prAttr = paf.setRedundantCopies(red).setLocalMaxMemory(localMaxMem).create();
    attr.setPartitionAttributes(prAttr);
    return attr.create();
}
Also used : PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) PartitionAttributes(org.apache.geode.cache.PartitionAttributes)

Example 80 with PartitionAttributesFactory

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

the class PRClientServerRegionFunctionExecutionFailoverDUnitTest method createServerWithLocator.

public int createServerWithLocator(String locator, boolean isAccessor, ArrayList commonAttrs) {
    Properties props = new Properties();
    props = new Properties();
    props.setProperty(LOCATORS, locator);
    DistributedSystem ds = getSystem(props);
    cache = new CacheFactory(props).create(ds);
    CacheServer server = cache.addCacheServer();
    int port = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
    server.setPort(port);
    server.setHostnameForClients("localhost");
    try {
        server.start();
    } catch (IOException e) {
        Assert.fail("Failed to start server ", e);
    }
    PartitionAttributesFactory paf = new PartitionAttributesFactory();
    if (isAccessor) {
        paf.setLocalMaxMemory(0);
    }
    paf.setTotalNumBuckets(((Integer) commonAttrs.get(3)).intValue()).setRedundantCopies(((Integer) commonAttrs.get(2)).intValue());
    AttributesFactory attr = new AttributesFactory();
    attr.setPartitionAttributes(paf.create());
    region = cache.createRegion(regionName, attr.create());
    assertNotNull(region);
    LogWriterUtils.getLogWriter().info("Partitioned Region " + regionName + " created Successfully :" + region.toString());
    return port;
}
Also used : PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CacheServer(org.apache.geode.cache.server.CacheServer) IOException(java.io.IOException) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) CacheFactory(org.apache.geode.cache.CacheFactory) DistributedSystem(org.apache.geode.distributed.DistributedSystem)

Aggregations

PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)340 AttributesFactory (org.apache.geode.cache.AttributesFactory)289 Region (org.apache.geode.cache.Region)173 Test (org.junit.Test)154 Cache (org.apache.geode.cache.Cache)136 PartitionAttributes (org.apache.geode.cache.PartitionAttributes)116 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)112 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)110 VM (org.apache.geode.test.dunit.VM)101 Host (org.apache.geode.test.dunit.Host)99 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)95 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)75 CacheException (org.apache.geode.cache.CacheException)58 LocalRegion (org.apache.geode.internal.cache.LocalRegion)48 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)47 IOException (java.io.IOException)42 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)42 DiskStore (org.apache.geode.cache.DiskStore)41 RegionAttributes (org.apache.geode.cache.RegionAttributes)41 BucketRegion (org.apache.geode.internal.cache.BucketRegion)35