Github user davies commented on a diff in the pull request:

    https://github.com/apache/spark/pull/9241#discussion_r43458390
  
    --- Diff: 
core/src/main/java/org/apache/spark/unsafe/map/BytesToBytesMap.java ---
    @@ -219,61 +232,156 @@ public BytesToBytesMap(
        */
       public int numElements() { return numElements; }
     
    -  public static final class BytesToBytesMapIterator implements 
Iterator<Location> {
    +  public final class MapIterator implements Iterator<Location> {
     
    -    private final int numRecords;
    -    private final Iterator<MemoryBlock> dataPagesIterator;
    +    private int numRecords;
         private final Location loc;
     
         private MemoryBlock currentPage = null;
    -    private int currentRecordNumber = 0;
    +    private int recordsInPage = 0;
         private Object pageBaseObject;
         private long offsetInPage;
     
         // If this iterator destructive or not. When it is true, it frees each 
page as it moves onto
         // next one.
         private boolean destructive = false;
    -    private BytesToBytesMap bmap;
    +    private LinkedList<UnsafeSorterSpillWriter> spillWriters = new 
LinkedList<>();
    +    private UnsafeSorterSpillReader reader = null;
     
    -    private BytesToBytesMapIterator(
    -        int numRecords, Iterator<MemoryBlock> dataPagesIterator, Location 
loc,
    -        boolean destructive, BytesToBytesMap bmap) {
    +    private MapIterator(int numRecords, Location loc, boolean destructive) 
{
           this.numRecords = numRecords;
    -      this.dataPagesIterator = dataPagesIterator;
           this.loc = loc;
           this.destructive = destructive;
    -      this.bmap = bmap;
    -      if (dataPagesIterator.hasNext()) {
    -        advanceToNextPage();
    +      if (destructive) {
    +        destructiveIterator = this;
           }
         }
     
         private void advanceToNextPage() {
    -      if (destructive && currentPage != null) {
    -        dataPagesIterator.remove();
    -        this.bmap.taskMemoryManager.freePage(currentPage);
    +      synchronized (this) {
    +        int nextIdx = dataPages.indexOf(currentPage) + 1;
    +        if (destructive && currentPage != null) {
    +          dataPages.remove(currentPage);
    +          freePage(currentPage);
    +          nextIdx --;
    +        }
    +        if (dataPages.size() > nextIdx) {
    +          currentPage = dataPages.get(nextIdx);
    +          pageBaseObject = currentPage.getBaseObject();
    +          offsetInPage = currentPage.getBaseOffset();
    +          recordsInPage = Platform.getInt(pageBaseObject, offsetInPage);
    +          offsetInPage += 4;
    +        } else {
    +          currentPage = null;
    +          if (reader != null) {
    +            // remove the spill file from disk
    +            File file = spillWriters.removeFirst().getFile();
    +            if (file != null && file.exists()) {
    +              if (!file.delete()) {
    +                logger.error("Was unable to delete spill file {}", 
file.getAbsolutePath());
    +              }
    +            }
    +          }
    +          try {
    +            reader = spillWriters.getFirst().getReader(blockManager);
    +            recordsInPage = -1;
    +          } catch (IOException e) {
    +            // Scala iterator does not handle exception
    +            Platform.throwException(e);
    +          }
    +        }
           }
    -      currentPage = dataPagesIterator.next();
    -      pageBaseObject = currentPage.getBaseObject();
    -      offsetInPage = currentPage.getBaseOffset();
         }
     
         @Override
         public boolean hasNext() {
    -      return currentRecordNumber != numRecords;
    +      return numRecords > 0;
         }
     
         @Override
         public Location next() {
    -      int totalLength = Platform.getInt(pageBaseObject, offsetInPage);
    -      if (totalLength == END_OF_PAGE_MARKER) {
    +      if (recordsInPage == 0) {
             advanceToNextPage();
    -        totalLength = Platform.getInt(pageBaseObject, offsetInPage);
           }
    -      loc.with(currentPage, offsetInPage);
    -      offsetInPage += 4 + totalLength;
    -      currentRecordNumber++;
    -      return loc;
    +      numRecords--;
    +      if (currentPage != null) {
    +        int totalLength = Platform.getInt(pageBaseObject, offsetInPage);
    +        loc.with(currentPage, offsetInPage);
    +        offsetInPage += 4 + totalLength;
    +        recordsInPage --;
    +        return loc;
    +      } else {
    +        assert(reader != null);
    +        if (!reader.hasNext()) {
    +          advanceToNextPage();
    +        }
    +        try {
    +          reader.loadNext();
    +        } catch (IOException e) {
    +          // Scala iterator does not handle exception
    +          Platform.throwException(e);
    +        }
    +        loc.with(reader.getBaseObject(), reader.getBaseOffset(), 
reader.getRecordLength());
    +        return loc;
    +      }
    +    }
    +
    +    public long spill(long numBytes) throws IOException {
    +      synchronized (this) {
    +        if (!destructive || dataPages.size() == 1) {
    +          return 0L;
    +        }
    +
    +        // TODO: use existing ShuffleWriteMetrics
    +        ShuffleWriteMetrics writeMetrics = new ShuffleWriteMetrics();
    +
    +        long released = 0L;
    +        while (dataPages.size() > 0) {
    +          MemoryBlock block = dataPages.getLast();
    +          // The currentPage is used, cannot be released
    +          if (block == currentPage) {
    +            break;
    +          }
    +
    +          Object base = block.getBaseObject();
    +          long offset = block.getBaseOffset();
    +          int numRecords = Platform.getInt(base, offset);
    +          offset += 4;
    +          final UnsafeSorterSpillWriter writer =
    +            new UnsafeSorterSpillWriter(blockManager, 32 * 1024, 
writeMetrics, numRecords);
    +          while (numRecords-- > 0) {
    +            int length = Platform.getInt(base, offset);
    +            writer.write(base, offset + 4, length, 0);
    +            offset += 4 + length;
    +          }
    +          writer.close();
    +          spillWriters.add(writer);
    --- End diff --
    
    yes


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to