timoninmaxim commented on a change in pull request #8206: URL: https://github.com/apache/ignite/pull/8206#discussion_r494250150
########## File path: modules/core/src/test/java/org/apache/ignite/internal/client/thin/ThinClientPartitionAwarenessDiscoveryTest.java ########## @@ -0,0 +1,141 @@ +/* + * 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.client.thin; + +import java.util.Arrays; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import org.apache.ignite.client.ClientAddressFinder; +import org.apache.ignite.client.ClientCache; +import org.apache.ignite.configuration.ClientConfiguration; +import org.apache.ignite.internal.IgnitionEx; +import org.apache.ignite.internal.processors.cache.IgniteInternalCache; +import org.junit.Test; + +import static org.apache.ignite.internal.processors.odbc.ClientListenerProcessor.CLIENT_LISTENER_PORT; + +/** + * Test partition awareness of thin client on changed topology. + */ +public class ThinClientPartitionAwarenessDiscoveryTest extends ThinClientAbstractPartitionAwarenessTest { + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + super.afterTest(); + + stopAllGrids(); + } + + /** + * Test that client use channels to all running nodes while new nodes start + */ + @Test + public void testClientDiscoveryNodesJoin() throws Exception { + for (int i = 0; i < MAX_CLUSTER_SIZE; ++i) { + startGrid(i); + awaitPartitionMapExchange(); + + int[] workChannels = IntStream.rangeClosed(0, i).toArray(); + + if (i == 0) + initClient(getClientConfigurationWithDiscovery(), workChannels); + else { + detectTopologyChange(); + awaitChannelsInit(workChannels); + } + + testPartitionAwareness(workChannels); + } + } + + /** + * Test that client use channels to all running nodes while nodes stop + */ + @Test + public void testClientDiscoveryNodesLeave() throws Exception { + startGrids(MAX_CLUSTER_SIZE); + awaitPartitionMapExchange(); + + initClient(getClientConfigurationWithDiscovery(), 0, 1, 2, 3); + detectTopologyChange(); + + for (int i = MAX_CLUSTER_SIZE - 1; i != 0; i--) { + int[] workChannels = IntStream.range(0, i).toArray(); + + channels[i] = null; + stopGrid(i); + awaitPartitionMapExchange(); + detectTopologyChange(); + + awaitChannelsInit(workChannels); Review comment: fixied ########## File path: modules/core/src/test/java/org/apache/ignite/internal/client/thin/ThinClientPartitionAwarenessDiscoveryTest.java ########## @@ -0,0 +1,141 @@ +/* + * 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.client.thin; + +import java.util.Arrays; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import org.apache.ignite.client.ClientAddressFinder; +import org.apache.ignite.client.ClientCache; +import org.apache.ignite.configuration.ClientConfiguration; +import org.apache.ignite.internal.IgnitionEx; +import org.apache.ignite.internal.processors.cache.IgniteInternalCache; +import org.junit.Test; + +import static org.apache.ignite.internal.processors.odbc.ClientListenerProcessor.CLIENT_LISTENER_PORT; + +/** + * Test partition awareness of thin client on changed topology. + */ +public class ThinClientPartitionAwarenessDiscoveryTest extends ThinClientAbstractPartitionAwarenessTest { + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + super.afterTest(); + + stopAllGrids(); + } + + /** + * Test that client use channels to all running nodes while new nodes start + */ + @Test + public void testClientDiscoveryNodesJoin() throws Exception { + for (int i = 0; i < MAX_CLUSTER_SIZE; ++i) { + startGrid(i); + awaitPartitionMapExchange(); + + int[] workChannels = IntStream.rangeClosed(0, i).toArray(); + + if (i == 0) + initClient(getClientConfigurationWithDiscovery(), workChannels); + else { + detectTopologyChange(); + awaitChannelsInit(workChannels); + } + + testPartitionAwareness(workChannels); + } + } + + /** + * Test that client use channels to all running nodes while nodes stop + */ + @Test + public void testClientDiscoveryNodesLeave() throws Exception { Review comment: Yes, it's ok that test will pass with default configuration too. It's like regression test, that discovery work expectedly. The case you describe will work the same way, it will pass both for default and discovery configurations, as both of them will configure the same set of channels. But it is a good test case and I've added it too. ########## File path: modules/core/src/test/java/org/apache/ignite/internal/client/thin/ReliableChannelTest.java ########## @@ -0,0 +1,229 @@ +/* + * 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.client.thin; + +import java.util.Collections; +import java.util.List; +import java.util.UUID; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import org.apache.ignite.client.ClientAddressFinder; +import org.apache.ignite.client.ClientAuthorizationException; +import org.apache.ignite.client.ClientConnectionException; +import org.apache.ignite.client.ClientException; +import org.apache.ignite.configuration.ClientConfiguration; +import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +/** + * Class test ReliableChannel channels re-initialization. + */ +public class ReliableChannelTest { + /** Mock factory for creating new channels. */ + private final Function<ClientChannelConfiguration, ClientChannel> chFactory = cfg -> new TestClientChannel(); + + /** Checks that channel holders are not reinited for static address configuration. */ + @Test + public void testChannelsNotReinitForStaticAddressConfiguration() { + ClientConfiguration ccfg = new ClientConfiguration() + .setAddresses("127.0.0.1:8000", "127.0.0.1:8001", "127.0.0.1:8002"); + + checkDoesNotReinit(ccfg); + } + + /** Checks that channel holders are not reinited if address finder return the same list of addresses. */ + @Test + public void testChannelsNotReinitForStableDynamicAddressConfiguration() { + ClientConfiguration ccfg = new ClientConfiguration() + .setAddressesFinder(new TestAddressFinder("127.0.0.1:8000", "127.0.0.1:8001", "127.0.0.1:8002")); + + checkDoesNotReinit(ccfg); + } + + /** */ + private void checkDoesNotReinit(ClientConfiguration ccfg) { + ReliableChannel rc = new ReliableChannel(chFactory, ccfg, null); + rc.initConnection(); + List<ReliableChannel.ClientChannelHolder> originalChannels = rc.getChannelHolders(); + + // Imitate topology change. + rc.initChannelHolders(true); + List<ReliableChannel.ClientChannelHolder> newChannels = rc.getChannelHolders(); + + assertSame(originalChannels, newChannels); + IntStream.range(0, 3).forEach(i -> { + assertSame(originalChannels.get(i), newChannels.get(i)); Review comment: FIxed, add test for that. ########## File path: modules/core/src/test/java/org/apache/ignite/internal/client/thin/ThinClientPartitionAwarenessDiscoveryTest.java ########## @@ -0,0 +1,141 @@ +/* + * 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.client.thin; + +import java.util.Arrays; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import org.apache.ignite.client.ClientAddressFinder; +import org.apache.ignite.client.ClientCache; +import org.apache.ignite.configuration.ClientConfiguration; +import org.apache.ignite.internal.IgnitionEx; +import org.apache.ignite.internal.processors.cache.IgniteInternalCache; +import org.junit.Test; + +import static org.apache.ignite.internal.processors.odbc.ClientListenerProcessor.CLIENT_LISTENER_PORT; + +/** + * Test partition awareness of thin client on changed topology. + */ +public class ThinClientPartitionAwarenessDiscoveryTest extends ThinClientAbstractPartitionAwarenessTest { + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + super.afterTest(); + + stopAllGrids(); + } + + /** + * Test that client use channels to all running nodes while new nodes start + */ + @Test + public void testClientDiscoveryNodesJoin() throws Exception { + for (int i = 0; i < MAX_CLUSTER_SIZE; ++i) { + startGrid(i); + awaitPartitionMapExchange(); + + int[] workChannels = IntStream.rangeClosed(0, i).toArray(); + + if (i == 0) + initClient(getClientConfigurationWithDiscovery(), workChannels); + else { + detectTopologyChange(); + awaitChannelsInit(workChannels); + } + + testPartitionAwareness(workChannels); + } + } + + /** + * Test that client use channels to all running nodes while nodes stop + */ + @Test + public void testClientDiscoveryNodesLeave() throws Exception { + startGrids(MAX_CLUSTER_SIZE); + awaitPartitionMapExchange(); + + initClient(getClientConfigurationWithDiscovery(), 0, 1, 2, 3); + detectTopologyChange(); + + for (int i = MAX_CLUSTER_SIZE - 1; i != 0; i--) { + int[] workChannels = IntStream.range(0, i).toArray(); + + channels[i] = null; + stopGrid(i); + awaitPartitionMapExchange(); + detectTopologyChange(); + + awaitChannelsInit(workChannels); + testPartitionAwareness(workChannels); + } + } + + /** + * Checks that each request goes to right node. + */ + private void testPartitionAwareness(int... chIdxs) { + ClientCache<Object, Object> clientCache = client.cache(PART_CACHE_NAME); + IgniteInternalCache<Object, Object> igniteCache = grid(0).context().cache().cache(PART_CACHE_NAME); + + Map<TestTcpClientChannel, Boolean> channelHits = Arrays.stream(chIdxs).boxed() + .collect(Collectors.toMap(idx -> channels[idx], idx -> false)); + + for (int i = 0; i < KEY_CNT; i++) { + TestTcpClientChannel opCh = affinityChannel(i, igniteCache); + + clientCache.put(i, i); + + if (i == 0) + assertOpOnChannel(dfltCh, ClientOperation.CACHE_PARTITIONS); + + assertOpOnChannel(opCh, ClientOperation.CACHE_PUT); + assertTrue(channelHits.containsKey(opCh)); + + channelHits.compute(opCh, (c, old) -> true); + } + + assertFalse(channelHits.containsValue(false)); + } + + /** + * Provide ClientConfiguration with addrResolver that find all alive nodes + */ + private ClientConfiguration getClientConfigurationWithDiscovery() { + ClientAddressFinder addrFinder = () -> + IgnitionEx.allGrids().stream().map(node -> { + int port = (Integer) node.cluster().localNode().attributes().get(CLIENT_LISTENER_PORT); + return "127.0.0.1:" + port; + }).toArray(String[]::new); + + return new ClientConfiguration() + .setAddressesFinder(addrFinder) + .setPartitionAwarenessEnabled(true); + } + + /** + * Trigger client to detect topology change + */ + private void detectTopologyChange() { + // Send non-affinity request to detect topology change. + initDefaultChannel(); + client.getOrCreateCache(PART_CACHE_NAME); Review comment: Sorry, you're correct. Fixed. ########## File path: modules/core/src/test/java/org/apache/ignite/internal/client/thin/ThinClientPartitionAwarenessDiscoveryTest.java ########## @@ -0,0 +1,141 @@ +/* + * 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.client.thin; + +import java.util.Arrays; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import org.apache.ignite.client.ClientAddressFinder; +import org.apache.ignite.client.ClientCache; +import org.apache.ignite.configuration.ClientConfiguration; +import org.apache.ignite.internal.IgnitionEx; +import org.apache.ignite.internal.processors.cache.IgniteInternalCache; +import org.junit.Test; + +import static org.apache.ignite.internal.processors.odbc.ClientListenerProcessor.CLIENT_LISTENER_PORT; + +/** + * Test partition awareness of thin client on changed topology. + */ +public class ThinClientPartitionAwarenessDiscoveryTest extends ThinClientAbstractPartitionAwarenessTest { + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + super.afterTest(); + + stopAllGrids(); + } + + /** + * Test that client use channels to all running nodes while new nodes start + */ + @Test + public void testClientDiscoveryNodesJoin() throws Exception { + for (int i = 0; i < MAX_CLUSTER_SIZE; ++i) { + startGrid(i); + awaitPartitionMapExchange(); + + int[] workChannels = IntStream.rangeClosed(0, i).toArray(); + + if (i == 0) + initClient(getClientConfigurationWithDiscovery(), workChannels); + else { + detectTopologyChange(); + awaitChannelsInit(workChannels); + } + + testPartitionAwareness(workChannels); + } + } + + /** + * Test that client use channels to all running nodes while nodes stop + */ + @Test + public void testClientDiscoveryNodesLeave() throws Exception { + startGrids(MAX_CLUSTER_SIZE); + awaitPartitionMapExchange(); + + initClient(getClientConfigurationWithDiscovery(), 0, 1, 2, 3); + detectTopologyChange(); + + for (int i = MAX_CLUSTER_SIZE - 1; i != 0; i--) { + int[] workChannels = IntStream.range(0, i).toArray(); + + channels[i] = null; + stopGrid(i); + awaitPartitionMapExchange(); + detectTopologyChange(); + + awaitChannelsInit(workChannels); + testPartitionAwareness(workChannels); + } + } + + /** + * Checks that each request goes to right node. + */ + private void testPartitionAwareness(int... chIdxs) { + ClientCache<Object, Object> clientCache = client.cache(PART_CACHE_NAME); + IgniteInternalCache<Object, Object> igniteCache = grid(0).context().cache().cache(PART_CACHE_NAME); + + Map<TestTcpClientChannel, Boolean> channelHits = Arrays.stream(chIdxs).boxed() + .collect(Collectors.toMap(idx -> channels[idx], idx -> false)); + + for (int i = 0; i < KEY_CNT; i++) { + TestTcpClientChannel opCh = affinityChannel(i, igniteCache); + + clientCache.put(i, i); + + if (i == 0) + assertOpOnChannel(dfltCh, ClientOperation.CACHE_PARTITIONS); + + assertOpOnChannel(opCh, ClientOperation.CACHE_PUT); + assertTrue(channelHits.containsKey(opCh)); + + channelHits.compute(opCh, (c, old) -> true); + } + + assertFalse(channelHits.containsValue(false)); + } + + /** + * Provide ClientConfiguration with addrResolver that find all alive nodes + */ + private ClientConfiguration getClientConfigurationWithDiscovery() { + ClientAddressFinder addrFinder = () -> + IgnitionEx.allGrids().stream().map(node -> { + int port = (Integer) node.cluster().localNode().attributes().get(CLIENT_LISTENER_PORT); + return "127.0.0.1:" + port; + }).toArray(String[]::new); + + return new ClientConfiguration() + .setAddressesFinder(addrFinder) + .setPartitionAwarenessEnabled(true); + } + + /** + * Trigger client to detect topology change + */ + private void detectTopologyChange() { + // Send non-affinity request to detect topology change. + initDefaultChannel(); + client.getOrCreateCache(PART_CACHE_NAME); Review comment: FIxed, add test for that ########## File path: modules/core/src/main/java/org/apache/ignite/internal/client/thin/ReliableChannel.java ########## @@ -114,46 +135,32 @@ Function<ClientChannelConfiguration, ClientChannel> chFactory, ClientConfiguration clientCfg, IgniteBinary binary - ) throws ClientException { + ) { if (chFactory == null) throw new NullPointerException("chFactory"); if (clientCfg == null) throw new NullPointerException("clientCfg"); + this.clientCfg = clientCfg; this.chFactory = chFactory; - List<InetSocketAddress> addrs = parseAddresses(clientCfg.getAddresses()); - - channels = new ClientChannelHolder[addrs.size()]; - - for (int i = 0; i < channels.length; i++) - channels[i] = new ClientChannelHolder(new ClientChannelConfiguration(clientCfg, addrs.get(i))); - - curChIdx = new Random().nextInt(channels.length); // We already verified there is at least one address. - - partitionAwarenessEnabled = clientCfg.isPartitionAwarenessEnabled() && channels.length > 1; + partitionAwarenessEnabled = clientCfg.isPartitionAwarenessEnabled(); affinityCtx = new ClientCacheAffinityContext(binary); + } - ClientConnectionException lastEx = null; - - for (int i = 0; i < channels.length; i++) { - try { - channels[curChIdx].getOrCreateChannel(); - - if (partitionAwarenessEnabled) - initAllChannelsAsync(); - - return; - } catch (ClientConnectionException e) { - lastEx = e; - - rollCurrentChannel(); - } - } - - throw lastEx; + /** + * Establishing connections to servers. If partition awareness feature is enabled connections are created + * for every configured server. Otherwise only default channel is connected. + */ + void initConnection() { + channelsInit(false); + if (!partitionAwarenessEnabled) Review comment: FIxed, add test for that ########## File path: modules/core/src/main/java/org/apache/ignite/internal/client/thin/ReliableChannel.java ########## @@ -461,9 +418,8 @@ private void initAllChannelsAsync() { * @param ch Channel. */ private void onTopologyChanged(ClientChannel ch) { - if (partitionAwarenessEnabled && affinityCtx.updateLastTopologyVersion(ch.serverTopologyVersion(), - ch.serverNodeId())) - initAllChannelsAsync(); + if (affinityCtx.updateLastTopologyVersion(ch.serverTopologyVersion(), ch.serverNodeId())) + channelsInit(true); Review comment: Fair. Fixed it. Change signature of `initChannelHolders`, now it's not void but returns boolean (flag of interruption). Otherwise for disabled awareness and continuous topology events there would be a chances for StackOverflow, as channel failure trigger recursively `applyOnDefaultChannel` to enable default channel. ---------------------------------------------------------------- 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]
