Host2NodesMap { boolean remove(DatanodeDescriptor node) { if (node == null) { return false; } String ipAddr = node.getIpAddr(); String hostname = node.getHostName(); hostmapLock.writeLock().lock(); try { DatanodeDescriptor[] nodes = map.get(ipAddr); if (nodes == null) { return false; } if (nodes.length == 1) { if (nodes[0] == node) { map.remove(ipAddr); return true; } else { return false; } } int i = 0; for (; i < nodes.length; i++) { if (nodes[i] == node) { break; } } if (i == nodes.length) { return false; } else { DatanodeDescriptor[] newNodes; newNodes = new DatanodeDescriptor[nodes.length - 1]; System.arraycopy(nodes, 0, newNodes, 0, i); System.arraycopy(nodes, i + 1, newNodes, i, nodes.length - i - 1); map.put(ipAddr, newNodes); return true; } } finally { hostmapLock.writeLock().unlock(); } }  DatanodeDescriptor getDatanodeByXferAddr(String ipAddr, int xferPort); @Override String toString();  }
@Test public void testRemove() throws Exception { DatanodeDescriptor nodeNotInMap = DFSTestUtil.getDatanodeDescriptor("3.3.3.3", "/d1/r4"); assertFalse(map.remove(nodeNotInMap)); assertTrue(map.remove(dataNodes[0])); assertTrue(map.getDatanodeByHost("1.1.1.1.") == null); assertTrue(map.getDatanodeByHost("2.2.2.2") == dataNodes[1]); DatanodeDescriptor node = map.getDatanodeByHost("3.3.3.3"); assertTrue(node == dataNodes[2] || node == dataNodes[3]); assertNull(map.getDatanodeByHost("4.4.4.4")); assertTrue(map.remove(dataNodes[2])); assertNull(map.getDatanodeByHost("1.1.1.1")); assertEquals(map.getDatanodeByHost("2.2.2.2"), dataNodes[1]); assertEquals(map.getDatanodeByHost("3.3.3.3"), dataNodes[3]); assertTrue(map.remove(dataNodes[3])); assertNull(map.getDatanodeByHost("1.1.1.1")); assertEquals(map.getDatanodeByHost("2.2.2.2"), dataNodes[1]); assertNull(map.getDatanodeByHost("3.3.3.3")); assertFalse(map.remove(null)); assertTrue(map.remove(dataNodes[1])); assertFalse(map.remove(dataNodes[1])); }