NetworkTopologyWithNodeGroup extends NetworkTopology { @Override public void add(Node node) { if (node==null) return; if( node instanceof InnerNode ) { throw new IllegalArgumentException( "Not allow to add an inner node: "+NodeBase.getPath(node)); } netlock.writeLock().lock(); try { Node rack = null; if (NetworkTopology.DEFAULT_RACK.equals(node.getNetworkLocation())) { node.setNetworkLocation(node.getNetworkLocation() + NetworkTopologyWithNodeGroup.DEFAULT_NODEGROUP); } Node nodeGroup = getNode(node.getNetworkLocation()); if (nodeGroup == null) { nodeGroup = new InnerNodeWithNodeGroup(node.getNetworkLocation()); } rack = getNode(nodeGroup.getNetworkLocation()); if (rack != null && (!(rack instanceof InnerNode) || rack.getParent() == null)) { throw new IllegalArgumentException("Unexpected data node " + node.toString() + " at an illegal network location"); } if (clusterMap.add(node)) { LOG.info("Adding a new node: " + NodeBase.getPath(node)); if (rack == null) { numOfRacks++; } } if(LOG.isDebugEnabled()) { LOG.debug("NetworkTopology became:\n" + this.toString()); } } finally { netlock.writeLock().unlock(); } } NetworkTopologyWithNodeGroup(); @Override String getRack(String loc); String getNodeGroup(String loc); @Override boolean isOnSameRack( Node node1,  Node node2); @Override boolean isOnSameNodeGroup(Node node1, Node node2); @Override boolean isNodeGroupAware(); @Override void add(Node node); @Override void remove(Node node); @Override void sortByDistance(Node reader, Node[] nodes, int activeLen); final static String DEFAULT_NODEGROUP; }
@Test public void testAddNodeWithInvalidTopology() { try { cluster.add(rackOnlyNode); fail("Exception should be thrown, so we should not have reached here."); } catch (Exception e) { if (!(e instanceof IllegalArgumentException)) { fail("Expecting IllegalArgumentException, but caught:" + e); } assertTrue(e.getMessage().contains("illegal network location")); } }