Skip to main content

SqlStore

Struct SqlStore 

Source
pub struct SqlStore { /* private fields */ }

Implementations§

Source§

impl SqlStore

Source

pub async fn open(url: &str) -> Result<Self>

url 可以是:

  • sqlite:dtmrs.db / sqlite::memory:
  • postgres://user:pass@host:5432/db
Source

pub async fn migrate(&self) -> Result<()>

Source

pub fn backend(&self) -> Backend

Source

pub fn pool(&self) -> &AnyPool

Source

pub async fn create_global( &self, g: &GlobalRow, branches: &[BranchRow], ) -> Result<bool>

建全局事务 + 所有分支,一个事务里做完。

返回 false 表示 gid 已存在 —— 这是幂等提交,不是错误: 客户端重试提交时必须拿到“已受理“而不是报错。

Source

pub async fn create_token( &self, hash: &str, name: &str, secret: &str, ) -> Result<()>

Source

pub async fn list_tokens(&self) -> Result<Vec<TokenRow>>

Source

pub async fn revoke_token(&self, hash: &str) -> Result<bool>

作废。不删行 —— 留着才能在管理台看到「这个 token 什么时候被谁作废的」

Source

pub async fn active_token_hashes(&self) -> Result<Vec<String>>

当前有效的令牌哈希。认证的热路径不查这个 —— 上层按 TTL 缓存, 见 dtmrs_server::auth

Source

pub async fn touch_token(&self, hash: &str, ip: &str) -> Result<()>

记一次使用。尽力而为:失败只吞掉不影响请求 —— 统计信息不值得让一次正常的业务调用失败

Source

pub async fn get_global(&self, gid: &str) -> Result<Option<GlobalRow>>

Source

pub async fn list_branches(&self, gid: &str) -> Result<Vec<BranchRow>>

Source

pub async fn set_global_status( &self, gid: &str, status: GlobalStatus, _trans_type: TransType, reason: &str, ) -> Result<()>

落全局状态。

trans_type 这一层用不上(UPDATE 不需要它),但 Redis 后端靠它把 「落终态」这条热路径从 Lua 脚本降级成一次 MULTI —— 两边签名要一致

Source

pub async fn set_branch_result( &self, gid: &str, branch_id: &str, op: BranchOp, status: BranchStatus, payload: &str, ) -> Result<()>

落一个分支的状态和结果数据

workflow 模式的重放靠这个:函数崩溃后会从头再跑一遍,已完成的分支 不重新执行,而是把上次存的 payload 原样还给它。所以这个值必须跟 「分支已成功」在同一条 UPDATE 里落盘 —— 分两步写的话,中间崩了 就会出现「标了成功但结果丢了」,重放时拿不到返回值。

Source

pub async fn set_branch_status( &self, gid: &str, branch_id: &str, op: BranchOp, status: BranchStatus, ) -> Result<()>

Source

pub async fn lock_one_due( &self, owner: &str, lease: i64, ) -> Result<Option<GlobalRow>>

抢一个到期的待办事务,抢占式更新,原子的

多个 TC 实例同时跑也不会重复推进同一个事务:谁的 UPDATE 生效谁持有租约。 持租约的实例崩了,next_cron_time 到期后别的实例接手 —— 这就是崩溃恢复。

Source

pub async fn schedule_retry(&self, gid: &str, interval: i64) -> Result<()>

推进失败后设置下次重试时间(指数退避)

Source

pub async fn submit_prepared( &self, gid: &str, owner: &str, next_cron_time: i64, ) -> Result<SubmitOutcome>

把停在 prepared 的事务推成 submitted,并立刻排进调度队列。

一次调用做完原来三次的活get_global + set_global_status + schedule_now)。Redis 后端上这是一个 Lua 脚本,11 条命令降到 3 条 —— 那边是单线程 CPU 瓶颈,命令数直接决定吞吐。

owner / next_cron_time 让提交方顺便把租约占下来:传自己的 owner 和「现在 + 租约」,这一条 UPDATE 之后事务就归调用方推了, 不用再走一次抢占。不想占就传空 owner 和 now()

Source

pub async fn schedule_now(&self, gid: &str) -> Result<()>

让某个事务立刻可被调度(提交/中止之后叫一下,不用等 cron 周期)

Source

pub async fn register_branch( &self, gid: &str, branch_id: &str, ops: &[(BranchOp, String)], ) -> Result<()>

TCC 的 try 阶段:客户端在调 try 之前先来登记这个分支的 confirm/cancel。

必须先登记再调 try。反过来的话:try 成功了但登记失败, TC 就不知道有这个分支,回滚时不会 cancel 它 —— 资源永久泄漏。

冲突时忽略,所以重复登记是幂等的(客户端重试很常见)。

Source

pub async fn list_recent(&self, limit: i64) -> Result<Vec<GlobalRow>>

Trait Implementations§

Source§

impl Clone for SqlStore

Source§

fn clone(&self) -> SqlStore

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more