@Test public void waitUntilLockedReturnsTrueIfNoOtherThreadLockedKeys() { BucketRegion bucketRegion = spy(new BucketRegion(regionName, regionAttributes, partitionedRegion, cache, internalRegionArgs)); Integer[] keys = {1}; doReturn(null).when(bucketRegion).searchAndLock(keys); assertThat(bucketRegion.waitUntilLocked(keys)).isTrue(); }
@Test(expected = RegionDestroyedException.class) public void waitUntilLockedThrowsIfFoundLockAndPartitionedRegionIsClosing() { BucketRegion bucketRegion = spy(new BucketRegion(regionName, regionAttributes, partitionedRegion, cache, internalRegionArgs)); Integer[] keys = {1}; doReturn(mock(LockObject.class)).when(bucketRegion).searchAndLock(keys); doThrow(regionDestroyedException).when(partitionedRegion) .checkReadiness(); bucketRegion.waitUntilLocked(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 public void removeAndNotifyKeysIsNotInvokedIfKeysNotLocked() throws Exception { PutAllPRMessage message = spy(new PutAllPRMessage(bucketId, 1, false, false, false, null)); RegionDestroyedException regionDestroyedException = new RegionDestroyedException("", ""); message.addEntry(entryData); doReturn(keys).when(message).getKeysToBeLocked(); when(bucketRegion.waitUntilLocked(keys)).thenThrow(regionDestroyedException); message.doLocalPutAll(partitionedRegion, mock(InternalDistributedMember.class), 1); verify(bucketRegion, never()).removeAndNotifyKeys(eq(keys)); verify(dataStore).checkRegionDestroyedOnBucket(eq(bucketRegion), eq(true), eq(regionDestroyedException)); }
@Test public void removeAndNotifyKeysIsNotInvokedIfKeysNotLocked() throws Exception { RemoveAllPRMessage message = spy(new RemoveAllPRMessage(bucketId, 1, false, false, true, null)); message.addEntry(entryData); doReturn(keys).when(message).getKeysToBeLocked(); RegionDestroyedException regionDestroyedException = new RegionDestroyedException("", ""); when(bucketRegion.waitUntilLocked(keys)).thenThrow(regionDestroyedException); message.doLocalRemoveAll(partitionedRegion, mock(InternalDistributedMember.class), true); verify(bucketRegion, never()).removeAndNotifyKeys(eq(keys)); verify(dataStore).checkRegionDestroyedOnBucket(eq(bucketRegion), eq(true), eq(regionDestroyedException)); } }
@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); } } }
bucketRegion.recordBulkOpStart(membershipID, eventID); locked = bucketRegion.waitUntilLocked(keys); boolean lockedForPrimary = false; final ArrayList<Object> succeeded = new ArrayList<Object>();
bucketRegion.recordBulkOpStart(membershipID, eventID); locked = bucketRegion.waitUntilLocked(keys); boolean lockedForPrimary = false; final HashMap succeeded = new HashMap();