Source:
public ListenableFuture<List<Boolean>> apply(List<List<IMetric>> input) throws Exception { final long writeStartTime = System.currentTimeMillis(); final Timer.Context actualWriteCtx = writeDurationTimer.time(); final List<ListenableFuture<Boolean>> resultFutures = new ArrayList<ListenableFuture<Boolean>>(); for (List<IMetric> metrics: input) { final int batchId = batchIdGenerator.next(); final List<IMetric> batch = metrics; ListenableFuture<Boolean> futureBatchResult = getThreadPool().submit(new Callable<Boolean>() { public Boolean call() throws Exception { final Timer.Context singleBatchWriteCtx = batchWriteDurationTimer.time(); try { metricsRWDelegator.insertMetrics(batch); final Timer.Context dirtyTimerCtx = slotUpdateTimer.time(); try { for (IMetric metric : batch) { context.update(metric.getCollectionTime(), Util.getShard(metric.getLocator().toString())); } } finally { dirtyTimerCtx.stop(); } return true; } catch (Exception ex) { getLogger().error(ex.getMessage(), ex); getLogger().warn("Did not persist all metrics successfully for batch " + batchId); if ( getLogger().isDebugEnabled() ) { List<String> failedLocators = new ArrayList<String>() {{ for (IMetric metric : batch) { add(metric.getLocator().toString()); } }}; getLogger().debug("Failed batch contains: " + Arrays.toString(failedLocators.toArray())); } return false; } finally { singleBatchWriteCtx.stop(); bufferedMetrics.dec(batch.size()); long now = System.currentTimeMillis(); if ( now - writeStartTime > timeout.toMillis()) { exceededScribeProcessingTime.mark(); getLogger().info( String.format("Batch write time %d (ms) exceeded timeout %s before persisting " + "all metrics for batch %d", now - writeStartTime, timeout.toString(), batchId)); } } } }); resultFutures.add(futureBatchResult); } ListenableFuture<List<Boolean>> finalFuture = Futures.allAsList(resultFutures); finalFuture.addListener(new Runnable() { @Override public void run() { actualWriteCtx.stop(); } }, MoreExecutors.sameThreadExecutor()); return finalFuture; }

Target:
public ListenableFuture<List<Boolean>> apply(List<List<IMetric>> input) throws Exception { final long writeStartTime = System.currentTimeMillis(); final Timer.Context actualWriteCtx = writeDurationTimer.time(); final List<ListenableFuture<Boolean>> resultFutures = new ArrayList<ListenableFuture<Boolean>>(); for (List<IMetric> metrics: input) { final int batchId = batchIdGenerator.next(); final List<IMetric> batch = metrics; ListenableFuture<Boolean> futureBatchResult = getThreadPool().submit(new Callable<Boolean>() { public Boolean call() throws Exception { final Timer.Context singleBatchWriteCtx = batchWriteDurationTimer.time(); try { metricsRWDelegator.insertMetrics(batch); final Timer.Context dirtyTimerCtx = slotUpdateTimer.time(); try { for (IMetric metric : batch) { context.update(metric.getCollectionTime(), Util.getShard(metric.getLocator().toString())); } } finally { dirtyTimerCtx.stop(); } return true; } catch (Exception ex) { getLogger().error(ex.getMessage(), ex); getLogger().warn("Did not persist all metrics successfully for batch " + batchId); if ( getLogger().isDebugEnabled() ) { List<String> failedLocators = new ArrayList<String>() {{ for (IMetric metric : batch) { add(metric.getLocator().toString()); } }}; getLogger().debug("Failed batch contains: " + Arrays.toString(failedLocators.toArray())); } return false; } finally { singleBatchWriteCtx.stop(); bufferedMetrics.dec(batch.size()); long now = System.currentTimeMillis(); if ( now - writeStartTime > timeout.toMillis()) { exceededScribeProcessingTime.mark(); getLogger().debug( String.format("Batch write time %d (ms) exceeded timeout %s before persisting " + "all metrics for batch %d", now - writeStartTime, timeout.toString(), batchId)); } } } }); resultFutures.add(futureBatchResult); } ListenableFuture<List<Boolean>> finalFuture = Futures.allAsList(resultFutures); finalFuture.addListener(new Runnable() { @Override public void run() { actualWriteCtx.stop(); } }, MoreExecutors.sameThreadExecutor()); return finalFuture; }