Search in sources :

Example 11 with RebalanceOperation

use of org.apache.geode.cache.control.RebalanceOperation in project geode by apache.

the class RebalanceOperationDUnitTest method testCancelOperation.

@Test
public void testCancelOperation() {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    SerializableRunnable createPrRegion = new SerializableRunnable("createRegion") {

        public void run() {
            Cache cache = getCache();
            AttributesFactory attr = new AttributesFactory();
            PartitionAttributesFactory paf = new PartitionAttributesFactory();
            paf.setRedundantCopies(1);
            paf.setRecoveryDelay(-1);
            paf.setStartupRecoveryDelay(-1);
            PartitionAttributes prAttr = paf.create();
            attr.setPartitionAttributes(prAttr);
            cache.createRegion("region1", attr.create());
        }
    };
    // Create the region in only 1 VM
    vm0.invoke(createPrRegion);
    // Create some buckets
    vm0.invoke(new SerializableRunnable("createSomeBuckets") {

        public void run() {
            Cache cache = getCache();
            Region region = cache.getRegion("region1");
            region.put(Integer.valueOf(1), "A");
        }
    });
    SerializableRunnable checkLowRedundancy = new SerializableRunnable("checkLowRedundancy") {

        public void run() {
            Cache cache = getCache();
            Region region = cache.getRegion("region1");
            PartitionRegionInfo details = PartitionRegionHelper.getPartitionRegionInfo(region);
            assertEquals(1, details.getCreatedBucketCount());
            assertEquals(0, details.getActualRedundantCopies());
            assertEquals(1, details.getLowRedundancyBucketCount());
        }
    };
    // make sure we can tell that the buckets have low redundancy
    vm0.invoke(checkLowRedundancy);
    // Create the region in the other VM (should have no effect)
    vm1.invoke(createPrRegion);
    // Make sure we still have low redundancy
    vm0.invoke(checkLowRedundancy);
    // Now do a rebalance, but cancel it in the middle
    vm0.invoke(new SerializableCallable("D rebalance") {

        public Object call() throws Exception {
            GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
            InternalResourceManager manager = cache.getInternalResourceManager();
            final CountDownLatch rebalancingCancelled = new CountDownLatch(1);
            final CountDownLatch rebalancingFinished = new CountDownLatch(1);
            InternalResourceManager.setResourceObserver(new ResourceObserverAdapter() {

                @Override
                public void rebalancingOrRecoveryStarted(Region region) {
                    try {
                        rebalancingCancelled.await();
                    } catch (InterruptedException e) {
                        Thread.currentThread().interrupt();
                    }
                }

                @Override
                public void rebalancingOrRecoveryFinished(Region region) {
                    rebalancingFinished.countDown();
                }
            });
            RebalanceOperation op = manager.createRebalanceFactory().start();
            assertFalse(op.isCancelled());
            assertFalse(op.isDone());
            assertEquals(Collections.singleton(op), manager.getRebalanceOperations());
            try {
                op.getResults(5, TimeUnit.SECONDS);
                fail("Should have received a timeout exception");
            } catch (TimeoutException expected) {
            }
            assertTrue(op.cancel());
            rebalancingCancelled.countDown();
            assertTrue(op.isCancelled());
            assertTrue(op.isDone());
            rebalancingFinished.await();
            try {
                op.getResults(60, TimeUnit.SECONDS);
                fail("Should have received a cancellation exception");
            } catch (CancellationException expected) {
            }
            assertEquals(Collections.emptySet(), manager.getRebalanceOperations());
            return null;
        }
    });
    // We should still have low redundancy, because the rebalancing was cancelled
    vm0.invoke(checkLowRedundancy);
}
Also used : RebalanceOperation(org.apache.geode.cache.control.RebalanceOperation) 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) TimeoutException(java.util.concurrent.TimeoutException) CancellationException(java.util.concurrent.CancellationException) CacheWriterException(org.apache.geode.cache.CacheWriterException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CancellationException(java.util.concurrent.CancellationException) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) BucketRegion(org.apache.geode.internal.cache.BucketRegion) Region(org.apache.geode.cache.Region) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) PartitionRegionInfo(org.apache.geode.cache.partition.PartitionRegionInfo) Cache(org.apache.geode.cache.Cache) TimeoutException(java.util.concurrent.TimeoutException) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Example 12 with RebalanceOperation

use of org.apache.geode.cache.control.RebalanceOperation in project geode by apache.

the class RollingUpgrade2DUnitTest method rebalance.

public static void rebalance(Object cache) throws Exception {
    RebalanceOperation op = ((GemFireCache) cache).getResourceManager().createRebalanceFactory().start();
    // Wait until the rebalance is completex
    RebalanceResults results = op.getResults();
    Method getTotalTimeMethod = results.getClass().getMethod("getTotalTime");
    getTotalTimeMethod.setAccessible(true);
    System.out.println("Took " + results.getTotalTime() + " milliseconds\n");
    System.out.println("Transfered " + results.getTotalBucketTransferBytes() + "bytes\n");
}
Also used : RebalanceOperation(org.apache.geode.cache.control.RebalanceOperation) Method(java.lang.reflect.Method) RebalanceResults(org.apache.geode.cache.control.RebalanceResults) GemFireCache(org.apache.geode.cache.GemFireCache)

Example 13 with RebalanceOperation

use of org.apache.geode.cache.control.RebalanceOperation in project geode by apache.

the class RebalanceFunction method execute.

@Override
public void execute(FunctionContext context) {
    RebalanceOperation op = null;
    String[] str = new String[0];
    Cache cache = CacheFactory.getAnyInstance();
    ResourceManager manager = cache.getResourceManager();
    Object[] args = (Object[]) context.getArguments();
    String simulate = ((String) args[0]);
    Set<String> includeRegionNames = (Set<String>) args[1];
    Set<String> excludeRegionNames = (Set<String>) args[2];
    RebalanceFactory rbFactory = manager.createRebalanceFactory();
    rbFactory.excludeRegions(excludeRegionNames);
    rbFactory.includeRegions(includeRegionNames);
    RebalanceResults results = null;
    if (simulate.equals("true")) {
        op = rbFactory.simulate();
    } else {
        op = rbFactory.start();
    }
    try {
        results = op.getResults();
        logger.info("Starting RebalanceFunction got results = {}", results);
        StringBuilder str1 = new StringBuilder();
        str1.append(results.getTotalBucketCreateBytes() + "," + results.getTotalBucketCreateTime() + "," + results.getTotalBucketCreatesCompleted() + "," + results.getTotalBucketTransferBytes() + "," + results.getTotalBucketTransferTime() + "," + results.getTotalBucketTransfersCompleted() + "," + results.getTotalPrimaryTransferTime() + "," + results.getTotalPrimaryTransfersCompleted() + "," + results.getTotalTime() + ",");
        Set<PartitionRebalanceInfo> regns1 = results.getPartitionRebalanceDetails();
        Iterator it = regns1.iterator();
        while (it.hasNext()) {
            PartitionRebalanceInfo rgn = (PartitionRebalanceInfo) it.next();
            str1.append(rgn.getRegionPath() + ",");
        }
        logger.info("Starting RebalanceFunction str1={}", str1);
        context.getResultSender().lastResult(str1.toString());
    } catch (CancellationException e) {
        logger.info("Starting RebalanceFunction CancellationException: ", e.getMessage(), e);
        context.getResultSender().lastResult("CancellationException1 " + e.getMessage());
    } catch (InterruptedException e) {
        logger.info("Starting RebalanceFunction InterruptedException: {}", e.getMessage(), e);
        context.getResultSender().lastResult("InterruptedException2 " + e.getMessage());
    }
}
Also used : RebalanceOperation(org.apache.geode.cache.control.RebalanceOperation) Set(java.util.Set) PartitionRebalanceInfo(org.apache.geode.cache.partition.PartitionRebalanceInfo) ResourceManager(org.apache.geode.cache.control.ResourceManager) RebalanceFactory(org.apache.geode.cache.control.RebalanceFactory) CancellationException(java.util.concurrent.CancellationException) Iterator(java.util.Iterator) RebalanceResults(org.apache.geode.cache.control.RebalanceResults) Cache(org.apache.geode.cache.Cache)

Example 14 with RebalanceOperation

use of org.apache.geode.cache.control.RebalanceOperation in project geode by apache.

the class TestFunction method executeWithLastResult.

private void executeWithLastResult(FunctionContext context) {
    RegionFunctionContext rfContext = (RegionFunctionContext) context;
    final PartitionedRegion pr = (PartitionedRegion) rfContext.getDataSet();
    ResourceManager resMan = pr.getCache().getResourceManager();
    RebalanceFactory factory = resMan.createRebalanceFactory();
    RebalanceOperation rebalanceOp = factory.start();
    try {
        RebalanceResults rebalanceResults = rebalanceOp.getResults();
    } catch (CancellationException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    context.getResultSender().lastResult((Serializable) context.getArguments());
}
Also used : RebalanceFactory(org.apache.geode.cache.control.RebalanceFactory) RebalanceOperation(org.apache.geode.cache.control.RebalanceOperation) CancellationException(java.util.concurrent.CancellationException) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) RegionFunctionContext(org.apache.geode.cache.execute.RegionFunctionContext) ResourceManager(org.apache.geode.cache.control.ResourceManager) RebalanceResults(org.apache.geode.cache.control.RebalanceResults)

Example 15 with RebalanceOperation

use of org.apache.geode.cache.control.RebalanceOperation in project geode by apache.

the class LuceneQueriesAccessorBase method rebalanceRegion.

protected void rebalanceRegion(VM vm) {
    // Do a rebalance
    vm.invoke(() -> {
        RebalanceOperation op = getCache().getResourceManager().createRebalanceFactory().start();
        RebalanceResults results = op.getResults();
    });
}
Also used : RebalanceOperation(org.apache.geode.cache.control.RebalanceOperation) RebalanceResults(org.apache.geode.cache.control.RebalanceResults)

Aggregations

RebalanceOperation (org.apache.geode.cache.control.RebalanceOperation)16 RebalanceResults (org.apache.geode.cache.control.RebalanceResults)11 CancellationException (java.util.concurrent.CancellationException)7 Cache (org.apache.geode.cache.Cache)7 ResourceManager (org.apache.geode.cache.control.ResourceManager)7 Host (org.apache.geode.test.dunit.Host)7 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)7 VM (org.apache.geode.test.dunit.VM)7 RebalanceFactory (org.apache.geode.cache.control.RebalanceFactory)6 Region (org.apache.geode.cache.Region)5 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)5 Test (org.junit.Test)5 AttributesFactory (org.apache.geode.cache.AttributesFactory)4 PartitionAttributes (org.apache.geode.cache.PartitionAttributes)4 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)4 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)4 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)3 TimeoutException (java.util.concurrent.TimeoutException)2 CacheException (org.apache.geode.cache.CacheException)2 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)2