FSDirWriteFileOp { static DatanodeStorageInfo[] chooseTargetForNewBlock( BlockManager bm, String src, DatanodeInfo[] excludedNodes, String[] favoredNodes, EnumSet<AddBlockFlag> flags, ValidateAddBlockResult r) throws IOException { Node clientNode = null; boolean ignoreClientLocality = (flags != null && flags.contains(AddBlockFlag.IGNORE_CLIENT_LOCALITY)); if (!ignoreClientLocality) { clientNode = bm.getDatanodeManager().getDatanodeByHost(r.clientMachine); if (clientNode == null) { clientNode = getClientNode(bm, r.clientMachine); } } Set<Node> excludedNodesSet = (excludedNodes == null) ? new HashSet<>() : new HashSet<>(Arrays.asList(excludedNodes)); List<String> favoredNodesList = (favoredNodes == null) ? Collections.emptyList() : Arrays.asList(favoredNodes); return bm.chooseTarget4NewBlock(src, r.numTargets, clientNode, excludedNodesSet, r.blockSize, favoredNodesList, r.storagePolicyID, r.blockType, r.ecPolicy, flags); } private  FSDirWriteFileOp();   }
@Test @SuppressWarnings("unchecked") public void testIgnoreClientLocality() throws IOException { ValidateAddBlockResult addBlockResult = new ValidateAddBlockResult(1024L, 3, (byte) 0x01, null, null, null); EnumSet<AddBlockFlag> addBlockFlags = EnumSet.of(AddBlockFlag.IGNORE_CLIENT_LOCALITY); BlockManager bmMock = mock(BlockManager.class); ArgumentCaptor<Node> nodeCaptor = ArgumentCaptor.forClass(Node.class); when(bmMock.chooseTarget4NewBlock(anyString(), anyInt(), any(), anySet(), anyLong(), anyList(), anyByte(), any(), any(), any())).thenReturn(null); FSDirWriteFileOp.chooseTargetForNewBlock(bmMock, "localhost", null, null, addBlockFlags, addBlockResult); verify(bmMock, times(1)).chooseTarget4NewBlock(anyString(), anyInt(), nodeCaptor.capture(), anySet(), anyLong(), anyList(), anyByte(), any(), any(), any()); verifyNoMoreInteractions(bmMock); assertNull( "Source node was assigned a value. Expected 'null' value because " + "chooseTarget was flagged to ignore source node locality", nodeCaptor.getValue()); }