DFSNetworkTopology extends NetworkTopology { public Node chooseRandomWithStorageType(final String scope, final Collection<Node> excludedNodes, StorageType type) { netlock.readLock().lock(); try { if (scope.startsWith("~")) { return chooseRandomWithStorageType( NodeBase.ROOT, scope.substring(1), excludedNodes, type); } else { return chooseRandomWithStorageType( scope, null, excludedNodes, type); } } finally { netlock.readLock().unlock(); } }  static DFSNetworkTopology getInstance(Configuration conf); Node chooseRandomWithStorageType(final String scope,
      final Collection<Node> excludedNodes, StorageType type); Node chooseRandomWithStorageTypeTwoTrial(final String scope,
      final Collection<Node> excludedNodes, StorageType type);  }
@Test public void testChooseRandomWithStorageType() throws Exception { Node n; DatanodeDescriptor dd; Set<String> diskUnderL1 = Sets.newHashSet("host2", "host4", "host5", "host6"); Set<String> archiveUnderL1 = Sets.newHashSet("host1", "host3"); Set<String> ramdiskUnderL1 = Sets.newHashSet("host7"); Set<String> ssdUnderL1 = Sets.newHashSet("host8"); Set<String> nvdimmUnderL1 = Sets.newHashSet("host9"); for (int i = 0; i < 10; i++) { n = CLUSTER.chooseRandomWithStorageType("/l1", null, null, StorageType.DISK); assertTrue(n instanceof DatanodeDescriptor); dd = (DatanodeDescriptor) n; assertTrue(diskUnderL1.contains(dd.getHostName())); n = CLUSTER.chooseRandomWithStorageType("/l1", null, null, StorageType.RAM_DISK); assertTrue(n instanceof DatanodeDescriptor); dd = (DatanodeDescriptor) n; assertTrue(ramdiskUnderL1.contains(dd.getHostName())); n = CLUSTER.chooseRandomWithStorageType("/l1", null, null, StorageType.ARCHIVE); assertTrue(n instanceof DatanodeDescriptor); dd = (DatanodeDescriptor) n; assertTrue(archiveUnderL1.contains(dd.getHostName())); n = CLUSTER.chooseRandomWithStorageType("/l1", null, null, StorageType.SSD); assertTrue(n instanceof DatanodeDescriptor); dd = (DatanodeDescriptor) n; assertTrue(ssdUnderL1.contains(dd.getHostName())); } }
@Test public void testChooseRandomWithStorageTypeWithExcluded() throws Exception { Node n; DatanodeDescriptor dd; n = CLUSTER.chooseRandomWithStorageType("/l2/d3/r4", null, null, StorageType.ARCHIVE); HashSet<Node> excluded = new HashSet<>(); excluded.add(n); for (int i = 0; i < 10; i++) { n = CLUSTER.chooseRandomWithStorageType("/l2/d3", null, null, StorageType.ARCHIVE); assertTrue(n instanceof DatanodeDescriptor); dd = (DatanodeDescriptor) n; assertTrue(dd.getHostName().equals("host13") || dd.getHostName().equals("host14")); } for (int i = 0; i < 10; i++) { n = CLUSTER.chooseRandomWithStorageType("/l2/d3", null, excluded, StorageType.ARCHIVE); assertTrue(n instanceof DatanodeDescriptor); dd = (DatanodeDescriptor) n; assertTrue(dd.getHostName().equals("host14")); } for (int i = 0; i < 10; i++) { n = CLUSTER.chooseRandomWithStorageType("/l2/d3", "/l2/d3/r4", null, StorageType.ARCHIVE); assertTrue(n instanceof DatanodeDescriptor); dd = (DatanodeDescriptor) n; assertTrue(dd.getHostName().equals("host14")); } for (int i = 0; i < 10; i++) { n = CLUSTER.chooseRandomWithStorageType("/l2/d3", "/l2/d3/r5", excluded, StorageType.ARCHIVE); assertNull(n); } n = CLUSTER.chooseRandomWithStorageType("/l1/d2", null, null, StorageType.DISK); dd = (DatanodeDescriptor)n; assertEquals("host6", dd.getHostName()); excluded.add(n); Set<String> expectedSet = Sets.newHashSet("host4", "host5"); for (int i = 0; i < 10; i++) { n = CLUSTER.chooseRandomWithStorageType( "/l1", "/l1/d1/r1", excluded, StorageType.DISK); dd = (DatanodeDescriptor) n; assertTrue(expectedSet.contains(dd.getHostName())); } }
@Test public void testChooseRandomWithStorageTypeWithExcludedforNullCheck() throws Exception { HashSet<Node> excluded = new HashSet<>(); excluded.add(new DatanodeInfoBuilder() .setNodeID(DatanodeID.EMPTY_DATANODE_ID).build()); Node node = CLUSTER.chooseRandomWithStorageType("/", "/l1/d1/r1", excluded, StorageType.ARCHIVE); assertNotNull(node); }
@Test public void testChooseRandomWithStorageTypeWrapper() throws Exception { Node n; DatanodeDescriptor dd; n = CLUSTER.chooseRandomWithStorageType("/l2/d3/r4", null, null, StorageType.ARCHIVE); HashSet<Node> excluded = new HashSet<>(); excluded.add(n); for (int i = 0; i < 10; i++) { n = CLUSTER.chooseRandomWithStorageType( "/l2/d3", null, StorageType.ARCHIVE); assertTrue(n instanceof DatanodeDescriptor); dd = (DatanodeDescriptor) n; assertTrue(dd.getHostName().equals("host13") || dd.getHostName().equals("host14")); } for (int i = 0; i < 10; i++) { n = CLUSTER.chooseRandomWithStorageType( "/l2/d3", excluded, StorageType.ARCHIVE); assertTrue(n instanceof DatanodeDescriptor); dd = (DatanodeDescriptor) n; assertTrue(dd.getHostName().equals("host14")); } for (int i = 0; i < 10; i++) { n = CLUSTER.chooseRandomWithStorageType( "~/l2/d4", null, StorageType.RAM_DISK); assertTrue(n instanceof DatanodeDescriptor); dd = (DatanodeDescriptor) n; assertTrue(dd.getHostName().equals("host7") || dd.getHostName().equals("host11")); } n = CLUSTER.chooseRandomWithStorageType("/l2/d3/r2", null, null, StorageType.RAM_DISK); excluded.add(n); for (int i = 0; i < 10; i++) { n = CLUSTER.chooseRandomWithStorageType( "~/l2/d4", excluded, StorageType.RAM_DISK); assertTrue(n instanceof DatanodeDescriptor); dd = (DatanodeDescriptor) n; assertTrue(dd.getHostName().equals("host7")); } }
@Test public void testNonExistingNode() throws Exception { Node n; n = CLUSTER.chooseRandomWithStorageType( "/l100", null, null, StorageType.DISK); assertNull(n); n = CLUSTER.chooseRandomWithStorageType( "/l100/d100", null, null, StorageType.DISK); assertNull(n); n = CLUSTER.chooseRandomWithStorageType( "/l100/d100/r100", null, null, StorageType.DISK); assertNull(n); }