-
Notifications
You must be signed in to change notification settings - Fork 6
Various changes to the API and state assumptions in writers. #8
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 all commits
0445566
dd4656c
d032041
5a62c67
d21feb2
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 closeAndGetLength(); | ||
| /** | ||
| * 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. nit: Do we need to mention this in the API doc? Seems like a comment we can add to the implementation class.
Author
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. "default implementation" here refers to the |
||
| */ | ||
| @Override | ||
| void close() throws IOException; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,7 +73,7 @@ public DefaultShuffleMapOutputWriter( | |
| } | ||
|
|
||
| @Override | ||
| public ShufflePartitionWriter getNextPartitionWriter() throws IOException { | ||
| public ShufflePartitionWriter getNextPartitionWriter() { | ||
| return new DefaultShufflePartitionWriter(currPartitionId++); | ||
| } | ||
|
|
||
|
|
@@ -97,7 +97,7 @@ public void commitAllPartitions() throws IOException { | |
| } | ||
|
|
||
| @Override | ||
| public void abort(Throwable error) throws IOException { | ||
| public void abort(Throwable error) { | ||
| try { | ||
| cleanUp(); | ||
| } catch (Exception e) { | ||
|
|
@@ -107,7 +107,7 @@ public void abort(Throwable error) throws IOException { | |
| log.warn("Failed to delete temporary shuffle file at {}", outputTempFile.getAbsolutePath()); | ||
| } | ||
| if (!outputFile.delete() && outputFile.exists()) { | ||
| log.warn("Failed to delete outputshuffle file at {}", outputFile.getAbsolutePath()); | ||
| log.warn("Failed to delete output shuffle file at {}", outputFile.getAbsolutePath()); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -154,42 +154,42 @@ private DefaultShufflePartitionWriter(int partitionId) { | |
| } | ||
|
|
||
| @Override | ||
| public OutputStream openStream() throws IOException { | ||
| public OutputStream toStream() throws IOException { | ||
| initStream(); | ||
| stream = new PartitionWriterStream(); | ||
| return stream; | ||
| } | ||
|
|
||
| @Override | ||
| public long closeAndGetLength() { | ||
| public FileChannel toChannel() throws IOException { | ||
| initChannel(); | ||
| currChannelPosition = outputFileChannel.position(); | ||
| return outputFileChannel; | ||
| } | ||
|
|
||
| @Override | ||
| public long getNumBytesWritten() { | ||
| if (outputFileChannel != null && stream == null) { | ||
| try { | ||
| long newPosition = outputFileChannel.position(); | ||
| long length = newPosition - currChannelPosition; | ||
| partitionLengths[partitionId] = length; | ||
| currChannelPosition = newPosition; | ||
| return length; | ||
| return newPosition - currChannelPosition; | ||
| } catch (Exception e) { | ||
| log.error("The currPartition is: " + partitionId, e); | ||
| throw new IllegalStateException("Attempting to calculate position of file channel", e); | ||
| log.error("The currPartition is: {}", partitionId, e); | ||
| throw new IllegalStateException("Failed to calculate position of file channel", e); | ||
| } | ||
| } else if (stream != null) { | ||
| return stream.getCount(); | ||
|
Author
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. Observe that
Collaborator
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. Well, you are calling
Author
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. Not necessarily - see |
||
| } else { | ||
| try { | ||
| stream.close(); | ||
| } catch (Exception e) { | ||
| throw new IllegalStateException("Attempting to close output stream", e); | ||
| } | ||
| int length = stream.getCount(); | ||
| partitionLengths[partitionId] = length; | ||
| return length; | ||
| return 0; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public FileChannel openChannel() throws IOException { | ||
| initChannel(); | ||
| currChannelPosition = outputFileChannel.position(); | ||
| return outputFileChannel; | ||
| public void close() throws IOException { | ||
|
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 think it might be good to keep track of the |
||
| if (stream != null) { | ||
| stream.close(); | ||
| } | ||
| partitionLengths[partitionId] = getNumBytesWritten(); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -218,7 +218,9 @@ public void close() throws IOException { | |
|
|
||
| @Override | ||
| public void flush() throws IOException { | ||
| outputBufferedFileStream.flush(); | ||
| if (!isClosed) { | ||
| outputBufferedFileStream.flush(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed from
openStream->toStreamin order to better indicate that this writer is still responsible for closing its own resources. Meaning, "convert this writer to a stream", in a sense, rather than, "Open a stream to write contents". But there might be a better naming convention here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see how the conversion logic can be advantageous. +1