diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractSelfTest.java index 342eb5a..91a4918 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheAbstractSelfTest.java @@ -58,7 +58,7 @@ public abstract class GridCacheAbstractSelfTest extends GridCommonAbstractTest { protected static final Map map = new ConcurrentHashMap8<>(); /** VM ip finder for TCP discovery. */ - private static TcpDiscoveryIpFinder ipFinder = new TcpDiscoveryVmIpFinder(true); + protected static TcpDiscoveryIpFinder ipFinder = new TcpDiscoveryVmIpFinder(true); /** * @return Grids count to start. diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTcpClientDiscoveryMultiThreadedTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTcpClientDiscoveryMultiThreadedTest.java new file mode 100644 index 0000000..db2c9fe --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/GridCacheTcpClientDiscoveryMultiThreadedTest.java @@ -0,0 +1,231 @@ +/* + * _________ _____ __________________ _____ + * __ ____/___________(_)______ /__ ____/______ ____(_)_______ + * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \ + * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / / + * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/ + */ + +package org.apache.ignite.internal.processors.cache; + +import org.apache.ignite.*; +import org.apache.ignite.cache.*; +import org.apache.ignite.cache.affinity.rendezvous.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.internal.*; +import org.apache.ignite.spi.discovery.tcp.*; +import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.*; +import java.net.*; +import java.util.*; +import java.util.concurrent.*; +import java.util.concurrent.atomic.*; + +import static org.apache.ignite.cache.CacheMode.*; + +/** + * Tests TcpClientDiscovery SPI with multiple client nodes that interact with a cache concurrently. + */ +public class GridCacheTcpClientDiscoveryMultiThreadedTest extends GridCacheAbstractSelfTest { + /** Server nodes count. */ + private volatile static int serverNodesCount; + + /** Client nodes count. */ + private volatile static int clientNodesCount; + + /** Client node or not. */ + private volatile static boolean client; + + /** {@inheritDoc} */ + @Override protected int gridCount() { + return serverNodesCount + clientNodesCount; + } + + /** {@inheritDoc} */ + @Override protected void beforeTestsStarted() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override protected void beforeTest() throws Exception { + // No-op. + } + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(gridName); + + // Filling configuration for client nodes + if (client) { + TcpDiscoveryVmIpFinder clientFinder = new TcpDiscoveryVmIpFinder(); + ArrayList addresses = new ArrayList<>(ipFinder.getRegisteredAddresses().size()); + + for (InetSocketAddress sockAddr : ipFinder.getRegisteredAddresses()) { + addresses.add(sockAddr.getHostString() + ":" + sockAddr.getPort()); + } + + clientFinder.setAddresses(addresses); + + TcpClientDiscoverySpi discoverySpi = new TcpClientDiscoverySpi(); + discoverySpi.setIpFinder(clientFinder); + + cfg.setDiscoverySpi(discoverySpi); + cfg.setClientMode(true); + } + + cfg.setLocalHost("127.0.0.1"); + + return cfg; + } + + /** {@inheritDoc} */ + @SuppressWarnings("unchecked") + @Override protected CacheConfiguration cacheConfiguration(String gridName) throws Exception { + CacheConfiguration cfg = super.cacheConfiguration(gridName); + + cfg.setCacheStoreFactory(null); + cfg.setReadThrough(false); + cfg.setWriteThrough(false); + cfg.setAffinity(new RendezvousAffinityFunction(false, 32)); + cfg.setBackups(1); + + return cfg; + } + + /** {@inheritDoc} */ + @Override protected CacheMode cacheMode() { + return PARTITIONED; + } + + /** + * @throws Exception If failed. + */ + public void testCacheConcurrentlyWithMultipleClientNodes() throws Exception { + try { + serverNodesCount = 3; + clientNodesCount = 4; + + startServerNodes(); + + client = true; + startGridsMultiThreaded(serverNodesCount, clientNodesCount); + + checkTopology(gridCount()); + awaitPartitionMapExchange(); + + // Explicitly create near cache for even client nodes + final boolean[] nearCacheNode = new boolean[clientNodesCount]; + for (int i = serverNodesCount; i < gridCount(); i++) { + if (i % 2 == 0) { + grid(i).createNearCache(null, new NearCacheConfiguration<>()); + nearCacheNode[i - serverNodesCount] = true; + } + } + + super.beforeTest(); + + final AtomicInteger threadsCnt = new AtomicInteger(); + + IgniteInternalFuture f = multithreadedAsync( + new Callable() { + @Override + public Object call() throws Exception { + int clientIdx = serverNodesCount + threadsCnt.getAndIncrement(); + Ignite node = grid(clientIdx); + + assert node.configuration().isClientMode(); + + IgniteCache cache = node.cache(null); + boolean isNearCacheNode = nearCacheNode[clientIdx - serverNodesCount]; + + for (int i = 100 * clientIdx; i < 100 * (clientIdx + 1); i++) + cache.put(i, i); + + + for (int i = 100 * clientIdx; i < 100 * (clientIdx + 1); i++) { + assertEquals(i, (int) cache.get(i)); + + if (isNearCacheNode) + assertEquals(i, (int) cache.localPeek(i, CachePeekMode.ONHEAP)); + } + + stopGrid(clientIdx); + + return null; + } + }, + clientNodesCount + ); + + f.get(); + + } + finally { + afterTestsStopped(); + } + } + + /** + * @throws Exception If failed. + */ + public void testCacheWithServerNodesRestart() throws Exception { + try { + serverNodesCount = 3; + clientNodesCount = 1; + + startServerNodes(); + + client = true; + Ignite client = startGrid(serverNodesCount); + + checkTopology(gridCount()); + awaitPartitionMapExchange(); + super.beforeTest(); + + IgniteCache cache = client.cache(null); + + performSimpleOperationsOnCache(cache); + + // Restart server nodes, client node should reconnect automatically. + stopServerNodes(); + startServerNodes(); + checkTopology(gridCount()); + awaitPartitionMapExchange(); + super.beforeTest(); + + performSimpleOperationsOnCache(cache); + } + finally { + afterTestsStopped(); + } + } + + /** + * @throws Exception If failed. + */ + private void startServerNodes() throws Exception { + client = false; + for (int i = 0; i < serverNodesCount; i++) + startGrid(i); + } + + /** + * @throws Exception + */ + private void stopServerNodes() throws Exception { + for (int i = 0; i < serverNodesCount; i++) + stopGrid(i); + } + + /** + * Executes simple operation on the cache. + * + * @param cache Cache instance to use. + */ + private void performSimpleOperationsOnCache(IgniteCache cache) { + for (int i = 100; i < 200; i++) + cache.put(i, i); + + for (int i = 100; i < 200; i++) + assertEquals(i, (int) cache.get(i)); + } +} diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/GridCacheClientModesAbstractSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/GridCacheClientModesAbstractSelfTest.java index 85256b4..7a9b3e7 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/GridCacheClientModesAbstractSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/GridCacheClientModesAbstractSelfTest.java @@ -21,7 +21,6 @@ import org.apache.ignite.*; import org.apache.ignite.cache.*; import org.apache.ignite.cache.affinity.rendezvous.*; import org.apache.ignite.configuration.*; -import org.apache.ignite.internal.*; import org.apache.ignite.internal.processors.cache.*; import org.apache.ignite.internal.util.typedef.*; @@ -57,8 +56,9 @@ public abstract class GridCacheClientModesAbstractSelfTest extends GridCacheAbst /** {@inheritDoc} */ @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { IgniteConfiguration cfg = super.getConfiguration(gridName); + int count = gridCnt.incrementAndGet(); - if (gridCnt.getAndIncrement() == 0) { + if ((count == gridCount() && isClientStartedLast()) || (count == 1 && !isClientStartedLast())) { cfg.setClientMode(true); nearOnlyGridName = gridName; @@ -99,6 +99,13 @@ public abstract class GridCacheClientModesAbstractSelfTest extends GridCacheAbst protected abstract boolean clientOnly(); /** + * @return boolean {@code true} if client's grid must be started last, {@code false} if it must be started first. + */ + protected boolean isClientStartedLast() { + return false; + } + + /** * @throws Exception If failed. */ public void testPutFromClientNode() throws Exception { diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/GridCacheClientModesTcpClientDiscoveryAbstractTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/GridCacheClientModesTcpClientDiscoveryAbstractTest.java new file mode 100644 index 0000000..62e4a1e --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/GridCacheClientModesTcpClientDiscoveryAbstractTest.java @@ -0,0 +1,60 @@ +/* + * 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.distributed; + +import org.apache.ignite.configuration.*; +import org.apache.ignite.spi.discovery.tcp.*; +import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.*; +import java.net.*; +import java.util.*; + +/** + * Tests TcpClientDiscovery SPI in client modes. + */ +public abstract class GridCacheClientModesTcpClientDiscoveryAbstractTest extends GridCacheClientModesAbstractSelfTest { + /** {@inheritDoc} */ + @Override protected boolean isClientStartedLast() { + return true; + } + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(gridName); + + if (cfg.isClientMode() != null && cfg.isClientMode()) { + TcpDiscoveryVmIpFinder clientFinder = new TcpDiscoveryVmIpFinder(); + ArrayList addresses = new ArrayList<>(ipFinder.getRegisteredAddresses().size()); + + for (InetSocketAddress sockAddr : ipFinder.getRegisteredAddresses()) { + addresses.add(sockAddr.getHostString() + ":" + sockAddr.getPort()); + } + + clientFinder.setAddresses(addresses); + + TcpClientDiscoverySpi discoverySpi = new TcpClientDiscoverySpi(); + discoverySpi.setIpFinder(clientFinder); + + + cfg.setDiscoverySpi(discoverySpi); + } + + cfg.setLocalHost("127.0.0.1"); + + return cfg; + } +} diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/GridCacheClientOnlyTcpClientDiscoveryTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/GridCacheClientOnlyTcpClientDiscoveryTest.java new file mode 100644 index 0000000..877803a --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/GridCacheClientOnlyTcpClientDiscoveryTest.java @@ -0,0 +1,35 @@ +/* + * 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.distributed; + +import org.apache.ignite.configuration.*; + +/** + * Tests TcpClientDiscovery SPI in the client mode only. + */ +public class GridCacheClientOnlyTcpClientDiscoveryTest extends GridCacheClientModesTcpClientDiscoveryAbstractTest { + /** {@inheritDoc} */ + @Override protected NearCacheConfiguration nearConfiguration() { + return null; + } + + /** {@inheritDoc} */ + @Override protected boolean clientOnly() { + return true; + } +} diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/GridCacheNearOnlyTcpClientDiscoveryTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/GridCacheNearOnlyTcpClientDiscoveryTest.java new file mode 100644 index 0000000..ea4b5bd --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/distributed/GridCacheNearOnlyTcpClientDiscoveryTest.java @@ -0,0 +1,28 @@ +/* + * 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.distributed; + +/** + * Tests TcpClientDiscovery SPI in the client mode with the near cache enabled. + */ +public class GridCacheNearOnlyTcpClientDiscoveryTest extends GridCacheClientModesTcpClientDiscoveryAbstractTest { + /** {@inheritDoc} */ + @Override protected boolean clientOnly() { + return false; + } +} diff --git a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/GridReduceQueryExecutor.java b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/GridReduceQueryExecutor.java index 02c7c55..b7edd27 100644 --- a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/GridReduceQueryExecutor.java +++ b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/twostep/GridReduceQueryExecutor.java @@ -484,7 +484,7 @@ public class GridReduceQueryExecutor implements GridMessageListener { Query prepare = (Query)ses.prepare(qry.query(), false); - List parsedParams = prepare.getParameters(); + List parsedParams = prepare.getParameters(); for (int i = Math.min(parsedParams.size(), qry.parameters().length); --i >= 0; ) { Object val = qry.parameters()[i];