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 #include "mongo/db/repl/repl_set_request_votes_args.h"
32
33 #include "mongo/bson/util/bson_check.h"
34 #include "mongo/bson/util/bson_extract.h"
35 #include "mongo/db/jsobj.h"
36 #include "mongo/db/repl/bson_extract_optime.h"
37
38 namespace mongo {
39 namespace repl {
40 namespace {
41
42 const std::string kCandidateIndexFieldName = "candidateIndex";
43 const std::string kCommandName = "replSetRequestVotes";
44 const std::string kConfigVersionFieldName = "configVersion";
45 const std::string kDryRunFieldName = "dryRun";
46 // The underlying field name is inaccurate, but changing it requires a fair amount of cross
47 // compatibility work for no real benefit.
48 const std::string kLastDurableOpTimeFieldName = "lastCommittedOp";
49 const std::string kOkFieldName = "ok";
50 const std::string kReasonFieldName = "reason";
51 const std::string kSetNameFieldName = "setName";
52 const std::string kTermFieldName = "term";
53 const std::string kVoteGrantedFieldName = "voteGranted";
54 const std::string kOperationTime = "operationTime";
55
56 const std::string kLegalArgsFieldNames[] = {
57 kCandidateIndexFieldName,
58 kCommandName,
59 kConfigVersionFieldName,
60 kDryRunFieldName,
61 kLastDurableOpTimeFieldName,
62 kSetNameFieldName,
63 kTermFieldName,
64 kOperationTime,
65 };
66
67 } // namespace
68
69
initialize(const BSONObj & argsObj)70 Status ReplSetRequestVotesArgs::initialize(const BSONObj& argsObj) {
71 Status status =
72 bsonCheckOnlyHasFieldsForCommand("ReplSetRequestVotes", argsObj, kLegalArgsFieldNames);
73 if (!status.isOK())
74 return status;
75
76 status = bsonExtractIntegerField(argsObj, kTermFieldName, &_term);
77 if (!status.isOK())
78 return status;
79
80 status = bsonExtractIntegerField(argsObj, kCandidateIndexFieldName, &_candidateIndex);
81 if (!status.isOK())
82 return status;
83
84 status = bsonExtractIntegerField(argsObj, kConfigVersionFieldName, &_cfgver);
85 if (!status.isOK())
86 return status;
87
88 status = bsonExtractStringField(argsObj, kSetNameFieldName, &_setName);
89 if (!status.isOK())
90 return status;
91
92 status = bsonExtractBooleanField(argsObj, kDryRunFieldName, &_dryRun);
93 if (!status.isOK())
94 return status;
95
96 status = bsonExtractOpTimeField(argsObj, kLastDurableOpTimeFieldName, &_lastDurableOpTime);
97 if (!status.isOK())
98 return status;
99
100 return Status::OK();
101 }
102
getSetName() const103 const std::string& ReplSetRequestVotesArgs::getSetName() const {
104 return _setName;
105 }
106
getTerm() const107 long long ReplSetRequestVotesArgs::getTerm() const {
108 return _term;
109 }
110
getCandidateIndex() const111 long long ReplSetRequestVotesArgs::getCandidateIndex() const {
112 return _candidateIndex;
113 }
114
getConfigVersion() const115 long long ReplSetRequestVotesArgs::getConfigVersion() const {
116 return _cfgver;
117 }
118
getLastDurableOpTime() const119 OpTime ReplSetRequestVotesArgs::getLastDurableOpTime() const {
120 return _lastDurableOpTime;
121 }
122
isADryRun() const123 bool ReplSetRequestVotesArgs::isADryRun() const {
124 return _dryRun;
125 }
126
addToBSON(BSONObjBuilder * builder) const127 void ReplSetRequestVotesArgs::addToBSON(BSONObjBuilder* builder) const {
128 builder->append(kCommandName, 1);
129 builder->append(kSetNameFieldName, _setName);
130 builder->append(kDryRunFieldName, _dryRun);
131 builder->append(kTermFieldName, _term);
132 builder->appendIntOrLL(kCandidateIndexFieldName, _candidateIndex);
133 builder->appendIntOrLL(kConfigVersionFieldName, _cfgver);
134 _lastDurableOpTime.append(builder, kLastDurableOpTimeFieldName);
135 }
136
initialize(const BSONObj & argsObj)137 Status ReplSetRequestVotesResponse::initialize(const BSONObj& argsObj) {
138 auto status = bsonExtractIntegerField(argsObj, kTermFieldName, &_term);
139 if (!status.isOK())
140 return status;
141
142 status = bsonExtractBooleanField(argsObj, kVoteGrantedFieldName, &_voteGranted);
143 if (!status.isOK())
144 return status;
145
146 status = bsonExtractStringField(argsObj, kReasonFieldName, &_reason);
147 if (!status.isOK())
148 return status;
149
150 return Status::OK();
151 }
152
setVoteGranted(bool voteGranted)153 void ReplSetRequestVotesResponse::setVoteGranted(bool voteGranted) {
154 _voteGranted = voteGranted;
155 }
156
setTerm(long long term)157 void ReplSetRequestVotesResponse::setTerm(long long term) {
158 _term = term;
159 }
160
setReason(const std::string & reason)161 void ReplSetRequestVotesResponse::setReason(const std::string& reason) {
162 _reason = reason;
163 }
164
getTerm() const165 long long ReplSetRequestVotesResponse::getTerm() const {
166 return _term;
167 }
168
getVoteGranted() const169 bool ReplSetRequestVotesResponse::getVoteGranted() const {
170 return _voteGranted;
171 }
172
getReason() const173 const std::string& ReplSetRequestVotesResponse::getReason() const {
174 return _reason;
175 }
176
addToBSON(BSONObjBuilder * builder) const177 void ReplSetRequestVotesResponse::addToBSON(BSONObjBuilder* builder) const {
178 builder->append(kTermFieldName, _term);
179 builder->append(kVoteGrantedFieldName, _voteGranted);
180 builder->append(kReasonFieldName, _reason);
181 }
182
toBSON() const183 BSONObj ReplSetRequestVotesResponse::toBSON() const {
184 BSONObjBuilder builder;
185 addToBSON(&builder);
186 return builder.obj();
187 }
188
189 } // namespace repl
190 } // namespace mongo
191