2
0

maxmemory.tcl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. start_server {tags {"maxmemory"}} {
  2. test "Without maxmemory small integers are shared" {
  3. r config set maxmemory 0
  4. r set a 1
  5. assert {[r object refcount a] > 1}
  6. }
  7. test "With maxmemory and non-LRU policy integers are still shared" {
  8. r config set maxmemory 1073741824
  9. r config set maxmemory-policy allkeys-random
  10. r set a 1
  11. assert {[r object refcount a] > 1}
  12. }
  13. test "With maxmemory and LRU policy integers are not shared" {
  14. r config set maxmemory 1073741824
  15. r config set maxmemory-policy allkeys-lru
  16. r set a 1
  17. r config set maxmemory-policy volatile-lru
  18. r set b 1
  19. assert {[r object refcount a] == 1}
  20. assert {[r object refcount b] == 1}
  21. r config set maxmemory 0
  22. }
  23. foreach policy {
  24. allkeys-random allkeys-lru volatile-lru volatile-random volatile-ttl
  25. } {
  26. test "maxmemory - is the memory limit honoured? (policy $policy)" {
  27. # make sure to start with a blank instance
  28. r flushall
  29. # Get the current memory limit and calculate a new limit.
  30. # We just add 100k to the current memory size so that it is
  31. # fast for us to reach that limit.
  32. set used [s used_memory]
  33. set limit [expr {$used+100*1024}]
  34. r config set maxmemory $limit
  35. r config set maxmemory-policy $policy
  36. # Now add keys until the limit is almost reached.
  37. set numkeys 0
  38. while 1 {
  39. r setex [randomKey] 10000 x
  40. incr numkeys
  41. if {[s used_memory]+4096 > $limit} {
  42. assert {$numkeys > 10}
  43. break
  44. }
  45. }
  46. # If we add the same number of keys already added again, we
  47. # should still be under the limit.
  48. for {set j 0} {$j < $numkeys} {incr j} {
  49. r setex [randomKey] 10000 x
  50. }
  51. assert {[s used_memory] < ($limit+4096)}
  52. }
  53. }
  54. foreach policy {
  55. allkeys-random allkeys-lru volatile-lru volatile-random volatile-ttl
  56. } {
  57. test "maxmemory - only allkeys-* should remove non-volatile keys ($policy)" {
  58. # make sure to start with a blank instance
  59. r flushall
  60. # Get the current memory limit and calculate a new limit.
  61. # We just add 100k to the current memory size so that it is
  62. # fast for us to reach that limit.
  63. set used [s used_memory]
  64. set limit [expr {$used+100*1024}]
  65. r config set maxmemory $limit
  66. r config set maxmemory-policy $policy
  67. # Now add keys until the limit is almost reached.
  68. set numkeys 0
  69. while 1 {
  70. r set [randomKey] x
  71. incr numkeys
  72. if {[s used_memory]+4096 > $limit} {
  73. assert {$numkeys > 10}
  74. break
  75. }
  76. }
  77. # If we add the same number of keys already added again and
  78. # the policy is allkeys-* we should still be under the limit.
  79. # Otherwise we should see an error reported by Redis.
  80. set err 0
  81. for {set j 0} {$j < $numkeys} {incr j} {
  82. if {[catch {r set [randomKey] x} e]} {
  83. if {[string match {*used memory*} $e]} {
  84. set err 1
  85. }
  86. }
  87. }
  88. if {[string match allkeys-* $policy]} {
  89. assert {[s used_memory] < ($limit+4096)}
  90. } else {
  91. assert {$err == 1}
  92. }
  93. }
  94. }
  95. foreach policy {
  96. volatile-lru volatile-random volatile-ttl
  97. } {
  98. test "maxmemory - policy $policy should only remove volatile keys." {
  99. # make sure to start with a blank instance
  100. r flushall
  101. # Get the current memory limit and calculate a new limit.
  102. # We just add 100k to the current memory size so that it is
  103. # fast for us to reach that limit.
  104. set used [s used_memory]
  105. set limit [expr {$used+100*1024}]
  106. r config set maxmemory $limit
  107. r config set maxmemory-policy $policy
  108. # Now add keys until the limit is almost reached.
  109. set numkeys 0
  110. while 1 {
  111. # Odd keys are volatile
  112. # Even keys are non volatile
  113. if {$numkeys % 2} {
  114. r setex "key:$numkeys" 10000 x
  115. } else {
  116. r set "key:$numkeys" x
  117. }
  118. if {[s used_memory]+4096 > $limit} {
  119. assert {$numkeys > 10}
  120. break
  121. }
  122. incr numkeys
  123. }
  124. # Now we add the same number of volatile keys already added.
  125. # We expect Redis to evict only volatile keys in order to make
  126. # space.
  127. set err 0
  128. for {set j 0} {$j < $numkeys} {incr j} {
  129. catch {r setex "foo:$j" 10000 x}
  130. }
  131. # We should still be under the limit.
  132. assert {[s used_memory] < ($limit+4096)}
  133. # However all our non volatile keys should be here.
  134. for {set j 0} {$j < $numkeys} {incr j 2} {
  135. assert {[r exists "key:$j"]}
  136. }
  137. }
  138. }
  139. }