summaryrefslogtreecommitdiff
path: root/jstests/concurrency/fsm_workload_helpers/kill_session.js
blob: 004db48ecacd1d764e5f39e3d09d6cd9d978315e (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
/**
 * kill_session.js
 *
 * State function that kills a random session from config.system.sessions.
 */
function killSession(db, collName) {
    load("jstests/libs/killed_session_util.js");

    print("Starting killSession");
    let ourSessionWasKilled;
    do {
        ourSessionWasKilled = false;

        try {
            let res = db.adminCommand({refreshLogicalSessionCacheNow: 1});
            if (res.ok === 1) {
                assertAlways.commandWorked(res);
            } else if (res.code === 18630 || res.code === 18631) {
                // Refreshing the logical session cache may trigger sharding the sessions
                // collection, which can fail with 18630 or 18631 if its session is killed while
                // running DBClientBase::getCollectionInfos() or DBClientBase::getIndexSpecs(),
                // respectively. This means the collection is not set up, so retry.
                ourSessionWasKilled = true;
                continue;
            } else {
                assertAlways.commandFailedWithCode(
                    res,
                    [ErrorCodes.DuplicateKey, ErrorCodes.WriteConcernFailed],
                    'unexpected error code: ' + res.code + ': ' + res.message);
            }

            const sessionToKill = db.getSiblingDB("config").system.sessions.aggregate([
                {$listSessions: {}},
                {$match: {"_id.id": {$ne: db.getSession().getSessionId().id}}},
                {$sample: {size: 1}},
            ]);

            if (sessionToKill.toArray().length === 0) {
                break;
            }

            const sessionUUID = sessionToKill.toArray()[0]._id.id;
            res = db.runCommand({killSessions: [{id: sessionUUID}]});
            assertAlways.commandWorked(res);
        } catch (e) {
            if (KilledSessionUtil.isKilledSessionCode(e.code)) {
                // This session was killed when running either listSessions or killSesssions.
                // We should retry.
                ourSessionWasKilled = true;
                continue;
            }

            throw e;
        }
    } while (ourSessionWasKilled);
    print("Finished killSession");
}