Search in sources :

Example 11 with PartitionResolver

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);
}
Also used : PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) PartitionResolver(org.apache.geode.cache.PartitionResolver)

Example 12 with PartitionResolver

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;
}
Also used : PartitionResolver(org.apache.geode.cache.PartitionResolver) FixedPartitionResolver(org.apache.geode.cache.FixedPartitionResolver)

Example 13 with PartitionResolver

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;
}
Also used : EntryOperationImpl(org.apache.geode.internal.cache.EntryOperationImpl) FixedPartitionResolver(org.apache.geode.cache.FixedPartitionResolver) PartitionResolver(org.apache.geode.cache.PartitionResolver) FixedPartitionResolver(org.apache.geode.cache.FixedPartitionResolver) EntryOperation(org.apache.geode.cache.EntryOperation)

Example 14 with PartitionResolver

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;
}
Also used : EntryOperationImpl(org.apache.geode.internal.cache.EntryOperationImpl) ServerLocation(org.apache.geode.distributed.internal.ServerLocation) FixedPartitionResolver(org.apache.geode.cache.FixedPartitionResolver) PartitionResolver(org.apache.geode.cache.PartitionResolver) FixedPartitionResolver(org.apache.geode.cache.FixedPartitionResolver) EntryOperation(org.apache.geode.cache.EntryOperation)

Example 15 with PartitionResolver

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);
}
Also used : TestFunction(org.apache.geode.internal.cache.functions.TestFunction) RegionAttributes(org.apache.geode.cache.RegionAttributes) Host(org.apache.geode.test.dunit.Host) PartitionResolver(org.apache.geode.cache.PartitionResolver) IgnoredException(org.apache.geode.test.dunit.IgnoredException) FunctionException(org.apache.geode.cache.execute.FunctionException) Function(org.apache.geode.cache.execute.Function) TestFunction(org.apache.geode.internal.cache.functions.TestFunction) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) PartitionAttributesImpl(org.apache.geode.internal.cache.PartitionAttributesImpl) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Aggregations

PartitionResolver (org.apache.geode.cache.PartitionResolver)16 FixedPartitionResolver (org.apache.geode.cache.FixedPartitionResolver)5 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)5 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)5 PartitionAttributesImpl (org.apache.geode.internal.cache.PartitionAttributesImpl)4 ArrayList (java.util.ArrayList)3 EntryOperation (org.apache.geode.cache.EntryOperation)3 Region (org.apache.geode.cache.Region)3 EntryOperationImpl (org.apache.geode.internal.cache.EntryOperationImpl)3 VM (org.apache.geode.test.dunit.VM)3 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 List (java.util.List)2 AttributesFactory (org.apache.geode.cache.AttributesFactory)2 Cache (org.apache.geode.cache.Cache)2 FixedPartitionAttributes (org.apache.geode.cache.FixedPartitionAttributes)2 RegionAttributes (org.apache.geode.cache.RegionAttributes)2 Function (org.apache.geode.cache.execute.Function)2