junrao commented on a change in pull request #9838: URL: https://github.com/apache/kafka/pull/9838#discussion_r553069748
########## File path: core/src/main/scala/kafka/log/Log.scala ########## @@ -2124,13 +2132,15 @@ class Log(@volatile private var _dir: File, time = time, initFileSize = initFileSize, preallocate = config.preallocate)) - updateLogEndOffset(newOffset) leaderEpochCache.foreach(_.clearAndFlush()) - producerStateManager.truncate() - producerStateManager.updateMapEndOffset(newOffset) + // Clear producer state prior to adjusting log start and end offsets + // in case the first unstable offset is invalidated by the truncation. + producerStateManager.truncateFullyAndStartAt(newOffset) maybeIncrementFirstUnstableOffset() + updateLogStartOffset(newOffset) + updateLogEndOffset(newOffset) Review comment: This is an existing issue. updateLogEndOffset() makes the assumption that newOffset is nextOffset since it uses nextOffsetMetadata directly. This can be error prone in the future. While you are at this, perhaps we could remove the input param from updateLogEndOffset()? ########## File path: core/src/test/scala/unit/kafka/log/LogTest.scala ########## @@ -190,6 +190,59 @@ class LogTest { assertFetchSizeAndOffsets(fetchOffset = 3L, 0, Seq()) } + @Test + def testTruncateBelowFirstUnstableOffset(): Unit = { + testTruncateBelowFirstUnstableOffset(_.truncateTo) + } + + @Test + def testTruncateFullyAndStartBelowFirstUnstableOffset(): Unit = { + testTruncateBelowFirstUnstableOffset(_.truncateFullyAndStartAt) + } + + def testTruncateBelowFirstUnstableOffset( Review comment: Could this be private? ---------------------------------------------------------------- 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