-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-7211: MM should handle TimeoutException in commitSync #5492
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
Closed
Closed
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0defde1
KAFKA-7211: MM should handle TimeoutException in commitSync
huxi-2b 0228e18
Fix incorrect warning messages on tracking failed commits
huxi-2b b10c6ac
address lindong28's comments
huxi-2b b0c33b7
address lindong's comments
huxi-2b e02bf6e
Fixed the incorrect comment for MM
huxi-2b 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 hidden or 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 |
|---|---|---|
|
|
@@ -33,12 +33,13 @@ import org.apache.kafka.clients.producer.internals.ErrorLoggingCallback | |
| import org.apache.kafka.clients.producer.{KafkaProducer, ProducerConfig, ProducerRecord, RecordMetadata} | ||
| import org.apache.kafka.common.{KafkaException, TopicPartition} | ||
| import org.apache.kafka.common.serialization.{ByteArrayDeserializer, ByteArraySerializer} | ||
| import org.apache.kafka.common.utils.Utils | ||
| import org.apache.kafka.common.errors.WakeupException | ||
| import org.apache.kafka.common.utils.{Time, Utils} | ||
| import org.apache.kafka.common.errors.{TimeoutException, WakeupException} | ||
| import org.apache.kafka.common.record.RecordBatch | ||
|
|
||
| import scala.collection.JavaConverters._ | ||
| import scala.collection.mutable.HashMap | ||
| import scala.util.{Failure, Success, Try} | ||
| import scala.util.control.ControlThrowable | ||
|
|
||
| /** | ||
|
|
@@ -69,6 +70,8 @@ object MirrorMaker extends Logging with KafkaMetricsGroup { | |
| private var offsetCommitIntervalMs = 0 | ||
| private var abortOnSendFailure: Boolean = true | ||
| @volatile private var exitingOnSendFailure: Boolean = false | ||
| private var lastSuccessfulCommitTime = -1L | ||
| private val time = Time.SYSTEM | ||
|
|
||
| // If a message send failed after retries are exhausted. The offset of the messages will also be removed from | ||
| // the unacked offset list to avoid offset commit being stuck on that offset. In this case, the offset of that | ||
|
|
@@ -267,24 +270,45 @@ object MirrorMaker extends Logging with KafkaMetricsGroup { | |
| consumers.map(consumer => new ConsumerWrapper(consumer, customRebalanceListener, whitelist)) | ||
| } | ||
|
|
||
| def commitOffsets(consumerWrapper: ConsumerWrapper) { | ||
| def commitOffsets(consumerWrapper: ConsumerWrapper): Unit = { | ||
| if (!exitingOnSendFailure) { | ||
| trace("Committing offsets.") | ||
| try { | ||
| consumerWrapper.commit() | ||
| } catch { | ||
| case e: WakeupException => | ||
| // we only call wakeup() once to close the consumer, | ||
| // so if we catch it in commit we can safely retry | ||
| // and re-throw to break the loop | ||
| var retry = 0 | ||
| var retryNeeded = true | ||
| while (retryNeeded) { | ||
| trace("Committing offsets.") | ||
| try { | ||
| consumerWrapper.commit() | ||
| throw e | ||
| lastSuccessfulCommitTime = time.milliseconds | ||
| retryNeeded = false | ||
| } catch { | ||
| case e: WakeupException => | ||
| // we only call wakeup() once to close the consumer, | ||
| // so if we catch it in commit we can safely retry | ||
| // and re-throw to break the loop | ||
| commitOffsets(consumerWrapper) | ||
| throw e | ||
|
|
||
| case _: TimeoutException => | ||
| Try(consumerWrapper.consumer.listTopics) match { | ||
| case Success(visibleTopics) => | ||
| consumerWrapper.offsets.retain((tp, _) => visibleTopics.containsKey(tp.topic)) | ||
| case Failure(e) => | ||
| warn("Failed to list all authorized topics after committing offsets timed out: ", e) | ||
| } | ||
|
|
||
| case _: CommitFailedException => | ||
| warn("Failed to commit offsets because the consumer group has rebalanced and assigned partitions to " + | ||
| "another instance. If you see this regularly, it could indicate that you need to either increase " + | ||
| s"the consumer's ${ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG} or reduce the number of records " + | ||
| s"handled on each iteration with ${ConsumerConfig.MAX_POLL_RECORDS_CONFIG}") | ||
| retry += 1 | ||
| warn("Failed to commit offsets because the offset commit request processing can not be completed in time. " + | ||
| s"If you see this regularly, it could indicate that you need to increase the consumer's ${ConsumerConfig.DEFAULT_API_TIMEOUT_MS_CONFIG} " + | ||
| s"Last successful offset commit timestamp=${lastSuccessfulCommitTime}, retry count=${retry}") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nits: we can replace |
||
| Thread.sleep(100) | ||
|
|
||
| case _: CommitFailedException => | ||
| retryNeeded = false | ||
| warn("Failed to commit offsets because the consumer group has rebalanced and assigned partitions to " + | ||
| "another instance. If you see this regularly, it could indicate that you need to either increase " + | ||
| s"the consumer's ${ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG} or reduce the number of records " + | ||
| s"handled on each iteration with ${ConsumerConfig.MAX_POLL_RECORDS_CONFIG}") | ||
| } | ||
| } | ||
| } else { | ||
| info("Exiting on send failure, skip committing offsets.") | ||
|
|
@@ -422,14 +446,15 @@ object MirrorMaker extends Logging with KafkaMetricsGroup { | |
| } | ||
|
|
||
| // Visible for testing | ||
| private[tools] class ConsumerWrapper(consumer: Consumer[Array[Byte], Array[Byte]], | ||
| private[tools] class ConsumerWrapper(private[tools] val consumer: Consumer[Array[Byte], Array[Byte]], | ||
| customRebalanceListener: Option[ConsumerRebalanceListener], | ||
| whitelistOpt: Option[String]) { | ||
| val regex = whitelistOpt.getOrElse(throw new IllegalArgumentException("New consumer only supports whitelist.")) | ||
| var recordIter: java.util.Iterator[ConsumerRecord[Array[Byte], Array[Byte]]] = null | ||
|
|
||
| // We manually maintain the consumed offsets for historical reasons and it could be simplified | ||
| private val offsets = new HashMap[TopicPartition, Long]() | ||
| // Visible for testing | ||
| private[tools] val offsets = new HashMap[TopicPartition, Long]() | ||
|
|
||
| def init() { | ||
| debug("Initiating consumer") | ||
|
|
@@ -473,7 +498,7 @@ object MirrorMaker extends Logging with KafkaMetricsGroup { | |
| } | ||
|
|
||
| def commit() { | ||
| consumer.commitSync(offsets.map { case (tp, offset) => (tp, new OffsetAndMetadata(offset, ""))}.asJava) | ||
| consumer.commitSync(offsets.map { case (tp, offset) => (tp, new OffsetAndMetadata(offset)) }.asJava) | ||
| offsets.clear() | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or 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
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.
not related to this PR. Can we update the comments above:
"There are N mirror maker threads each having one KafkaConsumer instance" ?