diff --git a/src/mongo/db/repl/oplog.cpp b/src/mongo/db/repl/oplog.cpp index 87d5a1a..d9c4065 100644 --- a/src/mongo/db/repl/oplog.cpp +++ b/src/mongo/db/repl/oplog.cpp @@ -673,21 +673,21 @@ namespace { "Failed to apply insert due to missing _id: " << op.toString(), o.hasField("_id")); - // TODO: It may be better to do an insert here, and then catch the duplicate - // key exception and do update then. Very few upserts will not be inserts... - BSONObjBuilder b; - b.append(o.getField("_id")); + WriteUnitOfWork wuow(txn); + StatusWith status = collection->insertDocument( txn, o, true ); - const NamespaceString requestNs(ns); - UpdateRequest request(requestNs); - - request.setQuery(b.done()); - request.setUpdates(o); - request.setUpsert(); - UpdateLifecycleImpl updateLifecycle(true, requestNs); - request.setLifecycle(&updateLifecycle); - - update(txn, db, request, &debug); + if (status.isOK()) { + wuow.commit(); + } + else if (!status.isOK() && status.getStatus().code() != ErrorCodes::DuplicateKey) { + log() << "skipping document duplicate key error during replicated insert: " + << o.toString(); + } + else if (!status.isOK()) { + string msg = str::stream() << "failed to apply insert: " << op.toString(); + error() << msg; + return Status(ErrorCodes::OperationFailed, msg); + } } } else if ( *opType == 'u' ) {