diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheAffinitySharedManager.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheAffinitySharedManager.java index 75cacaab01ad0..c52e607e91797 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheAffinitySharedManager.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/CacheAffinitySharedManager.java @@ -2127,6 +2127,18 @@ private void initAffinityOnNodeJoin(final GridDhtPartitionsExchangeFuture fut, b forAllRegisteredCacheGroups(new IgniteInClosureX() { @Override public void applyx(CacheGroupDescriptor desc) throws IgniteCheckedException { + AffinityTopologyVersion grpStartTopVer = desc.startTopologyVersion(); + + if (grpStartTopVer != null && grpStartTopVer.after(fut.initialVersion())) { + if (log.isDebugEnabled()) { + log.debug("Skip affinity initialization for cache group started after current exchange " + + "[grp=" + desc.cacheOrGroupName() + ", curTopVer=" + fut.initialVersion() + + ", grpStartTopVer=" + grpStartTopVer + ']'); + } + + return; + } + CacheGroupHolder grpHolder = getOrCreateGroupHolder(evts.topologyVersion(), desc); CacheGroupHolder cache = getOrCreateGroupHolder(evts.topologyVersion(), desc); 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 c63df8c543b75..7e2e2090d9256 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 @@ -3214,8 +3214,15 @@ else if (task instanceof ForceRebalanceExchangeTask) { // same event. boolean changed = false; - for (CacheGroupContext grp : cctx.cache().cacheGroups()) + for (CacheGroupContext grp : cctx.cache().cacheGroups()) { + AffinityTopologyVersion grpStartTopVer = + cctx.cache().cacheGroupDescriptor(grp.groupId()).startTopologyVersion(); + + if (grpStartTopVer != null && grpStartTopVer.after(exchFut.initialVersion())) + continue; + changed |= grp.topology().afterExchange(exchFut); + } if (!cctx.kernalContext().clientNode() && changed) { if (log.isDebugEnabled()) @@ -3226,7 +3233,18 @@ else if (task instanceof ForceRebalanceExchangeTask) { } if (rebalanceRequired(exchFut)) { + final AffinityTopologyVersion exchInitVer = exchFut != null ? exchFut.initialVersion() : null; + NavigableSet assignsSet = cctx.cache().cacheGroups().stream() + .filter(grp -> { + if (exchInitVer == null) + return true; + + AffinityTopologyVersion grpStartTopVer = + cctx.cache().cacheGroupDescriptor(grp.groupId()).startTopologyVersion(); + + return grpStartTopVer == null || !grpStartTopVer.after(exchInitVer); + }) .collect(Collectors.toCollection(() -> new TreeSet<>(new CacheRebalanceOrderComparator()))); RebalanceFuture next = null; diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionsExchangeFuture.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionsExchangeFuture.java index 8c758424150a6..115c182c4575a 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionsExchangeFuture.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/distributed/dht/preloader/GridDhtPartitionsExchangeFuture.java @@ -2391,8 +2391,14 @@ private String exchangeTimingsLogMessage(String header, List timings) { cleanIdxRebuildFutures = false; - for (CacheGroupContext grp : cctx.cache().cacheGroups()) + for (CacheGroupContext grp : cctx.cache().cacheGroups()) { + AffinityTopologyVersion grpStartTopVer = cctx.cache().cacheGroupDescriptor(grp.groupId()).startTopologyVersion(); + + if (grpStartTopVer != null && grpStartTopVer.after(initialVersion())) + continue; + grp.topology().onExchangeDone(this, grp.affinity().readyAffinity(res), false); + } if (changedAffinity()) cctx.walState().disableGroupDurabilityForPreloading(this); @@ -3690,6 +3696,11 @@ private void finishExchangeOnCoordinator(@Nullable Collection sndRe cctx.kernalContext().pools().getSystemExecutorService(), cctx.affinity().cacheGroups().values(), desc -> { + AffinityTopologyVersion grpStartTopVer = desc.startTopologyVersion(); + + if (grpStartTopVer != null && grpStartTopVer.after(initialVersion())) + return null; + if (!enterBusy()) return null; @@ -4595,6 +4606,12 @@ private void updatePartitionFullMap(AffinityTopologyVersion resTopVer, GridDhtPa parallelismLvl, cctx.kernalContext().pools().getSystemExecutorService(), msg.partitions().keySet(), grpId -> { + CacheGroupDescriptor desc = cctx.cache().cacheGroupDescriptor(grpId); + + if (desc != null && desc.startTopologyVersion() != null && + desc.startTopologyVersion().after(initialVersion())) + return null; + CachePartitionFullCountersMap cntrMap = msg.partitionUpdateCounters(grpId); CacheGroupContext grp = cctx.cache().cacheGroup(grpId); diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/DynamicCacheOnNodeJoinTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/DynamicCacheOnNodeJoinTest.java new file mode 100644 index 0000000000000..a5eedd3af8a8d --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/DynamicCacheOnNodeJoinTest.java @@ -0,0 +1,269 @@ +/* + * 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 java.lang.reflect.Method; +import java.util.concurrent.atomic.AtomicReference; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.cache.CacheMode; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.DataRegionConfiguration; +import org.apache.ignite.configuration.DataStorageConfiguration; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.failure.AbstractFailureHandler; +import org.apache.ignite.failure.FailureContext; +import org.apache.ignite.internal.IgniteEx; +import org.apache.ignite.internal.IgniteInternalFuture; +import org.apache.ignite.internal.TestRecordingCommunicationSpi; +import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion; +import org.apache.ignite.internal.processors.affinity.GridAffinityAssignmentCache; +import org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture; +import org.apache.ignite.internal.util.typedef.internal.CU; +import org.apache.ignite.internal.util.typedef.X; +import org.apache.ignite.testframework.GridTestUtils; +import org.apache.ignite.testframework.ListeningTestLogger; +import org.apache.ignite.testframework.LogListener; +import org.apache.ignite.testframework.junits.GridAbstractTest; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.junit.Test; + +import static org.apache.ignite.cache.CacheAtomicityMode.ATOMIC; +import static org.apache.ignite.cluster.ClusterState.ACTIVE; +import static org.apache.ignite.internal.TestRecordingCommunicationSpi.blockSingleExhangeMessage; + +/** */ +public class DynamicCacheOnNodeJoinTest extends GridCommonAbstractTest { + /** */ + private static final String CACHE_NAME = "cacheIgn3576"; + + /** */ + private static final String GROUP_NAME = "testGrpIgn3576"; + + /** */ + private static final String STABLE_CACHE_NAME = "stableCacheIgn3576"; + + /** */ + private static final String STABLE_GROUP_NAME = "stableGrpIgn3576"; + + /** */ + private final AtomicReference failureCtx = new AtomicReference<>(); + + /** */ + private final TestRecordingCommunicationSpi joiningNodeSpi = new TestRecordingCommunicationSpi(); + + /** */ + private final ListeningTestLogger testLog = new ListeningTestLogger(GridAbstractTest.log); + + /** */ + private final LogListener tryMergeLsnr = LogListener.matches( + "Coordinator received all messages, try merge").build(); + + /** */ + private final LogListener customEventLsnr = LogListener.matches( + "Stop merge, custom event found").build(); + + /** */ + private final LogListener partValidationLsnr = LogListener.matches( + "Partition states validation has failed").atMost(0).build(); + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName); + + cfg.setGridLogger(testLog); + cfg.setConsistentId(igniteInstanceName); + cfg.setDataStorageConfiguration(new DataStorageConfiguration().setDefaultDataRegionConfiguration( + new DataRegionConfiguration().setPersistenceEnabled(false))); + + cfg.setFailureHandler(new AbstractFailureHandler() { + /** {@inheritDoc} */ + @Override protected boolean handle(Ignite ignite, FailureContext failureCtx) { + DynamicCacheOnNodeJoinTest.this.failureCtx.compareAndSet(null, failureCtx); + + return false; + } + }); + + if (getTestIgniteInstanceName(2).equals(igniteInstanceName)) + cfg.setCommunicationSpi(joiningNodeSpi); + else + cfg.setCommunicationSpi(new TestRecordingCommunicationSpi()); + + return cfg; + } + + /** {@inheritDoc} */ + @Override protected void beforeTest() throws Exception { + super.beforeTest(); + + testLog.registerAllListeners(tryMergeLsnr, customEventLsnr, partValidationLsnr); + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + stopAllGrids(); + + super.afterTest(); + } + + /** {@inheritDoc} */ + @Override protected long getTestTimeout() { + return 60_000; + } + + /** */ + @Test + public void testForceRebalance() throws Exception { + IgniteEx ignite = startGrid(1); + + ignite.cluster().state(ACTIVE); + + IgniteCache cache = ignite.getOrCreateCache( + cacheConfiguration(STABLE_CACHE_NAME, STABLE_GROUP_NAME)); + + awaitPartitionMapExchange(); + + cache.rebalance().get(30_000); + + assertNull("Unexpected critical failure during forced rebalance.", failureCtx.get()); + } + + /** */ + @Test + public void testDynamicCacheStartDuringNodeJoinExchange() throws Exception { + IgniteEx crd = startGrid(1); + + crd.cluster().state(ACTIVE); + + awaitPartitionMapExchange(); + + assertEquals(1, crd.cluster().currentBaselineTopology().size()); + + crd.getOrCreateCache(cacheConfiguration(STABLE_CACHE_NAME, STABLE_GROUP_NAME)); + + awaitPartitionMapExchange(); + + GridCachePartitionExchangeManager exchMgr = crd.context().cache().context().exchange(); + + exchMgr.mergeExchangesTestWaitVersion(new AffinityTopologyVersion(2, 1), null); + + joiningNodeSpi.blockMessages(blockSingleExhangeMessage()); + + IgniteInternalFuture startFut = GridTestUtils.runAsync(() -> startGrid(2), "node-starter"); + + try { + assertTrue("Failed to block the joining node's single message.", + joiningNodeSpi.waitForBlocked(1, 10_000)); + + IgniteInternalFuture> cacheStartFut = GridTestUtils.runAsync( + () -> crd.getOrCreateCache(cacheConfiguration(CACHE_NAME, GROUP_NAME)), "cache-starter"); + + AffinityTopologyVersion cacheStartTopVer = new AffinityTopologyVersion(2, 1); + + assertTrue("The dynamic cache exchange was not queued.", GridTestUtils.waitForCondition( + () -> cacheExchangeFuture(exchMgr, cacheStartTopVer) != null, 10_000)); + + GridDhtPartitionsExchangeFuture cacheExchFut = cacheExchangeFuture(exchMgr, cacheStartTopVer); + + CacheAffinitySharedManager affMgr = crd.context().cache().context().affinity(); + + GridTestUtils.setFieldValue(cacheExchFut, "exchCtx", + new ExchangeContext(crd.context().cache().context(), true, cacheExchFut)); + + CachesRegistry cachesRegistry = GridTestUtils.getFieldValue(affMgr, "cachesRegistry"); + + cachesRegistry.update(cacheExchFut.exchangeActions()); + + int grpId = CU.cacheGroupId(CACHE_NAME, GROUP_NAME); + + CacheGroupDescriptor grpDesc = crd.context().cache().cacheGroupDescriptor(grpId); + + Method holderMtd = affMgr.getClass().getDeclaredMethod("getOrCreateGroupHolder", + AffinityTopologyVersion.class, CacheGroupDescriptor.class); + + holderMtd.setAccessible(true); + + holderMtd.invoke(affMgr, new AffinityTopologyVersion(2, 0), grpDesc); + + GridAffinityAssignmentCache aff = affMgr.affinity(grpId); + + Method calculateMtd = affMgr.getClass().getDeclaredMethod("calculateAndInit", + ExchangeDiscoveryEvents.class, GridAffinityAssignmentCache.class, AffinityTopologyVersion.class); + + calculateMtd.setAccessible(true); + calculateMtd.invoke(affMgr, cacheExchFut.context().events(), aff, cacheStartTopVer); + + assertNotNull("The previous affinity assignment was not initialized.", aff.idealAssignmentRaw()); + + joiningNodeSpi.stopBlock(); + + startFut.get(30_000); + cacheStartFut.get(30_000); + + awaitPartitionMapExchange(); + + assertTrue("The merge protocol was not entered.", tryMergeLsnr.check()); + assertTrue("The dynamic cache start was not queued during the node join exchange.", + customEventLsnr.check()); + assertTrue("Partition states validation failed.", partValidationLsnr.check()); + + FailureContext failure = failureCtx.get(); + + assertNull("Unexpected critical failure: " + (failure == null ? null : X.getFullStackTrace(failure.error())), + failure); + + for (int p = 0; p < crd.affinity(CACHE_NAME).partitions(); p++) + assertNotNull("No primary node for partition " + p, crd.affinity(CACHE_NAME).mapPartitionToNode(p)); + } + finally { + joiningNodeSpi.stopBlock(); + } + } + + /** + * @param exchMgr Exchange manager. + * @param topVer Exchange topology version. + * @return Cache start exchange future or {@code null}. + */ + private GridDhtPartitionsExchangeFuture cacheExchangeFuture( + GridCachePartitionExchangeManager exchMgr, + AffinityTopologyVersion topVer + ) { + for (GridDhtPartitionsExchangeFuture fut : exchMgr.exchangeFutures()) { + if (topVer.equals(fut.initialVersion()) && fut.exchangeActions() != null) + return fut; + } + + return null; + } + + /** + * @param name Cache name. + * @param grpName Group name. + * @return Cache configuration. + */ + private CacheConfiguration cacheConfiguration(String name, String grpName) { + return new CacheConfiguration() + .setName(name) + .setGroupName(grpName) + .setCacheMode(CacheMode.PARTITIONED) + .setAtomicityMode(ATOMIC) + .setBackups(0); + } +} diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite6.java b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite6.java index 9a606cf27b8f4..9d23a94184952 100644 --- a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite6.java +++ b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteCacheTestSuite6.java @@ -25,6 +25,7 @@ import org.apache.ignite.internal.processors.cache.CacheIgniteOutOfMemoryExceptionTest; import org.apache.ignite.internal.processors.cache.CacheNoAffinityExchangeTest; import org.apache.ignite.internal.processors.cache.ClientFastReplyCoordinatorFailureTest; +import org.apache.ignite.internal.processors.cache.DynamicCacheOnNodeJoinTest; import org.apache.ignite.internal.processors.cache.IgniteOutOfMemoryPropagationTest; import org.apache.ignite.internal.processors.cache.PartitionedAtomicCacheGetsDistributionTest; import org.apache.ignite.internal.processors.cache.PartitionedTransactionalPessimisticCacheGetsDistributionTest; @@ -82,6 +83,7 @@ public static List> suite(Collection ignoredTests) { GridTestUtils.addTestIfNeeded(suite, PendingExchangeTest.class, ignoredTests); GridTestUtils.addTestIfNeeded(suite, ExchangeMergeStaleServerNodesTest.class, ignoredTests); GridTestUtils.addTestIfNeeded(suite, ClientFastReplyCoordinatorFailureTest.class, ignoredTests); + GridTestUtils.addTestIfNeeded(suite, DynamicCacheOnNodeJoinTest.class, ignoredTests); GridTestUtils.addTestIfNeeded(suite, TxRollbackOnTimeoutTest.class, ignoredTests); GridTestUtils.addTestIfNeeded(suite, IgniteCacheThreadLocalTxTest.class, ignoredTests);