barrier_handler.go 4.5 KB

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