barrier_handler.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 checkpoint
  15. import (
  16. "github.com/lf-edge/ekuiper/pkg/api"
  17. "github.com/lf-edge/ekuiper/pkg/infra"
  18. )
  19. type BarrierHandler interface {
  20. Process(data *BufferOrEvent, ctx api.StreamContext) bool // If data is barrier return true, else return false
  21. SetOutput(chan<- *BufferOrEvent) // It is using for block a channel
  22. }
  23. // For qos 1, simple track barriers
  24. type BarrierTracker struct {
  25. responder Responder
  26. inputCount int
  27. pendingCheckpoints map[int64]int
  28. }
  29. func NewBarrierTracker(responder Responder, inputCount int) *BarrierTracker {
  30. return &BarrierTracker{
  31. responder: responder,
  32. inputCount: inputCount,
  33. pendingCheckpoints: make(map[int64]int),
  34. }
  35. }
  36. func (h *BarrierTracker) Process(data *BufferOrEvent, ctx api.StreamContext) bool {
  37. d := data.Data
  38. if b, ok := d.(*Barrier); ok {
  39. h.processBarrier(b, ctx)
  40. return true
  41. }
  42. return false
  43. }
  44. func (h *BarrierTracker) SetOutput(_ chan<- *BufferOrEvent) {
  45. // do nothing, does not need it
  46. }
  47. func (h *BarrierTracker) processBarrier(b *Barrier, ctx api.StreamContext) {
  48. logger := ctx.GetLogger()
  49. if h.inputCount == 1 {
  50. err := h.responder.TriggerCheckpoint(b.CheckpointId)
  51. if err != nil {
  52. logger.Errorf("trigger checkpoint for %s err: %s", h.responder.GetName(), err)
  53. }
  54. return
  55. }
  56. if c, ok := h.pendingCheckpoints[b.CheckpointId]; ok {
  57. c += 1
  58. if c == h.inputCount {
  59. err := h.responder.TriggerCheckpoint(b.CheckpointId)
  60. if err != nil {
  61. logger.Errorf("trigger checkpoint for %s err: %s", h.responder.GetName(), err)
  62. return
  63. }
  64. delete(h.pendingCheckpoints, b.CheckpointId)
  65. for cid := range h.pendingCheckpoints {
  66. if cid < b.CheckpointId {
  67. delete(h.pendingCheckpoints, cid)
  68. }
  69. }
  70. } else {
  71. h.pendingCheckpoints[b.CheckpointId] = c
  72. }
  73. } else {
  74. h.pendingCheckpoints[b.CheckpointId] = 1
  75. }
  76. }
  77. // For qos 2, block an input until all barriers are received
  78. type BarrierAligner struct {
  79. responder Responder
  80. inputCount int
  81. currentCheckpointId int64
  82. output chan<- *BufferOrEvent
  83. blockedChannels map[string]bool
  84. buffer []*BufferOrEvent
  85. }
  86. func NewBarrierAligner(responder Responder, inputCount int) *BarrierAligner {
  87. ba := &BarrierAligner{
  88. responder: responder,
  89. inputCount: inputCount,
  90. blockedChannels: make(map[string]bool),
  91. }
  92. return ba
  93. }
  94. func (h *BarrierAligner) Process(data *BufferOrEvent, ctx api.StreamContext) bool {
  95. switch d := data.Data.(type) {
  96. case *Barrier:
  97. h.processBarrier(d, ctx)
  98. return true
  99. default:
  100. // If blocking, save to buffer
  101. if h.inputCount > 1 && len(h.blockedChannels) > 0 {
  102. if _, ok := h.blockedChannels[data.Channel]; ok {
  103. h.buffer = append(h.buffer, data)
  104. return true
  105. }
  106. }
  107. }
  108. return false
  109. }
  110. func (h *BarrierAligner) processBarrier(b *Barrier, ctx api.StreamContext) {
  111. logger := ctx.GetLogger()
  112. logger.Debugf("Aligner process barrier %+v", b)
  113. if h.inputCount == 1 {
  114. if b.CheckpointId > h.currentCheckpointId {
  115. h.currentCheckpointId = b.CheckpointId
  116. err := h.responder.TriggerCheckpoint(b.CheckpointId)
  117. if err != nil {
  118. logger.Errorf("trigger checkpoint for %s err: %s", h.responder.GetName(), err)
  119. }
  120. }
  121. return
  122. }
  123. if len(h.blockedChannels) > 0 {
  124. if b.CheckpointId == h.currentCheckpointId {
  125. h.onBarrier(b.OpId, ctx)
  126. } else if b.CheckpointId > h.currentCheckpointId {
  127. logger.Infof("Received checkpoint barrier for checkpoint %d before complete current checkpoint %d. Skipping current checkpoint.", b.CheckpointId, h.currentCheckpointId)
  128. // TODO Abort checkpoint
  129. h.releaseBlocksAndResetBarriers()
  130. h.beginNewAlignment(b, ctx)
  131. } else {
  132. return
  133. }
  134. } else if b.CheckpointId > h.currentCheckpointId {
  135. logger.Debugf("Aligner process new alignment", b)
  136. h.beginNewAlignment(b, ctx)
  137. } else {
  138. return
  139. }
  140. if len(h.blockedChannels) == h.inputCount {
  141. logger.Debugf("Received all barriers, triggering checkpoint %d", b.CheckpointId)
  142. err := h.responder.TriggerCheckpoint(b.CheckpointId)
  143. if err != nil {
  144. logger.Errorf("trigger checkpoint for %s err: %s", h.responder.GetName(), err)
  145. return
  146. }
  147. h.releaseBlocksAndResetBarriers()
  148. // clean up all the buffer
  149. var temp []*BufferOrEvent
  150. for _, d := range h.buffer {
  151. temp = append(temp, d)
  152. }
  153. go infra.SafeRun(func() error {
  154. for _, d := range temp {
  155. h.output <- d
  156. }
  157. return nil
  158. })
  159. h.buffer = make([]*BufferOrEvent, 0)
  160. }
  161. }
  162. func (h *BarrierAligner) onBarrier(name string, ctx api.StreamContext) {
  163. logger := ctx.GetLogger()
  164. if _, ok := h.blockedChannels[name]; !ok {
  165. h.blockedChannels[name] = true
  166. logger.Debugf("Received barrier from channel %s", name)
  167. }
  168. }
  169. func (h *BarrierAligner) SetOutput(output chan<- *BufferOrEvent) {
  170. h.output = output
  171. }
  172. func (h *BarrierAligner) releaseBlocksAndResetBarriers() {
  173. h.blockedChannels = make(map[string]bool)
  174. }
  175. func (h *BarrierAligner) beginNewAlignment(barrier *Barrier, ctx api.StreamContext) {
  176. logger := ctx.GetLogger()
  177. h.currentCheckpointId = barrier.CheckpointId
  178. h.onBarrier(barrier.OpId, ctx)
  179. logger.Debugf("Starting stream alignment for checkpoint %d", barrier.CheckpointId)
  180. }