Search in sources :

Example 16 with Bucket

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

the class AbstractIndex method query.

@Override
public void query(Object key, int operator, Collection results, ExecutionContext context) throws TypeMismatchException, FunctionDomainException, NameResolutionException, QueryInvocationTargetException {
    // get a read lock when doing a lookup
    if (context.getBucketList() != null && this.region instanceof BucketRegion) {
        PartitionedRegion pr = ((Bucket) this.region).getPartitionedRegion();
        long start = updateIndexUseStats();
        try {
            for (Object bucketId : context.getBucketList()) {
                AbstractIndex bucketIndex = PartitionedIndex.getBucketIndex(pr, this.indexName, (Integer) bucketId);
                if (bucketIndex == null) {
                    continue;
                }
                bucketIndex.lockedQuery(key, operator, results, null, /* No Keys to be removed */
                context);
            }
        } finally {
            updateIndexUseEndStats(start);
        }
    } else {
        long start = updateIndexUseStats();
        try {
            lockedQuery(key, operator, results, null, /* No Keys to be removed */
            context);
        } finally {
            updateIndexUseEndStats(start);
        }
    }
}
Also used : BucketRegion(org.apache.geode.internal.cache.BucketRegion) Bucket(org.apache.geode.internal.cache.partitioned.Bucket) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion)

Example 17 with Bucket

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

the class BucketAdvisor method acquiredPrimaryLock.

/**
   * Invoked when the primary lock has been acquired by this VM.
   * 
   * @return true if successfully changed state to IS_PRIMARY
   */
protected boolean acquiredPrimaryLock() {
    if (logger.isDebugEnabled()) {
        logger.debug("Acquired primary lock for BucketID {} PR : {}", getBucket().getId(), regionAdvisor.getPartitionedRegion().getFullPath());
    }
    boolean changedStateToIsPrimary = false;
    // Hold the primary move lock until we send a
    // profile update. This will prevent writes
    // from occurring until all members know that
    // this member is now the primary.
    boolean shouldInvokeListeners = false;
    activePrimaryMoveLock.lock();
    try {
        synchronized (this) {
            if (isHosting() && (isVolunteering() || isBecomingPrimary())) {
                Bucket br = this.regionAdvisor.getBucket(getBucket().getId());
                if (br != null && br instanceof BucketRegion) {
                    ((BucketRegion) br).beforeAcquiringPrimaryState();
                }
                if (requestPrimaryState(IS_PRIMARY_HOSTING)) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Acquired primary lock for setting primary now BucketID {} PR : {}", getBucket().getId(), regionAdvisor.getPartitionedRegion().getFullPath());
                    }
                    setPrimaryMember(getDistributionManager().getId());
                    changedStateToIsPrimary = true;
                    if (hasPrimary() && isPrimary()) {
                        shouldInvokeListeners = true;
                    }
                }
            }
        }
        if (shouldInvokeListeners) {
            invokePartitionListeners();
        }
        return changedStateToIsPrimary;
    } finally {
        try {
            if (changedStateToIsPrimary) {
                // send profile update AFTER releasing sync
                sendProfileUpdate();
                Bucket br = this.regionAdvisor.getBucket(getBucket().getId());
                if (br != null && br instanceof BucketRegion) {
                    ((BucketRegion) br).processPendingSecondaryExpires();
                }
                if (br instanceof BucketRegionQueue) {
                    // Shouldn't it be AbstractBucketRegionQueue
                    BucketRegionQueue brq = (BucketRegionQueue) br;
                    brq.incQueueSize(brq.size());
                }
                if (br != null && br instanceof BucketRegion) {
                    ((BucketRegion) br).afterAcquiringPrimaryState();
                }
            } else {
                // release primary lock AFTER releasing sync
                releasePrimaryLock();
            }
        } finally {
            activePrimaryMoveLock.unlock();
        }
    }
}
Also used : Bucket(org.apache.geode.internal.cache.partitioned.Bucket)

Example 18 with Bucket

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

the class BucketAdvisor method acquirePrimaryForRestOfTheBucket.

protected void acquirePrimaryForRestOfTheBucket() {
    List<FixedPartitionAttributesImpl> fpas = this.pRegion.getFixedPartitionAttributesImpl();
    if (fpas != null) {
        int bucketId = getBucket().getId();
        for (FixedPartitionAttributesImpl fpa : fpas) {
            if (fpa.getStartingBucketID() == bucketId) {
                for (int i = bucketId + 1; i <= fpa.getLastBucketID(); ) {
                    Bucket b = regionAdvisor.getBucket(i++);
                    if (b != null) {
                        BucketAdvisor ba = b.getBucketAdvisor();
                        ba.activePrimaryMoveLock.lock();
                        try {
                            if (ba.isHosting()) {
                                if (!ba.isPrimary()) {
                                    ba.setVolunteering();
                                    ba.acquiredPrimaryLock();
                                }
                            }
                        } finally {
                            ba.activePrimaryMoveLock.unlock();
                        }
                    }
                }
            } else {
                continue;
            }
        }
    }
}
Also used : Bucket(org.apache.geode.internal.cache.partitioned.Bucket)

Example 19 with Bucket

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

the class PersistentStateQueryMessage method process.

@Override
protected void process(DistributionManager dm) {
    // Set thread local flag to allow entrance through initialization Latch
    int oldLevel = LocalRegion.setThreadInitLevelRequirement(LocalRegion.ANY_INIT);
    PersistentMemberState state = null;
    PersistentMemberID myId = null;
    PersistentMemberID myInitializingId = null;
    DiskStoreID diskStoreId = null;
    HashSet<PersistentMemberID> onlineMembers = null;
    ReplyException exception = null;
    boolean successfulReply = false;
    try {
        // get the region from the path, but do NOT wait on initialization,
        // otherwise we could have a distributed deadlock
        Cache cache = CacheFactory.getInstance(dm.getSystem());
        Region region = cache.getRegion(this.regionPath);
        PersistenceAdvisor persistenceAdvisor = null;
        if (region instanceof DistributedRegion) {
            persistenceAdvisor = ((DistributedRegion) region).getPersistenceAdvisor();
        } else if (region == null) {
            Bucket proxy = PartitionedRegionHelper.getProxyBucketRegion(GemFireCacheImpl.getInstance(), this.regionPath, false);
            if (proxy != null) {
                persistenceAdvisor = proxy.getPersistenceAdvisor();
            }
        }
        if (persistenceAdvisor != null) {
            if (id != null) {
                state = persistenceAdvisor.getPersistedStateOfMember(id);
            }
            if (initializingId != null && state == null) {
                state = persistenceAdvisor.getPersistedStateOfMember(initializingId);
            }
            myId = persistenceAdvisor.getPersistentID();
            myInitializingId = persistenceAdvisor.getInitializingID();
            onlineMembers = persistenceAdvisor.getPersistedOnlineOrEqualMembers();
            diskStoreId = persistenceAdvisor.getDiskStoreID();
            successfulReply = true;
        }
    } catch (RegionDestroyedException e) {
        logger.debug("<RegionDestroyed> {}", this);
    } catch (CancelException e) {
        logger.debug("<CancelException> {}", this);
    } catch (VirtualMachineError e) {
        SystemFailure.initiateFailure(e);
        throw e;
    } catch (Throwable t) {
        SystemFailure.checkFailure();
        exception = new ReplyException(t);
    } finally {
        LocalRegion.setThreadInitLevelRequirement(oldLevel);
        ReplyMessage replyMsg;
        if (successfulReply) {
            PersistentStateQueryReplyMessage persistentReplyMessage = new PersistentStateQueryReplyMessage();
            persistentReplyMessage.myId = myId;
            persistentReplyMessage.persistedStateOfPeer = state;
            persistentReplyMessage.myInitializingId = myInitializingId;
            persistentReplyMessage.diskStoreId = diskStoreId;
            persistentReplyMessage.onlineMembers = onlineMembers;
            replyMsg = persistentReplyMessage;
        } else {
            replyMsg = new ReplyMessage();
        }
        replyMsg.setProcessorId(processorId);
        replyMsg.setRecipient(getSender());
        if (exception != null) {
            replyMsg.setException(exception);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Received {},replying with {}", this, replyMsg);
        }
        dm.putOutgoing(replyMsg);
    }
}
Also used : RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) ReplyException(org.apache.geode.distributed.internal.ReplyException) ReplyMessage(org.apache.geode.distributed.internal.ReplyMessage) Bucket(org.apache.geode.internal.cache.partitioned.Bucket) LocalRegion(org.apache.geode.internal.cache.LocalRegion) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) Region(org.apache.geode.cache.Region) CancelException(org.apache.geode.CancelException) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) Cache(org.apache.geode.cache.Cache)

Example 20 with Bucket

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

the class ResourceManagerDUnitTest method testRemoveColocatedBuckets.

/**
   * Creates a chain of three colocated PRs and then calls removeBucket to make sure that all
   * colocated buckets are removed together.
   */
// GEODE-928: RemoveBucketMessage failure?
@Category(FlakyTest.class)
@Test
public void testRemoveColocatedBuckets() {
    final String[] regionPath = new String[] { getUniqueName() + "-PR-0", getUniqueName() + "-PR-1", getUniqueName() + "-PR-2" };
    final int numBuckets = 1;
    final int redundantCopies = 1;
    final int localMaxMemory = 100;
    createRegion(Host.getHost(0).getVM(0), regionPath[0], localMaxMemory, numBuckets, redundantCopies);
    createRegion(Host.getHost(0).getVM(1), regionPath[0], localMaxMemory, numBuckets, redundantCopies);
    createRegion(Host.getHost(0).getVM(0), regionPath[1], localMaxMemory, numBuckets, redundantCopies, regionPath[0]);
    createRegion(Host.getHost(0).getVM(1), regionPath[1], localMaxMemory, numBuckets, redundantCopies, regionPath[0]);
    createRegion(Host.getHost(0).getVM(0), regionPath[2], localMaxMemory, numBuckets, redundantCopies, regionPath[1]);
    createRegion(Host.getHost(0).getVM(1), regionPath[2], localMaxMemory, numBuckets, redundantCopies, regionPath[1]);
    final Integer bucketKey = Integer.valueOf(0);
    final byte[] value = new byte[1];
    createBucket(0, regionPath[0], bucketKey, value);
    createBucket(0, regionPath[1], bucketKey, value);
    createBucket(0, regionPath[2], bucketKey, value);
    // identify the members and their config values
    final InternalDistributedMember[] members = new InternalDistributedMember[2];
    final long[] memberSizes = new long[members.length];
    final int[] memberBucketCounts = new int[members.length];
    final int[] memberPrimaryCounts = new int[members.length];
    fillValidationArrays(members, memberSizes, memberBucketCounts, memberPrimaryCounts, regionPath[0]);
    int primaryVM = -1;
    int otherVM = -1;
    for (int i = 0; i < memberPrimaryCounts.length; i++) {
        if (memberPrimaryCounts[i] == 0) {
            otherVM = i;
        } else if (memberPrimaryCounts[i] == 1) {
            // found the primary
            primaryVM = i;
        }
    }
    assertTrue(primaryVM > -1);
    assertTrue(otherVM > -1);
    assertTrue(primaryVM != otherVM);
    final int finalOtherVM = otherVM;
    Host.getHost(0).getVM(otherVM).invoke(new SerializableRunnable() {

        public void run() {
            for (int i = 0; i < regionPath.length; i++) {
                PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(regionPath[i]);
                Bucket bucket = pr.getRegionAdvisor().getBucket(0);
                assertTrue("Target member is not hosting bucket to remove for " + regionPath[i], bucket.isHosting());
                assertNotNull("Bucket is null on target member for " + regionPath[i], bucket);
                assertNotNull("BucketRegion is null on target member for " + regionPath[i], bucket.getBucketAdvisor().getProxyBucketRegion().getHostedBucketRegion());
            }
        }
    });
    boolean sentRemoveBucket = ((Boolean) Host.getHost(0).getVM(primaryVM).invoke(new SerializableCallable() {

        public Object call() {
            InternalDistributedMember recipient = members[finalOtherVM];
            PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(regionPath[0]);
            RemoveBucketResponse response = RemoveBucketMessage.send(recipient, pr, 0, false);
            if (response != null) {
                response.waitForRepliesUninterruptibly();
                return true;
            } else {
                return Boolean.FALSE;
            }
        }
    })).booleanValue();
    assertTrue("Failed to get reply to RemoveBucketMessage", sentRemoveBucket);
    Host.getHost(0).getVM(otherVM).invoke(new SerializableRunnable() {

        public void run() {
            for (int i = 0; i < regionPath.length; i++) {
                PartitionedRegion pr = (PartitionedRegion) getCache().getRegion(regionPath[i]);
                Bucket bucket = pr.getRegionAdvisor().getBucket(0);
                BucketRegion bucketRegion = bucket.getBucketAdvisor().getProxyBucketRegion().getHostedBucketRegion();
                assertFalse("Target member is still hosting removed bucket for " + regionPath[i], bucket.isHosting());
                assertNull("BucketRegion is not null on target member for " + regionPath[i], bucketRegion);
            }
        }
    });
}
Also used : SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) RemoveBucketResponse(org.apache.geode.internal.cache.partitioned.RemoveBucketMessage.RemoveBucketResponse) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) BucketRegion(org.apache.geode.internal.cache.BucketRegion) Bucket(org.apache.geode.internal.cache.partitioned.Bucket) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Category(org.junit.experimental.categories.Category) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Aggregations

Bucket (org.apache.geode.internal.cache.partitioned.Bucket)22 BucketRegion (org.apache.geode.internal.cache.BucketRegion)11 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)11 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)9 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)7 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)6 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)6 Test (org.junit.Test)6 CancelException (org.apache.geode.CancelException)5 Cache (org.apache.geode.cache.Cache)4 Region (org.apache.geode.cache.Region)4 RegionDestroyedException (org.apache.geode.cache.RegionDestroyedException)4 ReplyException (org.apache.geode.distributed.internal.ReplyException)4 DistributedRegion (org.apache.geode.internal.cache.DistributedRegion)4 LocalRegion (org.apache.geode.internal.cache.LocalRegion)4 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)4 ReplyMessage (org.apache.geode.distributed.internal.ReplyMessage)3 RemoveBucketResponse (org.apache.geode.internal.cache.partitioned.RemoveBucketMessage.RemoveBucketResponse)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1