Skip to content

Commit

Permalink
[SPARK-50853][CORE] Close temp shuffle file writable channel
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

Currently, there are two implementations of DownloadFileWritableChannel (which is used for writing data fetched to disk), SimpleDownloadWritableChannel and EncryptedDownloadWritableChannel. The latter closes the writable channel in it's implementation of closeAndRead method while the former does not. As a result, SimpleDownloadWritableChannel channel is never closed and is relying on either the finalizer in FileOutputStream or the phantom cleanable in FileDescriptor to close the file descriptor. The change in this PR is to close the channel in SimpleDownloadWritableChannel closeAndRead method.

### Why are the changes needed?

Should be closing file handles when they are not needed anymore instead of relying on finalizer/cleanables to do it.

### Does this PR introduce _any_ user-facing change?

No

### How was this patch tested?

Existing spark tests.

### Was this patch authored or co-authored using generative AI tooling?

No

Closes #49531 from ChenMichael/SPARK-50853-close-temp-shuffle-file-channel.

Authored-by: Michael Chen <[email protected]>
Signed-off-by: Dongjoon Hyun <[email protected]>
  • Loading branch information
Michael Chen authored and dongjoon-hyun committed Jan 24, 2025
1 parent 1569c8a commit 4e3b831
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@

import org.apache.spark.network.buffer.ManagedBuffer;

import java.io.IOException;
import java.nio.channels.WritableByteChannel;

/**
* A channel for writing data which is fetched to disk, which allows access to the written data only
* after the writer has been closed. Used with DownloadFile and DownloadFileManager.
*/
public interface DownloadFileWritableChannel extends WritableByteChannel {
ManagedBuffer closeAndRead();
ManagedBuffer closeAndRead() throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ private class SimpleDownloadWritableChannel implements DownloadFileWritableChann
}

@Override
public ManagedBuffer closeAndRead() {
public ManagedBuffer closeAndRead() throws IOException {
channel.close();
return new FileSegmentManagedBuffer(transportConf, file, 0, file.length());
}

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

import org.apache.spark.network.util.MapConfigProvider;
import org.apache.spark.network.util.TransportConf;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;

import org.junit.jupiter.api.Assertions;

public class SimpleDownloadFileSuite {
@Test
public void testChannelIsClosedAfterCloseAndRead() throws IOException {
File tempFile = File.createTempFile("testChannelIsClosed", ".tmp");
tempFile.deleteOnExit();
TransportConf conf = new TransportConf("test", MapConfigProvider.EMPTY);

DownloadFile downloadFile = null;
try {
downloadFile = new SimpleDownloadFile(tempFile, conf);
DownloadFileWritableChannel channel = downloadFile.openForWriting();
channel.closeAndRead();
Assertions.assertFalse(channel.isOpen(), "Channel should be closed after closeAndRead.");
} finally {
if (downloadFile != null) {
downloadFile.delete();
}
}
}
}

0 comments on commit 4e3b831

Please sign in to comment.