Apache Omid TSO 組件源碼實(shí)現(xiàn)原理解析
Apache Omid TSO 組件實(shí)現(xiàn)原理
作用
獨(dú)立進(jìn)程,處理全局事務(wù)之間的并發(fā)沖突。
流程
TSOChannelHandler#channelRead -> AbstractRequestProcessor -> PersistenceProcessorHandler
總體流程
thread1
TSOChannelHandler#channelRead
AbstractRequestProcessor#timestampRequest 接收 client 請(qǐng)求,創(chuàng)建 RequestEvent 并 publish
thread2
AbstractRequestProcessor#onEvent 處理 RequestEvent 請(qǐng)求
AbstractRequestProcessor#handleRequest
PersistenceProcessorImpl#addTimestampToBatch 創(chuàng)建 PersistEvent,當(dāng) batch 滿了發(fā)送事件
thread3
PersistenceProcessorHandler#onEvent 持久化事件處理類
TSOChannelHandler
繼承自 Netty 的 ChannelInboundHandlerAdapter,用于處理 TSO 的入站請(qǐng)求。
channelRead
委托 requestProcessor 創(chuàng)建 timestampRequest 和 commitRequest 請(qǐng)求事件。
AbstractRequestProcessor
處理 timestamp 和 commit 事件。
onEvent
處理 RequestEvent 事件,按照事件類型派發(fā)給 handleTimestamp 和 handleCommit 方法進(jìn)行處理。
handleTimestamp
1.通過 timestampOracle 獲取下一個(gè)時(shí)間戳;
2.PersistenceProcessorImpl#addBatch 事件添加到 batch,但是后續(xù)對(duì) timestamp 請(qǐng)求不會(huì)額外處理。
handleCommit
主要通過 hasConflictsWithCommittedTransactions 判斷 writeSet 和 CommitHashMap 里是否有事務(wù)寫沖突,如果沒有則可以提交事務(wù),分配 commitTimestamp。
private void handleCommit(RequestEvent event) throws Exception {
long startTimestamp = event.getStartTimestamp(); // startTimestamp
Iterable<Long> writeSet = event.writeSet(); // 寫入集,存儲(chǔ)的是 cellIds
Collection<Long> tableIdSet = event.getTableIdSet();
boolean isCommitRetry = event.isCommitRetry();
boolean nonEmptyWriteSet = writeSet.iterator().hasNext(); // 檢查寫集合是否為空,即事務(wù)是否有寫操作
if (startTimestamp > lowWatermark &&
!hasConflictsWithFences(startTimestamp, tableIdSet) &&
!hasConflictsWithCommittedTransactions(startTimestamp, writeSet)) { // 檢查事務(wù)是否滿足提交條件,通過 hasConflictsWithCommittedTransactions 判斷是否有事務(wù)寫沖突
// 可以進(jìn)行事務(wù)提交
long commitTimestamp = timestampOracle.next(); // 獲取提交時(shí)間戳
Optional<Long> forwardNewWaterMark = Optional.absent();
if (nonEmptyWriteSet) { // 寫集合非空
long newLowWatermark = lowWatermark;
for (long r : writeSet) { // 遍歷寫集合中的每個(gè)元素,更新其最新的寫入時(shí)間戳,并計(jì)算新的低水位線
long removed = hashmap.putLatestWriteForCell(r, commitTimestamp); // 更新 cellId 對(duì)應(yīng)的 commitTimestamp, 返回之前的 oldest commitTimestamp
newLowWatermark = Math.max(removed, newLowWatermark); // 更新低水位線
}
if (newLowWatermark != lowWatermark) { // 更新低水位線
lowWatermark = newLowWatermark;
forwardNewWaterMark = Optional.of(lowWatermark);
}
}
forwardCommit(startTimestamp, commitTimestamp, c, event.getMonCtx(), forwardNewWaterMark); // 持久化 commit 請(qǐng)求
} else { // 事務(wù)不滿足提交條件
if (isCommitRetry) { // Re-check if it was already committed but the client retried due to a lag replying
forwardCommitRetry(startTimestamp, c, event.getMonCtx()); // 若是提交重試,再次檢查是否已提交以避免因響應(yīng)延遲導(dǎo)致的重復(fù)提交
} else {
forwardAbort(startTimestamp, c, event.getMonCtx()); // 否則,中止事務(wù)
}
}
}CommitHashMap
通過 LongCache 緩存 cellId -> lastCommittedTimestamp 的映射。
getLatestWriteForCell 方法:
根據(jù) cellId 獲取 lastCommittedTimestamp。
putLatestWriteForCell 方法:
更新 cellId 對(duì)應(yīng)的 lastCommittedTimestamp。
LongCache
緩存 cellId -> lastCommittedTimestamp 的映射。
get 和 set 操作都是先將原始 cellId 進(jìn)行 hash 操作找到位置,所以可能存在沖突。
set
更新 cellId 對(duì)應(yīng)的 lastCommittedTimestamp。
public long set(long key, long value) {
final int index = index(key); // cellId 取模返回下標(biāo),可能會(huì)沖突
int oldestIndex = 0;
long oldestValue = Long.MAX_VALUE;
for (int i = 0; i < associativity; ++i) {
int currIndex = 2 * (index + i); // 計(jì)算 key 下標(biāo)
if (cache[currIndex] == key) { // 相同事務(wù) cellId, 替換場(chǎng)景
oldestValue = 0;
oldestIndex = currIndex;
break;
}
if (cache[currIndex + 1] <= oldestValue) { // 沒找到相同的key.通過和 oldestValue 比較會(huì)將最小的 timestamp 剔除
oldestValue = cache[currIndex + 1];
oldestIndex = currIndex;
}
}
// 替換最舊的鍵值對(duì),將其更新為新的鍵值對(duì)
cache[oldestIndex] = key;
cache[oldestIndex + 1] = value;
return oldestValue;
}get
獲取 cellId 對(duì)應(yīng)的 lastCommittedTimestamp,找不到則返回 0.
public long get(long key) {
final int index = index(key);
for (int i = 0; i < associativity; ++i) { // associativity 里存儲(chǔ)的元素key應(yīng)該是相同的
int currIndex = 2 * (index + i); // 計(jì)算 key 的下標(biāo)
if (cache[currIndex] == key) { // 找到 cache key
return cache[currIndex + 1]; // 返回對(duì)應(yīng)的 value
}
}
return 0;
}PersistenceProcessorImpl
將 startTimestamp 和 commitTimestamp 放入 batch.
addCommitToBatch
創(chuàng)建 event,添加到 current batch
如果 current batch is full
triggerCurrentBatchFlushtriggerCurrentBatchFlush
創(chuàng)建 PersistBatchEvent 并發(fā)送事件
PersistenceProcessorHandler
處理上面 PersistenceProcessorImpl 發(fā)送過來的事件,進(jìn)行持久化處理。
onEvent
實(shí)際上只處理 commit 事件,會(huì)創(chuàng)建 put 對(duì)象將事務(wù)信息持久化到 hbase 的 commitTable (OMID_COMMIT_TABLE).
HBaseCommitTable
構(gòu)造方法: 根據(jù) HBaseCommitTableConfig 配置初始化
到此這篇關(guān)于Apache Omid TSO 組件源碼實(shí)現(xiàn)原理的文章就介紹到這了,更多相關(guān)Apache Omid TSO 組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
linux設(shè)置服務(wù)開機(jī)自啟動(dòng)的三種方式
這篇文章主要介紹了linux設(shè)置服務(wù)開機(jī)自啟動(dòng)的三種方式,需要的朋友可以參考下2015-11-11
Service Temporarily Unavailable的503錯(cuò)誤是怎么回事?
一般來說,出現(xiàn)Service Temporarily Unavailable錯(cuò)誤多半是因?yàn)榫W(wǎng)站訪問量大,造成了流量超限或者并發(fā)數(shù)大引起的資源超限出現(xiàn)的錯(cuò)誤2013-05-05
Polysh命令實(shí)現(xiàn)多日志查詢的方法示例
大家應(yīng)該都知道Polysh是一個(gè)交互式命令,可以在一臺(tái)服務(wù)器上批量的對(duì)一批服務(wù)器進(jìn)行處理,運(yùn)行交互式命令。下面這篇文章主要給大家介紹了關(guān)于利用Polysh命令實(shí)現(xiàn)多日志查詢的相關(guān)資料,需要的朋友可以參考,下面來一起看看吧。2017-12-12
詳解Linux 安裝 JDK、Tomcat 和 MySQL(圖文并茂)
本文會(huì)詳細(xì)介紹 Windows 上安裝虛擬機(jī)之后,如何在 Linux 中安裝 JDK、Tomcat 和 MySQL 的過程,有興趣的可以了解一下2017-08-08
linux系列之常用運(yùn)維命令整理筆錄(小結(jié))
這篇文章主要介紹了linux系列之常用運(yùn)維命令整理筆錄(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
Centos7 firewall和docker沖突問題及解決過程
本文描述了一個(gè)在CentOS 7上使用firewalld和Docker容器的問題,當(dāng)firewalld啟動(dòng)或重啟時(shí),會(huì)從iptables中移除Docker的規(guī)則,導(dǎo)致Docker容器中的服務(wù)無(wú)法訪問,最終,作者通過先啟動(dòng)或重啟防火墻,然后重啟Docker服務(wù),并刪除有問題的容器,重新創(chuàng)建容器來解決了問題2025-12-12

