try { boolean locked = b.doLockForPrimary(true); if (locked) { obtained.add(b);
@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 lockedKeysAreRemoved() throws Exception { PutAllPRMessage message = spy(new PutAllPRMessage(bucketId, 1, false, false, false, null)); message.addEntry(entryData); doReturn(keys).when(message).getKeysToBeLocked(); when(bucketRegion.waitUntilLocked(keys)).thenReturn(true); when(bucketRegion.doLockForPrimary(false)).thenThrow(new PrimaryBucketException()); message.doLocalPutAll(partitionedRegion, mock(InternalDistributedMember.class), 1); verify(bucketRegion).removeAndNotifyKeys(eq(keys)); }
@Test(expected = PrimaryBucketException.class) public void lockedKeysAreRemoved() throws Exception { RemoveAllPRMessage message = spy(new RemoveAllPRMessage(bucketId, 1, false, false, true, null)); message.addEntry(entryData); doReturn(keys).when(message).getKeysToBeLocked(); when(bucketRegion.waitUntilLocked(keys)).thenReturn(true); when(bucketRegion.doLockForPrimary(false)).thenThrow(new PrimaryBucketException()); message.doLocalRemoveAll(partitionedRegion, mock(InternalDistributedMember.class), true); verify(bucketRegion).removeAndNotifyKeys(eq(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); } } }
Object key = keys[0]; try { bucketRegion.doLockForPrimary(false); lockedForPrimary = true;
Object key = keys[0]; try { bucketRegion.doLockForPrimary(false); lockedForPrimary = true;