kv_store_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 state
  15. import (
  16. "fmt"
  17. "log"
  18. "os"
  19. "path"
  20. "reflect"
  21. "sync"
  22. "testing"
  23. "github.com/lf-edge/ekuiper/internal/conf"
  24. "github.com/lf-edge/ekuiper/internal/pkg/store"
  25. "github.com/lf-edge/ekuiper/pkg/cast"
  26. )
  27. func TestLifecycle(t *testing.T) {
  28. var (
  29. i = 0
  30. ruleId = "test1"
  31. checkpointIds = []int64{1, 2, 3}
  32. opIds = []string{"op1", "op2", "op3"}
  33. r = map[string]interface{}{
  34. "1": map[string]interface{}{
  35. "op1": map[string]interface{}{
  36. "op": "op1",
  37. "oi": 0,
  38. "ci": 0,
  39. },
  40. "op2": map[string]interface{}{
  41. "op": "op2",
  42. "oi": 1,
  43. "ci": 0,
  44. },
  45. "op3": map[string]interface{}{
  46. "op": "op3",
  47. "oi": 2,
  48. "ci": 0,
  49. },
  50. },
  51. "2": map[string]interface{}{
  52. "op1": map[string]interface{}{
  53. "op": "op1",
  54. "oi": 0,
  55. "ci": 1,
  56. },
  57. "op2": map[string]interface{}{
  58. "op": "op2",
  59. "oi": 1,
  60. "ci": 1,
  61. },
  62. "op3": map[string]interface{}{
  63. "op": "op3",
  64. "oi": 2,
  65. "ci": 1,
  66. },
  67. },
  68. "3": map[string]interface{}{
  69. "op1": map[string]interface{}{
  70. "op": "op1",
  71. "oi": 0,
  72. "ci": 2,
  73. },
  74. "op2": map[string]interface{}{
  75. "op": "op2",
  76. "oi": 1,
  77. "ci": 2,
  78. },
  79. "op3": map[string]interface{}{
  80. "op": "op3",
  81. "oi": 2,
  82. "ci": 2,
  83. },
  84. },
  85. }
  86. rm = map[string]interface{}{
  87. "1": map[string]interface{}{
  88. "op1": map[string]interface{}{
  89. "op": "op1",
  90. "oi": 0,
  91. "ci": 0,
  92. },
  93. "op2": map[string]interface{}{
  94. "op": "op2",
  95. "oi": 1,
  96. "ci": 0,
  97. },
  98. "op3": map[string]interface{}{
  99. "op": "op3",
  100. "oi": 2,
  101. "ci": 0,
  102. },
  103. },
  104. "2": map[string]interface{}{
  105. "op1": map[string]interface{}{
  106. "op": "op1",
  107. "oi": 0,
  108. "ci": 1,
  109. },
  110. "op2": map[string]interface{}{
  111. "op": "op2",
  112. "oi": 1,
  113. "ci": 1,
  114. },
  115. "op3": map[string]interface{}{
  116. "op": "op3",
  117. "oi": 2,
  118. "ci": 1,
  119. },
  120. },
  121. "3": map[string]interface{}{
  122. "op1": map[string]interface{}{
  123. "op": "op1",
  124. "oi": 0,
  125. "ci": 2,
  126. },
  127. "op2": map[string]interface{}{
  128. "op": "op2",
  129. "oi": 1,
  130. "ci": 2,
  131. },
  132. "op3": map[string]interface{}{
  133. "op": "op3",
  134. "oi": 2,
  135. "ci": 2,
  136. },
  137. },
  138. "10000": map[string]interface{}{
  139. "op2": map[string]interface{}{
  140. "op": "op2",
  141. "oi": 1,
  142. "ci": 10000,
  143. },
  144. "op3": map[string]interface{}{
  145. "op": "op3",
  146. "oi": 2,
  147. "ci": 10000,
  148. },
  149. },
  150. }
  151. )
  152. func() {
  153. cleanStateData()
  154. err := store.SetupDefault()
  155. if err != nil {
  156. t.Error(err)
  157. }
  158. store, err := getKVStore(ruleId)
  159. if err != nil {
  160. t.Errorf("Get store for rule %s error: %s", ruleId, err)
  161. return
  162. }
  163. // Save for all checkpoints
  164. for i, cid := range checkpointIds {
  165. for j, opId := range opIds {
  166. err := store.SaveState(cid, opId, map[string]interface{}{
  167. "op": opId,
  168. "oi": j,
  169. "ci": i,
  170. })
  171. if err != nil {
  172. t.Errorf("Save state for rule %s op %s error: %s", ruleId, opId, err)
  173. return
  174. }
  175. }
  176. err := store.SaveCheckpoint(cid)
  177. if err != nil {
  178. t.Errorf("Save checkpoint %d for rule %s error: %s", cid, ruleId, err)
  179. return
  180. }
  181. }
  182. // compare checkpoints
  183. if !reflect.DeepEqual(checkpointIds, store.checkpoints) {
  184. t.Errorf("%d.Save checkpoint\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, checkpointIds, store.checkpoints)
  185. }
  186. // compare contents
  187. result := mapStoreToMap(store.mapStore)
  188. if !reflect.DeepEqual(r, result) {
  189. t.Errorf("%d.Save checkpoint\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, r, result)
  190. }
  191. // Save additional state but not serialized in checkpoint
  192. err = store.SaveState(10000, opIds[1], map[string]interface{}{
  193. "op": opIds[1],
  194. "oi": 1,
  195. "ci": 10000,
  196. })
  197. if err != nil {
  198. t.Errorf("Save state for rule %s op %s error: %s", ruleId, opIds[1], err)
  199. return
  200. }
  201. err = store.SaveState(10000, opIds[2], map[string]interface{}{
  202. "op": opIds[2],
  203. "oi": 2,
  204. "ci": 10000,
  205. })
  206. if err != nil {
  207. t.Errorf("Save state for rule %s op %s error: %s", ruleId, opIds[2], err)
  208. return
  209. }
  210. // compare checkpoints
  211. if !reflect.DeepEqual(checkpointIds, store.checkpoints) {
  212. t.Errorf("%d.Save state\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, checkpointIds, store.checkpoints)
  213. }
  214. // compare contents
  215. result = mapStoreToMap(store.mapStore)
  216. if !reflect.DeepEqual(rm, result) {
  217. t.Errorf("%d.Save state\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, r, result)
  218. }
  219. // simulate restore
  220. store = nil
  221. store, err = getKVStore(ruleId)
  222. if err != nil {
  223. t.Errorf("Restore store for rule %s error: %s", ruleId, err)
  224. return
  225. }
  226. // compare checkpoints
  227. if !reflect.DeepEqual(checkpointIds[2:], store.checkpoints) {
  228. t.Errorf("%d.Restore checkpoint\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, checkpointIds, store.checkpoints)
  229. return
  230. }
  231. // compare contents
  232. result = mapStoreToMap(store.mapStore)
  233. last := map[string]interface{}{
  234. "3": r["3"],
  235. }
  236. if !reflect.DeepEqual(last, result) {
  237. t.Errorf("%d.Restore checkpoint\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, last, result)
  238. return
  239. }
  240. ns, err := store.GetOpState(opIds[1])
  241. if err != nil {
  242. t.Errorf("Get op %s state for rule %s error: %s", opIds[1], ruleId, err)
  243. return
  244. }
  245. sm := r[fmt.Sprintf("%v", checkpointIds[len(checkpointIds)-1])].(map[string]interface{})[opIds[1]]
  246. nsm := cast.SyncMapToMap(ns)
  247. if !reflect.DeepEqual(sm, nsm) {
  248. t.Errorf("%d.Restore op state\n\nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, sm, nsm)
  249. return
  250. }
  251. }()
  252. }
  253. func mapStoreToMap(sm *sync.Map) map[string]interface{} {
  254. m := make(map[string]interface{})
  255. sm.Range(func(k interface{}, v interface{}) bool {
  256. switch t := v.(type) {
  257. case *sync.Map:
  258. m[fmt.Sprintf("%v", k)] = mapStoreToMap(t)
  259. default:
  260. m[fmt.Sprintf("%v", k)] = t
  261. }
  262. return true
  263. })
  264. return m
  265. }
  266. func cleanStateData() {
  267. dbDir, err := conf.GetDataLoc()
  268. if err != nil {
  269. log.Panic(err)
  270. }
  271. c := path.Join(dbDir)
  272. err = os.RemoveAll(c)
  273. if err != nil {
  274. conf.Log.Error(err)
  275. }
  276. }