Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.IOException;

import java.util.Optional;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: import grouping

import org.apache.spark.annotation.Private;

/**
Expand All @@ -39,17 +40,39 @@ public interface ShuffleExecutorComponents {
/**
* Called once per map task to create a writer that will be responsible for persisting all the
* partitioned bytes written by that map task.
* @param shuffleId Unique identifier for the shuffle the map task is a part of
*
* @param shuffleId Unique identifier for the shuffle the map task is a part of
* @param mapId Within the shuffle, the identifier of the map task
* @param mapTaskAttemptId Identifier of the task attempt. Multiple attempts of the same map task
* with the same (shuffleId, mapId) pair can be distinguished by the
* different values of mapTaskAttemptId.
* with the same (shuffleId, mapId) pair can be distinguished by the
* different values of mapTaskAttemptId.
* @param numPartitions The number of partitions that will be written by the map task. Some of
* these partitions may be empty.
* these partitions may be empty.
*/
ShuffleMapOutputWriter createMapOutputWriter(
int shuffleId,
int mapId,
long mapTaskAttemptId,
int numPartitions) throws IOException;

/**
* An optional extension for creating a map output writer that can optimize the transfer of a
* single partition file, as the entire result of a map task, to the backing store.
* <p>
* Most implementations should return the default {@link Optional#empty()} to indicate that
* they do not support this optimization. This primarily is for backwards-compatibility in
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably it's better to indicate what kinds of implementations may support this optimization? Otherwise it's confusing. I think the storage that has API like move supports this optimization, is this right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Truth be told, even plugins that support remote FS move would unlikely be able to support this well - one would still have to transfer the whole file up to the remote storage layer, but that could just as easily be done by writing the data from the file through an output stream.

I think only implementations that stage the files locally could support this in any meaningful way at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with leaving out the docs if only because very very few implementations should even care about this API.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I don't think its necessary to go into more details. I would say the "casual" plugin developer wouldn't bother with this, and if they're really serious, they can look at what the existing implementation does. The comment is sufficient for that.

* preserving an optimization in the local disk shuffle storage implementation.
*
* @param shuffleId Unique identifier for the shuffle the map task is a part of
* @param mapId Within the shuffle, the identifier of the map task
* @param mapTaskAttemptId Identifier of the task attempt. Multiple attempts of the same map task
* with the same (shuffleId, mapId) pair can be distinguished by the
* different values of mapTaskAttemptId.
*/
default Optional<SingleFileShuffleMapOutputWriter> createSingleFileMapOutputWriter(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like a lot of indirection to implement one method... what about returning a boolean if the transfer is supported, or throwing UnsupportedOperationException (although that's a bit slower)?

(If the transfer is supported but fails you'd still throw an IOException.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer Optional to both of these, specifically because throwing an exception by default forces a try...catch on the caller, and a boolean requires a separate method call outside of the plugin tree to implement which splices the logic between the plugin tree and the UnsafeShuffleWriter.

int shuffleId,
int mapId,
long mapTaskAttemptId) throws IOException {
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.api;

import java.io.File;

import java.io.IOException;
import org.apache.spark.annotation.Private;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import grouping


/**
* Optional extension for partition writing that is optimized for transferring a single
* file to the backing store.
*/
@Private
public interface SingleFileShuffleMapOutputWriter {

/**
* Transfer a file that contains the bytes of all the splits written by this map task.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

splits -> partitions

*/
void transferMapOutputFile(File mapOutputFile, long[] partitionLengths) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.spark.shuffle.sort;

import java.nio.channels.Channels;
import java.util.Optional;
import javax.annotation.Nullable;
import java.io.*;
import java.nio.channels.FileChannel;
Expand Down Expand Up @@ -53,6 +54,7 @@
import org.apache.spark.shuffle.api.ShuffleExecutorComponents;
import org.apache.spark.shuffle.api.ShuffleMapOutputWriter;
import org.apache.spark.shuffle.api.ShufflePartitionWriter;
import org.apache.spark.shuffle.api.SingleFileShuffleMapOutputWriter;
import org.apache.spark.shuffle.api.WritableByteChannelWrapper;
import org.apache.spark.storage.BlockManager;
import org.apache.spark.unsafe.Platform;
Expand Down Expand Up @@ -215,31 +217,15 @@ void closeAndWriteOutput() throws IOException {
serOutputStream = null;
final SpillInfo[] spills = sorter.closeAndGetSpills();
sorter = null;
final ShuffleMapOutputWriter mapWriter = shuffleExecutorComponents
.createMapOutputWriter(
shuffleId,
mapId,
taskContext.taskAttemptId(),
partitioner.numPartitions());
final long[] partitionLengths;
try {
try {
partitionLengths = mergeSpills(spills, mapWriter);
} finally {
for (SpillInfo spill : spills) {
if (spill.file.exists() && !spill.file.delete()) {
logger.error("Error while deleting spill file {}", spill.file.getPath());
}
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());
}
}
mapWriter.commitAllPartitions();
} catch (Exception e) {
try {
mapWriter.abort(e);
} catch (Exception innerE) {
logger.error("Failed to abort the Map Output Writer", innerE);
}
throw e;
}
mapStatus = MapStatus$.MODULE$.apply(blockManager.shuffleServerId(), partitionLengths);
}
Expand Down Expand Up @@ -273,57 +259,93 @@ void forceSorterToSpill() throws IOException {
*
* @return the partition lengths in the merged file.
*/
private long[] mergeSpills(SpillInfo[] spills,
ShuffleMapOutputWriter mapWriter) throws IOException {
private long[] mergeSpills(SpillInfo[] spills) throws IOException {
long[] partitionLengths;
if (spills.length == 0) {
final ShuffleMapOutputWriter mapWriter = shuffleExecutorComponents
.createMapOutputWriter(
shuffleId,
mapId,
taskContext.taskAttemptId(),
partitioner.numPartitions());
mapWriter.commitAllPartitions();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you just return the value returned here instead of creating a new array?

return new long[partitioner.numPartitions()];
} else if (spills.length == 1) {
Optional<SingleFileShuffleMapOutputWriter> maybeSingleFileWriter =
shuffleExecutorComponents.createSingleFileMapOutputWriter(
shuffleId, mapId, taskContext.taskAttemptId());
if (maybeSingleFileWriter.isPresent()) {
// Here, we don't need to perform any metrics updates because the bytes written to this
// output file would have already been counted as shuffle bytes written.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment is only true for the local disk implementation. eg. if some other implementation did take advantage of the single merged file somehow, and wrote it all to a remote store, it would be doing another write.

But I am not really worried about this, as I don't think any other store will actually use this ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where could we move this?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@squito I think this SingleSpillShuffleMapOutputWriter can be pretty useful, it may avoid some byte-to-byte read/write, instead the custom store can have implementation with more performant.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dunno if there is a great alternative. We could say its the job of individual implementations to increment the metrics, and then move this comment into LocalDiskSingleSpillMapOutputWriter on why the metrics aren't incremented. But we're specifically trying to avoid exposing metrics to the api. you could also have transferMapSpillFile() return the number of bytes written, and then the existing implementation would return 0.

It all kinda feels like overkill to me. @gczsjdy I agree its possible for another store to take advantage of this, but do you have a specific case in mind? I'd like to avoid adding too many things to the api and keep things simple (with odd cases just to support the existing implementation).

partitionLengths = spills[0].partitionLengths;
maybeSingleFileWriter.get().transferMapOutputFile(spills[0].file, partitionLengths);
} else {
partitionLengths = mergeSpillsUsingStandardWriter(spills);
}
} else {
partitionLengths = mergeSpillsUsingStandardWriter(spills);
}
return partitionLengths;
}

private long[] mergeSpillsUsingStandardWriter(SpillInfo[] spills) throws IOException {
long[] partitionLengths;
final boolean compressionEnabled = (boolean) sparkConf.get(package$.MODULE$.SHUFFLE_COMPRESS());
final CompressionCodec compressionCodec = CompressionCodec$.MODULE$.createCodec(sparkConf);
final boolean fastMergeEnabled =
(boolean) sparkConf.get(package$.MODULE$.SHUFFLE_UNDAFE_FAST_MERGE_ENABLE());
(boolean) sparkConf.get(package$.MODULE$.SHUFFLE_UNDAFE_FAST_MERGE_ENABLE());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You didn't really need to touch these lines, but since you did, there's a typo in that constant's name.

final boolean fastMergeIsSupported = !compressionEnabled ||
CompressionCodec$.MODULE$.supportsConcatenationOfSerializedStreams(compressionCodec);
CompressionCodec$.MODULE$.supportsConcatenationOfSerializedStreams(compressionCodec);
final boolean encryptionEnabled = blockManager.serializerManager().encryptionEnabled();
final int numPartitions = partitioner.numPartitions();
long[] partitionLengths = new long[numPartitions];
final ShuffleMapOutputWriter mapWriter = shuffleExecutorComponents
.createMapOutputWriter(
shuffleId,
mapId,
taskContext.taskAttemptId(),
partitioner.numPartitions());
try {
if (spills.length == 0) {
return partitionLengths;
} else {
// There are multiple spills to merge, so none of these spill files' lengths were counted
// towards our shuffle write count or shuffle write time. If we use the slow merge path,
// then the final output file's size won't necessarily be equal to the sum of the spill
// files' sizes. To guard against this case, we look at the output file's actual size when
// computing shuffle bytes written.
//
// We allow the individual merge methods to report their own IO times since different merge
// strategies use different IO techniques. We count IO during merge towards the shuffle
// shuffle write time, which appears to be consistent with the "not bypassing merge-sort"
// branch in ExternalSorter.
if (fastMergeEnabled && fastMergeIsSupported) {
// Compression is disabled or we are using an IO compression codec that supports
// decompression of concatenated compressed streams, so we can perform a fast spill merge
// that doesn't need to interpret the spilled bytes.
if (transferToEnabled && !encryptionEnabled) {
logger.debug("Using transferTo-based fast merge");
partitionLengths = mergeSpillsWithTransferTo(spills, mapWriter);
} else {
logger.debug("Using fileStream-based fast merge");
partitionLengths = mergeSpillsWithFileStream(spills, mapWriter, null);
}
// There are multiple spills to merge, so none of these spill files' lengths were counted
// towards our shuffle write count or shuffle write time. If we use the slow merge path,
// then the final output file's size won't necessarily be equal to the sum of the spill
// files' sizes. To guard against this case, we look at the output file's actual size when
// computing shuffle bytes written.
//
// We allow the individual merge methods to report their own IO times since different merge
// strategies use different IO techniques. We count IO during merge towards the shuffle
// shuffle write time, which appears to be consistent with the "not bypassing merge-sort"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: shuffle shuffle (it was there before, might as well fix it now)

// branch in ExternalSorter.
if (fastMergeEnabled && fastMergeIsSupported) {
// Compression is disabled or we are using an IO compression codec that supports
// decompression of concatenated compressed streams, so we can perform a fast spill merge
// that doesn't need to interpret the spilled bytes.
if (transferToEnabled && !encryptionEnabled) {
logger.debug("Using transferTo-based fast merge");
partitionLengths = mergeSpillsWithTransferTo(spills, mapWriter);
} else {
logger.debug("Using slow merge");
partitionLengths = mergeSpillsWithFileStream(spills, mapWriter, compressionCodec);
logger.debug("Using fileStream-based fast merge");
partitionLengths = mergeSpillsWithFileStream(spills, mapWriter, null);
}
// When closing an UnsafeShuffleExternalSorter that has already spilled once but also has
// in-memory records, we write out the in-memory records to a file but do not count that
// final write as bytes spilled (instead, it's accounted as shuffle write). The merge needs
// to be counted as shuffle write, but this will lead to double-counting of the final
// SpillInfo's bytes.
writeMetrics.decBytesWritten(spills[spills.length - 1].file.length());
return partitionLengths;
} else {
logger.debug("Using slow merge");
partitionLengths = mergeSpillsWithFileStream(spills, mapWriter, compressionCodec);
}
// When closing an UnsafeShuffleExternalSorter that has already spilled once but also has
// in-memory records, we write out the in-memory records to a file but do not count that
// final write as bytes spilled (instead, it's accounted as shuffle write). The merge needs
// to be counted as shuffle write, but this will lead to double-counting of the final
// SpillInfo's bytes.
writeMetrics.decBytesWritten(spills[spills.length - 1].file.length());
mapWriter.commitAllPartitions();
} catch (Exception e) {
try {
mapWriter.abort(e);
} catch (Exception e2) {
logger.warn("Failed to abort writing the map output.", e2);
e.addSuppressed(e2);
}
} catch (IOException e) {
throw e;
}
return partitionLengths;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@

package org.apache.spark.shuffle.sort.io;

import java.util.Optional;

import com.google.common.annotations.VisibleForTesting;

import org.apache.spark.SparkConf;
import org.apache.spark.SparkEnv;
import org.apache.spark.shuffle.api.ShuffleExecutorComponents;
import org.apache.spark.shuffle.api.ShuffleMapOutputWriter;
import org.apache.spark.shuffle.IndexShuffleBlockResolver;
import org.apache.spark.shuffle.api.SingleFileShuffleMapOutputWriter;
import org.apache.spark.storage.BlockManager;

public class LocalDiskShuffleExecutorComponents implements ShuffleExecutorComponents {
Expand Down Expand Up @@ -68,4 +71,16 @@ public ShuffleMapOutputWriter createMapOutputWriter(
return new LocalDiskShuffleMapOutputWriter(
shuffleId, mapId, numPartitions, blockResolver, sparkConf);
}

@Override
public Optional<SingleFileShuffleMapOutputWriter> createSingleFileMapOutputWriter(
int shuffleId,
int mapId,
long mapTaskAttemptId) {
if (blockResolver == null) {
throw new IllegalStateException(
"Executor components must be initialized before getting writers.");
}
return Optional.of(new LocalDiskSingleFileMapOutputWriter(shuffleId, mapId, blockResolver));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public ShufflePartitionWriter getPartitionWriter(int reducePartitionId) throws I
}
return new LocalDiskShufflePartitionWriter(reducePartitionId);
}

@Override
public void commitAllPartitions() throws IOException {
// Check the position after transferTo loop to see if it is in the right position and raise a
Expand Down Expand Up @@ -137,8 +138,6 @@ private void cleanUp() throws IOException {

private void initStream() throws IOException {
if (outputFileStream == null) {
// This file needs to opened in append mode in order to work around a Linux kernel bug that
// affects transferTo; see SPARK-3948 for more details.
outputFileStream = new FileOutputStream(outputTempFile, true);
}
if (outputBufferedFileStream == null) {
Expand All @@ -147,11 +146,10 @@ private void initStream() throws IOException {
}

private void initChannel() throws IOException {
if (outputFileStream == null) {
outputFileStream = new FileOutputStream(outputTempFile, true);
}
// This file needs to opened in append mode in order to work around a Linux kernel bug that
// affects transferTo; see SPARK-3948 for more details.
if (outputFileChannel == null) {
outputFileChannel = outputFileStream.getChannel();
outputFileChannel = new FileOutputStream(outputTempFile, true).getChannel();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.io;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import org.apache.spark.shuffle.IndexShuffleBlockResolver;
import org.apache.spark.shuffle.api.SingleFileShuffleMapOutputWriter;
import org.apache.spark.util.Utils;

public class LocalDiskSingleFileMapOutputWriter
implements SingleFileShuffleMapOutputWriter {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry I know this was my naming suggestion earlier, but after more thought, how about File -> Spill?
LocalDiskSingleSpillMapOutputWriter
SingleSpillShuffleMapOutputWriter


private final int shuffleId;
private final int mapId;
private final IndexShuffleBlockResolver blockResolver;

public LocalDiskSingleFileMapOutputWriter(
int shuffleId,
int mapId,
IndexShuffleBlockResolver blockResolver) {
this.shuffleId = shuffleId;
this.mapId = mapId;
this.blockResolver = blockResolver;
}

@Override
public void transferMapOutputFile(
File mapOutputFile,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and maybe rename this to mapSpillFile

long[] partitionLengths) throws IOException {
File outputFile = blockResolver.getDataFile(shuffleId, mapId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a brief comment here would help, eg. "we've only got one spill file, which is already in the right format of the final data file. So no merging to do, just move it to the right location"

File tempFile = Utils.tempFileWith(outputFile);
Files.move(mapOutputFile.toPath(), tempFile.toPath());
blockResolver.writeIndexFileAndCommit(shuffleId, mapId, partitionLengths, tempFile);
}
}