Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-58943

Add more test coverage for unique indexes

    • Type: Icon: Task Task
    • Resolution: Fixed
    • Priority: Icon: Critical - P2 Critical - P2
    • 5.1.0-rc0
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • Fully Compatible
    • Execution Team 2021-08-09, Execution Team 2021-08-23, Execution Team 2021-09-06

      SERVER-56509 introduced a bug where the unique index constraint was not enforced in some cases. We should add more test coverage for this.

      The issue could be reproduced with the following script that we can base additional testing on:

      function repro1() {
       
          db.c.drop()
          db.c.createIndex({t: 1, i: 1}, {unique: true})
       
          const n = 10000
          const t = 0
          for (var i = 0; i < n; i++)
              db.c.insert({t, i})
          for (iter = 0; ; iter++) {
              print("iter", iter)
              for (var i = 0; i < n; i++) {
                  r = db.c.insert({t, i, x: 'x'})
                  if (r.nInserted != 0)
                      print("dup!", t, i, "iter", iter, "count", db.c.count({t, i}))
              }
          }
      }
      

      Which reproduces the issue after a couple of iterations:

      iter 0
      iter 1
      dup! 0 9999 iter 1 count 2
      

      A multithreaded version also reproduces:

      function repro() {
       
          db.c.drop()
          db.c.createIndex({t: 1, i: 1}, {unique: true})
       
          nthreads = 100
          threads = []
          for (var t=0; t<nthreads; t++) {
              thread = new Thread(function(t) {
                  for (var i = 0; i < 100; i++)
                      db.c.insert({t, i, x: 'x'})
                  while (true) {
                      for (var i = 0; i < 100; i++) {
                          r = db.c.insert({t, i, x: 'x'})
                          if (r.nInserted != 0)
                              print("dup!", t, i, "count", db.c.count({t, i}))
                      }
                  }
              }, t)
              threads.push(thread)
              thread.start()
          }
          for (var t = 0; t < nthreads; t++)
              threads[t].join()
      }
      

            Assignee:
            haley.connelly@mongodb.com Haley Connelly
            Reporter:
            henrik.edin@mongodb.com Henrik Edin
            Votes:
            0 Vote for this issue
            Watchers:
            17 Start watching this issue

              Created:
              Updated:
              Resolved: