Search in sources :

Example 6 with OffHeapRegionEntry

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();
            }
        }
    }
}
Also used : OffHeapRegionEntry(org.apache.geode.internal.cache.OffHeapRegionEntry) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) CancelException(org.apache.geode.CancelException)

Example 7 with OffHeapRegionEntry

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());
}
Also used : OffHeapRegionEntry(org.apache.geode.internal.cache.OffHeapRegionEntry) UnitTest(org.apache.geode.test.junit.categories.UnitTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with OffHeapRegionEntry

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);
}
Also used : Token(org.apache.geode.internal.cache.Token) OffHeapRegionEntry(org.apache.geode.internal.cache.OffHeapRegionEntry) UnitTest(org.apache.geode.test.junit.categories.UnitTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with OffHeapRegionEntry

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);
}
Also used : OffHeapRegionEntry(org.apache.geode.internal.cache.OffHeapRegionEntry) UnitTest(org.apache.geode.test.junit.categories.UnitTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 10 with OffHeapRegionEntry

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);
}
Also used : Token(org.apache.geode.internal.cache.Token) OffHeapRegionEntry(org.apache.geode.internal.cache.OffHeapRegionEntry) UnitTest(org.apache.geode.test.junit.categories.UnitTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

OffHeapRegionEntry (org.apache.geode.internal.cache.OffHeapRegionEntry)11 UnitTest (org.apache.geode.test.junit.categories.UnitTest)10 Test (org.junit.Test)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 Token (org.apache.geode.internal.cache.Token)4 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 CancelException (org.apache.geode.CancelException)1 InternalDistributedSystem (org.apache.geode.distributed.internal.InternalDistributedSystem)1 DiskEntry (org.apache.geode.internal.cache.DiskEntry)1 DiskId (org.apache.geode.internal.cache.DiskId)1