| /*========================================================================= |
| * Copyright (c) 2010-2014 Pivotal Software, Inc. All Rights Reserved. |
| * This product is protected by U.S. and international copyright |
| * and intellectual property laws. Pivotal products are covered by |
| * one or more patents listed at http://www.pivotal.io/patents. |
| *========================================================================= |
| */ |
| /** |
| * |
| */ |
| package com.gemstone.gemfire.internal.cache; |
| |
| import com.gemstone.gemfire.cache.Cache; |
| import com.gemstone.gemfire.cache.CacheFactory; |
| import com.gemstone.gemfire.cache.DataPolicy; |
| import com.gemstone.gemfire.cache.DiskStore; |
| import com.gemstone.gemfire.cache.PartitionAttributesFactory; |
| import com.gemstone.gemfire.cache.Region; |
| import com.gemstone.gemfire.cache.RegionFactory; |
| import com.gemstone.gemfire.distributed.DistributedSystem; |
| import com.gemstone.gemfire.distributed.internal.DistributionConfig; |
| import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem; |
| import com.gemstone.gemfire.internal.admin.remote.ShutdownAllRequest; |
| import com.gemstone.gemfire.internal.cache.GemFireCacheImpl; |
| |
| import java.io.File; |
| import java.util.Properties; |
| |
| /** |
| * @author xzhou |
| * |
| */ |
| public class RunCacheInOldGemfire { |
| |
| public static final String diskStoreName1 = "ds1"; |
| public static final String diskStoreName2 = "ds2"; |
| public static final String regionName1 = "region1"; |
| public static final String regionName2 = "region2"; |
| public static final int maxOplogSize = 1; |
| public static final int entrySize = 1024; |
| public static final int numOfKeys = maxOplogSize * 1024 * 1024 / entrySize; |
| |
| protected Cache createCache(String mcastPort) { |
| Properties config = new Properties(); |
| config.setProperty(DistributionConfig.MCAST_PORT_NAME, mcastPort); |
| config.setProperty(DistributionConfig.LOCATORS_NAME, ""); |
| config.setProperty(DistributionConfig.LOG_FILE_NAME, "oldgemfire.log"); |
| InternalDistributedSystem localsystem = (InternalDistributedSystem)DistributedSystem.connect(config); |
| Cache cache = CacheFactory.create(localsystem); |
| return cache; |
| } |
| |
| protected DiskStore createDiskStore(Cache cache, String diskStoreName, String dirName) { |
| // maxOplogSize==1m |
| File dir = new File(dirName); |
| dir.mkdirs(); |
| |
| DiskStore ds = cache.findDiskStore(diskStoreName); |
| if(ds == null) { |
| ds = cache.createDiskStoreFactory() |
| .setDiskDirs(new File[] {dir}).setMaxOplogSize(maxOplogSize).create(diskStoreName); |
| } |
| return ds; |
| } |
| |
| protected Region createPersistentRegion(Cache cache, String regionName, String diskStoreName, boolean isPR) { |
| RegionFactory factory; |
| if (isPR) { |
| PartitionAttributesFactory paf = new PartitionAttributesFactory(); |
| paf.setRedundantCopies(1); |
| factory = cache.createRegionFactory().setDiskStoreName(diskStoreName) |
| .setDataPolicy(DataPolicy.PERSISTENT_PARTITION) |
| .setPartitionAttributes(paf.create()); |
| } else { |
| factory = cache.createRegionFactory().setDiskStoreName(diskStoreName) |
| .setDataPolicy(DataPolicy.PERSISTENT_REPLICATE); |
| } |
| return factory.create(regionName); |
| } |
| |
| /** |
| * start a cache with data |
| * |
| * @param args |
| */ |
| public static void main(String[] args) { |
| // The main() is used for run by a script under older gemfire version |
| if (args.length != 5) { |
| System.out.println("Usage: java -cp gemfire-core-dependencies.jar:$JTESTS com.gemstone.gemfire.internal.cache.RunCacheInOldGemfire mcastPort diskdir isPR doOps(true or false) isShutDownAll"); |
| } |
| |
| String mcastPort = args[0]; |
| String diskdir = args[1]; |
| boolean isPR = Boolean.valueOf(args[2]); |
| boolean doOps = Boolean.valueOf(args[3]); |
| boolean isShutDownAll = Boolean.valueOf(args[4]); |
| |
| RunCacheInOldGemfire test = new RunCacheInOldGemfire(); |
| Cache cache = test.createCache(mcastPort); |
| |
| // create 2 diskstores using the same directory, which should be different with another cache instance |
| test.createDiskStore(cache, diskStoreName1, diskdir); |
| test.createDiskStore(cache, diskStoreName2, diskdir); |
| // create 2 regions each uses different diskstore |
| Region region1 = test.createPersistentRegion(cache, regionName1, diskStoreName1, isPR); |
| Region region2 = test.createPersistentRegion(cache, regionName2, diskStoreName2, isPR); |
| |
| if (doOps) { |
| try { |
| Thread.sleep(5000); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| |
| for (Region region: new Region[] {region1, region2}) { |
| // create enough entries to roll the oplog |
| // do some destroys, invalidates |
| region.put("key1", "value1"); |
| for (int i=0; i<numOfKeys; i++) { |
| byte [] value = new byte[entrySize]; |
| region.put(""+i, value); |
| } |
| region.put("key2", "value2"); |
| region.put("key3", "value3"); |
| region.put("key4", "value4"); |
| region.put("key5", "value5");// create |
| |
| region.destroy("key1"); |
| region.destroy("key2"); |
| region.invalidate("key3"); |
| region.put("key5", "value6");// update |
| } |
| } // doOps |
| |
| try { |
| if (isShutDownAll) { |
| try { |
| Thread.sleep(2000); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| |
| GemFireCacheImpl gfc = (GemFireCacheImpl)cache; |
| ShutdownAllRequest.send(gfc.getDistributedSystem().getDistributionManager(), 0); |
| } else { |
| // wait until all the operations are done |
| final long tilt = System.currentTimeMillis() + 60000; |
| while (true) { |
| String value1 = (String)region1.get("key5"); |
| String value2 = (String)region2.get("key5"); |
| if (value1 != null && value2 != null && value1.equals("value6") && value2.equals("value6")) { |
| break; |
| } else { |
| long timeLeft = tilt - System.currentTimeMillis(); |
| if (timeLeft <= 0) { |
| break; |
| } |
| try { |
| Thread.sleep(2000); |
| } catch (InterruptedException e) { |
| e.printStackTrace(); |
| } |
| } |
| } // while |
| } |
| } catch (Throwable t) { |
| boolean ignore = false; |
| if (t instanceof java.lang.Error) { |
| if (t.getMessage().contains("Maximum permit count exceeded")) { |
| System.out.println("Known issue caused by jdk1.6, ignored for the test:" + t.getMessage()); |
| ignore = true; |
| } |
| } |
| if (!ignore) { |
| throw new RuntimeException(t); |
| } |
| } |
| } |
| } |