summaryrefslogtreecommitdiff
path: root/src/mongo/db/internal_session_pool_test.cpp
blob: 983fbb7e429dbf535a7d44f7215aaaacb62c4b11 (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/**
 *    Copyright (C) 2021-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#include "mongo/platform/basic.h"

#include "mongo/db/internal_session_pool.h"
#include "mongo/db/logical_session_cache.h"
#include "mongo/db/logical_session_cache_impl.h"
#include "mongo/db/logical_session_id_helpers.h"
#include "mongo/db/service_context_test_fixture.h"
#include "mongo/db/service_liaison_mock.h"
#include "mongo/db/sessions_collection_mock.h"
#include "mongo/idl/server_parameter_test_util.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/clock_source_mock.h"

namespace mongo {
namespace {

class InternalSessionPoolTest : public ServiceContextTest {
public:
    void setUp() override {
        ServiceContextTest::setUp();
        serverGlobalParams.clusterRole = ClusterRole::ShardServer;
        _pool = InternalSessionPool::get(getServiceContext());
        _opCtx = makeOperationContext();
    }

    OperationContext* opCtx() const {
        return _opCtx.get();
    }

protected:
    InternalSessionPool* _pool;
    RAIIServerParameterControllerForTest _featureFlagInternalTransactions{
        "featureFlagInternalTransactions", true};

private:
    ServiceContext::UniqueOperationContext _opCtx;
};

TEST_F(InternalSessionPoolTest, AcquireStandaloneSessionFromEmptyPool) {
    auto session = _pool->acquireStandaloneSession(opCtx());
    ASSERT_EQ(TxnNumber(0), session.getTxnNumber());
}

TEST_F(InternalSessionPoolTest, AcquireWithParentSessionFromEmptyPool) {
    auto parentLsid = makeLogicalSessionIdForTest();
    auto session = _pool->acquireChildSession(opCtx(), parentLsid);

    ASSERT_EQ(parentLsid, *getParentSessionId(session.getSessionId()));
    ASSERT_EQ(TxnNumber(0), session.getTxnNumber());
}

TEST_F(InternalSessionPoolTest, AcquireStandaloneSessionFromPool) {
    auto expectedLsid = makeLogicalSessionIdForTest();
    auto sessionToRelease = InternalSessionPool::Session(expectedLsid, TxnNumber(0));
    _pool->release(sessionToRelease);

    auto session = _pool->acquireStandaloneSession(opCtx());

    ASSERT_EQ(expectedLsid, session.getSessionId());
    // txnNumber should be 1 larger than the released session.
    ASSERT_EQ(TxnNumber(1), session.getTxnNumber());
}

TEST_F(InternalSessionPoolTest, AcquireWithParentSessionFromPoolWithoutParentEntry) {
    LogicalSessionId parentLsid1 = makeLogicalSessionIdForTest();
    LogicalSessionId parentLsid2 = makeLogicalSessionIdForTest();

    auto parentSession1 = InternalSessionPool::Session(parentLsid1, TxnNumber(1));
    _pool->release(parentSession1);

    auto session = _pool->acquireChildSession(opCtx(), parentLsid2);

    ASSERT_NOT_EQUALS(parentLsid1, session.getSessionId());
    ASSERT_EQ(TxnNumber(0), session.getTxnNumber());
}

TEST_F(InternalSessionPoolTest, AcquireChildForSameParentWithoutIntermediateRelease) {
    LogicalSessionId parentLsid = makeLogicalSessionIdForTest();

    auto childLsid1 = makeLogicalSessionIdWithTxnUUID(parentLsid);
    _pool->release(InternalSessionPool::Session(childLsid1, TxnNumber(0)));

    auto childSession1 = _pool->acquireChildSession(opCtx(), parentLsid);

    ASSERT_NOT_EQUALS(parentLsid, childSession1.getSessionId());
    ASSERT_EQ(childLsid1, childSession1.getSessionId());
    ASSERT_EQ(TxnNumber(1), childSession1.getTxnNumber());

    // If we acquire a child for the same parent without first releasing the checked out child, it
    // shouldn't block or prevent future child session reuse.

    auto childSession2 = _pool->acquireChildSession(opCtx(), parentLsid);
    ASSERT_NOT_EQUALS(parentLsid, childSession2.getSessionId());
    ASSERT_NOT_EQUALS(childLsid1, childSession2.getSessionId());
    ASSERT_EQ(TxnNumber(0), childSession2.getTxnNumber());

    _pool->release(childSession2);

    auto childSession3 = _pool->acquireChildSession(opCtx(), parentLsid);
    ASSERT_EQ(childSession2.getSessionId(), childSession3.getSessionId());
    ASSERT_EQ(TxnNumber(1), childSession3.getTxnNumber());

    // Releasing the first child session back into the pool should overwrite the previous session
    // and still allow for reuse.

    _pool->release(childSession1);

    auto childSession4 = _pool->acquireChildSession(opCtx(), parentLsid);
    ASSERT_EQ(childSession1.getSessionId(), childSession4.getSessionId());
    ASSERT_EQ(TxnNumber(2), childSession4.getTxnNumber());
}

TEST_F(InternalSessionPoolTest, AcquireWithParentSessionFromPoolWithParentEntry) {
    LogicalSessionId parentLsid1 = makeLogicalSessionIdForTest();
    LogicalSessionId parentLsid2 = makeLogicalSessionIdForTest();

    // Create child sessions for each parent session and release them in the pool.
    auto childLsid1 = makeLogicalSessionIdWithTxnUUID(parentLsid1);
    auto childLsid2 = makeLogicalSessionIdWithTxnUUID(parentLsid2);

    _pool->release(InternalSessionPool::Session(childLsid1, TxnNumber(1)));
    _pool->release(InternalSessionPool::Session(childLsid2, TxnNumber(2)));

    auto childSession2 = _pool->acquireChildSession(opCtx(), parentLsid2);

    ASSERT_NOT_EQUALS(parentLsid1, childSession2.getSessionId());
    ASSERT_NOT_EQUALS(childLsid1, childSession2.getSessionId());
    ASSERT_NOT_EQUALS(parentLsid2, childSession2.getSessionId());
    ASSERT_EQ(childLsid2, childSession2.getSessionId());

    // txnNumber should be 1 larger than the released parent session.
    ASSERT_EQ(TxnNumber(3), childSession2.getTxnNumber());
}

TEST_F(InternalSessionPoolTest, ReuseUnexpiredStandaloneSessionForSystemUser) {
    auto expectedLsid = makeSystemLogicalSessionId();
    auto sessionToRelease = InternalSessionPool::Session(expectedLsid, TxnNumber(0));
    _pool->release(sessionToRelease);

    auto session = _pool->acquireSystemSession();
    ASSERT_EQ(expectedLsid, session.getSessionId());

    // txnNumber should be 1 larger than the released session.
    ASSERT_EQ(TxnNumber(1), session.getTxnNumber());
}

TEST_F(InternalSessionPoolTest, ReuseUnexpiredStandaloneSession) {
    auto session = _pool->acquireStandaloneSession(opCtx());
    auto expectedLsid = session.getSessionId();
    _pool->release(session);

    auto acquiredSession = _pool->acquireStandaloneSession(opCtx());
    ASSERT_EQ(expectedLsid, session.getSessionId());

    // txnNumber should be 1 larger than the released session.
    ASSERT_EQ(TxnNumber(1), acquiredSession.getTxnNumber());
}

TEST_F(InternalSessionPoolTest, StandaloneSessionAcquiredInReverseOrder) {
    auto expectedLsid1 = makeLogicalSessionId(opCtx());
    auto expectedLsid2 = makeLogicalSessionId(opCtx());
    auto sessionToRelease1 = InternalSessionPool::Session(expectedLsid1, TxnNumber(0));
    auto sessionToRelease2 = InternalSessionPool::Session(expectedLsid2, TxnNumber(0));
    _pool->release(sessionToRelease1);
    _pool->release(sessionToRelease2);

    auto session = _pool->acquireStandaloneSession(opCtx());
    ASSERT_EQ(expectedLsid2, session.getSessionId());
    // txnNumber should be 1 larger than the released session.
    ASSERT_EQ(TxnNumber(1), session.getTxnNumber());

    auto nextSession = _pool->acquireStandaloneSession(opCtx());
    ASSERT_EQ(expectedLsid1, nextSession.getSessionId());
    // txnNumber should be 1 larger than the released session.
    ASSERT_EQ(TxnNumber(1), nextSession.getTxnNumber());
}

TEST_F(InternalSessionPoolTest, UserCannotAcquireSystemStandaloneSession) {
    auto lsid = makeLogicalSessionId(opCtx());
    auto sessionToRelease = InternalSessionPool::Session(lsid, TxnNumber(0));
    _pool->release(sessionToRelease);

    auto systemLsid = makeSystemLogicalSessionId();
    _pool->release(InternalSessionPool::Session(systemLsid, TxnNumber(0)));

    auto session = _pool->acquireStandaloneSession(opCtx());
    ASSERT_NE(systemLsid, session.getSessionId());
    ASSERT_EQ(lsid, session.getSessionId());
    // txnNumber should be 1 larger than the released session.
    ASSERT_EQ(TxnNumber(1), session.getTxnNumber());
    _pool->release(session);

    auto nextSession = _pool->acquireSystemSession();
    ASSERT_NE(lsid, nextSession.getSessionId());
    ASSERT_EQ(systemLsid, nextSession.getSessionId());
    // txnNumber should be 1 larger than the released session.
    ASSERT_EQ(TxnNumber(1), nextSession.getTxnNumber());
}

TEST_F(InternalSessionPoolTest, StaleStandaloneSessionIsNotAcquired) {
    auto service = getServiceContext();
    const std::shared_ptr<ClockSourceMock> mockClock = std::make_shared<ClockSourceMock>();
    service->setFastClockSource(std::make_unique<SharedClockSourceAdapter>(mockClock));

    auto expectedLsid = makeSystemLogicalSessionId();
    auto sessionToRelease = InternalSessionPool::Session(expectedLsid, TxnNumber(0));
    _pool->release(sessionToRelease);
    mockClock->advance(Minutes{localLogicalSessionTimeoutMinutes});

    auto acquiredSession = _pool->acquireSystemSession();
    ASSERT_NE(expectedLsid, acquiredSession.getSessionId());
    // Should be a new session.
    ASSERT_EQ(TxnNumber(0), acquiredSession.getTxnNumber());
}

TEST_F(InternalSessionPoolTest, ExpiredSessionReapedDuringRelease) {
    auto service = getServiceContext();
    const std::shared_ptr<ClockSourceMock> mockClock = std::make_shared<ClockSourceMock>();
    service->setFastClockSource(std::make_unique<SharedClockSourceAdapter>(mockClock));

    auto lsid0 = makeSystemLogicalSessionId();
    auto session0 = InternalSessionPool::Session(lsid0, TxnNumber(0));
    _pool->release(session0);

    auto lsid1 = makeSystemLogicalSessionId();
    auto session1 = InternalSessionPool::Session(lsid1, TxnNumber(0));
    mockClock->advance(Minutes{localLogicalSessionTimeoutMinutes});
    _pool->release(session1);

    // Both system sessions have the same userDigest, but the session with lsid0 should've been
    // removed during release.
    ASSERT_EQ(1, _pool->numSessionsForUser_forTest(lsid1.getUid()));
}

TEST_F(InternalSessionPoolTest, UserExpiredSessionsReapedWhenAnotherUserReleasesSession) {
    auto service = getServiceContext();
    const std::shared_ptr<ClockSourceMock> mockClock = std::make_shared<ClockSourceMock>();
    service->setFastClockSource(std::make_unique<SharedClockSourceAdapter>(mockClock));

    auto lsid0 = makeLogicalSessionId(opCtx());
    auto sessionToRelease = InternalSessionPool::Session(lsid0, TxnNumber(0));
    _pool->release(sessionToRelease);

    auto lsid1 = makeSystemLogicalSessionId();
    auto session1 = InternalSessionPool::Session(lsid1, TxnNumber(0));
    mockClock->advance(Minutes{localLogicalSessionTimeoutMinutes});
    _pool->release(session1);

    // The session with lsid0 should've been removed from the session pool when lsid1 was released.
    ASSERT_EQ(0, _pool->numSessionsForUser_forTest(lsid0.getUid()));
    ASSERT_EQ(1, _pool->numSessionsForUser_forTest(lsid1.getUid()));
}

}  // namespace
}  // namespace mongo