Search in sources :

Example 6 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 7 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 8 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 9 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 10 with PartitionResolver

use of org.apache.geode.cache.PartitionResolver in project geode by apache.

the class CacheXmlGenerator method generate.

/**
   * Generates XML for a <code>PartitionAttributes</code>
   */
private void generate(PartitionAttributes pa) throws SAXException {
    AttributesImpl atts = new AttributesImpl();
    if (generateDefaults() || pa.getRedundantCopies() != 0)
        atts.addAttribute("", "", PARTITION_REDUNDANT_COPIES, "", String.valueOf(pa.getRedundantCopies()));
    if (this.version.compareTo(CacheXmlVersion.GEMFIRE_5_1) >= 0) {
        if (generateDefaults() || pa.getLocalMaxMemory() != ((PartitionAttributesImpl) pa).getLocalMaxMemoryDefault())
            atts.addAttribute("", "", LOCAL_MAX_MEMORY, "", String.valueOf(pa.getLocalMaxMemory()));
        if (generateDefaults() || pa.getTotalMaxMemory() != PartitionAttributesFactory.GLOBAL_MAX_MEMORY_DEFAULT)
            atts.addAttribute("", "", TOTAL_MAX_MEMORY, "", String.valueOf(pa.getTotalMaxMemory()));
        if (generateDefaults() || pa.getTotalNumBuckets() != PartitionAttributesFactory.GLOBAL_MAX_BUCKETS_DEFAULT)
            atts.addAttribute("", "", TOTAL_NUM_BUCKETS, "", String.valueOf(pa.getTotalNumBuckets()));
    }
    if (this.version.compareTo(CacheXmlVersion.GEMFIRE_5_8) >= 0) {
        if (pa.getColocatedWith() != null)
            atts.addAttribute("", "", PARTITION_COLOCATED_WITH, "", pa.getColocatedWith());
    }
    if (this.version.compareTo(CacheXmlVersion.GEMFIRE_6_0) >= 0) {
        if (generateDefaults() || pa.getRecoveryDelay() != PartitionAttributesFactory.RECOVERY_DELAY_DEFAULT)
            atts.addAttribute("", "", RECOVERY_DELAY, "", String.valueOf(pa.getRecoveryDelay()));
        if (generateDefaults() || pa.getStartupRecoveryDelay() != PartitionAttributesFactory.STARTUP_RECOVERY_DELAY_DEFAULT)
            atts.addAttribute("", "", STARTUP_RECOVERY_DELAY, "", String.valueOf(pa.getStartupRecoveryDelay()));
    }
    if (!generateDefaults() && atts.getLength() == 0 && pa.getPartitionResolver() == null && pa.getPartitionListeners().length == 0 && (pa.getFixedPartitionAttributes() == null || pa.getFixedPartitionAttributes().isEmpty())) {
        return;
    }
    handler.startElement("", PARTITION_ATTRIBUTES, PARTITION_ATTRIBUTES, atts);
    if (this.version.compareTo(CacheXmlVersion.GEMFIRE_5_8) >= 0) {
        PartitionResolver rr = pa.getPartitionResolver();
        if (rr != null) {
            generate(PARTITION_RESOLVER, rr);
        }
    }
    if (this.version.compareTo(CacheXmlVersion.GEMFIRE_6_1) >= 0) {
        PartitionListener[] listeners = pa.getPartitionListeners();
        for (int i = 0; i < listeners.length; i++) {
            PartitionListener listener = listeners[i];
            if (listener != null) {
                generate(PARTITION_LISTENER, listener);
            }
        }
    }
    if (this.version.compareTo(CacheXmlVersion.GEMFIRE_6_6) >= 0) {
        List<FixedPartitionAttributes> staticAttrs = pa.getFixedPartitionAttributes();
        if (staticAttrs != null) {
            generateFixedPartitionAttributes(FIXED_PARTITION_ATTRIBUTES, staticAttrs);
        }
    }
    if (this.version.compareTo(CacheXmlVersion.GEMFIRE_5_1) < 0) {
        Properties p = pa.getLocalProperties();
        generate(p, LOCAL_PROPERTIES);
        p = pa.getGlobalProperties();
        generate(p, GLOBAL_PROPERTIES);
    }
    handler.endElement("", PARTITION_ATTRIBUTES, PARTITION_ATTRIBUTES);
}
Also used : PartitionListener(org.apache.geode.cache.partition.PartitionListener) DiskWriteAttributesImpl(org.apache.geode.internal.cache.DiskWriteAttributesImpl) PartitionAttributesImpl(org.apache.geode.internal.cache.PartitionAttributesImpl) AttributesImpl(org.xml.sax.helpers.AttributesImpl) PartitionAttributesImpl(org.apache.geode.internal.cache.PartitionAttributesImpl) FixedPartitionAttributes(org.apache.geode.cache.FixedPartitionAttributes) PartitionResolver(org.apache.geode.cache.PartitionResolver) Properties(java.util.Properties)

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