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

Creating a unique index that should fail can return OK occasionally from createIndexes

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 6.3.0-rc0
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • Storage Execution
    • Fully Compatible
    • ALL
    • Hide

      Create this file:

      jstests/concurrency/fsm_workloads/unique_index_duplicate_key.js
      

      Paste this in:

      "use strict";
      
      var $config = (function() {
          var states = (function() {
              function createIndex(db, collName) {
                  assert.commandFailedWithCode(db[collName].createIndex({x: 1}, {unique: true}),
                                               ErrorCodes.DuplicateKey);
              }
      
              function init(db, collName) {
                  assert.commandWorked(db[collName].insert({x: 1}));
                  assert.commandWorked(db[collName].insert({x: 1}));
              }
      
              return {init: init, createIndex: createIndex};
          })();
      
          var transitions = {
              init: {createIndex: 1},
              createIndex: {createIndex: 1},
          };
      
          return {
              threadCount: 25,
              iterations: 1000,
              states: states,
              transitions: transitions,
          };
      })();
      

      Run it:

      resmoke --suites=concurrency_replication jstests/concurrency/fsm_workloads/unique_index_duplicate_key.js
      

       

      Show
      Create this file: jstests/concurrency/fsm_workloads/unique_index_duplicate_key.js Paste this in: "use strict" ; var $config = (function() { var states = (function() { function createIndex(db, collName) { assert .commandFailedWithCode(db[collName].createIndex({x: 1}, {unique: true }), ErrorCodes.DuplicateKey); } function init(db, collName) { assert .commandWorked(db[collName].insert({x: 1})); assert .commandWorked(db[collName].insert({x: 1})); } return {init: init, createIndex: createIndex}; })(); var transitions = { init: {createIndex: 1}, createIndex: {createIndex: 1}, }; return { threadCount: 25, iterations: 1000, states: states, transitions: transitions, }; })(); Run it: resmoke --suites=concurrency_replication jstests/concurrency/fsm_workloads/unique_index_duplicate_key.js  
    • Execution Team 2023-02-06

      I wrote a concurrency test where multiple clients try to build the same unique index, all of which should fail due to documents with a duplicate key.

      Occasionally (once every 5 minutes), the test will return OK:

      Foreground jstests/concurrency/fsm_workloads/unique_index_duplicate_key.js
      Error: command worked when it should have failed: {
       "numIndexesBefore" : 2,
       "numIndexesAfter" : 2,
       "createdCollectionAutomatically" : false,
       "commitQuorum" : "votingMembers",
       "note" : "all indexes already exist",
       "ok" : 1,
       "$clusterTime" : {
               "clusterTime" : Timestamp(1670274510, 21),
               "signature" : {
                       "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                       "keyId" : NumberLong(0)
               }
       },
       "operationTime" : Timestamp(1670274510, 21)
      }
      

      We should only have one index (_id) in this workload, but it's reporting numIndexesBefore/numIndexesAfter as 2, which results in this returning OK. The index was not actually built as seen by validate:

      [j0:prim] {"t":{"$date":"2022-12-05T21:47:05.459+00:00"},"s":"I",  "c":"COMMAND",  "id":20514,   "ctx":"conn194","msg":"CMD: validate","attr":{"namespace":"test0_fsmdb0.fsmcoll0","background":false,"full":true,"enforceFastCount":true,"checkBSONConformance":false,"repair":false}}
      [j0:prim] {"t":{"$date":"2022-12-05T21:47:05.460+00:00"},"s":"I",  "c":"INDEX",    "id":20295,   "ctx":"conn194","msg":"Validating internal structure","attr":{"index":"_id_","namespace":"test0_fsmdb0.fsmcoll0"}}
      [j0:prim] {"t":{"$date":"2022-12-05T21:47:05.465+00:00"},"s":"I",  "c":"INDEX",    "id":20303,   "ctx":"conn194","msg":"validating collection","attr":{"namespace":"test0_fsmdb0.fsmcoll0","uuid":{"uuid":{"$uuid":"b868fd78-5ddb-4db2-aaf2-511b3325e25e"}}}}
      [j0:prim] {"t":{"$date":"2022-12-05T21:47:05.468+00:00"},"s":"I",  "c":"INDEX",    "id":20296,   "ctx":"conn194","msg":"Validating index consistency","attr":{"index":"_id_","namespace":"test0_fsmdb0.fsmcoll0"}}
      [j0:prim] {"t":{"$date":"2022-12-05T21:47:05.469+00:00"},"s":"I",  "c":"INDEX",    "id":20306,   "ctx":"conn194","msg":"Validation complete for collection. No corruption found","attr":{"namespace":"test0_fsmdb0.fsmcoll0","uuid":{"uuid":{"$uuid":"b868fd78-5ddb-4db2-aaf2-511b3325e25e"}}}}
      

            Assignee:
            gregory.noma@mongodb.com Gregory Noma
            Reporter:
            gregory.wlodarek@mongodb.com Gregory Wlodarek
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: