|
28 | 28 | import org.slf4j.LoggerFactory; |
29 | 29 |
|
30 | 30 | import org.apache.spark.SparkEnv; |
31 | | -import org.apache.spark.TaskContext; |
32 | 31 | import org.apache.spark.executor.ShuffleWriteMetrics; |
33 | 32 | import org.apache.spark.memory.MemoryConsumer; |
34 | 33 | import org.apache.spark.memory.TaskMemoryManager; |
|
40 | 39 | import org.apache.spark.unsafe.hash.Murmur3_x86_32; |
41 | 40 | import org.apache.spark.unsafe.memory.MemoryBlock; |
42 | 41 | import org.apache.spark.unsafe.memory.MemoryLocation; |
43 | | -import org.apache.spark.util.TaskCompletionListener; |
44 | 42 | import org.apache.spark.util.collection.unsafe.sort.UnsafeSorterSpillReader; |
45 | 43 | import org.apache.spark.util.collection.unsafe.sort.UnsafeSorterSpillWriter; |
46 | 44 |
|
@@ -175,6 +173,7 @@ public final class BytesToBytesMap extends MemoryConsumer { |
175 | 173 |
|
176 | 174 | private final BlockManager blockManager; |
177 | 175 | private volatile MapIterator destructiveIterator = null; |
| 176 | + private LinkedList<UnsafeSorterSpillWriter> spillWriters = new LinkedList<>(); |
178 | 177 |
|
179 | 178 | public BytesToBytesMap( |
180 | 179 | TaskMemoryManager taskMemoryManager, |
@@ -243,7 +242,6 @@ public final class MapIterator implements Iterator<Location> { |
243 | 242 | // If this iterator destructive or not. When it is true, it frees each page as it moves onto |
244 | 243 | // next one. |
245 | 244 | private boolean destructive = false; |
246 | | - private LinkedList<UnsafeSorterSpillWriter> spillWriters = new LinkedList<>(); |
247 | 245 | private UnsafeSorterSpillReader reader = null; |
248 | 246 |
|
249 | 247 | private MapIterator(int numRecords, Location loc, boolean destructive) { |
@@ -293,6 +291,17 @@ private void advanceToNextPage() { |
293 | 291 |
|
294 | 292 | @Override |
295 | 293 | public boolean hasNext() { |
| 294 | + if (numRecords == 0) { |
| 295 | + if (reader != null) { |
| 296 | + // remove the spill file from disk |
| 297 | + File file = spillWriters.removeFirst().getFile(); |
| 298 | + if (file != null && file.exists()) { |
| 299 | + if (!file.delete()) { |
| 300 | + logger.error("Was unable to delete spill file {}", file.getAbsolutePath()); |
| 301 | + } |
| 302 | + } |
| 303 | + } |
| 304 | + } |
296 | 305 | return numRecords > 0; |
297 | 306 | } |
298 | 307 |
|
@@ -355,21 +364,6 @@ public long spill(long numBytes) throws IOException { |
355 | 364 | } |
356 | 365 | writer.close(); |
357 | 366 | spillWriters.add(writer); |
358 | | - if (TaskContext.get() != null) { |
359 | | - TaskContext.get().addTaskCompletionListener( |
360 | | - new TaskCompletionListener() { |
361 | | - @Override |
362 | | - public void onTaskCompletion(TaskContext context) { |
363 | | - File file = writer.getFile(); |
364 | | - if (file != null && file.exists()) { |
365 | | - if (!file.delete()) { |
366 | | - logger.error("Was unable to delete spill file {}", file.getAbsolutePath()); |
367 | | - } |
368 | | - } |
369 | | - } |
370 | | - } |
371 | | - ); |
372 | | - } |
373 | 367 |
|
374 | 368 | dataPages.removeLast(); |
375 | 369 | released += block.size(); |
@@ -774,6 +768,15 @@ public void free() { |
774 | 768 | freePage(dataPage); |
775 | 769 | } |
776 | 770 | assert(dataPages.isEmpty()); |
| 771 | + |
| 772 | + while (!spillWriters.isEmpty()) { |
| 773 | + File file = spillWriters.removeFirst().getFile(); |
| 774 | + if (file != null && file.exists()) { |
| 775 | + if (!file.delete()) { |
| 776 | + logger.error("Was unable to delete spill file {}", file.getAbsolutePath()); |
| 777 | + } |
| 778 | + } |
| 779 | + } |
777 | 780 | } |
778 | 781 |
|
779 | 782 | public TaskMemoryManager getTaskMemoryManager() { |
|
0 commit comments