use of org.apache.geode.internal.cache.OffHeapRegionEntry in project geode by apache.
the class CustomEntryConcurrentHashMap method clear.
/**
* Removes all of the mappings from this map.
*/
@Override
public void clear() {
ArrayList<HashEntry<?, ?>> entries = null;
try {
for (int i = 0; i < this.segments.length; ++i) {
entries = this.segments[i].clear(entries);
}
} finally {
if (entries != null) {
final ArrayList<HashEntry<?, ?>> clearedEntries = entries;
final Runnable runnable = new Runnable() {
public void run() {
for (HashEntry<?, ?> he : clearedEntries) {
for (HashEntry<?, ?> p = he; p != null; p = p.getNextEntry()) {
synchronized (p) {
((OffHeapRegionEntry) p).release();
}
}
}
}
};
boolean submitted = false;
InternalDistributedSystem ids = InternalDistributedSystem.getConnectedInstance();
if (ids != null) {
try {
ids.getDistributionManager().getWaitingThreadPool().execute(runnable);
submitted = true;
} catch (RejectedExecutionException e) {
// fall through with submitted false
} catch (CancelException e) {
// fall through with submitted false
} catch (NullPointerException e) {
// fall through with submitted false
}
}
if (!submitted) {
String name = this.getClass().getSimpleName() + "@" + this.hashCode() + " Clear Thread";
Thread thread = new Thread(runnable, name);
thread.setDaemon(true);
thread.start();
}
}
}
}
use of org.apache.geode.internal.cache.OffHeapRegionEntry in project geode by apache.
the class OffHeapRegionEntryHelperJUnitTest method setValueShouldThrowIllegalExceptionIfNewValueCannotBeConvertedToAddress.
@Test(expected = IllegalStateException.class)
public void setValueShouldThrowIllegalExceptionIfNewValueCannotBeConvertedToAddress() {
// mock region entry
OffHeapRegionEntry re = mock(OffHeapRegionEntry.class);
// mock region entry methods required for test
when(re.getAddress()).thenReturn(1L);
// invoke the method under test with some object other than Chunk/DataAsAddress/Token
OffHeapRegionEntryHelper.setValue(re, new Object());
}
use of org.apache.geode.internal.cache.OffHeapRegionEntry in project geode by apache.
the class OffHeapRegionEntryHelperJUnitTest method getValueAsTokenShouldReturnAValidToken.
@Test
public void getValueAsTokenShouldReturnAValidToken() {
// mock region entry
OffHeapRegionEntry re = mock(OffHeapRegionEntry.class);
// mock region entry methods required for test
when(re.getAddress()).thenReturn(OffHeapRegionEntryHelper.NULL_ADDRESS);
Token token = OffHeapRegionEntryHelper.getValueAsToken(re);
assertThat(token).isNull();
// mock region entry methods required for test
when(re.getAddress()).thenReturn(OffHeapRegionEntryHelper.INVALID_ADDRESS);
token = OffHeapRegionEntryHelper.getValueAsToken(re);
assertThat(token).isEqualTo(Token.INVALID);
// mock region entry methods required for test
when(re.getAddress()).thenReturn(OffHeapRegionEntryHelper.LOCAL_INVALID_ADDRESS);
token = OffHeapRegionEntryHelper.getValueAsToken(re);
assertThat(token).isEqualTo(Token.LOCAL_INVALID);
// mock region entry methods required for test
when(re.getAddress()).thenReturn(OffHeapRegionEntryHelper.DESTROYED_ADDRESS);
token = OffHeapRegionEntryHelper.getValueAsToken(re);
assertThat(token).isEqualTo(Token.DESTROYED);
// mock region entry methods required for test
when(re.getAddress()).thenReturn(OffHeapRegionEntryHelper.REMOVED_PHASE1_ADDRESS);
token = OffHeapRegionEntryHelper.getValueAsToken(re);
assertThat(token).isEqualTo(Token.REMOVED_PHASE1);
// mock region entry methods required for test
when(re.getAddress()).thenReturn(OffHeapRegionEntryHelper.REMOVED_PHASE2_ADDRESS);
token = OffHeapRegionEntryHelper.getValueAsToken(re);
assertThat(token).isEqualTo(Token.REMOVED_PHASE2);
// mock region entry methods required for test
when(re.getAddress()).thenReturn(OffHeapRegionEntryHelper.END_OF_STREAM_ADDRESS);
token = OffHeapRegionEntryHelper.getValueAsToken(re);
assertThat(token).isEqualTo(Token.END_OF_STREAM);
// mock region entry methods required for test
when(re.getAddress()).thenReturn(OffHeapRegionEntryHelper.NOT_AVAILABLE_ADDRESS);
token = OffHeapRegionEntryHelper.getValueAsToken(re);
assertThat(token).isEqualTo(Token.NOT_AVAILABLE);
// mock region entry methods required for test
when(re.getAddress()).thenReturn(OffHeapRegionEntryHelper.TOMBSTONE_ADDRESS);
token = OffHeapRegionEntryHelper.getValueAsToken(re);
assertThat(token).isEqualTo(Token.TOMBSTONE);
}
use of org.apache.geode.internal.cache.OffHeapRegionEntry in project geode by apache.
the class OffHeapRegionEntryHelperJUnitTest method setValueShouldChangeTheRegionEntryAddressToNewAddressAndDoesNothingIfOldAddressIsAnEncodedAddress.
@Test
public void setValueShouldChangeTheRegionEntryAddressToNewAddressAndDoesNothingIfOldAddressIsAnEncodedAddress() {
// mock region entry
OffHeapRegionEntry re = mock(OffHeapRegionEntry.class);
byte[] oldData = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE).putInt((Integer) Integer.MAX_VALUE).array();
long oldAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(oldData, false, false);
byte[] newData = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE).putInt((Integer) Integer.MAX_VALUE - 1).array();
TinyStoredObject newAddress = new TinyStoredObject(OffHeapRegionEntryHelper.encodeDataAsAddress(newData, false, false));
// mock Chunk static methods - in-order to verify that release is never called
PowerMockito.spy(OffHeapStoredObject.class);
PowerMockito.doNothing().when(OffHeapStoredObject.class);
OffHeapStoredObject.release(oldAddress);
// mock region entry methods required for test
when(re.getAddress()).thenReturn(oldAddress);
when(re.setAddress(oldAddress, newAddress.getAddress())).thenReturn(Boolean.TRUE);
// invoke the method under test
OffHeapRegionEntryHelper.setValue(re, newAddress);
// verify oldAddress is changed to newAddress
verify(re, times(1)).setAddress(oldAddress, newAddress.getAddress());
// verify that release is never called as the old address is not on offheap
PowerMockito.verifyStatic(never());
OffHeapStoredObject.release(oldAddress);
}
use of org.apache.geode.internal.cache.OffHeapRegionEntry in project geode by apache.
the class OffHeapRegionEntryHelperJUnitTest method setValueShouldChangeTheRegionEntryAddressToNewAddressAndDoesNothingIfOldAddressIsATokenAddress.
@Test
public void setValueShouldChangeTheRegionEntryAddressToNewAddressAndDoesNothingIfOldAddressIsATokenAddress() {
// mock region entry
OffHeapRegionEntry re = mock(OffHeapRegionEntry.class);
long oldAddress = OffHeapRegionEntryHelper.REMOVED_PHASE1_ADDRESS;
Token newValue = Token.REMOVED_PHASE2;
long newAddress = OffHeapRegionEntryHelper.REMOVED_PHASE2_ADDRESS;
// mock Chunk static methods - in-order to verify that release is never called
PowerMockito.spy(OffHeapStoredObject.class);
PowerMockito.doNothing().when(OffHeapStoredObject.class);
OffHeapStoredObject.release(oldAddress);
// mock region entry methods required for test
when(re.getAddress()).thenReturn(oldAddress);
when(re.setAddress(oldAddress, newAddress)).thenReturn(Boolean.TRUE);
// invoke the method under test
OffHeapRegionEntryHelper.setValue(re, newValue);
// verify oldAddress is changed to newAddress
verify(re, times(1)).setAddress(oldAddress, newAddress);
// verify that release is never called as the old address is not on offheap
PowerMockito.verifyStatic(never());
OffHeapStoredObject.release(oldAddress);
}
Aggregations