-
Notifications
You must be signed in to change notification settings - Fork 13.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
KAFKA-4393: Improve invalid/negative TS handling #2117
Closed
Closed
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
23e08d1
KAFKA-4393: Improve invalid/negative TS handling
mjsax 9a732d5
addressed github comments
mjsax 0090d56
addressed Github comments
mjsax 9d7813b
updated parameter and class names
mjsax 6508e7f
Addressed Damian's comments
mjsax 67a4eb8
Damian's and Guozhang's comments
mjsax 53b3996
removed 'processed' sensor
mjsax 477bdb8
Change metric to use Rate()
mjsax File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 0 additions & 40 deletions
40
...ms/src/main/java/org/apache/kafka/streams/processor/ConsumerRecordTimestampExtractor.java
This file was deleted.
Oops, something went wrong.
77 changes: 77 additions & 0 deletions
77
streams/src/main/java/org/apache/kafka/streams/processor/ExtractRecordMetadataTimestamp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.kafka.streams.processor; | ||
|
||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
|
||
/** | ||
* Retrieves embedded metadata timestamps from Kafka messages. | ||
* If a record has a negative (invalid) timestamp value, an error handler method is called. | ||
* <p> | ||
* Embedded metadata timestamp was introduced in "KIP-32: Add timestamps to Kafka message" for the new | ||
* 0.10+ Kafka message format. | ||
* <p> | ||
* Here, "embedded metadata" refers to the fact that compatible Kafka producer clients automatically and | ||
* transparently embed such timestamps into message metadata they send to Kafka, which can then be retrieved | ||
* via this timestamp extractor. | ||
* <p> | ||
* If the embedded metadata timestamp represents <i>CreateTime</i> (cf. Kafka broker setting | ||
* {@code message.timestamp.type} and Kafka topic setting {@code log.message.timestamp.type}), | ||
* this extractor effectively provides <i>event-time</i> semantics. | ||
* If <i>LogAppendTime</i> is used as broker/topic setting to define the embedded metadata timestamps, | ||
* using this extractor effectively provides <i>ingestion-time</i> semantics. | ||
* <p> | ||
* If you need <i>processing-time</i> semantics, use {@link WallclockTimestampExtractor}. | ||
* | ||
* @see FailOnInvalidTimestamp | ||
* @see LogAndSkipOnInvalidTimestamp | ||
* @see UsePreviousTimeOnInvalidTimestamp | ||
* @see WallclockTimestampExtractor | ||
*/ | ||
abstract class ExtractRecordMetadataTimestamp implements TimestampExtractor { | ||
|
||
/** | ||
* Extracts the embedded metadata timestamp from the given {@link ConsumerRecord}. | ||
* | ||
* @param record a data record | ||
* @param previousTimestamp the latest extracted valid timestamp of the current record's partition˙ (could be -1 if unknown) | ||
* @return the embedded metadata timestamp of the given {@link ConsumerRecord} | ||
*/ | ||
@Override | ||
public long extract(final ConsumerRecord<Object, Object> record, final long previousTimestamp) { | ||
final long timestamp = record.timestamp(); | ||
|
||
if (timestamp < 0) { | ||
return onInvalidTimestamp(record, timestamp, previousTimestamp); | ||
} | ||
|
||
return timestamp; | ||
} | ||
|
||
/** | ||
* Called if no valid timestamp is embedded in the record meta data. | ||
* | ||
* @param record a data record | ||
* @param recordTimestamp the timestamp extractor from the record | ||
* @param previousTimestamp the latest extracted valid timestamp of the current record's partition˙ (could be -1 if unknown) | ||
* @return a new timestamp for the record (if negative, record will not be processed but dropped silently) | ||
*/ | ||
public abstract long onInvalidTimestamp(final ConsumerRecord<Object, Object> record, | ||
final long recordTimestamp, | ||
final long previousTimestamp); | ||
} |
68 changes: 68 additions & 0 deletions
68
streams/src/main/java/org/apache/kafka/streams/processor/FailOnInvalidTimestamp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.kafka.streams.processor; | ||
|
||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.apache.kafka.streams.errors.StreamsException; | ||
|
||
/** | ||
* Retrieves embedded metadata timestamps from Kafka messages. | ||
* If a record has a negative (invalid) timestamp value, this extractor raises an exception. | ||
* <p> | ||
* Embedded metadata timestamp was introduced in "KIP-32: Add timestamps to Kafka message" for the new | ||
* 0.10+ Kafka message format. | ||
* <p> | ||
* Here, "embedded metadata" refers to the fact that compatible Kafka producer clients automatically and | ||
* transparently embed such timestamps into message metadata they send to Kafka, which can then be retrieved | ||
* via this timestamp extractor. | ||
* <p> | ||
* If the embedded metadata timestamp represents <i>CreateTime</i> (cf. Kafka broker setting | ||
* {@code message.timestamp.type} and Kafka topic setting {@code log.message.timestamp.type}), | ||
* this extractor effectively provides <i>event-time</i> semantics. | ||
* If <i>LogAppendTime</i> is used as broker/topic setting to define the embedded metadata timestamps, | ||
* using this extractor effectively provides <i>ingestion-time</i> semantics. | ||
* <p> | ||
* If you need <i>processing-time</i> semantics, use {@link WallclockTimestampExtractor}. | ||
* | ||
* @see LogAndSkipOnInvalidTimestamp | ||
* @see UsePreviousTimeOnInvalidTimestamp | ||
* @see WallclockTimestampExtractor | ||
*/ | ||
public class FailOnInvalidTimestamp extends ExtractRecordMetadataTimestamp { | ||
|
||
/** | ||
* Raises an exception on every call. | ||
* | ||
* @param record a data record | ||
* @param recordTimestamp the timestamp extractor from the record | ||
* @param previousTimestamp the latest extracted valid timestamp of the current record's partition˙ (could be -1 if unknown) | ||
* @return nothing; always raises an exception | ||
* @throws StreamsException on every invocation | ||
*/ | ||
@Override | ||
public long onInvalidTimestamp(final ConsumerRecord<Object, Object> record, | ||
final long recordTimestamp, | ||
final long previousTimestamp) | ||
throws StreamsException { | ||
throw new StreamsException("Input record " + record + " has invalid (negative) timestamp. " + | ||
"Possibly because a pre-0.10 producer client was used to write this record to Kafka without embedding a timestamp, " + | ||
"or because the input topic was created before upgrading the Kafka cluster to 0.10+. " + | ||
"Use a different TimestampExtractor to process this data."); | ||
} | ||
|
||
} |
69 changes: 69 additions & 0 deletions
69
streams/src/main/java/org/apache/kafka/streams/processor/LogAndSkipOnInvalidTimestamp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.kafka.streams.processor; | ||
|
||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Retrieves embedded metadata timestamps from Kafka messages. | ||
* If a record has a negative (invalid) timestamp value the timestamp is returned as-is; | ||
* in addition, a WARN message is logged in your application. | ||
* Returning the timestamp as-is results in dropping the record, i.e., the record will not be processed. | ||
* <p> | ||
* Embedded metadata timestamp was introduced in "KIP-32: Add timestamps to Kafka message" for the new | ||
* 0.10+ Kafka message format. | ||
* <p> | ||
* Here, "embedded metadata" refers to the fact that compatible Kafka producer clients automatically and | ||
* transparently embed such timestamps into message metadata they send to Kafka, which can then be retrieved | ||
* via this timestamp extractor. | ||
* <p> | ||
* If the embedded metadata timestamp represents <i>CreateTime</i> (cf. Kafka broker setting | ||
* {@code message.timestamp.type} and Kafka topic setting {@code log.message.timestamp.type}), | ||
* this extractor effectively provides <i>event-time</i> semantics. | ||
* If <i>LogAppendTime</i> is used as broker/topic setting to define the embedded metadata timestamps, | ||
* using this extractor effectively provides <i>ingestion-time</i> semantics. | ||
* <p> | ||
* If you need <i>processing-time</i> semantics, use {@link WallclockTimestampExtractor}. | ||
* | ||
* @see FailOnInvalidTimestamp | ||
* @see UsePreviousTimeOnInvalidTimestamp | ||
* @see WallclockTimestampExtractor | ||
*/ | ||
public class LogAndSkipOnInvalidTimestamp extends ExtractRecordMetadataTimestamp { | ||
private static final Logger log = LoggerFactory.getLogger(LogAndSkipOnInvalidTimestamp.class); | ||
|
||
/** | ||
* Writes a log WARN message when the extracted timestamp is invalid (negative) but returns the invalid timestamp as-is, | ||
* which ultimately causes the record to be skipped and not to be processed. | ||
* | ||
* @param record a data record | ||
* @param recordTimestamp the timestamp extractor from the record | ||
* @param previousTimestamp the latest extracted valid timestamp of the current record's partition˙ (could be -1 if unknown) | ||
* @return the originally extracted timestamp of the record | ||
*/ | ||
@Override | ||
public long onInvalidTimestamp(final ConsumerRecord<Object, Object> record, | ||
final long recordTimestamp, | ||
final long previousTimestamp) { | ||
log.warn("Input record {} will be dropped because it has an invalid (negative) timestamp.", record); | ||
return recordTimestamp; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this absolutely needed? cc @ijuma
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO - it is useful and i don't see why it should not be allowed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm OK with it although @junrao seemed less convinced last time the subject came up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did talk to @ijuma and @guozhangwang and both told me they are ok with the change. Why no allowing hamcrest for tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As long as the usage is valid, this is fine for me.