ololo3000 commented on a change in pull request #86: URL: https://github.com/apache/ignite-extensions/pull/86#discussion_r786584062
########## File path: modules/topology-validator-ext/src/test/java/com/sbt/ignite/cache/IgniteCacheTopologyValidatorTest.java ########## @@ -0,0 +1,538 @@ +/* + * 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 com.sbt.ignite.cache; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.stream.Collectors; +import com.sbt.ignite.plugin.cache.CacheTopologyValidatorPluginProvider; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.cluster.ClusterNode; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.internal.IgniteEx; +import org.apache.ignite.internal.processors.cache.CacheInvalidStateException; +import org.apache.ignite.internal.processors.cache.distributed.dht.IgniteCacheTopologySplitAbstractTest; +import org.apache.ignite.internal.util.typedef.G; +import org.apache.ignite.internal.util.typedef.internal.U; +import org.apache.ignite.spi.IgniteSpiException; +import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi; +import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder; +import org.apache.ignite.testframework.GridTestUtils.RunnableX; +import org.junit.Test; + +import static com.sbt.ignite.plugin.cache.CacheTopologyValidatorPluginProvider.TOP_VALIDATOR_ENABLED_PROP_NAME; +import static java.util.Collections.singletonMap; +import static java.util.stream.Collectors.toList; +import static org.apache.ignite.cache.CacheMode.REPLICATED; +import static org.apache.ignite.cluster.ClusterState.ACTIVE; +import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY; +import static org.apache.ignite.events.EventType.EVT_NODE_JOINED; +import static org.apache.ignite.internal.processors.cache.distributed.GridCacheModuloAffinityFunction.IDX_ATTR; +import static org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi.DFLT_PORT; +import static org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi.DFLT_PORT_RANGE; +import static org.apache.ignite.testframework.GridTestUtils.assertThrowsAnyCause; +import static org.apache.ignite.testframework.GridTestUtils.waitForCondition; + +/** */ +public class IgniteCacheTopologyValidatorTest extends IgniteCacheTopologySplitAbstractTest { + /** */ + private static final String LOCAL_HOST = "localhost"; + + /** */ + private static final int CACHE_KEY_CNT = 1000; + + /** */ + public static final int CACHE_CNT = 2; + + /** */ + @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { + return getConfiguration(igniteInstanceName, true); + } + + /** */ + private IgniteConfiguration getConfiguration( + String igniteInstanceName, + boolean configureSegmentationResolverPlugin + ) throws Exception { + int idx = getTestIgniteInstanceIndex(igniteInstanceName); + + IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName) + .setUserAttributes(singletonMap(IDX_ATTR, idx)); + + if (configureSegmentationResolverPlugin) + cfg.setPluginProviders(new CacheTopologyValidatorPluginProvider()); + + ((TcpDiscoverySpi)cfg.getDiscoverySpi()) + .setIpFinder(sharedStaticIpFinder) + .setLocalPortRange(1) + .setLocalPort(discoPort(idx)) + .setConnectionRecoveryTimeout(0); + + return cfg; + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + super.afterTest(); + + stopAllGrids(); + } + + /** {@inheritDoc} */ + @Override protected boolean isBlocked(int locPort, int rmtPort) { + return isDiscoPort(locPort) && isDiscoPort(rmtPort) && segment(locPort) != segment(rmtPort); + } + + /** */ + private int segment(int discoPort) { + return (discoPort - DFLT_PORT) % 2 == 0 ? 0 : 1; + } + + /** */ + @Override public int segment(ClusterNode node) { + return node.<Integer>attribute(IDX_ATTR) % 2 == 0 ? 0 : 1; + } + + /** */ + @Test + public void testConnectionToIncompatibleCluster() throws Exception { + startGrid(getConfiguration(getTestIgniteInstanceName(0), false)); + + startGrid(1); + + assertTrue(waitForCondition( + () -> !(Boolean)grid(1).context().distributedConfiguration().property(TOP_VALIDATOR_ENABLED_PROP_NAME).get(), + getTestTimeout() + )); + + splitAndWait(); + + connectNodeToSegment(3, false, 1); + + assertTrue(waitForCondition( + () -> !(Boolean)grid(3).context().distributedConfiguration().property(TOP_VALIDATOR_ENABLED_PROP_NAME).get(), + getTestTimeout() + )); + } + + /** */ + @Test + public void testIncompatibleNodeConnection() throws Exception { + IgniteEx srv = startGrid(0); + + createCaches(); + + assertThrowsAnyCause( + log, + () -> startGrid(getConfiguration(getTestIgniteInstanceName(1), false)), + IgniteSpiException.class, + "The Topology Validator plugin is not configured for the server node that is trying to join the cluster." + ); + + startClientGrid(getConfiguration(getTestIgniteInstanceName(2), false)); + + assertEquals(2, srv.cluster().nodes().size()); + + checkPutGet(G.allGrids(), true); + } + + /** */ + @Test + public void testConnectionToSegmentedCluster() throws Exception { + startGridsMultiThreaded(6); + + grid(0).cluster().baselineAutoAdjustEnabled(false); + + createCaches(); + + stopGrid(4); + stopGrid(5); + + splitAndWait(); + + checkPutGet(G.allGrids(), false); + + connectNodeToSegment(4, false, 0); + connectNodeToSegment(6, true, 0); + checkPutGet(0, false); + + connectNodeToSegment(5, false, 1); + connectNodeToSegment(7, true, 1); + checkPutGet(1, false); + + stopSegmentNodes(1); + + unsplit(); + + startGrid(1); + + checkPutGet(G.allGrids(), false); + } + + /** */ + @Test + public void testRegularNodeStartStop() throws Exception { + startGrid(0); + + createCaches(); + + checkPutGetAfter(() -> startGrid(1)); + checkPutGetAfter(() -> stopGrid(1)); + + checkPutGetAfter(() -> startClientGrid(2)); + checkPutGetAfter(() -> stopGrid(2)); + + startGrid(1); + + grid(0).cluster().baselineAutoAdjustEnabled(false); + + checkPutGetAfter(() -> startGrid(3)); + checkPutGetAfter(() -> stopGrid(3)); + + checkPutGetAfter(() -> stopGrid(1)); + + checkPutGetAfter(() -> startClientGrid(2)); + checkPutGetAfter(() -> stopGrid(2)); + } + + /** */ + @Test + public void testClientNodeSegmentationIgnored() throws Exception { + IgniteEx srv = startGrid(0); + + startClientGrid(1); + + srv.cluster().baselineAutoAdjustEnabled(false); + + createCaches(); + + failNode(1, Collections.singleton(srv)); + + checkPutGet(Collections.singleton(srv), true); + } + + /** */ + @Test + public void testSplitWithoutBaseline() throws Exception { + startGridsMultiThreaded(4); + + createCaches(); + + splitAndWait(); + + checkPutGet(G.allGrids(), true); + } + + /** */ + @Test + public void testSplitWithBaseline() throws Exception { + startGridsMultiThreaded(3); + + grid(0).cluster().baselineAutoAdjustEnabled(false); + + createCaches(); + + startGrid(3); + + splitAndWait(); + + connectNodeToSegment(4, true, 0); + connectNodeToSegment(5, true, 1); + + checkPutGet(0, true); + checkPutGet(1, false); + + assertTrue(waitForCondition(() -> ACTIVE_READ_ONLY == grid(1).cluster().state(), getTestTimeout())); + + stopSegmentNodes(1); + stopGrid(4); + + unsplit(); + + startGrid(1); + startGrid(3); + + grid(0).cluster().setBaselineTopology(grid(0).cluster().topologyVersion()); + + splitAndWait(); + + checkPutGet(G.allGrids(), false); + + assertTrue(waitForCondition(() -> ACTIVE_READ_ONLY == grid(1).cluster().state(), getTestTimeout())); + assertTrue(waitForCondition(() -> ACTIVE_READ_ONLY == grid(0).cluster().state(), getTestTimeout())); + + grid(0).cluster().state(ACTIVE); + + checkPutGet(0, true); + checkPutGet(1, false); + } + + /** */ + @Test + public void testConsequentSegmentationResolving() throws Exception { + startGridsMultiThreaded(4); + + grid(0).cluster().baselineAutoAdjustEnabled(false); + + createCaches(); + + splitAndWait(); + + checkPutGet(G.allGrids(), false); + + grid(1).cluster().state(ACTIVE); + + checkPutGet(0, false); + checkPutGet(1, true); + + stopSegmentNodes(0); + + unsplit(); + + failNode(1, Collections.singleton(grid(3))); + + checkPutGet(Collections.singleton(grid(3)), false); + + grid(3).cluster().state(ACTIVE); + + checkPutGet(Collections.singleton(grid(3)), true); + } + + /** */ + @Test + public void testEnableProperty() throws Exception { + startGridsMultiThreaded(4); + + grid(0).cluster().baselineAutoAdjustEnabled(false); + + createCaches(); + + grid(1).context().distributedConfiguration().property(TOP_VALIDATOR_ENABLED_PROP_NAME).propagate(false); + + splitAndWait(); + + connectNodeToSegment(4, true, 0); + connectNodeToSegment(5, true, 1); + + checkPutGet(G.allGrids(), true); + + stopSegmentNodes(0); + + unsplit(); + + grid(1).context().distributedConfiguration().property(TOP_VALIDATOR_ENABLED_PROP_NAME).propagate(true); + + failNode(1, Collections.singleton(grid(3))); + + checkPutGet(Collections.singleton(grid(3)), false); + } + + /** */ + @Test + public void testNodeJoinWithHalfBaselineNodesLeft() throws Exception { + startGridsMultiThreaded(4); + + grid(0).cluster().baselineAutoAdjustEnabled(false); + + createCaches(); + + stopGrid(0); + stopGrid(1); + stopGrid(2); + + checkPutGet(G.allGrids(), true); + + startGrid(0); + + checkPutGet(G.allGrids(), true); + } + + /** */ + @Test + public void testNodeJoinConcurrentWithLeftRejected() throws Exception { + IgniteEx srv = startGrids(2); + + grid(0).cluster().baselineAutoAdjustEnabled(false); + + createCaches(); + + CountDownLatch discoveryWorkerBlockedLatch = new CountDownLatch(1); + + try { + srv.events().localListen(evt -> { + try { + discoveryWorkerBlockedLatch.await(); + } + catch (InterruptedException e) { + U.error(log, e); Review comment: If this exception occurs, assertion below will fail the test. I don't think it's worth it to fail the test immediately after this exception, given that the listener code is running from an Ignite worker thread. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
