Skip to content
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

Move data perparation to the batch implementation #873

Merged
merged 1 commit into from
Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions core/src/main/scala/akka/kafka/internal/CommittableSources.scala
Original file line number Diff line number Diff line change
Expand Up @@ -135,28 +135,21 @@ private[kafka] class KafkaAsyncConsumerCommitterRef(consumerActor: ActorRef, com

def commit(batch: CommittableOffsetBatch): Future[Done] = batch match {
case b: CommittableOffsetBatchImpl =>
val groupIdOffsetMaps: Map[String, Map[GroupTopicPartition, OffsetAndMetadata]] =
b.offsetsAndMetadata.groupBy(_._1.groupId)
val futures = groupIdOffsetMaps.map {
case (groupId, offsetsMap) =>
val committer = b.committers.getOrElse(
groupId,
throw new IllegalStateException(s"Unknown committer, got [$groupId]")
)
val offsets: Map[TopicPartition, OffsetAndMetadata] = offsetsMap.map {
case (gtp, offset) => gtp.topicPartition -> offset
}
committer.sendCommit(Commit(offsets))
val futures = b.groupIdOffsetMaps.map {
case (groupId, offsets) =>
b.committerFor(groupId).sendCommit(Commit(offsets))
}
Future.sequence(futures).map(_ => Done)

case _ =>
throw new IllegalArgumentException(
s"Unknown CommittableOffsetBatch, got [${batch.getClass.getName}], " +
s"expected [${classOf[CommittableOffsetBatchImpl].getName}]"
)
case _ => failForUnexpectedImplementation(batch)
}

private def failForUnexpectedImplementation(batch: CommittableOffsetBatch) =
throw new IllegalArgumentException(
s"Unknown CommittableOffsetBatch, got [${batch.getClass.getName}], " +
s"expected [${classOf[CommittableOffsetBatchImpl].getName}]"
)

private def sendCommit(msg: AnyRef): Future[Done] = {
import akka.pattern.ask
consumerActor
Expand Down
14 changes: 14 additions & 0 deletions core/src/main/scala/akka/kafka/internal/MessageBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,20 @@ private[kafka] final class CommittableOffsetBatchImpl(
case batch: CommittableOffsetBatch => updatedWithBatch(batch)
}

private[internal] def committerFor(groupId: String) = committers.getOrElse(
groupId,
throw new IllegalStateException(s"Unknown committer, got [$groupId]")
)

private[internal] def groupIdOffsetMaps: Map[String, Map[TopicPartition, OffsetAndMetadata]] =
offsetsAndMetadata.groupBy(_._1.groupId).map {
case (groupId, offsetsMap) =>
val offsets: Map[TopicPartition, OffsetAndMetadata] = offsetsMap.map {
case (gtp, offset) => gtp.topicPartition -> offset
}
(groupId, offsets)
}

private def updatedWithOffset(committableOffset: CommittableOffset): CommittableOffsetBatch = {
val partitionOffset = committableOffset.partitionOffset
val key = partitionOffset.key
Expand Down