BasicNodeSelectionPolicy implements INodeSelectionPolicy { public Map<String, List<String>> createNode2ShardsMap(Collection<String> shards) throws ShardAccessException { One2ManyListMap<String, String> node2ShardsMap = new One2ManyListMap<String, String>(); for (String shard : shards) { CircularList<String> nodeList = _shardsToNodeMap.get(shard); if (nodeList == null) { continue; } if(nodeList.isEmpty()) { throw new ShardAccessException(shard); } String node; synchronized (nodeList) { node = nodeList.getNext(); } node2ShardsMap.add(node, shard); } return node2ShardsMap.asMap(); }  void update(String shard, Collection<String> nodes); @Override Collection<String> getShardNodes(String shard); @Override List<String> remove(String shard); void removeNode(String node); Map<String, List<String>> createNode2ShardsMap(Collection<String> shards); @Override String toString();  }
@Test public void testIndexSpawnsMultipleNodes() throws Exception { final BasicNodeSelectionPolicy policy = new BasicNodeSelectionPolicy(); final Map<String, List<String>> indexToShards = new HashMap<String, List<String>>(); addIndex(indexToShards, "indexB", "shardB1", "shardB2"); addNodes(policy, "shardB1", "node1"); addNodes(policy, "shardB2", "node2"); Map<String, List<String>> nodeShardsMap = policy.createNode2ShardsMap(indexToShards.get("indexB")); assertEquals(2, nodeShardsMap.size()); assertEquals(2, extractFoundShards(nodeShardsMap).size()); assertTrue(nodeShardsMap.get("node1").contains("shardB1")); assertTrue(nodeShardsMap.get("node2").contains("shardB2")); }
@Test public void testQueryMultipleIndexes() throws Exception { final BasicNodeSelectionPolicy policy = new BasicNodeSelectionPolicy(); final Map<String, List<String>> indexToShards = new HashMap<String, List<String>>(); addIndex(indexToShards, "indexA", "shardA1"); addIndex(indexToShards, "indexB", "shardB1", "shardB2"); addNodes(policy, "shardA1", "node1", "node2"); addNodes(policy, "shardB1", "node1"); addNodes(policy, "shardB2", "node2"); List<String> shards = new ArrayList<String>(); shards.addAll(indexToShards.get("indexA")); shards.addAll(indexToShards.get("indexB")); Map<String, List<String>> nodeShardsMap = policy.createNode2ShardsMap(shards); assertEquals(2, nodeShardsMap.size()); assertEquals(3, extractFoundShards(nodeShardsMap).size()); }
@Test public void testSelection() throws Exception { final BasicNodeSelectionPolicy policy = new BasicNodeSelectionPolicy(); final Map<String, List<String>> indexToShards = new HashMap<String, List<String>>(); addIndex(indexToShards, "indexA", "shardA", "shardB"); addNodes(policy, "shardA", "node1", "node2"); addNodes(policy, "shardB", "node1", "node2"); Map<String, List<String>> nodeShardsMap1 = policy.createNode2ShardsMap(indexToShards.get("indexA")); Map<String, List<String>> nodeShardsMap2 = policy.createNode2ShardsMap(indexToShards.get("indexA")); assertEquals(1, nodeShardsMap1.size()); assertEquals(1, nodeShardsMap2.size()); assertFalse("nodes should differ", nodeShardsMap1.keySet().equals(nodeShardsMap2.keySet())); }
@Test public void testSetShardsAndNodes() throws Exception { final BasicNodeSelectionPolicy policy = new BasicNodeSelectionPolicy(); final Map<String, List<String>> indexToShards = new HashMap<String, List<String>>(); addIndex(indexToShards, "indexA", "shardA", "shardB"); addNodes(policy, "shardA", "node1", "node2"); addNodes(policy, "shardB", "node1", "node2"); Map<String, List<String>> nodeShardsMap = policy.createNode2ShardsMap(indexToShards.get("indexA")); assertEquals(1, nodeShardsMap.size()); List<String> shardList = nodeShardsMap.values().iterator().next(); assertEquals(2, shardList.size()); assertTrue(shardList.contains("shardA")); assertTrue(shardList.contains("shardB")); }
@Test public void testManyShards() throws Exception { final BasicNodeSelectionPolicy policy = new BasicNodeSelectionPolicy(); final Map<String, List<String>> indexToShards = new HashMap<String, List<String>>(); addIndex(indexToShards, "indexA", "shardA1", "shardA2", "shardA3"); addIndex(indexToShards, "indexB", "shardB1", "shardB2", "shardB3"); addIndex(indexToShards, "indexC", "shardC1", "shardC2", "shardC3", "shardC4"); addNodes(policy, "shardA1", "node1", "node2", "node3"); addNodes(policy, "shardA2", "node1", "node2", "node4"); addNodes(policy, "shardA3", "node1", "node2", "node5"); addNodes(policy, "shardB1", "node3", "node4", "node5"); addNodes(policy, "shardB2", "node2", "node4", "node5"); addNodes(policy, "shardB3", "node1", "node2"); addNodes(policy, "shardC1", "node2", "node3", "node5"); addNodes(policy, "shardC2", "node1", "node2", "node4"); addNodes(policy, "shardC3", "node2", "node4", "node5"); addNodes(policy, "shardC4", "node1", "node3", "node4"); List<String> shards = new ArrayList<String>(); shards.addAll(indexToShards.get("indexA")); shards.addAll(indexToShards.get("indexB")); shards.addAll(indexToShards.get("indexC")); Map<String, List<String>> nodeShardsMap = policy.createNode2ShardsMap(shards); assertEquals(10, extractFoundShards(nodeShardsMap).size()); }