diff options
author | Mathias Stearn <mathias@10gen.com> | 2017-07-10 16:52:49 -0400 |
---|---|---|
committer | Mathias Stearn <mathias@10gen.com> | 2017-07-26 15:13:34 -0400 |
commit | c731446ad3cc399d33b5b2cb28e5b776580f1beb (patch) | |
tree | 13be492fa9b16ed3d9e296d4d42e9fa2598cfad4 | |
parent | 191931c4390ab1ede373e6772aa4d3999518fdaf (diff) | |
download | mongo-c731446ad3cc399d33b5b2cb28e5b776580f1beb.tar.gz |
SERVER-28509 Make DBClient use write commands
Everything that needs to actually use legacy write ops now does so explicitly.
-rw-r--r-- | jstests/gle/core/error3.js | 5 | ||||
-rw-r--r-- | src/mongo/client/dbclient.cpp | 46 | ||||
-rw-r--r-- | src/mongo/db/pipeline/document_source_out.cpp | 4 | ||||
-rw-r--r-- | src/mongo/db/service_entry_point_mongod.cpp | 1 | ||||
-rw-r--r-- | src/mongo/scripting/mozjs/mongo.cpp | 17 | ||||
-rw-r--r-- | src/mongo/shell/bench.cpp | 23 |
6 files changed, 64 insertions, 32 deletions
diff --git a/jstests/gle/core/error3.js b/jstests/gle/core/error3.js deleted file mode 100644 index 7067f68f8a8..00000000000 --- a/jstests/gle/core/error3.js +++ /dev/null @@ -1,5 +0,0 @@ - -db.runCommand("forceerror"); -assert.eq("forced error", db.getLastError()); -db.runCommand("switchtoclienterrors"); -assert.isnull(db.getLastError()); diff --git a/src/mongo/client/dbclient.cpp b/src/mongo/client/dbclient.cpp index 95b66f88c2d..5f3c4760094 100644 --- a/src/mongo/client/dbclient.cpp +++ b/src/mongo/client/dbclient.cpp @@ -1057,33 +1057,49 @@ unsigned long long DBClientConnection::query(stdx::function<void(DBClientCursorB } void DBClientBase::insert(const string& ns, BSONObj obj, int flags) { - auto msg = makeInsertMessage(ns, obj, flags); - say(msg); + insert(ns, std::vector<BSONObj>{obj}, flags); } -// TODO: Merge with other insert implementation? void DBClientBase::insert(const string& ns, const vector<BSONObj>& v, int flags) { - auto msg = makeInsertMessage(ns, v.data(), v.size(), flags); - say(msg); + bool ordered = !(flags & InsertOption_ContinueOnError); + auto nss = NamespaceString(ns); + auto request = + OpMsgRequest::fromDBAndBody(nss.db(), BSON("insert" << nss.coll() << "ordered" << ordered)); + request.sequences.push_back({"documents", v}); + + // Ignoring reply to match fire-and-forget OP_INSERT behavior. + runCommand(std::move(request)); } void DBClientBase::remove(const string& ns, Query obj, int flags) { - auto msg = makeRemoveMessage(ns, obj.obj, flags); - say(msg); + int limit = (flags & RemoveOption_JustOne) ? 1 : 0; + auto nss = NamespaceString(ns); + + auto request = OpMsgRequest::fromDBAndBody(nss.db(), BSON("delete" << nss.coll())); + request.sequences.push_back({"deletes", {BSON("q" << obj.obj << "limit" << limit)}}); + + // Ignoring reply to match fire-and-forget OP_REMOVE behavior. + runCommand(std::move(request)); } void DBClientBase::update(const string& ns, Query query, BSONObj obj, bool upsert, bool multi) { - int flags = 0; - if (upsert) - flags |= UpdateOption_Upsert; - if (multi) - flags |= UpdateOption_Multi; - update(ns, query, obj, flags); + auto nss = NamespaceString(ns); + + auto request = OpMsgRequest::fromDBAndBody(nss.db(), BSON("update" << nss.coll())); + request.sequences.push_back( + {"updates", + {BSON("q" << query.obj << "u" << obj << "upsert" << upsert << "multi" << multi)}}); + + // Ignoring reply to match fire-and-forget OP_UPDATE behavior. + runCommand(std::move(request)); } void DBClientBase::update(const string& ns, Query query, BSONObj obj, int flags) { - auto msg = makeUpdateMessage(ns, query.obj, obj, flags); - say(msg); + update(ns, + std::move(query), + std::move(obj), + flags & UpdateOption_Upsert, + flags & UpdateOption_Multi); } void DBClientBase::killCursor(long long cursorId) { diff --git a/src/mongo/db/pipeline/document_source_out.cpp b/src/mongo/db/pipeline/document_source_out.cpp index 5eb3d8b4b51..3ac28ba4f62 100644 --- a/src/mongo/db/pipeline/document_source_out.cpp +++ b/src/mongo/db/pipeline/document_source_out.cpp @@ -30,6 +30,7 @@ #include "mongo/db/pipeline/document_source_out.h" +#include "mongo/db/ops/write_ops.h" #include "mongo/stdx/memory.h" #include "mongo/util/destructor_guard.h" @@ -169,7 +170,8 @@ DocumentSource::GetNextResult DocumentSourceOut::getNext() { BSONObj toInsert = nextInput.releaseDocument().toBson(); bufferedBytes += toInsert.objsize(); - if (!bufferedObjects.empty() && bufferedBytes > BSONObjMaxUserSize) { + if (!bufferedObjects.empty() && (bufferedBytes > BSONObjMaxUserSize || + bufferedObjects.size() >= write_ops::kMaxWriteBatchSize)) { spill(bufferedObjects); bufferedObjects.clear(); bufferedBytes = toInsert.objsize(); diff --git a/src/mongo/db/service_entry_point_mongod.cpp b/src/mongo/db/service_entry_point_mongod.cpp index eb2561ed73f..c575d5a05be 100644 --- a/src/mongo/db/service_entry_point_mongod.cpp +++ b/src/mongo/db/service_entry_point_mongod.cpp @@ -153,6 +153,7 @@ void generateLegacyQueryErrorResponse(const AssertionException* exception, } void registerError(OperationContext* opCtx, const DBException& exception) { + LastError::get(opCtx->getClient()).setLastError(exception.getCode(), exception.getInfo().msg); CurOp::get(opCtx)->debug().exceptionInfo = exception.getInfo(); } diff --git a/src/mongo/scripting/mozjs/mongo.cpp b/src/mongo/scripting/mozjs/mongo.cpp index 1c22e4e5e27..551c56f44a3 100644 --- a/src/mongo/scripting/mozjs/mongo.cpp +++ b/src/mongo/scripting/mozjs/mongo.cpp @@ -337,6 +337,7 @@ void MongoBase::Functions::insert::call(JSContext* cx, JS::CallArgs args) { return ValueWriter(cx, value).toBSON(); }; + Message toSend; if (args.get(1).isObject()) { bool isArray; @@ -366,14 +367,17 @@ void MongoBase::Functions::insert::call(JSContext* cx, JS::CallArgs args) { if (!foundElement) uasserted(ErrorCodes::BadValue, "attempted to insert an empty array"); - conn->insert(ns, bos, flags); + toSend = makeInsertMessage(ns, bos.data(), bos.size(), flags); } else { - conn->insert(ns, addId(args.get(1))); + toSend = makeInsertMessage(ns, addId(args.get(1))); } } else { - conn->insert(ns, addId(args.get(1))); + toSend = makeInsertMessage(ns, addId(args.get(1))); } + invariant(!toSend.empty()); + conn->say(toSend); + args.rval().setUndefined(); } @@ -399,7 +403,8 @@ void MongoBase::Functions::remove::call(JSContext* cx, JS::CallArgs args) { justOne = args.get(2).toBoolean(); } - conn->remove(ns, bson, justOne); + auto toSend = makeRemoveMessage(ns, bson, justOne ? RemoveOption_JustOne : 0); + conn->say(toSend); args.rval().setUndefined(); } @@ -427,7 +432,9 @@ void MongoBase::Functions::update::call(JSContext* cx, JS::CallArgs args) { bool upsert = args.length() > 3 && args.get(3).isBoolean() && args.get(3).toBoolean(); bool multi = args.length() > 4 && args.get(4).isBoolean() && args.get(4).toBoolean(); - conn->update(ns, q1, o1, upsert, multi); + auto toSend = makeUpdateMessage( + ns, q1, o1, (upsert ? UpdateOption_Upsert : 0) | (multi ? UpdateOption_Multi : 0)); + conn->say(toSend); args.rval().setUndefined(); } diff --git a/src/mongo/shell/bench.cpp b/src/mongo/shell/bench.cpp index d604c2bbdec..754f2bb091b 100644 --- a/src/mongo/shell/bench.cpp +++ b/src/mongo/shell/bench.cpp @@ -916,7 +916,13 @@ void BenchRunWorker::generateLoadOnConnection(DBClientBase* conn) { builder.done(), result); } else { - conn->update(op.ns, query, update, op.upsert, op.multi); + auto toSend = + makeUpdateMessage(op.ns, + query, + update, + (op.upsert ? UpdateOption_Upsert : 0) | + (op.multi ? UpdateOption_Multi : 0)); + conn->say(toSend); if (op.safe) result = conn->getLastErrorDetailed(); } @@ -972,17 +978,20 @@ void BenchRunWorker::generateLoadOnConnection(DBClientBase* conn) { builder.done(), result); } else { + std::vector<BSONObj> insertArray; if (op.isDocAnArray) { - std::vector<BSONObj> insertArray; for (const auto& element : op.doc) { BSONObj e = fixQuery(element.Obj(), bsonTemplateEvaluator); insertArray.push_back(e); } - conn->insert(op.ns, insertArray); } else { - insertDoc = fixQuery(op.doc, bsonTemplateEvaluator); - conn->insert(op.ns, insertDoc); + insertArray.push_back(fixQuery(op.doc, bsonTemplateEvaluator)); } + + auto toSend = makeInsertMessage( + op.ns, insertArray.data(), insertArray.size()); + conn->say(toSend); + if (op.safe) result = conn->getLastErrorDetailed(); } @@ -1029,7 +1038,9 @@ void BenchRunWorker::generateLoadOnConnection(DBClientBase* conn) { builder.done(), result); } else { - conn->remove(op.ns, predicate, !op.multi); + auto toSend = makeRemoveMessage( + op.ns, predicate, op.multi ? 0 : RemoveOption_JustOne); + conn->say(toSend); if (op.safe) result = conn->getLastErrorDetailed(); } |