use of org.apache.geode.internal.cache.control.InternalResourceManager.ResourceObserverAdapter 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);
}
Aggregations