You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kafka.apache.org by mj...@apache.org on 2019/10/15 05:01:44 UTC

[kafka] branch 2.4 updated: KAFKA-8671: NullPointerException occurs if topic associated with GlobalKTable changes (#7437)

This is an automated email from the ASF dual-hosted git repository.

mjsax pushed a commit to branch 2.4
in repository https://gitbox.apache.org/repos/asf/kafka.git


The following commit(s) were added to refs/heads/2.4 by this push:
     new 0c7dcce  KAFKA-8671: NullPointerException occurs if topic associated with GlobalKTable changes (#7437)
0c7dcce is described below

commit 0c7dccea55b13376d78cfcb0d080d68560115758
Author: Alex Leung <al...@bloomberg.net>
AuthorDate: Mon Oct 14 21:47:50 2019 -0700

    KAFKA-8671: NullPointerException occurs if topic associated with GlobalKTable changes (#7437)
    
    Reviewers: Matthias J. Sax <ma...@confluent.io>, Boyang Chen <bo...@confluent.io>
---
 .../internals/GlobalStateManagerImpl.java          | 20 ++++++++++++++++++
 .../internals/GlobalStateManagerImplTest.java      | 24 ++++++++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/streams/src/main/java/org/apache/kafka/streams/processor/internals/GlobalStateManagerImpl.java b/streams/src/main/java/org/apache/kafka/streams/processor/internals/GlobalStateManagerImpl.java
index 6a3959f..18574c7 100644
--- a/streams/src/main/java/org/apache/kafka/streams/processor/internals/GlobalStateManagerImpl.java
+++ b/streams/src/main/java/org/apache/kafka/streams/processor/internals/GlobalStateManagerImpl.java
@@ -131,10 +131,30 @@ public class GlobalStateManagerImpl implements GlobalStateManager {
         }
 
         final List<StateStore> stateStores = topology.globalStateStores();
+        final Map<String, String> storeNameToChangelog = topology.storeToChangelogTopic();
+        final Set<String> changelogTopics = new HashSet<>();
         for (final StateStore stateStore : stateStores) {
             globalStoreNames.add(stateStore.name());
+            final String sourceTopic = storeNameToChangelog.get(stateStore.name());
+            changelogTopics.add(sourceTopic);
             stateStore.init(globalProcessorContext, stateStore);
         }
+
+        // make sure each topic-partition from checkpointFileCache is associated with a global state store
+        checkpointFileCache.keySet().forEach(tp -> {
+            if (!changelogTopics.contains(tp.topic())) {
+                log.error("Encountered a topic-partition in the global checkpoint file not associated with any global" +
+                    " state store, topic-partition: {}, checkpoint file: {}. If this topic-partition is no longer valid," +
+                    " an application reset and state store directory cleanup will be required.",
+                    tp.topic(), checkpointFile.toString());
+                try {
+                    stateDirectory.unlockGlobalState();
+                } catch (final IOException e) {
+                    log.error("Failed to unlock the global state directory", e);
+                }
+                throw new StreamsException("Encountered a topic-partition not associated with any global state store");
+            }
+        });
         return Collections.unmodifiableSet(globalStoreNames);
     }
 
diff --git a/streams/src/test/java/org/apache/kafka/streams/processor/internals/GlobalStateManagerImplTest.java b/streams/src/test/java/org/apache/kafka/streams/processor/internals/GlobalStateManagerImplTest.java
index 425c074..f242a4e 100644
--- a/streams/src/test/java/org/apache/kafka/streams/processor/internals/GlobalStateManagerImplTest.java
+++ b/streams/src/test/java/org/apache/kafka/streams/processor/internals/GlobalStateManagerImplTest.java
@@ -66,6 +66,7 @@ import static org.hamcrest.CoreMatchers.equalTo;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
@@ -172,6 +173,29 @@ public class GlobalStateManagerImplTest {
     }
 
     @Test
+    public void shouldThrowStreamsExceptionForOldTopicPartitions() throws IOException {
+        final HashMap<TopicPartition, Long> expectedOffsets = new HashMap<>();
+        expectedOffsets.put(t1, 1L);
+        expectedOffsets.put(t2, 1L);
+        expectedOffsets.put(t3, 1L);
+        expectedOffsets.put(t4, 1L);
+
+        // add an old topic (a topic not associated with any global state store)
+        final HashMap<TopicPartition, Long> startOffsets = new HashMap<>(expectedOffsets);
+        final TopicPartition tOld = new TopicPartition("oldTopic", 1);
+        startOffsets.put(tOld, 1L);
+
+        // start with a checkpoint file will all topic-partitions: expected and old (not
+        // associated with any global state store).
+        final OffsetCheckpoint checkpoint = new OffsetCheckpoint(checkpointFile);
+        checkpoint.write(startOffsets);
+
+        // initialize will throw exception
+        final StreamsException e = assertThrows(StreamsException.class, () -> stateManager.initialize());
+        assertThat(e.getMessage(), equalTo("Encountered a topic-partition not associated with any global state store"));
+    }
+
+    @Test
     public void shouldNotDeleteCheckpointFileAfterLoaded() throws IOException {
         writeCheckpoint();
         stateManager.initialize();