@VisibleForTesting
 void closeAndWriteOutput() throws IOException {
   assert (sorter != null);
   updatePeakMemoryUsed();
   serBuffer = null;
   serOutputStream = null;
   final SpillInfo[] spills = sorter.closeAndGetSpills();
   sorter = null;
   final long[] partitionLengths;
   try {
     partitionLengths = mergeSpills(spills);
   } finally {
     for (SpillInfo spill : spills) {
       if (spill.file.exists() && !spill.file.delete()) {
         logger.error("Error while deleting spill file {}", spill.file.getPath());
       }
     }
   }
   shuffleBlockResolver.writeIndexFile(shuffleId, mapId, partitionLengths);
   mapStatus = MapStatus$.MODULE$.apply(blockManager.shuffleServerId(), partitionLengths);
 }