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

Add transaction / bulk send support in producer #7

Open
er1c opened this issue Nov 29, 2021 · 0 comments
Open

Add transaction / bulk send support in producer #7

er1c opened this issue Nov 29, 2021 · 0 comments

Comments

@er1c
Copy link

er1c commented Nov 29, 2021

Some psuedo code, that doesn't actually work because initTransactions() needs to only be called once

  final def produceWithTransaction[T <: Topic, M: Encoder: SchemaFor](
    topic: T,
    partitionKey: M => String,
    messages: Iterable[M]
  )(implicit topicSchema: TopicSchema[T, M]): Future[Iterable[ProducerResult[T, M]]] = {
    // This is the workflow documented in http://kafka.apache.org/0110/javadoc/org/apache/kafka/clients/producer/KafkaProducer.html
    initTransactions()

    try {
      beginTransaction()
      // TODO: messages.par.map...???
      Future.sequence(messages.map { m => produce(topic, partitionKey(m), m) }).map{ ret =>
        commitTransaction()
        ret
      }
    } catch {
      case ex if Producer.isFatalKafkaException(ex) =>
        // We can't recover from these exceptions, so our only option is to close the producer and exit.
        close()
        Future.failed(ex)
      case ex: KafkaException =>
        abortTransaction()
        Future.failed(ex)
    }
  }

  private def isFatalKafkaException(ex: Throwable): Boolean = ex match {
    case _: ProducerFencedException => true
    case _: OutOfOrderSequenceException => true
    case _: AuthorizationException => true
    case _ => false
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant