stream.tcl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. # return value is like strcmp() and similar.
  2. proc streamCompareID {a b} {
  3. if {$a eq $b} {return 0}
  4. lassign [split $a -] a_ms a_seq
  5. lassign [split $b -] b_ms b_seq
  6. if {$a_ms > $b_ms} {return 1}
  7. if {$a_ms < $b_ms} {return -1}
  8. # Same ms case, compare seq.
  9. if {$a_seq > $b_seq} {return 1}
  10. if {$a_seq < $b_seq} {return -1}
  11. }
  12. # return the ID immediately greater than the specified one.
  13. # Note that this function does not care to handle 'seq' overflow
  14. # since it's a 64 bit value.
  15. proc streamNextID {id} {
  16. lassign [split $id -] ms seq
  17. incr seq
  18. join [list $ms $seq] -
  19. }
  20. # Generate a random stream entry ID with the ms part between min and max
  21. # and a low sequence number (0 - 999 range), in order to stress test
  22. # XRANGE against a Tcl implementation implementing the same concept
  23. # with Tcl-only code in a linear array.
  24. proc streamRandomID {min_id max_id} {
  25. lassign [split $min_id -] min_ms min_seq
  26. lassign [split $max_id -] max_ms max_seq
  27. set delta [expr {$max_ms-$min_ms+1}]
  28. set ms [expr {$min_ms+[randomInt $delta]}]
  29. set seq [randomInt 1000]
  30. return $ms-$seq
  31. }
  32. # Tcl-side implementation of XRANGE to perform fuzz testing in the Redis
  33. # XRANGE implementation.
  34. proc streamSimulateXRANGE {items start end} {
  35. set res {}
  36. foreach i $items {
  37. set this_id [lindex $i 0]
  38. if {[streamCompareID $this_id $start] >= 0} {
  39. if {[streamCompareID $this_id $end] <= 0} {
  40. lappend res $i
  41. }
  42. }
  43. }
  44. return $res
  45. }
  46. set content {} ;# Will be populated with Tcl side copy of the stream content.
  47. start_server {
  48. tags {"stream"}
  49. } {
  50. test {XADD can add entries into a stream that XRANGE can fetch} {
  51. r XADD mystream * item 1 value a
  52. r XADD mystream * item 2 value b
  53. assert_equal 2 [r XLEN mystream]
  54. set items [r XRANGE mystream - +]
  55. assert_equal [lindex $items 0 1] {item 1 value a}
  56. assert_equal [lindex $items 1 1] {item 2 value b}
  57. }
  58. test {XADD IDs are incremental} {
  59. set id1 [r XADD mystream * item 1 value a]
  60. set id2 [r XADD mystream * item 2 value b]
  61. set id3 [r XADD mystream * item 3 value c]
  62. assert {[streamCompareID $id1 $id2] == -1}
  63. assert {[streamCompareID $id2 $id3] == -1}
  64. }
  65. test {XADD IDs are incremental when ms is the same as well} {
  66. r multi
  67. r XADD mystream * item 1 value a
  68. r XADD mystream * item 2 value b
  69. r XADD mystream * item 3 value c
  70. lassign [r exec] id1 id2 id3
  71. assert {[streamCompareID $id1 $id2] == -1}
  72. assert {[streamCompareID $id2 $id3] == -1}
  73. }
  74. test {XADD IDs correctly report an error when overflowing} {
  75. r DEL mystream
  76. r xadd mystream 18446744073709551615-18446744073709551615 a b
  77. assert_error ERR* {r xadd mystream * c d}
  78. }
  79. test {XADD with MAXLEN option} {
  80. r DEL mystream
  81. for {set j 0} {$j < 1000} {incr j} {
  82. if {rand() < 0.9} {
  83. r XADD mystream MAXLEN 5 * xitem $j
  84. } else {
  85. r XADD mystream MAXLEN 5 * yitem $j
  86. }
  87. }
  88. set res [r xrange mystream - +]
  89. set expected 995
  90. foreach r $res {
  91. assert {[lindex $r 1 1] == $expected}
  92. incr expected
  93. }
  94. }
  95. test {XADD mass insertion and XLEN} {
  96. r DEL mystream
  97. r multi
  98. for {set j 0} {$j < 10000} {incr j} {
  99. # From time to time insert a field with a different set
  100. # of fields in order to stress the stream compression code.
  101. if {rand() < 0.9} {
  102. r XADD mystream * item $j
  103. } else {
  104. r XADD mystream * item $j otherfield foo
  105. }
  106. }
  107. r exec
  108. set items [r XRANGE mystream - +]
  109. for {set j 0} {$j < 10000} {incr j} {
  110. assert {[lrange [lindex $items $j 1] 0 1] eq [list item $j]}
  111. }
  112. assert {[r xlen mystream] == $j}
  113. }
  114. test {XRANGE COUNT works as expected} {
  115. assert {[llength [r xrange mystream - + COUNT 10]] == 10}
  116. }
  117. test {XREVRANGE COUNT works as expected} {
  118. assert {[llength [r xrevrange mystream + - COUNT 10]] == 10}
  119. }
  120. test {XRANGE can be used to iterate the whole stream} {
  121. set last_id "-"
  122. set j 0
  123. while 1 {
  124. set elements [r xrange mystream $last_id + COUNT 100]
  125. if {[llength $elements] == 0} break
  126. foreach e $elements {
  127. assert {[lrange [lindex $e 1] 0 1] eq [list item $j]}
  128. incr j;
  129. }
  130. set last_id [streamNextID [lindex $elements end 0]]
  131. }
  132. assert {$j == 10000}
  133. }
  134. test {XREVRANGE returns the reverse of XRANGE} {
  135. assert {[r xrange mystream - +] == [lreverse [r xrevrange mystream + -]]}
  136. }
  137. test {XREAD with non empty stream} {
  138. set res [r XREAD COUNT 1 STREAMS mystream 0-0]
  139. assert {[lrange [lindex $res 0 1 0 1] 0 1] eq {item 0}}
  140. }
  141. test {Non blocking XREAD with empty streams} {
  142. set res [r XREAD STREAMS s1 s2 0-0 0-0]
  143. assert {$res eq {}}
  144. }
  145. test {XREAD with non empty second stream} {
  146. set res [r XREAD COUNT 1 STREAMS nostream mystream 0-0 0-0]
  147. assert {[lindex $res 0 0] eq {mystream}}
  148. assert {[lrange [lindex $res 0 1 0 1] 0 1] eq {item 0}}
  149. }
  150. test {Blocking XREAD waiting new data} {
  151. r XADD s2 * old abcd1234
  152. set rd [redis_deferring_client]
  153. $rd XREAD BLOCK 20000 STREAMS s1 s2 s3 $ $ $
  154. r XADD s2 * new abcd1234
  155. set res [$rd read]
  156. assert {[lindex $res 0 0] eq {s2}}
  157. assert {[lindex $res 0 1 0 1] eq {new abcd1234}}
  158. }
  159. test {Blocking XREAD waiting old data} {
  160. set rd [redis_deferring_client]
  161. $rd XREAD BLOCK 20000 STREAMS s1 s2 s3 $ 0-0 $
  162. r XADD s2 * foo abcd1234
  163. set res [$rd read]
  164. assert {[lindex $res 0 0] eq {s2}}
  165. assert {[lindex $res 0 1 0 1] eq {old abcd1234}}
  166. }
  167. test "XREAD: XADD + DEL should not awake client" {
  168. set rd [redis_deferring_client]
  169. r del s1
  170. $rd XREAD BLOCK 20000 STREAMS s1 $
  171. r multi
  172. r XADD s1 * old abcd1234
  173. r DEL s1
  174. r exec
  175. r XADD s1 * new abcd1234
  176. set res [$rd read]
  177. assert {[lindex $res 0 0] eq {s1}}
  178. assert {[lindex $res 0 1 0 1] eq {new abcd1234}}
  179. }
  180. test "XREAD: XADD + DEL + LPUSH should not awake client" {
  181. set rd [redis_deferring_client]
  182. r del s1
  183. $rd XREAD BLOCK 20000 STREAMS s1 $
  184. r multi
  185. r XADD s1 * old abcd1234
  186. r DEL s1
  187. r LPUSH s1 foo bar
  188. r exec
  189. r DEL s1
  190. r XADD s1 * new abcd1234
  191. set res [$rd read]
  192. assert {[lindex $res 0 0] eq {s1}}
  193. assert {[lindex $res 0 1 0 1] eq {new abcd1234}}
  194. }
  195. test {XREAD with same stream name multiple times should work} {
  196. r XADD s2 * old abcd1234
  197. set rd [redis_deferring_client]
  198. $rd XREAD BLOCK 20000 STREAMS s2 s2 s2 $ $ $
  199. r XADD s2 * new abcd1234
  200. set res [$rd read]
  201. assert {[lindex $res 0 0] eq {s2}}
  202. assert {[lindex $res 0 1 0 1] eq {new abcd1234}}
  203. }
  204. test {XREAD + multiple XADD inside transaction} {
  205. r XADD s2 * old abcd1234
  206. set rd [redis_deferring_client]
  207. $rd XREAD BLOCK 20000 STREAMS s2 s2 s2 $ $ $
  208. r MULTI
  209. r XADD s2 * field one
  210. r XADD s2 * field two
  211. r XADD s2 * field three
  212. r EXEC
  213. set res [$rd read]
  214. assert {[lindex $res 0 0] eq {s2}}
  215. assert {[lindex $res 0 1 0 1] eq {field one}}
  216. assert {[lindex $res 0 1 1 1] eq {field two}}
  217. }
  218. test {XDEL basic test} {
  219. r del somestream
  220. r xadd somestream * foo value0
  221. set id [r xadd somestream * foo value1]
  222. r xadd somestream * foo value2
  223. r xdel somestream $id
  224. assert {[r xlen somestream] == 2}
  225. set result [r xrange somestream - +]
  226. assert {[lindex $result 0 1 1] eq {value0}}
  227. assert {[lindex $result 1 1 1] eq {value2}}
  228. }
  229. # Here the idea is to check the consistency of the stream data structure
  230. # as we remove all the elements down to zero elements.
  231. test {XDEL fuzz test} {
  232. r del somestream
  233. set ids {}
  234. set x 0; # Length of the stream
  235. while 1 {
  236. lappend ids [r xadd somestream * item $x]
  237. incr x
  238. # Add enough elements to have a few radix tree nodes inside the stream.
  239. if {[dict get [r xinfo stream somestream] radix-tree-keys] > 20} break
  240. }
  241. # Now remove all the elements till we reach an empty stream
  242. # and after every deletion, check that the stream is sane enough
  243. # to report the right number of elements with XRANGE: this will also
  244. # force accessing the whole data structure to check sanity.
  245. assert {[r xlen somestream] == $x}
  246. # We want to remove elements in random order to really test the
  247. # implementation in a better way.
  248. set ids [lshuffle $ids]
  249. foreach id $ids {
  250. assert {[r xdel somestream $id] == 1}
  251. incr x -1
  252. assert {[r xlen somestream] == $x}
  253. # The test would be too slow calling XRANGE for every iteration.
  254. # Do it every 100 removal.
  255. if {$x % 100 == 0} {
  256. set res [r xrange somestream - +]
  257. assert {[llength $res] == $x}
  258. }
  259. }
  260. }
  261. test {XRANGE fuzzing} {
  262. set low_id [lindex $items 0 0]
  263. set high_id [lindex $items end 0]
  264. for {set j 0} {$j < 100} {incr j} {
  265. set start [streamRandomID $low_id $high_id]
  266. set end [streamRandomID $low_id $high_id]
  267. set range [r xrange mystream $start $end]
  268. set tcl_range [streamSimulateXRANGE $items $start $end]
  269. if {$range ne $tcl_range} {
  270. puts "*** WARNING *** - XRANGE fuzzing mismatch: $start - $end"
  271. puts "---"
  272. puts "XRANGE: '$range'"
  273. puts "---"
  274. puts "TCL: '$tcl_range'"
  275. puts "---"
  276. fail "XRANGE fuzzing failed, check logs for details"
  277. }
  278. }
  279. }
  280. test {XREVRANGE regression test for issue #5006} {
  281. # Add non compressed entries
  282. r xadd teststream 1234567891230 key1 value1
  283. r xadd teststream 1234567891240 key2 value2
  284. r xadd teststream 1234567891250 key3 value3
  285. # Add SAMEFIELD compressed entries
  286. r xadd teststream2 1234567891230 key1 value1
  287. r xadd teststream2 1234567891240 key1 value2
  288. r xadd teststream2 1234567891250 key1 value3
  289. assert_equal [r xrevrange teststream 1234567891245 -] {{1234567891240-0 {key2 value2}} {1234567891230-0 {key1 value1}}}
  290. assert_equal [r xrevrange teststream2 1234567891245 -] {{1234567891240-0 {key1 value2}} {1234567891230-0 {key1 value1}}}
  291. }
  292. }
  293. start_server {tags {"stream"} overrides {appendonly yes}} {
  294. test {XADD with MAXLEN > xlen can propagate correctly} {
  295. for {set j 0} {$j < 100} {incr j} {
  296. r XADD mystream * xitem v
  297. }
  298. r XADD mystream MAXLEN 200 * xitem v
  299. incr j
  300. assert {[r xlen mystream] == $j}
  301. r debug loadaof
  302. r XADD mystream * xitem v
  303. incr j
  304. assert {[r xlen mystream] == $j}
  305. }
  306. }
  307. start_server {tags {"stream"} overrides {appendonly yes}} {
  308. test {XADD with ~ MAXLEN can propagate correctly} {
  309. for {set j 0} {$j < 100} {incr j} {
  310. r XADD mystream * xitem v
  311. }
  312. r XADD mystream MAXLEN ~ $j * xitem v
  313. incr j
  314. assert {[r xlen mystream] == $j}
  315. r config set stream-node-max-entries 1
  316. r debug loadaof
  317. r XADD mystream * xitem v
  318. incr j
  319. assert {[r xlen mystream] == $j}
  320. }
  321. }
  322. start_server {tags {"stream"} overrides {appendonly yes stream-node-max-entries 10}} {
  323. test {XTRIM with ~ MAXLEN can propagate correctly} {
  324. for {set j 0} {$j < 100} {incr j} {
  325. r XADD mystream * xitem v
  326. }
  327. r XTRIM mystream MAXLEN ~ 85
  328. assert {[r xlen mystream] == 90}
  329. r config set stream-node-max-entries 1
  330. r debug loadaof
  331. r XADD mystream * xitem v
  332. incr j
  333. assert {[r xlen mystream] == 91}
  334. }
  335. }
  336. start_server {tags {"xsetid"}} {
  337. test {XADD can CREATE an empty stream} {
  338. r XADD mystream MAXLEN 0 * a b
  339. assert {[dict get [r xinfo stream mystream] length] == 0}
  340. }
  341. test {XSETID can set a specific ID} {
  342. r XSETID mystream "200-0"
  343. assert {[dict get [r xinfo stream mystream] last-generated-id] == "200-0"}
  344. }
  345. test {XSETID cannot SETID with smaller ID} {
  346. r XADD mystream * a b
  347. catch {r XSETID mystream "1-1"} err
  348. r XADD mystream MAXLEN 0 * a b
  349. set err
  350. } {ERR*smaller*}
  351. test {XSETID cannot SETID on non-existent key} {
  352. catch {r XSETID stream 1-1} err
  353. set _ $err
  354. } {ERR no such key}
  355. }
  356. start_server {tags {"stream"} overrides {appendonly yes aof-use-rdb-preamble no}} {
  357. test {Empty stream can be rewrite into AOF correctly} {
  358. r XADD mystream MAXLEN 0 * a b
  359. assert {[dict get [r xinfo stream mystream] length] == 0}
  360. r bgrewriteaof
  361. waitForBgrewriteaof r
  362. r debug loadaof
  363. assert {[dict get [r xinfo stream mystream] length] == 0}
  364. }
  365. test {Stream can be rewrite into AOF correctly after XDEL lastid} {
  366. r XSETID mystream 0-0
  367. r XADD mystream 1-1 a b
  368. r XADD mystream 2-2 a b
  369. assert {[dict get [r xinfo stream mystream] length] == 2}
  370. r XDEL mystream 2-2
  371. r bgrewriteaof
  372. waitForBgrewriteaof r
  373. r debug loadaof
  374. assert {[dict get [r xinfo stream mystream] length] == 1}
  375. assert {[dict get [r xinfo stream mystream] last-generated-id] == "2-2"}
  376. }
  377. }