abbccdda commented on a change in pull request #8833:
URL: https://github.com/apache/kafka/pull/8833#discussion_r437829555



##########
File path: 
streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamTask.java
##########
@@ -247,82 +246,23 @@ public void completeRestoration() {
         }
     }
 
-    /**
-     * <pre>
-     * the following order must be followed:
-     *  1. first close topology to make sure all cached records in the 
topology are processed
-     *  2. then flush the state, send any left changelog records
-     *  3. then flush the record collector
-     *  4. then commit the record collector -- for EOS this is the 
synchronization barrier
-     *  5. then checkpoint the state manager -- even if we crash before this 
step, EOS is still guaranteed
-     * </pre>
-     *
-     * @throws TaskMigratedException if committing offsets failed (non-EOS)
-     *                               or if the task producer got fenced (EOS)
-     */
-    @Override
-    public void prepareSuspend() {
-        switch (state()) {
-            case CREATED:
-            case SUSPENDED:
-                // do nothing
-                log.trace("Skip prepare suspending since state is {}", 
state());
-
-                break;
-
-            case RESTORING:
-                stateMgr.flush();
-                log.info("Prepare suspending restoring");
-
-                break;
-
-            case RUNNING:
-                closeTopology(true);
-
-                stateMgr.flush();
-                recordCollector.flush();
-
-                log.info("Prepare suspending running");
-
-                break;
-
-            case CLOSED:
-                throw new IllegalStateException("Illegal state " + state() + " 
while suspending active task " + id);
-
-            default:
-                throw new IllegalStateException("Unknown state " + state() + " 
while suspending active task " + id);
-        }
-    }
-
     @Override
     public void suspend() {
         switch (state()) {
             case CREATED:
             case SUSPENDED:
-                // do nothing
-                log.trace("Skip suspending since state is {}", state());
-
-                break;
-
-            case RUNNING:
-                stateMgr.checkpoint(checkpointableOffsets());
-                partitionGroup.clear();
-
-                transitionTo(State.SUSPENDED);
-                log.info("Suspended running");
+                log.info("Skip suspending since state is {}", state());
 
                 break;
 
             case RESTORING:
-                // we just checkpoint the position that we've restored up to 
without
-                // going through the commit process
-                stateMgr.checkpoint(emptyMap());
-
-                // we should also clear any buffered records of a task when 
suspending it
-                partitionGroup.clear();
-
-                transitionTo(State.SUSPENDED);
-                log.info("Suspended restoring");
+            case RUNNING:
+                try {
+                    closeTopology();

Review comment:
       Is this addressed?

##########
File path: 
streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamTask.java
##########
@@ -378,42 +320,88 @@ public void prepareCommit() {
                 break;
 
             case CREATED:
-            case SUSPENDED:
             case CLOSED:
                 throw new IllegalStateException("Illegal state " + state() + " 
while preparing active task " + id + " for committing");
 
             default:
                 throw new IllegalStateException("Unknown state " + state() + " 
while preparing active task " + id + " for committing");
         }
+
+        return committableOffsetsAndMetadata();
     }
 
-    @Override
-    public void postCommit() {
+    private Map<TopicPartition, OffsetAndMetadata> 
committableOffsetsAndMetadata() {
+        final Map<TopicPartition, OffsetAndMetadata> committableOffsets;
+
         switch (state()) {
-            case RUNNING:
-                commitNeeded = false;
-                commitRequested = false;
+            case CREATED:
+            case RESTORING:
+                committableOffsets = Collections.emptyMap();
 
-                if (!eosEnabled) {
-                    stateMgr.checkpoint(checkpointableOffsets());
-                }
+                break;
 
-                log.debug("Committed");
+            case RUNNING:
+            case SUSPENDED:
+                final Map<TopicPartition, Long> partitionTimes = 
extractPartitionTimes();
+
+                committableOffsets = new HashMap<>(consumedOffsets.size());

Review comment:
       +1, this seems not really necessary atm.

##########
File path: 
streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamTask.java
##########
@@ -813,6 +727,10 @@ private void updateProcessorContext(final StampedRecord 
record, final ProcessorN
      * Currently only changelog topic offsets need to be checkpointed.
      */
     private Map<TopicPartition, Long> checkpointableOffsets() {
+        if (state() == State.RESTORING) {

Review comment:
       Is this logic necessary? I don't think we would populate data in record 
collector or consumed offsets until we start processing?

##########
File path: 
streams/src/main/java/org/apache/kafka/streams/processor/internals/TaskManager.java
##########
@@ -623,7 +618,11 @@ private long sumOfChangelogOffsets(final TaskId id, final 
Map<TopicPartition, Lo
     }
 
     private void closeTaskDirty(final Task task) {
-        task.prepareCloseDirty();
+        try {
+            task.suspend();
+        } catch (final RuntimeException swallow) {

Review comment:
       Done

##########
File path: 
streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamTask.java
##########
@@ -506,19 +455,18 @@ public void update(final Set<TopicPartition> 
topicPartitions, final ProcessorTop
 
     @Override
     public void closeAndRecycleState() {
-        prepareClose(true);
-
+        suspendCleanAndPrepareCommit();
         writeCheckpointIfNeed();
 
         switch (state()) {
             case CREATED:
-            case RUNNING:
-            case RESTORING:
             case SUSPENDED:
                 stateMgr.recycle();
                 recordCollector.close();
                 break;
 
+            case RESTORING: // we should have transitioned to `SUSPENDED` 
already

Review comment:
       Maybe not necessary after a second thought. However, one more question: 
why not making `closeAndRecycleState` idempotent as well?




----------------------------------------------------------------
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:
[email protected]


Reply via email to