XA两阶段提交和准备阶段执行?

时间:2012-11-16 23:16:37

标签: distributed-transactions xa 2phase-commit paxos

我正在尝试理解两阶段提交,并且我不清楚每个本地站点何时执行其分布式事务的部分。

是否在发送准备消息之前发生这种情况。那是在两阶段提交xa协议甚至运行之前发生的吗?

或者每个站点在收到准备消息后是否执行其分布式事务的一部分,这意味着准备消息本身还包括要执行的事务查询?

1 个答案:

答案 0 :(得分:2)

是的,在发送准备消息之前执行。您应该假设整个2PC协议在所有内容都已执行后在commit()操作中运行。考虑以下最终提交的分布式事务的想象跟踪。缩进意味着过程嵌套:

transactionalOperation() is a procedure at some client site
    begin() is a remote invocation to the coordinator
        create global XID
        return global XID
    XID is added to the thread-local context at the caller
    executeOperation(params) is a remote invocation to a participant site
        XID is implicitly propagated to the server side thread
        inform coordinator that this site is a participant
        execute the operation! (acquiring locks)
    ... more operations ....
    commit() is a remote invocation to the coordinator
        XID is implicitly propagated to the server side thread
        -------------- 2PC starts here ----------------
        for all registered participants:
            prepare(XID) is a remote invocation to each site
                make sure that the transaction can be committed,
                usually by writing and flushing a log 
                return OK
        gather decisions
        for all reguistered participants:
            commit(XID) is a remote invocation to each site
                log transaction commit
                (release locks)
        -------------- 2PC ends here ----------------
    XID is removed from the thread-local context at the caller
    transaction is complete!

实际上,正如您所看到的,准备消息将由协调员仅发送到之前在此类交易的上下文中执行某些内容的网站,因此之前已注册为该交易的参与者。