use of org.apache.geode.cache.PartitionResolver in project geode by apache.
the class QueryUsingFunctionContextDUnitTest method createColoPR.
public void createColoPR() {
PartitionResolver testKeyBasedResolver = new QueryAPITestPartitionResolver();
cache = CacheFactory.getAnyInstance();
cache.createRegionFactory(RegionShortcut.PARTITION).setPartitionAttributes(new PartitionAttributesFactory().setTotalNumBuckets(numOfBuckets).setPartitionResolver(testKeyBasedResolver).create()).create(PartitionedRegionName1);
cache.createRegionFactory(RegionShortcut.PARTITION).setPartitionAttributes(new PartitionAttributesFactory().setTotalNumBuckets(numOfBuckets).setPartitionResolver(testKeyBasedResolver).setColocatedWith(PartitionedRegionName1).create()).create(PartitionedRegionName2);
cache.createRegionFactory(RegionShortcut.PARTITION).setPartitionAttributes(new PartitionAttributesFactory().setTotalNumBuckets(numOfBuckets).setPartitionResolver(testKeyBasedResolver).setColocatedWith(PartitionedRegionName2).create()).create(PartitionedRegionName3);
cache.createRegionFactory(RegionShortcut.PARTITION).setPartitionAttributes(new PartitionAttributesFactory().setTotalNumBuckets(numOfBuckets).setPartitionResolver(testKeyBasedResolver).create()).create(// not collocated
PartitionedRegionName4);
cache.createRegionFactory(RegionShortcut.PARTITION).setPartitionAttributes(new PartitionAttributesFactory().setTotalNumBuckets(numOfBuckets).setPartitionResolver(testKeyBasedResolver).setColocatedWith(PartitionedRegionName4).create()).create(// collocated with 4
PartitionedRegionName5);
}
use of org.apache.geode.cache.PartitionResolver in project geode by apache.
the class ClientMetadataService method getResolver.
private PartitionResolver getResolver(Region r, Object key, Object callbackArgument) {
// First choice is one associated with the region
final String regionFullPath = r.getFullPath();
ClientPartitionAdvisor advisor = this.getClientPartitionAdvisor(regionFullPath);
PartitionResolver result = null;
if (advisor != null) {
result = advisor.getPartitionResolver();
}
if (result != null) {
return result;
}
// Second is the key
if (key != null && key instanceof PartitionResolver) {
return (PartitionResolver) key;
}
// Third is the callback argument
if (callbackArgument != null && callbackArgument instanceof PartitionResolver) {
return (PartitionResolver) callbackArgument;
}
// There is no resolver.
return null;
}
use of org.apache.geode.cache.PartitionResolver in project geode by apache.
the class ClientMetadataService method extractBucketID.
private int extractBucketID(Region region, ClientPartitionAdvisor prAdvisor, int totalNumberOfBuckets, Object key) {
int bucketId = -1;
final PartitionResolver resolver = getResolver(region, key, null);
Object resolveKey;
EntryOperation entryOp = null;
if (resolver == null) {
// client has not registered PartitionResolver
// Assuming even PR at server side is not using PartitionResolver
resolveKey = key;
} else {
entryOp = new EntryOperationImpl(region, Operation.FUNCTION_EXECUTION, key, null, null);
resolveKey = resolver.getRoutingObject(entryOp);
if (resolveKey == null) {
throw new IllegalStateException(LocalizedStrings.PartitionedRegionHelper_THE_ROUTINGOBJECT_RETURNED_BY_PARTITIONRESOLVER_IS_NULL.toLocalizedString());
}
}
if (resolver instanceof FixedPartitionResolver) {
if (entryOp == null) {
entryOp = new EntryOperationImpl(region, Operation.FUNCTION_EXECUTION, key, null, null);
}
String partition = ((FixedPartitionResolver) resolver).getPartitionName(entryOp, prAdvisor.getFixedPartitionNames());
if (partition == null) {
Object[] prms = new Object[] { region.getName(), resolver };
throw new IllegalStateException(LocalizedStrings.PartitionedRegionHelper_FOR_REGION_0_PARTITIONRESOLVER_1_RETURNED_PARTITION_NAME_NULL.toLocalizedString(prms));
} else {
bucketId = prAdvisor.assignFixedBucketId(region, partition, resolveKey);
// Do proactive scheduling of metadata fetch
if (bucketId == -1) {
scheduleGetPRMetaData((LocalRegion) region, true);
}
}
} else {
bucketId = PartitionedRegionHelper.getHashKey(resolveKey, totalNumberOfBuckets);
}
return bucketId;
}
use of org.apache.geode.cache.PartitionResolver in project geode by apache.
the class ClientMetadataService method getBucketServerLocation.
public ServerLocation getBucketServerLocation(Region region, Operation operation, Object key, Object value, Object callbackArg) {
ClientPartitionAdvisor prAdvisor = this.getClientPartitionAdvisor(region.getFullPath());
if (prAdvisor == null) {
return null;
}
int totalNumberOfBuckets = prAdvisor.getTotalNumBuckets();
final PartitionResolver resolver = getResolver(region, key, callbackArg);
Object resolveKey;
EntryOperation entryOp = null;
if (resolver == null) {
// client has not registered PartitionResolver
// Assuming even PR at server side is not using PartitionResolver
resolveKey = key;
} else {
entryOp = new EntryOperationImpl(region, operation, key, value, callbackArg);
resolveKey = resolver.getRoutingObject(entryOp);
if (resolveKey == null) {
throw new IllegalStateException(LocalizedStrings.PartitionedRegionHelper_THE_ROUTINGOBJECT_RETURNED_BY_PARTITIONRESOLVER_IS_NULL.toLocalizedString());
}
}
int bucketId;
if (resolver instanceof FixedPartitionResolver) {
if (entryOp == null) {
entryOp = new EntryOperationImpl(region, Operation.FUNCTION_EXECUTION, key, null, null);
}
String partition = ((FixedPartitionResolver) resolver).getPartitionName(entryOp, prAdvisor.getFixedPartitionNames());
if (partition == null) {
Object[] prms = new Object[] { region.getName(), resolver };
throw new IllegalStateException(LocalizedStrings.PartitionedRegionHelper_FOR_REGION_0_PARTITIONRESOLVER_1_RETURNED_PARTITION_NAME_NULL.toLocalizedString(prms));
} else {
bucketId = prAdvisor.assignFixedBucketId(region, partition, resolveKey);
if (bucketId == -1) {
// scheduleGetPRMetaData((LocalRegion)region);
return null;
}
}
} else {
bucketId = PartitionedRegionHelper.getHashKey(resolveKey, totalNumberOfBuckets);
}
ServerLocation bucketServerLocation = getServerLocation(region, operation, bucketId);
ServerLocation location = null;
if (bucketServerLocation != null) {
location = new ServerLocation(bucketServerLocation.getHostName(), bucketServerLocation.getPort());
}
return location;
}
use of org.apache.geode.cache.PartitionResolver in project geode by apache.
the class PRFunctionExecutionDUnitTest method testBucketFilter_1.
/**
* Test bucketFilter functionality
*/
@Test
public void testBucketFilter_1() throws Exception {
final String rName = getUniqueName();
Host host = Host.getHost(0);
final VM accessor = host.getVM(3);
final VM datastore0 = host.getVM(0);
final VM datastore1 = host.getVM(1);
final VM datastore2 = host.getVM(2);
getCache();
accessor.invoke(new SerializableCallable("Create PR") {
public Object call() throws Exception {
PartitionResolver resolver = new BucketFilterPRResolver();
RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 0, resolver);
getCache().createRegion(rName, ra);
return Boolean.TRUE;
}
});
SerializableCallable dataStoreCreate = new SerializableCallable("Create PR with Function Factory") {
public Object call() throws Exception {
PartitionResolver resolver = new BucketFilterPRResolver();
RegionAttributes ra = PartitionedRegionTestHelper.createRegionAttrsForPR(0, 10, resolver);
AttributesFactory raf = new AttributesFactory(ra);
PartitionAttributesImpl pa = new PartitionAttributesImpl();
pa.setAll(ra.getPartitionAttributes());
raf.setPartitionAttributes(pa);
getCache().createRegion(rName, raf.create());
Function function = new TestFunction(true, TestFunction.TEST_FUNCTION_BUCKET_FILTER);
FunctionService.registerFunction(function);
return Boolean.TRUE;
}
};
datastore0.invoke(dataStoreCreate);
datastore1.invoke(dataStoreCreate);
datastore2.invoke(dataStoreCreate);
Object o = accessor.invoke(new SerializableCallable("Create data") {
public Object call() throws Exception {
PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(rName);
for (int i = 0; i < 50; ++i) {
pr.put(i, i);
}
return Boolean.TRUE;
}
});
assertEquals(Boolean.TRUE, o);
o = accessor.invoke(new SerializableCallable("Execute function single filter") {
public Object call() throws Exception {
PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(rName);
Function function = new TestFunction(true, TestFunction.TEST_FUNCTION_BUCKET_FILTER);
FunctionService.registerFunction(function);
InternalExecution dataSet = (InternalExecution) FunctionService.onRegion(pr);
Set<Integer> bucketSet = new HashSet<Integer>();
bucketSet.add(2);
ResultCollector<Integer, List<Integer>> rc = dataSet.withBucketFilter(bucketSet).execute(function);
List<Integer> results = rc.getResult();
assertEquals(bucketSet.size(), results.size());
for (Integer bucket : results) {
bucketSet.remove(bucket);
}
assertTrue(bucketSet.isEmpty());
return Boolean.TRUE;
}
});
assertEquals(Boolean.TRUE, o);
o = accessor.invoke(new SerializableCallable("Execute function multiple filter") {
public Object call() throws Exception {
PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(rName);
Function function = new TestFunction(true, TestFunction.TEST_FUNCTION_BUCKET_FILTER);
FunctionService.registerFunction(function);
InternalExecution dataSet = (InternalExecution) FunctionService.onRegion(pr);
Set<Integer> bucketSet = new HashSet<Integer>();
bucketSet.add(2);
bucketSet.add(3);
ResultCollector<Integer, List<Integer>> rc = dataSet.withBucketFilter(bucketSet).execute(function);
List<Integer> results = rc.getResult();
assertEquals(bucketSet.size(), results.size());
for (Integer bucket : results) {
bucketSet.remove(bucket);
}
assertTrue(bucketSet.isEmpty());
return Boolean.TRUE;
}
});
assertEquals(Boolean.TRUE, o);
o = accessor.invoke(new SerializableCallable("Execute function multiple filter") {
public Object call() throws Exception {
PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(rName);
Function function = new TestFunction(true, TestFunction.TEST_FUNCTION_BUCKET_FILTER);
FunctionService.registerFunction(function);
InternalExecution dataSet = (InternalExecution) FunctionService.onRegion(pr);
Set<Integer> bucketSet = new HashSet<Integer>();
bucketSet.add(1);
bucketSet.add(2);
bucketSet.add(3);
bucketSet.add(0);
bucketSet.add(4);
ResultCollector<Integer, List<Integer>> rc = dataSet.withBucketFilter(bucketSet).execute(function);
List<Integer> results = rc.getResult();
assertEquals(bucketSet.size(), results.size());
for (Integer bucket : results) {
bucketSet.remove(bucket);
}
getCache().getLogger().info("results buckets=" + results);
getCache().getLogger().info("bucketset=" + bucketSet);
assertTrue(bucketSet.isEmpty());
return Boolean.TRUE;
}
});
assertEquals(Boolean.TRUE, o);
}
Aggregations