-
Notifications
You must be signed in to change notification settings - Fork 476
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
5165-resource leaks in S3AccessIO calls #5305
Merged
kcondon
merged 4 commits into
IQSS:develop
from
TexasDigitalLibrary:5165-reource_leaks_in_S3AccessIO
Nov 14, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
import com.amazonaws.services.s3.model.DeleteObjectsRequest; | ||
import com.amazonaws.services.s3.model.DeleteObjectsRequest.KeyVersion; | ||
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest; | ||
import com.amazonaws.services.s3.model.GetObjectMetadataRequest; | ||
import com.amazonaws.services.s3.model.GetObjectRequest; | ||
import com.amazonaws.services.s3.model.ListObjectsRequest; | ||
import com.amazonaws.services.s3.model.ObjectListing; | ||
|
@@ -35,6 +36,7 @@ | |
import java.net.URLEncoder; | ||
import java.nio.channels.Channel; | ||
import java.nio.channels.Channels; | ||
import java.nio.channels.ReadableByteChannel; | ||
import java.nio.channels.WritableByteChannel; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
@@ -157,22 +159,13 @@ public void open(DataAccessOption... options) throws IOException { | |
|
||
if (isReadAccess) { | ||
key = getMainFileKey(); | ||
S3Object s3object = null; | ||
ObjectMetadata objectMetadata = null; | ||
try { | ||
s3object = s3.getObject(new GetObjectRequest(bucketName, key)); | ||
objectMetadata = s3.getObjectMetadata(bucketName, key); | ||
} catch (SdkClientException sce) { | ||
throw new IOException("Cannot get S3 object " + key + " ("+sce.getMessage()+")"); | ||
} | ||
InputStream in = s3object.getObjectContent(); | ||
|
||
if (in == null) { | ||
throw new IOException("Cannot get InputStream for S3 Object" + key); | ||
} | ||
|
||
this.setInputStream(in); | ||
|
||
setChannel(Channels.newChannel(in)); | ||
this.setSize(s3object.getObjectMetadata().getContentLength()); | ||
this.setSize(objectMetadata.getContentLength()); | ||
|
||
if (dataFile.getContentType() != null | ||
&& dataFile.getContentType().equals("text/tab-separated-values") | ||
|
@@ -215,6 +208,40 @@ public void open(DataAccessOption... options) throws IOException { | |
} | ||
} | ||
|
||
@Override | ||
public InputStream getInputStream() throws IOException { | ||
if(super.getInputStream()==null) { | ||
try { | ||
setInputStream(s3.getObject(new GetObjectRequest(bucketName, key)).getObjectContent()); | ||
} catch (SdkClientException sce) { | ||
throw new IOException("Cannot get S3 object " + key + " ("+sce.getMessage()+")"); | ||
} | ||
} | ||
|
||
if (super.getInputStream() == null) { | ||
throw new IOException("Cannot get InputStream for S3 Object" + key); | ||
} | ||
|
||
setChannel(Channels.newChannel(super.getInputStream())); | ||
|
||
return super.getInputStream(); | ||
} | ||
|
||
@Override | ||
public Channel getChannel() 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. Should this 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. Yep... |
||
if(super.getChannel()==null) { | ||
getInputStream(); | ||
} | ||
return channel; | ||
} | ||
|
||
@Override | ||
public ReadableByteChannel getReadChannel() throws IOException { | ||
//Make sure StorageIO.channel variable exists | ||
getChannel(); | ||
return super.getReadChannel(); | ||
} | ||
|
||
// StorageIO method for copying a local Path (for ex., a temp file), into this DataAccess location: | ||
@Override | ||
public void savePath(Path fileSystemPath) throws IOException { | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not sure if it applies here or not, but in past leak hunting removed several
if (foo.getInputStream() != null)
type statements, where the anonymous open statements appeared to be correlated with accumulating open file descriptors.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.
@pameyer - I think it should be OK in this case since the StorageIO class caches the InputStream - repeated calls to it don't create more streams as it would if you were directly opening a file stream, etc.
FWIW - In checking on this, I did find that that the code retrieved the channel, created from a stream, in order to create a new stream, rather than just retrieving the stream directly. Added a fix as a new commit.