Search in sources :

Example 1 with PartitionResolver

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

the class QueryDataInconsistencyDUnitTest method createPR.

public void createPR() {
    PartitionResolver testKeyBasedResolver = new QueryAPITestPartitionResolver();
    cache = CacheFactory.getAnyInstance();
    cache.createRegionFactory(RegionShortcut.PARTITION_REDUNDANT).setPartitionAttributes(new PartitionAttributesFactory().setTotalNumBuckets(numOfBuckets).setPartitionResolver(testKeyBasedResolver).create()).create(PartitionedRegionName1);
}
Also used : PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) PartitionResolver(org.apache.geode.cache.PartitionResolver)

Example 2 with PartitionResolver

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

the class GetClientPartitionAttributesCommand method cmdExecute.

@SuppressWarnings("unchecked")
@Override
public void cmdExecute(Message clientMessage, ServerConnection serverConnection, long start) throws IOException, ClassNotFoundException, InterruptedException {
    String regionFullPath = null;
    regionFullPath = clientMessage.getPart(0).getString();
    String errMessage = "";
    if (regionFullPath == null) {
        logger.warn(LocalizedMessage.create(LocalizedStrings.GetClientPartitionAttributes_THE_INPUT_REGION_PATH_IS_NULL));
        errMessage = LocalizedStrings.GetClientPartitionAttributes_THE_INPUT_REGION_PATH_IS_NULL.toLocalizedString();
        writeErrorResponse(clientMessage, MessageType.GET_CLIENT_PARTITION_ATTRIBUTES_ERROR, errMessage.toString(), serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    }
    Region region = serverConnection.getCache().getRegion(regionFullPath);
    if (region == null) {
        logger.warn(LocalizedMessage.create(LocalizedStrings.GetClientPartitionAttributes_REGION_NOT_FOUND_FOR_SPECIFIED_REGION_PATH, regionFullPath));
        errMessage = LocalizedStrings.GetClientPartitionAttributes_REGION_NOT_FOUND.toLocalizedString() + regionFullPath;
        writeErrorResponse(clientMessage, MessageType.GET_CLIENT_PARTITION_ATTRIBUTES_ERROR, errMessage.toString(), serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    }
    try {
        Message responseMsg = serverConnection.getResponseMessage();
        responseMsg.setTransactionId(clientMessage.getTransactionId());
        responseMsg.setMessageType(MessageType.RESPONSE_CLIENT_PARTITION_ATTRIBUTES);
        PartitionedRegion prRgion = (PartitionedRegion) region;
        PartitionResolver partitionResolver = prRgion.getPartitionResolver();
        // MINUMUM PARTS
        int numParts = 2;
        if (partitionResolver != null) {
            numParts++;
        }
        responseMsg.setNumberOfParts(numParts);
        // PART 1
        responseMsg.addObjPart(prRgion.getTotalNumberOfBuckets());
        // PART 2
        if (partitionResolver != null) {
            responseMsg.addObjPart(partitionResolver.getClass().toString().substring(6));
        }
        // PART 3
        String leaderRegionPath = null;
        PartitionedRegion leaderRegion = null;
        String leaderRegionName = prRgion.getColocatedWith();
        if (leaderRegionName != null) {
            Cache cache = prRgion.getCache();
            while (leaderRegionName != null) {
                leaderRegion = (PartitionedRegion) cache.getRegion(leaderRegionName);
                if (leaderRegion.getColocatedWith() == null) {
                    leaderRegionPath = leaderRegion.getFullPath();
                    break;
                } else {
                    leaderRegionName = leaderRegion.getColocatedWith();
                }
            }
        }
        responseMsg.addObjPart(leaderRegionPath);
        responseMsg.send();
        clientMessage.clearParts();
    } catch (Exception e) {
        writeException(clientMessage, e, false, serverConnection);
    } finally {
        serverConnection.setAsTrue(Command.RESPONDED);
    }
}
Also used : LocalizedMessage(org.apache.geode.internal.logging.log4j.LocalizedMessage) Message(org.apache.geode.internal.cache.tier.sockets.Message) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) PartitionResolver(org.apache.geode.cache.PartitionResolver) IOException(java.io.IOException) Cache(org.apache.geode.cache.Cache)

Example 3 with PartitionResolver

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

the class GetClientPartitionAttributesCommand66 method cmdExecute.

@SuppressWarnings("unchecked")
@Override
public void cmdExecute(Message clientMessage, ServerConnection serverConnection, long start) throws IOException, ClassNotFoundException, InterruptedException {
    String regionFullPath = null;
    regionFullPath = clientMessage.getPart(0).getString();
    String errMessage = "";
    if (regionFullPath == null) {
        logger.warn(LocalizedMessage.create(LocalizedStrings.GetClientPartitionAttributes_THE_INPUT_REGION_PATH_IS_NULL));
        errMessage = LocalizedStrings.GetClientPartitionAttributes_THE_INPUT_REGION_PATH_IS_NULL.toLocalizedString();
        writeErrorResponse(clientMessage, MessageType.GET_CLIENT_PARTITION_ATTRIBUTES_ERROR, errMessage.toString(), serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    }
    Region region = serverConnection.getCache().getRegion(regionFullPath);
    if (region == null) {
        logger.warn(LocalizedMessage.create(LocalizedStrings.GetClientPartitionAttributes_REGION_NOT_FOUND_FOR_SPECIFIED_REGION_PATH, regionFullPath));
        errMessage = LocalizedStrings.GetClientPartitionAttributes_REGION_NOT_FOUND.toLocalizedString() + regionFullPath;
        writeErrorResponse(clientMessage, MessageType.GET_CLIENT_PARTITION_ATTRIBUTES_ERROR, errMessage.toString(), serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        return;
    }
    try {
        Message responseMsg = serverConnection.getResponseMessage();
        responseMsg.setTransactionId(clientMessage.getTransactionId());
        responseMsg.setMessageType(MessageType.RESPONSE_CLIENT_PARTITION_ATTRIBUTES);
        if (!(region instanceof PartitionedRegion)) {
            responseMsg.setNumberOfParts(2);
            responseMsg.addObjPart(-1);
            responseMsg.addObjPart(region.getFullPath());
        } else {
            PartitionedRegion prRgion = (PartitionedRegion) region;
            PartitionResolver partitionResolver = prRgion.getPartitionResolver();
            // MINUMUM PARTS
            int numParts = 2;
            if (partitionResolver != null) {
                numParts++;
            }
            if (prRgion.isFixedPartitionedRegion()) {
                numParts++;
            }
            responseMsg.setNumberOfParts(numParts);
            // PART 1
            responseMsg.addObjPart(prRgion.getTotalNumberOfBuckets());
            // PART 2
            String leaderRegionPath = null;
            PartitionedRegion leaderRegion = null;
            String leaderRegionName = prRgion.getColocatedWith();
            if (leaderRegionName != null) {
                Cache cache = prRgion.getCache();
                while (leaderRegionName != null) {
                    leaderRegion = (PartitionedRegion) cache.getRegion(leaderRegionName);
                    if (leaderRegion.getColocatedWith() == null) {
                        leaderRegionPath = leaderRegion.getFullPath();
                        break;
                    } else {
                        leaderRegionName = leaderRegion.getColocatedWith();
                    }
                }
            }
            responseMsg.addObjPart(leaderRegionPath);
            // PART 3
            if (partitionResolver != null) {
                responseMsg.addObjPart(partitionResolver.getClass().toString().substring(6));
            }
            // PART 4
            if (prRgion.isFixedPartitionedRegion()) {
                Set<FixedPartitionAttributes> fpaSet = null;
                if (leaderRegion != null) {
                    fpaSet = PartitionedRegionHelper.getAllFixedPartitionAttributes(leaderRegion);
                } else {
                    fpaSet = PartitionedRegionHelper.getAllFixedPartitionAttributes(prRgion);
                }
                responseMsg.addObjPart(fpaSet);
            }
        }
        responseMsg.send();
        clientMessage.clearParts();
    } catch (Exception e) {
        writeException(clientMessage, e, false, serverConnection);
    } finally {
        serverConnection.setAsTrue(Command.RESPONDED);
    }
}
Also used : FixedPartitionAttributes(org.apache.geode.cache.FixedPartitionAttributes) LocalizedMessage(org.apache.geode.internal.logging.log4j.LocalizedMessage) Message(org.apache.geode.internal.cache.tier.sockets.Message) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) PartitionResolver(org.apache.geode.cache.PartitionResolver) IOException(java.io.IOException) Cache(org.apache.geode.cache.Cache)

Example 4 with PartitionResolver

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

the class ClientMetadataService method getMetaDataVersion.

public byte getMetaDataVersion(Region region, Operation operation, Object key, Object value, Object callbackArg) {
    ClientPartitionAdvisor prAdvisor = this.getClientPartitionAdvisor(region.getFullPath());
    if (prAdvisor == null) {
        return 0;
    }
    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);
        }
    } else {
        bucketId = PartitionedRegionHelper.getHashKey(resolveKey, totalNumberOfBuckets);
    }
    BucketServerLocation66 bsl = (BucketServerLocation66) getPrimaryServerLocation(region, bucketId);
    if (bsl == null) {
        return 0;
    }
    return bsl.getVersion();
}
Also used : EntryOperationImpl(org.apache.geode.internal.cache.EntryOperationImpl) BucketServerLocation66(org.apache.geode.internal.cache.BucketServerLocation66) 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 5 with PartitionResolver

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

the class CacheXmlParser method endPartitionResolver.

/**
   * When a <code>partition-resolver</code> element is finished, the {@link Parameter}s and class
   * names are popped off the stack. The <code>PartitionResolver</code> is instantiated and
   * initialized with the parameters, if appropriate.
   */
private void endPartitionResolver() {
    Declarable d = createDeclarable();
    if (!(d instanceof PartitionResolver)) {
        throw new CacheXmlException(LocalizedStrings.CacheXmlParser_A_0_IS_NOT_AN_INSTANCE_OF_A_1.toLocalizedString(new Object[] { d.getClass().getName(), "PartitionResolver" }));
    }
    PartitionAttributesImpl pai = peekPartitionAttributesImpl(PARTITION_ATTRIBUTES);
    pai.setPartitionResolver((PartitionResolver) d);
}
Also used : FixedPartitionAttributesImpl(org.apache.geode.internal.cache.FixedPartitionAttributesImpl) PartitionAttributesImpl(org.apache.geode.internal.cache.PartitionAttributesImpl) Declarable(org.apache.geode.cache.Declarable) CacheXmlException(org.apache.geode.cache.CacheXmlException) PartitionResolver(org.apache.geode.cache.PartitionResolver)

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