Phoenix Omid Timestamp Oracle 组件实现原理

devtools/2024/10/21 21:05:30/

Omid Timestamp Oracle 组件实现原理

作用

生成全局单调递增的时间戳,支持获取操作和崩溃恢复。

功能

1.生成全局单调递增的时间戳(支持崩溃恢复)apinext返回下一个时间戳getLast返回最后一个分配的时间戳(当前时间戳)实现方式TimestampOracleImpl单调递增的时间戳在分配时间戳时,如果当前的最大时间戳已经用完,它会触发一个后台任务来更新最大时间戳next:分配下一个时间戳。如果当前的最大时间戳已经用完,它会等待后台任务分配新的时间戳WorldClockOracleImpl基于世界时间(System.timestamp)生成全局单调递增的时间戳
2.存储时间戳存储方式in memoryzkhbaseapiupdateMaxTimestamp更新最大时间戳getMaxTimestamp获取最大时间戳

TimestampStorage

存储时间戳

方法:
updateMaxTimestamp 更新最大时间戳
getMaxTimestamp 获取最大时间戳

实现类:

TimestampStorageInMemoryTimestampStorage in TimestampOracleImpl (org.apache.omid.tso) 使用内存存储ZKTimestampStorage (org.apache.omid.timestamp.storage) 使用 zk 存储HBaseTimestampStorage (org.apache.omid.timestamp.storage) 使用 hbase 存储InMemoryTimestampStorage in WorldClockOracleImpl (org.apache.omid.tso) 使用内存存储
ZKTimestampStorage

通过 zk curator DistributedAtomicLong 实现 DistributedAtomicLong timestamp

方法:
updateMaxTimestamp():
执行 timestamp.compareAndSet(previousMaxTimestamp, newMaxTimestamp)
getMaxTimestamp():
返回 timestamp.get()

TimestampOracle

TimestampOracle 提供了一个服务,用于生成单调递增的时间戳

方法:
initialize()
用于初始化时间戳服务。

next()
返回下一个时间戳。

getLast()
返回最后一个分配的时间戳(当前时间戳)

实现类
TimestampOracle (org.apache.omid.tso)TimestampOracleImpl (org.apache.omid.tso)PausableTimestampOracle (org.apache.omid.tso)WorldClockOracleImpl (org.apache.omid.tso)
TimestampOracleImpl
initialize()

1.从存储中获取 maxTimestamp;
2.初始化 AllocateTimestampBatchTask 用于分配时间戳,并执行一次.

next()

生成下一个时间戳。

@Override
public long next() {lastTimestamp += CommitTable.MAX_CHECKPOINTS_PER_TXN;// 当前时间戳超过分配阈值时,触发执行时间戳批次分配任务if (lastTimestamp >= nextAllocationThreshold) {// 设置下一次分配阈值为Long.MAX_VALUE,确保只有一个线程会执行分配任务nextAllocationThreshold = Long.MAX_VALUE; executor.execute(allocateTimestampsBatchTask);}// 当lastTimestamp超过当前已分配的最大时间戳时,等待新一批时间戳被分配if (lastTimestamp >= maxTimestamp) {assert (maxTimestamp <= maxAllocatedTimestamp);// 自旋等待,直到已分配的最大时间戳被更新while (maxAllocatedTimestamp == maxTimestamp) {// 自旋}assert (maxAllocatedTimestamp > maxTimestamp);// 更新当前最大时间戳并计算下次分配的阈值maxTimestamp = maxAllocatedTimestamp;nextAllocationThreshold = maxTimestamp - TIMESTAMP_REMAINING_THRESHOLD;assert (nextAllocationThreshold > lastTimestamp && nextAllocationThreshold < maxTimestamp);assert (lastTimestamp < maxTimestamp);}// 返回新生成的时间戳return lastTimestamp;
}
AllocateTimestampBatchTask

定时任务批量更新存储系统中最大的时间戳,每次预分配 TIMESTAMP_BATCH 数量的时间戳。

static final long TIMESTAMP_BATCH = 10_000_000 * CommitTable.MAX_CHECKPOINTS_PER_TXN; // 10 million
long newMaxTimestamp = previousMaxTimestamp + TIMESTAMP_BATCH;
WorldClockOracleImpl

基于世界时间的单调递增时间戳。

initialize
从 TimestampStorage 中获取最大的时间戳,并启动定时任务来定期更新最大时间戳。

next
用于获取下一个时间戳。
如果当前时间戳可用,则直接返回;
否则,等待定时任务分配新的时间戳。

getLast
方法返回最后一个时间戳。

AllocateTimestampBatchTask#run

定时任务预分配时间戳。

// 预测一个未来的时间窗口内允许的最大事务时间戳.
// 当前时间(System.currentTimeMillis())加上一个预设的时间间隔TIMESTAMP_INTERVAL_MS,然后乘以每毫秒允许的最大事务数MAX_TX_PER_MS
long newMaxTime = (System.currentTimeMillis() + TIMESTAMP_INTERVAL_MS) * MAX_TX_PER_MS;
next()
public long next() {long currentMsFirstTimestamp = System.currentTimeMillis() * MAX_TX_PER_MS; // 通过当前时间乘以每毫秒的最大事务数来计算当前毫秒的第一个时间戳lastTimestamp += CommitTable.MAX_CHECKPOINTS_PER_TXN;if (lastTimestamp >= currentMsFirstTimestamp) { // 如果lastTimestamp大于等于当前毫秒的第一个时间戳,直接返回lastTimestampreturn lastTimestamp;}// 当前 timestamp 的第一个时间戳 >= 最大时间戳.这里sleep等待 allocate 线程进行分配if (currentMsFirstTimestamp >= maxTimestamp) { // Intentional race to reduce synchronization overhead in every access to maxTimestamp                                                                                                                       while (maxAllocatedTime <= currentMsFirstTimestamp) { // Waiting for the interval allocationtry {Thread.sleep(1000);} catch (InterruptedException e) {continue;}}assert (maxAllocatedTime > maxTimestamp);maxTimestamp = maxAllocatedTime;}lastTimestamp = currentMsFirstTimestamp;return lastTimestamp;
}

http://www.ppmy.cn/devtools/57036.html

相关文章

【Docker项目实战篇】Docker部署PDF多功能工具Stirling-PDF

【Docker项目实战篇】Docker部署PDF多功能工具Stirling-PDF 前言一、Stirling-PDF介绍1.1 Stirling-PDF简介1.2 Stirling-PDF功能 二、本次实践规划2.1 本地环境规划2.2 本次实践介绍 三、本地环境检查3.1 检查Docker服务状态3.2 检查Docker版本3.3 检查docker compose 版本 四…

uniapp标题水平对齐微信小程序胶囊按钮及适配

uniapp标题水平对齐微信小程序胶囊按钮及适配 状态栏高度胶囊按钮的信息计算顶部边距模板样式 标签加样式加动态计算实现效果 t是胶囊按钮距离的top h是胶囊按钮的高度 s是状态栏高度 大概是这样 状态栏高度 获取系统信息里的状态栏高度 const statusBarHeight uni.getSy…

如何在Java中实现自定义数据结构:从头开始

如何在Java中实现自定义数据结构&#xff1a;从头开始 大家好&#xff0c;我是免费搭建查券返利机器人省钱赚佣金就用微赚淘客系统3.0的小编&#xff0c;也是冬天不穿秋裤&#xff0c;天冷也要风度的程序猿&#xff01;今天我们将探讨如何在Java中实现自定义数据结构&#xff…

Vue项目打包上线

Nginx 是一个高性能的开源HTTP和反向代理服务器&#xff0c;也是一个IMAP/POP3/SMTP代理服务器。它在设计上旨在处理高并发的请求&#xff0c;是一个轻量级、高效能的Web服务器和反向代理服务器&#xff0c;广泛用于提供静态资源、负载均衡、反向代理等功能。 1、下载nginx 2、…

【C语言】union 关键字

在C语言中&#xff0c;union关键字用于定义联合体。联合体是一种特殊的数据结构&#xff0c;它允许不同的数据类型共享同一段内存。所有联合体成员共享同一个内存位置&#xff0c;因此联合体的大小取决于其最大成员的大小。 定义和使用联合体 基本定义 定义一个联合体类型时…

【Qt】字符串加密

1. 代码 //加密算法MD5 QString Encrypt::encryptMd5(QString str) {QByteArray btArray QString(str).toLocal8Bit();QCryptographicHash hash(QCryptographicHash::Md5);hash.addData(btArray);QByteArray resultArray hash.result();QString md5 resultArray.toHex(); r…

为何很多事情只有三分钟热度~包括机器人学习和研究

这是一篇纯人工撰写的博客。 如果没有兴趣和热爱&#xff0c;又没有足够大的物质和&#xff08;或&#xff09;精神激励&#xff0c;我觉得让我做三分钟我都会觉得很长时间了。 没有主题只是一些想法-CSDN博客 想了很久也想不出如何起一个题目-CSDN博客 兴趣思考题 其实很多…

elementPlus自定义el-select下拉样式

如何在f12元素选择器上找到下拉div呢&#xff1f; 给el-select添加 :popper-append-to-body"false" 即可&#xff0c;这样就可以将下拉框添加到body元素中去&#xff0c;否则当我们失去焦点&#xff0c;下拉就消失了&#xff0c;在元素中找不到el-select。剩下就可以…