[jira] [Commented] (KAFKA-3955) Kafka log recovery doesn't truncate logs on non-monotonic offsets, leading to failed broker boot
[ https://issues.apache.org/jira/browse/KAFKA-3955?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=17059302#comment-17059302 ] zhangchenghui commented on KAFKA-3955: -- I made an analysis of this problem specifically: https://objcoding.com/2020/03/14/kafka-startup-fail-code-review/ > Kafka log recovery doesn't truncate logs on non-monotonic offsets, leading to > failed broker boot > > > Key: KAFKA-3955 > URL: https://issues.apache.org/jira/browse/KAFKA-3955 > Project: Kafka > Issue Type: Bug >Affects Versions: 0.10.0.0, 0.10.1.1, 0.11.0.0, 1.0.0 >Reporter: Tom Crayford >Assignee: Dhruvil Shah >Priority: Critical > Labels: reliability > > Hi, > I've found a bug impacting kafka brokers on startup after an unclean > shutdown. If a log segment is corrupt and has non-monotonic offsets (see the > appendix of this bug for a sample output from {{DumpLogSegments}}), then > {{LogSegment.recover}} throws an {{InvalidOffsetException}} error: > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/OffsetIndex.scala#L218 > That code is called by {{LogSegment.recover}}: > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/LogSegment.scala#L191 > Which is called in several places in {{Log.scala}}. Notably it's called four > times during recovery: > Thrice in Log.loadSegments > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L199 > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L204 > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L226 > and once in Log.recoverLog > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L268 > Of these, only the very last one has a {{catch}} for > {{InvalidOffsetException}}. When that catches the issue, it truncates the > whole log (not just this segment): > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L274 > to the start segment of the bad log segment. > However, this code can't be hit on recovery, because of the code paths in > {{loadSegments}} - they mean we'll never hit truncation here, as we always > throw this exception and that goes all the way to the toplevel exception > handler and crashes the JVM. > As {{Log.recoverLog}} is always called during recovery, I *think* a fix for > this is to move this crash recovery/truncate code inside a new method in > {{Log.scala}}, and call that instead of {{LogSegment.recover}} in each place. > That code should return the number of {{truncatedBytes}} like we do in > {{Log.recoverLog}} and then truncate the log. The callers will have to be > notified "stop iterating over files in the directory", likely via a return > value of {{truncatedBytes}} like {{Log.recoverLog` does right now. > I'm happy working on a patch for this. I'm aware this recovery code is tricky > and important to get right. > I'm also curious (and currently don't have good theories as of yet) as to how > this log segment got into this state with non-monotonic offsets. This segment > is using gzip compression, and is under 0.9.0.1. The same bug with respect to > recovery exists in trunk, but I'm unsure if the new handling around > compressed messages (KIP-31) means the bug where non-monotonic offsets get > appended is still present in trunk. > As a production workaround, one can manually truncate that log folder > yourself (delete all .index/.log files including and after the one with the > bad offset). However, kafka should (and can) handle this case well - with > replication we can truncate in broker startup. > stacktrace and error message: > {code} > pri=WARN t=pool-3-thread-4 at=Log Found a corrupted index file, > /$DIRECTORY/$TOPIC-22/14306536.index, deleting and rebuilding > index... > pri=ERROR t=main at=LogManager There was an error in one of the threads > during logs loading: kafka.common.InvalidOffsetException: Attempt to append > an offset (15000337) to position 111719 no larger than the last offset > appended (15000337) to /$DIRECTORY/$TOPIC-8/14008931.index. > pri=FATAL t=main at=KafkaServer Fatal error during KafkaServer startup. > Prepare to shutdown kafka.common.InvalidOffsetException: Attempt to append an > offset (15000337) to position 111719 no larger than the last offset appended > (15000337) to /$DIRECTORY/$TOPIC-8/14008931.index. > at > kafka.log.OffsetIndex$$anonfun$append$1.apply$mcV$sp(OffsetIndex.scala:207) > at > kafka.log.OffsetIndex$$anonfun$append$1.apply(OffsetIndex.scala:197) > at > kafka.log.OffsetIndex$$anonfun$append$1.apply(OffsetIndex.scala:197) > at
[jira] [Commented] (KAFKA-3955) Kafka log recovery doesn't truncate logs on non-monotonic offsets, leading to failed broker boot
[ https://issues.apache.org/jira/browse/KAFKA-3955?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16834399#comment-16834399 ] ASF GitHub Bot commented on KAFKA-3955: --- dhruvilshah3 commented on pull request #5854: KAFKA-3955: Handle out of order offsets on log recovery URL: https://github.com/apache/kafka/pull/5854 This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org > Kafka log recovery doesn't truncate logs on non-monotonic offsets, leading to > failed broker boot > > > Key: KAFKA-3955 > URL: https://issues.apache.org/jira/browse/KAFKA-3955 > Project: Kafka > Issue Type: Bug >Affects Versions: 0.10.0.0, 0.10.1.1, 0.11.0.0, 1.0.0 >Reporter: Tom Crayford >Assignee: Dhruvil Shah >Priority: Critical > Labels: reliability > > Hi, > I've found a bug impacting kafka brokers on startup after an unclean > shutdown. If a log segment is corrupt and has non-monotonic offsets (see the > appendix of this bug for a sample output from {{DumpLogSegments}}), then > {{LogSegment.recover}} throws an {{InvalidOffsetException}} error: > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/OffsetIndex.scala#L218 > That code is called by {{LogSegment.recover}}: > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/LogSegment.scala#L191 > Which is called in several places in {{Log.scala}}. Notably it's called four > times during recovery: > Thrice in Log.loadSegments > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L199 > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L204 > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L226 > and once in Log.recoverLog > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L268 > Of these, only the very last one has a {{catch}} for > {{InvalidOffsetException}}. When that catches the issue, it truncates the > whole log (not just this segment): > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L274 > to the start segment of the bad log segment. > However, this code can't be hit on recovery, because of the code paths in > {{loadSegments}} - they mean we'll never hit truncation here, as we always > throw this exception and that goes all the way to the toplevel exception > handler and crashes the JVM. > As {{Log.recoverLog}} is always called during recovery, I *think* a fix for > this is to move this crash recovery/truncate code inside a new method in > {{Log.scala}}, and call that instead of {{LogSegment.recover}} in each place. > That code should return the number of {{truncatedBytes}} like we do in > {{Log.recoverLog}} and then truncate the log. The callers will have to be > notified "stop iterating over files in the directory", likely via a return > value of {{truncatedBytes}} like {{Log.recoverLog` does right now. > I'm happy working on a patch for this. I'm aware this recovery code is tricky > and important to get right. > I'm also curious (and currently don't have good theories as of yet) as to how > this log segment got into this state with non-monotonic offsets. This segment > is using gzip compression, and is under 0.9.0.1. The same bug with respect to > recovery exists in trunk, but I'm unsure if the new handling around > compressed messages (KIP-31) means the bug where non-monotonic offsets get > appended is still present in trunk. > As a production workaround, one can manually truncate that log folder > yourself (delete all .index/.log files including and after the one with the > bad offset). However, kafka should (and can) handle this case well - with > replication we can truncate in broker startup. > stacktrace and error message: > {code} > pri=WARN t=pool-3-thread-4 at=Log Found a corrupted index file, > /$DIRECTORY/$TOPIC-22/14306536.index, deleting and rebuilding > index... > pri=ERROR t=main at=LogManager There was an error in one of the threads > during logs loading: kafka.common.InvalidOffsetException: Attempt to append > an offset (15000337) to position 111719 no larger than the last offset > appended (15000337) to /$DIRECTORY/$TOPIC-8/14008931.index. > pri=FATAL t=main at=KafkaServer Fatal error during KafkaServer startup. > Prepare to shutdown kafka.common.InvalidOffsetException: Attempt to append an > offset (15000337) to position 111719 no larger than the last
[jira] [Commented] (KAFKA-3955) Kafka log recovery doesn't truncate logs on non-monotonic offsets, leading to failed broker boot
[ https://issues.apache.org/jira/browse/KAFKA-3955?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16765447#comment-16765447 ] Matthias J. Sax commented on KAFKA-3955: [~dhruvilshah] This is marked as "Critical" for 2.2 release but it seems there was no progress on the PR since October. What is the current status? > Kafka log recovery doesn't truncate logs on non-monotonic offsets, leading to > failed broker boot > > > Key: KAFKA-3955 > URL: https://issues.apache.org/jira/browse/KAFKA-3955 > Project: Kafka > Issue Type: Bug >Affects Versions: 0.10.0.0, 0.10.1.1, 0.11.0.0, 1.0.0 >Reporter: Tom Crayford >Assignee: Dhruvil Shah >Priority: Critical > Labels: reliability > Fix For: 2.2.0 > > > Hi, > I've found a bug impacting kafka brokers on startup after an unclean > shutdown. If a log segment is corrupt and has non-monotonic offsets (see the > appendix of this bug for a sample output from {{DumpLogSegments}}), then > {{LogSegment.recover}} throws an {{InvalidOffsetException}} error: > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/OffsetIndex.scala#L218 > That code is called by {{LogSegment.recover}}: > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/LogSegment.scala#L191 > Which is called in several places in {{Log.scala}}. Notably it's called four > times during recovery: > Thrice in Log.loadSegments > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L199 > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L204 > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L226 > and once in Log.recoverLog > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L268 > Of these, only the very last one has a {{catch}} for > {{InvalidOffsetException}}. When that catches the issue, it truncates the > whole log (not just this segment): > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L274 > to the start segment of the bad log segment. > However, this code can't be hit on recovery, because of the code paths in > {{loadSegments}} - they mean we'll never hit truncation here, as we always > throw this exception and that goes all the way to the toplevel exception > handler and crashes the JVM. > As {{Log.recoverLog}} is always called during recovery, I *think* a fix for > this is to move this crash recovery/truncate code inside a new method in > {{Log.scala}}, and call that instead of {{LogSegment.recover}} in each place. > That code should return the number of {{truncatedBytes}} like we do in > {{Log.recoverLog}} and then truncate the log. The callers will have to be > notified "stop iterating over files in the directory", likely via a return > value of {{truncatedBytes}} like {{Log.recoverLog` does right now. > I'm happy working on a patch for this. I'm aware this recovery code is tricky > and important to get right. > I'm also curious (and currently don't have good theories as of yet) as to how > this log segment got into this state with non-monotonic offsets. This segment > is using gzip compression, and is under 0.9.0.1. The same bug with respect to > recovery exists in trunk, but I'm unsure if the new handling around > compressed messages (KIP-31) means the bug where non-monotonic offsets get > appended is still present in trunk. > As a production workaround, one can manually truncate that log folder > yourself (delete all .index/.log files including and after the one with the > bad offset). However, kafka should (and can) handle this case well - with > replication we can truncate in broker startup. > stacktrace and error message: > {code} > pri=WARN t=pool-3-thread-4 at=Log Found a corrupted index file, > /$DIRECTORY/$TOPIC-22/14306536.index, deleting and rebuilding > index... > pri=ERROR t=main at=LogManager There was an error in one of the threads > during logs loading: kafka.common.InvalidOffsetException: Attempt to append > an offset (15000337) to position 111719 no larger than the last offset > appended (15000337) to /$DIRECTORY/$TOPIC-8/14008931.index. > pri=FATAL t=main at=KafkaServer Fatal error during KafkaServer startup. > Prepare to shutdown kafka.common.InvalidOffsetException: Attempt to append an > offset (15000337) to position 111719 no larger than the last offset appended > (15000337) to /$DIRECTORY/$TOPIC-8/14008931.index. > at > kafka.log.OffsetIndex$$anonfun$append$1.apply$mcV$sp(OffsetIndex.scala:207) > at > kafka.log.OffsetIndex$$anonfun$append$1.apply(OffsetIndex.scala:197) > at >
[jira] [Commented] (KAFKA-3955) Kafka log recovery doesn't truncate logs on non-monotonic offsets, leading to failed broker boot
[ https://issues.apache.org/jira/browse/KAFKA-3955?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16668141#comment-16668141 ] ASF GitHub Bot commented on KAFKA-3955: --- dhruvilshah3 opened a new pull request #5854: KAFKA-3955: Handle out of order offsets on log recovery [WIP] URL: https://github.com/apache/kafka/pull/5854 *More detailed description of your change, if necessary. The PR title and PR message become the squashed commit message, so use a separate comment to ping reviewers.* *Summary of testing strategy (including rationale) for the feature or bug fix. Unit and/or integration tests are expected for any behaviour change and system tests should be considered for larger changes.* ### Committer Checklist (excluded from commit message) - [ ] Verify design and implementation - [ ] Verify test coverage and CI build status - [ ] Verify documentation (including upgrade notes) This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org > Kafka log recovery doesn't truncate logs on non-monotonic offsets, leading to > failed broker boot > > > Key: KAFKA-3955 > URL: https://issues.apache.org/jira/browse/KAFKA-3955 > Project: Kafka > Issue Type: Bug >Affects Versions: 0.10.0.0, 0.10.1.1, 0.11.0.0, 1.0.0 >Reporter: Tom Crayford >Assignee: Dhruvil Shah >Priority: Critical > Labels: reliability > Fix For: 2.2.0 > > > Hi, > I've found a bug impacting kafka brokers on startup after an unclean > shutdown. If a log segment is corrupt and has non-monotonic offsets (see the > appendix of this bug for a sample output from {{DumpLogSegments}}), then > {{LogSegment.recover}} throws an {{InvalidOffsetException}} error: > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/OffsetIndex.scala#L218 > That code is called by {{LogSegment.recover}}: > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/LogSegment.scala#L191 > Which is called in several places in {{Log.scala}}. Notably it's called four > times during recovery: > Thrice in Log.loadSegments > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L199 > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L204 > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L226 > and once in Log.recoverLog > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L268 > Of these, only the very last one has a {{catch}} for > {{InvalidOffsetException}}. When that catches the issue, it truncates the > whole log (not just this segment): > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L274 > to the start segment of the bad log segment. > However, this code can't be hit on recovery, because of the code paths in > {{loadSegments}} - they mean we'll never hit truncation here, as we always > throw this exception and that goes all the way to the toplevel exception > handler and crashes the JVM. > As {{Log.recoverLog}} is always called during recovery, I *think* a fix for > this is to move this crash recovery/truncate code inside a new method in > {{Log.scala}}, and call that instead of {{LogSegment.recover}} in each place. > That code should return the number of {{truncatedBytes}} like we do in > {{Log.recoverLog}} and then truncate the log. The callers will have to be > notified "stop iterating over files in the directory", likely via a return > value of {{truncatedBytes}} like {{Log.recoverLog` does right now. > I'm happy working on a patch for this. I'm aware this recovery code is tricky > and important to get right. > I'm also curious (and currently don't have good theories as of yet) as to how > this log segment got into this state with non-monotonic offsets. This segment > is using gzip compression, and is under 0.9.0.1. The same bug with respect to > recovery exists in trunk, but I'm unsure if the new handling around > compressed messages (KIP-31) means the bug where non-monotonic offsets get > appended is still present in trunk. > As a production workaround, one can manually truncate that log folder > yourself (delete all .index/.log files including and after the one with the > bad offset). However, kafka should (and can) handle this case well - with > replication we can truncate in broker startup. > stacktrace and error message: > {code} > pri=WARN t=pool-3-thread-4
[jira] [Commented] (KAFKA-3955) Kafka log recovery doesn't truncate logs on non-monotonic offsets, leading to failed broker boot
[ https://issues.apache.org/jira/browse/KAFKA-3955?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16348428#comment-16348428 ] Damian Guy commented on KAFKA-3955: --- Moving this to 1.2 > Kafka log recovery doesn't truncate logs on non-monotonic offsets, leading to > failed broker boot > > > Key: KAFKA-3955 > URL: https://issues.apache.org/jira/browse/KAFKA-3955 > Project: Kafka > Issue Type: Bug >Affects Versions: 0.10.0.0, 0.10.1.1, 0.11.0.0, 1.0.0 >Reporter: Tom Crayford >Assignee: Ismael Juma >Priority: Critical > Labels: reliability > Fix For: 1.2.0 > > > Hi, > I've found a bug impacting kafka brokers on startup after an unclean > shutdown. If a log segment is corrupt and has non-monotonic offsets (see the > appendix of this bug for a sample output from {{DumpLogSegments}}), then > {{LogSegment.recover}} throws an {{InvalidOffsetException}} error: > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/OffsetIndex.scala#L218 > That code is called by {{LogSegment.recover}}: > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/LogSegment.scala#L191 > Which is called in several places in {{Log.scala}}. Notably it's called four > times during recovery: > Thrice in Log.loadSegments > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L199 > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L204 > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L226 > and once in Log.recoverLog > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L268 > Of these, only the very last one has a {{catch}} for > {{InvalidOffsetException}}. When that catches the issue, it truncates the > whole log (not just this segment): > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L274 > to the start segment of the bad log segment. > However, this code can't be hit on recovery, because of the code paths in > {{loadSegments}} - they mean we'll never hit truncation here, as we always > throw this exception and that goes all the way to the toplevel exception > handler and crashes the JVM. > As {{Log.recoverLog}} is always called during recovery, I *think* a fix for > this is to move this crash recovery/truncate code inside a new method in > {{Log.scala}}, and call that instead of {{LogSegment.recover}} in each place. > That code should return the number of {{truncatedBytes}} like we do in > {{Log.recoverLog}} and then truncate the log. The callers will have to be > notified "stop iterating over files in the directory", likely via a return > value of {{truncatedBytes}} like {{Log.recoverLog` does right now. > I'm happy working on a patch for this. I'm aware this recovery code is tricky > and important to get right. > I'm also curious (and currently don't have good theories as of yet) as to how > this log segment got into this state with non-monotonic offsets. This segment > is using gzip compression, and is under 0.9.0.1. The same bug with respect to > recovery exists in trunk, but I'm unsure if the new handling around > compressed messages (KIP-31) means the bug where non-monotonic offsets get > appended is still present in trunk. > As a production workaround, one can manually truncate that log folder > yourself (delete all .index/.log files including and after the one with the > bad offset). However, kafka should (and can) handle this case well - with > replication we can truncate in broker startup. > stacktrace and error message: > {code} > pri=WARN t=pool-3-thread-4 at=Log Found a corrupted index file, > /$DIRECTORY/$TOPIC-22/14306536.index, deleting and rebuilding > index... > pri=ERROR t=main at=LogManager There was an error in one of the threads > during logs loading: kafka.common.InvalidOffsetException: Attempt to append > an offset (15000337) to position 111719 no larger than the last offset > appended (15000337) to /$DIRECTORY/$TOPIC-8/14008931.index. > pri=FATAL t=main at=KafkaServer Fatal error during KafkaServer startup. > Prepare to shutdown kafka.common.InvalidOffsetException: Attempt to append an > offset (15000337) to position 111719 no larger than the last offset appended > (15000337) to /$DIRECTORY/$TOPIC-8/14008931.index. > at > kafka.log.OffsetIndex$$anonfun$append$1.apply$mcV$sp(OffsetIndex.scala:207) > at > kafka.log.OffsetIndex$$anonfun$append$1.apply(OffsetIndex.scala:197) > at > kafka.log.OffsetIndex$$anonfun$append$1.apply(OffsetIndex.scala:197) > at kafka.utils.CoreUtils$.inLock(CoreUtils.scala:262) > at
[jira] [Commented] (KAFKA-3955) Kafka log recovery doesn't truncate logs on non-monotonic offsets, leading to failed broker boot
[ https://issues.apache.org/jira/browse/KAFKA-3955?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16343657#comment-16343657 ] Damian Guy commented on KAFKA-3955: --- [~ijuma] is this going to make it for 1.1? > Kafka log recovery doesn't truncate logs on non-monotonic offsets, leading to > failed broker boot > > > Key: KAFKA-3955 > URL: https://issues.apache.org/jira/browse/KAFKA-3955 > Project: Kafka > Issue Type: Bug >Affects Versions: 0.10.0.0, 0.10.1.1, 0.11.0.0, 1.0.0 >Reporter: Tom Crayford >Assignee: Ismael Juma >Priority: Critical > Labels: reliability > Fix For: 1.1.0 > > > Hi, > I've found a bug impacting kafka brokers on startup after an unclean > shutdown. If a log segment is corrupt and has non-monotonic offsets (see the > appendix of this bug for a sample output from {{DumpLogSegments}}), then > {{LogSegment.recover}} throws an {{InvalidOffsetException}} error: > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/OffsetIndex.scala#L218 > That code is called by {{LogSegment.recover}}: > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/LogSegment.scala#L191 > Which is called in several places in {{Log.scala}}. Notably it's called four > times during recovery: > Thrice in Log.loadSegments > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L199 > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L204 > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L226 > and once in Log.recoverLog > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L268 > Of these, only the very last one has a {{catch}} for > {{InvalidOffsetException}}. When that catches the issue, it truncates the > whole log (not just this segment): > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L274 > to the start segment of the bad log segment. > However, this code can't be hit on recovery, because of the code paths in > {{loadSegments}} - they mean we'll never hit truncation here, as we always > throw this exception and that goes all the way to the toplevel exception > handler and crashes the JVM. > As {{Log.recoverLog}} is always called during recovery, I *think* a fix for > this is to move this crash recovery/truncate code inside a new method in > {{Log.scala}}, and call that instead of {{LogSegment.recover}} in each place. > That code should return the number of {{truncatedBytes}} like we do in > {{Log.recoverLog}} and then truncate the log. The callers will have to be > notified "stop iterating over files in the directory", likely via a return > value of {{truncatedBytes}} like {{Log.recoverLog` does right now. > I'm happy working on a patch for this. I'm aware this recovery code is tricky > and important to get right. > I'm also curious (and currently don't have good theories as of yet) as to how > this log segment got into this state with non-monotonic offsets. This segment > is using gzip compression, and is under 0.9.0.1. The same bug with respect to > recovery exists in trunk, but I'm unsure if the new handling around > compressed messages (KIP-31) means the bug where non-monotonic offsets get > appended is still present in trunk. > As a production workaround, one can manually truncate that log folder > yourself (delete all .index/.log files including and after the one with the > bad offset). However, kafka should (and can) handle this case well - with > replication we can truncate in broker startup. > stacktrace and error message: > {code} > pri=WARN t=pool-3-thread-4 at=Log Found a corrupted index file, > /$DIRECTORY/$TOPIC-22/14306536.index, deleting and rebuilding > index... > pri=ERROR t=main at=LogManager There was an error in one of the threads > during logs loading: kafka.common.InvalidOffsetException: Attempt to append > an offset (15000337) to position 111719 no larger than the last offset > appended (15000337) to /$DIRECTORY/$TOPIC-8/14008931.index. > pri=FATAL t=main at=KafkaServer Fatal error during KafkaServer startup. > Prepare to shutdown kafka.common.InvalidOffsetException: Attempt to append an > offset (15000337) to position 111719 no larger than the last offset appended > (15000337) to /$DIRECTORY/$TOPIC-8/14008931.index. > at > kafka.log.OffsetIndex$$anonfun$append$1.apply$mcV$sp(OffsetIndex.scala:207) > at > kafka.log.OffsetIndex$$anonfun$append$1.apply(OffsetIndex.scala:197) > at > kafka.log.OffsetIndex$$anonfun$append$1.apply(OffsetIndex.scala:197) > at kafka.utils.CoreUtils$.inLock(CoreUtils.scala:262) >
[jira] [Commented] (KAFKA-3955) Kafka log recovery doesn't truncate logs on non-monotonic offsets, leading to failed broker boot
[ https://issues.apache.org/jira/browse/KAFKA-3955?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16293291#comment-16293291 ] James Cheng commented on KAFKA-3955: [~ijuma], I said in my comment that: {quote} Note that it is only safe to do this if the partition is online (being lead by someone other than the affected broker). If the partition is offline and the preferred replica for it is the one with this problem, then deleting the log directory on the leader will cause https://issues.apache.org/jira/browse/KAFKA-3410 to happen (because the leader will now have no data, but followers do have data, and so follower will be ahead of leader). {quote} If this happens on the offline partition leader, then what are my options? I think that I will need to first, delete all the logs for this partition from the offline partition leader's log.dir, and then I think I will need to turn on unclean.leader.election for that topic (in order to prevent KAFKA-3410) Does that sound right? > Kafka log recovery doesn't truncate logs on non-monotonic offsets, leading to > failed broker boot > > > Key: KAFKA-3955 > URL: https://issues.apache.org/jira/browse/KAFKA-3955 > Project: Kafka > Issue Type: Bug >Affects Versions: 0.10.0.0, 0.10.1.1, 0.11.0.0, 1.0.0 >Reporter: Tom Crayford >Assignee: Ismael Juma >Priority: Critical > Labels: reliability > Fix For: 1.1.0 > > > Hi, > I've found a bug impacting kafka brokers on startup after an unclean > shutdown. If a log segment is corrupt and has non-monotonic offsets (see the > appendix of this bug for a sample output from {{DumpLogSegments}}), then > {{LogSegment.recover}} throws an {{InvalidOffsetException}} error: > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/OffsetIndex.scala#L218 > That code is called by {{LogSegment.recover}}: > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/LogSegment.scala#L191 > Which is called in several places in {{Log.scala}}. Notably it's called four > times during recovery: > Thrice in Log.loadSegments > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L199 > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L204 > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L226 > and once in Log.recoverLog > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L268 > Of these, only the very last one has a {{catch}} for > {{InvalidOffsetException}}. When that catches the issue, it truncates the > whole log (not just this segment): > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L274 > to the start segment of the bad log segment. > However, this code can't be hit on recovery, because of the code paths in > {{loadSegments}} - they mean we'll never hit truncation here, as we always > throw this exception and that goes all the way to the toplevel exception > handler and crashes the JVM. > As {{Log.recoverLog}} is always called during recovery, I *think* a fix for > this is to move this crash recovery/truncate code inside a new method in > {{Log.scala}}, and call that instead of {{LogSegment.recover}} in each place. > That code should return the number of {{truncatedBytes}} like we do in > {{Log.recoverLog}} and then truncate the log. The callers will have to be > notified "stop iterating over files in the directory", likely via a return > value of {{truncatedBytes}} like {{Log.recoverLog` does right now. > I'm happy working on a patch for this. I'm aware this recovery code is tricky > and important to get right. > I'm also curious (and currently don't have good theories as of yet) as to how > this log segment got into this state with non-monotonic offsets. This segment > is using gzip compression, and is under 0.9.0.1. The same bug with respect to > recovery exists in trunk, but I'm unsure if the new handling around > compressed messages (KIP-31) means the bug where non-monotonic offsets get > appended is still present in trunk. > As a production workaround, one can manually truncate that log folder > yourself (delete all .index/.log files including and after the one with the > bad offset). However, kafka should (and can) handle this case well - with > replication we can truncate in broker startup. > stacktrace and error message: > {code} > pri=WARN t=pool-3-thread-4 at=Log Found a corrupted index file, > /$DIRECTORY/$TOPIC-22/14306536.index, deleting and rebuilding > index... > pri=ERROR t=main at=LogManager There was an error in one of the threads > during logs loading: kafka.common.InvalidOffsetException: Attempt
[jira] [Commented] (KAFKA-3955) Kafka log recovery doesn't truncate logs on non-monotonic offsets, leading to failed broker boot
[ https://issues.apache.org/jira/browse/KAFKA-3955?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16285159#comment-16285159 ] Ismael Juma commented on KAFKA-3955: I have to look closer, but it seems to me that LogSegment.recover should be catching InvalidOffsetException in the same way that it catches CorruptRecordException. Since the report was filed, a Log.recoverSegment has been added (which is the additional method that [~tcrayford-heroku] had suggested), so some of the logic in Log.recover can be moved to that method. > Kafka log recovery doesn't truncate logs on non-monotonic offsets, leading to > failed broker boot > > > Key: KAFKA-3955 > URL: https://issues.apache.org/jira/browse/KAFKA-3955 > Project: Kafka > Issue Type: Bug >Affects Versions: 0.10.0.0, 0.10.1.1, 0.11.0.0, 1.0.0 >Reporter: Tom Crayford >Assignee: Ismael Juma >Priority: Critical > Labels: reliability > Fix For: 1.1.0 > > > Hi, > I've found a bug impacting kafka brokers on startup after an unclean > shutdown. If a log segment is corrupt and has non-monotonic offsets (see the > appendix of this bug for a sample output from {{DumpLogSegments}}), then > {{LogSegment.recover}} throws an {{InvalidOffsetException}} error: > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/OffsetIndex.scala#L218 > That code is called by {{LogSegment.recover}}: > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/LogSegment.scala#L191 > Which is called in several places in {{Log.scala}}. Notably it's called four > times during recovery: > Thrice in Log.loadSegments > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L199 > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L204 > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L226 > and once in Log.recoverLog > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L268 > Of these, only the very last one has a {{catch}} for > {{InvalidOffsetException}}. When that catches the issue, it truncates the > whole log (not just this segment): > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L274 > to the start segment of the bad log segment. > However, this code can't be hit on recovery, because of the code paths in > {{loadSegments}} - they mean we'll never hit truncation here, as we always > throw this exception and that goes all the way to the toplevel exception > handler and crashes the JVM. > As {{Log.recoverLog}} is always called during recovery, I *think* a fix for > this is to move this crash recovery/truncate code inside a new method in > {{Log.scala}}, and call that instead of {{LogSegment.recover}} in each place. > That code should return the number of {{truncatedBytes}} like we do in > {{Log.recoverLog}} and then truncate the log. The callers will have to be > notified "stop iterating over files in the directory", likely via a return > value of {{truncatedBytes}} like {{Log.recoverLog` does right now. > I'm happy working on a patch for this. I'm aware this recovery code is tricky > and important to get right. > I'm also curious (and currently don't have good theories as of yet) as to how > this log segment got into this state with non-monotonic offsets. This segment > is using gzip compression, and is under 0.9.0.1. The same bug with respect to > recovery exists in trunk, but I'm unsure if the new handling around > compressed messages (KIP-31) means the bug where non-monotonic offsets get > appended is still present in trunk. > As a production workaround, one can manually truncate that log folder > yourself (delete all .index/.log files including and after the one with the > bad offset). However, kafka should (and can) handle this case well - with > replication we can truncate in broker startup. > stacktrace and error message: > {code} > pri=WARN t=pool-3-thread-4 at=Log Found a corrupted index file, > /$DIRECTORY/$TOPIC-22/14306536.index, deleting and rebuilding > index... > pri=ERROR t=main at=LogManager There was an error in one of the threads > during logs loading: kafka.common.InvalidOffsetException: Attempt to append > an offset (15000337) to position 111719 no larger than the last offset > appended (15000337) to /$DIRECTORY/$TOPIC-8/14008931.index. > pri=FATAL t=main at=KafkaServer Fatal error during KafkaServer startup. > Prepare to shutdown kafka.common.InvalidOffsetException: Attempt to append an > offset (15000337) to position 111719 no larger than the last offset appended > (15000337) to /$DIRECTORY/$TOPIC-8/14008931.index. >
[jira] [Commented] (KAFKA-3955) Kafka log recovery doesn't truncate logs on non-monotonic offsets, leading to failed broker boot
[ https://issues.apache.org/jira/browse/KAFKA-3955?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16285155#comment-16285155 ] Ismael Juma commented on KAFKA-3955: OK, since KAFKA-1211 requires the message format to be 0.11.0, this makes sense. I can work on this after https://github.com/apache/kafka/pull/4040 is merged since the code is fresh on my mind. > Kafka log recovery doesn't truncate logs on non-monotonic offsets, leading to > failed broker boot > > > Key: KAFKA-3955 > URL: https://issues.apache.org/jira/browse/KAFKA-3955 > Project: Kafka > Issue Type: Bug >Affects Versions: 0.10.0.0, 0.10.1.1, 0.11.0.0, 1.0.0 >Reporter: Tom Crayford >Priority: Critical > Labels: reliability > Fix For: 1.0.1 > > > Hi, > I've found a bug impacting kafka brokers on startup after an unclean > shutdown. If a log segment is corrupt and has non-monotonic offsets (see the > appendix of this bug for a sample output from {{DumpLogSegments}}), then > {{LogSegment.recover}} throws an {{InvalidOffsetException}} error: > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/OffsetIndex.scala#L218 > That code is called by {{LogSegment.recover}}: > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/LogSegment.scala#L191 > Which is called in several places in {{Log.scala}}. Notably it's called four > times during recovery: > Thrice in Log.loadSegments > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L199 > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L204 > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L226 > and once in Log.recoverLog > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L268 > Of these, only the very last one has a {{catch}} for > {{InvalidOffsetException}}. When that catches the issue, it truncates the > whole log (not just this segment): > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L274 > to the start segment of the bad log segment. > However, this code can't be hit on recovery, because of the code paths in > {{loadSegments}} - they mean we'll never hit truncation here, as we always > throw this exception and that goes all the way to the toplevel exception > handler and crashes the JVM. > As {{Log.recoverLog}} is always called during recovery, I *think* a fix for > this is to move this crash recovery/truncate code inside a new method in > {{Log.scala}}, and call that instead of {{LogSegment.recover}} in each place. > That code should return the number of {{truncatedBytes}} like we do in > {{Log.recoverLog}} and then truncate the log. The callers will have to be > notified "stop iterating over files in the directory", likely via a return > value of {{truncatedBytes}} like {{Log.recoverLog` does right now. > I'm happy working on a patch for this. I'm aware this recovery code is tricky > and important to get right. > I'm also curious (and currently don't have good theories as of yet) as to how > this log segment got into this state with non-monotonic offsets. This segment > is using gzip compression, and is under 0.9.0.1. The same bug with respect to > recovery exists in trunk, but I'm unsure if the new handling around > compressed messages (KIP-31) means the bug where non-monotonic offsets get > appended is still present in trunk. > As a production workaround, one can manually truncate that log folder > yourself (delete all .index/.log files including and after the one with the > bad offset). However, kafka should (and can) handle this case well - with > replication we can truncate in broker startup. > stacktrace and error message: > {code} > pri=WARN t=pool-3-thread-4 at=Log Found a corrupted index file, > /$DIRECTORY/$TOPIC-22/14306536.index, deleting and rebuilding > index... > pri=ERROR t=main at=LogManager There was an error in one of the threads > during logs loading: kafka.common.InvalidOffsetException: Attempt to append > an offset (15000337) to position 111719 no larger than the last offset > appended (15000337) to /$DIRECTORY/$TOPIC-8/14008931.index. > pri=FATAL t=main at=KafkaServer Fatal error during KafkaServer startup. > Prepare to shutdown kafka.common.InvalidOffsetException: Attempt to append an > offset (15000337) to position 111719 no larger than the last offset appended > (15000337) to /$DIRECTORY/$TOPIC-8/14008931.index. > at > kafka.log.OffsetIndex$$anonfun$append$1.apply$mcV$sp(OffsetIndex.scala:207) > at > kafka.log.OffsetIndex$$anonfun$append$1.apply(OffsetIndex.scala:197) > at >
[jira] [Commented] (KAFKA-3955) Kafka log recovery doesn't truncate logs on non-monotonic offsets, leading to failed broker boot
[ https://issues.apache.org/jira/browse/KAFKA-3955?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16285148#comment-16285148 ] James Cheng commented on KAFKA-3955: log.message.format.version = 0.10.0.1 > Kafka log recovery doesn't truncate logs on non-monotonic offsets, leading to > failed broker boot > > > Key: KAFKA-3955 > URL: https://issues.apache.org/jira/browse/KAFKA-3955 > Project: Kafka > Issue Type: Bug >Affects Versions: 0.10.0.0, 0.10.1.1, 0.11.0.0, 1.0.0 >Reporter: Tom Crayford >Priority: Critical > Labels: reliability > Fix For: 1.0.1 > > > Hi, > I've found a bug impacting kafka brokers on startup after an unclean > shutdown. If a log segment is corrupt and has non-monotonic offsets (see the > appendix of this bug for a sample output from {{DumpLogSegments}}), then > {{LogSegment.recover}} throws an {{InvalidOffsetException}} error: > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/OffsetIndex.scala#L218 > That code is called by {{LogSegment.recover}}: > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/LogSegment.scala#L191 > Which is called in several places in {{Log.scala}}. Notably it's called four > times during recovery: > Thrice in Log.loadSegments > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L199 > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L204 > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L226 > and once in Log.recoverLog > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L268 > Of these, only the very last one has a {{catch}} for > {{InvalidOffsetException}}. When that catches the issue, it truncates the > whole log (not just this segment): > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L274 > to the start segment of the bad log segment. > However, this code can't be hit on recovery, because of the code paths in > {{loadSegments}} - they mean we'll never hit truncation here, as we always > throw this exception and that goes all the way to the toplevel exception > handler and crashes the JVM. > As {{Log.recoverLog}} is always called during recovery, I *think* a fix for > this is to move this crash recovery/truncate code inside a new method in > {{Log.scala}}, and call that instead of {{LogSegment.recover}} in each place. > That code should return the number of {{truncatedBytes}} like we do in > {{Log.recoverLog}} and then truncate the log. The callers will have to be > notified "stop iterating over files in the directory", likely via a return > value of {{truncatedBytes}} like {{Log.recoverLog` does right now. > I'm happy working on a patch for this. I'm aware this recovery code is tricky > and important to get right. > I'm also curious (and currently don't have good theories as of yet) as to how > this log segment got into this state with non-monotonic offsets. This segment > is using gzip compression, and is under 0.9.0.1. The same bug with respect to > recovery exists in trunk, but I'm unsure if the new handling around > compressed messages (KIP-31) means the bug where non-monotonic offsets get > appended is still present in trunk. > As a production workaround, one can manually truncate that log folder > yourself (delete all .index/.log files including and after the one with the > bad offset). However, kafka should (and can) handle this case well - with > replication we can truncate in broker startup. > stacktrace and error message: > {code} > pri=WARN t=pool-3-thread-4 at=Log Found a corrupted index file, > /$DIRECTORY/$TOPIC-22/14306536.index, deleting and rebuilding > index... > pri=ERROR t=main at=LogManager There was an error in one of the threads > during logs loading: kafka.common.InvalidOffsetException: Attempt to append > an offset (15000337) to position 111719 no larger than the last offset > appended (15000337) to /$DIRECTORY/$TOPIC-8/14008931.index. > pri=FATAL t=main at=KafkaServer Fatal error during KafkaServer startup. > Prepare to shutdown kafka.common.InvalidOffsetException: Attempt to append an > offset (15000337) to position 111719 no larger than the last offset appended > (15000337) to /$DIRECTORY/$TOPIC-8/14008931.index. > at > kafka.log.OffsetIndex$$anonfun$append$1.apply$mcV$sp(OffsetIndex.scala:207) > at > kafka.log.OffsetIndex$$anonfun$append$1.apply(OffsetIndex.scala:197) > at > kafka.log.OffsetIndex$$anonfun$append$1.apply(OffsetIndex.scala:197) > at kafka.utils.CoreUtils$.inLock(CoreUtils.scala:262) > at
[jira] [Commented] (KAFKA-3955) Kafka log recovery doesn't truncate logs on non-monotonic offsets, leading to failed broker boot
[ https://issues.apache.org/jira/browse/KAFKA-3955?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16285147#comment-16285147 ] Ismael Juma commented on KAFKA-3955: [~wushujames], what message format version was being used when this happened? > Kafka log recovery doesn't truncate logs on non-monotonic offsets, leading to > failed broker boot > > > Key: KAFKA-3955 > URL: https://issues.apache.org/jira/browse/KAFKA-3955 > Project: Kafka > Issue Type: Bug >Affects Versions: 0.10.0.0, 0.10.1.1, 0.11.0.0, 1.0.0 >Reporter: Tom Crayford >Priority: Critical > Labels: reliability > Fix For: 1.0.1 > > > Hi, > I've found a bug impacting kafka brokers on startup after an unclean > shutdown. If a log segment is corrupt and has non-monotonic offsets (see the > appendix of this bug for a sample output from {{DumpLogSegments}}), then > {{LogSegment.recover}} throws an {{InvalidOffsetException}} error: > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/OffsetIndex.scala#L218 > That code is called by {{LogSegment.recover}}: > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/LogSegment.scala#L191 > Which is called in several places in {{Log.scala}}. Notably it's called four > times during recovery: > Thrice in Log.loadSegments > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L199 > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L204 > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L226 > and once in Log.recoverLog > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L268 > Of these, only the very last one has a {{catch}} for > {{InvalidOffsetException}}. When that catches the issue, it truncates the > whole log (not just this segment): > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L274 > to the start segment of the bad log segment. > However, this code can't be hit on recovery, because of the code paths in > {{loadSegments}} - they mean we'll never hit truncation here, as we always > throw this exception and that goes all the way to the toplevel exception > handler and crashes the JVM. > As {{Log.recoverLog}} is always called during recovery, I *think* a fix for > this is to move this crash recovery/truncate code inside a new method in > {{Log.scala}}, and call that instead of {{LogSegment.recover}} in each place. > That code should return the number of {{truncatedBytes}} like we do in > {{Log.recoverLog}} and then truncate the log. The callers will have to be > notified "stop iterating over files in the directory", likely via a return > value of {{truncatedBytes}} like {{Log.recoverLog` does right now. > I'm happy working on a patch for this. I'm aware this recovery code is tricky > and important to get right. > I'm also curious (and currently don't have good theories as of yet) as to how > this log segment got into this state with non-monotonic offsets. This segment > is using gzip compression, and is under 0.9.0.1. The same bug with respect to > recovery exists in trunk, but I'm unsure if the new handling around > compressed messages (KIP-31) means the bug where non-monotonic offsets get > appended is still present in trunk. > As a production workaround, one can manually truncate that log folder > yourself (delete all .index/.log files including and after the one with the > bad offset). However, kafka should (and can) handle this case well - with > replication we can truncate in broker startup. > stacktrace and error message: > {code} > pri=WARN t=pool-3-thread-4 at=Log Found a corrupted index file, > /$DIRECTORY/$TOPIC-22/14306536.index, deleting and rebuilding > index... > pri=ERROR t=main at=LogManager There was an error in one of the threads > during logs loading: kafka.common.InvalidOffsetException: Attempt to append > an offset (15000337) to position 111719 no larger than the last offset > appended (15000337) to /$DIRECTORY/$TOPIC-8/14008931.index. > pri=FATAL t=main at=KafkaServer Fatal error during KafkaServer startup. > Prepare to shutdown kafka.common.InvalidOffsetException: Attempt to append an > offset (15000337) to position 111719 no larger than the last offset appended > (15000337) to /$DIRECTORY/$TOPIC-8/14008931.index. > at > kafka.log.OffsetIndex$$anonfun$append$1.apply$mcV$sp(OffsetIndex.scala:207) > at > kafka.log.OffsetIndex$$anonfun$append$1.apply(OffsetIndex.scala:197) > at > kafka.log.OffsetIndex$$anonfun$append$1.apply(OffsetIndex.scala:197) > at kafka.utils.CoreUtils$.inLock(CoreUtils.scala:262) >
[jira] [Commented] (KAFKA-3955) Kafka log recovery doesn't truncate logs on non-monotonic offsets, leading to failed broker boot
[ https://issues.apache.org/jira/browse/KAFKA-3955?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel=16285136#comment-16285136 ] James Cheng commented on KAFKA-3955: We encountered this last week on 0.11.0.1. Thanks to [~tcrayford-heroku]'s detailed bug report, we were able to understand and (manually) recover from the issue by manually deleting the partition folder that contains the .log/.index files. Note that it is only safe to do this if the partition is online (being lead by someone other than the affected broker). If the partition is offline and the preferred replica for it is the one with this problem, then deleting the log directory on the leader will cause https://issues.apache.org/jira/browse/KAFKA-3410 to happen (because the leader will now have no data, but followers do have data, and so follower will be ahead of leader). > Kafka log recovery doesn't truncate logs on non-monotonic offsets, leading to > failed broker boot > > > Key: KAFKA-3955 > URL: https://issues.apache.org/jira/browse/KAFKA-3955 > Project: Kafka > Issue Type: Bug >Affects Versions: 0.10.0.0, 0.10.1.1, 0.11.0.0, 1.0.0 >Reporter: Tom Crayford >Priority: Critical > Labels: reliability > Fix For: 1.0.1 > > > Hi, > I've found a bug impacting kafka brokers on startup after an unclean > shutdown. If a log segment is corrupt and has non-monotonic offsets (see the > appendix of this bug for a sample output from {{DumpLogSegments}}), then > {{LogSegment.recover}} throws an {{InvalidOffsetException}} error: > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/OffsetIndex.scala#L218 > That code is called by {{LogSegment.recover}}: > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/LogSegment.scala#L191 > Which is called in several places in {{Log.scala}}. Notably it's called four > times during recovery: > Thrice in Log.loadSegments > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L199 > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L204 > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L226 > and once in Log.recoverLog > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L268 > Of these, only the very last one has a {{catch}} for > {{InvalidOffsetException}}. When that catches the issue, it truncates the > whole log (not just this segment): > https://github.com/apache/kafka/blob/trunk/core/src/main/scala/kafka/log/Log.scala#L274 > to the start segment of the bad log segment. > However, this code can't be hit on recovery, because of the code paths in > {{loadSegments}} - they mean we'll never hit truncation here, as we always > throw this exception and that goes all the way to the toplevel exception > handler and crashes the JVM. > As {{Log.recoverLog}} is always called during recovery, I *think* a fix for > this is to move this crash recovery/truncate code inside a new method in > {{Log.scala}}, and call that instead of {{LogSegment.recover}} in each place. > That code should return the number of {{truncatedBytes}} like we do in > {{Log.recoverLog}} and then truncate the log. The callers will have to be > notified "stop iterating over files in the directory", likely via a return > value of {{truncatedBytes}} like {{Log.recoverLog` does right now. > I'm happy working on a patch for this. I'm aware this recovery code is tricky > and important to get right. > I'm also curious (and currently don't have good theories as of yet) as to how > this log segment got into this state with non-monotonic offsets. This segment > is using gzip compression, and is under 0.9.0.1. The same bug with respect to > recovery exists in trunk, but I'm unsure if the new handling around > compressed messages (KIP-31) means the bug where non-monotonic offsets get > appended is still present in trunk. > As a production workaround, one can manually truncate that log folder > yourself (delete all .index/.log files including and after the one with the > bad offset). However, kafka should (and can) handle this case well - with > replication we can truncate in broker startup. > stacktrace and error message: > {code} > pri=WARN t=pool-3-thread-4 at=Log Found a corrupted index file, > /$DIRECTORY/$TOPIC-22/14306536.index, deleting and rebuilding > index... > pri=ERROR t=main at=LogManager There was an error in one of the threads > during logs loading: kafka.common.InvalidOffsetException: Attempt to append > an offset (15000337) to position 111719 no larger than the last offset > appended (15000337) to /$DIRECTORY/$TOPIC-8/14008931.index. > pri=FATAL t=main