KeyDB icon indicating copy to clipboard operation
KeyDB copied to clipboard

[BUG] After flushall, the inserted key dbsize is still 0 in some scenarios.

Open saadnaseem97 opened this issue 3 years ago • 0 comments

Describe the bug

After flushall, the inserted key dbsize is still 0 in some scenarios.

The root cause is that snapshots are not cleared during flushall. Only m_pdict of the database is reset.

If a snapshot exists before flushall, the m_pdict of the snapshot is not cleared. When the endSnapshot function is executed to destroy the snapshot, the m_pdict of the snapshot is exchanged with the m_pdict of the database. In this case, the inserted key is not in the graveyard m_pdictTombstone and is not deleted. As a result, the key still exists in m_pdict in the database.

When the key inserted before flushall is inserted next time, the key is not considered as a new key and dbsize is not updated because the key still exists in m_pdict of the database.

To reproduce

  1. Change the value of databases in tests/assets/default.conf to 20 and add the following code to SET of existing but flushed key with EXPIRE works test case in tests/unit/flash.tcl:
test { SET of existing but flushed key with EXPIRE works } {
    for {set j 0} {$j < 20} {incr j} {
        r select $j
        r set $j 1
    }
    r select 9
  1. execute tcl test: tclsh tests/test_helper.tcl --single unit/flash

  2. In the command output, the value of dbsize is 0 which should be 1.

[err]:  SET of existing but flushed key with EXPIRE works  in tests/unit/flash.tcl
Expected '1' to be equal to '0' (detail: Only one key after first insert)

Note: If the error is not triggered, increase the value of databases and change the number of cycles in the test case accordingly.

Expected behavior

The new key should be added properly in the test and the dbsize should be 1 instead of 0.

Additional information

I also tried to add a wait condition to wait for the dbsize to be updated but that doesn't help, below is the test:

test { SET of existing but flushed key with EXPIRE works } {
      for {set j 0} {$j < 20} {incr j} {
          r select $j
          r set $j 1
      }
      r select 9
      r flushall
      after 10000
      assert_equal {0} [r dbsize]
      r set testkey foo ex 10000
      wait_for_condition 1000 50 {
          [r dbsize] == 1
      } else {
          fail "Expected '1' to be equal to '0' (detail: Only one key after first insert)"
      }  

      r flushall cache
      assert_equal {1} [r dbsize] "Only one key after flushall cache"
      r set testkey bar ex 10000
      assert_equal {1} [r dbsize] "Only one key after overwrite"
      assert_equal {bar} [r get testkey]
      assert [expr [r ttl testkey] > 0]
  }

saadnaseem97 avatar Mar 30 '23 15:03 saadnaseem97