-
Notifications
You must be signed in to change notification settings - Fork 28.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-4307] Initialize FileDescriptor lazily in FileRegion.
Netty's DefaultFileRegion requires a FileDescriptor in its constructor, which means we need to have a opened file handle. In super large workloads, this could lead to too many open files due to the way these file descriptors are cleaned. This pull request creates a new LazyFileRegion that initializes the FileDescriptor when we are sending data for the first time. Author: Reynold Xin <rxin@databricks.com> Author: Reynold Xin <rxin@apache.org> Closes #3172 from rxin/lazyFD and squashes the following commits: 0bdcdc6 [Reynold Xin] Added reference to Netty's DefaultFileRegion d4564ae [Reynold Xin] Added SparkConf to the ctor argument of IndexShuffleBlockManager. 6ed369e [Reynold Xin] Code review feedback. 04cddc8 [Reynold Xin] [SPARK-4307] Initialize FileDescriptor lazily in FileRegion. (cherry picked from commit ef29a9a) Signed-off-by: Aaron Davidson <aaron@databricks.com>
- Loading branch information
Showing
16 changed files
with
191 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
network/common/src/main/java/org/apache/spark/network/buffer/LazyFileRegion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* 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.buffer; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.channels.FileChannel; | ||
import java.nio.channels.WritableByteChannel; | ||
|
||
import com.google.common.base.Objects; | ||
import io.netty.channel.FileRegion; | ||
import io.netty.util.AbstractReferenceCounted; | ||
|
||
import org.apache.spark.network.util.JavaUtils; | ||
|
||
/** | ||
* A FileRegion implementation that only creates the file descriptor when the region is being | ||
* transferred. This cannot be used with Epoll because there is no native support for it. | ||
* | ||
* This is mostly copied from DefaultFileRegion implementation in Netty. In the future, we | ||
* should push this into Netty so the native Epoll transport can support this feature. | ||
*/ | ||
public final class LazyFileRegion extends AbstractReferenceCounted implements FileRegion { | ||
|
||
private final File file; | ||
private final long position; | ||
private final long count; | ||
|
||
private FileChannel channel; | ||
|
||
private long numBytesTransferred = 0L; | ||
|
||
/** | ||
* @param file file to transfer. | ||
* @param position start position for the transfer. | ||
* @param count number of bytes to transfer starting from position. | ||
*/ | ||
public LazyFileRegion(File file, long position, long count) { | ||
this.file = file; | ||
this.position = position; | ||
this.count = count; | ||
} | ||
|
||
@Override | ||
protected void deallocate() { | ||
JavaUtils.closeQuietly(channel); | ||
} | ||
|
||
@Override | ||
public long position() { | ||
return position; | ||
} | ||
|
||
@Override | ||
public long transfered() { | ||
return numBytesTransferred; | ||
} | ||
|
||
@Override | ||
public long count() { | ||
return count; | ||
} | ||
|
||
@Override | ||
public long transferTo(WritableByteChannel target, long position) throws IOException { | ||
if (channel == null) { | ||
channel = new FileInputStream(file).getChannel(); | ||
} | ||
|
||
long count = this.count - position; | ||
if (count < 0 || position < 0) { | ||
throw new IllegalArgumentException( | ||
"position out of range: " + position + " (expected: 0 - " + (count - 1) + ')'); | ||
} | ||
|
||
if (count == 0) { | ||
return 0L; | ||
} | ||
|
||
long written = channel.transferTo(this.position + position, count, target); | ||
if (written > 0) { | ||
numBytesTransferred += written; | ||
} | ||
return written; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return Objects.toStringHelper(this) | ||
.add("file", file) | ||
.add("position", position) | ||
.add("count", count) | ||
.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.