checkpoint_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package processors
  2. import (
  3. "fmt"
  4. "github.com/emqx/kuiper/common"
  5. "github.com/emqx/kuiper/xsql"
  6. "github.com/emqx/kuiper/xstream/api"
  7. "github.com/emqx/kuiper/xstream/nodes"
  8. "github.com/emqx/kuiper/xstream/test"
  9. "reflect"
  10. "strings"
  11. "testing"
  12. "time"
  13. )
  14. // Full lifecycle test: Run window rule; trigger checkpoints by mock timer; restart rule; make sure the result is right;
  15. func TestCheckpointCount(t *testing.T) {
  16. common.IsTesting = true
  17. var tests = []struct {
  18. name string
  19. sql string
  20. size int
  21. breakSize int
  22. cc int
  23. r [][]map[string]interface{}
  24. }{
  25. {
  26. name: `rule1`,
  27. sql: `SELECT * FROM demo GROUP BY HOPPINGWINDOW(ss, 2, 1)`,
  28. size: 5,
  29. breakSize: 2,
  30. cc: 2,
  31. r: [][]map[string]interface{}{
  32. {{
  33. "color": "red",
  34. "size": float64(3),
  35. "ts": float64(1541152486013),
  36. }, {
  37. "color": "blue",
  38. "size": float64(6),
  39. "ts": float64(1541152486822),
  40. }},
  41. {{
  42. "color": "red",
  43. "size": float64(3),
  44. "ts": float64(1541152486013),
  45. }, {
  46. "color": "blue",
  47. "size": float64(6),
  48. "ts": float64(1541152486822),
  49. }, {
  50. "color": "blue",
  51. "size": float64(2),
  52. "ts": float64(1541152487632),
  53. }},
  54. {{
  55. "color": "blue",
  56. "size": float64(2),
  57. "ts": float64(1541152487632),
  58. }, {
  59. "color": "yellow",
  60. "size": float64(4),
  61. "ts": float64(1541152488442),
  62. }},
  63. },
  64. },
  65. }
  66. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  67. createStreams(t)
  68. defer dropStreams(t)
  69. options := []*api.RuleOption{
  70. {
  71. BufferLength: 100,
  72. Qos: api.AtLeastOnce,
  73. CheckpointInterval: 1000,
  74. }, {
  75. BufferLength: 100,
  76. Qos: api.ExactlyOnce,
  77. CheckpointInterval: 1000,
  78. },
  79. }
  80. for j, opt := range options {
  81. for i, tt := range tests {
  82. test.ResetClock(1541152486000)
  83. p := NewRuleProcessor(DbDir)
  84. parser := xsql.NewParser(strings.NewReader(tt.sql))
  85. var (
  86. sources []*nodes.SourceNode
  87. syncs []chan int
  88. )
  89. if stmt, err := xsql.Language.Parse(parser); err != nil {
  90. t.Errorf("parse sql %s error: %s", tt.sql, err)
  91. } else {
  92. if selectStmt, ok := stmt.(*xsql.SelectStatement); !ok {
  93. t.Errorf("sql %s is not a select statement", tt.sql)
  94. } else {
  95. streams := xsql.GetStreams(selectStmt)
  96. for _, stream := range streams {
  97. next := make(chan int)
  98. syncs = append(syncs, next)
  99. source := getMockSource(stream, next, tt.size)
  100. sources = append(sources, source)
  101. }
  102. }
  103. }
  104. tp, inputs, err := p.createTopoWithSources(&api.Rule{Id: fmt.Sprintf("%s_%d", tt.name, j), Sql: tt.sql, Options: opt}, sources)
  105. if err != nil {
  106. t.Error(err)
  107. }
  108. mockSink := test.NewMockSink()
  109. sink := nodes.NewSinkNodeWithSink("mockSink", mockSink, nil)
  110. tp.AddSink(inputs, sink)
  111. errCh := tp.Open()
  112. func() {
  113. for i := 0; i < tt.breakSize*len(syncs); i++ {
  114. syncs[i%len(syncs)] <- i
  115. for {
  116. time.Sleep(1)
  117. if getMetric(tp, "op_window_0_records_in_total") == (i + 1) {
  118. break
  119. }
  120. }
  121. select {
  122. case err = <-errCh:
  123. t.Log(err)
  124. tp.Cancel()
  125. return
  126. default:
  127. }
  128. }
  129. mockClock := test.GetMockClock()
  130. mockClock.Set(common.TimeFromUnixMilli(int64(1541152486014 + tt.breakSize*1000)))
  131. actual := tp.GetCoordinator().GetCompleteCount()
  132. if !reflect.DeepEqual(tt.cc, actual) {
  133. t.Errorf("%d-%d. checkpoint count\n\nresult mismatch:\n\nexp=%#v\n\ngot=%d\n\n", i, j, tt.cc, actual)
  134. return
  135. }
  136. time.Sleep(1000)
  137. tp.Cancel()
  138. //TODO window memory
  139. // errCh := tp.Open()
  140. // for i := tt.breakSize; i < tt.size*len(syncs); i++ {
  141. // syncs[i%len(syncs)] <- i
  142. // retry := 100
  143. // for ; retry > 0; retry-- {
  144. // time.Sleep(1)
  145. // if getMetric(tp, "op_window_0_records_in_total") == (i - tt.breakSize + 1) {
  146. // break
  147. // }
  148. // }
  149. // select {
  150. // case err = <-errCh:
  151. // t.Log(err)
  152. // tp.Cancel()
  153. // return
  154. // default:
  155. // }
  156. // }
  157. // time.Sleep(1000)
  158. }()
  159. //results := mockSink.GetResults()
  160. //var maps [][]map[string]interface{}
  161. //for _, v := range results {
  162. // var mapRes []map[string]interface{}
  163. // err := json.Unmarshal(v, &mapRes)
  164. // if err != nil {
  165. // t.Errorf("Failed to parse the input into map")
  166. // continue
  167. // }
  168. // maps = append(maps, mapRes)
  169. //}
  170. //if !reflect.DeepEqual(tt.r, maps) {
  171. // t.Errorf("%d. %q\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.sql, tt.r, maps)
  172. //}
  173. //tp.Cancel()
  174. }
  175. cleanStateData()
  176. }
  177. }