gridgain-7.5.11 Affinity history cleanup ignoring client discovery event.
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/e7ab8eef Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/e7ab8eef Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/e7ab8eef Branch: refs/heads/ignite-testing-discovery Commit: e7ab8eef504cdcf8987941e8b7a552ada451aec8 Parents: b2c934d Author: sboikov <[email protected]> Authored: Wed Apr 6 17:38:04 2016 +0300 Committer: sboikov <[email protected]> Committed: Wed Apr 6 17:38:04 2016 +0300 ---------------------------------------------------------------------- .../affinity/GridAffinityAssignment.java | 14 + .../affinity/GridAffinityAssignmentCache.java | 67 ++++- .../cache/GridCacheAffinityManager.java | 11 - .../GridCachePartitionExchangeManager.java | 14 - .../affinity/AffinityHistoryCleanupTest.java | 295 +++++++++++++++++++ .../ignite/testsuites/IgniteCacheTestSuite.java | 2 + 6 files changed, 363 insertions(+), 40 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/e7ab8eef/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityAssignment.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityAssignment.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityAssignment.java index 810843c..ef228cf 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityAssignment.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityAssignment.java @@ -47,6 +47,9 @@ class GridAffinityAssignment implements Serializable { /** Map of backup node partitions. */ private final Map<UUID, Set<Integer>> backup; + /** */ + private final boolean clientEvtChange; + /** * Constructs cached affinity calculations item. * @@ -56,6 +59,7 @@ class GridAffinityAssignment implements Serializable { this.topVer = topVer; primary = new HashMap<>(); backup = new HashMap<>(); + clientEvtChange = false; } /** @@ -68,6 +72,7 @@ class GridAffinityAssignment implements Serializable { primary = new HashMap<>(); backup = new HashMap<>(); + clientEvtChange = false; initPrimaryBackupMaps(); } @@ -82,6 +87,15 @@ class GridAffinityAssignment implements Serializable { assignment = aff.assignment; primary = aff.primary; backup = aff.backup; + + clientEvtChange = true; + } + + /** + * @return {@code True} if + */ + public boolean clientEventChange() { + return clientEvtChange; } /** http://git-wip-us.apache.org/repos/asf/ignite/blob/e7ab8eef/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityAssignmentCache.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityAssignmentCache.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityAssignmentCache.java index d728927..ac8d19d 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityAssignmentCache.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityAssignmentCache.java @@ -25,6 +25,7 @@ import java.util.Map; import java.util.Set; import java.util.UUID; import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; import org.apache.ignite.IgniteCheckedException; import org.apache.ignite.IgniteException; @@ -47,12 +48,17 @@ import org.jetbrains.annotations.Nullable; import org.jsr166.ConcurrentHashMap8; import org.jsr166.ConcurrentLinkedHashMap; +import static org.apache.ignite.IgniteSystemProperties.IGNITE_AFFINITY_HISTORY_SIZE; +import static org.apache.ignite.IgniteSystemProperties.getInteger; import static org.apache.ignite.internal.events.DiscoveryCustomEvent.EVT_DISCOVERY_CUSTOM_EVT; /** * Affinity cached function. */ public class GridAffinityAssignmentCache { + /** Cleanup history size. */ + private final int MAX_HIST_SIZE = getInteger(IGNITE_AFFINITY_HISTORY_SIZE, 500); + /** Cache name. */ private final String cacheName; @@ -86,6 +92,9 @@ public class GridAffinityAssignmentCache { /** Node stop flag. */ private volatile IgniteCheckedException stopErr; + /** History size ignoring*/ + private final AtomicInteger histSize = new AtomicInteger(); + /** * Constructs affinity cached calculations. * @@ -141,6 +150,8 @@ public class GridAffinityAssignmentCache { entry.getValue().onDone(topVer); } } + + onHistoryAdded(assignment); } /** @@ -244,6 +255,8 @@ public class GridAffinityAssignmentCache { } } + onHistoryAdded(updated); + return updated.assignment(); } @@ -274,6 +287,8 @@ public class GridAffinityAssignmentCache { entry.getValue().onDone(topVer); } } + + onHistoryAdded(assignmentCpy); } /** @@ -284,21 +299,6 @@ public class GridAffinityAssignmentCache { } /** - * Clean up outdated cache items. - * - * @param topVer Actual topology version, older versions will be removed. - */ - public void cleanUpCache(AffinityTopologyVersion topVer) { - if (log.isDebugEnabled()) - log.debug("Cleaning up cache for version [locNodeId=" + ctx.localNodeId() + - ", topVer=" + topVer + ']'); - - for (Iterator<AffinityTopologyVersion> it = affCache.keySet().iterator(); it.hasNext(); ) - if (it.next().compareTo(topVer) < 0) - it.remove(); - } - - /** * @param topVer Topology version. * @return Affinity assignment. */ @@ -483,6 +483,43 @@ public class GridAffinityAssignmentCache { } /** + * @param aff Added affinity assignment. + */ + private void onHistoryAdded(GridAffinityAssignment aff) { + int size; + + if (aff.clientEventChange()) + size = histSize.get(); + else + size = histSize.incrementAndGet(); + + int rmvCnt = size - MAX_HIST_SIZE; + + if (rmvCnt <= 0) { + int sizex = affCache.sizex(); + + if (sizex > MAX_HIST_SIZE * 2) + rmvCnt = MAX_HIST_SIZE; + } + + if (rmvCnt > 0) { + Iterator<GridAffinityAssignment> it = affCache.values().iterator(); + + while (it.hasNext() && rmvCnt > 0) { + GridAffinityAssignment aff0 = it.next(); + + it.remove(); + + rmvCnt--; + + if (!aff0.clientEventChange()) + histSize.decrementAndGet(); + } + } + } + + + /** * Affinity ready future. Will remove itself from ready futures map. */ private class AffinityReadyFuture extends GridFutureAdapter<AffinityTopologyVersion> { http://git-wip-us.apache.org/repos/asf/ignite/blob/e7ab8eef/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAffinityManager.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAffinityManager.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAffinityManager.java index eddffea..5afced8 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAffinityManager.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheAffinityManager.java @@ -126,17 +126,6 @@ public class GridCacheAffinityManager extends GridCacheManagerAdapter { } /** - * Clean up outdated cache items. - * - * @param topVer Actual topology version, older versions will be removed. - */ - public void cleanUpCache(AffinityTopologyVersion topVer) { - assert !cctx.isLocal(); - - aff.cleanUpCache(topVer); - } - - /** * Initializes affinity for joined node. * * @param topVer Topology version. http://git-wip-us.apache.org/repos/asf/ignite/blob/e7ab8eef/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCachePartitionExchangeManager.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCachePartitionExchangeManager.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCachePartitionExchangeManager.java index 14df1aa..5e91d01 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCachePartitionExchangeManager.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCachePartitionExchangeManager.java @@ -88,9 +88,7 @@ import org.jsr166.ConcurrentHashMap8; import org.jsr166.ConcurrentLinkedDeque8; import static java.util.concurrent.TimeUnit.MILLISECONDS; -import static org.apache.ignite.IgniteSystemProperties.IGNITE_AFFINITY_HISTORY_SIZE; import static org.apache.ignite.IgniteSystemProperties.IGNITE_PRELOAD_RESEND_TIMEOUT; -import static org.apache.ignite.IgniteSystemProperties.getInteger; import static org.apache.ignite.IgniteSystemProperties.getLong; import static org.apache.ignite.events.EventType.EVT_CACHE_REBALANCE_STARTED; import static org.apache.ignite.events.EventType.EVT_NODE_FAILED; @@ -108,9 +106,6 @@ public class GridCachePartitionExchangeManager<K, V> extends GridCacheSharedMana /** Exchange history size. */ private static final int EXCHANGE_HISTORY_SIZE = 1000; - /** Cleanup history size. */ - public static final int EXCH_FUT_CLEANUP_HISTORY_SIZE = getInteger(IGNITE_AFFINITY_HISTORY_SIZE, 1000); - /** Atomic reference for pending timeout object. */ private AtomicReference<ResendTimeoutObject> pendingResend = new AtomicReference<>(); @@ -938,15 +933,6 @@ public class GridCachePartitionExchangeManager<K, V> extends GridCacheSharedMana skipped++; - if (skipped == EXCH_FUT_CLEANUP_HISTORY_SIZE) { - for (GridCacheContext cacheCtx : cctx.cacheContexts()) { - if (err == null) { - if (!cacheCtx.isLocal()) - cacheCtx.affinity().cleanUpCache(fut.topologyVersion()); - } - } - } - if (skipped > 10) fut.cleanUp(); } http://git-wip-us.apache.org/repos/asf/ignite/blob/e7ab8eef/modules/core/src/test/java/org/apache/ignite/cache/affinity/AffinityHistoryCleanupTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/cache/affinity/AffinityHistoryCleanupTest.java b/modules/core/src/test/java/org/apache/ignite/cache/affinity/AffinityHistoryCleanupTest.java new file mode 100644 index 0000000..39a2a76 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/cache/affinity/AffinityHistoryCleanupTest.java @@ -0,0 +1,295 @@ +/* + * 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.cache.affinity; + +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteSystemProperties; +import org.apache.ignite.cache.affinity.fair.FairAffinityFunction; +import org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.internal.IgniteKernal; +import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion; +import org.apache.ignite.internal.processors.affinity.GridAffinityAssignmentCache; +import org.apache.ignite.internal.processors.cache.GridCacheContext; +import org.apache.ignite.internal.processors.cache.GridCacheProcessor; +import org.apache.ignite.internal.util.typedef.F; +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; + +/** + * + */ +public class AffinityHistoryCleanupTest extends GridCommonAbstractTest { + /** */ + private static final TcpDiscoveryIpFinder ipFinder = new TcpDiscoveryVmIpFinder(true); + + /** */ + private boolean client; + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(gridName); + + ((TcpDiscoverySpi)cfg.getDiscoverySpi()).setIpFinder(ipFinder); + + CacheConfiguration[] ccfgs = new CacheConfiguration[4]; + + for (int i = 0; i < ccfgs.length; i++) { + CacheConfiguration ccfg = new CacheConfiguration(); + + ccfg.setName("static-cache-" + i); + ccfg.setAffinity(i % 2 == 0 ? new RendezvousAffinityFunction() : new FairAffinityFunction()); + + ccfgs[i] = ccfg; + } + + cfg.setCacheConfiguration(ccfgs); + + cfg.setClientMode(client); + + return cfg; + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + stopAllGrids(); + + super.afterTest(); + } + + /** + * @throws Exception If failed. + */ + public void testAffinityHistoryCleanup() throws Exception { + String histProp = System.getProperty(IgniteSystemProperties.IGNITE_AFFINITY_HISTORY_SIZE); + + try { + System.setProperty(IgniteSystemProperties.IGNITE_AFFINITY_HISTORY_SIZE, "5"); + + Ignite ignite = startGrid(0); + + checkHistory(ignite, F.asList(topVer(1, 0)), 1); + + for (int i = 0; i < 3; i++) { + startGrid(1); + + stopGrid(1); + } + + checkHistory(ignite, F.asList( + topVer(3, 0), + topVer(4, 0), + topVer(5, 0), + topVer(6, 0), + topVer(7, 0)), + 5); + + client = true; + + startGrid(1); + + stopGrid(1); + + checkHistory(ignite, F.asList( + topVer(3, 0), + topVer(4, 0), + topVer(5, 0), + topVer(6, 0), + topVer(7, 0), + topVer(8, 0), + topVer(9, 0)), + 5); + + startGrid(1); + + stopGrid(1); + + checkHistory(ignite, F.asList( + topVer(3, 0), + topVer(4, 0), + topVer(5, 0), + topVer(6, 0), + topVer(7, 0), + topVer(8, 0), + topVer(9, 0), + topVer(10, 0), + topVer(11, 0)), + 5); + + startGrid(1); + + checkHistory(ignite, F.asList( + topVer(3, 0), + topVer(4, 0), + topVer(5, 0), + topVer(6, 0), + topVer(7, 0), + topVer(8, 0), + topVer(9, 0), + topVer(10, 0), + topVer(11, 0), + topVer(12, 0)), + 5); + + stopGrid(1); + + checkHistory(ignite, F.asList( + topVer(8, 0), + topVer(9, 0), + topVer(10, 0), + topVer(11, 0), + topVer(12, 0), + topVer(13, 0)), + 0); + + client = false; + + startGrid(1); + + stopGrid(1); + + checkHistory(ignite, F.asList( + topVer(8, 0), + topVer(9, 0), + topVer(10, 0), + topVer(11, 0), + topVer(12, 0), + topVer(13, 0), + topVer(14, 0), + topVer(15, 0)), + 2); + + startGrid(1); + + stopGrid(1); + + checkHistory(ignite, F.asList( + topVer(8, 0), + topVer(9, 0), + topVer(10, 0), + topVer(11, 0), + topVer(12, 0), + topVer(13, 0), + topVer(14, 0), + topVer(15, 0), + topVer(16, 0), + topVer(17, 0)), + 4); + + startGrid(1); + + checkHistory(ignite, F.asList( + topVer(13, 0), + topVer(14, 0), + topVer(15, 0), + topVer(16, 0), + topVer(17, 0), + topVer(18, 0)), + 5); + + stopGrid(1); + + checkHistory(ignite, F.asList( + topVer(14, 0), + topVer(15, 0), + topVer(16, 0), + topVer(17, 0), + topVer(18, 0), + topVer(19, 0)), + 6); + + startGrid(1); + + checkHistory(ignite, F.asList( + topVer(16, 0), + topVer(17, 0), + topVer(18, 0), + topVer(19, 0), + topVer(20, 0)), + 5); + + client = true; + + startGrid(2); + + stopGrid(2); + + checkHistory(ignite, F.asList( + topVer(16, 0), + topVer(17, 0), + topVer(18, 0), + topVer(19, 0), + topVer(20, 0), + topVer(21, 0), + topVer(22, 0)), + 5); + } + finally { + if (histProp != null) + System.setProperty(IgniteSystemProperties.IGNITE_AFFINITY_HISTORY_SIZE, histProp); + else + System.clearProperty(IgniteSystemProperties.IGNITE_AFFINITY_HISTORY_SIZE); + } + } + + /** + * @param ignite Node. + * @param expHist Expected history. + * @param expSize Expected 'non client events' history size. + */ + private void checkHistory(Ignite ignite, List<AffinityTopologyVersion> expHist, int expSize) { + GridCacheProcessor proc = ((IgniteKernal)ignite).context().cache(); + + int cnt = 0; + + for (GridCacheContext cctx : proc.context().cacheContexts()) { + GridAffinityAssignmentCache aff = GridTestUtils.getFieldValue(cctx.affinity(), "aff"); + + AtomicInteger histSize = GridTestUtils.getFieldValue(aff, "histSize"); + + assertEquals(expSize, histSize.get()); + + Map<AffinityTopologyVersion, Object> cache = GridTestUtils.getFieldValue(aff, "affCache"); + + assertEquals("Unexpected history: " + cache.keySet(), expHist.size(), cache.size()); + + for (AffinityTopologyVersion topVer : expHist) + assertTrue("No history [ver=" + topVer + ", hist=" + cache.keySet() + ']', cache.containsKey(topVer)); + + cnt++; + } + + assert cnt > 4; + } + + /** + * @param major Major version. + * @param minor Minor version. + * @return Version. + */ + private static AffinityTopologyVersion topVer(int major, int minor) { + return new AffinityTopologyVersion(major, minor); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/e7ab8eef/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite.java b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite.java index 9892ff7..7f1d7df 100644 --- a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite.java +++ b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite.java @@ -23,6 +23,7 @@ import org.apache.ignite.GridCacheAffinityBackupsSelfTest; import org.apache.ignite.IgniteCacheAffinitySelfTest; import org.apache.ignite.cache.IgniteWarmupClosureSelfTest; import org.apache.ignite.cache.affinity.AffinityClientNodeSelfTest; +import org.apache.ignite.cache.affinity.AffinityHistoryCleanupTest; import org.apache.ignite.cache.affinity.fair.FairAffinityDynamicCacheSelfTest; import org.apache.ignite.cache.affinity.fair.FairAffinityFunctionNodesSelfTest; import org.apache.ignite.cache.affinity.fair.FairAffinityFunctionSelfTest; @@ -209,6 +210,7 @@ public class IgniteCacheTestSuite extends TestSuite { suite.addTestSuite(GridCacheAffinityBackupsSelfTest.class); suite.addTestSuite(IgniteCacheAffinitySelfTest.class); suite.addTestSuite(AffinityClientNodeSelfTest.class); + suite.addTestSuite(AffinityHistoryCleanupTest.class); // Swap tests. suite.addTestSuite(GridCacheSwapPreloadSelfTest.class);
