xtern commented on a change in pull request #8648: URL: https://github.com/apache/ignite/pull/8648#discussion_r622751052
########## File path: modules/core/src/test/java/org/apache/ignite/internal/processors/cache/persistence/snapshot/IgniteClusterSnapshotRestoreSelfTest.java ########## @@ -0,0 +1,774 @@ +/* + * 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.File; +import java.io.IOException; +import java.nio.file.OpenOption; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Collections; +import java.util.UUID; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.IntSupplier; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.IgniteCheckedException; +import org.apache.ignite.IgniteException; +import org.apache.ignite.IgniteIllegalStateException; +import org.apache.ignite.IgniteSnapshot; +import org.apache.ignite.binary.BinaryObject; +import org.apache.ignite.binary.BinaryObjectBuilder; +import org.apache.ignite.binary.BinaryObjectException; +import org.apache.ignite.binary.BinaryType; +import org.apache.ignite.cache.CacheExistsException; +import org.apache.ignite.cache.CacheMode; +import org.apache.ignite.cluster.ClusterState; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.internal.GridKernalContext; +import org.apache.ignite.internal.IgniteEx; +import org.apache.ignite.internal.IgniteInternalFuture; +import org.apache.ignite.internal.TestRecordingCommunicationSpi; +import org.apache.ignite.internal.cluster.ClusterTopologyCheckedException; +import org.apache.ignite.internal.processors.cache.CacheGroupDescriptor; +import org.apache.ignite.internal.processors.cache.DynamicCacheChangeBatch; +import org.apache.ignite.internal.processors.cache.distributed.dht.preloader.GridDhtPartitionsSingleMessage; +import org.apache.ignite.internal.processors.cache.persistence.file.FileIO; +import org.apache.ignite.internal.processors.cache.persistence.file.FileIOFactory; +import org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager; +import org.apache.ignite.internal.processors.cache.persistence.file.RandomAccessFileIOFactory; +import org.apache.ignite.internal.util.distributed.DistributedProcess.DistributedProcessType; +import org.apache.ignite.internal.util.distributed.SingleNodeMessage; +import org.apache.ignite.internal.util.typedef.G; +import org.apache.ignite.internal.util.typedef.internal.CU; +import org.apache.ignite.internal.util.typedef.internal.U; +import org.apache.ignite.lang.IgniteFuture; +import org.apache.ignite.spi.IgniteSpiException; +import org.apache.ignite.testframework.GridTestUtils; +import org.jetbrains.annotations.Nullable; +import org.junit.Test; + +import static org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.CACHE_DIR_PREFIX; +import static org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.FILE_SUFFIX; +import static org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.PART_FILE_PREFIX; +import static org.apache.ignite.internal.processors.cache.persistence.snapshot.SnapshotRestoreProcess.TMP_CACHE_DIR_PREFIX; +import static org.apache.ignite.internal.util.distributed.DistributedProcess.DistributedProcessType.RESTORE_CACHE_GROUP_SNAPSHOT_PREPARE; +import static org.apache.ignite.internal.util.distributed.DistributedProcess.DistributedProcessType.RESTORE_CACHE_GROUP_SNAPSHOT_START; +import static org.apache.ignite.testframework.GridTestUtils.runAsync; + +/** + * Snapshot restore tests. + */ +public class IgniteClusterSnapshotRestoreSelfTest extends IgniteClusterSnapshotRestoreBaseTest { + /** Type name used for binary and SQL. */ + private static final String TYPE_NAME = "CustomType"; + + /** Cache 1 name. */ + private static final String CACHE1 = "cache1"; + + /** Cache 2 name. */ + private static final String CACHE2 = "cache2"; + + /** Default shared cache group name. */ + private static final String SHARED_GRP = "shared"; + + /** Cache value builder. */ + private Function<Integer, Object> valBuilder = String::valueOf; + + /** {@inheritDoc} */ + @Override protected Function<Integer, Object> valueBuilder() { + return valBuilder; + } + + /** @throws Exception If failed. */ + @Test + public void testRestoreAllGroups() throws Exception { + CacheConfiguration<Integer, Object> cacheCfg1 = + txCacheConfig(new CacheConfiguration<Integer, Object>(CACHE1)).setGroupName(SHARED_GRP); + + CacheConfiguration<Integer, Object> cacheCfg2 = + txCacheConfig(new CacheConfiguration<Integer, Object>(CACHE2)).setGroupName(SHARED_GRP); + + IgniteEx ignite = startGridsWithCache(2, CACHE_KEYS_RANGE, valBuilder, + dfltCacheCfg.setBackups(0), cacheCfg1, cacheCfg2); + + ignite.snapshot().createSnapshot(SNAPSHOT_NAME).get(TIMEOUT); + + ignite.cache(CACHE1).destroy(); + ignite.cache(CACHE2).destroy(); + ignite.cache(DEFAULT_CACHE_NAME).destroy(); + + awaitPartitionMapExchange(); + + // Restore all cache groups. + grid(0).snapshot().restoreSnapshot(SNAPSHOT_NAME, null).get(TIMEOUT); + + checkCacheKeys(ignite.cache(DEFAULT_CACHE_NAME), CACHE_KEYS_RANGE); + checkCacheKeys(ignite.cache(CACHE1), CACHE_KEYS_RANGE); + checkCacheKeys(ignite.cache(CACHE2), CACHE_KEYS_RANGE); + } + + /** @throws Exception If failed. */ + @Test + public void testStartClusterSnapshotRestoreMultipleThreadsSameNode() throws Exception { + checkStartClusterSnapshotRestoreMultithreaded(() -> 0); + } + + /** @throws Exception If failed. */ + @Test + public void testStartClusterSnapshotRestoreMultipleThreadsDiffNode() throws Exception { + AtomicInteger nodeIdx = new AtomicInteger(); + + checkStartClusterSnapshotRestoreMultithreaded(nodeIdx::getAndIncrement); + } + + /** + * @param nodeIdxSupplier Ignite node index supplier. + */ + public void checkStartClusterSnapshotRestoreMultithreaded(IntSupplier nodeIdxSupplier) throws Exception { + Ignite ignite = startGridsWithSnapshot(2, CACHE_KEYS_RANGE); + + CountDownLatch startLatch = new CountDownLatch(1); + AtomicInteger successCnt = new AtomicInteger(); + + IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(() -> { + try { + startLatch.await(TIMEOUT, TimeUnit.MILLISECONDS); + + grid(nodeIdxSupplier.getAsInt()).snapshot().restoreSnapshot( + SNAPSHOT_NAME, Collections.singleton(DEFAULT_CACHE_NAME)).get(TIMEOUT); + + successCnt.incrementAndGet(); + } + catch (Exception ignore) { + // Expected exception. Review comment: There possible 2 exceptions, first about another process started, second (rare) - cache exists (if the second process will be delayed). Do you suggest checking for exceptions manually? -- 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: [email protected]
