Skip to content

Conversation

MayerRoman
Copy link
Contributor

@MayerRoman MayerRoman commented Dec 20, 2016

[FLINK-4616] Kafka consumer doesn't store last emmited watermarks per partition in state.

@MayerRoman MayerRoman changed the title [FLINK-4616] Added functionality through which watermarks for each pa… [FLINK-4616] Added functionality through which watermarks for each partition are saved and loaded via checkpointing mechanism Dec 20, 2016
…rtition are saved and loaded via checkpointing mechanism
@MayerRoman
Copy link
Contributor Author

MayerRoman commented Dec 22, 2016

I think that the changes that I propose eliminates the possibility of starting with checkpoints created before my code changes.

Because now it saves ListState<Tuple2<KafkaTopicPartition, Tuple2<Long, Long>>> (partition + offset + watermark).
And before it saved ListState<Tuple2<KafkaTopicPartition, Long>> (partition + offset).

(I mean checkpoints version later then 1.1.
Recently added backward compatibility with 1.1 snapshots is taken into account in my commit, with it, I think, everything is ok)

Please advise me how to repair backward compatibility.

I have some idea of how to implement it:

  1. somehow verify returned from stateStore.getSerializableListState(..) object
    in initializeState method
    https://github.com/apache/flink/pull/3031/files?diff=unified#diff-06bf4a7f73d98ef91309154654563475R321

is it
ListState<Tuple2<KafkaTopicPartition, Long>>
or
ListState<Tuple2<KafkaTopicPartition, Tuple<Long, Long>>>

  1. Use for saving watermark separate state-object.

Or it is necessary implement different way.

I would be grateful for help.

@tzulitai
Copy link
Contributor

Thank you for the contribution @MayerRoman. Just want to let you know that I've noticed this PR, and I think the issue is definitely something we want to fix. I'll allocate some time this week to review the PR.

@MayerRoman
Copy link
Contributor Author

Thank you Tzu-Li Tai.

Copy link
Contributor

@tzulitai tzulitai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MayerRoman I've done a first pass review, I'll definitely do a second detailed pass once we sort the following problems out:

  1. I think it'll make sense to have a new specific state class that wraps a topic-partition's offset and watermark as checkpointed state.

  2. This feature is expected to be fixed for 1.3.0, which means savepoints taken during 1.2 and 1.1 will be considered as old version checkpoints. Therefore, I think the original Tuple2<KafkaTopicPartition, Long> state will first be passed through the old restoreState() method. Then, in initializeState(), we can try to bridge the state type change from the old one to the new one. I'm still confirming this though, as this method call pattern for old version state might only be the case for 1.1 -> 1.2, and not for 1.2 -> 1.3.

  3. There's an important thing missing in this change: after a restore with watermarks, when starting to run the source, you'll need to check if a watermark should first be emitted from the source considering the already existing watermarks across its subscribed partitions. I don't think this PR has addressed this yet.

Let me know what you think about the above :-)

private SerializedValue<AssignerWithPunctuatedWatermarks<T>> punctuatedWatermarkAssigner;

private transient ListState<Tuple2<KafkaTopicPartition, Long>> offsetsStateForCheckpoint;
private transient ListState<Tuple2<KafkaTopicPartition, Tuple2<Long, Long>>> offsetsAndWatermarksStateForCheckpoint;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should switch to have a specific checkpointed state object instead of continuing to "extend" the original Tuple. This will also be helpful for compatibility for any future changes to the checkpointed state.

Tuple2.of(kafkaTopicPartitionLongEntry.getKey(), kafkaTopicPartitionLongEntry.getValue()));
for (Map.Entry<KafkaTopicPartition, Tuple2<Long, Long>> kafkaTopicPartitionOffsetAndWatermark : restoreToOffsetAndWatermark.entrySet()) {
offsetsAndWatermarksStateForCheckpoint.add(
Tuple2.of(kafkaTopicPartitionOffsetAndWatermark.getKey(), kafkaTopicPartitionOffsetAndWatermark.getValue()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a specific checkpoint state object will also be helpful for code readability in situations like this one (it's quite tricky to understand quickly what the key / value refers to, as well as some of the f0, f1 calls in other parts of the PR. I know the previous code used f0 and f1 also, but I think it's a good opportunity to improve that).


case NO_TIMESTAMPS_WATERMARKS: {

for (KafkaTopicPartitionState<KPH> partition : allPartitions) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excessive empty line above this line.


default:
// cannot happen, add this as a guard for the future
throw new RuntimeException();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to have a reason message here.


default:
// cannot happen, add this as a guard for the future
throw new RuntimeException();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to have a reason message here.

return partitionWatermark;
}

void setCurrentWatermarkTimestamp(long watermarkTimestamp) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other methods seem to be public (although they can actually be package-private). Should we stay consistent with that here?

@tzulitai
Copy link
Contributor

tzulitai commented Jan 26, 2017

A re-clarification about backwards compatibility for state type change:
Currently, one limitation for compatible applications across savepoint restore is that you can't change the type of state otherwise state restore will fail, therefore not compatible. The only work around, is to have another field as the new state with the new type, and somehow try to "encode" / "decode" the watermark state into / from the original Tuple2<KafkaTopicPartition, Long>. I don't think this is easily possible ...

At the same time, there was recent discussion about letting the window operators also checkpoint watermarks. So perhaps we might not need to let the Kafka sources checkpoint watermarks in the end, if the window operators already take care of restoring the previous event time.
What I'm curious about right now is whether or not in the future, redistributions of Kafka partition states across source subtasks will work well with the checkpointed watermarks in the downstream window operators. I don't think there should be a problem, i.e. late elements should still be dropped by the window operators in that case.

@aljoscha can you perhaps help clarify this?

@rmetzger
Copy link
Contributor

I think this PR should also include a test for the added feature.

@MayerRoman
Copy link
Contributor Author

I hope that I ended up with another issue, and I come back to this.

First, I want to ask a question that perhaps remove all the others.

Tzu-Li Tai, did I understand correctly that if discussion about letting the window operators checkpoint watermarks lead to the decision to implement this functionality in the window operators, the need to preserve the state of watermarks in Kafka consumer will disappear?

@tzulitai
Copy link
Contributor

tzulitai commented Feb 13, 2017

Hi @MayerRoman! Thank you for coming back to this issue.

I had a quick chat offline with @aljoscha about whether or not it'll be reasonable to add this. Either your approach in this PR or letting window operators checkpoint watermarks will both solve the issue of late elements after restore. However, we thought that we should let the user function code be free of responsibility of checkpointing watermarks, and let user code simply leave that to the streaming internals (checkpointed by window operators, and perhaps also by source operators).

So, essentially, the Kafka consumer should not need to checkpoint watermarks, and we can close this PR and the JIRA ticket. Very sorry for the late discussion on this, and having you worked on it already.

Let we know what you think and whether or not you agree :-)

@MayerRoman
Copy link
Contributor Author

Hello, Tzu-Li Tai!
I think you make a good decision and agree to the fact that the PR and JIRA ticket can be closed.

Do not worry about the done work, I got good experience in the process.

@tzulitai
Copy link
Contributor

Thanks. Can you please close this PR then :-D ? I'll close the JIRA.

@MayerRoman
Copy link
Contributor Author

Ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants