summaryrefslogtreecommitdiff
path: root/jstests/replsets/multikey_write_avoids_prepare_conflict.js
blob: 46d58e0b6ca416b7c91d7dd249d383544a1494d9 (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
/**
 * Verifies that a write that sets the on-disk multikey flag does not generate prepare conflicts
 * that would lead to a deadlock during secondary oplog application.
 *
 * This is a regression test for SERVER-41766.
 *
 * @tags: [uses_transactions, uses_prepare_transaction]
 */
(function() {
    "use strict";
    load("jstests/core/txns/libs/prepare_helpers.js");

    const replTest = new ReplSetTest({name: 'multikey_write_avoids_prepare_conflict', nodes: 2});
    replTest.startSet();
    replTest.initiate();

    const dbName = "test";
    const collName = "coll";

    const primary = replTest.getPrimary();
    const secondary = replTest.getSecondary();
    const primaryColl = primary.getDB(dbName)[collName];

    jsTestLog("Creating a collection and an index on the primary, with spec {x:1}.");
    assert.commandWorked(primaryColl.createIndex({x: 1}));
    replTest.awaitReplication();

    const session = primary.startSession();
    const sessionDB = session.getDatabase(dbName);
    const sessionColl = sessionDB.getCollection(collName);

    jsTestLog("Preparing a transaction on primary that should set the multikey flag.");
    session.startTransaction();
    // This write should update the multikey flag in the catalog but we don't want it to generate
    // prepare conflicts. In general, it is always safe to set an index as multikey earlier than is
    // necessary.
    assert.commandWorked(sessionColl.insert({x: [1, 2]}));
    PrepareHelpers.prepareTransaction(session);

    jsTestLog("Switching primaries by stepping up node " + secondary);
    replTest.stepUp(secondary);
    const newPrimary = replTest.getPrimary();
    const newPrimaryColl = newPrimary.getDB(dbName)[collName];

    jsTestLog("Doing an insert on the new primary that should also try to set the multikey flag.");
    assert.commandWorked(newPrimaryColl.insert({x: [3, 4]}));
    replTest.awaitReplication();

    jsTestLog("Aborting the prepared transaction on session " + tojson(session.getSessionId()));
    assert.commandWorked(newPrimary.adminCommand({
        abortTransaction: 1,
        lsid: session.getSessionId(),
        txnNumber: session.getTxnNumber_forTesting(),
        autocommit: false
    }));

    replTest.stopSet();
}());