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 "mongo/base/disallow_copying.h"
34 #include "mongo/db/repl/optime.h"
35 #include "mongo/s/client/shard.h"
36 #include "mongo/stdx/mutex.h"
37 
38 namespace mongo {
39 
40 /**
41  * Implements the support for "read your own write" when run on a node of a replica set. Can be used
42  * in the scenarios where causal consistency is not available yet.
43  */
44 class RSLocalClient {
45     MONGO_DISALLOW_COPYING(RSLocalClient);
46 
47 public:
48     explicit RSLocalClient() = default;
49 
50     ~RSLocalClient() = default;
51 
52     /**
53      * Runs the specified command returns the BSON command response plus parsed out Status of this
54      * response and write concern error (if present).
55      */
56     StatusWith<Shard::CommandResponse> runCommandOnce(OperationContext* opCtx,
57                                                       const std::string& dbName,
58                                                       const BSONObj& cmdObj);
59 
60     /**
61      * Warning: This method exhausts the cursor and pulls all data into memory.
62      * Do not use other than for very small (i.e., admin or metadata) collections.
63      */
64     StatusWith<Shard::QueryResponse> queryOnce(OperationContext* opCtx,
65                                                const ReadPreferenceSetting& readPref,
66                                                const repl::ReadConcernLevel& readConcernLevel,
67                                                const NamespaceString& nss,
68                                                const BSONObj& query,
69                                                const BSONObj& sort,
70                                                boost::optional<long long> limit);
71 
72 private:
73     /**
74      * Checks if an OpTime was set on the current Client (ie if the current operation performed a
75      * write) and if so updates _lastOpTime to the OpTime from the write that was just performed.
76      * The 'previousOpTimeOnClient' parameter is the optime that *was* the optime on this client
77      * before we ran this command through this RSLocalClient. By the time this method is called,
78      * if the optime set on the Client is different than 'previousOpTimeOnClient' then that means
79      * the command just run did a write and we should update _lastOpTime to capture the optime of
80      * the write we just did.  If the current optime on the client is the same as
81      * 'previousOpTimeOnClient' then the command we just ran didn't do a write, and we should leave
82      * _lastOpTime alone.
83      */
84     void _updateLastOpTimeFromClient(OperationContext* opCtx,
85                                      const repl::OpTime& previousOpTimeOnClient);
86 
87     repl::OpTime _getLastOpTime();
88 
89     // Guards _lastOpTime below.
90     stdx::mutex _mutex;
91 
92     // Stores the optime that was generated by the last operation to perform a write that was run
93     // through _runCommand.  Used in _exhaustiveFindOnConfig for waiting for that optime to be
94     // committed so that readConcern majority reads will read the writes that were performed without
95     // a w:majority write concern.
96     repl::OpTime _lastOpTime{};
97 };
98 
99 }  // namespace mongo
100