DFSNetworkTopology extends NetworkTopology { public Node chooseRandomWithStorageTypeTwoTrial(final String scope, final Collection<Node> excludedNodes, StorageType type) { netlock.readLock().lock(); try { String searchScope; String excludedScope; if (scope.startsWith("~")) { searchScope = NodeBase.ROOT; excludedScope = scope.substring(1); } else { searchScope = scope; excludedScope = null; } Node n = chooseRandom(searchScope, excludedScope, excludedNodes); if (n == null) { if (LOG.isDebugEnabled()) { LOG.debug("No node to choose."); } return null; } Preconditions.checkArgument(n instanceof DatanodeDescriptor); DatanodeDescriptor dnDescriptor = (DatanodeDescriptor)n; if (dnDescriptor.hasStorageType(type)) { return dnDescriptor; } else { LOG.debug("First trial failed, node has no type {}, " + "making second trial carrying this type", type); return chooseRandomWithStorageType(searchScope, excludedScope, 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 testChooseRandomWithStorageTypeTwoTrial() 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.chooseRandomWithStorageTypeTwoTrial( "/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.chooseRandomWithStorageTypeTwoTrial( "/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.chooseRandomWithStorageTypeTwoTrial( "~/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.chooseRandomWithStorageTypeTwoTrial( "~/l2/d4", excluded, StorageType.RAM_DISK); assertTrue(n instanceof DatanodeDescriptor); dd = (DatanodeDescriptor) n; assertTrue(dd.getHostName().equals("host7")); } }