Search in sources :

Example 1 with FixedPartitionResolver

use of org.apache.geode.cache.FixedPartitionResolver 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 2 with FixedPartitionResolver

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

the class PartitionedRegionHelper method getHashKey.

/**
   * Runs hashCode() on given key/routing object producing a long value and then finds absolute
   * value of the modulus with bucketSize. For better key distribution, possibly use MD5 or SHA or
   * any unique ID generator for the hash function.
   * 
   * @param event entry event created for this entry operation; can be null
   * @param pr the partitioned region on which to operate
   * @param operation operation
   * @param key the key on which to determine the hash key
   * @param callbackArgument the callbackArgument is passed to <code>PartitionResolver</code> to get
   *        Routing object
   * @return the bucket id the key/routing object hashes to
   */
private static int getHashKey(EntryOperation event, PartitionedRegion pr, Operation operation, Object key, Object value, Object callbackArgument) {
    // avoid creating EntryOperation if there is no resolver
    if (event != null) {
        pr = (PartitionedRegion) event.getRegion();
        key = event.getKey();
        callbackArgument = event.getCallbackArgument();
    }
    PartitionResolver resolver = getResolver(pr, key, callbackArgument);
    Object resolveKey = null;
    if (pr.isFixedPartitionedRegion()) {
        String partition = null;
        if (resolver instanceof FixedPartitionResolver) {
            Map<String, Integer[]> partitionMap = pr.getPartitionsMap();
            if (event == null) {
                event = new EntryOperationImpl(pr, operation, key, value, callbackArgument);
            }
            partition = ((FixedPartitionResolver) resolver).getPartitionName(event, partitionMap.keySet());
            if (partition == null) {
                Object[] prms = new Object[] { pr.getName(), resolver };
                throw new IllegalStateException(LocalizedStrings.PartitionedRegionHelper_FOR_REGION_0_PARTITIONRESOLVER_1_RETURNED_PARTITION_NAME_NULL.toLocalizedString(prms));
            }
            Integer[] bucketArray = partitionMap.get(partition);
            if (bucketArray == null) {
                Object[] prms = new Object[] { pr.getName(), partition };
                throw new PartitionNotAvailableException(LocalizedStrings.PartitionedRegionHelper_FOR_FIXED_PARTITIONED_REGION_0_FIXED_PARTITION_1_IS_NOT_AVAILABLE_ON_ANY_DATASTORE.toLocalizedString(prms));
            }
            int numBukets = bucketArray[1];
            resolveKey = (numBukets == 1) ? partition : resolver.getRoutingObject(event);
        } else if (resolver == null) {
            throw new IllegalStateException(LocalizedStrings.PartitionedRegionHelper_FOR_FIXED_PARTITIONED_REGION_0_FIXED_PARTITION_RESOLVER_IS_NOT_AVAILABLE.toString(pr.getName()));
        } else if (!(resolver instanceof FixedPartitionResolver)) {
            Object[] prms = new Object[] { pr.getName(), resolver };
            throw new IllegalStateException(LocalizedStrings.PartitionedRegionHelper_FOR_FIXED_PARTITIONED_REGION_0_RESOLVER_DEFINED_1_IS_NOT_AN_INSTANCE_OF_FIXEDPARTITIONRESOLVER.toLocalizedString(prms));
        }
        return assignFixedBucketId(pr, partition, resolveKey);
    } else {
        // Calculate resolveKey.
        if (resolver == null) {
            // no custom partitioning at all
            resolveKey = key;
            if (resolveKey == null) {
                throw new IllegalStateException("attempting to hash null");
            }
        } else {
            if (event == null) {
                event = new EntryOperationImpl(pr, operation, key, value, callbackArgument);
            }
            resolveKey = resolver.getRoutingObject(event);
            if (resolveKey == null) {
                throw new IllegalStateException(LocalizedStrings.PartitionedRegionHelper_THE_ROUTINGOBJECT_RETURNED_BY_PARTITIONRESOLVER_IS_NULL.toLocalizedString());
            }
        }
        // Finally, calculate the hash.
        return getHashKey(pr, resolveKey);
    }
}
Also used : FixedPartitionResolver(org.apache.geode.cache.FixedPartitionResolver) PartitionResolver(org.apache.geode.cache.PartitionResolver) FixedPartitionResolver(org.apache.geode.cache.FixedPartitionResolver) PartitionNotAvailableException(org.apache.geode.cache.partition.PartitionNotAvailableException)

Example 3 with FixedPartitionResolver

use of org.apache.geode.cache.FixedPartitionResolver 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 4 with FixedPartitionResolver

use of org.apache.geode.cache.FixedPartitionResolver 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)

Aggregations

FixedPartitionResolver (org.apache.geode.cache.FixedPartitionResolver)4 PartitionResolver (org.apache.geode.cache.PartitionResolver)4 EntryOperation (org.apache.geode.cache.EntryOperation)3 EntryOperationImpl (org.apache.geode.internal.cache.EntryOperationImpl)3 PartitionNotAvailableException (org.apache.geode.cache.partition.PartitionNotAvailableException)1 ServerLocation (org.apache.geode.distributed.internal.ServerLocation)1 BucketServerLocation66 (org.apache.geode.internal.cache.BucketServerLocation66)1