Skip to content

AVRO-1985: Unreleased resources#177

Closed
gszadovszky wants to merge 5 commits intoapache:masterfrom
gszadovszky:fortify-fixes
Closed

AVRO-1985: Unreleased resources#177
gszadovszky wants to merge 5 commits intoapache:masterfrom
gszadovszky:fortify-fixes

Conversation

@gszadovszky
Copy link
Contributor

Fixes for some unreleased resource issues found by static code analyzer tool.

@gszadovszky gszadovszky changed the title Unreleased resource fixes AVRO-1983: Unreleased resources Jan 13, 2017
@gszadovszky gszadovszky changed the title AVRO-1983: Unreleased resources AVRO-1985: Unreleased resources Jan 18, 2017
} finally {
if (reader != null)
reader.close();
in.close();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the change to close the passed SeekableInput? Shouldn't that be up to the caller?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, it should be closed by the caller but it is already implemented this way as we are closing the reader which closes the SeekableInput as well. I did not want to break backward compatibility but fix the actual situation that the resources are not closed every time.
That's why I've tried to highlight in the method comment that this method closes the specified SeekableInput.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we update DataFileReader to properly not close the passed input stream instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can but it would be a backward incompatible change as this method is public.
If we would like to follow the usual pattern for this case we would need another method which does not close it and deprecate this one then remove this one in the next major release.
Or is it enough marking this JIRA with "incompatible change" and change as you are suggesting?

if (args.size() == 2 && ! "-".equals(args.get(1))) {
parseOut = new PrintStream(new FileOutputStream(args.get(1)));
}
if (args.size() == 2 && !"-".equals(args.get(1))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like these indents are incorrect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems you're right. I'll check it out.

for (int i = in.read(buffer); i != -1; i = in.read(buffer))
System.err.write(buffer, 0, i);
} finally {
in.close();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we closing in within this method instead of having the caller do it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is a private method and it seemed to be easier and shorter to close the stream inside this method instead of the public caller. Do you think I should rewrite it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah please have the caller close it instead. we should be internally disciplined.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. On the way...

@gszadovszky
Copy link
Contributor Author

@busbey, could you check my answers, please?

} finally {
if (reader != null)
reader.close();
in.close();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we update DataFileReader to properly not close the passed input stream instead?

for (int i = in.read(buffer); i != -1; i = in.read(buffer))
System.err.write(buffer, 0, i);
} finally {
in.close();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah please have the caller close it instead. we should be internally disciplined.

@gszadovszky
Copy link
Contributor Author

@busbey, could you please check the latest changes?

return appendTo(new SeekableFileInput(file),
new SyncableFileOutputStream(file, true));
SeekableInput input = new SeekableFileInput(file);
OutputStream output = new SyncableFileOutputStream(file, true);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line should be inside the try block as well to guarantee that input is always closed. Or, alternatively, it may be before the first line if order does not matter.

try {
return appendTo(input, output);
} finally {
input.close();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a comment about why output does not have to be closed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. Will do it.

}

/** Open a writer appending to an existing file.
* <strong>Since 1.9.0 this method does not close in.</strong>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.9.0 is already out. Additionally, isn't this an API breaking change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, unfortunately, this changes didn't make 1.9.0. And yes, this is an API breaking change so the next target is 1.10.0. Will rewrite the comment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.9.0 isn't out yet afaik, so this should be fine as is.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry, I was thinking of Parquet... :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that was my mistake too :)

if (fw != null)
fw.close();
if (fos != null)
fos.close();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that the second close() may not be called if the first one throws an IOException.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fos.close() is invoked separately to close the file in case of the fw creation fails (fw starts writing at the constructor). If both fos and fw creation was successful than a problem occurs during writing fw.close() shall close the file. If it fails, fos.close() would probably fail too.

@@ -55,8 +56,12 @@ private Json() {} // singleton: no public ctor
public static final Schema SCHEMA;
static {
try {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have to support Java 1.6 in Avro, or we require 1.7+? I'm wondering if we can use try-with-resource statements instead of closing the stream in finally. Also, can we merge the two nested try{} blocks into one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, Avro supports 1.6 so we are not able to use try-with-resources, unfortunately.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could start discussing dropping 1.6 support in the 1.9.0 release on the dev list.

@gszadovszky
Copy link
Contributor Author

Pushed another update (hopefully, the last one).
@busbey, could you please check if this one suits you, so I can commit it.
Thanks a lot in advance.

Copy link
Contributor

@busbey busbey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please be sure to squash into a single commit with an appropriate commit message. otherwise LGTM.

@asfgit asfgit closed this in 528a1c8 Jun 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants