Source:
public List<List<BlockTransferInfo>> partitionTransfers(List<BlockTransferInfo> transferInfos, int maxPartitionCount) { TransferPartitionKey key = findTransferBucketKey(transferInfos); if (key == TransferPartitionKey.NONE) { LOG.debug("Un-optimizable transfer list encountered."); return new ArrayList<List<BlockTransferInfo>>() { { add(transferInfos); } }; } Hashtable<BlockStoreLocation, List<BlockTransferInfo>> transferBuckets = new Hashtable<>(); for (BlockTransferInfo transferInfo : transferInfos) { BlockStoreLocation keyLoc; switch (key) { case SRC: keyLoc = transferInfo.getSrcLocation(); break; case DST: keyLoc = transferInfo.getDstLocation(); break; default: throw new IllegalStateException( String.format("Unsupported key type for bucketing transfer infos: %s", key.name())); } if (!transferBuckets.containsKey(keyLoc)) { transferBuckets.put(keyLoc, new LinkedList<>()); } transferBuckets.get(keyLoc).add(transferInfo); } List<List<BlockTransferInfo>> balancedPartitions = balancePartitions( transferBuckets.values().stream().collect(Collectors.toList()), maxPartitionCount); if (LOG.isDebugEnabled()) { StringBuilder partitionDbgStr = new StringBuilder(); partitionDbgStr .append(String.format("Bucketed %d transfers into %d partitions using key:%s.%n", transferInfos.size(), balancedPartitions.size(), key.name())); for (int i = 0; i < balancedPartitions.size(); i++) { partitionDbgStr.append(String.format("Partition-%d:%n ->%s%n", i, balancedPartitions.get(i) .stream().map(Objects::toString).collect(Collectors.joining("\n ->")))); } LOG.debug(partitionDbgStr.toString()); } return balancedPartitions; }

Target:
public List<List<BlockTransferInfo>> partitionTransfers(List<BlockTransferInfo> transferInfos, int maxPartitionCount) { TransferPartitionKey key = findTransferBucketKey(transferInfos); if (key == TransferPartitionKey.NONE) { LOG.debug("Un-optimizable transfer list encountered."); return new ArrayList<List<BlockTransferInfo>>() { { add(transferInfos); } }; } Map<BlockStoreLocation, List<BlockTransferInfo>> transferBuckets = new HashMap<>(); for (BlockTransferInfo transferInfo : transferInfos) { BlockStoreLocation keyLoc; switch (key) { case SRC: keyLoc = transferInfo.getSrcLocation(); break; case DST: keyLoc = transferInfo.getDstLocation(); break; default: throw new IllegalStateException( String.format("Unsupported key type for bucketing transfer infos: %s", key.name())); } if (!transferBuckets.containsKey(keyLoc)) { transferBuckets.put(keyLoc, new LinkedList<>()); } transferBuckets.get(keyLoc).add(transferInfo); } List<List<BlockTransferInfo>> balancedPartitions = balancePartitions( transferBuckets.values().stream().collect(Collectors.toList()), maxPartitionCount); if (LOG.isDebugEnabled()) { StringBuilder partitionDbgStr = new StringBuilder(); partitionDbgStr .append(String.format("Bucketed %d transfers into %d partitions using key:%s.%n", transferInfos.size(), balancedPartitions.size(), key.name())); for (int i = 0; i < balancedPartitions.size(); i++) { partitionDbgStr.append(String.format("Partition-%d:%n ->%s%n", i, balancedPartitions.get(i) .stream().map(Objects::toString).collect(Collectors.joining("\n ->")))); } LOG.debug(partitionDbgStr.toString()); } return balancedPartitions; }