-
Notifications
You must be signed in to change notification settings - Fork 6
[SPARK-25299] Implement default version of the API for shuffle writes #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 16 commits
7160ce3
460f0ea
96d1774
64fb327
996e903
3b9d33c
1f1c159
0737515
1ded83d
3353155
7a79bd9
9e3f05c
9f6230b
14df750
8cf80f7
46a0174
1325903
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
|
|
||
| package org.apache.spark.api.shuffle; | ||
|
|
||
| import java.io.Closeable; | ||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
| import java.nio.channels.Channels; | ||
|
|
@@ -31,12 +32,43 @@ | |
| * @since 3.0.0 | ||
| */ | ||
| @Experimental | ||
| public interface ShufflePartitionWriter { | ||
| OutputStream openStream() throws IOException; | ||
| public interface ShufflePartitionWriter extends Closeable { | ||
|
|
||
| long getLength(); | ||
| /** | ||
| * Returns an underlying {@link OutputStream} that can write bytes to the underlying data store. | ||
| * <p> | ||
| * Note that this stream itself is not closed by the caller; close the stream in | ||
| * the implementation of this class's {@link #close()}.. | ||
| */ | ||
| OutputStream toStream() throws IOException; | ||
|
|
||
| default WritableByteChannel openChannel() throws IOException { | ||
| return Channels.newChannel(openStream()); | ||
| /** | ||
| * Returns an underlying {@link WritableByteChannel} that can write bytes to the underlying data | ||
| * store. | ||
| * <p> | ||
| * Note that this channel itself is not closed by the caller; close the stream in | ||
| * the implementation of this class's {@link #close()}.. | ||
| */ | ||
| default WritableByteChannel toChannel() throws IOException { | ||
| return Channels.newChannel(toStream()); | ||
| } | ||
|
|
||
| /** | ||
| * Get the number of bytes written by this writer's stream returned by {@link #toStream()} or | ||
| * the channel returned by {@link #toChannel()}. | ||
| */ | ||
| long getNumBytesWritten(); | ||
|
|
||
| /** | ||
| * Close all resources created by this ShufflePartitionWriter, via calls to {@link #toStream()} | ||
| * or {@link #toChannel()}. | ||
| * <p> | ||
| * This must always close any stream returned by {@link #toStream()}. | ||
| * <p> | ||
| * Note that the default version of {@link #toChannel()} returns a {@link WritableByteChannel} | ||
| * that does not itself need to be closed up front; only the underlying output stream given by | ||
| * {@link #toStream()} must be closed. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm looking at the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can cross that bridge when we get there - might be something to check for future versions of Java. |
||
| */ | ||
| @Override | ||
| void close() throws IOException; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,10 +19,13 @@ | |
|
|
||
| import java.io.File; | ||
| import java.io.FileInputStream; | ||
| import java.io.FileOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
| import java.nio.channels.FileChannel; | ||
| import java.nio.channels.WritableByteChannel; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| import org.apache.spark.api.shuffle.ShufflePartitionWriter; | ||
| import scala.None$; | ||
| import scala.Option; | ||
| import scala.Product2; | ||
|
|
@@ -34,6 +37,8 @@ | |
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import org.apache.spark.api.shuffle.ShuffleMapOutputWriter; | ||
| import org.apache.spark.api.shuffle.ShuffleWriteSupport; | ||
| import org.apache.spark.internal.config.package$; | ||
| import org.apache.spark.Partitioner; | ||
| import org.apache.spark.ShuffleDependency; | ||
|
|
@@ -82,6 +87,7 @@ final class BypassMergeSortShuffleWriter<K, V> extends ShuffleWriter<K, V> { | |
| private final int shuffleId; | ||
| private final int mapId; | ||
| private final Serializer serializer; | ||
| private final ShuffleWriteSupport shuffleWriteSupport; | ||
| private final IndexShuffleBlockResolver shuffleBlockResolver; | ||
|
|
||
| /** Array of file writers, one for each partition */ | ||
|
|
@@ -103,7 +109,8 @@ final class BypassMergeSortShuffleWriter<K, V> extends ShuffleWriter<K, V> { | |
| BypassMergeSortShuffleHandle<K, V> handle, | ||
| int mapId, | ||
| SparkConf conf, | ||
| ShuffleWriteMetricsReporter writeMetrics) { | ||
| ShuffleWriteMetricsReporter writeMetrics, | ||
| ShuffleWriteSupport shuffleWriteSupport) { | ||
| // Use getSizeAsKb (not bytes) to maintain backwards compatibility if no units are provided | ||
| this.fileBufferSize = (int) (long) conf.get(package$.MODULE$.SHUFFLE_FILE_BUFFER_SIZE()) * 1024; | ||
| this.transferToEnabled = conf.getBoolean("spark.file.transferTo", true); | ||
|
|
@@ -116,57 +123,61 @@ final class BypassMergeSortShuffleWriter<K, V> extends ShuffleWriter<K, V> { | |
| this.writeMetrics = writeMetrics; | ||
| this.serializer = dep.serializer(); | ||
| this.shuffleBlockResolver = shuffleBlockResolver; | ||
| this.shuffleWriteSupport = shuffleWriteSupport; | ||
| } | ||
|
|
||
| @Override | ||
| public void write(Iterator<Product2<K, V>> records) throws IOException { | ||
| assert (partitionWriters == null); | ||
| if (!records.hasNext()) { | ||
| partitionLengths = new long[numPartitions]; | ||
| shuffleBlockResolver.writeIndexFileAndCommit(shuffleId, mapId, partitionLengths, null); | ||
| mapStatus = MapStatus$.MODULE$.apply(blockManager.shuffleServerId(), partitionLengths); | ||
| return; | ||
| } | ||
| final SerializerInstance serInstance = serializer.newInstance(); | ||
| final long openStartTime = System.nanoTime(); | ||
| partitionWriters = new DiskBlockObjectWriter[numPartitions]; | ||
| partitionWriterSegments = new FileSegment[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); | ||
| } | ||
| // 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.incWriteTime(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()); | ||
| } | ||
| ShuffleMapOutputWriter mapOutputWriter = shuffleWriteSupport | ||
| .createMapOutputWriter(shuffleId, mapId, numPartitions); | ||
| try { | ||
| if (!records.hasNext()) { | ||
| partitionLengths = new long[numPartitions]; | ||
| mapOutputWriter.commitAllPartitions(); | ||
| mapStatus = MapStatus$.MODULE$.apply(blockManager.shuffleServerId(), partitionLengths); | ||
| return; | ||
| } | ||
| final SerializerInstance serInstance = serializer.newInstance(); | ||
| final long openStartTime = System.nanoTime(); | ||
| partitionWriters = new DiskBlockObjectWriter[numPartitions]; | ||
| partitionWriterSegments = new FileSegment[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); | ||
| } | ||
| // 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.incWriteTime(System.nanoTime() - openStartTime); | ||
|
|
||
| for (int i = 0; i < numPartitions; i++) { | ||
| try (DiskBlockObjectWriter writer = partitionWriters[i]) { | ||
| partitionWriterSegments[i] = writer.commitAndGet(); | ||
| while (records.hasNext()) { | ||
| final Product2<K, V> record = records.next(); | ||
| final K key = record._1(); | ||
| partitionWriters[partitioner.getPartition(key)].write(key, record._2()); | ||
| } | ||
| } | ||
|
|
||
| File output = shuffleBlockResolver.getDataFile(shuffleId, mapId); | ||
| File tmp = Utils.tempFileWith(output); | ||
| try { | ||
| partitionLengths = writePartitionedFile(tmp); | ||
| shuffleBlockResolver.writeIndexFileAndCommit(shuffleId, mapId, partitionLengths, tmp); | ||
| } finally { | ||
| if (tmp.exists() && !tmp.delete()) { | ||
| logger.error("Error while deleting temp file {}", tmp.getAbsolutePath()); | ||
| for (int i = 0; i < numPartitions; i++) { | ||
| try (DiskBlockObjectWriter writer = partitionWriters[i]) { | ||
| partitionWriterSegments[i] = writer.commitAndGet(); | ||
| } | ||
| } | ||
|
|
||
| partitionLengths = writePartitionedData(mapOutputWriter); | ||
| mapOutputWriter.commitAllPartitions(); | ||
| mapStatus = MapStatus$.MODULE$.apply(blockManager.shuffleServerId(), partitionLengths); | ||
| } catch (Exception e) { | ||
| try { | ||
| mapOutputWriter.abort(e); | ||
| } catch (Exception e2) { | ||
| logger.error("Failed to abort the writer after failing to write map output.", e2); | ||
| } | ||
| throw e; | ||
| } | ||
| mapStatus = MapStatus$.MODULE$.apply(blockManager.shuffleServerId(), partitionLengths); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
|
|
@@ -179,37 +190,51 @@ long[] getPartitionLengths() { | |
| * | ||
| * @return array of lengths, in bytes, of each partition of the file (used by map output tracker). | ||
| */ | ||
| private long[] writePartitionedFile(File outputFile) throws IOException { | ||
| private long[] writePartitionedData(ShuffleMapOutputWriter mapOutputWriter) throws IOException { | ||
| // Track location of the partition starts in the output file | ||
| final long[] lengths = new long[numPartitions]; | ||
| if (partitionWriters == null) { | ||
| // We were passed an empty iterator | ||
| return lengths; | ||
| } | ||
|
|
||
| final FileOutputStream out = new FileOutputStream(outputFile, true); | ||
| final long writeStartTime = System.nanoTime(); | ||
| boolean threwException = true; | ||
| try { | ||
| for (int i = 0; i < numPartitions; i++) { | ||
| final File file = partitionWriterSegments[i].file(); | ||
| if (file.exists()) { | ||
| final FileInputStream in = new FileInputStream(file); | ||
| boolean copyThrewException = true; | ||
| try { | ||
| lengths[i] = Utils.copyStream(in, out, false, transferToEnabled); | ||
| copyThrewException = false; | ||
| } finally { | ||
| Closeables.close(in, copyThrewException); | ||
| if (!file.exists()) continue; | ||
| boolean copyThrewException = true; | ||
| ShufflePartitionWriter writer = null; | ||
| try { | ||
ifilonenko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| writer = mapOutputWriter.getNextPartitionWriter(); | ||
| if (transferToEnabled) { | ||
| WritableByteChannel outputChannel = writer.toChannel(); | ||
| FileInputStream in = new FileInputStream(file); | ||
| try (FileChannel inputChannel = in.getChannel()) { | ||
| Utils.copyFileStreamNIO(inputChannel, outputChannel, 0, inputChannel.size()); | ||
| copyThrewException = false; | ||
| } finally { | ||
| Closeables.close(in, copyThrewException); | ||
| } | ||
| } else { | ||
| OutputStream tempOutputStream = writer.toStream(); | ||
| FileInputStream in = new FileInputStream(file); | ||
| try { | ||
| Utils.copyStream(in, tempOutputStream, false, false); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, on second thought, you probably just want to fork the code paths here into a "transferTo" block and a "outputstreams" block to allow more flexibility in the plugins to return whatever outputstreams they want. |
||
| copyThrewException = false; | ||
| } finally { | ||
| Closeables.close(in, copyThrewException); | ||
| } | ||
| } | ||
| if (!file.delete()) { | ||
| logger.error("Unable to delete file for partition {}", i); | ||
| } | ||
| } finally { | ||
| Closeables.close(writer, copyThrewException); | ||
| } | ||
|
|
||
| lengths[i] = writer.getNumBytesWritten(); | ||
| } | ||
| threwException = false; | ||
| } finally { | ||
| Closeables.close(out, threwException); | ||
| writeMetrics.incWriteTime(System.nanoTime() - writeStartTime); | ||
| } | ||
| partitionWriters = null; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * 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 org.apache.spark.SparkConf; | ||
| import org.apache.spark.api.shuffle.ShuffleExecutorComponents; | ||
| import org.apache.spark.api.shuffle.ShuffleDataIO; | ||
|
|
||
| public class DefaultShuffleDataIO implements ShuffleDataIO { | ||
|
|
||
| private final SparkConf sparkConf; | ||
|
|
||
| public DefaultShuffleDataIO(SparkConf sparkConf) { | ||
| this.sparkConf = sparkConf; | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public ShuffleExecutorComponents executor() { | ||
| return new DefaultShuffleExecutorComponents(sparkConf); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* | ||
| * 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 org.apache.spark.SparkConf; | ||
| import org.apache.spark.SparkEnv; | ||
| import org.apache.spark.api.shuffle.ShuffleExecutorComponents; | ||
| import org.apache.spark.api.shuffle.ShuffleWriteSupport; | ||
| import org.apache.spark.shuffle.IndexShuffleBlockResolver; | ||
| import org.apache.spark.storage.BlockManager; | ||
|
|
||
| public class DefaultShuffleExecutorComponents implements ShuffleExecutorComponents { | ||
|
|
||
| private final SparkConf sparkConf; | ||
| private BlockManager blockManager; | ||
| private IndexShuffleBlockResolver blockResolver; | ||
|
|
||
| public DefaultShuffleExecutorComponents(SparkConf sparkConf) { | ||
| this.sparkConf = sparkConf; | ||
| } | ||
|
|
||
| @Override | ||
| public void intitializeExecutor(String appId, String execId) { | ||
| blockManager = SparkEnv.get().blockManager(); | ||
| blockResolver = new IndexShuffleBlockResolver(sparkConf, blockManager); | ||
| } | ||
|
|
||
| @Override | ||
| public ShuffleWriteSupport writes() { | ||
| if (blockResolver == null) { | ||
| throw new IllegalStateException( | ||
| "Executor components must be initialized before getting writers."); | ||
| } | ||
| return new DefaultShuffleWriteSupport(sparkConf, blockResolver); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.