Search in sources :

Example 1 with FetchKeysResponse

use of org.apache.geode.internal.cache.partitioned.FetchKeysMessage.FetchKeysResponse in project geode by apache.

the class PartitionedRegion method _getKeysWithInterest.

/**
   * finds all the keys matching the given interest type and passes them to the given collector
   * 
   * @param allowTombstones whether to return destroyed entries
   */
private void _getKeysWithInterest(int interestType, Object interestArg, boolean allowTombstones, SetCollector collector) throws IOException {
    // this could be parallelized by building up a list of buckets for each
    // vm and sending out the requests for keys in parallel. That might dump
    // more onto this vm in one swoop than it could handle, though, so we're
    // keeping it simple for now
    int totalBuckets = getTotalNumberOfBuckets();
    int retryAttempts = calcRetry();
    for (int bucket = 0; bucket < totalBuckets; bucket++) {
        Set bucketSet = null;
        Integer lbucket = bucket;
        final RetryTimeKeeper retryTime = new RetryTimeKeeper(Integer.MAX_VALUE);
        InternalDistributedMember bucketNode = getOrCreateNodeForBucketRead(lbucket);
        for (int count = 0; count <= retryAttempts; count++) {
            if (logger.isDebugEnabled()) {
                logger.debug("_getKeysWithInterest bucketId={} attempt={}", bucket, (count + 1));
            }
            try {
                if (bucketNode != null) {
                    if (bucketNode.equals(getMyId())) {
                        bucketSet = this.dataStore.handleRemoteGetKeys(lbucket, interestType, interestArg, allowTombstones);
                    } else {
                        FetchKeysResponse r = FetchKeysMessage.sendInterestQuery(bucketNode, this, lbucket, interestType, interestArg, allowTombstones);
                        bucketSet = r.waitForKeys();
                    }
                }
                break;
            } catch (PRLocallyDestroyedException ignore) {
                if (logger.isDebugEnabled()) {
                    logger.debug("_getKeysWithInterest: Encountered PRLocallyDestroyedException");
                }
                checkReadiness();
            } catch (ForceReattemptException prce) {
                // no checkKey possible
                if (logger.isDebugEnabled()) {
                    logger.debug("_getKeysWithInterest: retry attempt: {}", count, prce);
                }
                checkReadiness();
                InternalDistributedMember lastTarget = bucketNode;
                bucketNode = getOrCreateNodeForBucketRead(lbucket);
                if (lastTarget != null && lastTarget.equals(bucketNode)) {
                    if (retryTime.overMaximum()) {
                        break;
                    }
                    retryTime.waitToRetryNode();
                }
            }
        }
        // for(count)
        if (bucketSet != null) {
            collector.receiveSet(bucketSet);
        }
    }
// for(bucket)
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashSet(java.util.HashSet) Set(java.util.Set) ResultsSet(org.apache.geode.cache.query.internal.ResultsSet) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) PRLocallyDestroyedException(org.apache.geode.internal.cache.partitioned.PRLocallyDestroyedException) FetchKeysResponse(org.apache.geode.internal.cache.partitioned.FetchKeysMessage.FetchKeysResponse)

Example 2 with FetchKeysResponse

use of org.apache.geode.internal.cache.partitioned.FetchKeysMessage.FetchKeysResponse in project geode by apache.

the class PartitionedRegion method getBucketKeys.

/**
   * Fetch the keys for the given bucket identifier, if the bucket is local or remote. This version
   * of the method allows you to retrieve Tombstone entries as well as undestroyed entries.
   * 
   * @param allowTombstones whether to include destroyed entries in the result
   * @return A set of keys from bucketNum or {@link Collections#EMPTY_SET}if no keys can be found.
   */
public Set getBucketKeys(int bucketNum, boolean allowTombstones) {
    Integer buck = bucketNum;
    final int retryAttempts = calcRetry();
    Set ret = null;
    int count = 0;
    InternalDistributedMember nod = getOrCreateNodeForBucketRead(bucketNum);
    RetryTimeKeeper snoozer = null;
    while (count <= retryAttempts) {
        // It's possible this is a GemFire thread e.g. ServerConnection
        // which got to this point because of a distributed system shutdown or
        // region closure which uses interrupt to break any sleep() or wait()
        // calls
        // e.g. waitForPrimary or waitForBucketRecovery
        checkShutdown();
        if (nod == null) {
            if (snoozer == null) {
                snoozer = new RetryTimeKeeper(this.retryTimeout);
            }
            nod = getOrCreateNodeForBucketRead(bucketNum);
            // No storage found for bucket, early out preventing hot loop, bug 36819
            if (nod == null) {
                checkShutdown();
                break;
            }
            count++;
            continue;
        }
        try {
            if (nod.equals(getMyId())) {
                ret = this.dataStore.getKeysLocally(buck, allowTombstones);
            } else {
                FetchKeysResponse r = FetchKeysMessage.send(nod, this, buck, allowTombstones);
                ret = r.waitForKeys();
            }
            if (ret != null) {
                return ret;
            }
        } catch (PRLocallyDestroyedException ignore) {
            if (logger.isDebugEnabled()) {
                logger.debug("getBucketKeys: Encountered PRLocallyDestroyedException");
            }
            checkReadiness();
        } catch (ForceReattemptException prce) {
            if (logger.isDebugEnabled()) {
                logger.debug("getBucketKeys: attempt:{}", (count + 1), prce);
            }
            checkReadiness();
            if (snoozer == null) {
                snoozer = new RetryTimeKeeper(this.retryTimeout);
            }
            InternalDistributedMember oldNode = nod;
            nod = getNodeForBucketRead(buck);
            if (nod != null && nod.equals(oldNode)) {
                if (snoozer.overMaximum()) {
                    checkReadiness();
                    throw new TimeoutException(LocalizedStrings.PartitionedRegion_ATTEMPT_TO_ACQUIRE_PRIMARY_NODE_FOR_READ_ON_BUCKET_0_TIMED_OUT_IN_1_MS.toLocalizedString(new Object[] { getBucketName(buck), snoozer.getRetryTime() }));
                }
                snoozer.waitToRetryNode();
            }
        }
        count++;
    }
    if (logger.isDebugEnabled()) {
        logger.debug("getBucketKeys: no keys found returning empty set");
    }
    return Collections.emptySet();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashSet(java.util.HashSet) Set(java.util.Set) ResultsSet(org.apache.geode.cache.query.internal.ResultsSet) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) PRLocallyDestroyedException(org.apache.geode.internal.cache.partitioned.PRLocallyDestroyedException) FetchKeysResponse(org.apache.geode.internal.cache.partitioned.FetchKeysMessage.FetchKeysResponse) TimeoutException(org.apache.geode.cache.TimeoutException)

Example 3 with FetchKeysResponse

use of org.apache.geode.internal.cache.partitioned.FetchKeysMessage.FetchKeysResponse in project geode by apache.

the class PartitionedRegion method handleOldNodes.

/**
   * @return set of bucket-ids that could not be read from.
   */
private Set<Integer> handleOldNodes(HashMap nodeToBuckets, VersionedObjectList values, ServerConnection servConn) throws IOException {
    Set<Integer> failures = new HashSet<Integer>();
    HashMap oldFellas = filterOldMembers(nodeToBuckets);
    for (Iterator it = oldFellas.entrySet().iterator(); it.hasNext(); ) {
        Map.Entry e = (Map.Entry) it.next();
        InternalDistributedMember member = (InternalDistributedMember) e.getKey();
        Object bucketInfo = e.getValue();
        HashMap<Integer, HashSet> bucketKeys = null;
        Set<Integer> buckets = null;
        if (bucketInfo instanceof Set) {
            buckets = (Set<Integer>) bucketInfo;
        } else {
            bucketKeys = (HashMap<Integer, HashSet>) bucketInfo;
            buckets = bucketKeys.keySet();
        }
        for (Integer bucket : buckets) {
            Set keys = null;
            if (bucketKeys == null) {
                try {
                    FetchKeysResponse fkr = FetchKeysMessage.send(member, this, bucket, true);
                    keys = fkr.waitForKeys();
                } catch (ForceReattemptException ignore) {
                    failures.add(bucket);
                }
            } else {
                keys = bucketKeys.get(bucket);
            }
            // TODO (ashetkar) Use single Get70 instance for all?
            for (Object key : keys) {
                Get70 command = (Get70) Get70.getCommand();
                Get70.Entry ge = command.getValueAndIsObject(this, key, null, servConn);
                if (ge.keyNotPresent) {
                    values.addObjectPartForAbsentKey(key, ge.value, ge.versionTag);
                } else {
                    values.addObjectPart(key, ge.value, ge.isObject, ge.versionTag);
                }
                if (values.size() == BaseCommand.MAXIMUM_CHUNK_SIZE) {
                    BaseCommand.sendNewRegisterInterestResponseChunk(this, "keyList", values, false, servConn);
                    values.clear();
                }
            }
        }
    }
    return failures;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ResultsSet(org.apache.geode.cache.query.internal.ResultsSet) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) Get70(org.apache.geode.internal.cache.tier.sockets.command.Get70) PREntriesIterator(org.apache.geode.internal.cache.partitioned.PREntriesIterator) Iterator(java.util.Iterator) FetchKeysResponse(org.apache.geode.internal.cache.partitioned.FetchKeysMessage.FetchKeysResponse) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 4 with FetchKeysResponse

use of org.apache.geode.internal.cache.partitioned.FetchKeysMessage.FetchKeysResponse in project geode by apache.

the class PartitionedRegion method getSomeKeys.

/**
   * Test Method: Get a random set of keys from a randomly selected bucket using the provided
   * {@code Random} number generator.
   * 
   * @return A set of keys from a randomly chosen bucket or {@link Collections#EMPTY_SET}
   */
public Set getSomeKeys(Random rnd) throws IOException, ClassNotFoundException {
    InternalDistributedMember nod = null;
    Integer buck = null;
    Set buks = getRegionAdvisor().getBucketSet();
    if (buks != null && !buks.isEmpty()) {
        Object[] buksA = buks.toArray();
        Set ret = null;
        // Randomly pick a node to get some data from
        for (int i = 0; i < buksA.length; i++) {
            try {
                logger.debug("getSomeKeys: iteration: {}", i);
                int ind = rnd.nextInt(buksA.length);
                if (ind >= buksA.length) {
                    // The GSRandom.nextInt(int) may return a value that includes the
                    // maximum.
                    ind = buksA.length - 1;
                }
                buck = (Integer) buksA[ind];
                nod = getNodeForBucketRead(buck);
                if (nod != null) {
                    logger.debug("getSomeKeys: iteration: {} for node {}", i, nod);
                    if (nod.equals(getMyId())) {
                        ret = dataStore.handleRemoteGetKeys(buck, InterestType.REGULAR_EXPRESSION, ".*", false);
                    } else {
                        FetchKeysResponse r = FetchKeysMessage.send(nod, this, buck, false);
                        ret = r.waitForKeys();
                    }
                    if (ret != null && !ret.isEmpty()) {
                        return ret;
                    }
                }
            } catch (ForceReattemptException movinOn) {
                checkReadiness();
                logger.debug("Test hook getSomeKeys caught a ForceReattemptException for bucketId={}{}{}. Moving on to another bucket", getPRId(), BUCKET_ID_SEPARATOR, buck, movinOn);
                continue;
            } catch (PRLocallyDestroyedException ignore) {
                logger.debug("getSomeKeys: Encountered PRLocallyDestroyedException");
                checkReadiness();
                continue;
            }
        }
    // nod != null
    }
    // for
    logger.debug("getSomeKeys: no keys found returning empty set");
    return Collections.emptySet();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashSet(java.util.HashSet) Set(java.util.Set) ResultsSet(org.apache.geode.cache.query.internal.ResultsSet) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) PRLocallyDestroyedException(org.apache.geode.internal.cache.partitioned.PRLocallyDestroyedException) FetchKeysResponse(org.apache.geode.internal.cache.partitioned.FetchKeysMessage.FetchKeysResponse)

Aggregations

HashSet (java.util.HashSet)4 Set (java.util.Set)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 ResultsSet (org.apache.geode.cache.query.internal.ResultsSet)4 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)4 FetchKeysResponse (org.apache.geode.internal.cache.partitioned.FetchKeysMessage.FetchKeysResponse)4 PRLocallyDestroyedException (org.apache.geode.internal.cache.partitioned.PRLocallyDestroyedException)3 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 TimeoutException (org.apache.geode.cache.TimeoutException)1 PREntriesIterator (org.apache.geode.internal.cache.partitioned.PREntriesIterator)1 Get70 (org.apache.geode.internal.cache.tier.sockets.command.Get70)1