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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
/**
* Copyright (C) 2015 MongoDB Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* 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 GNU Affero General 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.
*/
#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kSharding
#include "mongo/platform/basic.h"
#include <vector>
#include "mongo/client/remote_command_targeter.h"
#include "mongo/db/client.h"
#include "mongo/db/commands.h"
#include "mongo/db/lasterror.h"
#include "mongo/executor/task_executor_pool.h"
#include "mongo/s/async_requests_sender.h"
#include "mongo/s/client/shard_registry.h"
#include "mongo/s/cluster_last_error_info.h"
#include "mongo/s/grid.h"
#include "mongo/s/write_ops/batch_downconvert.h"
#include "mongo/util/log.h"
namespace mongo {
namespace {
using std::vector;
// Adds a wOpTime and a wElectionId field to a set of gle options
BSONObj buildGLECmdWithOpTime(const BSONObj& gleOptions,
const repl::OpTime& opTime,
const OID& electionId) {
BSONObjBuilder builder;
BSONObjIterator it(gleOptions);
for (int i = 0; it.more(); ++i) {
BSONElement el = it.next();
// Make sure first element is getLastError : 1
if (i == 0) {
StringData elName(el.fieldName());
if (!elName.equalCaseInsensitive("getLastError")) {
builder.append("getLastError", 1);
}
}
builder.append(el);
}
opTime.append(&builder, "wOpTime");
builder.appendOID("wElectionId", const_cast<OID*>(&electionId));
return builder.obj();
}
/**
* Uses GLE and the shard hosts and opTimes last written by write commands to enforce a
* write concern across the previously used shards.
*
* Returns OK with the LegacyWCResponses containing only write concern error information
* Returns !OK if there was an error getting a GLE response
*/
Status enforceLegacyWriteConcern(OperationContext* opCtx,
StringData dbName,
const BSONObj& options,
const HostOpTimeMap& hostOpTimes,
std::vector<LegacyWCResponse>* legacyWCResponses) {
if (hostOpTimes.empty()) {
return Status::OK();
}
// Assemble requests
std::vector<AsyncRequestsSender::Request> requests;
for (HostOpTimeMap::const_iterator it = hostOpTimes.begin(); it != hostOpTimes.end(); ++it) {
const ConnectionString& shardConnStr = it->first;
const auto& hot = it->second;
const repl::OpTime& opTime = hot.opTime;
const OID& electionId = hot.electionId;
auto swShard = Grid::get(opCtx)->shardRegistry()->getShard(opCtx, shardConnStr.toString());
if (!swShard.isOK()) {
return swShard.getStatus();
}
LOG(3) << "enforcing write concern " << options << " on " << shardConnStr.toString()
<< " at opTime " << opTime.getTimestamp().toStringPretty() << " with electionID "
<< electionId;
BSONObj gleCmd = buildGLECmdWithOpTime(options, opTime, electionId);
requests.emplace_back(swShard.getValue()->getId(), gleCmd);
}
// Send the requests.
const ReadPreferenceSetting readPref(ReadPreference::PrimaryOnly, TagSet());
AsyncRequestsSender ars(opCtx,
Grid::get(opCtx)->getExecutorPool()->getArbitraryExecutor(),
dbName.toString(),
requests,
readPref,
Shard::RetryPolicy::kIdempotent);
// Receive the responses.
vector<Status> failedStatuses;
while (!ars.done()) {
// Block until a response is available.
auto response = ars.next();
// Return immediately if we failed to contact a shard.
if (!response.shardHostAndPort) {
invariant(!response.swResponse.isOK());
return response.swResponse.getStatus();
}
// We successfully contacted the shard, but it returned some error.
if (!response.swResponse.isOK()) {
failedStatuses.push_back(std::move(response.swResponse.getStatus()));
continue;
}
BSONObj gleResponse = stripNonWCInfo(response.swResponse.getValue().data);
// Use the downconversion tools to determine if this GLE response is ok, a
// write concern error, or an unknown error we should immediately abort for.
GLEErrors errors;
Status extractStatus = extractGLEErrors(gleResponse, &errors);
if (!extractStatus.isOK()) {
failedStatuses.push_back(extractStatus);
continue;
}
LegacyWCResponse wcResponse;
invariant(response.shardHostAndPort);
wcResponse.shardHost = response.shardHostAndPort->toString();
wcResponse.gleResponse = gleResponse;
if (errors.wcError.get()) {
wcResponse.errToReport = errors.wcError->toString();
}
legacyWCResponses->push_back(wcResponse);
}
if (failedStatuses.empty()) {
return Status::OK();
}
StringBuilder builder;
builder << "could not enforce write concern";
for (vector<Status>::const_iterator it = failedStatuses.begin(); it != failedStatuses.end();
++it) {
const Status& failedStatus = *it;
if (it == failedStatuses.begin()) {
builder << causedBy(failedStatus.toString());
} else {
builder << ":: and ::" << failedStatus.toString();
}
}
if (failedStatuses.size() == 1u) {
return failedStatuses.front();
} else {
return Status(ErrorCodes::MultipleErrorsOccurred, builder.str());
}
}
class GetLastErrorCmd : public BasicCommand {
public:
GetLastErrorCmd() : BasicCommand("getLastError", "getlasterror") {}
virtual bool supportsWriteConcern(const BSONObj& cmd) const override {
return false;
}
virtual bool slaveOk() const {
return true;
}
virtual void help(std::stringstream& help) const {
help << "check for an error on the last command executed";
}
virtual void addRequiredPrivileges(const std::string& dbname,
const BSONObj& cmdObj,
std::vector<Privilege>* out) {
// No auth required for getlasterror
}
bool requiresAuth() const override {
return false;
}
virtual bool run(OperationContext* opCtx,
const std::string& dbname,
const BSONObj& cmdObj,
BSONObjBuilder& result) {
// Mongos GLE - finicky.
//
// To emulate mongod, we first append any write errors we had, then try to append
// write concern error if there was no write error. We need to contact the previous
// shards regardless to maintain 2.4 behavior.
//
// If there are any unexpected or connectivity errors when calling GLE, fail the
// command.
//
// Finally, report the write concern errors IF we don't already have an error.
// If we only get one write concern error back, report that, otherwise report an
// aggregated error.
//
// TODO: Do we need to contact the prev shards regardless - do we care that much
// about 2.4 behavior?
//
LastError* le = &LastError::get(cc());
le->disable();
// Write commands always have the error stored in the mongos last error
bool errorOccurred = false;
if (le->getNPrev() == 1) {
errorOccurred = le->appendSelf(result, false);
}
// For compatibility with 2.4 sharded GLE, we always enforce the write concern
// across all shards.
const HostOpTimeMap hostOpTimes(ClusterLastErrorInfo::get(cc())->getPrevHostOpTimes());
std::vector<LegacyWCResponse> wcResponses;
auto status =
enforceLegacyWriteConcern(opCtx,
dbname,
CommandHelpers::filterCommandRequestForPassthrough(cmdObj),
hostOpTimes,
&wcResponses);
// Don't forget about our last hosts, reset the client info
ClusterLastErrorInfo::get(cc())->disableForCommand();
// We're now done contacting all remote servers, just report results
if (!status.isOK()) {
// Return immediately if we failed to contact a shard, unexpected GLE issue
// Can't return code, since it may have been set above (2.4 compatibility)
result.append("errmsg", status.reason());
return false;
}
// Go through all the write concern responses and find errors
BSONArrayBuilder shards;
BSONObjBuilder shardRawGLE;
BSONArrayBuilder errors;
BSONArrayBuilder errorRawGLE;
int numWCErrors = 0;
const LegacyWCResponse* lastErrResponse = NULL;
for (std::vector<LegacyWCResponse>::const_iterator it = wcResponses.begin();
it != wcResponses.end();
++it) {
const LegacyWCResponse& wcResponse = *it;
shards.append(wcResponse.shardHost);
shardRawGLE.append(wcResponse.shardHost, wcResponse.gleResponse);
if (!wcResponse.errToReport.empty()) {
numWCErrors++;
lastErrResponse = &wcResponse;
errors.append(wcResponse.errToReport);
errorRawGLE.append(wcResponse.gleResponse);
}
}
// Always report what we found to match 2.4 behavior and for debugging
if (wcResponses.size() == 1u) {
result.append("singleShard", wcResponses.front().shardHost);
} else {
result.append("shards", shards.arr());
result.append("shardRawGLE", shardRawGLE.obj());
}
// Suppress write concern errors if a write error occurred, to match mongod behavior
if (errorOccurred || numWCErrors == 0) {
// Still need to return err
if (!errorOccurred) {
result.appendNull("err");
}
return true;
}
if (numWCErrors == 1) {
// Return the single write concern error we found, err should be set or not
// from gle response
CommandHelpers::filterCommandReplyForPassthrough(lastErrResponse->gleResponse, &result);
return lastErrResponse->gleResponse["ok"].trueValue();
} else {
// Return a generic combined WC error message
result.append("errs", errors.arr());
result.append("errObjects", errorRawGLE.arr());
// Need to always return err
result.appendNull("err");
return CommandHelpers::appendCommandStatus(
result,
Status(ErrorCodes::WriteConcernFailed, "multiple write concern errors occurred"));
}
}
} cmdGetLastError;
} // namespace
} // namespace mongo
|