summaryrefslogtreecommitdiff
path: root/jstests/libs/override_methods/causally_consistent_index_builds.js
blob: cacd1312f8020d974917f6865f95de47a9f76079 (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
/**
 * Overrides runCommand so that background index builds are causally consistent.
 * TODO: SERVER-38961 This override is not necessary when two-phase index builds are complete.
 */
(function() {
"use strict";

load("jstests/libs/override_methods/override_helpers.js");

// This override runs a collMod after a createIndexes command. After collMod completes
// we can guarantee the background index build started earlier has also completed. We update the
// command response operationTime and $clusterTime so causally consistent reads only read from
// that point onwards.
function runCommandWithCollMod(conn, dbName, commandName, commandObj, func, makeFuncArgs) {
    if (typeof commandObj !== "object" || commandObj === null) {
        return func.apply(conn, makeFuncArgs(commandObj));
    }

    let res = func.apply(conn, makeFuncArgs(commandObj));
    if (commandName !== "createIndexes") {
        return res;
    }
    if (!res.ok) {
        return res;
    }

    let collModCmd = {collMod: commandObj[commandName]};
    let collModRes = func.apply(conn, makeFuncArgs(collModCmd));

    // If a follow-up collMod fails, another command was likely able to execute after the
    // createIndexes command. That means it is safe to use the latest operationTime for
    // causal consistency purposes.
    if (!collModRes.ok) {
        print('note: ignoring collMod failure after sending createIndex command: ' +
              tojson(collModRes));
    }

    // Overwrite the createIndex command's operation and cluster times, so that the owning
    // session can perform causal reads.
    if (collModRes.hasOwnProperty("operationTime")) {
        res.operationTime = collModRes["operationTime"];
    }
    if (collModRes.hasOwnProperty("$clusterTime")) {
        res.$clusterTime = collModRes["$clusterTime"];
    }
    return res;
}

OverrideHelpers.overrideRunCommand(runCommandWithCollMod);
})();