use of org.apache.geode.internal.cache.BucketRegion in project geode by apache.
the class RegionValue method testLocalDataSetIndexing.
@Test
public void testLocalDataSetIndexing() {
final CacheSerializableRunnable createPRs = new CacheSerializableRunnable("create prs ") {
public void run2() {
AttributesFactory<Integer, RegionValue> factory = new AttributesFactory<Integer, RegionValue>();
factory.setPartitionAttributes(new PartitionAttributesFactory<Integer, RegionValue>().setRedundantCopies(1).setTotalNumBuckets(8).create());
final PartitionedRegion pr1 = (PartitionedRegion) createRootRegion("pr1", factory.create());
factory = new AttributesFactory<Integer, RegionValue>();
factory.setPartitionAttributes(new PartitionAttributesFactory<Integer, RegionValue>().setRedundantCopies(1).setTotalNumBuckets(8).setColocatedWith(pr1.getName()).create());
final PartitionedRegion pr2 = (PartitionedRegion) createRootRegion("pr2", factory.create());
}
};
final CacheSerializableRunnable createIndexesOnPRs = new CacheSerializableRunnable("create prs ") {
public void run2() {
try {
QueryService qs = getCache().getQueryService();
qs.createIndex("valueIndex1", IndexType.FUNCTIONAL, "e1.value", "/pr1 e1");
qs.createIndex("valueIndex2", IndexType.FUNCTIONAL, "e2.value", "/pr2 e2");
} catch (Exception e) {
org.apache.geode.test.dunit.Assert.fail("Test failed due to Exception in index creation ", e);
}
}
};
final CacheSerializableRunnable execute = new CacheSerializableRunnable("execute function") {
public void run2() {
final PartitionedRegion pr1 = (PartitionedRegion) getRootRegion("pr1");
final PartitionedRegion pr2 = (PartitionedRegion) getRootRegion("pr2");
final Set<Integer> filter = new HashSet<Integer>();
for (int i = 1; i <= 80; i++) {
pr1.put(i, new RegionValue(i));
if (i <= 20) {
pr2.put(i, new RegionValue(i));
if ((i % 5) == 0) {
filter.add(i);
}
}
}
ArrayList<List> result = (ArrayList<List>) FunctionService.onRegion(pr1).withFilter(filter).execute(new FunctionAdapter() {
public void execute(FunctionContext context) {
try {
RegionFunctionContext rContext = (RegionFunctionContext) context;
Region pr1 = rContext.getDataSet();
LocalDataSet localCust = (LocalDataSet) PartitionRegionHelper.getLocalDataForContext(rContext);
Map<String, Region<?, ?>> colocatedRegions = PartitionRegionHelper.getColocatedRegions(pr1);
Map<String, Region<?, ?>> localColocatedRegions = PartitionRegionHelper.getLocalColocatedRegions(rContext);
Region pr2 = colocatedRegions.get("/pr2");
LocalDataSet localOrd = (LocalDataSet) localColocatedRegions.get("/pr2");
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
QueryService qs = pr1.getCache().getQueryService();
DefaultQuery query = (DefaultQuery) qs.newQuery("select distinct e1.value from /pr1 e1, /pr2 e2 where e1.value=e2.value");
GemFireCacheImpl.getInstance().getLogger().fine(" Num BUCKET SET: " + localCust.getBucketSet());
GemFireCacheImpl.getInstance().getLogger().fine("VALUES FROM PR1 bucket:");
for (Integer bId : localCust.getBucketSet()) {
BucketRegion br = ((PartitionedRegion) pr1).getDataStore().getLocalBucketById(bId);
String val = "";
for (Object e : br.values()) {
val += (e + ",");
}
GemFireCacheImpl.getInstance().getLogger().fine(": " + val);
}
GemFireCacheImpl.getInstance().getLogger().fine("VALUES FROM PR2 bucket:");
for (Integer bId : localCust.getBucketSet()) {
BucketRegion br = ((PartitionedRegion) pr2).getDataStore().getLocalBucketById(bId);
String val = "";
for (Object e : br.values()) {
val += (e + ",");
}
GemFireCacheImpl.getInstance().getLogger().fine(": " + val);
}
SelectResults r = (SelectResults) localCust.executeQuery(query, null, localCust.getBucketSet());
GemFireCacheImpl.getInstance().getLogger().fine("Result :" + r.asList());
Assert.assertTrue(observer.isIndexesUsed);
pr1.getCache().getLogger().fine("Index Used: " + observer.numIndexesUsed());
Assert.assertTrue(2 == observer.numIndexesUsed());
context.getResultSender().lastResult((Serializable) r.asList());
} catch (Exception e) {
context.getResultSender().lastResult(Boolean.TRUE);
}
}
@Override
public String getId() {
return "ok";
}
@Override
public boolean optimizeForWrite() {
return false;
}
}).getResult();
int numResults = 0;
for (List oneNodeResult : result) {
GemFireCacheImpl.getInstance().getLogger().fine("Result :" + numResults + " oneNodeResult.size(): " + oneNodeResult.size() + " oneNodeResult :" + oneNodeResult);
numResults = +oneNodeResult.size();
}
Assert.assertTrue(10 == numResults);
}
};
dataStore1.invoke(createPRs);
dataStore2.invoke(createPRs);
dataStore1.invoke(createIndexesOnPRs);
dataStore1.invoke(execute);
}
use of org.apache.geode.internal.cache.BucketRegion in project geode by apache.
the class QueryDataDUnitTest method getLocalDataSet.
private List<String> getLocalDataSet(final String region) {
PartitionedRegion partitionedRegion = PartitionedRegionHelper.getPartitionedRegion(region, this.managementTestRule.getCache());
Set<BucketRegion> localPrimaryBucketRegions = partitionedRegion.getDataStore().getAllLocalPrimaryBucketRegions();
List<String> allPrimaryValues = new ArrayList<>();
for (BucketRegion bucketRegion : localPrimaryBucketRegions) {
for (Object value : bucketRegion.values()) {
allPrimaryValues.add((String) value);
}
}
return allPrimaryValues;
}
use of org.apache.geode.internal.cache.BucketRegion in project geode by apache.
the class ResourceManagerDUnitTest method doOpDuringBucketRemove.
private void doOpDuringBucketRemove(final OpDuringBucketRemove op) {
final String[] regionPath = new String[] { getUniqueName() + "_PR_0" };
final int[] numBuckets = new int[] { 1 };
final int[] redundantCopies = new int[] { 1 };
// localMaxMemory config to use for three members
final int[] localMaxMemory = new int[] { 100, 100 };
final Integer KEY = Integer.valueOf(69);
// bucketKeys to use for making one bucket
final Integer[] bucketKeys = new Integer[] { KEY };
createRegion(Host.getHost(0).getVM(0), regionPath[0], localMaxMemory[0], numBuckets[0], redundantCopies[0]);
createRegion(Host.getHost(0).getVM(1), regionPath[0], localMaxMemory[1], numBuckets[0], redundantCopies[0]);
final String VALUE = "doOpDuringBucketRemove.VALUE";
createBuckets(0, regionPath[0], bucketKeys, VALUE);
// identify the members and their config values
final InternalDistributedMember[] members = new InternalDistributedMember[2];
final long[] memberSizes = new long[members.length];
final int[] memberBucketCounts = new int[members.length];
final int[] memberPrimaryCounts = new int[members.length];
fillValidationArrays(members, memberSizes, memberBucketCounts, memberPrimaryCounts, regionPath[0]);
int primaryVM = -1;
int otherVM = -1;
for (int i = 0; i < memberPrimaryCounts.length; i++) {
if (memberPrimaryCounts[i] == 0) {
otherVM = i;
} else if (memberPrimaryCounts[i] == 1) {
// found the primary
primaryVM = i;
}
}
assertTrue(primaryVM > -1);
assertTrue(otherVM > -1);
assertTrue(primaryVM != otherVM);
final int finalOtherVM = otherVM;
Host.getHost(0).getVM(otherVM).invoke(new SerializableRunnable() {
public void run() {
PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(regionPath[0]);
Bucket bucket = pr.getRegionAdvisor().getBucket(0);
assertTrue("Target member is not hosting bucket to remove", bucket.isHosting());
assertNotNull("Bucket is null on target member", bucket);
assertNotNull("BucketRegion is null on target member", bucket.getBucketAdvisor().getProxyBucketRegion().getHostedBucketRegion());
EntrySnapshot re = (EntrySnapshot) pr.getEntry(KEY);
assertEquals(true, re.wasInitiallyLocal());
assertEquals(false, re.isLocal());
assertEquals(VALUE, re.getValue());
}
});
Host.getHost(0).getVM(otherVM).invoke(new SerializableRunnable() {
public void run() {
final PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(regionPath[0]);
final boolean[] invoked = new boolean[] { false };
final PartitionedRegionDataStore prds = pr.getDataStore();
prds.setBucketReadHook(new Runnable() {
public void run() {
invoked[0] = true;
logger.debug("In bucketReadHook");
assertTrue(prds.removeBucket(0, false));
}
});
try {
Bucket bucket = pr.getRegionAdvisor().getBucket(0);
assertTrue("Target member is not hosting bucket to remove", bucket.isHosting());
assertNotNull("Bucket is null on target member", bucket);
assertNotNull("BucketRegion is null on target member", bucket.getBucketAdvisor().getProxyBucketRegion().getHostedBucketRegion());
assertEquals(false, invoked[0]);
op.runit(pr, KEY, VALUE);
assertEquals(true, invoked[0]);
} finally {
prds.setBucketReadHook(null);
}
}
});
Host.getHost(0).getVM(otherVM).invoke(new SerializableRunnable() {
public void run() {
PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(regionPath[0]);
Bucket bucket = pr.getRegionAdvisor().getBucket(0);
BucketRegion bucketRegion = bucket.getBucketAdvisor().getProxyBucketRegion().getHostedBucketRegion();
assertFalse("Target member is still hosting removed bucket. Bucket:" + bucket + " Advisor state:" + bucket.getBucketAdvisor(), bucket.isHosting());
assertNull(bucketRegion);
// assertTrue(bucketRegion == null || bucketRegion.isDestroyed());
EntrySnapshot re = (EntrySnapshot) pr.getEntry(KEY);
assertEquals(false, re.wasInitiallyLocal());
assertEquals(false, re.isLocal());
assertEquals(VALUE, re.getValue());
}
});
}
use of org.apache.geode.internal.cache.BucketRegion in project geode by apache.
the class ResourceManagerDUnitTest method testMoveBucket.
/**
* Creates a bucket on two members. Then brings up a third member and moves the non-primary bucket
* to it.
*/
@Test
public void testMoveBucket() {
final String[] regionPath = new String[] { getUniqueName() + "-PR-0" };
final int numBuckets = 1;
final int redundantCopies = 1;
final int localMaxMemory = 100;
// create the PartitionedRegion on the first two members
createRegion(Host.getHost(0).getVM(0), regionPath[0], localMaxMemory, numBuckets, redundantCopies);
createRegion(Host.getHost(0).getVM(1), regionPath[0], localMaxMemory, numBuckets, redundantCopies);
// create the bucket on the first two members
final Integer bucketKey = Integer.valueOf(0);
// 2 MB in size
final byte[] value = new byte[1];
createBucket(0, regionPath[0], bucketKey, value);
// identify the primaryVM and otherVM
final InternalDistributedMember[] members = new InternalDistributedMember[2];
final long[] memberSizes = new long[members.length];
final int[] memberBucketCounts = new int[members.length];
final int[] memberPrimaryCounts = new int[members.length];
fillValidationArrays(members, memberSizes, memberBucketCounts, memberPrimaryCounts, regionPath[0]);
int primaryVM = -1;
int otherVM = -1;
for (int i = 0; i < memberPrimaryCounts.length; i++) {
if (memberPrimaryCounts[i] == 0) {
otherVM = i;
} else if (memberPrimaryCounts[i] == 1) {
// found the primary
primaryVM = i;
}
}
assertTrue(primaryVM > -1);
assertTrue(otherVM > -1);
assertTrue(primaryVM != otherVM);
final int finalOtherVM = otherVM;
// make sure bucket exists on otherVM
Host.getHost(0).getVM(otherVM).invoke(new SerializableRunnable() {
public void run() {
PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(regionPath[0]);
Bucket bucket = pr.getRegionAdvisor().getBucket(0);
assertNotNull("Bucket is null on SRC member", bucket);
BucketRegion bucketRegion = bucket.getBucketAdvisor().getProxyBucketRegion().getHostedBucketRegion();
assertTrue("SRC member is not hosting bucket", bucket.isHosting());
assertNotNull("BucketRegion is null on SRC member", bucketRegion);
int redundancy = bucket.getBucketAdvisor().getBucketRedundancy();
assertEquals("SRC member reports redundancy " + redundancy, redundantCopies, redundancy);
}
});
// create newVM to move bucket to
final int finalNewVM = 2;
createRegion(Host.getHost(0).getVM(finalNewVM), regionPath[0], localMaxMemory, numBuckets, redundantCopies);
// initiate moveBucket to move from otherVM to newVM
boolean movedBucket = ((Boolean) Host.getHost(0).getVM(finalNewVM).invoke(new SerializableCallable() {
public Object call() {
InternalDistributedMember recipient = members[finalOtherVM];
PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(regionPath[0]);
return pr.getDataStore().moveBucket(0, recipient, true);
}
})).booleanValue();
assertTrue("Failed in call to moveBucket", movedBucket);
// validate that otherVM no longer hosts bucket
Host.getHost(0).getVM(otherVM).invoke(new SerializableRunnable() {
public void run() {
PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(regionPath[0]);
Bucket bucket = pr.getRegionAdvisor().getBucket(0);
assertFalse("SRC member is still hosting moved bucket", bucket.isHosting());
BucketRegion bucketRegion = bucket.getBucketAdvisor().getProxyBucketRegion().getHostedBucketRegion();
assertNull("BucketRegion is not null on SRC member", bucketRegion);
}
});
// validate that newVM now hosts bucket
Host.getHost(0).getVM(finalNewVM).invoke(new SerializableRunnable() {
public void run() {
PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(regionPath[0]);
Bucket bucket = pr.getRegionAdvisor().getBucket(0);
assertNotNull("Bucket is null on DST member", bucket);
BucketRegion bucketRegion = bucket.getBucketAdvisor().getProxyBucketRegion().getHostedBucketRegion();
assertTrue("DST member is not hosting bucket", bucket.isHosting());
assertNotNull("BucketRegion is null on DST member", bucketRegion);
assertEquals(redundantCopies, bucket.getBucketAdvisor().getBucketRedundancy());
}
});
}
use of org.apache.geode.internal.cache.BucketRegion in project geode by apache.
the class ResourceManagerDUnitTest method testCreateRedundantBucket.
/**
* Creates a bucket on two members. Then brings up a third member and creates an extra redundant
* copy of the bucket on it.
*/
@Test
public void testCreateRedundantBucket() {
final String[] regionPath = new String[] { getUniqueName() + "-PR-0" };
final int numBuckets = 1;
final int redundantCopies = 1;
final int localMaxMemory = 100;
// create the PartitionedRegion on the first two members
createRegion(Host.getHost(0).getVM(0), regionPath[0], localMaxMemory, numBuckets, redundantCopies);
createRegion(Host.getHost(0).getVM(1), regionPath[0], localMaxMemory, numBuckets, redundantCopies);
// create the bucket on the first two members
final Integer bucketKey = Integer.valueOf(0);
// 2 MB in size
final byte[] value = new byte[1];
createBucket(0, regionPath[0], bucketKey, value);
// identify the primaryVM and otherVM
final InternalDistributedMember[] members = new InternalDistributedMember[2];
final long[] memberSizes = new long[members.length];
final int[] memberBucketCounts = new int[members.length];
final int[] memberPrimaryCounts = new int[members.length];
fillValidationArrays(members, memberSizes, memberBucketCounts, memberPrimaryCounts, regionPath[0]);
int primaryVM = -1;
int otherVM = -1;
for (int i = 0; i < memberPrimaryCounts.length; i++) {
if (memberPrimaryCounts[i] == 0) {
otherVM = i;
} else if (memberPrimaryCounts[i] == 1) {
// found the primary
primaryVM = i;
}
}
assertTrue(primaryVM > -1);
assertTrue(otherVM > -1);
assertTrue(primaryVM != otherVM);
final int finalOtherVM = otherVM;
// make sure bucket exists on otherVM
Host.getHost(0).getVM(otherVM).invoke(new SerializableRunnable() {
public void run() {
PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(regionPath[0]);
Bucket bucket = pr.getRegionAdvisor().getBucket(0);
assertNotNull("Bucket is null on SRC member", bucket);
BucketRegion bucketRegion = bucket.getBucketAdvisor().getProxyBucketRegion().getHostedBucketRegion();
assertTrue("SRC member is not hosting bucket", bucket.isHosting());
assertNotNull("BucketRegion is null on SRC member", bucketRegion);
int redundancy = bucket.getBucketAdvisor().getBucketRedundancy();
assertEquals("SRC member reports redundancy " + redundancy, redundantCopies, redundancy);
}
});
// create newVM to create extra redundant bucket on
final int finalNewVM = 2;
createRegion(Host.getHost(0).getVM(finalNewVM), regionPath[0], localMaxMemory, numBuckets, redundantCopies);
// create an extra redundant bucket on finalNewVM
Host.getHost(0).getVM(finalNewVM).invoke(new SerializableRunnable() {
public void run() {
PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(regionPath[0]);
assertEquals(CreateBucketResult.CREATED, pr.getDataStore().createRedundantBucket(0, false, new InternalDistributedMember()));
Bucket bucket = pr.getRegionAdvisor().getBucket(0);
assertNotNull("Bucket is null on DST member", bucket);
BucketRegion bucketRegion = bucket.getBucketAdvisor().getProxyBucketRegion().getHostedBucketRegion();
assertTrue("DST member is not hosting bucket", bucket.isHosting());
assertNotNull("BucketRegion is null on DST member", bucketRegion);
assertEquals(redundantCopies + 1, bucket.getBucketAdvisor().getBucketRedundancy());
}
});
}
Aggregations