summaryrefslogtreecommitdiff
path: root/src/mongo/scripting/mozjs/session.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/scripting/mozjs/session.cpp')
-rw-r--r--src/mongo/scripting/mozjs/session.cpp106
1 files changed, 102 insertions, 4 deletions
diff --git a/src/mongo/scripting/mozjs/session.cpp b/src/mongo/scripting/mozjs/session.cpp
index 676e865b317..477ab4024dc 100644
--- a/src/mongo/scripting/mozjs/session.cpp
+++ b/src/mongo/scripting/mozjs/session.cpp
@@ -41,28 +41,70 @@
#include "mongo/scripting/mozjs/valuereader.h"
#include "mongo/scripting/mozjs/wrapconstrainedmethod.h"
#include "mongo/util/log.h"
+#include "mongo/util/mongoutils/str.h"
namespace mongo {
namespace mozjs {
-const JSFunctionSpec SessionInfo::methods[3] = {
+const JSFunctionSpec SessionInfo::methods[8] = {
MONGO_ATTACH_JS_CONSTRAINED_METHOD_NO_PROTO(end, SessionInfo),
MONGO_ATTACH_JS_CONSTRAINED_METHOD_NO_PROTO(getId, SessionInfo),
+ MONGO_ATTACH_JS_CONSTRAINED_METHOD_NO_PROTO(getTxnState, SessionInfo),
+ MONGO_ATTACH_JS_CONSTRAINED_METHOD_NO_PROTO(setTxnState, SessionInfo),
+ MONGO_ATTACH_JS_CONSTRAINED_METHOD_NO_PROTO(getTxnNumber, SessionInfo),
+ MONGO_ATTACH_JS_CONSTRAINED_METHOD_NO_PROTO(setTxnNumber, SessionInfo),
+ MONGO_ATTACH_JS_CONSTRAINED_METHOD_NO_PROTO(incrementTxnNumber, SessionInfo),
JS_FS_END,
};
const char* const SessionInfo::className = "Session";
-
struct SessionHolder {
+ enum class TransactionState { kActive, kInactive, kCommitted, kAborted };
+ // txnNumber starts at -1 because when we increment it, the first transaction
+ // and retryable write will both have a txnNumber of 0.
SessionHolder(std::shared_ptr<DBClientBase> client, BSONObj lsid)
- : client(std::move(client)), lsid(std::move(lsid)) {}
+ : client(std::move(client)),
+ lsid(std::move(lsid)),
+ txnState(TransactionState::kInactive),
+ txnNumber(-1) {}
std::shared_ptr<DBClientBase> client;
BSONObj lsid;
+ TransactionState txnState;
+ std::int64_t txnNumber;
};
namespace {
+StringData transactionStateName(SessionHolder::TransactionState state) {
+ switch (state) {
+ case SessionHolder::TransactionState::kActive:
+ return "active"_sd;
+ case SessionHolder::TransactionState::kInactive:
+ return "inactive"_sd;
+ case SessionHolder::TransactionState::kCommitted:
+ return "committed"_sd;
+ case SessionHolder::TransactionState::kAborted:
+ return "aborted"_sd;
+ }
+
+ MONGO_UNREACHABLE;
+}
+
+SessionHolder::TransactionState transactionStateEnum(StringData name) {
+ if (name == "active"_sd) {
+ return SessionHolder::TransactionState::kActive;
+ } else if (name == "inactive"_sd) {
+ return SessionHolder::TransactionState::kInactive;
+ } else if (name == "committed"_sd) {
+ return SessionHolder::TransactionState::kCommitted;
+ } else if (name == "aborted"_sd) {
+ return SessionHolder::TransactionState::kAborted;
+ } else {
+ uasserted(ErrorCodes::BadValue, str::stream() << "Invalid TransactionState name: " << name);
+ }
+}
+
SessionHolder* getHolder(JSObject* thisv) {
return static_cast<SessionHolder*>(JS_GetPrivate(thisv));
}
@@ -76,11 +118,22 @@ void endSession(SessionHolder* holder) {
return;
}
+ BSONObj out;
+
+ if (holder->txnState == SessionHolder::TransactionState::kActive) {
+ holder->txnState = SessionHolder::TransactionState::kAborted;
+ BSONObj abortObj = BSON("abortTransaction" << 1 << "lsid" << holder->lsid << "txnNumber"
+ << holder->txnNumber
+ << "autocommit"
+ << false);
+
+ holder->client->runCommand("admin", abortObj, out);
+ }
+
EndSessions es;
es.setEndSessions({holder->lsid});
- BSONObj out;
holder->client->runCommand("admin", es.toBSON(), out);
holder->client.reset();
@@ -126,6 +179,51 @@ void SessionInfo::Functions::getId::call(JSContext* cx, JS::CallArgs args) {
ValueReader(cx, args.rval()).fromBSON(holder->lsid, nullptr, 1);
}
+void SessionInfo::Functions::getTxnState::call(JSContext* cx, JS::CallArgs args) {
+ auto holder = getHolder(args);
+ invariant(holder);
+ uassert(ErrorCodes::BadValue, "getTxnState takes no arguments", args.length() == 0);
+
+ ValueReader(cx, args.rval()).fromStringData(transactionStateName(holder->txnState));
+}
+
+void SessionInfo::Functions::setTxnState::call(JSContext* cx, JS::CallArgs args) {
+ auto holder = getHolder(args);
+ invariant(holder);
+ uassert(ErrorCodes::BadValue, "setTxnState takes 1 argument", args.length() == 1);
+
+ auto arg = args.get(0);
+ holder->txnState = transactionStateEnum(ValueWriter(cx, arg).toString().c_str());
+ args.rval().setUndefined();
+}
+
+void SessionInfo::Functions::getTxnNumber::call(JSContext* cx, JS::CallArgs args) {
+ auto holder = getHolder(args);
+ invariant(holder);
+ uassert(ErrorCodes::BadValue, "getTxnNumber takes no arguments", args.length() == 0);
+
+ ValueReader(cx, args.rval()).fromInt64(holder->txnNumber);
+}
+
+void SessionInfo::Functions::setTxnNumber::call(JSContext* cx, JS::CallArgs args) {
+ auto holder = getHolder(args);
+ invariant(holder);
+ uassert(ErrorCodes::BadValue, "setTxnNumber takes 1 argument", args.length() == 1);
+
+ auto arg = args.get(0);
+ holder->txnNumber = ValueWriter(cx, arg).toInt64();
+ args.rval().setUndefined();
+}
+
+void SessionInfo::Functions::incrementTxnNumber::call(JSContext* cx, JS::CallArgs args) {
+ auto holder = getHolder(args);
+ invariant(holder);
+ uassert(ErrorCodes::BadValue, "incrementTxnNumber takes no arguments", args.length() == 0);
+
+ ++holder->txnNumber;
+ args.rval().setUndefined();
+}
+
void SessionInfo::make(JSContext* cx,
JS::MutableHandleObject obj,
std::shared_ptr<DBClientBase> client,