[
https://issues.apache.org/jira/browse/MAPREDUCE-2243?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12979580#action_12979580
]
Bhallamudi Venkata Siva Kamesh commented on MAPREDUCE-2243:
-----------------------------------------------------------
Hi Owen,
The above example given is great and describes when to close the streams
clearly. But there is chance of missing the streams closure as given below.
If try block throws any exception other than IOException, then the chances of
stream not getting closed will be there.
{code:title=Given style of closing streams|borderStyle=solid}
try {
f = fs.open(...);
f.write(...);
// If any exception other than IOException thrown from here will lead to
missing the stream close
f.close();
} catch (IOException ioe) {
IOUtils.cleanup(LOG, f);
throw ioe;
}
{code}
So consider the following approach.
{code:title=Proposed style of closing streams|borderStyle=solid}
try {
f = fs.open(....);
f.write(....);
f.close();
f = null; // set to null explicitly so that close in finally
will not execute again and also just to avoid double close issue, if any.
}
catch(IOException ioe) {
throw ioe;
}
finally {
IOUtils.cleanup(LOG, f); // This will do nothing if the try
block completed successfully.
}
{code}
But I feel we need to check the feasibility of the getting other kinds of
exception in try block except the ones which we are catching!.
If all kinds of exceptions have been handled then I feel no need of final block.
> Close all the file streams propely in a finally block to avoid their leakage.
> -----------------------------------------------------------------------------
>
> Key: MAPREDUCE-2243
> URL: https://issues.apache.org/jira/browse/MAPREDUCE-2243
> Project: Hadoop Map/Reduce
> Issue Type: Improvement
> Components: jobtracker, tasktracker
> Affects Versions: 0.20.1, 0.22.0
> Environment: NA
> Reporter: Bhallamudi Venkata Siva Kamesh
> Priority: Minor
> Fix For: 0.22.0
>
> Original Estimate: 72h
> Remaining Estimate: 72h
>
> In the following classes streams should be closed in finally block to avoid
> their leakage in the exceptional cases.
> CompletedJobStatusStore.java
> ------------------------------------------
> dataOut.writeInt(events.length);
> for (TaskCompletionEvent event : events) {
> event.write(dataOut);
> }
> dataOut.close() ;
> EventWriter.java
> ----------------------
> encoder.flush();
> out.close();
> MapTask.java
> -------------------
> splitMetaInfo.write(out);
> out.close();
> TaskLog
> ------------
> 1) str = fis.readLine();
> fis.close();
> 2) dos.writeBytes(Long.toString(new File(logLocation, LogName.SYSLOG
> .toString()).length() - prevLogLength) + "\n");
> dos.close();
> TotalOrderPartitioner.java
> -----------------------------------
> while (reader.next(key, value)) {
> parts.add(key);
> key = ReflectionUtils.newInstance(keyClass, conf);
> }
> reader.close();
--
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.