电竞比分网-中国电竞赛事及体育赛事平台

分享

HBase的事務(wù)性(Transaction)

 昵稱10504424 2013-02-20

What is Transaction

先摘錄一段Wiki關(guān)于Transaction(事務(wù))的釋義:

A transaction comprises a unit of work performed within a database management system (or similar system) against a database, and treated in a coherent and reliable way independent of other transactions. Transactions in a database environment have two main purposes:
1. To provide reliable units of work that allow correct recovery from failures and keep a database consistent even in cases of system failure, when execution stops (completely or partially) and many operations upon a database remain uncompleted, with unclear status.

2. To provide isolation between programs accessing a database concurrently. If this isolation is not provided, the program's outcome are possibly erroneous.
A database transaction, by definition, must be atomic, consistent, isolated and durable. Database practitioners often refer to these properties of database transactions using the acronym ACID.
Transactions provide an "all-or-nothing" proposition, stating that each work-unit performed in a database must either complete in its entirety or have no effect whatsoever. Further, the system must isolate each transaction from other transactions, results must conform to existing constraints in the database, and transactions that complete successfully must get written to durable storage.

其中,all-or-nothing很明確的表達了事務(wù)的本質(zhì):“要么全成功,要么什么都沒發(fā)生過”??梢园阉醋饕粋€原子操作。



HBase Transaction

HBase的支持的事務(wù)很有限,0.94版本的新特性中有一條:

[HBASE-5229] - Provide basic building blocks for "multi-row" local transactions.

引用NoSQLFan博客中的解讀:

“0.94版本具備更完整的事務(wù)支持: 之前Hbase提供行級的事務(wù),不過每次事務(wù)只能執(zhí)行一個寫操作,比如連續(xù)地執(zhí)行一系列Put,Delete操作,那么這些操作是單獨一個個的事務(wù),其整體并不是原子性執(zhí)行的。而在0.94版本中,可以實現(xiàn)Put、Delete在同一個事務(wù)中一起原子性執(zhí)行。見提案HBASE-3584?!?/SPAN>

具體怎么用呢?有下面這一段Sample:

可以看到,這里的事務(wù)僅僅是針對某一行的一系列Put/Delete操作。不同行、不同表間一坨操作是無法放在一個事務(wù)中的。



What We Need

結(jié)合我之前的博文《HBase多條件查詢》,我們通常需要為了查詢而建立多個索引表。

比如我Save一條player數(shù)據(jù),主表(信息完整表)的rowKey是以ID構(gòu)成,順序排列。

我如果需要按player的積分(Scores)排個TOP 10,我可能還需要用(Max - player.getScores) + ID建一張索引表。同理可能還有多個……

每當插入一條player記錄,我需要同時對這兩個rowKey進行put操作。

這,就已經(jīng)超出了HBase支持的事務(wù)范疇(同一行的一系列操作事務(wù))。

此處無事務(wù)會怎樣?比如我新增一個player,首先執(zhí)行IDTbl.put,再執(zhí)行ScoresTbl.put。

在執(zhí)行第二步ScoresTbl.put時與HBase Cluster的網(wǎng)絡(luò)中斷了,此時ScoresTbl.put超時失敗,但IDTbl.put已經(jīng)成功完成了。

如果僅僅在業(yè)務(wù)層做了RollBack,此時應(yīng)該會執(zhí)行IDTbl.delete,但是網(wǎng)絡(luò)中斷,delete操作一樣會失敗。

網(wǎng)絡(luò)恢復(fù)了!

最終,我們新增的這位Player登錄進來(因為IDTbl.put中有他的記錄,所以可以成功登錄)。

在點擊“查看我的排名”按鈕時會觸發(fā)ScoresTbl.get操作,但ScoresTbl中沒有他的記錄,對后續(xù)操作會有不可預(yù)知的影響。

如果,我們有10幾張索引表……

因此,事務(wù)是必須的!


What Can We Do

為了解決這種不同記錄、不同表間的事務(wù)問題,我看到兩個項目。

1、yahoo/omid


The Omid project provides transactional support for key-value stores using Snapshot Isolation. Omid stands for Optimistically transactional Management in Datastores. At this stage of the project, HBase is the only supported data-store.

使用方式可以參考項目中的:

從Sample中看,我認為會與Spring Hadoop的hbaseTemplate有沖突,因為hbaseTemplate一次只能獲得唯一的HTable實例……

2、Indexed Transactional HBase


描述更是簡單:Transactional and indexing extensions for hbase

不過從github代碼的更新記錄來看,貌似已經(jīng)2年沒更新了,支持到0.90……


其他解決方案,我也會繼續(xù)尋找,也請有過HBase事務(wù)應(yīng)用的大牛們留言指個方向。謝謝 ^_^


    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多