mjsax commented on a change in pull request #8833:
URL: https://github.com/apache/kafka/pull/8833#discussion_r438370089
##########
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());
+ for (final Map.Entry<TopicPartition, Long> entry :
consumedOffsets.entrySet()) {
+ final TopicPartition partition = entry.getKey();
+ Long offset = partitionGroup.headRecordOffset(partition);
+ if (offset == null) {
+ try {
+ offset = mainConsumer.position(partition);
+ } catch (final TimeoutException error) {
+ // the `consumer.position()` call should never
block, because we know that we did process data
+ // for the requested partition and thus the
consumer should have a valid local position
+ // that it can return immediately
+
+ // hence, a `TimeoutException` indicates a bug and
thus we rethrow it as fatal `IllegalStateException`
+ throw new IllegalStateException(error);
+ } catch (final KafkaException fatal) {
+ throw new StreamsException(fatal);
+ }
+ }
+ final long partitionTime = partitionTimes.get(partition);
+ committableOffsets.put(partition, new
OffsetAndMetadata(offset, encodeTimestamp(partitionTime)));
+ }
break;
+ case CLOSED:
+ throw new IllegalStateException("Illegal state " + state() + "
while getting commitable offsets for active task " + id);
+
+ default:
+ throw new IllegalStateException("Unknown state " + state() + "
while post committing active task " + id);
+ }
+
+ return committableOffsets;
+ }
+
+ @Override
+ public void postCommit() {
+ switch (state()) {
case RESTORING:
- commitNeeded = false;
+ case RUNNING:
+ case SUSPENDED:
commitRequested = false;
+ commitNeeded = false;
+
+ if (state() == State.RESTORING || state() == State.SUSPENDED) {
+ writeCheckpointIfNeed();
+ } else if (!eosEnabled) { // if RUNNING, checkpoint only for
non-eos
+ writeCheckpointIfNeed();
+ }
- stateMgr.checkpoint(checkpointableOffsets());
+ if (state() == State.SUSPENDED) {
+ partitionGroup.clear();
Review comment:
Sure -- but if they change it (I also did the change originally) a unit
test fails anyway :)
----------------------------------------------------------------
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]