You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

Status

Current state: Under Discussion

Discussion thread

JIRA: KAFKA-7699 - Getting issue details... STATUS

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: 

Achored 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. 

Proposed Changes

The goal is to introduce an anchored wall-clock punctuation that will have similarities with that of triggering a cron job.

Questions

  • Whether to epoch-align the trigger time (like done with the window boundaries), or actually pull in a cron-like scheduling library like Quartz?
  • What should the API be for this new type of punctuation? Should we introduce a `schedule(String cronExpression, Punctuator callback)` interface for `ProcessingContext`? 

Compatibility, Deprecation, and Migration Plan

  • What impact (if any) will there be on existing users?
  • If we are changing behavior how will we phase out the older behavior?
  • If we need special migration tools, describe them here.
  • When will we remove the existing behavior?

Test Plan

Describe in few sentences how the KIP will be tested. We are mostly interested in system tests (since unit-tests are specific to implementation details). How will we know that the implementation works as expected? How will we know nothing broke?

Rejected Alternatives

If there are alternative ways of accomplishing the same thing, what were they? The purpose of this section is to motivate why the design is the way it is and not some other way.

  • No labels