HBase中的RIT機制(Region in Transcation)
每天到公司第一件事,就是檢查測試環境和線上環境的幾個hbase叢集執行狀態。由於測試環境用的幾臺機器都是虛擬機器,因此不可避免的很容易出現regionserver宕機的情況。每次對於宕機的regionserver重啟後的一段時間內,部門裡的其他一些需要呼叫hbase的開發人員就開始抱怨,hbase 客戶端連線錯誤 超時等等各種抱怨,其實也是因為regionserver 啟動後,本身會進行一次 RIT操作,這時候 client 服務會暫時關閉掉,這裡稍微解釋下RIT機制,以便我們更好的進行hbase叢集的維護工作。
RIT 的全稱是region in transcation. 每次hbase master 對region 的一個open 或一個close 操作都會想Master 的RIT中插入一條記錄,因為master 對region 的操作要保持原子性,region 的 open 和 close 是通過Hmaster 和 region server 協助來完成的. 所以為了滿足這些操作的協調,回滾,和一致性.Hmaster 採用了 RIT 機制並結合Zookeeper 中Node的狀態來保證操作的安全和一致性.
為了弄清楚RIT的狀態機制, 我們先來了解下下面的2個enum. 第一個是 RegionState的狀態, 它的作用是用於記錄Hmaster在操作region時region 所處的狀態.
- /**
- * State of a Region while undergoing transitions.
- */
- publicstaticclass RegionState implements Writable {
- private HRegionInfo region;
- publicenum State {
-
OFFLINE, // region is in an offline state
- PENDING_OPEN, // sent rpc to server to open but has not begun
- OPENING, // server has begun to open but not yet done
- OPEN, // server opened region and updated meta
- PENDING_CLOSE, // sent rpc to server to close but has not begun
-
CLOSING, // server has begun to close but not yet done
- CLOSED // server closed region and updated meta
- }
- private State state;
- privatelong stamp;
- RS_ZK_REGION_CLOSING, RS_ZK_REGION_CLOSED ,RS_ZK_REGION_OPENING ,RS_ZK_REGION_OPENED ,M_ZK_REGION_OFFLINE
/**
- * List of all HBase event handler types. Event types are named by a
- * convention: event type names specify the component from which the event
- * originated and then where its destined -- e.g. RS2ZK_ prefix means the
- * event came from a regionserver destined for zookeeper -- and then what
- * the even is; e.g. REGION_OPENING.
- *
- * <p>We give the enums indices so we can add types later and keep them
- * grouped together rather than have to add them always to the end as we
- * would have to if we used raw enum ordinals.
- */
- publicenum EventType {
- // Messages originating from RS (NOTE: there is NO direct communication from
- // RS to Master). These are a result of RS updates into ZK.
- RS_ZK_REGION_CLOSING (1), // RS is in process of closing a region
- RS_ZK_REGION_CLOSED (2), // RS has finished closing a region
- RS_ZK_REGION_OPENING (3), // RS is in process of opening a region
- RS_ZK_REGION_OPENED (4), // RS has finished opening a region
- // Messages originating from Master to RS
- M_RS_OPEN_REGION (20), // Master asking RS to open a region
- M_RS_OPEN_ROOT (21), // Master asking RS to open root
- M_RS_OPEN_META (22), // Master asking RS to open meta
- M_RS_CLOSE_REGION (23), // Master asking RS to close a region
- M_RS_CLOSE_ROOT (24), // Master asking RS to close root
- M_RS_CLOSE_META (25), // Master asking RS to close meta
- // Messages originating from Client to Master
- C_M_DELETE_TABLE (40), // Client asking Master to delete a table
- C_M_DISABLE_TABLE (41), // Client asking Master to disable a table
- C_M_ENABLE_TABLE (42), // Client asking Master to enable a table
- C_M_MODIFY_TABLE (43), // Client asking Master to modify a table
- C_M_ADD_FAMILY (44), // Client asking Master to add family to table
- C_M_DELETE_FAMILY (45), // Client asking Master to delete family of table
- C_M_MODIFY_FAMILY (46), // Client asking Master to modify family of table
- // Updates from master to ZK. This is done by the master and there is
- // nothing to process by either Master or RS
- M_ZK_REGION_OFFLINE (50), // Master adds this region as offline in ZK
- // Master controlled events to be executed on the master
- M_SERVER_SHUTDOWN (70), // Master is processing shutdown of a RS
- M_META_SERVER_SHUTDOWN (72); // Master is processing shutdown of RS hosting a meta region (-ROOT- or .META.).
- /**
- * Constructor
- */
- EventType(int value) {}
- }
Hmaster中的AssignmentManager是主要來管理region 上線下線操作的。我們可以暫時理解為Assign就是將region 上線,Unassign就是將region下線。
Assign region 時會先將regionstate 置為OFFLINE,等getregionplan結束後取得了region on line 的server 。 後將其置為PENDING_OPEN並將reionstatus加入到RIT之中.隨後呼叫RPC讓regionserver 中OpenRegionHandler來在region server端處理open region的動作,其中包括將zk中node的狀態置為RS_ZK_REGION_OPENING 然後對hregion 進行初始話,最後將zk中
node 的狀態置為RS_ZK_REGION_OPENED。 後續master端會通過zk 的watch機制發現node的狀態發生變化。詳見圖三中左半部分流程。
同樣Unassign region 時會先將regionstate 置為PENDING_CLOSE狀態並將reionstatus加入到RIT之中,在完成一系列檢查之後會呼叫RPC讓regionserver 中CloseRegionHandler來在region server端處理open region的動作,其中包括將zk中node的狀態置為RS_ZK_REGION_CLOSEING 然後對hregion 進行CLOSE,最後將zk中 node 的狀態置為RS_ZK_REGION_CLOSEED。 後續master端會通過zk
的watch機制發現node的狀態發生變化。詳見圖三中左半部分流程。
在圖三所示中是hmaster在對RIT的處理。如果是第一次啟動,會監視ZK的變化,發現ZK變化後根據ZK中node的狀態呼叫hmaster中的hander(ClosedRegionHandler,OpeneRegionHandler)來處理,如果node 狀態為RS_ZK_REGION_CLOSED ,RS_ZK_REGION_OPENED,
表示region server動作已經完成,master 完成相應動作後會其對於的RIT中的regionstate刪除。這樣就完成了RIT中regionstate的一個生命過程。 同樣如果是FAILOVER中做recover的master會對ZK中的node狀態進行掃描,然後處理對應的RIT。
RIT中儲存的長時間沒有處理的regionstate會被chore執行緒處理掉,重新加入assign和unassign佇列之中。