[SERVER-71862] Creating a unique index that should fail can return OK occasionally from createIndexes Created: 05/Dec/22  Updated: 29/Oct/23  Resolved: 26/Jan/23

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 6.3.0-rc0

Type: Bug Priority: Major - P3
Reporter: Gregory Wlodarek Assignee: Gregory Noma
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Assigned Teams:
Storage Execution
Backwards Compatibility: Fully Compatible
Operating System: ALL
Steps To Reproduce:

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

 

Sprint: Execution Team 2023-02-06
Participants:

 Description   

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"}}}}



 Comments   
Comment by Githook User [ 26/Jan/23 ]

Author:

{'name': 'Gregory Noma', 'email': 'gregory.noma@gmail.com', 'username': 'gregorynoma'}

Message: SERVER-71862 Return index build result from duplicate `createIndexes`
Branch: master
https://github.com/mongodb/mongo/commit/364aa0b1cfb69bc53615ab11b7d62e918025ec79

Comment by Gregory Noma [ 25/Jan/23 ]

Usually, if a createIndexes command finds an in-progress index build for an identical index spec, it will wait for that index build to complete (and in this case end up returning the same DuplicateKey error that the index build encounters). However, if we get past this check before the index build (from the concurrent createIndexes command) is registered, instead the spec will get filtered out when attempting to register our own index build.

Generated at Thu Feb 08 06:20:10 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.