summaryrefslogtreecommitdiff
path: root/src/mongo/db/commands/txn_cmds.cpp
diff options
context:
space:
mode:
authorLouis Williams <louis.williams@mongodb.com>2018-03-21 11:37:40 -0400
committerLouis Williams <louis.williams@mongodb.com>2018-03-21 13:19:54 -0400
commit9799d8b7223205635ccb428bbec1ba4d947ba13b (patch)
treee11df2a05cfcbd09bbf8f236b619b988495e01ca /src/mongo/db/commands/txn_cmds.cpp
parent0d409c4d63afb585b329187ff904be9068df9466 (diff)
downloadmongo-9799d8b7223205635ccb428bbec1ba4d947ba13b.tar.gz
SERVER-33306 SERVER-33307 SERVER-33308 Add prepareTransaction command stub. Add prepare method to
WriteUnitOfWork. Implement prepareUnitOfWork in WiredTigerRecoveryUnit.
Diffstat (limited to 'src/mongo/db/commands/txn_cmds.cpp')
-rw-r--r--src/mongo/db/commands/txn_cmds.cpp56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/mongo/db/commands/txn_cmds.cpp b/src/mongo/db/commands/txn_cmds.cpp
index da055acd7ed..3abfc87c2dd 100644
--- a/src/mongo/db/commands/txn_cmds.cpp
+++ b/src/mongo/db/commands/txn_cmds.cpp
@@ -37,6 +37,7 @@
#include "mongo/db/operation_context.h"
#include "mongo/db/service_context.h"
#include "mongo/db/session_catalog.h"
+#include "mongo/util/fail_point_service.h"
namespace mongo {
namespace {
@@ -87,6 +88,61 @@ public:
} commitTxn;
+// TODO: This is a stub for testing storage prepare functionality.
+class CmdPrepareTxn : public BasicCommand {
+public:
+ CmdPrepareTxn() : BasicCommand("prepareTransaction") {}
+
+ AllowedOnSecondary secondaryAllowed(ServiceContext*) const override {
+ return AllowedOnSecondary::kNever;
+ }
+
+ bool supportsWriteConcern(const BSONObj& cmd) const override {
+ return true;
+ }
+
+ std::string help() const override {
+ return "Preprares a transaction. THIS IS A STUB FOR TESTING.";
+ }
+
+ Status checkAuthForOperation(OperationContext* opCtx,
+ const std::string& dbname,
+ const BSONObj& cmdObj) const override {
+ return Status::OK();
+ }
+
+ bool run(OperationContext* opCtx,
+ const std::string& dbname,
+ const BSONObj& cmdObj,
+ BSONObjBuilder& result) override {
+ auto session = OperationContextSession::get(opCtx);
+ uassert(
+ ErrorCodes::CommandFailed, "prepareTransaction must be run within a session", session);
+
+ uassert(ErrorCodes::CommandFailed,
+ "Transaction isn't in progress",
+ session->inMultiDocumentTransaction());
+
+ auto opObserver = opCtx->getServiceContext()->getOpObserver();
+ invariant(opObserver);
+ opObserver->onTransactionPrepare(opCtx);
+
+ // For testing purposes, this command prepares and immediately aborts the transaction,
+ // Running commit after prepare is not allowed yet.
+ // Prepared units of work cannot be released by the session, so we immediately abort here.
+ opCtx->getWriteUnitOfWork()->prepare();
+ opCtx->setWriteUnitOfWork(nullptr);
+ return true;
+ }
+};
+
+MONGO_INITIALIZER(RegisterPrepareTxnCmd)(InitializerContext* context) {
+ if (getTestCommandsEnabled()) {
+ new CmdPrepareTxn();
+ }
+ return Status::OK();
+}
+
class CmdAbortTxn : public BasicCommand {
public:
CmdAbortTxn() : BasicCommand("abortTransaction") {}