1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
use std::collections::HashMap;
use std::ops::{Deref, DerefMut};
use std::time::{Duration, Instant};
use crate::core::db::DBPool;
use crate::core::db::DBTx;
use crate::core::runtime::{Arc, Receiver, RwLock, RwLockReadGuard, Sender};
use crate::core::sync::sync_map::{RefMut, SyncMap};
use crate::plugin::log::LogPlugin;
use crate::rbatis::Rbatis;
#[derive(Debug)]
pub struct TxManager {
pub tx_prefix: String,
pub tx_context: SyncMap<String, (DBTx, TxState)>,
pub tx_lock_wait_timeout: Duration,
pub tx_check_interval: Duration,
alive: RwLock<bool>,
close_sender: Sender<bool>,
close_recv: Receiver<bool>,
pub log_plugin: Option<Arc<Box<dyn LogPlugin>>>,
}
#[derive(Debug)]
pub enum TxState {
StateBegin(Instant),
StateFinish(Instant),
}
impl TxManager {
pub fn new_arc(tx_prefix: &str, plugin: Arc<Box<dyn LogPlugin>>, tx_lock_wait_timeout: Duration, tx_check_interval: Duration) -> Arc<Self> {
let (s, r) = crate::core::runtime::channel::bounded(1);
let s = Self {
tx_prefix: tx_prefix.to_string(),
tx_context: SyncMap::new(),
tx_lock_wait_timeout,
tx_check_interval,
alive: RwLock::new(false),
close_sender: s,
close_recv: r,
log_plugin: Some(plugin),
};
let arc = Arc::new(s);
TxManager::polling_check(arc.clone());
arc
}
async fn set_alive(&self, alive: bool) {
let mut l = self.alive.write().await;
*l = alive;
}
pub async fn get_alive(&self) -> RwLockReadGuard<'_, bool> {
self.alive.read().await
}
pub async fn close(&self) {
if self.get_alive().await.eq(&true) {
self.set_alive(false).await;
let r = self.close_recv.recv().await;
}
}
fn is_enable_log(&self) -> bool {
self.log_plugin.is_some() && self.log_plugin.as_ref().unwrap().is_enable()
}
fn do_log(&self, arg: &str) {
if self.is_enable_log() {
match &self.log_plugin {
Some(v) => {
v.do_log(arg);
}
_ => {}
}
}
}
fn polling_check(manager: Arc<Self>) {
crate::core::runtime::spawn(async move {
loop {
if manager.get_alive().await.deref() == &false {
let m = manager.tx_context.read().await;
let mut rollback_ids = vec![];
for (k, (tx, state)) in m.deref() {
rollback_ids.push(k.to_string());
}
drop(m);
for context_id in &rollback_ids {
if manager.is_enable_log() {
manager.do_log(&format!("[rbatis] rollback context_id:{},Because the manager exits", context_id));
}
manager.rollback(context_id).await;
}
manager.close_sender.send(true);
return;
}
let m = manager.tx_context.read().await;
let mut need_rollback = None;
for (k, (tx, state)) in m.deref() {
match state {
TxState::StateBegin(instant) => {
let out_time = instant.elapsed();
if out_time > manager.tx_lock_wait_timeout {
if need_rollback == None {
need_rollback = Some(vec![]);
}
match &mut need_rollback {
Some(v) => {
v.push(k.to_string());
}
_ => {}
}
}
}
_ => {}
}
}
drop(m);
match &mut need_rollback {
Some(v) => {
for context_id in v {
if manager.is_enable_log() {
manager.do_log(&format!("[rbatis] rollback context_id:{},out of time:{:?}", context_id, &manager.tx_lock_wait_timeout));
}
manager.rollback(context_id).await;
}
manager.tx_context.shrink_to_fit().await;
}
_ => {}
}
crate::core::runtime::sleep(manager.tx_check_interval).await;
}
});
}
pub async fn get_mut<'a>(&'a self, context_id: &str) -> Option<RefMut<'a, String, (DBTx, TxState)>> {
self.tx_context.get_mut(context_id).await
}
pub async fn begin(&self, new_context_id: &str, pool: &DBPool) -> Result<String, crate::core::Error> {
if new_context_id.is_empty() {
return Err(crate::core::Error::from("[rbatis] context_id can not be empty"));
}
let conn: DBTx = pool.begin().await?;
self.tx_context.insert(new_context_id.to_string(), (conn, TxState::StateBegin(Instant::now()))).await;
if self.is_enable_log() {
self.do_log(&format!("[rbatis] [{}] Begin", new_context_id));
}
return Ok(new_context_id.to_string());
}
pub async fn commit(&self, context_id: &str) -> Result<String, crate::core::Error> {
let tx_op = self.tx_context.remove(context_id).await;
if tx_op.is_none() {
return Err(crate::core::Error::from(format!("[rbatis] tx:{} not exist!", context_id)));
}
let (mut tx, state): (DBTx, TxState) = tx_op.unwrap();
let result = tx.commit().await?;
if self.is_enable_log() {
self.do_log(&format!("[rbatis] [{}] Commit", context_id));
}
return Ok(context_id.to_string());
}
pub async fn rollback(&self, context_id: &str) -> Result<String, crate::core::Error> {
let tx_op = self.tx_context.remove(context_id).await;
if tx_op.is_none() {
return Err(crate::core::Error::from(format!("[rbatis] tx:{} not exist!", context_id)));
}
let (tx, state): (DBTx, TxState) = tx_op.unwrap();
let result = tx.rollback().await?;
if self.is_enable_log() {
self.do_log(&format!("[rbatis] [{}] Rollback", context_id));
}
return Ok(context_id.to_string());
}
pub fn is_tx_prifix_id(&self, context_id: &str) -> bool {
return context_id.starts_with(&self.tx_prefix);
}
}
pub struct TxGuard {
pub tx_id: String,
pub is_drop_commit: bool,
pub manager: Option<Arc<TxManager>>,
}
impl TxGuard {
pub fn new(tx_id: &str, is_drop_commit: bool, manager: Arc<TxManager>) -> Self {
Self {
tx_id: tx_id.to_string(),
is_drop_commit,
manager: Some(manager),
}
}
pub async fn try_commit(&mut self) -> Result<String, crate::core::Error> {
match &mut self.manager {
Some(m) => {
let result = m.commit(&self.tx_id).await?;
self.manager = None;
return Ok(result);
}
_ => {}
}
return Result::Ok(self.tx_id.clone());
}
pub async fn try_rollback(&mut self) -> Result<String, crate::core::Error> {
match &mut self.manager {
Some(m) => {
let result = m.rollback(&self.tx_id).await?;
self.manager = None;
return Ok(result);
}
_ => {}
}
return Result::Ok(self.tx_id.clone());
}
}
impl Drop for TxGuard {
fn drop(&mut self) {
if self.manager.is_none() {
return;
}
let tx_id = self.tx_id.clone();
let is_drop_commit = self.is_drop_commit;
let manager = self.manager.take().unwrap();
crate::core::runtime::spawn(async move {
if is_drop_commit {
manager.commit(&tx_id).await;
} else {
manager.rollback(&tx_id).await;
}
drop(manager);
});
}
}