Skip to content

Commit 4e3b831

Browse files
Michael Chendongjoon-hyun
authored andcommitted
[SPARK-50853][CORE] Close temp shuffle file writable channel
### 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]>
1 parent 1569c8a commit 4e3b831

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

common/network-shuffle/src/main/java/org/apache/spark/network/shuffle/DownloadFileWritableChannel.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919

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

22+
import java.io.IOException;
2223
import java.nio.channels.WritableByteChannel;
2324

2425
/**
2526
* A channel for writing data which is fetched to disk, which allows access to the written data only
2627
* after the writer has been closed. Used with DownloadFile and DownloadFileManager.
2728
*/
2829
public interface DownloadFileWritableChannel extends WritableByteChannel {
29-
ManagedBuffer closeAndRead();
30+
ManagedBuffer closeAndRead() throws IOException;
3031
}

common/network-shuffle/src/main/java/org/apache/spark/network/shuffle/SimpleDownloadFile.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ private class SimpleDownloadWritableChannel implements DownloadFileWritableChann
6969
}
7070

7171
@Override
72-
public ManagedBuffer closeAndRead() {
72+
public ManagedBuffer closeAndRead() throws IOException {
73+
channel.close();
7374
return new FileSegmentManagedBuffer(transportConf, file, 0, file.length());
7475
}
7576

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.network.shuffle;
19+
20+
import org.apache.spark.network.util.MapConfigProvider;
21+
import org.apache.spark.network.util.TransportConf;
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.io.File;
25+
import java.io.IOException;
26+
27+
import org.junit.jupiter.api.Assertions;
28+
29+
public class SimpleDownloadFileSuite {
30+
@Test
31+
public void testChannelIsClosedAfterCloseAndRead() throws IOException {
32+
File tempFile = File.createTempFile("testChannelIsClosed", ".tmp");
33+
tempFile.deleteOnExit();
34+
TransportConf conf = new TransportConf("test", MapConfigProvider.EMPTY);
35+
36+
DownloadFile downloadFile = null;
37+
try {
38+
downloadFile = new SimpleDownloadFile(tempFile, conf);
39+
DownloadFileWritableChannel channel = downloadFile.openForWriting();
40+
channel.closeAndRead();
41+
Assertions.assertFalse(channel.isOpen(), "Channel should be closed after closeAndRead.");
42+
} finally {
43+
if (downloadFile != null) {
44+
downloadFile.delete();
45+
}
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)