Uploaded image for project: 'Spark'
  1. Spark
  2. SPARK-21873

CachedKafkaConsumer throws NonLocalReturnControl during fetching from Kafka

    XMLWordPrintableJSON

    Details

    • Type: Improvement
    • Status: Resolved
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.1.0, 2.1.1, 2.2.0
    • Fix Version/s: 2.3.0
    • Component/s: Structured Streaming
    • Labels:
      None

      Description

      In Scala, using `return` inside a function causes a `NonLocalReturnControl` exception to be thrown and caught in order to escape the current scope.

      While profiling Structured Streaming in production, it clearly shows:

      This happens during a 1 minute profiling session on a single executor. The code is:

      while (toFetchOffset != UNKNOWN_OFFSET) {
            try {
              return fetchData(toFetchOffset, untilOffset, pollTimeoutMs, failOnDataLoss)
            } catch {
              case e: OffsetOutOfRangeException =>
                // When there is some error thrown, it's better to use a new consumer to drop all cached
                // states in the old consumer. We don't need to worry about the performance because this
                // is not a common path.
                resetConsumer()
                reportDataLoss(failOnDataLoss, s"Cannot fetch offset $toFetchOffset", e)
                toFetchOffset = getEarliestAvailableOffsetBetween(toFetchOffset, untilOffset)
            }
          }
      

      This happens because this method is converted to a function which is ran inside:

      private def runUninterruptiblyIfPossible[T](body: => T): T
      

      We should avoid using `return` in general, and here specifically as it is a hot path for applications using Kafka.

        Attachments

          Activity

            People

            • Assignee:
              Yuval.Itzchakov Yuval Itzchakov
              Reporter:
              Yuval.Itzchakov Yuval Itzchakov
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: