Source:
private void runMoves(final String firstContextId, final String secondContextId) { String srcId = firstContextId; String destId = secondContextId; for (int i = 0; i < iterations; i++) { final CountDownLatch finishedLatch = new CountDownLatch(1); final int initialSrcNumBlocks = getNumBlocks(srcId); final int initialDestNumBlocks = getNumBlocks(destId); final int numToMove = Math.max(1, RANDOM.nextInt(initialSrcNumBlocks)); LOG.log(Level.INFO, "Move {0} blocks from {1} to {2} (Initial number of blocks: {3} / {4} respectively)", new Object[]{numToMove, srcId, destId, initialSrcNumBlocks, initialDestNumBlocks}); final boolean[] moveSucceeded = {false}; try (final TraceScope moveTraceScope = Trace.startSpan("simpleMove", Sampler.ALWAYS)) { emService.move(SimpleEMTask.DATATYPE, numToMove, srcId, destId, new EventHandler<AvroElasticMemoryMessage>() { @Override public void onNext(final AvroElasticMemoryMessage emMsg) { moveSucceeded[0] = emMsg.getResultMsg().getResult().equals(Result.SUCCESS); LOG.log(Level.INFO, "Move {0} succeeded {1} with result {2}", new Object[]{emMsg.getOperationId(), moveSucceeded[0], emMsg.getResultMsg() == null ? "" : emMsg.getResultMsg().getResult()}); finishedLatch.countDown(); } } ); } try { LOG.log(Level.INFO, "Waiting for move to finish on iteration {0}", i); finishedLatch.await(); checkBlocks(srcId, initialSrcNumBlocks - numToMove); checkBlocks(destId, initialDestNumBlocks + numToMove); if (moveSucceeded[0]) { LOG.log(Level.INFO, "Move finished on iteration {0}", i); } else { throw new RuntimeException("Move failed on iteration " + i); } } catch (final InterruptedException e) { throw new RuntimeException("Move wait interrupted on iteration " + i, e); } final String tmpContextId = srcId; srcId = destId; destId = tmpContextId; } }

Target:
private void runMoves(final String firstContextId, final String secondContextId) { String srcId = firstContextId; String destId = secondContextId; for (int i = 0; i < iterations; i++) { final CountDownLatch finishedLatch = new CountDownLatch(1); final int initialSrcNumBlocks = getNumBlocks(srcId); final int initialDestNumBlocks = getNumBlocks(destId); final int numToMove = Math.max(1, RANDOM.nextInt(initialSrcNumBlocks)); LOG.log(Level.INFO, "Move {0} blocks from {1} to {2} (Initial number of blocks: {3} / {4} respectively)", new Object[]{numToMove, srcId, destId, initialSrcNumBlocks, initialDestNumBlocks}); final boolean[] moveSucceeded = {false}; try (final TraceScope moveTraceScope = Trace.startSpan("simpleMove", Sampler.ALWAYS)) { emService.move(SimpleEMTask.DATATYPE, numToMove, srcId, destId, new EventHandler<AvroElasticMemoryMessage>() { @Override public void onNext(final AvroElasticMemoryMessage emMsg) { moveSucceeded[0] = emMsg.getResultMsg().getResult().equals(Result.SUCCESS); LOG.log(Level.INFO, "Was Move {0} successful? {1}. The result: {2}", new Object[]{emMsg.getOperationId(), moveSucceeded[0], emMsg.getResultMsg() == null ? "" : emMsg.getResultMsg().getResult()}); finishedLatch.countDown(); } } ); } try { LOG.log(Level.INFO, "Waiting for move to finish on iteration {0}", i); finishedLatch.await(); checkNumBlocks(srcId, initialSrcNumBlocks - numToMove); checkNumBlocks(destId, initialDestNumBlocks + numToMove); if (moveSucceeded[0]) { LOG.log(Level.INFO, "Move finished on iteration {0}", i); } else { throw new RuntimeException("Move failed on iteration " + i); } } catch (final InterruptedException e) { throw new RuntimeException("Move wait interrupted on iteration " + i, e); } final String tmpContextId = srcId; srcId = destId; destId = tmpContextId; } }