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

    https://github.com/apache/spark/pull/6397#discussion_r31364644
  
    --- Diff: 
core/src/main/java/org/apache/spark/shuffle/sort/BypassMergeSortShuffleWriter.java
 ---
    @@ -0,0 +1,184 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.shuffle.sort;
    +
    +import java.io.File;
    +import java.io.FileInputStream;
    +import java.io.FileOutputStream;
    +import java.io.IOException;
    +
    +import scala.Product2;
    +import scala.Tuple2;
    +import scala.collection.Iterator;
    +
    +import com.google.common.io.Closeables;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import org.apache.spark.Partitioner;
    +import org.apache.spark.SparkConf;
    +import org.apache.spark.TaskContext;
    +import org.apache.spark.executor.ShuffleWriteMetrics;
    +import org.apache.spark.serializer.Serializer;
    +import org.apache.spark.serializer.SerializerInstance;
    +import org.apache.spark.storage.*;
    +import org.apache.spark.util.Utils;
    +
    +/**
    + * This class implements sort-based shuffle's hash-style shuffle fallback 
path. This write path
    + * writes incoming records to separate files, one file per reduce 
partition, then concatenates these
    + * per-partition files to form a single output file, regions of which are 
served to reducers.
    + * Records are not buffered in memory. This is essentially identical to
    + * {@link org.apache.spark.shuffle.hash.HashShuffleWriter}, except that it 
writes output in a format
    + * that can be served / consumed via {@link 
org.apache.spark.shuffle.IndexShuffleBlockResolver}.
    + * <p>
    + * This write path is inefficient for shuffles with large numbers of 
reduce partitions because it
    + * simultaneously opens separate serializers and file streams for all 
partitions. As a result,
    + * {@link SortShuffleManager} only selects this write path when
    + * <ul>
    + *    <li>no Ordering is specified,</li>
    + *    <li>no Aggregator is specific, and</li>
    + *    <li>the number of partitions is less than
    + *      <code>spark.shuffle.sort.bypassMergeThreshold</code>.</li>
    + * </ul>
    + *
    + * This code used to be part of {@link 
org.apache.spark.util.collection.ExternalSorter} but was
    + * refactored into its own class in order to reduce code complexity; see 
SPARK-7855 for details.
    + * <p>
    + * There have been proposals to completely remove this code path; see 
SPARK-6026 for details.
    + */
    +final class BypassMergeSortShuffleWriter<K, V> implements 
SortShuffleFileWriter<K, V> {
    +
    +  private final Logger logger = 
LoggerFactory.getLogger(BypassMergeSortShuffleWriter.class);
    +
    +  private final int fileBufferSize;
    +  private final boolean transferToEnabled;
    +  private final int numPartitions;
    +  private final BlockManager blockManager;
    +  private final Partitioner partitioner;
    +  private final ShuffleWriteMetrics writeMetrics;
    +  private final Serializer serializer;
    +
    +  /** Array of file writers, one for each partition */
    +  private BlockObjectWriter[] partitionWriters;
    +
    +  public BypassMergeSortShuffleWriter(
    +      SparkConf conf,
    +      BlockManager blockManager,
    +      Partitioner partitioner,
    +      ShuffleWriteMetrics writeMetrics,
    +      Serializer serializer) {
    +    // Use getSizeAsKb (not bytes) to maintain backwards compatibility if 
no units are provided
    +    this.fileBufferSize = (int) 
conf.getSizeAsKb("spark.shuffle.file.buffer", "32k") * 1024;
    +    this.transferToEnabled = conf.getBoolean("spark.file.transferTo", 
true);
    +    this.numPartitions = partitioner.numPartitions();
    +    this.blockManager = blockManager;
    +    this.partitioner = partitioner;
    +    this.writeMetrics = writeMetrics;
    +    this.serializer = serializer;
    +  }
    +
    +  @Override
    +  public void insertAll(Iterator<Product2<K, V>> records) throws 
IOException {
    +    assert (partitionWriters == null);
    +    if (!records.hasNext()) {
    +      return;
    +    }
    +    final SerializerInstance serInstance = serializer.newInstance();
    +    final long openStartTime = System.nanoTime();
    +    partitionWriters = new BlockObjectWriter[numPartitions];
    +    for (int i = 0; i < numPartitions; i++) {
    +      final Tuple2<TempShuffleBlockId, File> tempShuffleBlockIdPlusFile =
    +        blockManager.diskBlockManager().createTempShuffleBlock();
    +      final File file = tempShuffleBlockIdPlusFile._2();
    +      final BlockId blockId = tempShuffleBlockIdPlusFile._1();
    +      partitionWriters[i] =
    +        blockManager.getDiskWriter(blockId, file, serInstance, 
fileBufferSize, writeMetrics).open();
    +    }
    +    // Creating the file to write to and creating a disk writer both 
involve interacting with
    +    // the disk, and can take a long time in aggregate when we open many 
files, so should be
    +    // included in the shuffle write time.
    +    writeMetrics.incShuffleWriteTime(System.nanoTime() - openStartTime);
    +
    +    while (records.hasNext()) {
    +      final Product2<K, V> record = records.next();
    +      final K key = record._1();
    +      partitionWriters[partitioner.getPartition(key)].write(key, 
record._2());
    --- End diff --
    
    Thanks for noticing this.  I left this out on purpose, but should have 
probably commented on the diff to explain why.
    
    I think that the reason why `ExternalSorter` skips the 
`partitioner.getPartition(key)` call when there is only one partition is 
because `ExternalSorter` is also used for non-shuffle contexts for which we 
don't define a partitioner (such as the reduce-side sort in `sortByKey()`.  In 
those cases, we obviously want to avoid unnecessary hashing.
    
    BypassMergeSortShuffleWriter is only used for shuffles, though, and I 
expect that it's extremely rare to have shuffles that shuffle everything to a 
single partition (collecting results to the driver is handled by different 
code).  Therefore, I chose to leave out that check.


---
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