|
| 1 | +import time |
| 2 | +import json |
| 3 | +from RLTest import Defaults |
| 4 | + |
| 5 | +Defaults.decode_responses = True |
| 6 | + |
| 7 | +def enableDefrag(env): |
| 8 | + # make defrag as aggressive as possible |
| 9 | + env.cmd('CONFIG', 'SET', 'hz', '100') |
| 10 | + env.cmd('CONFIG', 'SET', 'active-defrag-ignore-bytes', '1') |
| 11 | + env.cmd('CONFIG', 'SET', 'active-defrag-threshold-lower', '0') |
| 12 | + env.cmd('CONFIG', 'SET', 'active-defrag-cycle-min', '99') |
| 13 | + |
| 14 | + try: |
| 15 | + env.cmd('CONFIG', 'SET', 'activedefrag', 'yes') |
| 16 | + except Exception: |
| 17 | + # If active defrag is not supported by the current Redis, simply skip the test. |
| 18 | + env.skip() |
| 19 | + |
| 20 | +def defragOnObj(env, obj): |
| 21 | + enableDefrag(env) |
| 22 | + json_str = json.dumps(obj) |
| 23 | + env.expect('JSON.SET', 'test', '$', json_str).ok() |
| 24 | + for i in range(10000): |
| 25 | + env.expect('JSON.SET', 'test%d' % i, '$', json_str).ok() |
| 26 | + i += 1 |
| 27 | + env.expect('JSON.SET', 'test%d' % i, '$', json_str).ok() |
| 28 | + for i in range(10000): |
| 29 | + env.expect('DEL', 'test%d' % i).equal(1) |
| 30 | + i += 1 |
| 31 | + _, _, _, _, _, keysDefrag = env.cmd('JSON.DEBUG', 'DEFRAG_INFO') |
| 32 | + startTime = time.time() |
| 33 | + # Wait for at least 2 defrag full cycles |
| 34 | + # We verify only the 'keysDefrag' value because the other values |
| 35 | + # are not promised to be updated. It depends if Redis support |
| 36 | + # the start/end defrag callbacks. |
| 37 | + while keysDefrag < 2: |
| 38 | + time.sleep(0.1) |
| 39 | + _, _, _, _, _, keysDefrag = env.cmd('JSON.DEBUG', 'DEFRAG_INFO') |
| 40 | + if time.time() - startTime > 30: |
| 41 | + # We will wait for up to 30 seconds and then we consider it a failure |
| 42 | + env.assertTrue(False, message='Failed waiting for defrag to run') |
| 43 | + return |
| 44 | + # make sure json is still valid. |
| 45 | + res = json.loads(env.cmd('JSON.GET', 'test%d' % i, '$'))[0] |
| 46 | + env.assertEqual(res, obj) |
| 47 | + env.assertGreater(env.cmd('info', 'Stats')['active_defrag_key_hits'], 0) |
| 48 | + |
| 49 | +def testDefragNumber(env): |
| 50 | + defragOnObj(env, 1) |
| 51 | + |
| 52 | +def testDefragBigNumber(env): |
| 53 | + defragOnObj(env, 100000000000000000000) |
| 54 | + |
| 55 | +def testDefragDouble(env): |
| 56 | + defragOnObj(env, 1.111111111111) |
| 57 | + |
| 58 | +def testDefragNegativeNumber(env): |
| 59 | + defragOnObj(env, -100000000000000000000) |
| 60 | + |
| 61 | +def testDefragNegativeDouble(env): |
| 62 | + defragOnObj(env, -1.111111111111) |
| 63 | + |
| 64 | +def testDefragTrue(env): |
| 65 | + defragOnObj(env, True) |
| 66 | + |
| 67 | +def testDefragFalse(env): |
| 68 | + defragOnObj(env, True) |
| 69 | + |
| 70 | +def testDefragNone(env): |
| 71 | + defragOnObj(env, None) |
| 72 | + |
| 73 | +def testDefragEmptyString(env): |
| 74 | + defragOnObj(env, "") |
| 75 | + |
| 76 | +def testDefragString(env): |
| 77 | + defragOnObj(env, "foo") |
| 78 | + |
| 79 | +def testDefragEmptyArray(env): |
| 80 | + defragOnObj(env, []) |
| 81 | + |
| 82 | +def testDefragArray(env): |
| 83 | + defragOnObj(env, [1, 2, 3]) |
| 84 | + |
| 85 | +def testDefragEmptyObject(env): |
| 86 | + defragOnObj(env, {}) |
| 87 | + |
| 88 | +def testDefragObject(env): |
| 89 | + defragOnObj(env, {"foo": "bar"}) |
| 90 | + |
| 91 | +def testDefragComplex(env): |
| 92 | + defragOnObj(env, {"foo": ["foo", 1, None, True, False, {}, {"foo": [], "bar": 1}]}) |
| 93 | + |
| 94 | +def testDefragBigJsons(env): |
| 95 | + enableDefrag(env) |
| 96 | + |
| 97 | + # Disable defrag so we can actually create fragmentation |
| 98 | + env.cmd('CONFIG', 'SET', 'activedefrag', 'no') |
| 99 | + |
| 100 | + env.expect('JSON.SET', 'key1', '$', "[]").ok() |
| 101 | + env.expect('JSON.SET', 'key2', '$', "[]").ok() |
| 102 | + |
| 103 | + for i in range(100000): |
| 104 | + env.cmd('JSON.ARRAPPEND', 'key1', '$', "[1.11111111111]") |
| 105 | + env.cmd('JSON.ARRAPPEND', 'key2', '$', "[1.11111111111]") |
| 106 | + |
| 107 | + # Now we delete key2 which should cause fragmenation |
| 108 | + env.expect('DEL', 'key2').equal(1) |
| 109 | + |
| 110 | + # wait for fragmentation for up to 30 seconds |
| 111 | + frag = env.cmd('info', 'memory')['allocator_frag_ratio'] |
| 112 | + startTime = time.time() |
| 113 | + while frag < 1.4: |
| 114 | + time.sleep(0.1) |
| 115 | + frag = env.cmd('info', 'memory')['allocator_frag_ratio'] |
| 116 | + if time.time() - startTime > 30: |
| 117 | + # We will wait for up to 30 seconds and then we consider it a failure |
| 118 | + env.assertTrue(False, message='Failed waiting for fragmentation, current value %s which is expected to be above 1.4.' % frag) |
| 119 | + return |
| 120 | + |
| 121 | + #enable active defrag |
| 122 | + env.cmd('CONFIG', 'SET', 'activedefrag', 'yes') |
| 123 | + |
| 124 | + # wait for fragmentation for go down for up to 30 seconds |
| 125 | + frag = env.cmd('info', 'memory')['allocator_frag_ratio'] |
| 126 | + startTime = time.time() |
| 127 | + while frag > 1.1: |
| 128 | + time.sleep(0.1) |
| 129 | + frag = env.cmd('info', 'memory')['allocator_frag_ratio'] |
| 130 | + if time.time() - startTime > 30: |
| 131 | + # We will wait for up to 30 seconds and then we consider it a failure |
| 132 | + env.assertTrue(False, message='Failed waiting for fragmentation to go down, current value %s which is expected to be bellow 1.1.' % frag) |
| 133 | + return |
0 commit comments