程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 二階段提交應用項目(Two-phase commit protocol )2PC 高並發

二階段提交應用項目(Two-phase commit protocol )2PC 高並發

編輯:C++入門知識

二階段提交應用項目(Two-phase commit protocol )2PC 高並發


整個系統的需求文檔為英文描述。

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 of mul-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住三個連接, 如果超時,則進行超時處理。


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved