You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@zookeeper.apache.org by "Edward Ribeiro (JIRA)" <ji...@apache.org> on 2016/09/13 22:10:20 UTC

[jira] [Comment Edited] (ZOOKEEPER-2572) Potential resource leak in FileTxnLog.truncate

    [ https://issues.apache.org/jira/browse/ZOOKEEPER-2572?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15488593#comment-15488593 ] 

Edward Ribeiro edited comment on ZOOKEEPER-2572 at 9/13/16 10:09 PM:
---------------------------------------------------------------------

Hey [~hanm], really good catch! :D 

/cc [~fpj], [~breed], [~rakesh_r]

While studying this piece of code, I have seen a curious fact: *{{FileTxnLog.truncate}} never returns false*. It's either throws IOException or returns {{true}}.

But then it's call hierarchy eventually calls {{ZkDatabase.truncateLog}} as below (my comments are in upper case below):
{code}
    public boolean truncateLog(long zxid) throws IOException {
        clear();

        // truncate the log
        boolean truncated = snapLog.truncateLog(zxid);

        // IT WILL NEVER ENTER IN THIS IF
        if (!truncated) {
            return false;
        }

        loadDataBase();
        return true;
    }
{code}

And the method above is called by {{Learner.syncWithLeader}}, again with a snippet below that will never be called, plus getting out of the {{syncWithLeader}} middway. 

{quote}
                boolean truncated=zk.getZKDatabase().truncateLog(qp.getZxid());
                // AGAIN, NEVER CAN WILL BE TRUE
                if (!truncated) {
                    // not able to truncate the log
                    LOG.error("Not able to truncate the log "
                            + Long.toHexString(qp.getZxid()));
                    System.exit(13);
                }
{quote}

Does it make sense what I am saying?





was (Author: eribeiro):
Hey [~hanm], really good catch! :D 

/cc [~fpj], [~breed], [~rakesh_r]

While studying this piece of code, I have seen a curious fact: *this method never returns false*. It's either IOException or {{true}}.

But then it's call hierarchy eventually calls {{ZkDatabase.truncateLog}} as below (my comments are in upper case below):
{code}
    public boolean truncateLog(long zxid) throws IOException {
        clear();

        // truncate the log
        boolean truncated = snapLog.truncateLog(zxid);

        // IT WILL NEVER ENTER IN THIS IF
        if (!truncated) {
            return false;
        }

        loadDataBase();
        return true;
    }
{code}

And the method above is called by {{Learner.syncWithLeader}}, again with a snippet below that will never be called, plus getting out of the {{syncWithLeader}} middway. 

{quote}
                boolean truncated=zk.getZKDatabase().truncateLog(qp.getZxid());
                // AGAIN, NEVER CAN WILL BE TRUE
                if (!truncated) {
                    // not able to truncate the log
                    LOG.error("Not able to truncate the log "
                            + Long.toHexString(qp.getZxid()));
                    System.exit(13);
                }
{quote}

Does it make sense what I am saying?




> Potential resource leak in FileTxnLog.truncate
> ----------------------------------------------
>
>                 Key: ZOOKEEPER-2572
>                 URL: https://issues.apache.org/jira/browse/ZOOKEEPER-2572
>             Project: ZooKeeper
>          Issue Type: Bug
>          Components: server
>    Affects Versions: 3.4.9, 3.5.2
>            Reporter: Michael Han
>             Fix For: 3.4.10, 3.5.3
>
>
> In FileTxnLog.truncate, we have:
> {code}
> public boolean truncate(long zxid) throws IOException {
>         FileTxnIterator itr = null;
>         try {
>             itr = new FileTxnIterator(this.logDir, zxid);
>             PositionInputStream input = itr.inputStream;
>             if(input == null) {
>                 throw new IOException("No log files found to truncate! This could " +
>                         "happen if you still have snapshots from an old setup or " +
>                         "log files were deleted accidentally or dataLogDir was changed in zoo.cfg.");
>             }
>             long pos = input.getPosition();
>             // now, truncate at the current position
>             RandomAccessFile raf=new RandomAccessFile(itr.logFile,"rw");
>             raf.setLength(pos);
>             raf.close();
>             while(itr.goToNextLog()) {
>                 if (!itr.logFile.delete()) {
>                     LOG.warn("Unable to truncate {}", itr.logFile);
>                 }
>             }
>         } finally {
>             close(itr);
>         }
>         return true;
>     }
> {code}
> {{raf}} here can be potentially in a state of not closed after leaving the method, if there is an (IO) exception thrown from setLength.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)