关于flink的时间处理不正确的现象复现原因分析

news/2024/7/16 4:33:16

  跟朋友聊天,说输出的时间不对,之前测试没关注到这个,然后就在processing模式下看了下,发现时间确实不正确
  
  然后就debug,看问题在哪,最终分析出了原因,记录如下:
  
    最下面给出了复现方案及原因分析
  
  let me show how to generate the wrong result
  
  background: processing time in tumbling window flink:1.5.0
  
  the invoke stack is as follows:
  
  [1] org.apache.calcite.runtime.SqlFunctions.internalToTimestamp (SqlFunctions.java:1,747)
  
  [2] org.apache.flink.table.runtime.aggregate.TimeWindowPropertyCollector.collect (TimeWindowPropertyCollector.scala:53)
  
  [3] org.apache.flink.table.runtime.aggregate.IncrementalAggregateWindowFunction.apply (IncrementalAggregateWindowFunction.scala:74)
  
  [4] org.apache.flink.table.runtime.aggregate.IncrementalAggregateTimeWindowFunction.apply (IncrementalAggregateTimeWindowFunction.scala:72)
  
  [5] org.apache.flink.table.runtime.aggregate.IncrementalAggregateTimeWindowFunction.apply (IncrementalAggregateTimeWindowFunction.scala:39)
  
  [6] org.apache.flink.streaming.runtime.operators.windowing.functions.InternalSingleValueWindowFunction.process (InternalSingleValueWindowFunction.java:46)
  
  [7] org.apache.flink.www.trgj888.com streaming.runtime.operators.www.gcyL157.com windowing.WindowOperator.emitWindowContents (WindowOperator.java:550)
  
  [8] org.apache.flink.www.mingcheng178.com streaming.runtime.operators.windowing.WindowOperator.onProcessingTime (WindowOperator.java:505)
  
  [9] org.apache.flink.www.yongshiyule178.com streaming.api.operators.HeapInternalTimerService.onProcessingTime (HeapInternalTimerService.java:266)
  
  [10] org.apache.flink.streaming.runtime.tasks.SystemProcessingTimeService$TriggerTask.run (SystemProcessingTimeService.java:281)
  
  [11] java.util.concurrent.Executors$RunnableAdapter.call (Executors.java:511)
  
  [12] java.util.concurrent.FutureTask.run (FutureTask.java:266)
  
  [13] java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201 (ScheduledThreadPoolExecutor.java:180)
  
  [14] java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run (ScheduledThreadPoolExecutor.java:293)
  
  [15] java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1,142)
  
  [16] java.util.www.yigouyule2.cn concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:617)
  
  [17] java.lang.Thread.run (Thread.java:www.michenggw.com 748)
  
  now ,we are at [1] org.apache.calcite.runtime.SqlFunctions.internalToTimestamp (SqlFunctions.java:1,747)
  
  and the code is as follows:
  
  public static Timestamp internalToTimestamp(long v) { return new Timestamp(v - LOCAL_TZ.getOffset(v)); }
  
  let us print the value of windowStart:v
  
  print v
  
  v = 1544074830000
  
  let us print the value of windowEnd:v
  
  print v
  
  v = 1544074833000
  
  after this, come back to
  
  [1] org.apache.flink.table.runtime.aggregate.TimeWindowPropertyCollector.collect (TimeWindowPropertyCollector.scala:51)
  
  then,we will execute
  
  `
  
  if (windowStartOffset.isDefined) {
  
  output.setField(www.mhylpt.com
  
  lastFieldPos + windowStartOffset.get,
  
  SqlFunctions.internalToTimestamp(windowStart))
  
  }
  
  if (windowEndOffset.isDefined) {
  
  output.setField(
  
  lastFieldPos + windowEndOffset.get,
  
  SqlFunctions.internalToTimestamp(windowEnd))
  
  }
  
  `
  
  before execute,the output is
  
  output = "pro0,throwable0,ERROR,ip0,1,ymm-appmetric-dev-self1_5_924367729,null,null,null"
  
  after execute,the output is
  
  output = "pro0,throwable0,ERROR,ip0,1,ymm-appmetric-dev-self1_5_924367729,2018-12-06 05:40:30.0,2018-12-06 05:40:33.0,null"
  
  so,do you think the
  
  long value 1544074830000 translated to be 2018-12-06 05:40:30.0
  
  long value 1544074833000 translated to be 2018-12-06 05:40:33.0
  
  would be right?
  
  I am in China, I think the timestamp should be 2018-12-06 13:40:30.0 and 2018-12-06 13:40:33.0
  
  okay,let us continue
  
  now ,the data will be write to kafka,before write ,the data will be serialized
  
  let us see what happened!
  
  the call stack is as follows:
  
  [1] org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ser.std.DateSerializer._timestamp (DateSerializer.java:41) [2] org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ser.std.DateSerializer.serialize (DateSerializer.java:48) [3] org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ser.std.DateSerializer.serialize (DateSerializer.java:15) [4] org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue (DefaultSerializerProvider.java:130) [5] org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper.writeValue (ObjectMapper.java:2,444) [6] org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper.valueToTree (ObjectMapper.java:2,586) [7] org.apache.flink.formats.json.JsonRowSerializationSchema.convert (JsonRowSerializationSchema.java:189) [8] org.apache.flink.formats.json.JsonRowSerializationSchema.convertRow (JsonRowSerializationSchema.java:128) [9] org.apache.flink.formats.json.JsonRowSerializationSchema.serialize (JsonRowSerializationSchema.java:102) [10] org.apache.flink.formats.json.JsonRowSerializationSchema.serialize (JsonRowSerializationSchema.java:51) [11] org.apache.flink.streaming.util.serialization.KeyedSerializationSchemaWrapper.serializeValue (KeyedSerializationSchemaWrapper.java:46) [12] org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer010.invoke (FlinkKafkaProducer010.java:355) [13] org.apache.flink.streaming.api.operators.StreamSink.processElement (StreamSink.java:56) [14] org.apache.flink.streaming.runtime.tasks.OperatorChain$CopyingChainingOutput.pushToOperator (OperatorChain.java:560) [15] org.apache.flink.streaming.runtime.tasks.OperatorChain$CopyingChainingOutput.collect (OperatorChain.java:535) [16] org.apache.flink.streaming.runtime.tasks.OperatorChain$CopyingChainingOutput.collect (OperatorChain.java:515) [17] org.apache.flink.streaming.api.operators.AbstractStreamOperator$CountingOutput.collect (AbstractStreamOperator.java:679) [18] org.apache.flink.streaming.api.operators.AbstractStreamOperator$CountingOutput.collect (AbstractStreamOperator.java:657) [19] org.apache.flink.streaming.api.operators.StreamMap.processElement (StreamMap.java:41) [20] org.apache.flink.streaming.runtime.tasks.OperatorChain$CopyingChainingOutput.pushToOperator (OperatorChain.java:560) [21] org.apache.flink.streaming.runtime.tasks.OperatorChain$CopyingChainingOutput.collect (OperatorChain.java:535) [22] org.apache.flink.streaming.runtime.tasks.OperatorChain$CopyingChainingOutput.collect (OperatorChain.java:515) [23] org.apache.flink.streaming.api.operators.AbstractStreamOperator$CountingOutput.collect (AbstractStreamOperator.java:679) [24] org.apache.flink.streaming.api.operators.AbstractStreamOperator$CountingOutput.collect (AbstractStreamOperator.java:657) [25] org.apache.flink.streaming.api.operators.TimestampedCollector.collect (TimestampedCollector.java:51) [26] org.apache.flink.table.runtime.CRowWrappingCollector.collect (CRowWrappingCollector.scala:37) [27] org.apache.flink.table.runtime.CRowWrappingCollector.collect (CRowWrappingCollector.scala:28) [28] DataStreamCalcRule$88.processElement (null) [29] org.apache.flink.table.runtime.CRowProcessRunner.processElement (CRowProcessRunner.scala:66) [30] org.apache.flink.table.runtime.CRowProcessRunner.processElement (CRowProcessRunner.scala:35) [31] org.apache.flink.streaming.api.operators.ProcessOperator.processElement (ProcessOperator.java:66) [32] org.apache.flink.streaming.runtime.tasks.OperatorChain$CopyingChainingOutput.pushToOperator (OperatorChain.java:560) [33] org.apache.flink.streaming.runtime.tasks.OperatorChain$CopyingChainingOutput.collect (OperatorChain.java:535) [34] org.apache.flink.streaming.runtime.tasks.OperatorChain$CopyingChainingOutput.collect (OperatorChain.java:515) [35] org.apache.flink.streaming.api.operators.AbstractStreamOperator$CountingOutput.collect (AbstractStreamOperator.java:679) [36] org.apache.flink.streaming.api.operators.AbstractStreamOperator$CountingOutput.collect (AbstractStreamOperator.java:657) [37] org.apache.flink.streaming.api.operators.TimestampedCollector.collect (TimestampedCollector.java:51) [38] org.apache.flink.table.runtime.aggregate.TimeWindowPropertyCollector.collect (TimeWindowPropertyCollector.scala:65) [39] org.apache.flink.table.runtime.aggregate.IncrementalAggregateWindowFunction.apply (IncrementalAggregateWindowFunction.scala:74) [40] org.apache.flink.table.runtime.aggregate.IncrementalAggregateTimeWindowFunction.apply (IncrementalAggregateTimeWindowFunction.scala:72) [41] org.apache.flink.table.runtime.aggregate.IncrementalAggregateTimeWindowFunction.apply (IncrementalAggregateTimeWindowFunction.scala:39) [42] org.apache.flink.streaming.runtime.operators.windowing.functions.InternalSingleValueWindowFunction.process (InternalSingleValueWindowFunction.java:46) [43] org.apache.flink.streaming.runtime.operators.windowing.WindowOperator.emitWindowContents (WindowOperator.java:550) [44] org.apache.flink.streaming.runtime.operators.windowing.WindowOperator.onProcessingTime (WindowOperator.java:505) [45] org.apache.flink.streaming.api.operators.HeapInternalTimerService.onProcessingTime (HeapInternalTimerService.java:266) [46] org.apache.flink.streaming.runtime.tasks.SystemProcessingTimeService$TriggerTask.run (SystemProcessingTimeService.java:281) [47] java.util.concurrent.Executors$RunnableAdapter.call (Executors.java:511) [48] java.util.concurrent.FutureTask.run (FutureTask.java:266) [49] java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201 (ScheduledThreadPoolExecutor.java:180) [50] java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run (ScheduledThreadPoolExecutor.java:293) [51] java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1,142) [52] java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:617) [53] java.lang.Thread.run (Thread.java:748)
  
  and the code is as follows:
  
  protected long _timestamp(Date value) { return value == null ? 0L : value.getTime(); }
  
  here,use windowEnd for example,the value is
  
  value = "2018-12-06 05:40:33.0"
  
  value.getTime() = 1544046033000
  
  see,the initial value is 1544074833000 and the final value is 1544046033000
  
  the minus value is 28800000, ---> 8 hours ,because I am in China.
  
  why? the key reason is SqlFunctions.internalToTimestamp
  
  public static Timestamp internalToTimestamp(long v)
  
  {
  
  return new Timestamp(v - LOCAL_TZ.getOffset(v));
  
  }
  
  in the code, It minus the LOCAL_TZ , I think it is redundant!
  
  刚才又看了下,其实根本原因就是时间转换来转换去,没有用同一个类,用了2个类的方法
  
  结果就乱套了,要改的话就是SqlFunctions的那个类


http://www.niftyadmin.cn/n/4022164.html

相关文章

什么是执行环境

什么是执行环境 当代码在JavaScript中运行的时候,代码在环境中被执行是非常重要的,它会被评估为以下之一类型来运行: 全局代码:默认环境,你的代码第一时间在这儿运行。 函数代码:当执行流进入一个函数体的…

css外层DIV半透明内层div不透明-弹出层效果的实现

css外层DIV半透明内层div不透明-弹出层效果的实现 <!DOCTYPE html> <html><head><meta charset"utf-8" /><title>css外层DIV半透明内层div不透明-弹出层效果的实现【实例】</title><style type"text/css">body…

android root检测原理wechat root检测

root 检测原理已经root设备&#xff0c;会增加一些特殊包或文件&#xff0c;所以可以通过检测这些包(如Superuser.apk、检测su命令)、activity、文件是否存在来判断。app检测是否可以执行在root下才能运行的命令。检测busybox工具是否存在,关于busybox的知识google上一大堆&…

ANGULAR JS 常用指令NG-IF、NG-CLASS、NG-OPTION、NG-VALUE、NG-CLICK是如何使用的?

大家好&#xff0c;我是IT修真院武汉分院第12期学员&#xff0c;一枚正直善良的web程序员。 今天给大家分享一下&#xff0c;修真院官网JS8任务中可能会使用到的知识点&#xff1a; ANGULAR JS 常用指令NG-IF、NG-CLASS、NG-OPTION、NG-VALUE、NG-CLICK是如何使用的&#xff1f…

i5服务器各种型号,酷睿i5型号有哪些 酷睿i5型号有哪些不同【详细介绍】

酷睿i5指的不是一款型号的处理器&#xff0c;它旗下有多个细分产品&#xff0c;不同的处理器在定价和销售定位以及用户的实际使用体验几个方面的内容都会有所区别。因此我们在实际选择购置的时候尤其需要注意它们之间的差异&#xff0c;并且还需要参考自己的实际需求报价进行筛…

总结的小知识点(一)

Vue-resource和后端做数据交互&#xff08;ajax通信&#xff09;&#xff0c;vue-router做前端路由&#xff0c;webpack工具编译我们的代码变成可以让浏览器识别的代码&#xff0c;实现单页应用&#xff0c;收藏商家利用html5的localStorage存储在浏览器端Npm install –g vue-…

Hashtable 为什么不叫 HashTable?

2019独角兽企业重金招聘Python工程师标准>>> 前几天在写《HashMap 和 Hashtable 的 6 个区别》这篇文章的时候&#xff0c;差点把 Hashtable 写成了 HashTable&#xff0c;后来看源码证实了是&#xff1a;Hashtable&#xff0c;小写的 "t"able&#xff0c…

Vue评论组件案例

最近学习了Vue前端框架&#xff0c;在这里记录一下组件的用法&#xff0c;我自己试着写了一个评论的组件&#xff0c;大神看到勿喷&#xff0c;欢迎提出宝贵意见。 首先看一下效果图 用到的文件有&#xff1a; <link rel"stylesheet" href"../js/bootstrap/d…