Search in sources :

Example 1 with PartitionedRegionObserverAdapter

use of org.apache.geode.internal.cache.partitioned.PartitionedRegionObserverAdapter in project geode by apache.

the class PartitionedRegionDataStoreJUnitTest method testChangeCacheLoaderDuringBucketCreation.

@Test
public void testChangeCacheLoaderDuringBucketCreation() throws Exception {
    final PartitionedRegion pr = (PartitionedRegion) cache.createRegionFactory(RegionShortcut.PARTITION).create("testChangeCacheLoaderDuringBucketCreation");
    // Add an observer which will block bucket creation and wait for a loader to be added
    final CountDownLatch loaderAdded = new CountDownLatch(1);
    final CountDownLatch bucketCreated = new CountDownLatch(1);
    PartitionedRegionObserverHolder.setInstance(new PartitionedRegionObserverAdapter() {

        @Override
        public void beforeAssignBucket(PartitionedRegion partitionedRegion, int bucketId) {
            try {
                // Indicate that the bucket has been created
                bucketCreated.countDown();
                // Wait for the loader to be added. if the synchronization
                // is correct, this would wait for ever because setting the
                // cache loader will wait for this method. So time out after
                // 1 second, which should be good enough to cause a failure
                // if the synchronization is broken.
                loaderAdded.await(1, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                throw new RuntimeException("Interrupted");
            }
        }
    });
    Thread createBuckets = new Thread() {

        public void run() {
            PartitionRegionHelper.assignBucketsToPartitions(pr);
        }
    };
    createBuckets.start();
    CacheLoader loader = new CacheLoader() {

        @Override
        public void close() {
        }

        @Override
        public Object load(LoaderHelper helper) throws CacheLoaderException {
            return null;
        }
    };
    bucketCreated.await();
    pr.getAttributesMutator().setCacheLoader(loader);
    loaderAdded.countDown();
    createBuckets.join();
    // Assert that all buckets have received the cache loader
    for (BucketRegion bucket : pr.getDataStore().getAllLocalBucketRegions()) {
        assertEquals(loader, bucket.getCacheLoader());
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) PartitionedRegionObserverAdapter(org.apache.geode.internal.cache.partitioned.PartitionedRegionObserverAdapter) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 2 with PartitionedRegionObserverAdapter

use of org.apache.geode.internal.cache.partitioned.PartitionedRegionObserverAdapter in project geode by apache.

the class FixedPartitioningTestBase method setPRObserverBeforeCalculateStartingBucketId.

public static void setPRObserverBeforeCalculateStartingBucketId() {
    PartitionedRegion.BEFORE_CALCULATE_STARTING_BUCKET_FLAG = true;
    origObserver = PartitionedRegionObserverHolder.setInstance(new PartitionedRegionObserverAdapter() {

        public void beforeCalculatingStartingBucketId() {
            WaitCriterion wc = new WaitCriterion() {

                String excuse;

                public boolean done() {
                    Region prRoot = PartitionedRegionHelper.getPRRoot(cache);
                    PartitionRegionConfig regionConfig = (PartitionRegionConfig) prRoot.get("#Quarter");
                    if (regionConfig == null) {
                        return false;
                    } else {
                        if (!regionConfig.isFirstDataStoreCreated()) {
                            return true;
                        } else {
                            return false;
                        }
                    }
                }

                public String description() {
                    return excuse;
                }
            };
            Wait.waitForCriterion(wc, 20000, 500, false);
            LogWriterUtils.getLogWriter().info("end of beforeCalculatingStartingBucketId");
        }
    });
}
Also used : WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) PartitionRegionConfig(org.apache.geode.internal.cache.PartitionRegionConfig) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) HARegion(org.apache.geode.internal.cache.HARegion) Region(org.apache.geode.cache.Region) PartitionedRegionObserverAdapter(org.apache.geode.internal.cache.partitioned.PartitionedRegionObserverAdapter)

Aggregations

PartitionedRegionObserverAdapter (org.apache.geode.internal.cache.partitioned.PartitionedRegionObserverAdapter)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 Region (org.apache.geode.cache.Region)1 HARegion (org.apache.geode.internal.cache.HARegion)1 PartitionRegionConfig (org.apache.geode.internal.cache.PartitionRegionConfig)1 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)1 WaitCriterion (org.apache.geode.test.dunit.WaitCriterion)1 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)1 Test (org.junit.Test)1