Uploaded image for project: 'WiredTiger'
  1. WiredTiger
  2. WT-11533

Investigate python reproducer showing weakness in random cursor with invisible records

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
    • StorEng - Defined Pipeline

      From the investigation of WT-8003, I have modified the cursor_random02 test to create a python reproducer that shows a weakness of grabbing a random record from the random cursor.

      #!/usr/bin/env python
      #
      # Public Domain 2014-present MongoDB, Inc.
      # Public Domain 2008-2014 WiredTiger, Inc.
      #
      # This is free and unencumbered software released into the public domain.
      #
      # Anyone is free to copy, modify, publish, use, compile, sell, or
      # distribute this software, either in source code form or as a compiled
      # binary, for any purpose, commercial or non-commercial, and by any
      # means.
      #
      # In jurisdictions that recognize copyright laws, the author or authors
      # of this software dedicate any and all copyright interest in the
      # software to the public domain. We make this dedication for the benefit
      # of the public at large and to the detriment of our heirs and
      # successors. We intend this dedication to be an overt act of
      # relinquishment in perpetuity of all present and future rights to this
      # software under copyright law.
      #
      # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
      # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
      # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
      # IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
      # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
      # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
      # OTHER DEALINGS IN THE SOFTWARE.
      
      import wttest
      from wtdataset import SimpleDataSet
      from wtscenario import make_scenarios
      
      # test_cursor_random02.py
      #    Cursor next_random operations
      class test_cursor_random02(wttest.WiredTigerTestCase):
          types = [
              #('lsm', dict(type='lsm:random')),
              ('table', dict(type='table:random'))
          ]
          config = [
              ('not-sample', dict(config='next_random=true'))
          ]
          records = [
              # ('1', dict(records=1)),
              # ('250', dict(records=250)),
              # ('500', dict(records=500)),
              # ('5000', dict(records=5000)),
              # ('10000', dict(records=10000)),
              ('50000', dict(records=50000)),
          ]
          scenarios = make_scenarios(config, records, types)
      
          # Check that next_random works in the presence of a larger set of values,
          # where the values are in an insert list.
          def test_cursor_random_reasonable_distribution(self):
              uri = self.type
              num_entries = self.records
              if uri == 'table:random':
                  config = 'leaf_page_max=100MB'
              else:
                  config = ''
      
              self.session.create(uri, 'key_format=i,value_format=S')
              
              self.session.begin_transaction()
              cursor = self.session.open_cursor(uri, None, None)
              for i in range(0, 24000):
                  cursor[i] = str(i) * 10
      
              cursor = self.session.open_cursor(uri, None, None)
              for i in range(26000, num_entries):
                  cursor[i] = str(i) * 10
              self.session.commit_transaction("commit_timestamp=" + self.timestamp_str(15))
      
              self.session.begin_transaction()
              cursor = self.session.open_cursor(uri, None, None)
              for i in range(24000, 26000):
                  cursor[i] = str(i) * 10
              self.session.commit_transaction("commit_timestamp=" + self.timestamp_str(10))
              cursor.close()
      
              # Set the leaf-page-max value, otherwise the page might split.
              # Setup an array to track which keys are seen
              visitedKeys = [0] * (num_entries + 1)
              # Setup a counter to see when we find a sequential key
              sequentialKeys = 0
      
              self.session.begin_transaction("read_timestamp=" + self.timestamp_str(13))
              cursor = self.session.open_cursor(uri, None, 'next_random=true')
              lastKey = None
              for i in range(0, 2000):
                  self.assertEqual(cursor.next(), 0)
                  current = cursor.get_key()
                  current = int(current)
                  #print(current)
                  visitedKeys[current] = visitedKeys[current] + 1
                  if lastKey != None:
                      if current == (lastKey + 1):
                          sequentialKeys += 1
                  lastKey = current
      
              self.session.commit_transaction()
              differentKeys = sum(x > 0 for x in visitedKeys)
      
              self.tty(visitedKeys[24000:26000])
              #print visitedKeys
              #print(differentKeys)
              
              self.tty('differentKeys: ' + str(differentKeys) + ' of ' + \
                  str(num_entries) + ', ' + \
                  str((int)((differentKeys * 100) // num_entries)) + '%')
      
              # Can't test for non-sequential data when there is 1 item in the table
              # if num_entries > 1:
              #     self.assertGreater(num_entries - 1, sequentialKeys,
              #         'cursor is returning sequential data')
              # self.assertGreater(differentKeys, num_entries // 4,
              #     'next_random random distribution not adequate')
      
      if __name__ == '__main__':
          wttest.run()
      
      

      To describe the context on why this issue happens is due how the random algorithm works:

      1. If disk records are > 1000, find a visible record from page disk.
      2. Look to find a visible record from the insert list
      3. If disk records are < 200 or the leaf page is clean, try to find a visible page disk.
      4. Last step, compute a random record between 0 - 250 entries from the page and perform bt_curnext and bt_curprev until we find a visible record.

      The python reproducer has roughly 90% invisible records to the transaction under the random cursor. It is expected that 90% of the time we would reach a page where none of the records are visible to us. None of the conditions are met such that we reach the last step which starts to skew the randomness of the cursor. In the case that we have key range 0 -> 200k keys, and the key range 100k -> 120k is only visible to us. Everytime we hit a page that is inside 0 -> 100k and 120k -> 200k, the records we can only randomly select is realistically 100,000 - 100,250 and 111,750 - 120,000 keys. Thus producing the problem.

      The ticket aims to investigate further on improving the random algorithm. The developer will look at the test and look into whether there are possible ways we can improve the randomness to this edge case. If not, it is okay to close this ticket as "Won't Do".

            Assignee:
            backlog-server-storage-engines [DO NOT USE] Backlog - Storage Engines Team
            Reporter:
            jie.chen@mongodb.com Jie Chen
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: