barrier_handler.go 5.5 KB

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