xtern commented on a change in pull request #9047: URL: https://github.com/apache/ignite/pull/9047#discussion_r623824121
########## File path: modules/core/src/test/java/org/apache/ignite/internal/processors/cache/persistence/snapshot/IgniteSnapshotWithMetastorageTest.java ########## @@ -0,0 +1,221 @@ +/* + * 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.persistence.snapshot; + +import java.io.Serializable; +import java.util.Collections; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.FutureTask; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.RunnableFuture; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.BiConsumer; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import org.apache.ignite.IgniteCheckedException; +import org.apache.ignite.IgniteException; +import org.apache.ignite.internal.IgniteEx; +import org.apache.ignite.internal.IgniteInternalFuture; +import org.apache.ignite.internal.processors.cache.GridCacheSharedContext; +import org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsExchangeFuture; +import org.apache.ignite.internal.processors.cache.distributed.dht.preloader.PartitionsExchangeAware; +import org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager; +import org.apache.ignite.internal.processors.cache.persistence.checkpoint.CheckpointListener; +import org.apache.ignite.internal.processors.metastorage.persistence.DistributedMetaStorageImpl; +import org.apache.ignite.internal.processors.metastorage.persistence.DmsDataWriterWorker; +import org.apache.ignite.internal.util.typedef.internal.U; +import org.apache.ignite.lang.IgniteFuture; +import org.apache.ignite.testframework.GridTestUtils; +import org.junit.Test; + +import static org.apache.ignite.internal.processors.cache.persistence.snapshot.IgniteSnapshotManager.resolveSnapshotWorkDirectory; + +/** + * Cluster-wide snapshot with distributed metastorage test. + */ +public class IgniteSnapshotWithMetastorageTest extends AbstractSnapshotSelfTest { + /** */ + private static final String SNAPSHOT_PREFIX = "SNAPSHOT_PREFIX_"; + + /** @throws Exception If fails. */ + @Test + public void testClusterSnapshotWithMetastorage() throws Exception { + IgniteEx ignite = startGridsWithCache(2, dfltCacheCfg, CACHE_KEYS_RANGE); + startClientGrid(); + + ignite.context().distributedMetastorage().write("key", "value"); + + ignite.snapshot().createSnapshot(SNAPSHOT_NAME).get(); + + stopAllGrids(); + + IgniteEx snp = startGridsFromSnapshot(2, SNAPSHOT_NAME); + + assertEquals("value", snp.context().distributedMetastorage().read("key")); + } + + /** @throws Exception If fails. */ + @Test + public void testMetastorageUpdateDuringSnapshot() throws Exception { + AtomicInteger keyCnt = new AtomicInteger(); + AtomicBoolean stop = new AtomicBoolean(); + CountDownLatch latch = new CountDownLatch(1); + + IgniteEx ignite = startGridsWithCache(2, dfltCacheCfg, CACHE_KEYS_RANGE); + + IgniteInternalFuture<?> updFut = GridTestUtils.runMultiThreadedAsync(() -> { + while (!Thread.currentThread().isInterrupted() && !stop.get()) { + try { + ignite.context().distributedMetastorage().write(SNAPSHOT_PREFIX + keyCnt.getAndIncrement(), "value"); + } + catch (IgniteCheckedException e) { + throw new IgniteException(e); + } + } + }, 3, "dms-updater"); + + DmsDataWriterWorker worker = GridTestUtils.getFieldValue(ignite.context().distributedMetastorage(), + DistributedMetaStorageImpl.class, "worker"); + LinkedBlockingQueue<RunnableFuture<?>> queue = GridTestUtils.getFieldValue(worker, DmsDataWriterWorker.class, + "updateQueue"); + + RunnableFuture<?> testTask = new FutureTask<>(() -> { + U.await(latch); + + return null; + }); + + queue.offer(testTask); + + assertTrue(GridTestUtils.waitForCondition(() -> queue.size() > 10, getTestTimeout())); + + ignite.context().cache().context().exchange() + .registerExchangeAwareComponent(new PartitionsExchangeAware() { + /** {@inheritDoc} */ + @Override public void onInitAfterTopologyLock(GridDhtPartitionsExchangeFuture fut) { + latch.countDown(); + } + }); + + ignite.snapshot().createSnapshot(SNAPSHOT_NAME).get(); + + stop.set(true); + updFut.get(); + + stopAllGrids(); + + IgniteEx snp0 = startGridsFromSnapshot(Collections.singleton(0), + cfg -> resolveSnapshotWorkDirectory(cfg).getAbsolutePath(), SNAPSHOT_NAME, false); + + Set<String> allKeys = new HashSet<>(); + snp0.context().distributedMetastorage().iterate(SNAPSHOT_PREFIX, (key, val) -> allKeys.add(key)); + + stopGrid(0); + + IgniteEx snp1 = startGridsFromSnapshot(Collections.singleton(1), + cfg -> resolveSnapshotWorkDirectory(cfg).getAbsolutePath(), SNAPSHOT_NAME, false); + + // Iterator reads keys from the node heap map. + snp1.context().distributedMetastorage() + .iterate(SNAPSHOT_PREFIX, new BiConsumer<String, Serializable>() { + @Override public void accept(String key, Serializable value) { + try { + assertTrue("Keys must be the same on all nodes [key=" + key + ", all=" + allKeys + ']', + allKeys.contains(key)); + } + catch (Throwable t) { + fail("Exception reading metastorage: " + t.getMessage()); + } + } + }); Review comment: TreeSet (in suggested code) required only for the output eg `` java.lang.AssertionError: Keys must be the same on all nodes Expected :[SNAPSHOT_PREFIX_0, SNAPSHOT_PREFIX_1, SNAPSHOT_PREFIX_10, SNAPSHOT_PREFIX_11, SNAPSHOT_PREFIX_12, SNAPSHOT_PREFIX_2, SNAPSHOT_PREFIX_3, SNAPSHOT_PREFIX_4, SNAPSHOT_PREFIX_5, SNAPSHOT_PREFIX_6, SNAPSHOT_PREFIX_7, SNAPSHOT_PREFIX_8, SNAPSHOT_PREFIX_9] Actual :[SNAPSHOT_PREFIX_0, SNAPSHOT_PREFIX_1, SNAPSHOT_PREFIX_10, SNAPSHOT_PREFIX_100, SNAPSHOT_PREFIX_101, SNAPSHOT_PREFIX_102, SNAPSHOT_PREFIX_103, SNAPSHOT_PREFIX_104, SNAPSHOT_PREFIX_105, SNAPSHOT_PREFIX_106, SNAPSHOT_PREFIX_107, SNAPSHOT_PREFIX_108, SNAPSHOT_PREFIX_109, SNAPSHOT_PREFIX_11, SNAPSHOT_PREFIX_110, SNAPSHOT_PREFIX_111, SNAPSHOT_PREFIX_112, SNAPSHOT_PREFIX_113, SNAPSHOT_PREFIX_114, SNAPSHOT_PREFIX_115, SNAPSHOT_PREFIX_116, SNAPSHOT_PREFIX_117, SNAPSHOT_PREFIX_118, SNAPSHOT_PREFIX_119, SNAPSHOT_PREFIX_12, SNAPSHOT_PREFIX_120, SNAPSHOT_PREFIX_121, SNAPSHOT_PREFIX_122, SNAPSHOT_PREFIX_123, SNAPSHOT_PREFIX_124, SNAPSHOT_PREFIX_125, SNAPSHOT_PREFIX_126, SNAPSHOT_PREFIX_127, SNAPSHOT_PREFIX_128, SNAPSHOT_PREFIX_129, SNAPSHOT_PREFIX_13, SNAPSHOT_PREFIX_130, SNAPSHOT_PREFIX_131, SNAPSHOT_PREFIX_132, SNAPSHOT_PREFIX_133, SNAPSHOT_PREFIX_134, SNAPSHOT_PREFIX_135, SNAPSHOT_PREFIX_136, SNAPSHOT_PREFIX_137, SNAPSHOT_PREFIX_138, SNAPSHOT_PREFIX_139, SNAPSHOT_PREFIX_14, SNAPSH OT_PREFIX_140, SNAPSHOT_PREFIX_141, SNAPSHOT_PREFIX_142, SNAPSHOT_PREFIX_143, SNAPSHOT_PREFIX_144, SNAPSHOT_PREFIX_145, SNAPSHOT_PREFIX_146, SNAPSHOT_PREFIX_147, SNAPSHOT_PREFIX_148, SNAPSHOT_PREFIX_149, SNAPSHOT_PREFIX_15, SNAPSHOT_PREFIX_150, SNAPSHOT_PREFIX_151, SNAPSHOT_PREFIX_152, SNAPSHOT_PREFIX_153, SNAPSHOT_PREFIX_154, SNAPSHOT_PREFIX_155, SNAPSHOT_PREFIX_156, SNAPSHOT_PREFIX_157, SNAPSHOT_PREFIX_158, SNAPSHOT_PREFIX_159, SNAPSHOT_PREFIX_16, SNAPSHOT_PREFIX_160, SNAPSHOT_PREFIX_161, SNAPSHOT_PREFIX_162, SNAPSHOT_PREFIX_163, SNAPSHOT_PREFIX_164, SNAPSHOT_PREFIX_165, SNAPSHOT_PREFIX_166, SNAPSHOT_PREFIX_167, SNAPSHOT_PREFIX_168, SNAPSHOT_PREFIX_169, SNAPSHOT_PREFIX_17, SNAPSHOT_PREFIX_170, SNAPSHOT_PREFIX_18, SNAPSHOT_PREFIX_19, SNAPSHOT_PREFIX_2, SNAPSHOT_PREFIX_20, SNAPSHOT_PREFIX_21, SNAPSHOT_PREFIX_22, SNAPSHOT_PREFIX_23, SNAPSHOT_PREFIX_24, SNAPSHOT_PREFIX_25, SNAPSHOT_PREFIX_26, SNAPSHOT_PREFIX_27, SNAPSHOT_PREFIX_28, SNAPSHOT_PREFIX_29, SNAPSHOT_PREFIX_3, SNAPSHOT_PREFI X_30, SNAPSHOT_PREFIX_31, SNAPSHOT_PREFIX_32, SNAPSHOT_PREFIX_33, SNAPSHOT_PREFIX_34, SNAPSHOT_PREFIX_35, SNAPSHOT_PREFIX_36, SNAPSHOT_PREFIX_37, SNAPSHOT_PREFIX_38, SNAPSHOT_PREFIX_39, SNAPSHOT_PREFIX_4, SNAPSHOT_PREFIX_40, SNAPSHOT_PREFIX_41, SNAPSHOT_PREFIX_42, SNAPSHOT_PREFIX_43, SNAPSHOT_PREFIX_44, SNAPSHOT_PREFIX_45, SNAPSHOT_PREFIX_46, SNAPSHOT_PREFIX_47, SNAPSHOT_PREFIX_48, SNAPSHOT_PREFIX_49, SNAPSHOT_PREFIX_5, SNAPSHOT_PREFIX_50, SNAPSHOT_PREFIX_51, SNAPSHOT_PREFIX_52, SNAPSHOT_PREFIX_53, SNAPSHOT_PREFIX_54, SNAPSHOT_PREFIX_55, SNAPSHOT_PREFIX_56, SNAPSHOT_PREFIX_57, SNAPSHOT_PREFIX_58, SNAPSHOT_PREFIX_59, SNAPSHOT_PREFIX_6, SNAPSHOT_PREFIX_60, SNAPSHOT_PREFIX_61, SNAPSHOT_PREFIX_62, SNAPSHOT_PREFIX_63, SNAPSHOT_PREFIX_64, SNAPSHOT_PREFIX_65, SNAPSHOT_PREFIX_66, SNAPSHOT_PREFIX_67, SNAPSHOT_PREFIX_68, SNAPSHOT_PREFIX_69, SNAPSHOT_PREFIX_7, SNAPSHOT_PREFIX_70, SNAPSHOT_PREFIX_71, SNAPSHOT_PREFIX_72, SNAPSHOT_PREFIX_73, SNAPSHOT_PREFIX_74, SNAPSHOT_PREFIX_75, SNAPSHOT_PREFIX _76, SNAPSHOT_PREFIX_77, SNAPSHOT_PREFIX_78, SNAPSHOT_PREFIX_79, SNAPSHOT_PREFIX_8, SNAPSHOT_PREFIX_80, SNAPSHOT_PREFIX_81, SNAPSHOT_PREFIX_82, SNAPSHOT_PREFIX_83, SNAPSHOT_PREFIX_84, SNAPSHOT_PREFIX_85, SNAPSHOT_PREFIX_86, SNAPSHOT_PREFIX_87, SNAPSHOT_PREFIX_88, SNAPSHOT_PREFIX_89, SNAPSHOT_PREFIX_9, SNAPSHOT_PREFIX_90, SNAPSHOT_PREFIX_91, SNAPSHOT_PREFIX_92, SNAPSHOT_PREFIX_93, SNAPSHOT_PREFIX_94, SNAPSHOT_PREFIX_95, SNAPSHOT_PREFIX_96, SNAPSHOT_PREFIX_97, SNAPSHOT_PREFIX_98, SNAPSHOT_PREFIX_99] <Click to see difference> ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org