xtern commented on a change in pull request #8648: URL: https://github.com/apache/ignite/pull/8648#discussion_r614262376
########## 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.util.Arrays; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Objects; +import java.util.UUID; +import java.util.function.Function; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.IgniteCheckedException; +import org.apache.ignite.IgniteException; +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.QueryEntity; +import org.apache.ignite.cache.QueryIndex; +import org.apache.ignite.cache.query.annotations.QuerySqlField; +import org.apache.ignite.cluster.ClusterState; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.IgniteConfiguration; +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.persistence.file.FilePageStoreManager; +import org.apache.ignite.internal.util.distributed.DistributedProcess.DistributedProcessType; +import org.apache.ignite.internal.util.distributed.SingleNodeMessage; +import org.apache.ignite.internal.util.typedef.F; +import org.apache.ignite.internal.util.typedef.internal.CU; +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.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 AbstractSnapshotSelfTest { + /** Timeout. */ + private static final long TIMEOUT = 15_000; + + /** Binary type name. */ + private static final String BIN_TYPE_NAME = "customType"; + + /** Static cache configurations. */ + protected CacheConfiguration<?, ?>[] cacheCfgs; + + /** Cache value builder. */ + protected Function<Integer, Object> valBuilder = new IndexedValueBuilder(); + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String name) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(name); + + if (cacheCfgs != null) + cfg.setCacheConfiguration(cacheCfgs); + else if (dfltCacheCfg != null) { + dfltCacheCfg.setSqlIndexMaxInlineSize(255); + dfltCacheCfg.setQueryEntities( + Arrays.asList(queryEntity(BIN_TYPE_NAME), queryEntity(IndexedObject.class.getName()))); + } + + return cfg; + } + + /** + * @param typeName Type name. + */ + private QueryEntity queryEntity(String typeName) { + return new QueryEntity() + .setKeyType(Integer.class.getName()) + .setValueType(typeName) + .setFields(new LinkedHashMap<>(F.asMap("id", Integer.class.getName(), "name", String.class.getName()))) + .setIndexes(Arrays.asList(new QueryIndex("id"), new QueryIndex("name"))); + } + + /** + * Ensures that the cache doesn't start if one of the baseline nodes fails. + * + * @throws Exception If failed. + */ + @Test + public void testCacheStartFailOnNodeLeft() throws Exception { + int keysCnt = 10_000; + + startGridsWithSnapshot(3, keysCnt, true); + + BlockingCustomMessageDiscoverySpi discoSpi = discoSpi(grid(0)); + + discoSpi.block((msg) -> msg instanceof DynamicCacheChangeBatch); + + IgniteFuture<Void> fut = + grid(0).snapshot().restoreSnapshot(SNAPSHOT_NAME, Collections.singleton(dfltCacheCfg.getName())); + + discoSpi.waitBlocked(TIMEOUT); + + stopGrid(2, true); + + discoSpi.unblock(); + + GridTestUtils.assertThrowsAnyCause(log, () -> fut.get(TIMEOUT), ClusterTopologyCheckedException.class, null); + + ensureCacheDirEmpty(2, dfltCacheCfg); + } + + /** @throws Exception If failed. */ + @Test + public void testBasicClusterSnapshotRestore() throws Exception { + int keysCnt = 10_000; + + IgniteEx ignite = startGridsWithSnapshot(2, keysCnt, true); + + grid(0).snapshot().restoreSnapshot(SNAPSHOT_NAME, Collections.singleton(dfltCacheCfg.getName())).get(TIMEOUT); + + IgniteCache<Object, Object> cache = ignite.cache(dfltCacheCfg.getName()); + + assertTrue(cache.indexReadyFuture().isDone()); + + checkCacheKeys(cache, keysCnt); + } + + /** @throws Exception If failed. */ + @Test + public void testBasicClusterSnapshotRestoreWithMetadata() throws Exception { + int keysCnt = 10_000; + + valBuilder = new BinaryValueBuilder(0, BIN_TYPE_NAME); + + IgniteEx ignite = startGridsWithSnapshot(2, keysCnt); + + // Remove metadata. + int typeId = ignite.context().cacheObjects().typeId(BIN_TYPE_NAME); + + ignite.context().cacheObjects().removeType(typeId); + + forceCheckpoint(); + + ignite.snapshot().restoreSnapshot(SNAPSHOT_NAME, Collections.singleton(dfltCacheCfg.getName())).get(TIMEOUT); + + IgniteCache<Object, Object> cache = ignite.cache(dfltCacheCfg.getName()).withKeepBinary(); + + assertTrue(cache.indexReadyFuture().isDone()); + + checkCacheKeys(cache, keysCnt); + } + + /** @throws Exception If failed. */ + @Test + public void testClusterSnapshotRestoreRejectOnInActiveCluster() throws Exception { + IgniteEx ignite = startGridsWithCache(2, CACHE_KEYS_RANGE, valBuilder, dfltCacheCfg); + + ignite.snapshot().createSnapshot(SNAPSHOT_NAME).get(TIMEOUT); + + ignite.cluster().state(ClusterState.INACTIVE); + + IgniteFuture<Void> fut = + ignite.snapshot().restoreSnapshot(SNAPSHOT_NAME, Collections.singleton(dfltCacheCfg.getName())); + + GridTestUtils.assertThrowsAnyCause( + log, () -> fut.get(TIMEOUT), IgniteException.class, "The cluster should be active"); + } + + /** @throws Exception If failed. */ + @Test + public void testClusterSnapshotRestoreDiffTopology() throws Exception { Review comment: Added test `testClusterSnapshotRestoreOnSmallerTopology` -- 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]
