1 
2 /**
3  *    Copyright (C) 2018-present MongoDB, Inc.
4  *
5  *    This program is free software: you can redistribute it and/or modify
6  *    it under the terms of the Server Side Public License, version 1,
7  *    as published by MongoDB, Inc.
8  *
9  *    This program is distributed in the hope that it will be useful,
10  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *    Server Side Public License for more details.
13  *
14  *    You should have received a copy of the Server Side Public License
15  *    along with this program. If not, see
16  *    <http://www.mongodb.com/licensing/server-side-public-license>.
17  *
18  *    As a special exception, the copyright holders give permission to link the
19  *    code of portions of this program with the OpenSSL library under certain
20  *    conditions as described in each individual source file and distribute
21  *    linked combinations including the program with the OpenSSL library. You
22  *    must comply with the Server Side Public License in all respects for
23  *    all of the code used other than as permitted herein. If you modify file(s)
24  *    with this exception, you may extend this exception to your version of the
25  *    file(s), but you are not obligated to do so. If you do not wish to do so,
26  *    delete this exception statement from your version. If you delete this
27  *    exception statement from all source files in the program, then also delete
28  *    it in the license file.
29  */
30 
31 #pragma once
32 
33 #include <memory>
34 
35 #include "mongo/client/connection_string.h"
36 #include "mongo/client/connpool.h"
37 #include "mongo/client/remote_command_targeter.h"
38 #include "mongo/db/logical_session_id.h"
39 #include "mongo/db/sessions_collection.h"
40 #include "mongo/util/time_support.h"
41 
42 namespace mongo {
43 
44 class DBDirectClient;
45 class OperationContext;
46 class RemoteCommandTargeter;
47 
48 /**
49  * Accesses the sessions collection for replica set members.
50  */
51 class SessionsCollectionRS : public SessionsCollection {
52 public:
53     /**
54      * Constructs a new SessionsCollectionRS.
55      */
56     SessionsCollectionRS() = default;
57 
58     /**
59      * Ensures that the sessions collection exists and has the proper indexes.
60      */
61     Status setupSessionsCollection(OperationContext* opCtx) override;
62 
63     /**
64      * Checks if the sessions collection exists and has the proper indexes.
65      */
66     Status checkSessionsCollectionExists(OperationContext* opCtx) override;
67 
68     /**
69      * Updates the last-use times on the given sessions to be greater than
70      * or equal to the current time.
71      *
72      * If a step-down happens on this node as this method is running, it may fail.
73      */
74     Status refreshSessions(OperationContext* opCtx,
75                            const LogicalSessionRecordSet& sessions) override;
76 
77     /**
78      * Removes the authoritative records for the specified sessions.
79      *
80      * If a step-down happens on this node as this method is running, it may fail.
81      */
82     Status removeRecords(OperationContext* opCtx, const LogicalSessionIdSet& sessions) override;
83 
84     /**
85      * Returns the subset of sessions from the given set that do not have entries
86      * in the sessions collection.
87      *
88      * If a step-down happens on this node as this method is running, it may
89      * return stale results.
90      */
91     StatusWith<LogicalSessionIdSet> findRemovedSessions(
92         OperationContext* opCtx, const LogicalSessionIdSet& sessions) override;
93 
94     /**
95      * Removes the transaction records for the specified sessions from the
96      * transaction table.
97      *
98      * If a step-down happens on this node as this method is running, it may fail.
99      */
100     Status removeTransactionRecords(OperationContext* opCtx,
101                                     const LogicalSessionIdSet& sessions) override;
102 
103     /**
104      * Helper for a shard server to run its transaction operations as a replica set
105      * member.
106      *
107      * If a step-down happens on this node as this method is running, it may fail.
108      */
109     static Status removeTransactionRecordsHelper(OperationContext* opCtx,
110                                                  const LogicalSessionIdSet& sessions);
111 };
112 
113 }  // namespace mongo
114