Chapter Three
Total Page:16
File Type:pdf, Size:1020Kb
Chapter Three 3. Distributed Transaction Management Transactions are used to protect shared data. A process can make used of a transaction to access and modify multiple data items as a single atomic operation. If a transaction operates on data that are distributed across multiple machines then that is a distributed transaction. There are three main components in a general distributed transaction management organization. The data manager which performs the actual read/write operations, the scheduler which handles concurrency control, and the transaction manager which manages the entire transaction (Figure 1) [7]. Figure 1: Components in a Distributed Transaction 15 Each transactional resource maintains ACID properties locally. A distributed transaction maintains the following global properties. Global atomicity: All resource managers must abort or all commit Global serialization: Distributed transaction must be globally serializable Global deadlocks: There must be no deadlocks involving multiple sites 3.1. Java Transaction API Java Transaction API (JTA) implements the X/Open Distributed Transaction Processing Model [12]. X/Open is an independent, worldwide, open systems organization that aims to create and promote a vendor-independent interface standard called the Common Applications Environment (CAE) supported by most of the world’s largest information systems suppliers, user organizations and software companies [38]. In the JTA specification, distributed transactions are also referred to as global transactions. In this model there are three components which get involved in a transaction (Figure 2) [12]. Application Program Resource Manager Transaction Manager The application program uses resources from a set of resource managers and interacts with the transaction manager to define transaction boundaries. 16 Figure 2: Components in a JTA Distributed Transaction A distributed transaction accesses resource managers distributed across a network. The transaction manager interacts with the resource managers to carry out the commit protocol. Here the resource manager is equivalent to the scheduler and the data manager put together in the Figure 1. Typically a JTA implementation is used within the J2EE environment. Therefore, the application server, or to be more generic the container also has an important role to play in such distributed transactions. Figure 3 illustrates the interfaces used and the interactions in carrying out a distributed transaction in a JTA implementation. 17 Figure 3: Interfaces Involved in a JTA Transaction Note: Original is in color Java Transaction Service (JTS) specifies the implementation of a transaction manager by implementing Java mapping of CORBA Object Transaction Service (OTS) [13]. JTS transaction manager at the high level implements JTA and at the low level implements the Java mapping of the CORBA OTS 1.1 Specification [13]. Following is a brief description of the execution flow in a JTA implementation of transaction handling [12]. 1. A client application invokes a method (in an EJB or a POJO in IOC container) with TX_REQUIRED transaction attribute. At that point container starts a global transaction by asking the Transaction Manager to start a transaction before making the invocation. 2. After the transaction starts, the container invokes the method. The method would require a handler to the connection based resource. For example if the resource is a database it would require a database connection. This handler can 18 be obtained using the API provided by the relevant resource adapter. In the case of Java the resource adapter for a database is the JDBC driver. 3. At this point the application server (container) obtains a resource from the resource adapter. 4. The resource adapter creates the Transactional Resource object and the associated XAResource and Connection objects. 5. The application server request for the associated XAResource reference to the transactional resource. 6. The application server enlists the resource to the transaction manager. 7. The transaction manager calls the start method of the XAResource to associate the current transaction to the resource. 8. The application server request for a Connection object reference to the transactional resource. 9. The application server returns the Connection object reference to the application. 10. The application performs one or more operations on the connection. 11. The application closes the connection. 12. The application server receives a notification on connection closure from the resource adapter and at that point delists the resource from the transaction manager. 13. The transaction manager calls the end method of the XAResource to disassociate the transaction from the resource. 14. The application server asks the transaction manager to commit the transaction. 15. The transaction manager calls the prepare method of the XAResource to inform the resource manager to prepare the transaction work for commit. 16. If the resource manager is ready the transaction manager calls the commit method of the XAResource to commit the transaction. 19 Figure 4: Sequence of Events in a JTA Transaction The sequence diagram in Figure 4 illustrates the sequence of method invocations relevant to the above description in a JTA implementation [12]. 3.2. Two Phase Commit Protocol An atomic commit protocol, initiated by the coordinator ensures the global atomicity. In order to maintain atomicity in a distributed transaction, the protocol should allow the whole transaction to be aborted if any one of the participants (resource managers) 20 aborts the transaction. The two phase commit protocol is designed to handle this [7]. Figure 5: Two Phase Commit Protocol State Changes in the Coordinator In the first phase of the two phase commit protocol, the coordinator sends a VOTE_REQUEST message to all participants. When a participant receives the VOTE_REQUEST message from the coordinator, it replies with either VOTE_COMMIT or VOTE_ABORT depend on its ability to commit its part of the transaction locally. The coordinator collects all the votes and if all participants have replied with VOTE_COMMIT, it also votes for a commit and sends a GLOBAL_COMMIT message to all participants. If at least one of the participants has replied VOTE_ABORT, it sends a GLOBAL_ABORT message to all the participants, aborting the distributed transaction. Each participant that has voted for a commit waits for the final message from the coordinator and if it receives a GLOBAL_COMMIT message, locally commit its part of the transaction or if it receives GLOBAL_ABORT message, locally abort its part of the transaction. 21 Figure 6: Two Phase Commit Protocol State Changes in the participant 3.2.1. Two Phase Commit Protocol under Failure Let us now evaluate the challenges of implementing a two phase commit protocol under failure. In a standard communication environment, if there are no byzantine failures, there could be communication failures and process failures. Any such failure could block the protocol execution. Typically timeout mechanisms are used to identify these types of failures. There are three scenarios where the protocol execution could get blocked. Blocked coordinator in the WAIT state waiting for Vote-commit or Vote-abort from a participant Blocked participant in the INIT state waiting for Vote-request from the coordinator or Blocked participant in the READY state waiting for Global-commit or Global- abort from the coordinator It can be understood that since the coordinator is the one who initiate the protocol it is not going wait in the INIT state. Also coordinator does not expect an acknowledgment for Global-commit and Global-abort. Hence it is not going to wait in those stages as well. Participant does not going get blocked at COMMIT or ABORT state since it is 22 the last state of the protocol execution. 3.2.1.1. Blocked Coordinator in the WAIT State The lost of messages VOTE_REQUEST, VOTE_COMMIT, VOTE_ABORT or a crashed participant could cause the coordinator to timeout in the state WAIT (Figure 7). Figure 7: Coordinator Blocked at State WAIT Note: Original is in color If a timeout occur at this state, the coordinator vote for an abort and sends a GLOBAL_ABORT to all participants. 3.2.1.2. Blocked Participant in the INIT State The lost of the message VOTE_REQUEST or a crashed coordinator could cause a participant to timeout in the INIT state (Figure 8). If a timeout occur at this stage the participant locally aborts the transaction and sends a VOTE_ABORT to the coordinator. 3.2.1.3. Blocked Participant in the READY state The lost of messages GLOBAL_COMMIT, GLOBAL_ABORT or a crashed coordinator could cause a participant to timeout in the READY state (Figure 9). 23 Figure 8: Participant Blocked at State INIT Note: Original is in color Figure 9: Participant Blocked at State READY Note: Original is in color In this case the participant has to find out whether the coordinator has sent a GLOBAL_COMMIT message or a GLOBAL_ABORT message. The participant can contact another participant and check its state. If the second participant has received a GLOBAL_COMMIT or a GLOBAL_ABORT the participant who didn't receive the message can decide whether to locally commit its part of the transaction or to locally abort its part of the transaction. The second participant could still be in the INIT state if the coordinator has crashed while multicasting the VOTE_REQUEST message. Then it is safe to locally abort the transaction. If the second participant is also in the 24 READY state then no decision can be made, since to commit the transaction all participants should vote for it. In this case it has to wait until the coordinator recovers and resend the decision. 3.2.1.4. Process Recovery When a crashed process recovered it needs to figure out what are the next steps to follow in order to either complete the transaction if it is possible, or to abort the transaction. Hence during the protocol execution a process should save its state so that it can execute a recovery algorithm once it recovers from a crash. A coordinator could be in any of the states, INIT, WAIT, COMMIT or ABORT when a crash occurs.