From ca530750d72a840be3885f6652c54d24d9e6f1ef Mon Sep 17 00:00:00 2001 From: ivasilinets Date: Tue, 27 Jan 2015 17:16:15 +0300 Subject: [PATCH] #IGNITE-99: Add different cache affinity functions to tests. --- .../affinity/GridAffinityAssignment.java | 4 +- .../affinity/GridAffinityProcessor.java | 62 ++++++------ .../ignite/internal/util/GridUtils.java | 9 ++ ...Test.java => IgniteCacheAffinityTest.java} | 98 ++++++++++++------- ...artitionedNearPartitionedAffinityTest.java | 18 ---- ...gniteCachePartitionedOnlyAffinityTest.java | 18 ---- ...ReplicatedPartitionedOnlyAffinityTest.java | 18 ---- 7 files changed, 105 insertions(+), 122 deletions(-) rename modules/core/src/test/java/org/apache/ignite/{IgniteCacheAffinityAbstractTest.java => IgniteCacheAffinityTest.java} (65%) delete mode 100644 modules/core/src/test/java/org/apache/ignite/IgniteCachePartitionedNearPartitionedAffinityTest.java delete mode 100644 modules/core/src/test/java/org/apache/ignite/IgniteCachePartitionedOnlyAffinityTest.java delete mode 100644 modules/core/src/test/java/org/apache/ignite/IgniteCacheReplicatedPartitionedOnlyAffinityTest.java diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityAssignment.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityAssignment.java index 1890fa4768212..673db6d3323e6 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityAssignment.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityAssignment.java @@ -103,7 +103,7 @@ public List get(int part) { public Set primaryPartitions(UUID nodeId) { Set set = primary.get(nodeId); - return set == null ? Collections.emptySet() : Collections.unmodifiableSet(set); + return set == null ? Collections.emptySet() : set; } /** @@ -115,7 +115,7 @@ public Set primaryPartitions(UUID nodeId) { public Set backupPartitions(UUID nodeId) { Set set = backup.get(nodeId); - return set == null ? Collections.emptySet() : Collections.unmodifiableSet(set); + return set == null ? Collections.emptySet() : set; } /** diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityProcessor.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityProcessor.java index f36faa23bfd3b..bad1f59f68c2c 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityProcessor.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/affinity/GridAffinityProcessor.java @@ -77,7 +77,7 @@ public class GridAffinityProcessor extends GridProcessorAdapter { // Clean up affinity functions if such cache no more exists. if (evtType == EVT_NODE_FAILED || evtType == EVT_NODE_LEFT) { - Collection caches = new HashSet<>(); + final Collection caches = new HashSet<>(); for (ClusterNode clusterNode : ((IgniteDiscoveryEvent)evt).topologyNodes()) caches.addAll(U.cacheNames(clusterNode)); @@ -495,6 +495,20 @@ private AffinityInfo(CacheAffinityFunction affFunc, CacheAffinityKeyMapper mappe this.portableEnabled = portableEnabled; } + /** + * @return Cache affinity function. + */ + private CacheAffinityFunction affFunc() { + return affFunc; + } + + /** + * @return Affinity assignment. + */ + private GridAffinityAssignment assignment() { + return assignment; + } + /** {@inheritDoc} */ @Override public String toString() { return S.toString(AffinityInfo.class, this); @@ -567,7 +581,7 @@ public CacheAffinityProxy(String cacheName) { ctx.gateway().readLock(); try { - return cache().affFunc.partitions(); + return cache().affFunc().partitions(); } catch (IgniteCheckedException e) { throw new IgniteException(e); @@ -582,7 +596,7 @@ public CacheAffinityProxy(String cacheName) { ctx.gateway().readLock(); try { - return cache().affFunc.partition(key); + return cache().affFunc().partition(key); } catch (IgniteCheckedException e) { throw new IgniteException(e); @@ -597,8 +611,7 @@ public CacheAffinityProxy(String cacheName) { ctx.gateway().readLock(); try { - return cache() - .assignment.primaryPartitions(n.id()).contains(partition(key)); + return cache().assignment().primaryPartitions(n.id()).contains(partition(key)); } catch (IgniteCheckedException e) { throw new IgniteException(e); @@ -608,17 +621,12 @@ public CacheAffinityProxy(String cacheName) { } } - private AffinityInfo cache() throws IgniteCheckedException { - return affinityCache(cacheName, topologyVersion()); - } - /** {@inheritDoc} */ @Override public boolean isBackup(ClusterNode n, K key) { ctx.gateway().readLock(); try { - return cache() - .assignment.backupPartitions(n.id()).contains(partition(key)); + return cache().assignment().backupPartitions(n.id()).contains(partition(key)); } catch (IgniteCheckedException e) { throw new IgniteException(e); @@ -645,7 +653,7 @@ private AffinityInfo cache() throws IgniteCheckedException { ctx.gateway().readLock(); try { - Set parts = cache().assignment.primaryPartitions(n.id()); + Set parts = cache().assignment().primaryPartitions(n.id()); return U.toIntArray(parts); } @@ -662,7 +670,7 @@ private AffinityInfo cache() throws IgniteCheckedException { ctx.gateway().readLock(); try { - Set parts = cache().assignment.backupPartitions(n.id()); + Set parts = cache().assignment().backupPartitions(n.id()); return U.toIntArray(parts); } @@ -679,21 +687,12 @@ private AffinityInfo cache() throws IgniteCheckedException { ctx.gateway().readLock(); try { - Collection parts = new HashSet<>(); - - AffinityInfo affInfo = cache(); - - for (int partsCnt = affInfo.affFunc.partitions(), part = 0; part < partsCnt; part++) { - for (ClusterNode affNode : affInfo.assignment.get(part)) { - if (n.id().equals(affNode.id())) { - parts.add(part); + GridAffinityAssignment assignment = cache().assignment(); - break; - } - } - } + int[] primary = U.toIntArray(assignment.primaryPartitions(n.id())); + int[] backup = U.toIntArray(assignment.backupPartitions(n.id())); - return U.toIntArray(parts); + return U.addAll(primary, backup); } catch (IgniteCheckedException e) { throw new IgniteException(e); @@ -769,7 +768,7 @@ private AffinityInfo cache() throws IgniteCheckedException { ctx.gateway().readLock(); try { - return F.first(cache().assignment.get(part)); + return F.first(cache().assignment().get(part)); } catch (IgniteCheckedException e) { throw new IgniteException(e); @@ -803,7 +802,7 @@ private AffinityInfo cache() throws IgniteCheckedException { ctx.gateway().readLock(); try { - return cache().assignment.get(part); + return cache().assignment().get(part); } catch (IgniteCheckedException e) { throw new IgniteException(e); @@ -813,6 +812,13 @@ private AffinityInfo cache() throws IgniteCheckedException { } } + /** + * @return Affinity info for current topology version. + */ + private AffinityInfo cache() throws IgniteCheckedException { + return affinityCache(cacheName, topologyVersion()); + } + /** * @return Topology version. */ diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/GridUtils.java b/modules/core/src/main/java/org/apache/ignite/internal/util/GridUtils.java index 5279b59070175..6a6b82cc1b74f 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/GridUtils.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/GridUtils.java @@ -6245,6 +6245,15 @@ public static int[] toIntArray(@Nullable Collection c) { return arr; } + public static int[] addAll(int[] arr1, int[] arr2) { + int[] all = new int[arr1.length + arr2.length]; + + System.arraycopy(arr1, 0, all, 0, arr1.length); + System.arraycopy(arr2, 0, all, arr1.length, arr2.length); + + return all; + } + /** * Converts array of integers into list. * diff --git a/modules/core/src/test/java/org/apache/ignite/IgniteCacheAffinityAbstractTest.java b/modules/core/src/test/java/org/apache/ignite/IgniteCacheAffinityTest.java similarity index 65% rename from modules/core/src/test/java/org/apache/ignite/IgniteCacheAffinityAbstractTest.java rename to modules/core/src/test/java/org/apache/ignite/IgniteCacheAffinityTest.java index 562ce0b6b58c9..96e0f2a661159 100644 --- a/modules/core/src/test/java/org/apache/ignite/IgniteCacheAffinityAbstractTest.java +++ b/modules/core/src/test/java/org/apache/ignite/IgniteCacheAffinityTest.java @@ -2,6 +2,9 @@ import org.apache.ignite.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.apache.ignite.cluster.*; import org.apache.ignite.configuration.*; import org.apache.ignite.internal.processors.cache.*; @@ -12,12 +15,18 @@ /** * Tests for {@link org.apache.ignite.internal.processors.affinity.GridAffinityProcessor.CacheAffinityProxy}. */ -public abstract class IgniteCacheAffinityAbstractTest extends IgniteCacheAbstractTest { +public class IgniteCacheAffinityTest extends IgniteCacheAbstractTest { /** Initial grid count. */ private int GRID_COUNT = 3; /** Cache name */ - private final String CACHE1 = "cache1"; + private final String CACHE1 = "ConsistentCache"; + + /** Cache name */ + private final String CACHE2 = "PartitionFairCache"; + + /** Cache name */ + private final String CACHE3 = "RendezvousCache"; /** {@inheritDoc} */ @Override protected int gridCount() { @@ -32,22 +41,39 @@ public abstract class IgniteCacheAffinityAbstractTest extends IgniteCacheAbstrac CacheConfiguration cache1 = cacheConfiguration(null); cache1.setName(CACHE1); + cache1.setAffinity(new CacheConsistentHashAffinityFunction()); + + CacheConfiguration cache2 = cacheConfiguration(null); + cache2.setName(CACHE2); + cache2.setAffinity(new CachePartitionFairAffinity()); + + CacheConfiguration cache3 = cacheConfiguration(null); + cache3.setName(CACHE3); + cache3.setAffinity(new CacheRendezvousAffinityFunction()); if (gridName.contains("0")) - cfg.setCacheConfiguration(); - else if (gridName.contains("1")) cfg.setCacheConfiguration(cache0); else - cfg.setCacheConfiguration(cache0, cache1); + cfg.setCacheConfiguration(cache0, cache1, cache2, cache3); return cfg; } + /** {@inheritDoc} */ + @Override protected CacheMode cacheMode() { + return CacheMode.PARTITIONED; + } + /** {@inheritDoc} */ @Override protected CacheAtomicityMode atomicityMode() { return CacheAtomicityMode.TRANSACTIONAL; } + /** {@inheritDoc} */ + @Override protected CacheDistributionMode distributionMode() { + return CacheDistributionMode.NEAR_PARTITIONED; + } + /** * Throws Exception if failed. */ @@ -59,7 +85,9 @@ public void testAffinity() throws Exception { } GridCache cache0 = grid(1).cache(null); - GridCache cache1 = grid(2).cache(CACHE1); + GridCache cache1 = grid(1).cache(CACHE1); + GridCache cache2 = grid(1).cache(CACHE2); + GridCache cache3 = grid(1).cache(CACHE3); for (int i = 0; i < 10; ++i) cache0.put(Integer.toString(i), i); @@ -67,12 +95,20 @@ public void testAffinity() throws Exception { for (int i = 10; i < 20; ++i) cache1.put(Integer.toString(i), i); + for (int i = 20; i < 30; ++i) + cache2.put(Integer.toString(i), i); + + for (int i = 30; i < 40; ++i) + cache3.put(Integer.toString(i), i); + checkAffinity(); + stopGrid(gridCount() - 1); + + startGrid(gridCount() - 1); startGrid(gridCount()); - startGrid(gridCount() + 1); - GRID_COUNT += 2; + GRID_COUNT += 1; checkAffinity(); } @@ -81,22 +117,13 @@ public void testAffinity() throws Exception { * Check CacheAffinityProxy methods. */ private void checkAffinity() { - for (int i = 0; i < gridCount(); ++i) { - if (grid(i).cachex(null) != null) - checkAffinity(grid(i).jcache(null), grid(i).cache(null)); + checkGridAffinity(grid(0).affinity(null), grid(1).jcache(null), grid(1).cache(null)); - if (grid(i).cachex(CACHE1) != null) - checkAffinity(grid(i).jcache(CACHE1), grid(i).cache(CACHE1)); - } - } + checkGridAffinity(grid(0).affinity(CACHE1), grid(1).jcache(CACHE1), grid(1).cache(CACHE1)); - /** - * @param jcache Jcache to iterate over. - * @param cache Cache to check. - */ - private void checkAffinity(IgniteCache jcache, GridCache cache) { - for (int i = 0; i < gridCount(); ++i) - checkGridAffinity(grid(i).affinity(cache.name()), jcache, cache); + checkGridAffinity(grid(0).affinity(CACHE2), grid(1).jcache(CACHE2), grid(1).cache(CACHE2)); + + checkGridAffinity(grid(0).affinity(CACHE3), grid(1).jcache(CACHE3), grid(1).cache(CACHE3)); } /** @@ -104,8 +131,7 @@ private void checkAffinity(IgniteCache jcache, GridCache testAff, IgniteCache jcache, - GridCache cache) { + private void checkGridAffinity(CacheAffinity testAff, IgniteCache jcache, GridCache cache) { checkAffinityKey(testAff, jcache, cache.affinity()); checkPartitions(testAff, cache.affinity()); @@ -118,12 +144,11 @@ private void checkGridAffinity(CacheAffinity testAff, IgniteCache testAff, - IgniteCache jcache, CacheAffinity aff) { - Iterator> iter = jcache.iterator(); + private void checkMapKeyToNode(CacheAffinity testAff, IgniteCache jcache, CacheAffinity aff) { + Iterator iter = jcache.iterator(); while (iter.hasNext()) { - Cache.Entry entry = iter.next(); + Cache.Entry entry = iter.next(); UUID node1 = testAff.mapKeyToNode(entry.getKey()).id(); UUID node2 = aff.mapKeyToNode(entry.getKey()).id(); @@ -144,12 +169,11 @@ private void checkMapKeyToNode(CacheAffinity testAff, /** * Check affinityKey method. */ - private void checkAffinityKey(CacheAffinity testAff, - IgniteCache jcache, CacheAffinity aff) { - Iterator> iter = jcache.iterator(); + private void checkAffinityKey(CacheAffinity testAff, IgniteCache jcache, CacheAffinity aff) { + Iterator iter = jcache.iterator(); while (iter.hasNext()) { - Cache.Entry entry = iter.next(); + Cache.Entry entry = iter.next(); assertEquals(testAff.affinityKey(entry.getKey()), (aff.affinityKey(entry.getKey()))); } @@ -158,13 +182,12 @@ private void checkAffinityKey(CacheAffinity testAff, /** * Check isBackup, isPrimary and isPrimaryOrBackup methods. */ - private void checkIsBackupOrPrimary(CacheAffinity testAff, IgniteCache jcache, - CacheAffinity aff) { + private void checkIsBackupOrPrimary(CacheAffinity testAff, IgniteCache jcache, CacheAffinity aff) { - Iterator> iter = jcache.iterator(); + Iterator iter = jcache.iterator(); while (iter.hasNext()) { - Cache.Entry entry = iter.next(); + Cache.Entry entry = iter.next(); for (ClusterNode n : nodes()) { assertEquals(testAff.isBackup(n, entry.getKey()), aff.isBackup(n, entry.getKey())); @@ -179,7 +202,7 @@ private void checkIsBackupOrPrimary(CacheAffinity testAff, IgniteCache testAff, CacheAffinity aff) { + private void checkPartitions(CacheAffinity testAff, CacheAffinity aff) { for (ClusterNode n : nodes()) { checkEqualIntArray(testAff.allPartitions(n), aff.allPartitions(n)); @@ -207,7 +230,6 @@ private void checkEqualIntArray(int[] arr1, int[] arr2) { } assertEquals(0, col1.size()); - } /** diff --git a/modules/core/src/test/java/org/apache/ignite/IgniteCachePartitionedNearPartitionedAffinityTest.java b/modules/core/src/test/java/org/apache/ignite/IgniteCachePartitionedNearPartitionedAffinityTest.java deleted file mode 100644 index 310eb04996869..0000000000000 --- a/modules/core/src/test/java/org/apache/ignite/IgniteCachePartitionedNearPartitionedAffinityTest.java +++ /dev/null @@ -1,18 +0,0 @@ -package org.apache.ignite; - -import org.apache.ignite.cache.*; - -/** - * Tests for {@link org.apache.ignite.internal.processors.affinity.GridAffinityProcessor.CacheAffinityProxy}. - */ -public class IgniteCachePartitionedNearPartitionedAffinityTest extends IgniteCacheAffinityAbstractTest { - /** {@inheritDoc} */ - @Override protected CacheMode cacheMode() { - return CacheMode.PARTITIONED; - } - - /** {@inheritDoc} */ - @Override protected CacheDistributionMode distributionMode() { - return CacheDistributionMode.NEAR_PARTITIONED; - } -} diff --git a/modules/core/src/test/java/org/apache/ignite/IgniteCachePartitionedOnlyAffinityTest.java b/modules/core/src/test/java/org/apache/ignite/IgniteCachePartitionedOnlyAffinityTest.java deleted file mode 100644 index b9fac92d3ab5f..0000000000000 --- a/modules/core/src/test/java/org/apache/ignite/IgniteCachePartitionedOnlyAffinityTest.java +++ /dev/null @@ -1,18 +0,0 @@ -package org.apache.ignite; - -import org.apache.ignite.cache.*; - -/** - * Tests for {@link org.apache.ignite.internal.processors.affinity.GridAffinityProcessor.CacheAffinityProxy}. - */ -public class IgniteCachePartitionedOnlyAffinityTest extends IgniteCacheAffinityAbstractTest { - /** {@inheritDoc} */ - @Override protected CacheMode cacheMode() { - return CacheMode.PARTITIONED; - } - - /** {@inheritDoc} */ - @Override protected CacheDistributionMode distributionMode() { - return CacheDistributionMode.PARTITIONED_ONLY; - } -} diff --git a/modules/core/src/test/java/org/apache/ignite/IgniteCacheReplicatedPartitionedOnlyAffinityTest.java b/modules/core/src/test/java/org/apache/ignite/IgniteCacheReplicatedPartitionedOnlyAffinityTest.java deleted file mode 100644 index efdb8bcc2e64b..0000000000000 --- a/modules/core/src/test/java/org/apache/ignite/IgniteCacheReplicatedPartitionedOnlyAffinityTest.java +++ /dev/null @@ -1,18 +0,0 @@ -package org.apache.ignite; - -import org.apache.ignite.cache.*; - -/** - * Tests for {@link org.apache.ignite.internal.processors.affinity.GridAffinityProcessor.CacheAffinityProxy}. - */ -public class IgniteCacheReplicatedPartitionedOnlyAffinityTest extends IgniteCacheAffinityAbstractTest{ - /** {@inheritDoc} */ - @Override protected CacheMode cacheMode() { - return CacheMode.REPLICATED; - } - - /** {@inheritDoc} */ - @Override protected CacheDistributionMode distributionMode() { - return CacheDistributionMode.PARTITIONED_ONLY; - } -}