Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

This page is meant as a template for writing a KIP. To create a KIP choose Tools->Copy on this page and modify with your content and replace the heading with the next KIP number and a description of your issue. Replace anything in italics with your own description.

Status

Current state:  [One of " Under Discussion", "Accepted", "Rejected"]

Discussion thread: here [Change the link from the KIP proposal email archive to your own email thread]

JIRA: here [Change the link from KAFKA-1 to your own ticket]

Jira
serverASF JIRA
serverId5aa69414-a9e9-3523-82ec-879b028fb15b
keyKAFKA-7699

Please keep the discussion on the mailing list rather than commenting on the wiki (wiki discussions get unwieldy fast).

Motivation

Kafka Streams do not provide a way to easily trigger periodic callbacks at specific times. Wall-clock time punctuation allow to schedule periodic callbacks based on wall-clock time progress, but the punctuation time starts when the punctuation is scheduled. As a result, the callback is triggered at a non-deterministic time. It would be nice to allow a punctuation to be triggered at a fixed/anchored time, independent of when the punctuation was registered. For instance, this will allow for triggering a punctuation at the start of every hour, i.e. HH:00:00.

Real life use cases can be taken from the energy sector. The automatic closed loop balancing of the power grid requires calculations to be published at the start of every 10th second. Today, this is solved by a work-around utilising a "has published this interval" flag and a state store to ensure a punctuation is only triggered once per interval: 

Code Block
titleAchored punctuation - work around
class PunctuateProcessor(
    private val hasForwardedStoreName: String,
    private val forwardTime: Duration,
) : ContextualProcessor<String, avro_value, String, avro_value>(),
    ILogging by Logging<PunctuateProcessor>() {
    private lateinit var hasForwardStore: WindowStore<String, Boolean>
    private lateinit var forwardSchedule: Cancellable

    private val hasForwardStoreKey = "hasPublished"

    override fun init(context: ProcessorContext<String, avro_value>) {
        super.init(context)
        this.hasForwardStore = context.getStateStore(hasForwardedStoreName)

        forwardSchedule =
            context().schedule(Duration.ofMillis(500), PunctuationType.WALL_CLOCK_TIME) { forwardRecordsIfTime() }
    }

    override fun process(record: Record<String, avro_value>) {
        // Store incoming records
    }

    private fun forwardRecordsIfTime() {
        val currentTime = Duration.ofMillis(context().currentSystemTimeMs())
        val flooredTime = Duration.ofMillis(floorTo10Second(currentTime.toMillis()))

        if (isTimeToForward(currentTime) && !hasForwardedThisInterval(flooredTime)) {
            forwardRecords()
            hasForwardStore.put(hasForwardStoreKey, true, flooredTime.toMillis())
        }
    }

    private fun isTimeToForward(currentTime: Duration): Boolean = (currentTime.toSecondsPart() % 10) >= (forwardTime.toSecondsPart() % 10)

    private fun hasForwardedThisInterval(intervalStart: Duration): Boolean =
        hasForwardStore.fetch(hasForwardStoreKey, intervalStart.toMillis()) ?: false

Generally, the work-around can be characterised as flag-polling, where the poll frequency is determined by a wall-clock punctuation. Hence, the punctuation trigger time is also only as precise as the wall-clock punctuation trigger interval. A more frequent wall-clock punctuation will give a more precise trigger time at the cost of firing an increased number of flag checks. This is a trade-off that can be removed with the anchored punctuation. Describe the problems you are trying to solve.

Public Interfaces

Briefly list any new interfaces that will be introduced as part of this proposal or any existing interfaces that will be removed or changed. The purpose of this section is to concisely call out the public contract that will come along with this feature.

...

  • Binary log format

  • The network protocol and api behavior

  • Any class in the public packages under clientsConfiguration, especially client configuration

    • org/apache/kafka/common/serialization

    • org/apache/kafka/common

    • org/apache/kafka/common/errors

    • org/apache/kafka/clients/producer

    • org/apache/kafka/clients/consumer (eventually, once stable)

  • Monitoring

  • Command line tools and arguments

  • Anything else that will likely break existing users in some way when they upgrade

Proposed Changes

The anchored wall-clock punctuation will have similarities with that of triggering a cron job

Describe the new thing you want to do in appropriate detail. This may be fairly extensive and have large subsections of its own. Or it may be a few sentences. Use judgement based on the scope of the change.

...