/** * Release the lock on the bucket that makes the bucket stay the primary during a write. * And release/remove the lockObject on the key(s) */ void releaseLockForKeysAndPrimary(EntryEventImpl event) { doUnlockForPrimary(); Object[] keys = getKeysToBeLocked(event); removeAndNotifyKeys(keys); }
@Test(expected = RegionDestroyedException.class) public void lockKeysAndPrimaryThrowsIfWaitUntilLockedThrows() { BucketRegion bucketRegion = spy(new BucketRegion(regionName, regionAttributes, partitionedRegion, cache, internalRegionArgs)); doReturn(keys).when(bucketRegion).getKeysToBeLocked(event); doThrow(regionDestroyedException).when(bucketRegion).waitUntilLocked(keys); bucketRegion.lockKeysAndPrimary(event); }
@Test public void lockKeysAndPrimaryReleaseLockHeldIfDoesNotLockForPrimary() { BucketRegion bucketRegion = spy(new BucketRegion(regionName, regionAttributes, partitionedRegion, cache, internalRegionArgs)); doReturn(keys).when(bucketRegion).getKeysToBeLocked(event); doReturn(true).when(bucketRegion).waitUntilLocked(keys); doReturn(true).when(bucketRegion).doLockForPrimary(false); bucketRegion.lockKeysAndPrimary(event); verify(bucketRegion, never()).removeAndNotifyKeys(keys); }
@Test(expected = PrimaryBucketException.class) public void lockKeysAndPrimaryReleaseLockHeldIfDoLockForPrimaryThrows() { BucketRegion bucketRegion = spy(new BucketRegion(regionName, regionAttributes, partitionedRegion, cache, internalRegionArgs)); doReturn(keys).when(bucketRegion).getKeysToBeLocked(event); doReturn(true).when(bucketRegion).waitUntilLocked(keys); doThrow(new PrimaryBucketException()).when(bucketRegion).doLockForPrimary(false); bucketRegion.lockKeysAndPrimary(event); verify(bucketRegion).removeAndNotifyKeys(keys); }
/** * Checks to make sure that this node is primary, and locks the bucket to make sure the bucket * stays the primary bucket while the write is in progress. This method must be followed with * a call to releaseLockForKeysAndPrimary() if keys and primary are locked. */ boolean lockKeysAndPrimary(EntryEventImpl event) { if (!needWriteLock(event)) { return false; } if (cache.isCacheAtShutdownAll()) { throw cache.getCacheClosedException("Cache is shutting down"); } Object[] keys = getKeysToBeLocked(event); waitUntilLocked(keys); // it might wait for long time boolean lockedForPrimary = false; try { lockedForPrimary = doLockForPrimary(false); // tryLock is false means doLockForPrimary won't return false. // either the method returns true or fails with an exception assert lockedForPrimary : "expected doLockForPrimary returns true"; return lockedForPrimary; } finally { if (!lockedForPrimary) { removeAndNotifyKeys(keys); } } }