Repository: ignite
Updated Branches:
  refs/heads/master f6d42f3e3 -> 78c371208


ignite-2714 Cleanup GridOffHeapPartitionedMap when cache is destroyed


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/78c37120
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/78c37120
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/78c37120

Branch: refs/heads/master
Commit: 78c3712085e3cd617c659b9f3c2c8e93741a61d7
Parents: f6d42f3e
Author: Andrey Martianov <amartia...@gridgain.com>
Authored: Tue Sep 20 17:11:23 2016 +0300
Committer: sboikov <sboi...@gridgain.com>
Committed: Tue Sep 20 17:11:23 2016 +0300

----------------------------------------------------------------------
 .../processors/cache/GridCacheSwapManager.java  |   7 +
 .../offheap/GridOffHeapProcessor.java           |  14 ++
 .../cache/GridCacheOffHeapCleanupTest.java      | 169 +++++++++++++++++++
 .../testsuites/IgniteCacheTestSuite5.java       |   3 +
 4 files changed, 193 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/78c37120/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheSwapManager.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheSwapManager.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheSwapManager.java
index fd0b471..8ba960a 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheSwapManager.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheSwapManager.java
@@ -138,6 +138,13 @@ public class GridCacheSwapManager extends 
GridCacheManagerAdapter {
             initOffHeap();
     }
 
+
+    /** {@inheritDoc} */
+    @Override protected void stop0(boolean cancel) {
+        if (offheapEnabled)
+            offheap.destruct(spaceName);
+    }
+
     /**
      *
      */

http://git-wip-us.apache.org/repos/asf/ignite/blob/78c37120/modules/core/src/main/java/org/apache/ignite/internal/processors/offheap/GridOffHeapProcessor.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/offheap/GridOffHeapProcessor.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/offheap/GridOffHeapProcessor.java
index d9d4421..912b826 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/offheap/GridOffHeapProcessor.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/offheap/GridOffHeapProcessor.java
@@ -78,6 +78,20 @@ public class GridOffHeapProcessor extends 
GridProcessorAdapter {
             old.destruct();
     }
 
+    /**
+     * Destructs offheap map for given space name.
+     *
+     * @param spaceName Space name.
+     * */
+    public void destruct(@Nullable String spaceName) {
+        spaceName = maskNull(spaceName);
+
+        GridOffHeapPartitionedMap map = offheap.remove(spaceName);
+
+        if (map != null)
+            map.destruct();
+    }
+
     /** {@inheritDoc} */
     @Override public void stop(boolean cancel) throws IgniteCheckedException {
         super.stop(cancel);

http://git-wip-us.apache.org/repos/asf/ignite/blob/78c37120/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheOffHeapCleanupTest.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheOffHeapCleanupTest.java
 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheOffHeapCleanupTest.java
new file mode 100644
index 0000000..ae94073
--- /dev/null
+++ 
b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheOffHeapCleanupTest.java
@@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.ignite.internal.processors.cache;
+
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.cache.CacheMemoryMode;
+import org.apache.ignite.cache.eviction.EvictionPolicy;
+import org.apache.ignite.cache.eviction.fifo.FifoEvictionPolicy;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.util.offheap.unsafe.GridUnsafeMemory;
+import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
+import org.apache.ignite.spi.discovery.tcp.ipfinder.TcpDiscoveryIpFinder;
+import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+
+import static org.apache.ignite.cache.CacheMemoryMode.OFFHEAP_TIERED;
+import static org.apache.ignite.cache.CacheMemoryMode.OFFHEAP_VALUES;
+import static org.apache.ignite.cache.CacheMemoryMode.ONHEAP_TIERED;
+
+/**
+ * Check offheap allocations are freed after cache destroy.
+ */
+public class GridCacheOffHeapCleanupTest extends GridCommonAbstractTest {
+    /** IP finder. */
+    private static final TcpDiscoveryIpFinder ipFinder = new 
TcpDiscoveryVmIpFinder(true);
+
+    /** */
+    private static final String CACHE_NAME = "testCache";
+
+    /** Memory mode. */
+    private CacheMemoryMode memoryMode;
+
+    /** Eviction policy. */
+    private EvictionPolicy evictionPlc;
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String gridName) 
throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(gridName);
+
+        ((TcpDiscoverySpi)cfg.getDiscoverySpi()).setIpFinder(ipFinder);
+
+        return cfg;
+    }
+
+    /**
+     * Checks offheap resources are freed after cache destroy - ONHEAP_TIERED 
memory mode
+     *
+     * @throws Exception If failed.
+     */
+    public void testCleanupOffheapAfterCacheDestroyOnheapTiered() throws 
Exception {
+        memoryMode = ONHEAP_TIERED;
+
+        FifoEvictionPolicy evictionPlc0 = new FifoEvictionPolicy();
+        evictionPlc0.setMaxSize(1);
+
+        evictionPlc = evictionPlc0;
+
+        checkCleanupOffheapAfterCacheDestroy();
+    }
+
+    /**
+     * Checks offheap resources are freed after cache destroy - OFFHEAP_TIERED 
memory mode
+     *
+     * @throws Exception If failed.
+     */
+    public void testCleanupOffheapAfterCacheDestroyOffheapTiered() throws 
Exception {
+        memoryMode = OFFHEAP_TIERED;
+        evictionPlc = null;
+
+        checkCleanupOffheapAfterCacheDestroy();
+    }
+
+    /**
+     * TODO: IGNITE-2714.
+     *
+     * Checks offheap resources are freed after cache destroy - OFFHEAP_VALUES 
memory mode
+     *
+     * @throws Exception If failed.
+     */
+    public void _testCleanupOffheapAfterCacheDestroyOffheapValues() throws 
Exception {
+        memoryMode = OFFHEAP_VALUES;
+        evictionPlc = null;
+
+        try (Ignite g = startGrid(0)) {
+            IgniteCache<Integer, String> cache = 
g.getOrCreateCache(createCacheConfiguration());
+
+            cache.put(1, "value_1");
+            cache.put(2, "value_2");
+
+            GridCacheContext ctx =  GridTestUtils.cacheContext(cache);
+            GridUnsafeMemory unsafeMemory = ctx.unsafeMemory();
+
+            g.destroyCache(null);
+
+            if (unsafeMemory != null)
+                assertEquals("Unsafe memory not freed", 0, 
unsafeMemory.allocatedSize());
+        }
+    }
+
+    /**
+     * Creates cache configuration.
+     *
+     * @return cache configuration.
+     * */
+    private CacheConfiguration<Integer, String> createCacheConfiguration() {
+        CacheConfiguration<Integer, String> ccfg = new CacheConfiguration<>();
+
+        ccfg.setName(CACHE_NAME);
+        ccfg.setOffHeapMaxMemory(0);
+        ccfg.setMemoryMode(memoryMode);
+        ccfg.setEvictionPolicy(evictionPlc);
+
+        return ccfg;
+    }
+
+    /**
+     * Check offheap resources are freed after cache destroy.
+     *
+     * @throws Exception If failed.
+     */
+    private void checkCleanupOffheapAfterCacheDestroy() throws Exception {
+        final String spaceName = "gg-swap-cache-" + CACHE_NAME;
+
+        try (Ignite g = startGrid(0)) {
+            checkOffheapAllocated(spaceName, false);
+
+            IgniteCache<Integer, String> cache = 
g.getOrCreateCache(createCacheConfiguration());
+
+            cache.put(1, "value_1");
+            cache.put(2, "value_2");
+
+            checkOffheapAllocated(spaceName, true);
+
+            g.destroyCache(cache.getName());
+
+            checkOffheapAllocated(spaceName, false);
+        }
+    }
+
+    /**
+     * Check is offheap allocated for given space name using internal API.
+     *
+     * @param spaceName Space name.
+     * @param allocated true, if we expected that offheap is allocated; false, 
otherwise.
+     * @throws Exception If failed.
+     * */
+    private void checkOffheapAllocated(String spaceName, boolean allocated) 
throws Exception {
+        long offheapSize = 
grid(0).context().offheap().allocatedSize(spaceName);
+
+        assertEquals("Unexpected offheap allocated size", allocated, 
(offheapSize >= 0));
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/78c37120/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite5.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite5.java
 
b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite5.java
index 7582f5c..7f0e23c 100644
--- 
a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite5.java
+++ 
b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite5.java
@@ -25,6 +25,7 @@ import 
org.apache.ignite.internal.processors.cache.EntryVersionConsistencyReadTh
 import 
org.apache.ignite.internal.processors.cache.IgniteCachePutStackOverflowSelfTest;
 import 
org.apache.ignite.internal.processors.cache.IgniteCacheReadThroughEvictionsVariationsSuite;
 import 
org.apache.ignite.internal.processors.cache.IgniteCacheStoreCollectionTest;
+import org.apache.ignite.internal.processors.cache.GridCacheOffHeapCleanupTest;
 import 
org.apache.ignite.internal.processors.cache.distributed.CacheLateAffinityAssignmentFairAffinityTest;
 import 
org.apache.ignite.internal.processors.cache.distributed.CacheLateAffinityAssignmentNodeJoinValidationTest;
 import 
org.apache.ignite.internal.processors.cache.distributed.CacheLateAffinityAssignmentTest;
@@ -59,6 +60,8 @@ public class IgniteCacheTestSuite5 extends TestSuite {
         suite.addTest(IgniteCacheReadThroughEvictionsVariationsSuite.suite());
         suite.addTestSuite(IgniteCacheTxIteratorSelfTest.class);
 
+        suite.addTestSuite(GridCacheOffHeapCleanupTest.class);
+
         return suite;
     }
 }

Reply via email to