summaryrefslogtreecommitdiff
path: root/jstests/replsets/internal_sessions_reaping_basic.js
blob: 18a15a6ae1b5fe9cac0a969a30d7a1717aa2cd74 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
 * Tests that the logical session cache reaper would only reap the config.transactions and
 * config.image_collection entries for a transaction session if the logical session that it
 * corresponds to has expired and been removed from the config.system.sessions collection.
 *
 * @tags: [requires_fcv_60, uses_transactions]
 */

(function() {
"use strict";

load("jstests/sharding/libs/sharded_transactions_helpers.js");

// This test runs the reapLogicalSessionCacheNow command. That can lead to direct writes to the
// config.transactions collection, which cannot be performed on a session.
TestData.disableImplicitSessions = true;

const rst = new ReplSetTest({
    nodes: 2,
    nodeOptions: {
        setParameter: {
            maxSessions: 1,
            // Force batch size 1 on secondaries.
            replBatchLimitOperations: 1,
            // Make transaction records expire immediately.
            TransactionRecordMinimumLifetimeMinutes: 0,
            storeFindAndModifyImagesInSideCollection: true,
            internalSessionsReapThreshold: 0
        }
    }
});
rst.startSet();
rst.initiate();

const primary = rst.getPrimary();

const kConfigSessionsNs = "config.system.sessions";
const kConfigTxnsNs = "config.transactions";
const kImageCollNs = "config.image_collection";
const kOplogCollNs = "local.oplog.rs";
const sessionsColl = primary.getCollection(kConfigSessionsNs);
const transactionsColl = primary.getCollection(kConfigTxnsNs);
const imageColl = primary.getCollection(kImageCollNs);
const oplogColl = primary.getCollection(kOplogCollNs);

const dbName = "testDb";
const collName = "testColl";
const testDB = primary.getDB(dbName);
const testColl = testDB.getCollection(collName);

assert.commandWorked(testDB.runCommand({
    insert: collName,
    documents: [{_id: 0}, {_id: 1}],
}));

const sessionUUID = UUID();
const parentLsid = {
    id: sessionUUID
};
const parentLsidFilter = makeLsidFilter(parentLsid, "_id");
let parentTxnNumber = 0;
const childTxnNumber = NumberLong(0);

let numTransactionsCollEntriesReaped = 0;

{
    jsTest.log("Test reaping when there is an expired internal transaction session for a " +
               "non-retryable write without an open transaction");

    parentTxnNumber++;
    assert.commandWorked(testDB.runCommand({
        findAndModify: collName,
        query: {_id: 0},
        update: {$set: {x: 0}},
        lsid: parentLsid,
        txnNumber: NumberLong(parentTxnNumber),
    }));
    assert.commandWorked(primary.adminCommand({refreshLogicalSessionCacheNow: 1}));
    assert.eq(1, sessionsColl.find({"_id.id": sessionUUID}).itcount());
    assert.eq(1, transactionsColl.find(parentLsidFilter).itcount());
    assert.eq(1, imageColl.find(parentLsidFilter).itcount());

    const childLsid = {id: sessionUUID, txnUUID: UUID()};
    const childLsidFilter = makeLsidFilter(childLsid, "_id");
    assert.commandWorked(testDB.runCommand({
        findAndModify: collName,
        query: {_id: 0},
        update: {$set: {y: 0}},
        lsid: childLsid,
        txnNumber: childTxnNumber,
        startTransaction: true,
        autocommit: false
    }));
    assert.commandWorked(
        testDB.adminCommand(makeCommitTransactionCmdObj(childLsid, childTxnNumber)));

    assert.eq({_id: 0, x: 0, y: 0}, testColl.findOne({_id: 0}));

    // Verify that the config.transactions entry for the internal transaction session does not get
    // reaped automatically when the transaction committed.
    assert.eq(1, sessionsColl.find({"_id.id": sessionUUID}).itcount());
    assert.eq(1, transactionsColl.find(parentLsidFilter).itcount());
    assert.eq(1, transactionsColl.find(childLsidFilter).itcount());
    assert.eq(1, imageColl.find(parentLsidFilter).itcount());
    assert.eq(0, imageColl.find(childLsidFilter).itcount());

    // Force the logical session cache to reap, and verify that the config.transactions (and
    // config.image_collection) entries for both transaction sessions do not get reaped because the
    // config.system.sessions entry still has not been deleted.
    assert.commandWorked(primary.adminCommand({reapLogicalSessionCacheNow: 1}));
    assert.eq(1, sessionsColl.find({"_id.id": sessionUUID}).itcount());
    assert.eq(1, transactionsColl.find(parentLsidFilter).itcount());
    assert.eq(1, transactionsColl.find(childLsidFilter).itcount());
    assert.eq(1, imageColl.find(parentLsidFilter).itcount());
    assert.eq(0, imageColl.find(childLsidFilter).itcount());

    // Delete the config.system.sessions entry, force the logical session cache to reap again, and
    // verify that the config.transactions (and config.image_collection) entries for both sessions
    // do get reaped this time.
    assert.commandWorked(sessionsColl.remove({}));
    assert.commandWorked(primary.adminCommand({reapLogicalSessionCacheNow: 1}));
    assert.eq(0, sessionsColl.find({"_id.id": sessionUUID}).itcount());
    assert.eq(0, transactionsColl.find(parentLsidFilter).itcount());
    assert.eq(0, transactionsColl.find(childLsidFilter).itcount());
    assert.eq(0, imageColl.find(parentLsidFilter).itcount());
    assert.eq(0, imageColl.find(childLsidFilter).itcount());
    numTransactionsCollEntriesReaped += 2;
}

{
    jsTest.log("Test reaping when there is an expired internal transaction session for a " +
               "previous retryable write (i.e. with an old txnNumber)");

    parentTxnNumber++;
    assert.commandWorked(testDB.runCommand({
        findAndModify: collName,
        query: {_id: 1},
        update: {$set: {x: 1}},
        lsid: parentLsid,
        txnNumber: NumberLong(parentTxnNumber),
    }));
    assert.commandWorked(primary.adminCommand({refreshLogicalSessionCacheNow: 1}));
    assert.eq(1, sessionsColl.find({"_id.id": sessionUUID}).itcount());
    assert.eq(1, transactionsColl.find(parentLsidFilter).itcount());
    assert.eq(1, imageColl.find(parentLsidFilter).itcount());

    parentTxnNumber++;
    const childLsid = {id: sessionUUID, txnNumber: NumberLong(parentTxnNumber), txnUUID: UUID()};
    const childLsidFilter = makeLsidFilter(childLsid, "_id");
    assert.commandWorked(testDB.runCommand({
        findAndModify: collName,
        query: {_id: 1},
        update: {$set: {y: 1}},
        lsid: childLsid,
        txnNumber: childTxnNumber,
        startTransaction: true,
        autocommit: false
    }));
    assert.commandWorked(
        testDB.adminCommand(makeCommitTransactionCmdObj(childLsid, childTxnNumber)));

    assert.eq({_id: 1, x: 1, y: 1}, testColl.findOne({_id: 1}));

    parentTxnNumber++;
    assert.commandWorked(testDB.runCommand({
        findAndModify: collName,
        query: {_id: 1},
        update: {$set: {y: 1}},
        lsid: parentLsid,
        txnNumber: NumberLong(parentTxnNumber),
        startTransaction: true,
        autocommit: false
    }));

    // Verify that the config.transactions and config.image_collection entries for the internal
    // transaction session do not get reaped automatically when the new txnNumber started since
    // eager reaping is not enabled.
    assert.eq(1, sessionsColl.find({"_id.id": sessionUUID}).itcount());
    assert.eq(1, transactionsColl.find(parentLsidFilter).itcount());
    assert.eq(1, transactionsColl.find(childLsidFilter).itcount());
    assert.eq(1, imageColl.find(parentLsidFilter).itcount());
    assert.eq(1, imageColl.find(childLsidFilter).itcount());

    // Force the logical session cache to reap, and verify that the config.transactions and
    // config.image_collection entries for both transaction sessions do not get reaped because the
    // config.system.sessions entry still has not been deleted.
    assert.commandWorked(primary.adminCommand({reapLogicalSessionCacheNow: 1}));
    assert.eq(1, sessionsColl.find({"_id.id": sessionUUID}).itcount());
    assert.eq(1, transactionsColl.find(parentLsidFilter).itcount());
    assert.eq(1, transactionsColl.find(childLsidFilter).itcount());
    assert.eq(1, imageColl.find(parentLsidFilter).itcount());
    assert.eq(1, imageColl.find(childLsidFilter).itcount());

    assert.commandWorked(
        testDB.adminCommand(makeCommitTransactionCmdObj(parentLsid, parentTxnNumber)));
    assert.eq({_id: 1, x: 1, y: 1}, testColl.findOne({_id: 1}));
    assert.eq(1, sessionsColl.find({"_id.id": sessionUUID}).itcount());
    assert.eq(1, transactionsColl.find(parentLsidFilter).itcount());
    assert.eq(1, transactionsColl.find(childLsidFilter).itcount());
    assert.eq(1, imageColl.find(parentLsidFilter).itcount());
    assert.eq(1, imageColl.find(childLsidFilter).itcount());

    // Force the logical session cache to reap, and verify that the config.transactions and
    // config.image_collection entries for both transaction sessions do not get reaped because the
    // config.system.sessions entry still has not been deleted.
    assert.commandWorked(primary.adminCommand({reapLogicalSessionCacheNow: 1}));
    assert.eq(1, sessionsColl.find({"_id.id": sessionUUID}).itcount());
    assert.eq(1, transactionsColl.find(parentLsidFilter).itcount());
    assert.eq(1, transactionsColl.find(childLsidFilter).itcount());
    assert.eq(1, imageColl.find(parentLsidFilter).itcount());
    assert.eq(1, imageColl.find(childLsidFilter).itcount());

    // Delete the config.system.sessions entry, force the logical session cache to reap again, and
    // verify that the config.transactions and config.image_collection entries for both transaction
    // sessions do get reaped this time.
    assert.commandWorked(sessionsColl.remove({}));
    assert.commandWorked(primary.adminCommand({reapLogicalSessionCacheNow: 1}));
    assert.eq(0, sessionsColl.find({"_id.id": sessionUUID}).itcount());
    assert.eq(0, transactionsColl.find(parentLsidFilter).itcount());
    assert.eq(0, transactionsColl.find(childLsidFilter).itcount());
    assert.eq(0, imageColl.find(parentLsidFilter).itcount());
    assert.eq(0, imageColl.find(childLsidFilter).itcount());
    numTransactionsCollEntriesReaped += 2;
}

// Validate that writes to config.transactions do not generate oplog entries, with the exception of
// deletions.
assert.eq(numTransactionsCollEntriesReaped, oplogColl.find({op: 'd', ns: kConfigTxnsNs}).itcount());
assert.eq(0, oplogColl.find({op: {'$ne': 'd'}, ns: kConfigTxnsNs}).itcount());

rst.stopSet();
})();