Program: 54479
Project: 54479
Buggy source:
private void cloneCollection(MongodServer localServer,MongoConnection remoteConnection,String toDb,CloneOptions opts,Materializer materializer,Entry collToClone) throws MongoException {
  String collName=collToClone.getCollectionName();
  MongoCursor<BsonDocument> cursor=openCursor(remoteConnection,collName,opts);
  CollectionIterator iterator=new CollectionIterator(cursor,retrier);
  Source<BsonDocument,NotUsed> source=Source.fromIterator(() -> iterator).buffer(cursorBatchBufferSize,OverflowStrategy.backpressure()).async();
  Flow<BsonDocument,Pair<Integer,Integer>,NotUsed> inserterFlow;
  if (maxParallelInsertTasks == 1) {
    inserterFlow=createCloneDocsWorker(localServer,toDb,collName);
  }
 else {
    Graph<FlowShape<BsonDocument,Pair<Integer,Integer>>,NotUsed> graph=GraphDSL.create(builder -> {
      UniformFanOutShape<BsonDocument,BsonDocument> balance=builder.add(Balance.create(maxParallelInsertTasks,false));
      UniformFanInShape<Pair<Integer,Integer>,Pair<Integer,Integer>> merge=builder.add(Merge.create(maxParallelInsertTasks,false));
      for (int i=0; i < maxParallelInsertTasks; i++) {
        builder.from(balance.out(i)).via(builder.add(createCloneDocsWorker(localServer,toDb,collName).async())).toInlet(merge.in(i));
      }
      return FlowShape.of(balance.in(),merge.out());
    }
);
    inserterFlow=Flow.fromGraph(graph);
  }
  try {
    retrier.retry(() -> {
      try {
        Pair<Integer,Integer> insertedDocs=source.via(inserterFlow).toMat(Sink.fold(new Pair<>(0,0),(        Pair<Integer,Integer> acum,        Pair<Integer,Integer> newBatch) -> new Pair<>(acum.first() + newBatch.first(),acum.second() + newBatch.second())),Keep.right()).run(materializer).toCompletableFuture().join();
        LOGGER.debug("String_Node_Str",insertedDocs.second(),insertedDocs.first());
        return Done.getInstance();
      }
 catch (      CompletionException ex) {
        Throwable cause=ex.getCause();
        if (cause != null && cause instanceof RollbackException) {
          throw (RollbackException)cause;
        }
        throw ex;
      }
    }
,Hint.TIME_SENSIBLE,Hint.INFREQUENT_ROLLBACK);
  }
 catch (  RetrierGiveUpException ex) {
    throw new CloningException(ex);
  }
}

Fixed version (ground truth):
private void cloneCollection(MongodServer localServer,MongoConnection remoteConnection,String toDb,CloneOptions opts,Materializer materializer,Entry collToClone) throws MongoException {
  String collName=collToClone.getCollectionName();
  MongoCursor<BsonDocument> cursor=openCursor(remoteConnection,collName,opts);
  CollectionIterator iterator=new CollectionIterator(cursor,retrier);
  Source<BsonDocument,NotUsed> source=Source.fromIterator(() -> iterator).buffer(cursorBatchBufferSize,OverflowStrategy.backpressure()).async();
  Flow<BsonDocument,Pair<Integer,Integer>,NotUsed> inserterFlow;
  if (maxParallelInsertTasks == 1) {
    inserterFlow=createCloneDocsWorker(localServer,toDb,collName);
  }
 else {
    Graph<FlowShape<BsonDocument,Pair<Integer,Integer>>,NotUsed> graph=GraphDSL.create(builder -> {
      UniformFanOutShape<BsonDocument,BsonDocument> balance=builder.add(Balance.create(maxParallelInsertTasks,false));
      UniformFanInShape<Pair<Integer,Integer>,Pair<Integer,Integer>> merge=builder.add(Merge.create(maxParallelInsertTasks,false));
      for (int i=0; i < maxParallelInsertTasks; i++) {
        builder.from(balance.out(i)).via(builder.add(createCloneDocsWorker(localServer,toDb,collName).async())).toInlet(merge.in(i));
      }
      return FlowShape.of(balance.in(),merge.out());
    }
);
    inserterFlow=Flow.fromGraph(graph);
  }
  try {
    source.via(inserterFlow).fold(new Tuple3<>(0,0,clock.instant()),(acum,batch) -> postInsertFold(toDb,collName,acum,batch)).toMat(Sink.foreach(tuple -> logCollectionCloning(toDb,collName,tuple.t1(),tuple.t2())),Keep.right()).run(materializer).toCompletableFuture().join();
  }
 catch (  CompletionException ex) {
    Throwable cause=ex.getCause();
    if (cause != null) {
      throw new CloningException("String_Node_Str" + toDb + "String_Node_Str"+ collName,cause);
    }
    throw ex;
  }
}