Search in sources :

Example 76 with Cache

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

the class PartitionedRegionPRIDDUnitTest method validatePRIDCreation.

/**
   * This function perfoms following checks on PRID creation 1. PRID generated should be between 0
   * to number of partition regions in distributed system 2. PRID should be unique for the partition
   * regions
   * 
   * @param startIndexForRegion
   * @param endIndexForRegion
   * @return
   */
private CacheSerializableRunnable validatePRIDCreation(final int startIndexForRegion, final int endIndexForRegion, final String prPrefix) {
    CacheSerializableRunnable validatePRID = new CacheSerializableRunnable("validatePRIDCreation") {

        String innerPrPrefix = prPrefix;

        public void run2() {
            int noPartitionRegions = endIndexForRegion - startIndexForRegion;
            Cache cache = getCache();
            // getting root region
            Region root = cache.getRegion(Region.SEPARATOR + PartitionedRegionHelper.PR_ROOT_REGION_NAME);
            // root region should niot be null
            assertNotNull("Root region can not be null", root);
            // getting allParititionedRegions
            // Region allPartitionedRegions = root
            // .getSubregion(PartitionedRegionHelper.PARTITIONED_REGION_CONFIG_NAME);
            // allPartitionedRegion should not be null
            // assertNotNull("allPartitionedRegion can not be null",
            // allPartitionedRegions);
            // scope of all partition region should be DISTRIBUTED_ACK
            List prIdList = new ArrayList();
            for (int i = startIndexForRegion; i < endIndexForRegion; i++) {
                final String rName = Region.SEPARATOR + innerPrPrefix + i;
                PartitionedRegion pr = (PartitionedRegion) cache.getRegion(rName);
                assertNotNull("This Partitioned Region " + rName + " cannot be null", pr);
                PartitionRegionConfig prConfig = (PartitionRegionConfig) root.get(pr.getRegionIdentifier());
                assertNotNull("PRConfig for Partitioned Region " + rName + " can not be null", prConfig);
                prIdList.add(Integer.toString(prConfig.getPRId()));
            // this partition region should present in prIdToPr
            /*
           * if (PartitionedRegion.prIdToPR.containsKey(Integer.toString(prConfig .getPRId())) ==
           * false) fail("this partition region is not present in the prIdToPR map " +
           * pr.getName());
           */
            }
            // checking uniqueness of prId in allPartitionRegion
            SortedSet prIdSet = new TreeSet(prIdList);
            if (prIdSet.size() != prIdList.size())
                fail("Duplicate PRID are generated");
            // prId generated should be between 0 to number of partition regions-1
            Iterator prIdSetItr = prIdSet.iterator();
            while (prIdSetItr.hasNext()) {
                int val = Integer.parseInt((String) prIdSetItr.next());
                if (val > noPartitionRegions - 1 & val < 0) {
                    fail("PRID limit is out of range");
                }
            }
            // no of PRID generated in allPartitionRegion should be equal to number of partition region
            if (prIdSet.size() != noPartitionRegions)
                fail("Different PRID generated equal to " + prIdSet.size());
            // no of PRID generated in prIdToPR should be equal to number of partition region
            if (PartitionedRegion.prIdToPR.size() != noPartitionRegions)
                fail("number of entries in the prIdToPR is " + PartitionedRegion.prIdToPR.size());
            // checking uniqueness of prId in prIdToPR
            SortedSet prIdPRSet = new TreeSet(PartitionedRegion.prIdToPR.keySet());
            if (prIdPRSet.size() != PartitionedRegion.prIdToPR.size())
                fail("Duplicate PRID are generated in prIdToPR");
            LogWriterUtils.getLogWriter().info("Size of allPartition region : " + prIdSet.size());
            LogWriterUtils.getLogWriter().info("Size of prIdToPR region     : " + prIdPRSet.size());
            LogWriterUtils.getLogWriter().info("PRID generated successfully");
        }
    };
    return validatePRID;
}
Also used : ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) ArrayList(java.util.ArrayList) List(java.util.List) Cache(org.apache.geode.cache.Cache)

Example 77 with Cache

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

the class PartitionedRegionDelayedRecoveryDUnitTest method testStartupDelay.

// GEODE-757: time sensitive, fails because event occurs 2 millis too
@Category(FlakyTest.class)
// thread unsafe test hook
@Test
public void testStartupDelay() {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    VM vm2 = host.getVM(2);
    SerializableRunnable createPrRegions = new SerializableRunnable("createRegions") {

        public void run() {
            Cache cache = getCache();
            InternalResourceManager.setResourceObserver(new MyResourceObserver());
            AttributesFactory attr = new AttributesFactory();
            PartitionAttributesFactory paf = new PartitionAttributesFactory();
            paf.setStartupRecoveryDelay(5000);
            paf.setRedundantCopies(1);
            PartitionAttributes prAttr = paf.create();
            attr.setPartitionAttributes(prAttr);
            cache.createRegion("region1", attr.create());
        }
    };
    // create the region in 2 VMS
    vm0.invoke(createPrRegions);
    vm1.invoke(createPrRegions);
    // Do 1 put, which should create 1 bucket
    vm0.invoke(new SerializableRunnable("putData") {

        public void run() {
            Cache cache = getCache();
            PartitionedRegion region1 = (PartitionedRegion) cache.getRegion("region1");
            region1.put(Integer.valueOf(1), "B");
            region1.put(Integer.valueOf(2), "B");
            region1.put(Integer.valueOf(3), "B");
            region1.put(Integer.valueOf(4), "B");
        }
    });
    // close 1 cache, which should make the bucket drop below
    // the expected redundancy level.
    vm1.invoke(new SerializableRunnable("close cache") {

        public void run() {
            Cache cache = getCache();
            cache.close();
        }
    });
    final long begin = System.currentTimeMillis();
    // create the region in a third VM, which won't have any buckets
    vm2.invoke(createPrRegions);
    long elapsed = System.currentTimeMillis() - begin;
    assertTrue("Create region should not have waited to recover redundancy. Elapsed=" + elapsed, elapsed < 5000);
    // wait for the bucket to be copied
    elapsed = waitForBucketRecovery(vm2, 4, begin);
    assertTrue("Did not wait at least 5 seconds to create the bucket. Elapsed=" + elapsed, elapsed >= 5000);
    vm2.invoke(new SerializableCallable("wait for primary move") {

        public Object call() throws Exception {
            Cache cache = getCache();
            MyResourceObserver observer = (MyResourceObserver) InternalResourceManager.getResourceObserver();
            observer.waitForRecovery(30, TimeUnit.SECONDS);
            PartitionedRegion region1 = (PartitionedRegion) cache.getRegion("region1");
            assertEquals(2, region1.getDataStore().getNumberOfPrimaryBucketsManaged());
            return null;
        }
    });
}
Also used : SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) PartitionAttributes(org.apache.geode.cache.PartitionAttributes) Host(org.apache.geode.test.dunit.Host) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Cache(org.apache.geode.cache.Cache) Category(org.junit.experimental.categories.Category) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest)

Example 78 with Cache

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

the class PartitionedRegionDelayedRecoveryDUnitTest method testDelay.

// GEODE-860: time sensitive, thread unsafe test hook, CountDownLatch,
@Category(FlakyTest.class)
// InterruptedException
@Test
public void testDelay() {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    VM vm2 = host.getVM(2);
    SerializableRunnable createPrRegions = new SerializableRunnable("createRegions") {

        public void run() {
            final CountDownLatch rebalancingFinished = new CountDownLatch(1);
            InternalResourceManager.setResourceObserver(new ResourceObserverAdapter() {

                @Override
                public void rebalancingOrRecoveryFinished(Region region) {
                    rebalancingFinished.countDown();
                }
            });
            try {
                Cache cache = getCache();
                AttributesFactory attr = new AttributesFactory();
                PartitionAttributesFactory paf = new PartitionAttributesFactory();
                paf.setRecoveryDelay(5000);
                paf.setRedundantCopies(1);
                PartitionAttributes prAttr = paf.create();
                attr.setPartitionAttributes(prAttr);
                cache.createRegion("region1", attr.create());
                if (!rebalancingFinished.await(60000, TimeUnit.MILLISECONDS)) {
                    fail("Redundancy recovery did not happen within 60 seconds");
                }
            } catch (InterruptedException e) {
                Assert.fail("interrupted", e);
            } finally {
                InternalResourceManager.setResourceObserver(null);
            }
        }
    };
    // create the region in 2 VMS
    vm0.invoke(createPrRegions);
    vm1.invoke(createPrRegions);
    // Do 1 put, which should create 1 bucket
    vm0.invoke(new SerializableRunnable("putData") {

        public void run() {
            Cache cache = getCache();
            PartitionedRegion region1 = (PartitionedRegion) cache.getRegion("region1");
            region1.put("A", "B");
        }
    });
    // create the region in a third VM, which won't have any buckets
    vm2.invoke(createPrRegions);
    final long begin = System.currentTimeMillis();
    // close 1 cache, which should make the bucket drop below
    // the expected redundancy level.
    vm1.invoke(new SerializableRunnable("close cache") {

        public void run() {
            Cache cache = getCache();
            cache.close();
        }
    });
    long elapsed = waitForBucketRecovery(vm2, 1, begin);
    assertTrue("Did not wait at least 5 seconds to create the bucket. Elapsed=" + elapsed, elapsed >= 5000);
}
Also used : SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) PartitionAttributes(org.apache.geode.cache.PartitionAttributes) Host(org.apache.geode.test.dunit.Host) CountDownLatch(java.util.concurrent.CountDownLatch) ResourceObserverAdapter(org.apache.geode.internal.cache.control.InternalResourceManager.ResourceObserverAdapter) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) VM(org.apache.geode.test.dunit.VM) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache) Category(org.junit.experimental.categories.Category) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest)

Example 79 with Cache

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

the class PartitionedRegionDelayedRecoveryDUnitTest method waitForBucketRecovery.

private long waitForBucketRecovery(VM vm2, final int numBuckets, final long begin) {
    // wait for the bucket to be copied
    Long elapsed = (Long) vm2.invoke(new SerializableCallable("putData") {

        public Object call() {
            Cache cache = getCache();
            PartitionedRegion region1 = (PartitionedRegion) cache.getRegion("region1");
            while (System.currentTimeMillis() - begin < 30000) {
                int bucketsManaged = region1.getDataStore().getBucketsManaged();
                if (bucketsManaged == numBuckets) {
                    break;
                } else {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        // TODO: don't catch InterruptedException -- let test
                        // fail!
                        e.printStackTrace();
                    }
                }
            }
            assertEquals("Did not start managing the bucket within 30 seconds", numBuckets, region1.getDataStore().getBucketsManaged());
            long elapsed = System.currentTimeMillis() - begin;
            return Long.valueOf(elapsed);
        }
    });
    return elapsed.longValue();
}
Also used : SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Cache(org.apache.geode.cache.Cache)

Example 80 with Cache

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

the class RollbackFunction method execute.

public void execute(FunctionContext context) {
    Cache cache = CacheFactory.getAnyInstance();
    TXId txId = null;
    try {
        txId = (TXId) context.getArguments();
    } catch (ClassCastException e) {
        logger.info("RollbackFunction should be invoked with a TransactionId as an argument i.e. setArguments(txId).execute(function)");
        throw e;
    }
    DistributedMember member = txId.getMemberId();
    Boolean result = false;
    final boolean isDebugEnabled = logger.isDebugEnabled();
    if (cache.getDistributedSystem().getDistributedMember().equals(member)) {
        if (isDebugEnabled) {
            logger.debug("RollbackFunction: for transaction: {} rolling back locally", txId);
        }
        CacheTransactionManager txMgr = cache.getCacheTransactionManager();
        if (txMgr.tryResume(txId)) {
            if (isDebugEnabled) {
                logger.debug("RollbackFunction: resumed transaction: {}", txId);
            }
            txMgr.rollback();
            result = true;
        }
    } else {
        ArrayList args = new ArrayList();
        args.add(txId);
        args.add(NestedTransactionFunction.ROLLBACK);
        Execution ex = FunctionService.onMember(member).setArguments(args);
        if (isDebugEnabled) {
            logger.debug("RollbackFunction: for transaction: {} executing NestedTransactionFunction on member: {}", txId, member);
        }
        try {
            List list = (List) ex.execute(new NestedTransactionFunction()).getResult();
            result = (Boolean) list.get(0);
        } catch (FunctionException fe) {
            throw new TransactionDataNodeHasDepartedException("Could not Rollback on member:" + member);
        }
    }
    if (isDebugEnabled) {
        logger.debug("RollbackFunction: for transaction: {} returning result: {}", txId, result);
    }
    context.getResultSender().lastResult(result);
}
Also used : ArrayList(java.util.ArrayList) FunctionException(org.apache.geode.cache.execute.FunctionException) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) TransactionDataNodeHasDepartedException(org.apache.geode.cache.TransactionDataNodeHasDepartedException) Execution(org.apache.geode.cache.execute.Execution) TXId(org.apache.geode.internal.cache.TXId) DistributedMember(org.apache.geode.distributed.DistributedMember) ArrayList(java.util.ArrayList) List(java.util.List) Cache(org.apache.geode.cache.Cache)

Aggregations

Cache (org.apache.geode.cache.Cache)1044 Region (org.apache.geode.cache.Region)478 Test (org.junit.Test)476 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)292 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)277 VM (org.apache.geode.test.dunit.VM)264 Host (org.apache.geode.test.dunit.Host)230 AttributesFactory (org.apache.geode.cache.AttributesFactory)229 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)177 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)176 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)164 LocalRegion (org.apache.geode.internal.cache.LocalRegion)153 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)123 ClientCache (org.apache.geode.cache.client.ClientCache)117 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)112 Properties (java.util.Properties)101 CacheException (org.apache.geode.cache.CacheException)101 RegionAttributes (org.apache.geode.cache.RegionAttributes)99 QueryService (org.apache.geode.cache.query.QueryService)95 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)93