sqltool 0.4.1

功能强大的数据库迁移、同步、运维工具,支持自动分库分表、慢查询检测、数据对比、备份恢复,提供多语言SDK
Documentation
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
use crate::databases::DatabaseConnection;
use crate::core::sharding::ShardInfo;
use anyhow::{Result, anyhow};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use tokio::sync::Mutex;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DistributedTransaction {
    pub tx_id: String,
    pub participants: Vec<TxParticipant>,
    pub status: TxStatus,
    pub created_at: i64,
    pub updated_at: i64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TxParticipant {
    pub shard_id: String,
    pub connection_info: String,
    pub status: ParticipantStatus,
    pub prepared_at: Option<i64>,
    pub committed_at: Option<i64>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum TxStatus {
    Initializing,
    Preparing,
    Prepared,
    Committing,
    Committed,
    Aborting,
    Aborted,
    RolledBack,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ParticipantStatus {
    Pending,
    Prepared,
    Committed,
    Aborted,
}

pub struct DistributedTxManager {
    active_transactions: RwLock<HashMap<String, Arc<Mutex<DistributedTransaction>>>>,
    coordinator: TxCoordinator,
}

#[derive(Clone)]
pub struct TxCoordinator {
    transactions: Arc<RwLock<HashMap<String, DistributedTransaction>>>,
    participant_locks: Arc<RwLock<HashMap<String, Arc<Mutex<()>>>>>,
}

impl TxCoordinator {
    pub fn new() -> Self {
        Self {
            transactions: Arc::new(RwLock::new(HashMap::new())),
            participant_locks: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    pub async fn create_transaction(&self, tx_id: &str) -> Result<DistributedTransaction> {
        let tx = DistributedTransaction {
            tx_id: tx_id.to_string(),
            participants: Vec::new(),
            status: TxStatus::Initializing,
            created_at: chrono::Utc::now().timestamp(),
            updated_at: chrono::Utc::now().timestamp(),
        };
        
        let mut transactions = self.transactions.write().unwrap();
        transactions.insert(tx_id.to_string(), tx.clone());
        
        Ok(tx)
    }

    pub async fn add_participant(&self, tx_id: &str, shard: &ShardInfo) -> Result<()> {
        let mut transactions = self.transactions.write().unwrap();

        if let Some(tx) = transactions.get_mut(tx_id) {
            let participant = TxParticipant {
                shard_id: format!("shard_{}", shard.shard_index),
                connection_info: shard.target_db.clone().unwrap_or_default(),
                status: ParticipantStatus::Pending,
                prepared_at: None,
                committed_at: None,
            };
            tx.participants.push(participant);
            tx.updated_at = chrono::Utc::now().timestamp();
            Ok(())
        } else {
            Err(anyhow!("Transaction not found: {}", tx_id))
        }
    }

    pub async fn prepare(&self, tx_id: &str) -> Result<bool> {
        let mut transactions = self.transactions.write().unwrap();
        
        if let Some(tx) = transactions.get_mut(tx_id) {
            tx.status = TxStatus::Preparing;
            tx.updated_at = chrono::Utc::now().timestamp();
            
            for participant in &mut tx.participants {
                participant.status = ParticipantStatus::Prepared;
                participant.prepared_at = Some(chrono::Utc::now().timestamp());
            }
            
            tx.status = TxStatus::Prepared;
            tx.updated_at = chrono::Utc::now().timestamp();
            
            Ok(true)
        } else {
            Err(anyhow!("Transaction not found: {}", tx_id))
        }
    }

    pub async fn commit(&self, tx_id: &str) -> Result<()> {
        let mut transactions = self.transactions.write().unwrap();
        
        if let Some(tx) = transactions.get_mut(tx_id) {
            tx.status = TxStatus::Committing;
            tx.updated_at = chrono::Utc::now().timestamp();
            
            for participant in &mut tx.participants {
                participant.status = ParticipantStatus::Committed;
                participant.committed_at = Some(chrono::Utc::now().timestamp());
            }
            
            tx.status = TxStatus::Committed;
            tx.updated_at = chrono::Utc::now().timestamp();
            
            Ok(())
        } else {
            Err(anyhow!("Transaction not found: {}", tx_id))
        }
    }

    pub async fn rollback(&self, tx_id: &str) -> Result<()> {
        let mut transactions = self.transactions.write().unwrap();
        
        if let Some(tx) = transactions.get_mut(tx_id) {
            tx.status = TxStatus::Aborting;
            tx.updated_at = chrono::Utc::now().timestamp();
            
            for participant in &mut tx.participants {
                participant.status = ParticipantStatus::Aborted;
            }
            
            tx.status = TxStatus::RolledBack;
            tx.updated_at = chrono::Utc::now().timestamp();
            
            Ok(())
        } else {
            Err(anyhow!("Transaction not found: {}", tx_id))
        }
    }

    pub async fn get_transaction(&self, tx_id: &str) -> Option<DistributedTransaction> {
        let transactions = self.transactions.read().unwrap();
        transactions.get(tx_id).cloned()
    }
}

impl Default for TxCoordinator {
    fn default() -> Self {
        Self::new()
    }
}

impl DistributedTxManager {
    pub fn new() -> Self {
        Self {
            active_transactions: RwLock::new(HashMap::new()),
            coordinator: TxCoordinator::new(),
        }
    }

    pub async fn begin_distributed_tx(&self, tx_id: &str) -> Result<DistributedTransaction> {
        let tx = self.coordinator.create_transaction(tx_id).await?;
        let tx_arc = Arc::new(Mutex::new(tx.clone()));
        
        let mut active = self.active_transactions.write().unwrap();
        active.insert(tx_id.to_string(), tx_arc);
        
        Ok(tx)
    }

    pub async fn add_participant(&self, tx_id: &str, shard: &ShardInfo) -> Result<()> {
        self.coordinator.add_participant(tx_id, shard).await
    }

    pub async fn prepare(&self, tx_id: &str) -> Result<bool> {
        self.coordinator.prepare(tx_id).await
    }

    pub async fn commit(&self, tx_id: &str) -> Result<()> {
        self.coordinator.commit(tx_id).await
    }

    pub async fn rollback(&self, tx_id: &str) -> Result<()> {
        self.coordinator.rollback(tx_id).await
    }

    pub async fn two_phase_commit(&self, tx_id: &str) -> Result<()> {
        let prepared = self.prepare(tx_id).await?;
        
        if prepared {
            self.commit(tx_id).await?;
        } else {
            self.rollback(tx_id).await?;
        }
        
        let mut active = self.active_transactions.write().unwrap();
        active.remove(tx_id);
        
        Ok(())
    }

    pub async fn execute_in_distributed_tx<F, Fut>(
        &self,
        tx_id: &str,
        operations: Vec<F>,
    ) -> Result<()>
    where
        F: Fn(String) -> Fut,
        Fut: std::future::Future<Output = Result<()>>,
    {
        let tx = self.begin_distributed_tx(tx_id).await?;
        
        for (participant, operation) in tx.participants.iter().zip(operations) {
            if let Err(e) = operation(participant.shard_id.clone()).await {
                self.rollback(tx_id).await?;
                return Err(e);
            }
        }
        
        self.two_phase_commit(tx_id).await?;
        Ok(())
    }
}

impl Default for DistributedTxManager {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Clone)]
pub struct SagaTransaction {
    pub saga_id: String,
    pub steps: Vec<SagaStep>,
    pub compensations: Vec<CompensationStep>,
    pub status: SagaStatus,
}

#[derive(Debug, Clone)]
pub struct SagaStep {
    pub step_id: String,
    pub shard_id: String,
    pub operation: String,
    pub parameters: Vec<serde_json::Value>,
}

#[derive(Debug, Clone)]
pub struct CompensationStep {
    pub step_id: String,
    pub original_step_id: String,
    pub compensating_operation: String,
    pub parameters: Vec<serde_json::Value>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SagaStatus {
    Running,
    Completed,
    Compensating,
    Compensated,
    Failed,
}

pub struct SagaManager {
    transactions: RwLock<HashMap<String, SagaTransaction>>,
}

impl SagaManager {
    pub fn new() -> Self {
        Self {
            transactions: RwLock::new(HashMap::new()),
        }
    }

    pub fn create_saga(&self, saga_id: &str) -> Result<SagaTransaction> {
        let saga = SagaTransaction {
            saga_id: saga_id.to_string(),
            steps: Vec::new(),
            compensations: Vec::new(),
            status: SagaStatus::Running,
        };
        
        let mut transactions = self.transactions.write().unwrap();
        transactions.insert(saga_id.to_string(), saga.clone());
        
        Ok(saga)
    }

    pub fn add_step(&self, saga_id: &str, step: SagaStep, compensation: CompensationStep) -> Result<()> {
        let mut transactions = self.transactions.write().unwrap();
        
        if let Some(saga) = transactions.get_mut(saga_id) {
            saga.steps.push(step);
            saga.compensations.push(compensation);
            Ok(())
        } else {
            Err(anyhow!("Saga not found: {}", saga_id))
        }
    }

    pub fn execute_saga<'a, F, Fut>(&self, saga_id: &'a str, executor: F) -> Result<()>
    where
        F: Fn(&SagaStep) -> Fut,
        Fut: std::future::Future<Output = Result<()>>,
    {
        let saga = {
            let transactions = self.transactions.read().unwrap();
            transactions.get(saga_id).cloned()
        };

        let saga = saga.ok_or_else(|| anyhow!("Saga not found: {}", saga_id))?;

        let mut executed_steps: Vec<usize> = Vec::new();
        let rt = tokio::runtime::Runtime::new().unwrap();

        for (i, step) in saga.steps.iter().enumerate() {
            let result = rt.block_on(executor(step));
            match result {
                Ok(()) => {
                    executed_steps.push(i);
                }
                Err(e) => {
                    let _ = self.compensate_saga_sync(saga_id, executed_steps);
                    return Err(e);
                }
            }
        }

        let mut transactions = self.transactions.write().unwrap();
        if let Some(saga) = transactions.get_mut(saga_id) {
            saga.status = SagaStatus::Completed;
        }

        Ok(())
    }

    fn compensate_saga_sync(&self, saga_id: &str, _executed_steps: Vec<usize>) -> Result<()> {
        let mut transactions = self.transactions.write().unwrap();

        if let Some(saga) = transactions.get_mut(saga_id) {
            saga.status = SagaStatus::Compensating;
        }

        if let Some(saga) = transactions.get_mut(saga_id) {
            saga.status = SagaStatus::Compensated;
        }

        Ok(())
    }
}

impl Default for SagaManager {
    fn default() -> Self {
        Self::new()
    }
}

pub struct XaTransaction {
    pub xid: String,
    pub format_id: i32,
    pub global_transaction_id: Vec<u8>,
    pub branch_qualifier: Vec<u8>,
    pub resourceManagers: HashMap<String, XaResourceManager>,
    pub state: XaState,
}

pub struct XaResourceManager {
    pub name: String,
    pub connection: Box<dyn DatabaseConnection>,
    pub prepared: bool,
}

impl std::fmt::Debug for XaResourceManager {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("XaResourceManager")
            .field("name", &self.name)
            .field("prepared", &self.prepared)
            .finish()
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum XaState {
    Active,
    Idle,
    Prepared,
    MarkedRollback,
    RolledBack,
    Committed,
    Unknown,
}

impl XaTransaction {
    pub fn new(xid: &str) -> Self {
        Self {
            xid: xid.to_string(),
            format_id: 0,
            global_transaction_id: xid.as_bytes().to_vec(),
            branch_qualifier: Vec::new(),
            resourceManagers: HashMap::new(),
            state: XaState::Active,
        }
    }

    pub fn add_resource_manager(&mut self, name: &str, conn: Box<dyn DatabaseConnection>) {
        self.resourceManagers.insert(name.to_string(), XaResourceManager {
            name: name.to_string(),
            connection: conn,
            prepared: false,
        });
    }

    pub async fn start(&mut self) -> Result<()> {
        for (_, rm) in &mut self.resourceManagers {
            let sql = format!("XA START '{}'", self.xid);
            rm.connection.execute(&sql).await?;
        }
        self.state = XaState::Active;
        Ok(())
    }

    pub async fn end(&mut self) -> Result<()> {
        for (_, rm) in &mut self.resourceManagers {
            let sql = format!("XA END '{}'", self.xid);
            rm.connection.execute(&sql).await?;
        }
        self.state = XaState::Idle;
        Ok(())
    }

    pub async fn prepare(&mut self) -> Result<bool> {
        for (_, rm) in &mut self.resourceManagers {
            let sql = format!("XA PREPARE '{}'", self.xid);
            rm.connection.execute(&sql).await?;
            rm.prepared = true;
        }
        self.state = XaState::Prepared;
        Ok(true)
    }

    pub async fn commit(&mut self) -> Result<()> {
        for (_, rm) in &mut self.resourceManagers {
            let sql = format!("XA COMMIT '{}'", self.xid);
            rm.connection.execute(&sql).await?;
        }
        self.state = XaState::Committed;
        Ok(())
    }

    pub async fn rollback(&mut self) -> Result<()> {
        for (_, rm) in &mut self.resourceManagers {
            let sql = format!("XA ROLLBACK '{}'", self.xid);
            rm.connection.execute(&sql).await?;
        }
        self.state = XaState::RolledBack;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_distributed_transaction_creation() {
        let coordinator = TxCoordinator::new();
        let rt = tokio::runtime::Runtime::new().unwrap();
        
        rt.block_on(async {
            let tx = coordinator.create_transaction("tx_001").await.unwrap();
            assert_eq!(tx.tx_id, "tx_001");
            assert_eq!(tx.status, TxStatus::Initializing);
        });
    }

    #[test]
    fn test_saga_transaction() {
        let manager = SagaManager::new();
        
        let saga = manager.create_saga("saga_001").unwrap();
        assert_eq!(saga.saga_id, "saga_001");
        assert_eq!(saga.status, SagaStatus::Running);
    }

    #[test]
    fn test_xa_transaction() {
        let xa_tx = XaTransaction::new("xa_001");
        assert_eq!(xa_tx.xid, "xa_001");
        assert_eq!(xa_tx.state, XaState::Active);
    }
}