dist_tx 0.5.1

Rust language bindings for XA Distributed Transactions
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
use super::{Status, TransactionManager};
use crate::{sync::rm::ResourceManager, ReturnCode, RmError, XaError, XaTransactionId};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use log::{debug, trace};
use std::{
    collections::{hash_map::DefaultHasher, HashMap},
    hash::{Hash, Hasher},
    io::Cursor,
    u64,
};

/// The format id that is used for the `XaTransactionId`s of `SimpleTransactionManager`.
const FORMAT_ID: i32 = 99;

/// `SimpleTransactionManager`
///
/// * identifies itself with a `u64` (hash of its name)
/// * identifies the resource managers with a `u64` given during the registration
/// * enumerates its global transactions with a `u64` counter,
///   starting with 0, or, if higher values are found during rm-registration,
///   with the highest found number (plus 1)
///
/// uses `XaTransactionId` with
///
/// * `format_id` = 99
/// * `global_tid`: u64 counter, starting from 0
/// * `branch_qualifier`: `tm_id`: u64, `rm_id`: u64
///
/// A minimal implementation of the `TransactionManager` interface.
///
/// Is identified with an application-defined String, whose hash is used as
/// `tm_id`.
///
/// No support is provided for multi-threading รก la XA.
///
#[derive(Debug)]
pub struct SimpleTransactionManager {
    name: String,
    id: u64,
    rms: HashMap<u64, Box<dyn ResourceManager>>,
    last_gtid: u64,
    current_gtid: Option<u64>,
    status: Status,
}
impl SimpleTransactionManager {
    /// Produces a new instance.
    #[must_use]
    pub fn new<S: AsRef<str>>(name: S) -> SimpleTransactionManager {
        trace!("new()");
        let name = name.as_ref().to_string();
        let mut s = DefaultHasher::new();
        name.hash(&mut s);
        SimpleTransactionManager {
            name,
            id: s.finish() & (u64::max_value() - 0b_1111_1111_u64),
            rms: HashMap::<u64, Box<dyn ResourceManager>>::new(),
            last_gtid: 0,
            current_gtid: None,
            status: Status::IDLE,
        }
    }

    /// Returns the global transaction id that is currently used
    /// by this `SimpleTransactionManager`.
    pub fn get_gtid(&mut self) -> Option<u64> {
        self.current_gtid
    }

    fn next_global_tid(&mut self) -> u64 {
        self.last_gtid += 1;
        self.last_gtid
    }

    fn get_current_gtid(&mut self) -> Result<u64, XaError> {
        match self.current_gtid {
            None => Err(XaError::Usage("No current transaction set")),
            Some(u) => Ok(u),
        }
    }

    fn validate_and_set_status(&mut self, required: Status, new: Status) -> Result<(), XaError> {
        if required.contains(self.status) {
            self.status = new;
            Ok(())
        } else {
            Err(XaError::UsageDetails(format!(
                "SimpleTransactionManager is in state {:?}, but state {required:?} is required",
                self.status,
            )))
        }
    }

    /// Reports the name of this instance.
    #[must_use]
    pub fn name(&self) -> &str {
        &self.name
    }

    fn rm_action<F>(&mut self, action: F, global_tid: u64) -> Result<(), XaError>
    where
        F: Fn(&mut Box<dyn ResourceManager>, &XaTransactionId) -> Result<ReturnCode, RmError>,
    {
        let mut errors = Vec::<RmError>::new();
        for (rm_id, rm) in &mut self.rms {
            let xatid = new_xatid(global_tid, self.id, *rm_id);
            if let Err(e) = action(rm, &xatid) {
                errors.push(e);
            }
        }
        if errors.is_empty() {
            Ok(())
        } else {
            Err(XaError::RmErrors(errors))
        }
    }

    fn rm_start(&mut self, global_tid: u64) -> Result<(), XaError> {
        self.rm_action(|rm, xatid| ((**rm).start(xatid)), global_tid)
    }

    // fn rm_join(&mut self, global_tid: &u64) -> Result<(),XaError> {
    //     self.rm_action(|rm, xatid| ((**rm).start_by_joining(xatid)), global_tid)
    // }

    // fn rm_resume(&mut self, global_tid: &u64) -> Result<(),XaError> {
    //     self.rm_action(|rm, xatid| ((**rm).start_by_resuming(xatid)), global_tid)
    // }

    // fn rm_suspend(&mut self, global_tid: &u64) -> Result<(),XaError> {
    //     self.rm_action(|rm, xatid| ((**rm).end_suspend(xatid)), global_tid)
    // }

    fn rm_end_success(&mut self, global_tid: u64) -> Result<(), XaError> {
        self.rm_action(|rm, xatid| ((**rm).end_success(xatid)), global_tid)
    }

    fn rm_end_failure(&mut self, global_tid: u64) -> Result<(), XaError> {
        self.rm_action(|rm, xatid| ((**rm).end_failure(xatid)), global_tid)
    }

    fn rm_prepare(&mut self, global_tid: u64) -> Result<(), XaError> {
        self.rm_action(|rm, xatid| ((**rm).prepare(xatid)), global_tid)
    }

    fn rm_commit(&mut self, global_tid: u64) -> Result<(), XaError> {
        self.rm_action(|rm, xatid| ((**rm).commit(xatid)), global_tid)
    }

    fn rm_commit_one_phase(&mut self, global_tid: u64) -> Result<(), XaError> {
        self.rm_action(|rm, xatid| ((**rm).commit_one_phase(xatid)), global_tid)
    }

    fn rm_rollback(&mut self, global_tid: u64) -> Result<(), XaError> {
        self.rm_action(|rm, xatid| ((**rm).rollback(xatid)), global_tid)
    }

    // fn rm_forget(&mut self, global_tid: &u64) -> Result<(),XaError> {
    //     self.rm_action(|rm, xatid| ((**rm).forget(xatid)), global_tid)
    // }

    // fn rm_recover(&mut self) -> XaResult<Vec<XaTransactionId>> {
    //     panic!("not yet implemented")
    // }

    fn try_rollback_after(
        &mut self,
        current_gtid: u64,
        method: &'static str,
    ) -> Result<(), XaError> {
        self.status = Status::ROLLINGBACK;
        let result = self.rm_rollback(current_gtid);
        if let Err(ref e) = result {
            trace_error(e, current_gtid, "error in rm_rollback");
        }
        self.status = Status::IDLE;
        match result {
            Err(e) => {
                if let XaError::RmErrors(v) = e {
                    Err(XaError::Inconsistency(
                        format!("rm_rollback() failed after a failed {method}()"),
                        v,
                    ))
                } else {
                    Err(e)
                }
            }
            Ok(()) => Ok(()),
        }
    }

    // fn is_my_xid(&self, xid: &XaTransactionId) -> bool {
    //     if xid.get_format_id() != FORMAT_ID {
    //         return false;
    //     }

    //     let bq: Vec<u8> = xid.get_branch_qualifier();
    //     if bq.len() != 16 {
    //         return false;
    //     };
    //     let mut rdr = Cursor::new(bq);

    //     rdr.read_u64::<LittleEndian>().unwrap() == self.id
    // }

    fn is_my_xid_and_rm(&self, xid: &XaTransactionId, rm_id: u64) -> bool {
        if xid.get_format_id() != FORMAT_ID {
            return false;
        }

        let bq = xid.get_branch_qualifier();
        if bq.len() != 16 {
            return false;
        };
        let mut rdr = Cursor::new(bq);

        if rdr.read_u64::<LittleEndian>().unwrap() != self.id {
            return false;
        }
        rdr.read_u64::<LittleEndian>().unwrap() == rm_id
    }
}

fn trace_error(e: &XaError, gtid: u64, method_name: &'static str) {
    if let XaError::RmErrors(ref vec_rmerr) = *e {
        for rm in vec_rmerr {
            trace!("{}({}) failed due to {:?}", method_name, gtid, rm);
        }
    } else {
        trace!("error in {}: {}", method_name, e);
    }
}

#[allow(clippy::similar_names)]
fn new_xatid(global_tid: u64, tm_id: u64, rm_id: u64) -> XaTransactionId {
    let mut v_gt = Vec::<u8>::with_capacity(64);
    v_gt.write_u64::<LittleEndian>(global_tid).unwrap();

    let mut v_bq = Vec::<u8>::with_capacity(128);
    v_bq.write_u64::<LittleEndian>(tm_id).unwrap();
    v_bq.write_u64::<LittleEndian>(rm_id).unwrap();

    XaTransactionId::try_new(FORMAT_ID, v_gt, v_bq).unwrap()
}

impl TransactionManager for SimpleTransactionManager {
    fn register(
        &mut self,
        mut rm: Box<dyn ResourceManager>,
        rm_id: u64,
        cleanup: bool,
    ) -> Result<(), XaError> {
        trace!("register(rm_id = {})", rm_id);
        if self.rms.get(&rm_id).is_some() {
            let errmsg = "cannot register with given rm_id, which is already in use";
            debug!("{}", errmsg);
            return Err(XaError::Usage(errmsg));
        }

        if cleanup {
            trace!("register(rm_id = {}) -> starting cleanup", rm_id);
            for xid in &(*rm).recover().unwrap_or_default() {
                trace!("found xid {:?}", xid);
                if self.is_my_xid_and_rm(xid, rm_id) {
                    trace!("trying to forget {:?}", xid);
                    (*rm).forget(xid).unwrap_or(ReturnCode::Ok);
                }
            }
        }

        self.rms.insert(rm_id, rm);
        Ok(())
    }

    fn unregister(&mut self, rm_id: u64) -> Result<(), XaError> {
        self.rms.remove(&rm_id);
        Ok(())
    }

    // Creates a new Global Transaction and tells all rms to start working for a
    // respective branch.
    //
    // TM must be in status TmStatus::Idle.
    // If successful, sets status to TmStatus::Active.
    // If not,???
    fn start_transaction(&mut self) -> Result<(), XaError> {
        trace!("start_transaction()");
        self.validate_and_set_status(
            Status::IDLE | Status::COMMITTED | Status::ROLLEDBACK,
            Status::ACTIVATING,
        )?;

        let global_tid = self.next_global_tid();

        trace!("start_transaction() -> rm_start({})", global_tid);
        match self.rm_start(global_tid) {
            Ok(()) => {
                self.current_gtid = Some(global_tid);
                self.status = Status::ACTIVE;
                return Ok(());
            }
            Err(e) => {
                trace!(
                    "start_transaction() -> rm_start({}) failed with {:?}",
                    global_tid,
                    e
                );

                trace!("start_transaction() -> rm_end_failure({})", global_tid);
                if let Err(XaError::RmErrors(v)) = self.rm_end_failure(global_tid) {
                    trace!(
                        "start_transaction() -> rm_end_failure({}) failed with {:?}",
                        global_tid,
                        v
                    );
                }

                trace!("start_transaction() -> rm_rollback({})", global_tid);
                if let Err(XaError::RmErrors(v)) = self.rm_rollback(global_tid) {
                    trace!(
                        "start_transaction() -> rm_rollback({}) failed with {:?}",
                        global_tid,
                        v
                    );
                }
            }
        }

        trace!(
            "start_transaction() -> rm_start({}), second attempt after cleanup",
            global_tid
        );
        match self.rm_start(global_tid) {
            Ok(()) => {
                self.current_gtid = Some(global_tid);
                self.status = Status::ACTIVE;
                Ok(())
            }
            Err(e) => {
                trace!(
                    "start_transaction() -> rm_start({}), second attempt failed, too",
                    global_tid
                );
                self.status = Status::IDLE;
                Err(e)
            }
        }
    }

    // Internally, does commit_one_phase if only a single RM is involved, otherwise
    // does 2PC: (end_success(), preprare(), commit() on all participating RMs)
    // Completes the transaction, if it is in state `TmStatus::Active`.
    //
    // If successful, the transaction is set to state `TmStatus::Committed`,
    // otherwise to `TmStatus::Failed` or `TmStatus::RolledBack`.
    fn commit_transaction(&mut self) -> Result<(), XaError> {
        trace!("commit()");
        let current_gtid = self.get_current_gtid()?;
        self.validate_and_set_status(Status::ACTIVE, Status::COMMITTING)?;

        // shortcut, if possible
        if self.rms.len() < 2 {
            trace!("commit() -> rm_commit_one_phase()");
            self.rm_commit_one_phase(current_gtid)?;
        } else {
            // 1. end_success()
            trace!("commit() -> rm_end_success()");
            if let Err(e) = self.rm_end_success(current_gtid) {
                trace_error(&e, current_gtid, "rm_end_success");
                self.try_rollback_after(current_gtid, "rm_end_success")?;
            }

            // 2. prepare()
            trace!("commit() -> rm_prepare()");
            if let Err(e) = self.rm_prepare(current_gtid) {
                trace_error(&e, current_gtid, "rm_prepare");
                self.try_rollback_after(current_gtid, "rm_prepare")?;
            }

            // 3. commit()
            trace!("commit() -> rm_commit()");
            if let Err(e) = self.rm_commit(current_gtid) {
                trace_error(&e, current_gtid, "rm_commit");
                self.try_rollback_after(current_gtid, "rm_commit")?;
            }
        }
        self.status = Status::COMMITTED;

        Ok(())
    }

    fn rollback_transaction(&mut self) -> Result<(), XaError> {
        trace!("rollback()");
        let current_gtid = self.get_current_gtid()?;
        match self.status {
            Status::ACTIVE => {
                trace!("rollback() ACTIVE -> rm_end_failure()");
                self.rm_end_failure(current_gtid)?;
                self.rm_rollback(current_gtid)?;
            }
            Status::PREPARED | Status::ROLLBACK_ONLY => {
                trace!("rollback() PREPARED or ROLLBACK_ONLY -> rm_rollback()");
                self.rm_rollback(current_gtid)?;
            }
            _ => {}
        }
        self.status = Status::ROLLEDBACK;
        Ok(())
    }

    fn set_transaction_rollbackonly(&mut self) -> Result<(), XaError> {
        self.validate_and_set_status(
            Status::IDLE | Status::ACTIVE | Status::PREPARED | Status::ROLLBACK_ONLY,
            Status::ROLLBACK_ONLY,
        )
    }

    fn get_status(&mut self) -> Result<Status, XaError> {
        Ok(self.status)
    }
}

impl Drop for SimpleTransactionManager {
    fn drop(&mut self) {
        trace!("Drop of SimpleTransactionManager");
        if (Status::ACTIVATING
            | Status::ACTIVE
            | Status::PREPARING
            | Status::PREPARED
            | Status::ROLLBACK_ONLY
            | Status::ROLLINGBACK)
            .contains(self.status)
        {
            let gtid = self.current_gtid.unwrap_or_default();
            self.rm_rollback(gtid).ok();
        }
    }
}