Search in sources :

Example 36 with CacheFactory

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

the class ClientsWithVersioningRetryDUnitTest method createServerRegionWithPersistence.

private int createServerRegionWithPersistence(VM vm, final boolean persistentPdxRegistry) {
    SerializableCallable createRegion = new SerializableCallable() {

        public Object call() throws Exception {
            CacheFactory cf = new CacheFactory();
            if (persistentPdxRegistry) {
                cf.setPdxPersistent(true).setPdxDiskStore("store");
            }
            //
            Cache cache = getCache(cf);
            cache.createDiskStoreFactory().setDiskDirs(getDiskDirs()).create("store");
            AttributesFactory af = new AttributesFactory();
            af.setScope(Scope.DISTRIBUTED_ACK);
            af.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
            af.setDiskStoreName("store");
            createRootRegion("testSimplePdx", af.create());
            CacheServer server = getCache().addCacheServer();
            int port = AvailablePortHelper.getRandomAvailableTCPPort();
            server.setPort(port);
            server.start();
            return port;
        }
    };
    return (Integer) vm.invoke(createRegion);
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) CacheServer(org.apache.geode.cache.server.CacheServer) CacheFactory(org.apache.geode.cache.CacheFactory) ClientCacheFactory(org.apache.geode.cache.client.ClientCacheFactory) Cache(org.apache.geode.cache.Cache) ClientCache(org.apache.geode.cache.client.ClientCache)

Example 37 with CacheFactory

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

the class PdxSerializableJUnitTest method testPdxPersistentKeysDefDS.

@Test
public void testPdxPersistentKeysDefDS() throws Exception {
    this.c.close();
    this.c = (GemFireCacheImpl) new CacheFactory().set(MCAST_PORT, "0").setPdxPersistent(true).create();
    try {
        this.c.createDiskStoreFactory().create("r2DS");
        Region r1 = this.c.createRegionFactory(RegionShortcut.LOCAL_PERSISTENT).setDiskStoreName("r2DS").create("r1");
        r1.put(new SimpleClass(1, (byte) 1), "1");
        r1.put(new SimpleClass(2, (byte) 2), "2");
        // so we have something to compact offline
        r1.put(new SimpleClass(1, (byte) 1), "1.2");
        Region r2 = this.c.createRegionFactory(RegionShortcut.LOCAL_PERSISTENT).setDiskStoreName("r2DS").create("r2");
        r2.put(new SimpleClass(1, (byte) 1), new SimpleClass(1, (byte) 1));
        r2.put(new SimpleClass(2, (byte) 2), new SimpleClass(2, (byte) 2));
        this.c.close();
        this.c = (GemFireCacheImpl) new CacheFactory().set(MCAST_PORT, "0").setPdxPersistent(true).create();
        this.c.createDiskStoreFactory().create("r2DS");
        r1 = this.c.createRegionFactory(RegionShortcut.LOCAL_PERSISTENT).setDiskStoreName("r2DS").create("r1");
        r2 = this.c.createRegionFactory(RegionShortcut.LOCAL_PERSISTENT).setDiskStoreName("r2DS").create("r2");
        assertEquals(true, r1.containsKey(new SimpleClass(1, (byte) 1)));
        assertEquals(true, r1.containsKey(new SimpleClass(2, (byte) 2)));
        assertEquals(true, r2.containsKey(new SimpleClass(1, (byte) 1)));
        assertEquals(true, r2.containsKey(new SimpleClass(2, (byte) 2)));
        assertEquals(new SimpleClass(1, (byte) 1), r2.get(new SimpleClass(1, (byte) 1)));
        assertEquals(new SimpleClass(2, (byte) 2), r2.get(new SimpleClass(2, (byte) 2)));
        this.c.close();
        // use a cache.xml to recover
        this.c = (GemFireCacheImpl) new CacheFactory().set(MCAST_PORT, "0").create();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos), true);
        pw.println("<?xml version=\"1.0\"?>");
        pw.println("<!DOCTYPE cache PUBLIC");
        pw.println("  \"-//GemStone Systems, Inc.//GemFire Declarative Caching 7.0//EN\"");
        pw.println("  \"http://www.gemstone.com/dtd/cache7_0.dtd\">");
        pw.println("<cache>");
        pw.println("  <disk-store name=\"r2DS\"/>");
        pw.println("  <pdx persistent=\"true\"/>");
        pw.println("  <region name=\"r1\" refid=\"LOCAL_PERSISTENT\">");
        pw.println("    <region-attributes disk-store-name=\"r2DS\"/>");
        pw.println("  </region>");
        pw.println("  <region name=\"r2\" refid=\"LOCAL_PERSISTENT\">");
        pw.println("    <region-attributes disk-store-name=\"r2DS\"/>");
        pw.println("  </region>");
        pw.println("</cache>");
        pw.close();
        byte[] bytes = baos.toByteArray();
        this.c.loadCacheXml(new ByteArrayInputStream(bytes));
        r1 = this.c.getRegion("/r1");
        r2 = this.c.getRegion("/r2");
        assertEquals(true, r1.containsKey(new SimpleClass(1, (byte) 1)));
        assertEquals(true, r1.containsKey(new SimpleClass(2, (byte) 2)));
        assertEquals(true, r2.containsKey(new SimpleClass(1, (byte) 1)));
        assertEquals(true, r2.containsKey(new SimpleClass(2, (byte) 2)));
        assertEquals(new SimpleClass(1, (byte) 1), r2.get(new SimpleClass(1, (byte) 1)));
        assertEquals(new SimpleClass(2, (byte) 2), r2.get(new SimpleClass(2, (byte) 2)));
        this.c.close();
        // make sure offlines tools work with disk store that has pdx keys
        SystemAdmin.validateDiskStore("DEFAULT", ".");
        SystemAdmin.compactDiskStore("DEFAULT", ".");
        SystemAdmin.modifyDiskStore("DEFAULT", ".");
        SystemAdmin.validateDiskStore("r2DS", ".");
        SystemAdmin.compactDiskStore("r2DS", ".");
        SystemAdmin.modifyDiskStore("r2DS", ".");
    } finally {
        try {
            this.c.close();
        } finally {
            Pattern pattern = Pattern.compile("BACKUP(DEFAULT|r2DS).*");
            File[] files = new File(".").listFiles((dir1, name) -> pattern.matcher(name).matches());
            if (files != null) {
                for (File file : files) {
                    Files.delete(file.toPath());
                }
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) ByteArrayInputStream(java.io.ByteArrayInputStream) Region(org.apache.geode.cache.Region) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CacheFactory(org.apache.geode.cache.CacheFactory) File(java.io.File) PrintWriter(java.io.PrintWriter) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 38 with CacheFactory

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

the class PdxSerializableJUnitTest method testPdxPersistentKeys.

// for bugs 44271 and 44914
@Test
public void testPdxPersistentKeys() throws Exception {
    this.c.close();
    this.c = (GemFireCacheImpl) new CacheFactory().set(MCAST_PORT, "0").setPdxPersistent(true).setPdxDiskStore("pdxDS").create();
    try {
        DiskStoreFactory dsf = this.c.createDiskStoreFactory();
        dsf.create("pdxDS");
        this.c.createDiskStoreFactory().create("r2DS");
        Region r1 = this.c.createRegionFactory(RegionShortcut.LOCAL_PERSISTENT).create("r1");
        r1.put(new SimpleClass(1, (byte) 1), "1");
        r1.put(new SimpleClass(2, (byte) 2), "2");
        // so we have something to compact offline
        r1.put(new SimpleClass(1, (byte) 1), "1.2");
        Region r2 = this.c.createRegionFactory(RegionShortcut.LOCAL_PERSISTENT).setDiskStoreName("r2DS").create("r2");
        r2.put(new SimpleClass(1, (byte) 1), new SimpleClass(1, (byte) 1));
        r2.put(new SimpleClass(2, (byte) 2), new SimpleClass(2, (byte) 2));
        this.c.close();
        this.c = (GemFireCacheImpl) new CacheFactory().set(MCAST_PORT, "0").setPdxPersistent(true).setPdxDiskStore("pdxDS").create();
        dsf = this.c.createDiskStoreFactory();
        dsf.create("pdxDS");
        this.c.createDiskStoreFactory().create("r2DS");
        r1 = this.c.createRegionFactory(RegionShortcut.LOCAL_PERSISTENT).create("r1");
        r2 = this.c.createRegionFactory(RegionShortcut.LOCAL_PERSISTENT).setDiskStoreName("r2DS").create("r2");
        assertEquals(true, r1.containsKey(new SimpleClass(1, (byte) 1)));
        assertEquals(true, r1.containsKey(new SimpleClass(2, (byte) 2)));
        assertEquals(true, r2.containsKey(new SimpleClass(1, (byte) 1)));
        assertEquals(true, r2.containsKey(new SimpleClass(2, (byte) 2)));
        assertEquals(new SimpleClass(1, (byte) 1), r2.get(new SimpleClass(1, (byte) 1)));
        assertEquals(new SimpleClass(2, (byte) 2), r2.get(new SimpleClass(2, (byte) 2)));
        this.c.close();
        // use a cache.xml to recover
        this.c = (GemFireCacheImpl) new CacheFactory().set(MCAST_PORT, "0").create();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos), true);
        pw.println("<?xml version=\"1.0\"?>");
        pw.println("<!DOCTYPE cache PUBLIC");
        pw.println("  \"-//GemStone Systems, Inc.//GemFire Declarative Caching 7.0//EN\"");
        pw.println("  \"http://www.gemstone.com/dtd/cache7_0.dtd\">");
        pw.println("<cache>");
        pw.println("  <disk-store name=\"r2DS\"/>");
        pw.println("  <disk-store name=\"pdxDS\"/>");
        pw.println("  <pdx persistent=\"true\" disk-store-name=\"pdxDS\"/>");
        pw.println("  <region name=\"r1\" refid=\"LOCAL_PERSISTENT\"/>");
        pw.println("  <region name=\"r2\" refid=\"LOCAL_PERSISTENT\">");
        pw.println("    <region-attributes disk-store-name=\"r2DS\"/>");
        pw.println("  </region>");
        pw.println("</cache>");
        pw.close();
        byte[] bytes = baos.toByteArray();
        this.c.loadCacheXml(new ByteArrayInputStream(bytes));
        r1 = this.c.getRegion("/r1");
        r2 = this.c.getRegion("/r2");
        assertEquals(true, r1.containsKey(new SimpleClass(1, (byte) 1)));
        assertEquals(true, r1.containsKey(new SimpleClass(2, (byte) 2)));
        assertEquals(true, r2.containsKey(new SimpleClass(1, (byte) 1)));
        assertEquals(true, r2.containsKey(new SimpleClass(2, (byte) 2)));
        assertEquals(new SimpleClass(1, (byte) 1), r2.get(new SimpleClass(1, (byte) 1)));
        assertEquals(new SimpleClass(2, (byte) 2), r2.get(new SimpleClass(2, (byte) 2)));
        this.c.close();
        // make sure offlines tools work with disk store that has pdx keys
        SystemAdmin.validateDiskStore("DEFAULT", ".");
        SystemAdmin.compactDiskStore("DEFAULT", ".");
        SystemAdmin.modifyDiskStore("DEFAULT", ".");
        SystemAdmin.validateDiskStore("r2DS", ".");
        SystemAdmin.compactDiskStore("r2DS", ".");
        SystemAdmin.modifyDiskStore("r2DS", ".");
        SystemAdmin.validateDiskStore("pdxDS", ".");
        SystemAdmin.compactDiskStore("pdxDS", ".");
        SystemAdmin.modifyDiskStore("pdxDS", ".");
    } finally {
        try {
            this.c.close();
        } finally {
            Pattern pattern = Pattern.compile("BACKUP(DEFAULT|pdxDS|r2DS).*");
            File[] files = new File(".").listFiles((dir1, name) -> pattern.matcher(name).matches());
            if (files != null) {
                for (File file : files) {
                    Files.delete(file.toPath());
                }
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) ByteArrayInputStream(java.io.ByteArrayInputStream) Region(org.apache.geode.cache.Region) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CacheFactory(org.apache.geode.cache.CacheFactory) File(java.io.File) DiskStoreFactory(org.apache.geode.cache.DiskStoreFactory) PrintWriter(java.io.PrintWriter) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 39 with CacheFactory

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

the class PdxSerializableDUnitTest method testPersistenceDefaultDiskStore.

@Test
public void testPersistenceDefaultDiskStore() throws Throwable {
    SerializableCallable createRegion = new SerializableCallable() {

        public Object call() throws Exception {
            // Make sure the type registry is persistent
            CacheFactory cf = new CacheFactory();
            cf.setPdxPersistent(true);
            getCache(cf);
            AttributesFactory af = new AttributesFactory();
            af.setScope(Scope.DISTRIBUTED_ACK);
            af.setDataPolicy(DataPolicy.PERSISTENT_REPLICATE);
            createRootRegion("testSimplePdx", af.create());
            return null;
        }
    };
    persistenceTest(createRegion);
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) CacheFactory(org.apache.geode.cache.CacheFactory) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 40 with CacheFactory

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

the class PdxAttributesJUnitTest method testDuplicatePdxTypeId.

@Test
public void testDuplicatePdxTypeId() throws Exception {
    int dsId = 5;
    CacheFactory cf = new CacheFactory();
    cf.set(MCAST_PORT, "0");
    cf.set(ConfigurationProperties.DISTRIBUTED_SYSTEM_ID, String.valueOf(dsId));
    Cache cache = cf.create();
    // define a type.
    defineAType();
    Region pdxRegion = cache.getRegion(PeerTypeRegistration.REGION_NAME);
    Iterator itr = pdxRegion.entrySet().iterator();
    boolean foundException = false;
    boolean foundEnumException = false;
    while (itr.hasNext()) {
        Map.Entry ent = (Map.Entry) itr.next();
        if (ent.getKey() instanceof Integer) {
            int pdxTypeId = (int) ent.getKey();
            try {
                pdxRegion.put(pdxTypeId, new PdxType());
            } catch (CacheWriterException cwe) {
                foundException = true;
            }
        } else {
            EnumId enumId = (EnumId) ent.getKey();
            EnumInfo enumInfo = new EnumInfo(SimpleEnum.ONE);
            try {
                pdxRegion.put(enumId, enumInfo);
            } catch (CacheWriterException cwe) {
                foundEnumException = true;
            }
        }
    }
    assertEquals(true, foundException);
    assertEquals(true, foundEnumException);
    cache.close();
}
Also used : PdxType(org.apache.geode.pdx.internal.PdxType) EnumInfo(org.apache.geode.pdx.internal.EnumInfo) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) CacheFactory(org.apache.geode.cache.CacheFactory) Map(java.util.Map) Cache(org.apache.geode.cache.Cache) CacheWriterException(org.apache.geode.cache.CacheWriterException) EnumId(org.apache.geode.pdx.internal.EnumId) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

CacheFactory (org.apache.geode.cache.CacheFactory)125 Properties (java.util.Properties)51 Test (org.junit.Test)51 Cache (org.apache.geode.cache.Cache)44 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)38 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)28 Region (org.apache.geode.cache.Region)21 ClientCacheFactory (org.apache.geode.cache.client.ClientCacheFactory)18 CacheServer (org.apache.geode.cache.server.CacheServer)17 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)17 File (java.io.File)15 Before (org.junit.Before)14 SerializationTest (org.apache.geode.test.junit.categories.SerializationTest)13 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)12 ClientCache (org.apache.geode.cache.client.ClientCache)11 Host (org.apache.geode.test.dunit.Host)11 IOException (java.io.IOException)10 AttributesFactory (org.apache.geode.cache.AttributesFactory)10 PdxType (org.apache.geode.pdx.internal.PdxType)9 MyPdxSerializer (com.examples.snapshot.MyPdxSerializer)8