http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/main/java/org/gridgain/grid/kernal/processors/ggfs/GridGgfsProcessor.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/ggfs/GridGgfsProcessor.java b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/ggfs/GridGgfsProcessor.java index 9f38469..0ab67c1 100644 --- a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/ggfs/GridGgfsProcessor.java +++ b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/ggfs/GridGgfsProcessor.java @@ -24,9 +24,8 @@ import org.apache.ignite.configuration.*; import org.apache.ignite.fs.*; import org.apache.ignite.fs.mapreduce.*; import org.apache.ignite.lang.*; -import org.gridgain.grid.*; import org.gridgain.grid.cache.*; -import org.gridgain.grid.cache.affinity.*; +import org.apache.ignite.cache.affinity.*; import org.gridgain.grid.kernal.*; import org.gridgain.grid.kernal.processors.cache.*; import org.gridgain.grid.kernal.processors.license.*; @@ -268,7 +267,7 @@ public class GridGgfsProcessor extends GridGgfsProcessorAdapter { if (cacheCfg == null) continue; // No cache for the given GGFS configuration. - GridCacheAffinityKeyMapper affMapper = cacheCfg.getAffinityMapper(); + CacheAffinityKeyMapper affMapper = cacheCfg.getAffinityMapper(); if (!(affMapper instanceof IgniteFsGroupDataBlocksKeyMapper)) // Do not create GGFS attributes for such a node nor throw error about invalid configuration.
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/main/java/org/gridgain/grid/kernal/processors/rest/handlers/cache/GridCacheCommandHandler.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/rest/handlers/cache/GridCacheCommandHandler.java b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/rest/handlers/cache/GridCacheCommandHandler.java index e8a2a98..698b44b 100644 --- a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/rest/handlers/cache/GridCacheCommandHandler.java +++ b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/rest/handlers/cache/GridCacheCommandHandler.java @@ -563,8 +563,9 @@ public class GridCacheCommandHandler extends GridRestCommandHandlerAdapter { resp.setResponse(f.get()); + Ignite ignite = c.gridProjection().ignite(); if (key != null) - resp.setAffinityNodeId(c.cache().affinity().mapKeyToNode(key).id().toString()); + resp.setAffinityNodeId(ignite.affinity(c.name()).mapKeyToNode(key).id().toString()); return resp; } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/main/java/org/gridgain/grid/kernal/processors/rest/handlers/top/GridTopologyCommandHandler.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/rest/handlers/top/GridTopologyCommandHandler.java b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/rest/handlers/top/GridTopologyCommandHandler.java index 8560db3..1de4795 100644 --- a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/rest/handlers/top/GridTopologyCommandHandler.java +++ b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/rest/handlers/top/GridTopologyCommandHandler.java @@ -21,7 +21,7 @@ import org.apache.ignite.*; import org.apache.ignite.cluster.*; import org.apache.ignite.lang.*; import org.apache.ignite.spi.*; -import org.gridgain.grid.cache.affinity.consistenthash.*; +import org.apache.ignite.cache.affinity.consistenthash.*; import org.gridgain.grid.kernal.*; import org.gridgain.grid.kernal.processors.cache.*; import org.gridgain.grid.kernal.processors.port.*; @@ -175,10 +175,10 @@ public class GridTopologyCommandHandler extends GridRestCommandHandlerAdapter { nodeBean.setTcpAddresses(nonEmptyList(node.<Collection<String>>attribute(ATTR_REST_TCP_ADDRS))); nodeBean.setTcpHostNames(nonEmptyList(node.<Collection<String>>attribute(ATTR_REST_TCP_HOST_NAMES))); - Integer dfltReplicaCnt = node.attribute(GridCacheConsistentHashAffinityFunction.DFLT_REPLICA_COUNT_ATTR_NAME); + Integer dfltReplicaCnt = node.attribute(CacheConsistentHashAffinityFunction.DFLT_REPLICA_COUNT_ATTR_NAME); if (dfltReplicaCnt == null) - dfltReplicaCnt = GridCacheConsistentHashAffinityFunction.DFLT_REPLICA_COUNT; + dfltReplicaCnt = CacheConsistentHashAffinityFunction.DFLT_REPLICA_COUNT; nodeBean.setReplicaCount(dfltReplicaCnt); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/main/java/org/gridgain/grid/kernal/processors/service/GridServiceProcessor.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/service/GridServiceProcessor.java b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/service/GridServiceProcessor.java index b909230..669fdc5 100644 --- a/modules/core/src/main/java/org/gridgain/grid/kernal/processors/service/GridServiceProcessor.java +++ b/modules/core/src/main/java/org/gridgain/grid/kernal/processors/service/GridServiceProcessor.java @@ -977,7 +977,7 @@ public class GridServiceProcessor extends GridProcessorAdapter { GridServiceAssignmentsKey key = new GridServiceAssignmentsKey(name); // Remove assignment on primary node in case of undeploy. - if (cache.cache().affinity().isPrimary(ctx.discovery().localNode(), key)) { + if (ctx.grid().affinity(cache.name()).isPrimary(ctx.discovery().localNode(), key)) { try { cache.remove(key); } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/main/java/org/gridgain/grid/kernal/visor/cache/VisorCache.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/gridgain/grid/kernal/visor/cache/VisorCache.java b/modules/core/src/main/java/org/gridgain/grid/kernal/visor/cache/VisorCache.java index c89c656..4c33c34 100644 --- a/modules/core/src/main/java/org/gridgain/grid/kernal/visor/cache/VisorCache.java +++ b/modules/core/src/main/java/org/gridgain/grid/kernal/visor/cache/VisorCache.java @@ -19,8 +19,8 @@ package org.gridgain.grid.kernal.visor.cache; import org.apache.ignite.*; import org.apache.ignite.cluster.*; -import org.gridgain.grid.*; import org.gridgain.grid.cache.*; +import org.apache.ignite.IgniteCacheAffinity; import org.gridgain.grid.kernal.*; import org.gridgain.grid.kernal.processors.cache.*; import org.gridgain.grid.kernal.processors.cache.distributed.dht.*; @@ -106,6 +106,8 @@ public class VisorCache implements Serializable { GridCacheAdapter ca = ((GridKernal)g).internalCache(cacheName); + IgniteCacheAffinity aff = g.affinity(ca.name()); + long swapSize; long swapKeys; @@ -163,7 +165,7 @@ public class VisorCache implements Serializable { // Old way of collecting partitions info. ClusterNode node = g.cluster().localNode(); - int[] pp = ca.affinity().primaryPartitions(node); + int[] pp = aff.primaryPartitions(node); pps = new ArrayList<>(pp.length); @@ -173,7 +175,7 @@ public class VisorCache implements Serializable { pps.add(new IgnitePair<>(p, set != null ? set.size() : 0)); } - int[] bp = ca.affinity().backupPartitions(node); + int[] bp = aff.backupPartitions(node); bps = new ArrayList<>(bp.length); @@ -220,7 +222,7 @@ public class VisorCache implements Serializable { cache.offHeapEntriesCount(ca.offHeapEntriesCount()); cache.swapSize(swapSize); cache.swapKeys(swapKeys); - cache.partitions(ca.affinity().partitions()); + cache.partitions(aff.partitions()); cache.primaryPartitions(pps); cache.backupPartitions(bps); cache.metrics(VisorCacheMetrics.from(ca)); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/main/java/org/gridgain/grid/kernal/visor/cache/VisorCacheAffinityConfiguration.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/gridgain/grid/kernal/visor/cache/VisorCacheAffinityConfiguration.java b/modules/core/src/main/java/org/gridgain/grid/kernal/visor/cache/VisorCacheAffinityConfiguration.java index 504e63a..c1d006f 100644 --- a/modules/core/src/main/java/org/gridgain/grid/kernal/visor/cache/VisorCacheAffinityConfiguration.java +++ b/modules/core/src/main/java/org/gridgain/grid/kernal/visor/cache/VisorCacheAffinityConfiguration.java @@ -18,8 +18,8 @@ package org.gridgain.grid.kernal.visor.cache; import org.gridgain.grid.cache.*; -import org.gridgain.grid.cache.affinity.*; -import org.gridgain.grid.cache.affinity.consistenthash.*; +import org.apache.ignite.cache.affinity.*; +import org.apache.ignite.cache.affinity.consistenthash.*; import org.gridgain.grid.util.typedef.internal.*; import org.jetbrains.annotations.*; @@ -57,13 +57,13 @@ public class VisorCacheAffinityConfiguration implements Serializable { * @return Data transfer object for affinity configuration properties. */ public static VisorCacheAffinityConfiguration from(GridCacheConfiguration ccfg) { - GridCacheAffinityFunction aff = ccfg.getAffinity(); + CacheAffinityFunction aff = ccfg.getAffinity(); Integer dfltReplicas = null; Boolean excludeNeighbors = null; - if (aff instanceof GridCacheConsistentHashAffinityFunction) { - GridCacheConsistentHashAffinityFunction hashAffFunc = (GridCacheConsistentHashAffinityFunction)aff; + if (aff instanceof CacheConsistentHashAffinityFunction) { + CacheConsistentHashAffinityFunction hashAffFunc = (CacheConsistentHashAffinityFunction)aff; dfltReplicas = hashAffFunc.getDefaultReplicas(); excludeNeighbors = hashAffFunc.isExcludeNeighbors(); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/main/java/org/gridgain/jdbc/GridJdbcDriver.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/gridgain/jdbc/GridJdbcDriver.java b/modules/core/src/main/java/org/gridgain/jdbc/GridJdbcDriver.java index 826d5c8..c7bd442 100644 --- a/modules/core/src/main/java/org/gridgain/jdbc/GridJdbcDriver.java +++ b/modules/core/src/main/java/org/gridgain/jdbc/GridJdbcDriver.java @@ -17,8 +17,6 @@ package org.gridgain.jdbc; -import org.gridgain.grid.cache.affinity.*; - import java.sql.*; import java.util.*; import java.util.logging.*; @@ -49,7 +47,7 @@ import java.util.logging.*; * <li> * Joins will work correctly only if joined objects are stored in * collocated mode. Refer to - * {@link GridCacheAffinityKey} + * {@link org.apache.ignite.cache.affinity.CacheAffinityKey} * javadoc for more details. * </li> * <li> http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/test/config/load/dsi-49-server-production.xml ---------------------------------------------------------------------- diff --git a/modules/core/src/test/config/load/dsi-49-server-production.xml b/modules/core/src/test/config/load/dsi-49-server-production.xml index c9ea62d..8c06414 100644 --- a/modules/core/src/test/config/load/dsi-49-server-production.xml +++ b/modules/core/src/test/config/load/dsi-49-server-production.xml @@ -52,7 +52,7 @@ <!-- 1 backup --> <property name="backups" value="1"/> <property name="affinity"> - <bean class="org.gridgain.grid.cache.affinity.consistenthash.GridCacheConsistentHashAffinityFunction"> + <bean class="org.apache.ignite.cache.affinity.consistenthash.CacheConsistentHashAffinityFunction"> <property name="excludeNeighbors" value="true"/> </bean> </property> http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/test/java/org/apache/ignite/cache/affinity/fair/CachePartitionFairAffinityNodesSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/cache/affinity/fair/CachePartitionFairAffinityNodesSelfTest.java b/modules/core/src/test/java/org/apache/ignite/cache/affinity/fair/CachePartitionFairAffinityNodesSelfTest.java new file mode 100644 index 0000000..4ff4023 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/cache/affinity/fair/CachePartitionFairAffinityNodesSelfTest.java @@ -0,0 +1,246 @@ +/* + * 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.cache.affinity.fair; + +import org.apache.ignite.cluster.*; +import org.apache.ignite.configuration.*; +import org.gridgain.grid.cache.*; +import org.gridgain.grid.kernal.*; +import org.apache.ignite.spi.discovery.tcp.*; +import org.apache.ignite.spi.discovery.tcp.ipfinder.*; +import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.*; +import org.gridgain.testframework.junits.common.*; + +import java.util.*; + +/** + * Tests partition fair affinity in real grid. + */ +public class CachePartitionFairAffinityNodesSelfTest extends GridCommonAbstractTest { + /** IP finder. */ + private static final TcpDiscoveryIpFinder IP_FINDER = new TcpDiscoveryVmIpFinder(true); + + /** Number of backups. */ + private int backups; + + /** Number of partitions. */ + private int parts = 512; + + /** Add nodes test. */ + private static final boolean[] ADD_ONLY = new boolean[] {true, true, true, true, true, true}; + + /** Add nodes test. */ + private static final boolean[] ADD_REMOVE = new boolean[] + { + true, true, true, true, true, true, + false, false, false, false, false + }; + + /** */ + private static final boolean[] MIXED1 = new boolean[] + { + // 1 2 3 2 3 4 3 4 5 4 3 2 + true, true, true, false, true, true, false, true, true, false, false, false + }; + + /** */ + private static final boolean[] MIXED2 = new boolean[] + { + // 1 2 3 2 1 2 1 2 3 2 1 2 + true, true, true, false, false, true, false, true, true, false, false, true + }; + + /** */ + private static final boolean[] MIXED3 = new boolean[] + { + // 1 2 3 4 5 6 5 6 7 8 9 8 7 8 9 + true, true, true, true, true, true, false, true, true, true, true, false, false, true, true, + // 8 7 6 + false, false, false + }; + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(gridName); + + GridCacheConfiguration ccfg = cacheConfiguration(); + + cfg.setCacheConfiguration(ccfg); + + TcpDiscoverySpi discoSpi = new TcpDiscoverySpi(); + + discoSpi.setIpFinder(IP_FINDER); + + cfg.setDiscoverySpi(discoSpi); + + return cfg; + } + + /** + * @return Cache configuration. + */ + private GridCacheConfiguration cacheConfiguration() { + GridCacheConfiguration cfg = new GridCacheConfiguration(); + + cfg.setBackups(backups); + + cfg.setCacheMode(GridCacheMode.PARTITIONED); + + cfg.setDistributionMode(GridCacheDistributionMode.PARTITIONED_ONLY); + + cfg.setAffinity(new CachePartitionFairAffinity(parts)); + + return cfg; + } + + /** + * @throws Exception If failed. + */ + public void testAdd() throws Exception { + checkSequence(ADD_ONLY); + } + + /** + * @throws Exception If failed. + */ + public void testAddRemove() throws Exception { + checkSequence(ADD_REMOVE); + } + + /** + * @throws Exception If failed. + */ + public void testMixed1() throws Exception { + checkSequence(MIXED1); + } + + /** + * @throws Exception If failed. + */ + public void testMixed2() throws Exception { + checkSequence(MIXED2); + } + + /** + * @throws Exception If failed. + */ + public void testMixed3() throws Exception { + checkSequence(MIXED3); + } + + /** + * @throws Exception If failed. + */ + private void checkSequence(boolean[] seq) throws Exception { + for (int b = 0; b < 3; b++) { + backups = b; + + info(">>>>>>>>>>>>>>>> Checking backups: " + backups); + + checkSequence0(seq); + + info(">>>>>>>>>>>>>>>> Finished check: " + backups); + } + } + + /** + * @param seq Start/stop sequence. + * @throws Exception If failed. + */ + private void checkSequence0(boolean[] seq) throws Exception { + try { + startGrid(0); + + TreeSet<Integer> started = new TreeSet<>(); + + started.add(0); + + int topVer = 1; + + for (boolean start : seq) { + if (start) { + int nextIdx = nextIndex(started); + + startGrid(nextIdx); + + started.add(nextIdx); + } + else { + int idx = started.last(); + + stopGrid(idx); + + started.remove(idx); + } + + topVer++; + + info("Grid 0: " + grid(0).localNode().id()); + + ((GridKernal)grid(0)).internalCache().context().affinity().affinityReadyFuture(topVer).get(); + + for (int i : started) { + if (i != 0) { + GridEx grid = grid(i); + + ((GridKernal)grid).internalCache().context().affinity().affinityReadyFuture(topVer).get(); + + info("Grid " + i + ": " + grid.localNode().id()); + + for (int part = 0; part < parts; part++) { + GridCache<Object, Object> cache0 = grid(0).cache(null); + + GridCache<Object, Object> cache = grid.cache(null); + + List<ClusterNode> firstNodes = (List<ClusterNode>)grid(0).affinity(cache0.name()) + .mapPartitionToPrimaryAndBackups(part); + + List<ClusterNode> secondNodes = (List<ClusterNode>)grid.affinity(cache.name()) + .mapPartitionToPrimaryAndBackups(part); + + assertEquals(firstNodes.size(), secondNodes.size()); + + for (int n = 0; n < firstNodes.size(); n++) + assertEquals(firstNodes.get(n), secondNodes.get(n)); + } + } + } + } + } + finally { + stopAllGrids(); + } + } + + /** + * First positive integer that is not present in started set. + * + * @param started Already started indices. + * @return First positive integer that is not present in started set. + */ + private int nextIndex(Collection<Integer> started) { + assert started.contains(0); + + for (int i = 1; i < 10000; i++) { + if (!started.contains(i)) + return i; + } + + throw new IllegalStateException(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/test/java/org/apache/ignite/cache/affinity/fair/CachePartitionFairAffinitySelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/cache/affinity/fair/CachePartitionFairAffinitySelfTest.java b/modules/core/src/test/java/org/apache/ignite/cache/affinity/fair/CachePartitionFairAffinitySelfTest.java new file mode 100644 index 0000000..e261ab5 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/cache/affinity/fair/CachePartitionFairAffinitySelfTest.java @@ -0,0 +1,261 @@ +/* + * 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.cache.affinity.fair; + +import org.apache.ignite.cluster.*; +import org.apache.ignite.events.*; +import org.apache.ignite.cache.affinity.*; +import org.gridgain.grid.kernal.processors.affinity.*; +import org.gridgain.testframework.*; +import org.gridgain.testframework.junits.common.*; + +import java.util.*; + +/** + * + */ +public class CachePartitionFairAffinitySelfTest extends GridCommonAbstractTest { + /** + * @throws Exception If failed. + */ + public void testNodeRemovedNoBackups() throws Exception { + checkNodeRemoved(0); + } + + /** + * @throws Exception If failed. + */ + public void testNodeRemovedOneBackup() throws Exception { + checkNodeRemoved(1); + } + + /** + * @throws Exception If failed. + */ + public void testNodeRemovedTwoBackups() throws Exception { + checkNodeRemoved(2); + } + + /** + * @throws Exception If failed. + */ + public void testNodeRemovedThreeBackups() throws Exception { + checkNodeRemoved(3); + } + + /** + * @throws Exception If failed. + */ + public void testRandomReassignmentNoBackups() throws Exception { + checkRandomReassignment(0); + } + + /** + * @throws Exception If failed. + */ + public void testRandomReassignmentOneBackup() throws Exception { + checkRandomReassignment(1); + } + + /** + * @throws Exception If failed. + */ + public void testRandomReassignmentTwoBackups() throws Exception { + checkRandomReassignment(2); + } + + /** + * @throws Exception If failed. + */ + public void testRandomReassignmentThreeBackups() throws Exception { + checkRandomReassignment(3); + } + + /** + * @throws Exception If failed. + */ + private void checkNodeRemoved(int backups) throws Exception { + int parts = 256; + + CacheAffinityFunction aff = new CachePartitionFairAffinity(parts); + + int nodesCnt = 50; + + List<ClusterNode> nodes = new ArrayList<>(nodesCnt); + + List<List<ClusterNode>> prev = null; + + for (int i = 0; i < nodesCnt; i++) { + info("======================================"); + info("Assigning partitions: " + i); + info("======================================"); + + ClusterNode node = new GridTestNode(UUID.randomUUID()); + + nodes.add(node); + + IgniteDiscoveryEvent discoEvt = new IgniteDiscoveryEvent(node, "", IgniteEventType.EVT_NODE_JOINED, + node); + + List<List<ClusterNode>> assignment = aff.assignPartitions( + new CacheAffinityFunctionContextImpl(nodes, prev, discoEvt, i, backups)); + + info("Assigned."); + + verifyAssignment(assignment, backups, parts, nodes.size()); + + prev = assignment; + } + + info("======================================"); + info("Will remove nodes."); + info("======================================"); + + for (int i = 0; i < nodesCnt - 1; i++) { + info("======================================"); + info("Assigning partitions: " + i); + info("======================================"); + + ClusterNode rmv = nodes.remove(nodes.size() - 1); + + IgniteDiscoveryEvent discoEvt = new IgniteDiscoveryEvent(rmv, "", IgniteEventType.EVT_NODE_LEFT, rmv); + + List<List<ClusterNode>> assignment = aff.assignPartitions( + new CacheAffinityFunctionContextImpl(nodes, prev, discoEvt, i, backups)); + + info("Assigned."); + + verifyAssignment(assignment, backups, parts, nodes.size()); + + prev = assignment; + } + } + + @SuppressWarnings("IfMayBeConditional") + private void checkRandomReassignment(int backups) { + int parts = 256; + + CacheAffinityFunction aff = new CachePartitionFairAffinity(parts); + + Random rnd = new Random(); + + int maxNodes = 50; + + List<ClusterNode> nodes = new ArrayList<>(maxNodes); + + List<List<ClusterNode>> prev = null; + + int state = 0; + + int i = 0; + + while (true) { + boolean add; + + if (nodes.size() < 2) { + // Returned back to one node? + if (state == 1) + return; + + add = true; + } + else if (nodes.size() == maxNodes) { + if (state == 0) + state = 1; + + add = false; + } + else { + // Nodes size in [2, maxNodes - 1]. + if (state == 0) + add = rnd.nextInt(3) != 0; // 66% to add, 33% to remove. + else + add = rnd.nextInt(3) == 0; // 33% to add, 66% to remove. + } + + IgniteDiscoveryEvent discoEvt; + + if (add) { + ClusterNode addedNode = new GridTestNode(UUID.randomUUID()); + + nodes.add(addedNode); + + discoEvt = new IgniteDiscoveryEvent(addedNode, "", IgniteEventType.EVT_NODE_JOINED, addedNode); + } + else { + ClusterNode rmvNode = nodes.remove(rnd.nextInt(nodes.size())); + + discoEvt = new IgniteDiscoveryEvent(rmvNode, "", IgniteEventType.EVT_NODE_LEFT, rmvNode); + } + + info("======================================"); + info("Assigning partitions [iter=" + i + ", discoEvt=" + discoEvt + ", nodesSize=" + nodes.size() + ']'); + info("======================================"); + + List<List<ClusterNode>> assignment = aff.assignPartitions( + new CacheAffinityFunctionContextImpl(nodes, prev, discoEvt, i, backups)); + + verifyAssignment(assignment, backups, parts, nodes.size()); + + prev = assignment; + + i++; + } + } + + /** + * @param assignment Assignment to verify. + */ + private void verifyAssignment(List<List<ClusterNode>> assignment, int keyBackups, int partsCnt, int topSize) { + Map<UUID, Collection<Integer>> mapping = new HashMap<>(); + + int ideal = Math.round((float)partsCnt / topSize * Math.min(keyBackups + 1, topSize)); + + for (int part = 0; part < assignment.size(); part++) { + for (ClusterNode node : assignment.get(part)) { + assert node != null; + + Collection<Integer> parts = mapping.get(node.id()); + + if (parts == null) { + parts = new HashSet<>(); + + mapping.put(node.id(), parts); + } + + assertTrue(parts.add(part)); + } + } + + int max = -1, min = Integer.MAX_VALUE; + + for (Collection<Integer> parts : mapping.values()) { + max = Math.max(max, parts.size()); + min = Math.min(min, parts.size()); + } + + log().warning("max=" + max + ", min=" + min + ", ideal=" + ideal + ", minDev=" + deviation(min, ideal) + "%, " + + "maxDev=" + deviation(max, ideal) + "%"); + + assertTrue("max=" + max + ", min=" + min, max - min < (keyBackups + 1) * topSize); + } + + private static int deviation(int val, int ideal) { + return Math.round(Math.abs(((float)val - ideal) / ideal * 100)); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheInvokeAbstractTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheInvokeAbstractTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheInvokeAbstractTest.java index bda973f..356ed77 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheInvokeAbstractTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/IgniteCacheInvokeAbstractTest.java @@ -460,7 +460,7 @@ public abstract class IgniteCacheInvokeAbstractTest extends IgniteCacheAbstractT Object val = cache.localPeek(key); if (val == null) - assertFalse(cache(0).affinity().isPrimaryOrBackup(ignite(i).cluster().localNode(), key)); + assertFalse(affinity(0).isPrimaryOrBackup(ignite(i).cluster().localNode(), key)); else assertEquals("Unexpected value for grid " + i, expVal, val); } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/expiry/IgniteCacheExpiryPolicyAbstractTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/expiry/IgniteCacheExpiryPolicyAbstractTest.java b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/expiry/IgniteCacheExpiryPolicyAbstractTest.java index 0d3b54f..7f6cdef 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/expiry/IgniteCacheExpiryPolicyAbstractTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/processors/cache/expiry/IgniteCacheExpiryPolicyAbstractTest.java @@ -14,6 +14,7 @@ import org.apache.ignite.cluster.*; import org.apache.ignite.internal.processors.cache.*; import org.apache.ignite.transactions.*; import org.gridgain.grid.cache.*; +import org.apache.ignite.IgniteCacheAffinity; import org.gridgain.grid.kernal.*; import org.gridgain.grid.kernal.processors.cache.*; import org.gridgain.grid.util.lang.*; @@ -741,7 +742,7 @@ public abstract class IgniteCacheExpiryPolicyAbstractTest extends IgniteCacheAbs checkTtl(key, 60_000L); IgniteCache<Object, Object> cache = - cache(0).affinity().isPrimary(grid(1).localNode(), key) ? jcache(1) : jcache(2); + affinity(0).isPrimary(grid(1).localNode(), key) ? jcache(1) : jcache(2); assertEquals(1, cache.get(key)); @@ -803,6 +804,7 @@ public abstract class IgniteCacheExpiryPolicyAbstractTest extends IgniteCacheAbs }, 3000); GridCache<Integer, Object> cache = cache(0); + IgniteCacheAffinity<Integer> affinity = affinity(0); for (int i = 0; i < gridCount(); i++) { ClusterNode node = grid(i).cluster().localNode(); @@ -812,8 +814,8 @@ public abstract class IgniteCacheExpiryPolicyAbstractTest extends IgniteCacheAbs if (val != null) { log.info("Unexpected value [grid=" + i + - ", primary=" + cache.affinity().isPrimary(node, key) + - ", backup=" + cache.affinity().isBackup(node, key) + ']'); + ", primary=" + affinity.isPrimary(node, key) + + ", backup=" + affinity.isBackup(node, key) + ']'); } assertNull("Unexpected non-null value for grid " + i, val); @@ -857,7 +859,7 @@ public abstract class IgniteCacheExpiryPolicyAbstractTest extends IgniteCacheAbs e = cache.context().near().dht().peekEx(key); if (e == null) - assertTrue("Not found " + key, !cache.affinity().isPrimaryOrBackup(grid.localNode(), key)); + assertTrue("Not found " + key, !grid.affinity(cache.name()).isPrimaryOrBackup(grid.localNode(), key)); else { found = true; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/test/java/org/gridgain/grid/GridCacheAffinityBackupsSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/gridgain/grid/GridCacheAffinityBackupsSelfTest.java b/modules/core/src/test/java/org/gridgain/grid/GridCacheAffinityBackupsSelfTest.java deleted file mode 100644 index 4845c75..0000000 --- a/modules/core/src/test/java/org/gridgain/grid/GridCacheAffinityBackupsSelfTest.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * 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.gridgain.grid; - -import org.apache.ignite.cluster.*; -import org.apache.ignite.configuration.*; -import org.gridgain.grid.cache.*; -import org.gridgain.grid.cache.affinity.*; -import org.gridgain.grid.cache.affinity.consistenthash.*; -import org.gridgain.grid.cache.affinity.fair.*; -import org.gridgain.grid.cache.affinity.rendezvous.*; -import org.gridgain.testframework.junits.common.*; - -import java.util.*; - -/** - * Tests affinity function with different number of backups. - */ -public class GridCacheAffinityBackupsSelfTest extends GridCommonAbstractTest { - /** Number of backups. */ - private int backups; - - /** Affinity function. */ - private GridCacheAffinityFunction func; - - private int nodesCnt = 5; - - /** {@inheritDoc} */ - @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { - IgniteConfiguration cfg = super.getConfiguration(gridName); - - GridCacheConfiguration ccfg = new GridCacheConfiguration(); - - ccfg.setCacheMode(GridCacheMode.PARTITIONED); - ccfg.setBackups(backups); - ccfg.setAffinity(func); - - cfg.setCacheConfiguration(ccfg); - - return cfg; - } - - /** - * @throws Exception If failed. - */ - public void testConsistentHashBackups() throws Exception { - for (int i = 0; i < nodesCnt; i++) - checkBackups(i, new GridCacheConsistentHashAffinityFunction()); - } - - /** - * @throws Exception If failed. - */ - public void testRendezvousBackups() throws Exception { - for (int i = 0; i < nodesCnt; i++) - checkBackups(i, new GridCacheRendezvousAffinityFunction()); - } - - /** - * @throws Exception If failed. - */ - public void testFairBackups() throws Exception { - for (int i = 0; i < nodesCnt; i++) - checkBackups(i, new GridCachePartitionFairAffinity()); - } - - /** - * @throws Exception If failed. - */ - private void checkBackups(int backups, GridCacheAffinityFunction func) throws Exception { - this.backups = backups; - this.func = func; - - startGrids(nodesCnt); - - try { - GridCache<Object, Object> cache = grid(0).cache(null); - - Collection<UUID> members = new HashSet<>(); - - for (int i = 0; i < 10000; i++) { - Collection<ClusterNode> nodes = cache.affinity().mapKeyToPrimaryAndBackups(i); - - assertEquals(backups + 1, nodes.size()); - - for (ClusterNode n : nodes) - members.add(n.id()); - } - - assertEquals(nodesCnt, members.size()); - } - finally { - stopAllGrids(); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/test/java/org/gridgain/grid/IgniteCacheAffinityBackupsSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/gridgain/grid/IgniteCacheAffinityBackupsSelfTest.java b/modules/core/src/test/java/org/gridgain/grid/IgniteCacheAffinityBackupsSelfTest.java new file mode 100644 index 0000000..e2cca3a --- /dev/null +++ b/modules/core/src/test/java/org/gridgain/grid/IgniteCacheAffinityBackupsSelfTest.java @@ -0,0 +1,111 @@ +/* + * 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.gridgain.grid; + +import org.apache.ignite.cluster.*; +import org.apache.ignite.configuration.*; +import org.gridgain.grid.cache.*; +import org.apache.ignite.cache.affinity.*; +import org.apache.ignite.cache.affinity.consistenthash.*; +import org.apache.ignite.cache.affinity.fair.*; +import org.apache.ignite.cache.affinity.rendezvous.*; +import org.gridgain.testframework.junits.common.*; + +import java.util.*; + +/** + * Tests affinity function with different number of backups. + */ +public class IgniteCacheAffinityBackupsSelfTest extends GridCommonAbstractTest { + /** Number of backups. */ + private int backups; + + /** Affinity function. */ + private CacheAffinityFunction func; + + private int nodesCnt = 5; + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { + IgniteConfiguration cfg = super.getConfiguration(gridName); + + GridCacheConfiguration ccfg = new GridCacheConfiguration(); + + ccfg.setCacheMode(GridCacheMode.PARTITIONED); + ccfg.setBackups(backups); + ccfg.setAffinity(func); + + cfg.setCacheConfiguration(ccfg); + + return cfg; + } + + /** + * @throws Exception If failed. + */ + public void testConsistentHashBackups() throws Exception { + for (int i = 0; i < nodesCnt; i++) + checkBackups(i, new CacheConsistentHashAffinityFunction()); + } + + /** + * @throws Exception If failed. + */ + public void testRendezvousBackups() throws Exception { + for (int i = 0; i < nodesCnt; i++) + checkBackups(i, new CacheRendezvousAffinityFunction()); + } + + /** + * @throws Exception If failed. + */ + public void testFairBackups() throws Exception { + for (int i = 0; i < nodesCnt; i++) + checkBackups(i, new CachePartitionFairAffinity()); + } + + /** + * @throws Exception If failed. + */ + private void checkBackups(int backups, CacheAffinityFunction func) throws Exception { + this.backups = backups; + this.func = func; + + startGrids(nodesCnt); + + try { + GridCache<Object, Object> cache = grid(0).cache(null); + + Collection<UUID> members = new HashSet<>(); + + for (int i = 0; i < 10000; i++) { + Collection<ClusterNode> nodes = grid(0).affinity(cache.name()).mapKeyToPrimaryAndBackups(i); + + assertEquals(backups + 1, nodes.size()); + + for (ClusterNode n : nodes) + members.add(n.id()); + } + + assertEquals(nodesCnt, members.size()); + } + finally { + stopAllGrids(); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/test/java/org/gridgain/grid/cache/affinity/fair/GridCachePartitionFairAffinityNodesSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/gridgain/grid/cache/affinity/fair/GridCachePartitionFairAffinityNodesSelfTest.java b/modules/core/src/test/java/org/gridgain/grid/cache/affinity/fair/GridCachePartitionFairAffinityNodesSelfTest.java deleted file mode 100644 index 0d5b7a1..0000000 --- a/modules/core/src/test/java/org/gridgain/grid/cache/affinity/fair/GridCachePartitionFairAffinityNodesSelfTest.java +++ /dev/null @@ -1,242 +0,0 @@ -/* - * 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.gridgain.grid.cache.affinity.fair; - -import org.apache.ignite.cluster.*; -import org.apache.ignite.configuration.*; -import org.gridgain.grid.cache.*; -import org.gridgain.grid.kernal.*; -import org.apache.ignite.spi.discovery.tcp.*; -import org.apache.ignite.spi.discovery.tcp.ipfinder.*; -import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.*; -import org.gridgain.testframework.junits.common.*; - -import java.util.*; - -/** - * Tests partition fair affinity in real grid. - */ -public class GridCachePartitionFairAffinityNodesSelfTest extends GridCommonAbstractTest { - /** IP finder. */ - private static final TcpDiscoveryIpFinder IP_FINDER = new TcpDiscoveryVmIpFinder(true); - - /** Number of backups. */ - private int backups; - - /** Number of partitions. */ - private int parts = 512; - - /** Add nodes test. */ - private static final boolean[] ADD_ONLY = new boolean[] {true, true, true, true, true, true}; - - /** Add nodes test. */ - private static final boolean[] ADD_REMOVE = new boolean[] - { - true, true, true, true, true, true, - false, false, false, false, false - }; - - /** */ - private static final boolean[] MIXED1 = new boolean[] - { - // 1 2 3 2 3 4 3 4 5 4 3 2 - true, true, true, false, true, true, false, true, true, false, false, false - }; - - /** */ - private static final boolean[] MIXED2 = new boolean[] - { - // 1 2 3 2 1 2 1 2 3 2 1 2 - true, true, true, false, false, true, false, true, true, false, false, true - }; - - /** */ - private static final boolean[] MIXED3 = new boolean[] - { - // 1 2 3 4 5 6 5 6 7 8 9 8 7 8 9 - true, true, true, true, true, true, false, true, true, true, true, false, false, true, true, - // 8 7 6 - false, false, false - }; - - /** {@inheritDoc} */ - @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception { - IgniteConfiguration cfg = super.getConfiguration(gridName); - - GridCacheConfiguration ccfg = cacheConfiguration(); - - cfg.setCacheConfiguration(ccfg); - - TcpDiscoverySpi discoSpi = new TcpDiscoverySpi(); - - discoSpi.setIpFinder(IP_FINDER); - - cfg.setDiscoverySpi(discoSpi); - - return cfg; - } - - /** - * @return Cache configuration. - */ - private GridCacheConfiguration cacheConfiguration() { - GridCacheConfiguration cfg = new GridCacheConfiguration(); - - cfg.setBackups(backups); - - cfg.setCacheMode(GridCacheMode.PARTITIONED); - - cfg.setDistributionMode(GridCacheDistributionMode.PARTITIONED_ONLY); - - cfg.setAffinity(new GridCachePartitionFairAffinity(parts)); - - return cfg; - } - - /** - * @throws Exception If failed. - */ - public void testAdd() throws Exception { - checkSequence(ADD_ONLY); - } - - /** - * @throws Exception If failed. - */ - public void testAddRemove() throws Exception { - checkSequence(ADD_REMOVE); - } - - /** - * @throws Exception If failed. - */ - public void testMixed1() throws Exception { - checkSequence(MIXED1); - } - - /** - * @throws Exception If failed. - */ - public void testMixed2() throws Exception { - checkSequence(MIXED2); - } - - /** - * @throws Exception If failed. - */ - public void testMixed3() throws Exception { - checkSequence(MIXED3); - } - - /** - * @throws Exception If failed. - */ - private void checkSequence(boolean[] seq) throws Exception { - for (int b = 0; b < 3; b++) { - backups = b; - - info(">>>>>>>>>>>>>>>> Checking backups: " + backups); - - checkSequence0(seq); - - info(">>>>>>>>>>>>>>>> Finished check: " + backups); - } - } - - /** - * @param seq Start/stop sequence. - * @throws Exception If failed. - */ - private void checkSequence0(boolean[] seq) throws Exception { - try { - startGrid(0); - - TreeSet<Integer> started = new TreeSet<>(); - - started.add(0); - - int topVer = 1; - - for (boolean start : seq) { - if (start) { - int nextIdx = nextIndex(started); - - startGrid(nextIdx); - - started.add(nextIdx); - } - else { - int idx = started.last(); - - stopGrid(idx); - - started.remove(idx); - } - - topVer++; - - info("Grid 0: " + grid(0).localNode().id()); - - ((GridKernal)grid(0)).internalCache().context().affinity().affinityReadyFuture(topVer).get(); - - for (int i : started) { - if (i != 0) { - GridEx grid = grid(i); - - ((GridKernal)grid).internalCache().context().affinity().affinityReadyFuture(topVer).get(); - - info("Grid " + i + ": " + grid.localNode().id()); - - for (int part = 0; part < parts; part++) { - List<ClusterNode> firstNodes = (List<ClusterNode>)grid(0).cache(null).affinity() - .mapPartitionToPrimaryAndBackups(part); - - List<ClusterNode> secondNodes = (List<ClusterNode>)grid.cache(null).affinity() - .mapPartitionToPrimaryAndBackups(part); - - assertEquals(firstNodes.size(), secondNodes.size()); - - for (int n = 0; n < firstNodes.size(); n++) - assertEquals(firstNodes.get(n), secondNodes.get(n)); - } - } - } - } - } - finally { - stopAllGrids(); - } - } - - /** - * First positive integer that is not present in started set. - * - * @param started Already started indices. - * @return First positive integer that is not present in started set. - */ - private int nextIndex(Collection<Integer> started) { - assert started.contains(0); - - for (int i = 1; i < 10000; i++) { - if (!started.contains(i)) - return i; - } - - throw new IllegalStateException(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/test/java/org/gridgain/grid/cache/affinity/fair/GridCachePartitionFairAffinitySelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/gridgain/grid/cache/affinity/fair/GridCachePartitionFairAffinitySelfTest.java b/modules/core/src/test/java/org/gridgain/grid/cache/affinity/fair/GridCachePartitionFairAffinitySelfTest.java deleted file mode 100644 index 2e9b784..0000000 --- a/modules/core/src/test/java/org/gridgain/grid/cache/affinity/fair/GridCachePartitionFairAffinitySelfTest.java +++ /dev/null @@ -1,261 +0,0 @@ -/* - * 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.gridgain.grid.cache.affinity.fair; - -import org.apache.ignite.cluster.*; -import org.apache.ignite.events.*; -import org.gridgain.grid.cache.affinity.*; -import org.gridgain.grid.kernal.processors.affinity.*; -import org.gridgain.testframework.*; -import org.gridgain.testframework.junits.common.*; - -import java.util.*; - -/** - * - */ -public class GridCachePartitionFairAffinitySelfTest extends GridCommonAbstractTest { - /** - * @throws Exception If failed. - */ - public void testNodeRemovedNoBackups() throws Exception { - checkNodeRemoved(0); - } - - /** - * @throws Exception If failed. - */ - public void testNodeRemovedOneBackup() throws Exception { - checkNodeRemoved(1); - } - - /** - * @throws Exception If failed. - */ - public void testNodeRemovedTwoBackups() throws Exception { - checkNodeRemoved(2); - } - - /** - * @throws Exception If failed. - */ - public void testNodeRemovedThreeBackups() throws Exception { - checkNodeRemoved(3); - } - - /** - * @throws Exception If failed. - */ - public void testRandomReassignmentNoBackups() throws Exception { - checkRandomReassignment(0); - } - - /** - * @throws Exception If failed. - */ - public void testRandomReassignmentOneBackup() throws Exception { - checkRandomReassignment(1); - } - - /** - * @throws Exception If failed. - */ - public void testRandomReassignmentTwoBackups() throws Exception { - checkRandomReassignment(2); - } - - /** - * @throws Exception If failed. - */ - public void testRandomReassignmentThreeBackups() throws Exception { - checkRandomReassignment(3); - } - - /** - * @throws Exception If failed. - */ - private void checkNodeRemoved(int backups) throws Exception { - int parts = 256; - - GridCacheAffinityFunction aff = new GridCachePartitionFairAffinity(parts); - - int nodesCnt = 50; - - List<ClusterNode> nodes = new ArrayList<>(nodesCnt); - - List<List<ClusterNode>> prev = null; - - for (int i = 0; i < nodesCnt; i++) { - info("======================================"); - info("Assigning partitions: " + i); - info("======================================"); - - ClusterNode node = new GridTestNode(UUID.randomUUID()); - - nodes.add(node); - - IgniteDiscoveryEvent discoEvt = new IgniteDiscoveryEvent(node, "", IgniteEventType.EVT_NODE_JOINED, - node); - - List<List<ClusterNode>> assignment = aff.assignPartitions( - new GridCacheAffinityFunctionContextImpl(nodes, prev, discoEvt, i, backups)); - - info("Assigned."); - - verifyAssignment(assignment, backups, parts, nodes.size()); - - prev = assignment; - } - - info("======================================"); - info("Will remove nodes."); - info("======================================"); - - for (int i = 0; i < nodesCnt - 1; i++) { - info("======================================"); - info("Assigning partitions: " + i); - info("======================================"); - - ClusterNode rmv = nodes.remove(nodes.size() - 1); - - IgniteDiscoveryEvent discoEvt = new IgniteDiscoveryEvent(rmv, "", IgniteEventType.EVT_NODE_LEFT, rmv); - - List<List<ClusterNode>> assignment = aff.assignPartitions( - new GridCacheAffinityFunctionContextImpl(nodes, prev, discoEvt, i, backups)); - - info("Assigned."); - - verifyAssignment(assignment, backups, parts, nodes.size()); - - prev = assignment; - } - } - - @SuppressWarnings("IfMayBeConditional") - private void checkRandomReassignment(int backups) { - int parts = 256; - - GridCacheAffinityFunction aff = new GridCachePartitionFairAffinity(parts); - - Random rnd = new Random(); - - int maxNodes = 50; - - List<ClusterNode> nodes = new ArrayList<>(maxNodes); - - List<List<ClusterNode>> prev = null; - - int state = 0; - - int i = 0; - - while (true) { - boolean add; - - if (nodes.size() < 2) { - // Returned back to one node? - if (state == 1) - return; - - add = true; - } - else if (nodes.size() == maxNodes) { - if (state == 0) - state = 1; - - add = false; - } - else { - // Nodes size in [2, maxNodes - 1]. - if (state == 0) - add = rnd.nextInt(3) != 0; // 66% to add, 33% to remove. - else - add = rnd.nextInt(3) == 0; // 33% to add, 66% to remove. - } - - IgniteDiscoveryEvent discoEvt; - - if (add) { - ClusterNode addedNode = new GridTestNode(UUID.randomUUID()); - - nodes.add(addedNode); - - discoEvt = new IgniteDiscoveryEvent(addedNode, "", IgniteEventType.EVT_NODE_JOINED, addedNode); - } - else { - ClusterNode rmvNode = nodes.remove(rnd.nextInt(nodes.size())); - - discoEvt = new IgniteDiscoveryEvent(rmvNode, "", IgniteEventType.EVT_NODE_LEFT, rmvNode); - } - - info("======================================"); - info("Assigning partitions [iter=" + i + ", discoEvt=" + discoEvt + ", nodesSize=" + nodes.size() + ']'); - info("======================================"); - - List<List<ClusterNode>> assignment = aff.assignPartitions( - new GridCacheAffinityFunctionContextImpl(nodes, prev, discoEvt, i, backups)); - - verifyAssignment(assignment, backups, parts, nodes.size()); - - prev = assignment; - - i++; - } - } - - /** - * @param assignment Assignment to verify. - */ - private void verifyAssignment(List<List<ClusterNode>> assignment, int keyBackups, int partsCnt, int topSize) { - Map<UUID, Collection<Integer>> mapping = new HashMap<>(); - - int ideal = Math.round((float)partsCnt / topSize * Math.min(keyBackups + 1, topSize)); - - for (int part = 0; part < assignment.size(); part++) { - for (ClusterNode node : assignment.get(part)) { - assert node != null; - - Collection<Integer> parts = mapping.get(node.id()); - - if (parts == null) { - parts = new HashSet<>(); - - mapping.put(node.id(), parts); - } - - assertTrue(parts.add(part)); - } - } - - int max = -1, min = Integer.MAX_VALUE; - - for (Collection<Integer> parts : mapping.values()) { - max = Math.max(max, parts.size()); - min = Math.min(min, parts.size()); - } - - log().warning("max=" + max + ", min=" + min + ", ideal=" + ideal + ", minDev=" + deviation(min, ideal) + "%, " + - "maxDev=" + deviation(max, ideal) + "%"); - - assertTrue("max=" + max + ", min=" + min, max - min < (keyBackups + 1) * topSize); - } - - private static int deviation(int val, int ideal) { - return Math.round(Math.abs(((float)val - ideal) / ideal * 100)); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/test/java/org/gridgain/grid/kernal/GridAffinityMappedTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/gridgain/grid/kernal/GridAffinityMappedTest.java b/modules/core/src/test/java/org/gridgain/grid/kernal/GridAffinityMappedTest.java index 40ff0e1..6b97e4f 100644 --- a/modules/core/src/test/java/org/gridgain/grid/kernal/GridAffinityMappedTest.java +++ b/modules/core/src/test/java/org/gridgain/grid/kernal/GridAffinityMappedTest.java @@ -20,9 +20,8 @@ package org.gridgain.grid.kernal; import org.apache.ignite.*; import org.apache.ignite.cluster.*; import org.apache.ignite.configuration.*; -import org.gridgain.grid.*; import org.gridgain.grid.cache.*; -import org.gridgain.grid.cache.affinity.*; +import org.apache.ignite.cache.affinity.*; import org.gridgain.grid.kernal.processors.cache.distributed.*; import org.apache.ignite.spi.discovery.tcp.*; import org.apache.ignite.spi.discovery.tcp.ipfinder.*; @@ -35,7 +34,7 @@ import java.util.*; import static org.gridgain.grid.cache.GridCacheMode.*; /** - * Tests affinity mapping when {@link GridCacheAffinityKeyMapper} is used. + * Tests affinity mapping when {@link org.apache.ignite.cache.affinity.CacheAffinityKeyMapper} is used. */ public class GridAffinityMappedTest extends GridCommonAbstractTest { /** VM ip finder for TCP discovery. */ @@ -69,7 +68,7 @@ public class GridAffinityMappedTest extends GridCommonAbstractTest { cacheCfg.setAffinityMapper(new MockCacheAffinityKeyMapper()); cfg.setCacheConfiguration(cacheCfg); - cfg.setUserAttributes(F.asMap(GridCacheModuloAffinityFunction.IDX_ATTR, gridName.endsWith("2") ? 0 : 1)); + cfg.setUserAttributes(F.asMap(CacheModuloAffinityFunction.IDX_ATTR, gridName.endsWith("2") ? 0 : 1)); } return cfg; @@ -126,10 +125,10 @@ public class GridAffinityMappedTest extends GridCommonAbstractTest { } /** - * Mock affinity implementation that ensures constant key-to-node mapping based on {@link GridCacheModuloAffinityFunction} + * Mock affinity implementation that ensures constant key-to-node mapping based on {@link org.gridgain.grid.kernal.processors.cache.distributed.CacheModuloAffinityFunction} * The partition selection is as follows: 0 maps to partition 0 and any other value maps to partition 1 */ - private static class MockCacheAffinityFunction extends GridCacheModuloAffinityFunction { + private static class MockCacheAffinityFunction extends CacheModuloAffinityFunction { /** * Initializes module affinity with 2 parts and 0 backups */ @@ -151,7 +150,7 @@ public class GridAffinityMappedTest extends GridCommonAbstractTest { /** * Mock affinity mapper implementation that substitutes values other than 0 and 1 with 0. */ - private static class MockCacheAffinityKeyMapper implements GridCacheAffinityKeyMapper { + private static class MockCacheAffinityKeyMapper implements CacheAffinityKeyMapper { /** {@inheritDoc} */ @Override public Object affinityKey(Object key) { return key instanceof Integer ? 1 == (Integer)key ? key : 0 : key; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/test/java/org/gridgain/grid/kernal/GridAffinityP2PSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/gridgain/grid/kernal/GridAffinityP2PSelfTest.java b/modules/core/src/test/java/org/gridgain/grid/kernal/GridAffinityP2PSelfTest.java index a835240..ea6d13c 100644 --- a/modules/core/src/test/java/org/gridgain/grid/kernal/GridAffinityP2PSelfTest.java +++ b/modules/core/src/test/java/org/gridgain/grid/kernal/GridAffinityP2PSelfTest.java @@ -21,7 +21,7 @@ import org.apache.ignite.*; import org.apache.ignite.cluster.*; import org.apache.ignite.configuration.*; import org.gridgain.grid.cache.*; -import org.gridgain.grid.cache.affinity.*; +import org.apache.ignite.cache.affinity.*; import org.gridgain.grid.kernal.processors.cache.distributed.*; import org.apache.ignite.spi.discovery.tcp.*; import org.apache.ignite.spi.discovery.tcp.ipfinder.*; @@ -99,12 +99,12 @@ public class GridAffinityP2PSelfTest extends GridCommonAbstractTest { GridTestExternalClassLoader ldr = new GridTestExternalClassLoader(URLS); - cc.setAffinity((GridCacheAffinityFunction)ldr.loadClass(EXT_AFFINITY_CLS_NAME).newInstance()); - cc.setAffinityMapper((GridCacheAffinityKeyMapper)ldr.loadClass(EXT_AFFINITY_MAPPER_CLS_NAME) + cc.setAffinity((CacheAffinityFunction)ldr.loadClass(EXT_AFFINITY_CLS_NAME).newInstance()); + cc.setAffinityMapper((CacheAffinityKeyMapper)ldr.loadClass(EXT_AFFINITY_MAPPER_CLS_NAME) .newInstance()); c.setCacheConfiguration(cc); - c.setUserAttributes(F.asMap(GridCacheModuloAffinityFunction.IDX_ATTR, gridName.endsWith("2") ? 0 : 1)); + c.setUserAttributes(F.asMap(CacheModuloAffinityFunction.IDX_ATTR, gridName.endsWith("2") ? 0 : 1)); } return c; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/test/java/org/gridgain/grid/kernal/GridJobMasterLeaveAwareSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/gridgain/grid/kernal/GridJobMasterLeaveAwareSelfTest.java b/modules/core/src/test/java/org/gridgain/grid/kernal/GridJobMasterLeaveAwareSelfTest.java index 998d0ea..19bc6ff 100644 --- a/modules/core/src/test/java/org/gridgain/grid/kernal/GridJobMasterLeaveAwareSelfTest.java +++ b/modules/core/src/test/java/org/gridgain/grid/kernal/GridJobMasterLeaveAwareSelfTest.java @@ -28,7 +28,6 @@ import org.apache.ignite.resources.*; import org.apache.ignite.spi.*; import org.gridgain.grid.*; import org.gridgain.grid.cache.*; -import org.gridgain.grid.cache.affinity.*; import org.gridgain.grid.kernal.managers.communication.*; import org.apache.ignite.spi.communication.tcp.*; import org.apache.ignite.spi.discovery.tcp.*; @@ -444,7 +443,9 @@ public class GridJobMasterLeaveAwareSelfTest extends GridCommonAbstractTest { @Override public IgniteFuture<?> applyx(ClusterGroup prj) throws IgniteCheckedException { IgniteCompute comp = compute(prj).enableAsync(); - GridCacheAffinity<Object> aff = prj.ignite().cache(null).affinity(); + GridCache<Object, Object> cache = prj.ignite().cache(null); + + IgniteCacheAffinity<Object> aff = prj.ignite().affinity(cache.name()); ClusterNode node = F.first(prj.nodes()); @@ -463,7 +464,9 @@ public class GridJobMasterLeaveAwareSelfTest extends GridCommonAbstractTest { @Override public IgniteFuture<?> applyx(ClusterGroup prj) throws IgniteCheckedException { IgniteCompute comp = compute(prj).enableAsync(); - GridCacheAffinity<Object> aff = prj.ignite().cache(null).affinity(); + GridCache<Object, Object> cache = prj.ignite().cache(null); + + IgniteCacheAffinity<Object> aff = prj.ignite().affinity(cache.name()); ClusterNode node = F.first(prj.nodes()); @@ -479,7 +482,7 @@ public class GridJobMasterLeaveAwareSelfTest extends GridCommonAbstractTest { * @param node Node. * @return Finds some cache key for which given node is primary. */ - private Object keyForNode(GridCacheAffinity<Object> aff, ClusterNode node) { + private Object keyForNode(IgniteCacheAffinity<Object> aff, ClusterNode node) { assertNotNull(node); Object key = null; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/test/java/org/gridgain/grid/kernal/GridMultipleJobsSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/gridgain/grid/kernal/GridMultipleJobsSelfTest.java b/modules/core/src/test/java/org/gridgain/grid/kernal/GridMultipleJobsSelfTest.java index 021002b..d244984 100644 --- a/modules/core/src/test/java/org/gridgain/grid/kernal/GridMultipleJobsSelfTest.java +++ b/modules/core/src/test/java/org/gridgain/grid/kernal/GridMultipleJobsSelfTest.java @@ -20,9 +20,8 @@ package org.gridgain.grid.kernal; import org.apache.ignite.*; import org.apache.ignite.configuration.*; import org.apache.ignite.lang.*; -import org.gridgain.grid.*; import org.gridgain.grid.cache.*; -import org.gridgain.grid.cache.affinity.*; +import org.apache.ignite.cache.affinity.*; import org.apache.ignite.spi.discovery.tcp.*; import org.apache.ignite.spi.discovery.tcp.ipfinder.*; import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.*; @@ -224,7 +223,7 @@ public class GridMultipleJobsSelfTest extends GridCommonAbstractTest { /** * @return Affinity key. */ - @GridCacheAffinityKeyMapped + @CacheAffinityKeyMapped public String affinityKey() { return "key"; } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/test/java/org/gridgain/grid/kernal/processors/affinity/GridAffinityProcessorAbstractSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/affinity/GridAffinityProcessorAbstractSelfTest.java b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/affinity/GridAffinityProcessorAbstractSelfTest.java index ceb5861..cdaed13 100644 --- a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/affinity/GridAffinityProcessorAbstractSelfTest.java +++ b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/affinity/GridAffinityProcessorAbstractSelfTest.java @@ -17,10 +17,11 @@ package org.gridgain.grid.kernal.processors.affinity; +import org.apache.ignite.IgniteCacheAffinity; import org.apache.ignite.cluster.*; import org.apache.ignite.configuration.*; import org.gridgain.grid.cache.*; -import org.gridgain.grid.cache.affinity.*; +import org.apache.ignite.cache.affinity.*; import org.gridgain.grid.kernal.*; import org.apache.ignite.spi.discovery.tcp.*; import org.apache.ignite.spi.discovery.tcp.ipfinder.*; @@ -79,7 +80,7 @@ public abstract class GridAffinityProcessorAbstractSelfTest extends GridCommonAb * * @return Affinity function. */ - protected abstract GridCacheAffinityFunction affinityFunction(); + protected abstract CacheAffinityFunction affinityFunction(); /** {@inheritDoc} */ @SuppressWarnings({"ConstantConditions"}) @@ -127,6 +128,8 @@ public abstract class GridAffinityProcessorAbstractSelfTest extends GridCommonAb GridCache<Integer, Integer> cache = grid2.cache(CACHE_NAME); + IgniteCacheAffinity<Integer> affinity = grid2.affinity(CACHE_NAME); + assertNotNull(cache); GridAffinityProcessor affPrc1 = grid1.context().affinity(); @@ -144,7 +147,7 @@ public abstract class GridAffinityProcessorAbstractSelfTest extends GridCommonAb Map<ClusterNode, Collection<Integer>> node1Map = affPrc1.mapKeysToNodes(CACHE_NAME, keys); Map<ClusterNode, Collection<Integer>> node2Map = affPrc2.mapKeysToNodes(CACHE_NAME, keys); - Map<ClusterNode, Collection<Integer>> cacheMap = cache.affinity().mapKeysToNodes(keys); + Map<ClusterNode, Collection<Integer>> cacheMap = affinity.mapKeysToNodes(keys); assertEquals(cacheMap.size(), node1Map.size()); assertEquals(cacheMap.size(), node2Map.size()); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/test/java/org/gridgain/grid/kernal/processors/affinity/GridAffinityProcessorConsistentHashSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/affinity/GridAffinityProcessorConsistentHashSelfTest.java b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/affinity/GridAffinityProcessorConsistentHashSelfTest.java index 80d30e8..56b28e7 100644 --- a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/affinity/GridAffinityProcessorConsistentHashSelfTest.java +++ b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/affinity/GridAffinityProcessorConsistentHashSelfTest.java @@ -17,15 +17,15 @@ package org.gridgain.grid.kernal.processors.affinity; -import org.gridgain.grid.cache.affinity.*; -import org.gridgain.grid.cache.affinity.consistenthash.*; +import org.apache.ignite.cache.affinity.*; +import org.apache.ignite.cache.affinity.consistenthash.*; /** * Tests consistent hash affinity function. */ public class GridAffinityProcessorConsistentHashSelfTest extends GridAffinityProcessorAbstractSelfTest { /** {@inheritDoc} */ - @Override protected GridCacheAffinityFunction affinityFunction() { - return new GridCacheConsistentHashAffinityFunction(); + @Override protected CacheAffinityFunction affinityFunction() { + return new CacheConsistentHashAffinityFunction(); } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/test/java/org/gridgain/grid/kernal/processors/affinity/GridAffinityProcessorRendezvousSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/affinity/GridAffinityProcessorRendezvousSelfTest.java b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/affinity/GridAffinityProcessorRendezvousSelfTest.java index c3bb84e..a6c894a 100644 --- a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/affinity/GridAffinityProcessorRendezvousSelfTest.java +++ b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/affinity/GridAffinityProcessorRendezvousSelfTest.java @@ -17,15 +17,15 @@ package org.gridgain.grid.kernal.processors.affinity; -import org.gridgain.grid.cache.affinity.*; -import org.gridgain.grid.cache.affinity.rendezvous.*; +import org.apache.ignite.cache.affinity.*; +import org.apache.ignite.cache.affinity.rendezvous.*; /** * Tests affinity processor with rendezvous affinity function. */ public class GridAffinityProcessorRendezvousSelfTest extends GridAffinityProcessorAbstractSelfTest { /** {@inheritDoc} */ - @Override protected GridCacheAffinityFunction affinityFunction() { - return new GridCacheRendezvousAffinityFunction(); + @Override protected CacheAffinityFunction affinityFunction() { + return new CacheRendezvousAffinityFunction(); } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractFullApiSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractFullApiSelfTest.java b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractFullApiSelfTest.java index a44acd1..53993c5 100644 --- a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractFullApiSelfTest.java +++ b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractFullApiSelfTest.java @@ -1058,7 +1058,7 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract assertEquals(exp, cache.get("key")); for (int i = 0; i < gridCount(); i++) { - if (cache(i).affinity().isPrimaryOrBackup(grid(i).localNode(), "key")) + if (affinity(i).isPrimaryOrBackup(grid(i).localNode(), "key")) assertEquals(exp, peek(cache(i), "key")); } } @@ -2322,7 +2322,7 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract GridCacheEntryEx<String, Integer> entry = cctx.isNear() ? cctx.near().dht().peekEx(key) : cctx.cache().peekEx(key); - if (cache().affinity().mapKeyToPrimaryAndBackups(key).contains(grid(g).localNode())) { + if (affinity(0).mapKeyToPrimaryAndBackups(key).contains(grid(g).localNode())) { assertNotNull(entry); assertTrue(entry.deleted()); } @@ -2356,7 +2356,7 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract for (int i = 0; i < cnt; i++) { String key = String.valueOf(i); - if (cache().affinity().mapKeyToPrimaryAndBackups(key).contains(grid(g).localNode())) + if (affinity(0).mapKeyToPrimaryAndBackups(key).contains(grid(g).localNode())) assertEquals((Integer)i, cache(g).peek(key)); else assertNull(cache(g).peek(key)); @@ -3018,7 +3018,7 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract List<Integer> keys = new ArrayList<>(); for (int k = 0; k < 2; k++) { - while (!grid.cache(null).affinity().isPrimary(grid.localNode(), key)) + while (!grid.affinity(grid.cache(null).name()).isPrimary(grid.localNode(), key)) key++; keys.add(key); @@ -3065,7 +3065,7 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract int key = 0; for (int k = 0; k < 2; k++) { - while (!grid.cache(null).affinity().isPrimary(grid.localNode(), key)) + while (!grid.affinity(grid.cache(null).name()).isPrimary(grid.localNode(), key)) key++; keys.add(key); @@ -3519,7 +3519,7 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract public void testLockAsyncWithTimeoutEntry() throws Exception { if (lockingEnabled()) { // Put only to primary. - ClusterNode node = F.first(cache().affinity().mapKeyToPrimaryAndBackups("key")); + ClusterNode node = F.first(affinity(0).mapKeyToPrimaryAndBackups("key")); if (node == null) throw new IgniteCheckedException("Failed to map key."); @@ -5192,7 +5192,7 @@ public abstract class GridCacheAbstractFullApiSelfTest extends GridCacheAbstract * @throws Exception If failed. */ protected GridCache<String, Integer> primaryCache(String key) throws Exception { - ClusterNode node = cache().affinity().mapKeyToNode(key); + ClusterNode node = affinity(0).mapKeyToNode(key); if (node == null) throw new IgniteCheckedException("Failed to find primary node."); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java index 54a2b2d..f07904f 100644 --- a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java +++ b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractMetricsSelfTest.java @@ -91,7 +91,7 @@ public abstract class GridCacheAbstractMetricsSelfTest extends GridCacheAbstract for (int i = 0; i < keyCnt; i++) { cache0.put(i, i); // +1 read - boolean isPrimary = cache0.affinity().isPrimary(grid(0).localNode(), i); + boolean isPrimary = grid(0).affinity(cache0.name()).isPrimary(grid(0).localNode(), i); expReads += expectedReadsPerPut(isPrimary); expMisses += expectedMissesPerPut(isPrimary); @@ -152,7 +152,7 @@ public abstract class GridCacheAbstractMetricsSelfTest extends GridCacheAbstract for (int i = 0; i < keyCnt; i++) { assertNull("Value is not null for key: " + i, cache.get(i)); - if (cache.affinity().isPrimary(grid(0).localNode(), i)) + if (grid(0).affinity(cache.name()).isPrimary(grid(0).localNode(), i)) expReads++; else expReads += 2; @@ -192,7 +192,7 @@ public abstract class GridCacheAbstractMetricsSelfTest extends GridCacheAbstract Integer key = null; for (int i = 0; i < 1000; i++) { - if (cache.affinity().isPrimary(grid(0).localNode(), i)) { + if (grid(0).affinity(cache.name()).isPrimary(grid(0).localNode(), i)) { key = i; break; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractRemoveFailureTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractRemoveFailureTest.java b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractRemoveFailureTest.java index bfde6a5..9c3a9ea 100644 --- a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractRemoveFailureTest.java +++ b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractRemoveFailureTest.java @@ -296,8 +296,8 @@ public abstract class GridCacheAbstractRemoveFailureTest extends GridCacheAbstra if (!F.eq(expVal.getValue().get(), val)) { failedKeys.add(expVal.getKey()); - boolean primary = cache.affinity().isPrimary(ignite.cluster().localNode(), expVal.getKey()); - boolean backup = cache.affinity().isBackup(ignite.cluster().localNode(), expVal.getKey()); + boolean primary = ignite.affinity(cache.name()).isPrimary(ignite.cluster().localNode(), expVal.getKey()); + boolean backup = ignite.affinity(cache.name()).isBackup(ignite.cluster().localNode(), expVal.getKey()); log.error("Unexpected cache data [exp=" + expVal + ", actual=" + val + ", nodePrimary=" + primary + ", nodeBackup=" + backup + ", nodeId=" + ignite.cluster().localNode().id() + ']'); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/25609651/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractSelfTest.java b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractSelfTest.java index d4008f7..a057fd3 100644 --- a/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractSelfTest.java +++ b/modules/core/src/test/java/org/gridgain/grid/kernal/processors/cache/GridCacheAbstractSelfTest.java @@ -27,6 +27,7 @@ import org.apache.ignite.spi.discovery.tcp.ipfinder.*; import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.*; import org.apache.ignite.transactions.*; import org.gridgain.grid.cache.*; +import org.apache.ignite.IgniteCacheAffinity; import org.gridgain.grid.cache.store.*; import org.gridgain.grid.kernal.*; import org.gridgain.grid.util.lang.*; @@ -340,6 +341,15 @@ public abstract class GridCacheAbstractSelfTest extends GridCommonAbstractTest { /** * @param idx Index of grid. + * @return Affinity. + */ + @SuppressWarnings({"unchecked"}) + @Override protected IgniteCacheAffinity<String> affinity(int idx) { + return grid(idx).affinity(null); + } + + /** + * @param idx Index of grid. * @return Cache instance casted to work with string and integer types for convenience. */ @SuppressWarnings({"unchecked"}) @@ -386,7 +396,7 @@ public abstract class GridCacheAbstractSelfTest extends GridCommonAbstractTest { * @return {@code True} if key belongs to node with index idx. */ protected boolean belongs(String key, int idx) { - return context(idx).cache().affinity().isPrimaryOrBackup(context(idx).localNode(), key); + return context(idx).grid().affinity(context(idx).cache().name()).isPrimaryOrBackup(context(idx).localNode(), key); } /**