watermark.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // Copyright 2021-2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package node
  15. import (
  16. "context"
  17. "fmt"
  18. "github.com/lf-edge/ekuiper/internal/xsql"
  19. "github.com/lf-edge/ekuiper/pkg/api"
  20. "github.com/lf-edge/ekuiper/pkg/ast"
  21. "github.com/lf-edge/ekuiper/pkg/infra"
  22. "math"
  23. "sort"
  24. )
  25. type WatermarkTuple struct {
  26. Timestamp int64
  27. }
  28. func (t *WatermarkTuple) GetTimestamp() int64 {
  29. return t.Timestamp
  30. }
  31. func (t *WatermarkTuple) IsWatermark() bool {
  32. return true
  33. }
  34. const WATERMARK_KEY = "$$wartermark"
  35. type WatermarkGenerator struct {
  36. inputTopics []string
  37. topicToTs map[string]int64
  38. window *WindowConfig
  39. lateTolerance int64
  40. interval int
  41. //ticker *clock.Ticker
  42. stream chan<- interface{}
  43. //state
  44. lastWatermarkTs int64
  45. }
  46. func NewWatermarkGenerator(window *WindowConfig, l int64, s []string, stream chan<- interface{}) (*WatermarkGenerator, error) {
  47. w := &WatermarkGenerator{
  48. window: window,
  49. topicToTs: make(map[string]int64),
  50. lateTolerance: l,
  51. inputTopics: s,
  52. stream: stream,
  53. }
  54. switch window.Type {
  55. case ast.NOT_WINDOW:
  56. case ast.TUMBLING_WINDOW:
  57. w.interval = window.Length
  58. case ast.HOPPING_WINDOW:
  59. w.interval = window.Interval
  60. case ast.SLIDING_WINDOW:
  61. w.interval = window.Length
  62. case ast.SESSION_WINDOW:
  63. //Use timeout to update watermark
  64. w.interval = window.Interval
  65. default:
  66. return nil, fmt.Errorf("unsupported window type %d", window.Type)
  67. }
  68. return w, nil
  69. }
  70. func (w *WatermarkGenerator) track(s string, ts int64, ctx api.StreamContext) bool {
  71. log := ctx.GetLogger()
  72. log.Debugf("watermark generator track event from topic %s at %d", s, ts)
  73. currentVal, ok := w.topicToTs[s]
  74. if !ok || ts > currentVal {
  75. w.topicToTs[s] = ts
  76. }
  77. r := ts >= w.lastWatermarkTs
  78. if r {
  79. w.trigger(ctx)
  80. }
  81. return r
  82. }
  83. func (w *WatermarkGenerator) trigger(ctx api.StreamContext) {
  84. log := ctx.GetLogger()
  85. watermark := w.computeWatermarkTs(ctx)
  86. log.Debugf("compute watermark event at %d with last %d", watermark, w.lastWatermarkTs)
  87. if watermark > w.lastWatermarkTs {
  88. t := &WatermarkTuple{Timestamp: watermark}
  89. select {
  90. case w.stream <- t:
  91. default: //TODO need to set buffer
  92. }
  93. w.lastWatermarkTs = watermark
  94. ctx.PutState(WATERMARK_KEY, w.lastWatermarkTs)
  95. log.Debugf("scan watermark event at %d", watermark)
  96. }
  97. }
  98. func (w *WatermarkGenerator) computeWatermarkTs(_ context.Context) int64 {
  99. var ts int64
  100. if len(w.topicToTs) >= len(w.inputTopics) {
  101. ts = math.MaxInt64
  102. for _, key := range w.inputTopics {
  103. if ts > w.topicToTs[key] {
  104. ts = w.topicToTs[key]
  105. }
  106. }
  107. }
  108. return ts - w.lateTolerance
  109. }
  110. //If window end cannot be determined yet, return max int64 so that it can be recalculated for the next watermark
  111. func (w *WatermarkGenerator) getNextWindow(inputs []*xsql.Tuple, current int64, watermark int64, triggered bool) int64 {
  112. switch w.window.Type {
  113. case ast.TUMBLING_WINDOW, ast.HOPPING_WINDOW:
  114. if triggered {
  115. return current + int64(w.interval)
  116. } else {
  117. interval := int64(w.interval)
  118. nextTs := getEarliestEventTs(inputs, current, watermark)
  119. if nextTs == math.MaxInt64 || nextTs%interval == 0 {
  120. return nextTs
  121. }
  122. return nextTs + (interval - nextTs%interval)
  123. }
  124. case ast.SLIDING_WINDOW:
  125. nextTs := getEarliestEventTs(inputs, current, watermark)
  126. return nextTs
  127. default:
  128. return math.MaxInt64
  129. }
  130. }
  131. func (w *WatermarkGenerator) getNextSessionWindow(inputs []*xsql.Tuple) (int64, bool) {
  132. if len(inputs) > 0 {
  133. timeout, duration := int64(w.window.Interval), int64(w.window.Length)
  134. sort.SliceStable(inputs, func(i, j int) bool {
  135. return inputs[i].Timestamp < inputs[j].Timestamp
  136. })
  137. et := inputs[0].Timestamp
  138. tick := et + (duration - et%duration)
  139. if et%duration == 0 {
  140. tick = et
  141. }
  142. var p int64
  143. ticked := false
  144. for _, tuple := range inputs {
  145. var r int64 = math.MaxInt64
  146. if p > 0 {
  147. if tuple.Timestamp-p > timeout {
  148. r = p + timeout
  149. }
  150. }
  151. if tuple.Timestamp > tick {
  152. if tick-duration > et && tick < r {
  153. r = tick
  154. ticked = true
  155. }
  156. tick += duration
  157. }
  158. if r < math.MaxInt64 {
  159. return r, ticked
  160. }
  161. p = tuple.Timestamp
  162. }
  163. }
  164. return math.MaxInt64, false
  165. }
  166. func (o *WindowOperator) execEventWindow(ctx api.StreamContext, inputs []*xsql.Tuple, errCh chan<- error) {
  167. log := ctx.GetLogger()
  168. var (
  169. triggered bool
  170. nextWindowEndTs int64
  171. prevWindowEndTs int64
  172. lastTicked bool
  173. )
  174. o.watermarkGenerator.lastWatermarkTs = 0
  175. if s, err := ctx.GetState(WATERMARK_KEY); err == nil && s != nil {
  176. if si, ok := s.(int64); ok {
  177. o.watermarkGenerator.lastWatermarkTs = si
  178. } else {
  179. infra.DrainError(ctx, fmt.Errorf("restore window state `lastWatermarkTs` %v error, invalid type", s), errCh)
  180. return
  181. }
  182. }
  183. log.Infof("Start with window state lastWatermarkTs: %d", o.watermarkGenerator.lastWatermarkTs)
  184. for {
  185. select {
  186. // process incoming item
  187. case item, opened := <-o.input:
  188. processed := false
  189. if item, processed = o.preprocess(item); processed {
  190. break
  191. }
  192. o.statManager.ProcessTimeStart()
  193. if !opened {
  194. o.statManager.IncTotalExceptions()
  195. break
  196. }
  197. switch d := item.(type) {
  198. case error:
  199. o.statManager.IncTotalRecordsIn()
  200. o.Broadcast(d)
  201. o.statManager.IncTotalExceptions()
  202. case xsql.Event:
  203. if d.IsWatermark() {
  204. watermarkTs := d.GetTimestamp()
  205. windowEndTs := nextWindowEndTs
  206. ticked := false
  207. //Session window needs a recalculation of window because its window end depends the inputs
  208. if windowEndTs == math.MaxInt64 || o.window.Type == ast.SESSION_WINDOW || o.window.Type == ast.SLIDING_WINDOW {
  209. if o.window.Type == ast.SESSION_WINDOW {
  210. windowEndTs, ticked = o.watermarkGenerator.getNextSessionWindow(inputs)
  211. } else {
  212. windowEndTs = o.watermarkGenerator.getNextWindow(inputs, prevWindowEndTs, watermarkTs, triggered)
  213. }
  214. }
  215. for windowEndTs <= watermarkTs && windowEndTs >= 0 {
  216. log.Debugf("Window end ts %d Watermark ts %d", windowEndTs, watermarkTs)
  217. log.Debugf("Current input count %d", len(inputs))
  218. //scan all events and find out the event in the current window
  219. if o.window.Type == ast.SESSION_WINDOW && !lastTicked {
  220. o.triggerTime = inputs[0].Timestamp
  221. }
  222. inputs, triggered = o.scan(inputs, windowEndTs, ctx)
  223. prevWindowEndTs = windowEndTs
  224. lastTicked = ticked
  225. if o.window.Type == ast.SESSION_WINDOW {
  226. windowEndTs, ticked = o.watermarkGenerator.getNextSessionWindow(inputs)
  227. } else {
  228. windowEndTs = o.watermarkGenerator.getNextWindow(inputs, prevWindowEndTs, watermarkTs, triggered)
  229. }
  230. }
  231. nextWindowEndTs = windowEndTs
  232. log.Debugf("next window end %d", nextWindowEndTs)
  233. } else {
  234. o.statManager.IncTotalRecordsIn()
  235. tuple, ok := d.(*xsql.Tuple)
  236. if !ok {
  237. log.Debugf("receive non tuple element %v", d)
  238. }
  239. log.Debugf("event window receive tuple %s", tuple.Message)
  240. if o.watermarkGenerator.track(tuple.Emitter, d.GetTimestamp(), ctx) {
  241. inputs = append(inputs, tuple)
  242. }
  243. }
  244. o.statManager.ProcessTimeEnd()
  245. ctx.PutState(WINDOW_INPUTS_KEY, inputs)
  246. default:
  247. o.statManager.IncTotalRecordsIn()
  248. o.Broadcast(fmt.Errorf("run Window error: expect xsql.Event type but got %[1]T(%[1]v)", d))
  249. o.statManager.IncTotalExceptions()
  250. }
  251. // is cancelling
  252. case <-ctx.Done():
  253. log.Infoln("Cancelling window....")
  254. if o.ticker != nil {
  255. o.ticker.Stop()
  256. }
  257. return
  258. }
  259. }
  260. }
  261. func getEarliestEventTs(inputs []*xsql.Tuple, startTs int64, endTs int64) int64 {
  262. var minTs int64 = math.MaxInt64
  263. for _, t := range inputs {
  264. if t.Timestamp > startTs && t.Timestamp <= endTs && t.Timestamp < minTs {
  265. minTs = t.Timestamp
  266. }
  267. }
  268. return minTs
  269. }