summaryrefslogtreecommitdiff
path: root/jstests/sharding/updateOne_without_shard_key/errors.js
blob: 953317d6300ff6af1ca1b2cdbf61f9c2c6d673fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
 * Ensure write errors produced by writes without shard keys are propagated to the client.
 *
 * @tags: [
 *    requires_sharding,
 *    requires_fcv_63,
 *    uses_transactions,
 *    uses_multi_shard_transaction,
 *    featureFlagUpdateOneWithoutShardKey,
 * ]
 */

(function() {
"use strict";

load("jstests/sharding/updateOne_without_shard_key/libs/write_without_shard_key_test_util.js");

// Make sure we're testing with no implicit session.
TestData.disableImplicitSessions = true;

// 2 shards single node, 1 mongos, 1 config server 3-node.
const st = new ShardingTest({});
const dbName = "testDb";
const collectionName = "testColl";
const nss = dbName + "." + collectionName;
const testColl = st.getDB(dbName).getCollection(collectionName);
const splitPoint = 0;
const insertDocs = [{_id: 0, x: -2, y: 5}, {_id: 1, x: 2, y: 5}, {_id: 2, x: 3, y: 5}];

// Sets up a 2 shard cluster using 'x' as a shard key where Shard 0 owns x <
// splitPoint and Shard 1 splitPoint >= 0.
WriteWithoutShardKeyTestUtil.setupShardedCollection(
    st, nss, {x: 1}, [{x: splitPoint}], [{query: {x: splitPoint}, shard: st.shard1.shardName}]);

// Insert initial data.
assert.commandWorked(testColl.insert(insertDocs));

function runCommandAndCheckError(testCase, additionalCmdFields = {}) {
    const cmdObjWithAdditionalFields = Object.assign({}, testCase.cmdObj, additionalCmdFields);
    jsTest.log(cmdObjWithAdditionalFields);

    const res = st.getDB(dbName).runCommand(cmdObjWithAdditionalFields);
    assert.commandFailedWithCode(res, testCase.errorCode);

    // FindAndModify is not a batch command, thus will not have a writeErrors field.
    if (!testCase.cmdObj.findAndModify) {
        res.writeErrors.forEach(writeError => {
            assert(testCase.errorCode.includes(writeError.code));
            assert(testCase.index.includes(writeError.index));
        });
    }
}

const testCases = [
    {
        logMessage: "Unknown modifier in findAndModify, FailedToParse expected.",
        errorCode: [ErrorCodes.FailedToParse],
        cmdObj: {
            findAndModify: collectionName,
            query: {y: 5},
            update: {$match: {y: 3}},
        }
    },
    {
        logMessage: "Unknown modifier in batch update, FailedToParse expected.",
        errorCode: [ErrorCodes.FailedToParse],
        index: [0],
        cmdObj: {
            update: collectionName,
            updates: [{q: {y: 5}, u: {$match: {z: 0}}}],
        }
    },
    {
        logMessage: "Incorrect query in delete, BadValue expected.",
        errorCode: [ErrorCodes.BadValue],
        index: [0],
        cmdObj: {
            delete: collectionName,
            deletes: [{q: {y: {$match: 5}}, limit: 1}],
        }
    },
    {
        logMessage: "Two updates in a batch, one successful, one FailedToParse expected.",
        errorCode: [ErrorCodes.FailedToParse],
        index: [0],
        cmdObj: {
            update: collectionName,
            updates: [{q: {y: 5}, u: {$match: {z: 0}}}, {q: {y: 5}, u: {$set: {a: 0}}}],
        }
    },
    {
        logMessage:
            "Three updates in a batch, one BadValue and two FailedToParse expected. If this " +
            "command is run in a transaction, it will abort upon first error.",
        errorCode: [ErrorCodes.BadValue, ErrorCodes.FailedToParse],
        index: [0, 1, 2],
        cmdObj: {
            update: collectionName,
            updates: [
                {q: {y: {$match: 5}}, u: {$set: {z: 0}}},
                {q: {y: 5}, u: {$match: {z: 0}}},
                {q: {y: 5}, u: {$match: {z: 0}}}
            ],
            ordered: false
        }
    },
    {
        logMessage: "Two deletes in a batch, one successful, one BadValue expected.",
        errorCode: [ErrorCodes.BadValue],
        index: [1],
        cmdObj: {
            delete: collectionName,
            deletes: [{q: {y: 5}, limit: 1}, {q: {y: {$match: 5}}, limit: 1}],
        }
    },
    {
        logMessage:
            "Three deletes in a batch, one successful, two BadValues expected. If this command is" +
            " run in a transaction, it will abort upon first error.",
        errorCode: [ErrorCodes.BadValue],
        index: [0, 2],
        cmdObj: {
            delete: collectionName,
            deletes: [
                {q: {y: {$match: 5}}, limit: 1},
                {q: {y: 5}, limit: 1},
                {q: {y: {$match: 5}}, limit: 1}
            ],
            ordered: false
        }
    },
];

testCases.forEach(testCase => {
    jsTest.log(testCase.logMessage + "\n" +
               "Running as non-retryable write.");
    runCommandAndCheckError(testCase);

    jsTest.log(testCase.logMessage + "\n" +
               "Running as non-retryable write in a session.");
    const logicalSessionFields = {lsid: {id: UUID()}};
    runCommandAndCheckError(testCase, logicalSessionFields);

    jsTest.log(testCase.logMessage + "\n" +
               "Running as retryable write.");
    const retryableWriteFields = {
        lsid: {id: UUID()},
        txnNumber: NumberLong(0),
    };
    runCommandAndCheckError(testCase, retryableWriteFields);

    jsTest.log(testCase.logMessage + "\n" +
               "Running in a transaction.");
    const transactionFields =
        {lsid: {id: UUID()}, txnNumber: NumberLong(0), startTransaction: true, autocommit: false};
    runCommandAndCheckError(testCase, transactionFields);
});

st.stop();
})();