2
0

maxmemory.tcl 4.6 KB

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