Flink Timer timer mechanism and specific implementation

Introduction to Timer

Timer timer is a mechanism provided by Flink Streaming API for sensing and using processing time/event event changes. The
most obvious way for timer is KeyedProcessFunction. Register Timer in its processElement() method, and then override its onTimer method as Timer trigger time The callback logic, according to the time characteristics:

  1. Processing time-call context.timerService().registerProcessingTime() to register; onTimer() is triggered when the system timestamp reaches the timestamp set by the timer
  2. Event time-call Context.timerService().registerEventTimer() to register, onTimer() is triggered when the watermark in Flink reaches or exceeds the timestamp set by Timer

Timer usage example

1. Real-time statistics indicators are stored in the status by day, and the status is cleared at 0 o'clock every day, and the statistics can be re-registered in the processElement() method.

//按天实时统计指标并存储在状态中,每天0点清除状态重新统计,就可以在processElement()方法里注册Timer
ctx.timerService().registerProcessingTimeTimer(
  tomorrowZeroTimeStampMs(System.currentTimeMillis(),8)+1
)
public static long tomorrowZeroTimestampMs(long now,int timeZone){
    
    
  return now-(now+timeZone*3600000)%86400000+86400000
}
  1. It also has an important position in the window mechanism. When you lift a window, you can naturally think of Trigger, that is, trigger. Part of the code of Flink's own EventTimeTrigger, it is the default trigger under the event time feature. When the watermark has not reached the right edge of the window, register the Timer with the timestamp on the right edge of the window. After the timer expires, the onEventTime() method is triggered, and then the window is closed.
@override
public TriggerResult onElement(Object element,long timestamp,TimeWindow window,TriggerContext ctx) throws Exeption{
    
    
   if(window.maxTimestamp()<=ctx.getCurrentWatermark()){
    
    
     return TriggerResult.FIRE;
   }else{
    
    
     ctx.registerEventTimeTimer(window.maxTimestamp())
     return TriggerResult.CONTINUE;
   }
}


@override
public TriggerResult onEventTime(long time,TimeWindow window,TriggerContext ctx){
    
    
   return time == window.maxTimestamp()?TriggerResult.FIRE:TriggerResult.CONTINUE;
}

Features of Timer

  1. 作用于keyedStream : Timers are registered on a keyedstream
  2. Timers are automatically replicated: Timers are automatically deduplicated
  3. Timers are persistently saved by flink: Timers are checkpointed by flink
  4. timers can be deleted

Principle analysis of Timers

Can refer to
https://blog.csdn.net/nazeniwaresakini/article/details/104220113

Guess you like

Origin blog.csdn.net/weixin_38813363/article/details/114886030