for (DatanodeDescriptor dn : datanodes) { long remaining = dn.getCacheRemaining(); for (Iterator<CachedBlock> it = dn.getPendingCached().iterator(); it.hasNext();) { CachedBlock cblock = it.next();
/** * Convert a CachedBlockList into a DatanodeCommand with a list of blocks. * * @param list The {@link CachedBlocksList}. This function * clears the list. * @param datanode The datanode. * @param action The action to perform in the command. * @param poolId The block pool id. * @return A DatanodeCommand to be sent back to the DN, or null if * there is nothing to be done. */ private DatanodeCommand getCacheCommand(CachedBlocksList list, DatanodeDescriptor datanode, int action, String poolId) { int length = list.size(); if (length == 0) { return null; } // Read the existing cache commands. long[] blockIds = new long[length]; int i = 0; for (Iterator<CachedBlock> iter = list.iterator(); iter.hasNext(); ) { CachedBlock cachedBlock = iter.next(); blockIds[i++] = cachedBlock.getBlockId(); } return new BlockIdCommand(action, poolId, blockIds); }
!dn.getPendingCached().iterator().hasNext()); Assert.assertTrue("expected cached list to start off empty.", !dn.getCached().iterator().hasNext()); Assert.assertTrue("expected pending uncached list to start off empty.", !dn.getPendingUncached().iterator().hasNext()); !dn.getPendingCached().iterator().hasNext()); Assert.assertEquals("failed to insert blocks[0]", blocks[0], dn.getCached().iterator().next()); Assert.assertTrue("expected pending uncached list to still be empty.", !dn.getPendingUncached().iterator().hasNext()); Iterator<CachedBlock> iter = dn.getCached().iterator(); Assert.assertEquals(blocks[0], iter.next()); Assert.assertEquals(blocks[1], iter.next()); iter = dn.getCached().iterator(); Assert.assertEquals(blocks[2], iter.next()); Assert.assertEquals(blocks[0], iter.next()); iter = dn.getCached().iterator(); Assert.assertEquals(blocks[2], iter.next()); Assert.assertEquals(blocks[1], iter.next()); !dn.getPendingCached().iterator().hasNext());
private void testRemoveElementsFromList(Random r, CachedBlocksList list, CachedBlock[] blocks) { int i = 0; for (Iterator<CachedBlock> iter = list.iterator(); iter.hasNext(); ) { Assert.assertEquals(blocks[i], iter.next()); i++; } if (r.nextBoolean()) { LOG.info("Removing via iterator"); for (Iterator<CachedBlock> iter = list.iterator(); iter.hasNext() ;) { iter.next(); iter.remove(); } } else { LOG.info("Removing in pseudo-random order"); CachedBlock[] remainingBlocks = Arrays.copyOf(blocks, blocks.length); for (int removed = 0; removed < remainingBlocks.length; ) { int toRemove = r.nextInt(remainingBlocks.length); if (remainingBlocks[toRemove] != null) { Assert.assertTrue(list.remove(remainingBlocks[toRemove])); remainingBlocks[toRemove] = null; removed++; } } } Assert.assertTrue("expected list to be empty after everything " + "was removed.", !list.iterator().hasNext()); }
/** * Convert a CachedBlockList into a DatanodeCommand with a list of blocks. * * @param list The {@link CachedBlocksList}. This function * clears the list. * @param datanode The datanode. * @param action The action to perform in the command. * @param poolId The block pool id. * @return A DatanodeCommand to be sent back to the DN, or null if * there is nothing to be done. */ private DatanodeCommand getCacheCommand(CachedBlocksList list, DatanodeDescriptor datanode, int action, String poolId) { int length = list.size(); if (length == 0) { return null; } // Read the existing cache commands. long[] blockIds = new long[length]; int i = 0; for (Iterator<CachedBlock> iter = list.iterator(); iter.hasNext(); ) { CachedBlock cachedBlock = iter.next(); blockIds[i++] = cachedBlock.getBlockId(); } return new BlockIdCommand(action, poolId, blockIds); }
private void testAddElementsToList(CachedBlocksList list, CachedBlock[] blocks) { Assert.assertTrue("expected list to start off empty.", !list.iterator().hasNext()); for (CachedBlock block : blocks) { Assert.assertTrue(list.add(block)); } }