diff --git a/src/main/java/org/alfresco/util/shard/ExplicitShardingPolicy.java b/src/main/java/org/alfresco/util/shard/ExplicitShardingPolicy.java index 979d25471b..f5c421fef2 100644 --- a/src/main/java/org/alfresco/util/shard/ExplicitShardingPolicy.java +++ b/src/main/java/org/alfresco/util/shard/ExplicitShardingPolicy.java @@ -21,6 +21,8 @@ package org.alfresco.util.shard; import java.util.LinkedList; import java.util.List; +import org.alfresco.error.AlfrescoRuntimeException; + /** * Common ACL based index sharding behaviour for SOLR and the repository * @@ -67,6 +69,13 @@ public class ExplicitShardingPolicy { if (test % numNodes == nodeInstance - 1) { + // This algorithm fails for some sets of parameters. (See SEARCH-1785) + if (shardIds.contains(shard % numShards)) + { + throw new AlfrescoRuntimeException("Sharding configuration not supported - unable to create shard list for node " + nodeInstance + + " (shards:" + numShards + ", replication:" + replicationFactor + ", nodes:" + numNodes + ")." + + " Please set up the shards manually or use a different sharding configuration."); + } shardIds.add(shard % numShards); } test++; diff --git a/src/test/java/org/alfresco/util/shard/ExplicitShardingPolicyTest.java b/src/test/java/org/alfresco/util/shard/ExplicitShardingPolicyTest.java index 937c647225..ce188fb229 100644 --- a/src/test/java/org/alfresco/util/shard/ExplicitShardingPolicyTest.java +++ b/src/test/java/org/alfresco/util/shard/ExplicitShardingPolicyTest.java @@ -20,8 +20,11 @@ package org.alfresco.util.shard; import static org.junit.Assert.*; +import java.util.HashSet; import java.util.List; +import java.util.Set; +import org.alfresco.error.AlfrescoRuntimeException; import org.junit.Test; /** @@ -116,6 +119,20 @@ public class ExplicitShardingPolicyTest { buildAndTest(10, 2, 4); } + + /** ExplicitShardingPolicy algorithm fails for 2 shards, 3 replicas, 3 nodes. (See SEARCH-1785) */ + @Test(expected = AlfrescoRuntimeException.class) + public void search1785_233() + { + buildAndTest(2, 3, 3); + } + + /** ExplicitShardingPolicy algorithm fails for 4 shards, 3 replicas, 6 nodes. (See SEARCH-1785) */ + @Test (expected = AlfrescoRuntimeException.class) + public void search1785_436() + { + buildAndTest(4, 3, 6); + } @Test public void check_10_2() @@ -163,7 +180,8 @@ public class ExplicitShardingPolicyTest int[] found = new int[numShards]; for (int i = 0; i < numNodes; i++) { - List shardIds = policy.getShardIdsForNode(i + 1); + // Convert to a set to remove any duplicates. + Set shardIds = new HashSet<>(policy.getShardIdsForNode(i + 1)); assertEquals(numShards * replicationFactor / numNodes, shardIds.size()); for (Integer shardId : shardIds) {