blob: 1a3c2c9aea323672e51e4af9a119aaefe51f76b5 [file] [log] [blame]
/*=========================================================================
* 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.execute;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import com.gemstone.gemfire.cache.AttributesFactory;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.DataPolicy;
import com.gemstone.gemfire.cache.PartitionAttributesFactory;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.Scope;
import com.gemstone.gemfire.cache.execute.FunctionAdapter;
import com.gemstone.gemfire.cache.execute.FunctionContext;
import com.gemstone.gemfire.cache.execute.FunctionService;
import com.gemstone.gemfire.cache.partition.PartitionRegionHelper;
import com.gemstone.gemfire.cache30.CacheTestCase;
import com.gemstone.gemfire.distributed.DistributedSystem;
import com.gemstone.gemfire.internal.Assert;
import dunit.Host;
import dunit.SerializableRunnable;
import dunit.VM;
@SuppressWarnings("serial")
public class MultiRegionFunctionExecutionDUnitTest extends CacheTestCase {
VM vm0 = null;
VM vm1 = null;
VM vm2 = null;
VM vm3 = null;
static Region PR1 = null ;
static Region PR2 = null ;
static Region RR1 = null ;
static Region RR2 = null ;
static Region LR1 = null ;
static Cache cache = null ;
public MultiRegionFunctionExecutionDUnitTest(String name) {
super(name);
}
public void setUp() throws Exception {
super.setUp();
Host host = Host.getHost(0);
vm0 = host.getVM(0);
vm1 = host.getVM(1);
vm2 = host.getVM(2);
vm3 = host.getVM(3);
}
public void tearDown2() throws Exception {
super.tearDown2();
cache = null;
invokeInEveryVM(new SerializableRunnable() { public void run() { cache = null; } });
}
public void testMultiRegionFunctionExecution(){
vm0.invoke(MultiRegionFunctionExecutionDUnitTest.class, "createRegionsOnVm0");
vm1.invoke(MultiRegionFunctionExecutionDUnitTest.class, "createRegionsOnVm1");
vm2.invoke(MultiRegionFunctionExecutionDUnitTest.class, "createRegionsOnVm2");
vm3.invoke(MultiRegionFunctionExecutionDUnitTest.class, "createRegionsOnVm3");
createRegionsOnUnitControllerVm();
Set<Region> regions = new HashSet<Region>();
regions.add(PR1);
InternalFunctionService.onRegions(regions).execute(new FunctionAdapter(){
@Override
public void execute(FunctionContext context) {
MultiRegionFunctionContext mrContext = (MultiRegionFunctionContext)context;
Set<Region> regions = mrContext.getRegions();
Assert.assertTrue(1 == regions.size());
context.getResultSender().lastResult(Boolean.TRUE);
}
@Override
public String getId() {
// TODO Auto-generated method stub
return getClass().getName();
}
}).getResult();
regions.add(PR2);
InternalFunctionService.onRegions(regions).execute(new FunctionAdapter(){
@Override
public void execute(FunctionContext context) {
MultiRegionFunctionContext mrContext = (MultiRegionFunctionContext)context;
Set<Region> regions = mrContext.getRegions();
Assert.assertTrue(0 != regions.size());
context.getResultSender().lastResult(Boolean.TRUE);
}
@Override
public String getId() {
// TODO Auto-generated method stub
return getClass().getName();
}
}).getResult();
regions.add(PR2);
regions.add(RR1);
regions.add(RR2);
regions.add(LR1);
InternalFunctionService.onRegions(regions).execute(new FunctionAdapter(){
@Override
public void execute(FunctionContext context) {
MultiRegionFunctionContext mrContext = (MultiRegionFunctionContext)context;
Set<Region> regions = mrContext.getRegions();
Assert.assertTrue(0 != regions.size());
context.getResultSender().lastResult(Boolean.TRUE);
}
@Override
public String getId() {
// TODO Auto-generated method stub
return getClass().getName();
}
}).getResult();
}
public void createCache() {
try {
Properties props = new Properties();
DistributedSystem ds = getSystem(props);
assertNotNull(ds);
ds.disconnect();
ds = getSystem(props);
cache = CacheFactory.create(ds);
assertNotNull(cache);
}
catch (Exception e) {
fail("Failed while creating the cache", e);
}
}
@SuppressWarnings("unchecked")
public static void createRegionsOnVm0() {
new MultiRegionFunctionExecutionDUnitTest("temp").createCache();
PartitionAttributesFactory pf = new PartitionAttributesFactory();
pf.setTotalNumBuckets(12);
pf.setRedundantCopies(1);
AttributesFactory factory = new AttributesFactory();
factory.setPartitionAttributes(pf.create());
cache.createRegion("PR1", factory.create());
factory = new AttributesFactory();
factory.setDataPolicy(DataPolicy.REPLICATE);
cache.createRegion("RR1", factory.create());
factory = new AttributesFactory();
factory.setDataPolicy(DataPolicy.EMPTY);
cache.createRegion("RR2", factory.create());
}
@SuppressWarnings("unchecked")
public static void createRegionsOnVm1() {
new MultiRegionFunctionExecutionDUnitTest("temp").createCache();
PartitionAttributesFactory pf = new PartitionAttributesFactory();
pf.setTotalNumBuckets(12);
pf.setRedundantCopies(1);
AttributesFactory factory = new AttributesFactory();
factory.setPartitionAttributes(pf.create());
cache.createRegion("PR1", factory.create());
factory = new AttributesFactory();
factory.setDataPolicy(DataPolicy.NORMAL);
cache.createRegion("RR1", factory.create());
factory = new AttributesFactory();
factory.setDataPolicy(DataPolicy.REPLICATE);
cache.createRegion("RR2", factory.create());
}
@SuppressWarnings("unchecked")
public static void createRegionsOnVm2() {
new MultiRegionFunctionExecutionDUnitTest("temp").createCache();
PartitionAttributesFactory pf = new PartitionAttributesFactory();
pf.setTotalNumBuckets(12);
pf.setRedundantCopies(1);
pf.setLocalMaxMemory(0);
AttributesFactory factory = new AttributesFactory();
factory.setPartitionAttributes(pf.create());
cache.createRegion("PR1", factory.create());
pf = new PartitionAttributesFactory();
pf.setTotalNumBuckets(12);
pf.setRedundantCopies(1);
factory = new AttributesFactory();
factory.setPartitionAttributes(pf.create());
cache.createRegion("PR2", factory.create());
factory = new AttributesFactory();
factory.setDataPolicy(DataPolicy.EMPTY);
cache.createRegion("RR1", factory.create());
factory = new AttributesFactory();
factory.setDataPolicy(DataPolicy.NORMAL);
cache.createRegion("RR2", factory.create());
}
@SuppressWarnings("unchecked")
public static void createRegionsOnVm3() {
new MultiRegionFunctionExecutionDUnitTest("temp").createCache();
PartitionAttributesFactory pf = new PartitionAttributesFactory();
pf.setTotalNumBuckets(12);
pf.setRedundantCopies(1);
AttributesFactory factory = new AttributesFactory();
factory.setPartitionAttributes(pf.create());
cache.createRegion("PR1", factory.create());
pf = new PartitionAttributesFactory();
pf.setTotalNumBuckets(12);
pf.setRedundantCopies(1);
factory = new AttributesFactory();
factory.setPartitionAttributes(pf.create());
cache.createRegion("PR2", factory.create());
factory = new AttributesFactory();
factory.setDataPolicy(DataPolicy.REPLICATE);
cache.createRegion("RR1", factory.create());
factory = new AttributesFactory();
factory.setDataPolicy(DataPolicy.REPLICATE);
cache.createRegion("RR2", factory.create());
}
@SuppressWarnings("unchecked")
public static void createRegionsOnUnitControllerVm() {
new MultiRegionFunctionExecutionDUnitTest("temp").createCache();
PartitionAttributesFactory pf = new PartitionAttributesFactory();
pf.setTotalNumBuckets(12);
pf.setRedundantCopies(1);
AttributesFactory factory = new AttributesFactory();
factory.setPartitionAttributes(pf.create());
PR1 = cache.createRegion("PR1", factory.create());
pf = new PartitionAttributesFactory();
pf.setTotalNumBuckets(12);
pf.setRedundantCopies(1);
pf.setLocalMaxMemory(0);
factory = new AttributesFactory();
factory.setPartitionAttributes(pf.create());
PR2 = cache.createRegion("PR2", factory.create());
factory = new AttributesFactory();
factory.setDataPolicy(DataPolicy.REPLICATE);
RR1 = cache.createRegion("RR1", factory.create());
factory = new AttributesFactory();
factory.setDataPolicy(DataPolicy.EMPTY);
RR2 = cache.createRegion("RR2", factory.create());
factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
LR1 = cache.createRegion("LR1", factory.create());
for (int i = 0; i < 24; i++) {
PR1.put(new Integer(i), new Integer(i));
PR2.put(new Integer(i), new Integer(i));
RR1.put(new Integer(i), new Integer(i));
RR2.put(new Integer(i), new Integer(i));
LR1.put(new Integer(i), new Integer(i));
}
}
}