diff options
author | Kaloian Manassiev <kaloian.manassiev@mongodb.com> | 2018-09-12 11:35:12 -0400 |
---|---|---|
committer | Kaloian Manassiev <kaloian.manassiev@mongodb.com> | 2018-09-12 13:59:02 -0400 |
commit | d6336b278148540584eb39a658057dc8ae442788 (patch) | |
tree | edc50ddf8e3792d8840981564a3bed405e07a933 /src/mongo/db/transaction_participant.h | |
parent | c055154b81b4baa72f23c6760ace606d80aeeea9 (diff) | |
download | mongo-d6336b278148540584eb39a658057dc8ae442788.tar.gz |
SERVER-36799 Get rid of the separate TransactionParticipant 2PC state machine
Diffstat (limited to 'src/mongo/db/transaction_participant.h')
-rw-r--r-- | src/mongo/db/transaction_participant.h | 149 |
1 files changed, 9 insertions, 140 deletions
diff --git a/src/mongo/db/transaction_participant.h b/src/mongo/db/transaction_participant.h index 62e59be544c..6946c1a24c0 100644 --- a/src/mongo/db/transaction_participant.h +++ b/src/mongo/db/transaction_participant.h @@ -1,4 +1,4 @@ -/* +/** * Copyright (C) 2018 MongoDB, Inc. * * This program is free software: you can redistribute it and/or modify @@ -127,78 +127,20 @@ public: OperationContext* _opCtx; }; + TransactionParticipant() = default; + + /** + * Obtains the transaction participant from an operation context on which the session has been + * checked-out. + */ static TransactionParticipant* get(OperationContext* opCtx); /** - * This should only be used when session was obtained without checking it out. + * This should only be used when session was obtained without checking it out and its only user + * should be chunk migration. */ static TransactionParticipant* getFromNonCheckedOutSession(Session* session); - TransactionParticipant() = default; - - static boost::optional<TransactionParticipant>& get(Session* session); - static void create(Session* session); - - class StateMachine { - public: - friend class TransactionParticipant; - - // Note: We must differentiate the 'committed/aborted' and 'committed/aborted after prepare' - // states, because it is illegal to receive, for example, a prepare request after a - // commit/abort if no prepare was received before the commit/abort. - enum class State { - kUnprepared, - kAborted, - kCommitted, - kWaitingForDecision, - kAbortedAfterPrepare, - kCommittedAfterPrepare, - - // The state machine transitions to this state when a message that is considered illegal - // to receive in a particular state is received. This indicates either a byzantine - // message, or that the transition table does not accurately reflect an asynchronous - // network. - kBroken, - }; - - // State machine inputs - enum class Event { - kRecvPrepare, - kVoteCommitRejected, - kRecvAbort, - kRecvCommit, - }; - - // State machine outputs - enum class Action { - kNone, - kPrepare, - kAbort, - kCommit, - kSendCommitAck, - kSendAbortAck, - }; - - Action onEvent(Event e); - - State state() { - return _state; - } - - private: - struct Transition { - Transition() : action(Action::kNone) {} - Transition(Action action) : action(action) {} - Transition(Action action, State state) : action(action), nextState(state) {} - - Action action; - boost::optional<State> nextState; - }; - - static const std::map<State, std::map<Event, Transition>> transitionTable; - State _state{State::kUnprepared}; - }; - /** * Called for speculative transactions to fix the optime of the snapshot to read from. */ @@ -616,8 +558,6 @@ private: // Maintains the transaction state and the transition table for legal state transitions. TransactionState _txnState; - StateMachine _stateMachine; - // Holds oplog data for operations which have been applied in the current multi-document // transaction. std::vector<repl::ReplOperation> _transactionOperations; @@ -665,75 +605,4 @@ private: TransactionMetricsObserver _transactionMetricsObserver; }; -inline StringBuilder& operator<<(StringBuilder& sb, - const TransactionParticipant::StateMachine::State& state) { - using State = TransactionParticipant::StateMachine::State; - switch (state) { - // clang-format off - case State::kUnprepared: return sb << "Unprepared"; - case State::kAborted: return sb << "Aborted"; - case State::kCommitted: return sb << "Committed"; - case State::kWaitingForDecision: return sb << "WaitingForDecision"; - case State::kAbortedAfterPrepare: return sb << "AbortedAfterPrepare"; - case State::kCommittedAfterPrepare: return sb << "CommittedAfterPrepare"; - case State::kBroken: return sb << "Broken"; - // clang-format on - default: - MONGO_UNREACHABLE; - }; -} - -inline std::ostream& operator<<(std::ostream& os, - const TransactionParticipant::StateMachine::State& state) { - StringBuilder sb; - sb << state; - return os << sb.str(); -} - -inline StringBuilder& operator<<(StringBuilder& sb, - const TransactionParticipant::StateMachine::Event& event) { - using Event = TransactionParticipant::StateMachine::Event; - switch (event) { - // clang-format off - case Event::kRecvPrepare: return sb << "RecvPrepare"; - case Event::kVoteCommitRejected: return sb << "VoteCommitRejected"; - case Event::kRecvAbort: return sb << "RecvAbort"; - case Event::kRecvCommit: return sb << "RecvCommit"; - // clang-format on - default: - MONGO_UNREACHABLE; - }; -} - -inline std::ostream& operator<<(std::ostream& os, - const TransactionParticipant::StateMachine::Event& event) { - StringBuilder sb; - sb << event; - return os << sb.str(); -} - -inline StringBuilder& operator<<(StringBuilder& sb, - const TransactionParticipant::StateMachine::Action& action) { - using Action = TransactionParticipant::StateMachine::Action; - switch (action) { - // clang-format off - case Action::kNone: return sb << "None"; - case Action::kPrepare: return sb << "Prepare"; - case Action::kAbort: return sb << "Abort"; - case Action::kCommit: return sb << "Commit"; - case Action::kSendCommitAck: return sb << "SendCommitAck"; - case Action::kSendAbortAck: return sb << "SendAbortAck"; - // clang-format on - default: - MONGO_UNREACHABLE; - }; -} - -inline std::ostream& operator<<(std::ostream& os, - const TransactionParticipant::StateMachine::Action& action) { - StringBuilder sb; - sb << action; - return os << sb.str(); -} - } // namespace mongo |