Search in sources :

Example 1 with OutOfOffHeapMemoryException

use of org.apache.geode.OutOfOffHeapMemoryException in project geode by apache.

the class FreeListManager method allocateFromFragments.

private OffHeapStoredObject allocateFromFragments(int chunkSize) {
    do {
        final int lastAllocationId = this.lastFragmentAllocation.get();
        for (int i = lastAllocationId; i < this.fragmentList.size(); i++) {
            OffHeapStoredObject result = allocateFromFragment(i, chunkSize);
            if (result != null) {
                return result;
            }
        }
        for (int i = 0; i < lastAllocationId; i++) {
            OffHeapStoredObject result = allocateFromFragment(i, chunkSize);
            if (result != null) {
                return result;
            }
        }
    } while (defragment(chunkSize));
    // We tried all the fragments and didn't find any free memory.
    logOffHeapState(chunkSize);
    final OutOfOffHeapMemoryException failure = new OutOfOffHeapMemoryException("Out of off-heap memory. Could not allocate size of " + chunkSize);
    try {
        throw failure;
    } finally {
        this.ma.getOutOfOffHeapMemoryListener().outOfOffHeapMemory(failure);
    }
}
Also used : OutOfOffHeapMemoryException(org.apache.geode.OutOfOffHeapMemoryException)

Example 2 with OutOfOffHeapMemoryException

use of org.apache.geode.OutOfOffHeapMemoryException in project geode by apache.

the class OffHeapRegionBase method keep_testOutOfOffHeapMemoryErrorClosesCache.

public void keep_testOutOfOffHeapMemoryErrorClosesCache() {
    // this test is redundant but may be useful
    final GemFireCacheImpl gfc = createCache();
    try {
        MemoryAllocator ma = gfc.getOffHeapStore();
        assertNotNull(ma);
        final long offHeapSize = ma.getFreeMemory();
        assertEquals(0, ma.getUsedMemory());
        StoredObject mc1 = ma.allocate(64);
        assertEquals(64 + perObjectOverhead(), ma.getUsedMemory());
        assertEquals(offHeapSize - (64 + perObjectOverhead()), ma.getFreeMemory());
        mc1.release();
        assertEquals(offHeapSize, ma.getFreeMemory());
        assertEquals(0, ma.getUsedMemory());
        // do an allocation larger than the slab size
        try {
            ma.allocate(1024 * 1024 * 10);
            fail("Expected an out of heap exception");
        } catch (OutOfOffHeapMemoryException expected) {
        // passed
        }
        assertEquals(0, ma.getUsedMemory());
        final WaitCriterion waitForDisconnect = new WaitCriterion() {

            public boolean done() {
                return gfc.isClosed();
            }

            public String description() {
                return "Waiting for disconnect to complete";
            }
        };
        org.apache.geode.test.dunit.Wait.waitForCriterion(waitForDisconnect, 10 * 1000, 100, true);
        assertTrue(gfc.isClosed());
    } finally {
        closeCache(gfc, false);
    }
}
Also used : WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) OutOfOffHeapMemoryException(org.apache.geode.OutOfOffHeapMemoryException)

Example 3 with OutOfOffHeapMemoryException

use of org.apache.geode.OutOfOffHeapMemoryException in project geode by apache.

the class OffHeapStorageJUnitTest method testCreateOffHeapStorage.

@Test
public void testCreateOffHeapStorage() {
    StatisticsFactory localStatsFactory = new LocalStatisticsFactory(null);
    OutOfOffHeapMemoryListener ooohml = mock(OutOfOffHeapMemoryListener.class);
    MemoryAllocator ma = OffHeapStorage.basicCreateOffHeapStorage(localStatsFactory, 1024 * 1024, ooohml);
    try {
        OffHeapMemoryStats stats = ma.getStats();
        assertNotNull(stats.getStats());
        assertEquals(1024 * 1024, stats.getFreeMemory());
        assertEquals(1024 * 1024, stats.getMaxMemory());
        assertEquals(0, stats.getUsedMemory());
        assertEquals(0, stats.getDefragmentations());
        assertEquals(0, stats.getDefragmentationsInProgress());
        assertEquals(0, stats.getDefragmentationTime());
        assertEquals(0, stats.getFragmentation());
        assertEquals(1, stats.getFragments());
        assertEquals(1024 * 1024, stats.getLargestFragment());
        assertEquals(0, stats.getObjects());
        assertEquals(0, stats.getReads());
        stats.incFreeMemory(100);
        assertEquals(1024 * 1024 + 100, stats.getFreeMemory());
        stats.incFreeMemory(-100);
        assertEquals(1024 * 1024, stats.getFreeMemory());
        stats.incMaxMemory(100);
        assertEquals(1024 * 1024 + 100, stats.getMaxMemory());
        stats.incMaxMemory(-100);
        assertEquals(1024 * 1024, stats.getMaxMemory());
        stats.incUsedMemory(100);
        assertEquals(100, stats.getUsedMemory());
        stats.incUsedMemory(-100);
        assertEquals(0, stats.getUsedMemory());
        stats.incObjects(100);
        assertEquals(100, stats.getObjects());
        stats.incObjects(-100);
        assertEquals(0, stats.getObjects());
        stats.incReads();
        assertEquals(1, stats.getReads());
        stats.setFragmentation(100);
        assertEquals(100, stats.getFragmentation());
        stats.setFragmentation(0);
        assertEquals(0, stats.getFragmentation());
        stats.setFragments(2);
        assertEquals(2, stats.getFragments());
        stats.setFragments(1);
        assertEquals(1, stats.getFragments());
        stats.setLargestFragment(100);
        assertEquals(100, stats.getLargestFragment());
        stats.setLargestFragment(1024 * 1024);
        assertEquals(1024 * 1024, stats.getLargestFragment());
        boolean originalEnableClockStats = DistributionStats.enableClockStats;
        DistributionStats.enableClockStats = true;
        try {
            long start = stats.startDefragmentation();
            assertEquals(1, stats.getDefragmentationsInProgress());
            while (DistributionStats.getStatTime() == start) {
                Thread.yield();
            }
            stats.endDefragmentation(start);
            assertEquals(1, stats.getDefragmentations());
            assertEquals(0, stats.getDefragmentationsInProgress());
            assertTrue(stats.getDefragmentationTime() > 0);
        } finally {
            DistributionStats.enableClockStats = originalEnableClockStats;
        }
        stats.incObjects(100);
        stats.incUsedMemory(100);
        stats.setFragmentation(100);
        OffHeapStorage ohs = (OffHeapStorage) stats;
        ohs.initialize(new NullOffHeapMemoryStats());
        assertEquals(0, stats.getFreeMemory());
        assertEquals(0, stats.getMaxMemory());
        assertEquals(0, stats.getUsedMemory());
        assertEquals(0, stats.getDefragmentations());
        assertEquals(0, stats.getDefragmentationsInProgress());
        assertEquals(0, stats.getDefragmentationTime());
        assertEquals(0, stats.getFragmentation());
        assertEquals(0, stats.getFragments());
        assertEquals(0, stats.getLargestFragment());
        assertEquals(0, stats.getObjects());
        assertEquals(0, stats.getReads());
        OutOfOffHeapMemoryException ex = null;
        try {
            ma.allocate(1024 * 1024 + 1);
            fail("expected OutOfOffHeapMemoryException");
        } catch (OutOfOffHeapMemoryException expected) {
            ex = expected;
        }
        verify(ooohml).outOfOffHeapMemory(ex);
        try {
            ma.allocate(1024 * 1024 + 1);
            fail("expected OutOfOffHeapMemoryException");
        } catch (OutOfOffHeapMemoryException expected) {
            ex = expected;
        }
        verify(ooohml).outOfOffHeapMemory(ex);
    } finally {
        System.setProperty(MemoryAllocatorImpl.FREE_OFF_HEAP_MEMORY_PROPERTY, "true");
        try {
            ma.close();
        } finally {
            System.clearProperty(MemoryAllocatorImpl.FREE_OFF_HEAP_MEMORY_PROPERTY);
        }
    }
}
Also used : OutOfOffHeapMemoryException(org.apache.geode.OutOfOffHeapMemoryException) LocalStatisticsFactory(org.apache.geode.internal.statistics.LocalStatisticsFactory) StatisticsFactory(org.apache.geode.StatisticsFactory) LocalStatisticsFactory(org.apache.geode.internal.statistics.LocalStatisticsFactory) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Example 4 with OutOfOffHeapMemoryException

use of org.apache.geode.OutOfOffHeapMemoryException in project geode by apache.

the class OffHeapManagementDUnitTest method doPut.

/**
   * Performs a put operation.
   *
   * @param key region entry key.
   * @param value region entry value.
   * @param regionName a region name.
   */
private void doPut(final Object key, final Object value, final String regionName, final boolean expectException) {
    Region region = getCache().getRegion(regionName);
    assertNotNull(region);
    try {
        region.put(key, value);
        if (expectException) {
            fail("Expected OutOfOffHeapMemoryException");
        }
    } catch (OutOfOffHeapMemoryException e) {
        if (!expectException) {
            throw e;
        }
    }
}
Also used : Region(org.apache.geode.cache.Region) OutOfOffHeapMemoryException(org.apache.geode.OutOfOffHeapMemoryException)

Example 5 with OutOfOffHeapMemoryException

use of org.apache.geode.OutOfOffHeapMemoryException in project geode by apache.

the class OutOfOffHeapMemoryDUnitTest method testSimpleOutOfOffHeapMemoryMemberDisconnects.

@Test
public void testSimpleOutOfOffHeapMemoryMemberDisconnects() {
    final DistributedSystem system = getSystem();
    final Cache cache = getCache();
    final DistributionManager dm = (DistributionManager) ((InternalDistributedSystem) system).getDistributionManager();
    Region<Object, Object> region = cache.createRegionFactory(getRegionShortcut()).setOffHeap(true).create(getRegionName());
    OutOfOffHeapMemoryException ooohme;
    try {
        Object value = new byte[1024];
        for (int i = 0; true; i++) {
            region.put("key-" + i, value);
        }
    } catch (OutOfOffHeapMemoryException e) {
        ooohme = e;
    }
    assertNotNull(ooohme);
    with().pollInterval(100, TimeUnit.MILLISECONDS).await().atMost(10, TimeUnit.SECONDS).until(() -> cache.isClosed() && !system.isConnected() && dm.isClosed());
    // wait for cache instance to be nulled out
    with().pollInterval(100, TimeUnit.MILLISECONDS).await().atMost(10, TimeUnit.SECONDS).until(() -> GemFireCacheImpl.getInstance() == null && InternalDistributedSystem.getAnyInstance() == null);
    assertNull(GemFireCacheImpl.getInstance());
    // verify system was closed out due to OutOfOffHeapMemoryException
    assertFalse(system.isConnected());
    InternalDistributedSystem ids = (InternalDistributedSystem) system;
    try {
        ids.getDistributionManager();
        fail("InternalDistributedSystem.getDistributionManager() should throw DistributedSystemDisconnectedException");
    } catch (DistributedSystemDisconnectedException expected) {
        assertRootCause(expected, OutOfOffHeapMemoryException.class);
    }
    // verify dm was closed out due to OutOfOffHeapMemoryException
    assertTrue(dm.isClosed());
    try {
        dm.throwIfDistributionStopped();
        fail("DistributionManager.throwIfDistributionStopped() should throw DistributedSystemDisconnectedException");
    } catch (DistributedSystemDisconnectedException expected) {
        assertRootCause(expected, OutOfOffHeapMemoryException.class);
    }
    // verify cache was closed out due to OutOfOffHeapMemoryException
    assertTrue(cache.isClosed());
    try {
        cache.getCancelCriterion().checkCancelInProgress(null);
        fail("GemFireCacheImpl.getCancelCriterion().checkCancelInProgress should throw DistributedSystemDisconnectedException");
    } catch (DistributedSystemDisconnectedException expected) {
        assertRootCause(expected, OutOfOffHeapMemoryException.class);
    }
}
Also used : DistributedSystemDisconnectedException(org.apache.geode.distributed.DistributedSystemDisconnectedException) OutOfOffHeapMemoryException(org.apache.geode.OutOfOffHeapMemoryException) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) DistributedSystem(org.apache.geode.distributed.DistributedSystem) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) DistributionManager(org.apache.geode.distributed.internal.DistributionManager) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

OutOfOffHeapMemoryException (org.apache.geode.OutOfOffHeapMemoryException)5 Test (org.junit.Test)2 StatisticsFactory (org.apache.geode.StatisticsFactory)1 Cache (org.apache.geode.cache.Cache)1 Region (org.apache.geode.cache.Region)1 DistributedSystem (org.apache.geode.distributed.DistributedSystem)1 DistributedSystemDisconnectedException (org.apache.geode.distributed.DistributedSystemDisconnectedException)1 DistributionManager (org.apache.geode.distributed.internal.DistributionManager)1 InternalDistributedSystem (org.apache.geode.distributed.internal.InternalDistributedSystem)1 LocalStatisticsFactory (org.apache.geode.internal.statistics.LocalStatisticsFactory)1 WaitCriterion (org.apache.geode.test.dunit.WaitCriterion)1 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)1 UnitTest (org.apache.geode.test.junit.categories.UnitTest)1