This is an automated email from the ASF dual-hosted git repository. showuon pushed a commit to branch 3.7 in repository https://gitbox.apache.org/repos/asf/kafka.git
The following commit(s) were added to refs/heads/3.7 by this push: new 9b254d396e4 KAFKA-16209 : fetchSnapshot might return null if topic is created before v2.8 (#15444) 9b254d396e4 is described below commit 9b254d396e454de5628aad6dccbbc527a2bed0ae Author: John Yu <54207775+chia...@users.noreply.github.com> AuthorDate: Wed Mar 6 09:00:58 2024 +0800 KAFKA-16209 : fetchSnapshot might return null if topic is created before v2.8 (#15444) Change the function with a better way to deal with the NULL pointer exception. Reviewers: Luke Chen <show...@gmail.com> --- core/src/test/scala/unit/kafka/log/ProducerStateManagerTest.scala | 6 ++++++ .../apache/kafka/storage/internals/log/ProducerStateManager.java | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/core/src/test/scala/unit/kafka/log/ProducerStateManagerTest.scala b/core/src/test/scala/unit/kafka/log/ProducerStateManagerTest.scala index f6e29a73428..0f8dfda4995 100644 --- a/core/src/test/scala/unit/kafka/log/ProducerStateManagerTest.scala +++ b/core/src/test/scala/unit/kafka/log/ProducerStateManagerTest.scala @@ -578,6 +578,12 @@ class ProducerStateManagerTest { assertTrue(logDir.list().head.nonEmpty, "Snapshot file is empty") } + @Test + def testFetchSnapshotEmptySnapShot(): Unit = { + val offset = 1 + assertEquals(Optional.empty(), stateManager.fetchSnapshot(offset)) + } + @Test def testRecoverFromSnapshotUnfinishedTransaction(): Unit = { val epoch = 0.toShort diff --git a/storage/src/main/java/org/apache/kafka/storage/internals/log/ProducerStateManager.java b/storage/src/main/java/org/apache/kafka/storage/internals/log/ProducerStateManager.java index 6bcafd2d607..73e3e708a5f 100644 --- a/storage/src/main/java/org/apache/kafka/storage/internals/log/ProducerStateManager.java +++ b/storage/src/main/java/org/apache/kafka/storage/internals/log/ProducerStateManager.java @@ -605,7 +605,7 @@ public class ProducerStateManager { } public Optional<File> fetchSnapshot(long offset) { - return Optional.of(snapshots.get(offset)).map(x -> x.file()); + return Optional.ofNullable(snapshots.get(offset)).map(x -> x.file()); } private Optional<SnapshotFile> oldestSnapshotFile() {