整个系统的需求文档为英文描述。
A Simple 2-Phase Commit System
The company ABC provides its customers wire transfer service. For example, it can withdraw $1000 from John‘s account in Bank of China and
deposit the money to John‘s another account in China Construction Bank. Also, it should report this transaction to China Banking Regulatory
Commission (CBRC) for auditing purpose.
The following diagram illustrates the system architecture.
This is a typical example of distributed transaction.
1. There is no direct communication among Bank of China (BoC), China Construction Bank (CCB), and CBRC‘s application server. They
only interact with ABC‘s transaction manager.
2. A wire transfer like this is a transaction (so ACID). All operations of the transaction (BoC substracts $1000 from John‘s account, CCB
adds $1000 to John‘s account, and CBRC records this transaction) must be all-done or none-done.
Please write 5 small programs:
1. A client program that can talk to ABC Transaction Manager to require a wire transfer. This is a command line program (e.g. "cli john
1000 boc ccb 127.0.0.1 8888" means sending a wire transfer request to ABC transaction manager that runs on 127.0.0.1 and listens on
the port 8888. The request requires a wire transfer of $1000 from John‘s BoC account to John‘s CCB account)
2. ABC transaction manager - it waits for requests from the client, initiates the distributed transaction among Boc, CCB, and CBRC, then
send the client the wire transfer result (succeed or fail)
3. BoC Application Server - it uses a MySQL db to store account information, and waits for requests from ABC‘s transaction manager.
4.CCB Application Server - same as BoC Application Server (you can combine 3 and 4 as one program)
5.CBRC Application Server - it uses a MySQL db to store all transactions, and waits for requests from ABC‘s transaction manager.
Your system should be able to handle all kinds of failures that can be handled by the classical 2PC algorithm.
在接到这个项目需求后,首先是了解需求,分析2PC原理,然后对整个系统进行设计。因本系统是一个类似模拟项目,并在一周的业余时间完成,因此,笔者做了以下基本的目标定位:
vGlob Description: this is a demo system of wire transfer between banks , the system mainly focus on the 2 phase communication.
v
vFeature
1.User interface is simply and easy to use. The company use web server to receive the concurrent requests from users , Even we provide a command client(line) tools for user , Actually, if provide a simple web page, user can use browser to access our server.
2.Extendibility: All the data’s format between the servers is json format, the communicate protocol is HTTP. This is a simply and frequently-used data format and protocol , it is self-reading and easy to add new function.
3.High concurrency : both the company and the bank servers use one thread to hold all the concurrent requests, but the company server use multi-threads to support concurrent access to the banks’ server, the bank servers use multi-connection to support concurrent execution ofmul-sql request.
4.Support crashed recovery: both the company and bank server record all phases logs in disk to support crashed recovery.
5.Reliability: fully support 2pc .
因此,主要设计目标是高并发处理以及2PC容错协议设计。以下整个系统部署图。
整个系统的company Transaction WEB Server面向所有用户,实现高并发,Hold住用户的连接,隔离后端数据系统,在这一部分,只是简单的使用备份来实现HA。由company WEB Server来维护整个2PC协议。
后端的三个数据库系统分别有一个高并发的服务器,虽然这些服务器不直接面向用户,但并发量并一定小,因为他可能面向的不止一个Company server,而且异步的处理每个用户连接。并发量并不小。
后端数据库为mysql数据库,为了实现数据的原子操作,使用行锁的方式。
以下是整个系统的进程架构图。
1.客户端并发请求。解决办法:使用一个WEB服务器Hold住所有连接,将所有请求构建成一个队列,在后面使用多线程并发请求数据库并返回所有回复到回复队列。工作线程的数量不宜过多,避免后端数据库服务器过于繁忙挂掉。
2.各个线程池中的线程在收到一个请求后,与后端进行2PC过程,每个阶段在向后端发送阻塞式请求,使用poll方式来hold住三个连接, 如果超时,则进行超时处理。
接下来就是2pc中某个阶段错误的处理协议约定。详细信息在 共享资源-高并发服务器页面
二阶段提交应用项目(Two-phase commit protocol )2PC 高并发