|
| 1 | +using Dapper; |
| 2 | +using Microsoft.Extensions.Logging; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Data.Common; |
| 6 | +using System.Threading.Tasks; |
| 7 | + |
| 8 | +namespace Dtmcli |
| 9 | +{ |
| 10 | + public class BranchBarrier |
| 11 | + { |
| 12 | + public BranchBarrier(string transType, string gid, string branchID, string op, ILogger logger = null) |
| 13 | + { |
| 14 | + this.TransType = transType; |
| 15 | + this.Gid = gid; |
| 16 | + this.BranchID = branchID; |
| 17 | + this.Op = op; |
| 18 | + this.Logger = logger; |
| 19 | + } |
| 20 | + |
| 21 | + internal ILogger Logger { get; private set; } |
| 22 | + |
| 23 | + public string TransType { get; set; } |
| 24 | + |
| 25 | + public string Gid { get; set; } |
| 26 | + |
| 27 | + public string BranchID { get; set; } |
| 28 | + |
| 29 | + public string Op { get; set; } |
| 30 | + |
| 31 | + public int BarrierID { get; set; } |
| 32 | + |
| 33 | + public async Task Call(DbConnection db, Func<DbTransaction, Task> busiCall) |
| 34 | + { |
| 35 | + this.BarrierID = this.BarrierID + 1; |
| 36 | + var bid = this.BarrierID.ToString().PadLeft(2, '0'); |
| 37 | + |
| 38 | + // check the connection state |
| 39 | + if(db.State != System.Data.ConnectionState.Open) await db.OpenAsync(); |
| 40 | + |
| 41 | + var tx = db.BeginTransaction(); |
| 42 | + |
| 43 | + try |
| 44 | + { |
| 45 | + var originType = BarrierStatic.TypeDict.TryGetValue(this.Op, out var ot) ? ot : string.Empty; |
| 46 | + |
| 47 | + var originAffected = await DbUtils.InsertBarrier(db, this.TransType, this.Gid, this.BranchID, originType, bid, this.Op, tx); |
| 48 | + var currentAffected = await DbUtils.InsertBarrier(db, this.TransType, this.Gid, this.BranchID, this.Op, bid, this.Op, tx); |
| 49 | + |
| 50 | + Logger?.LogDebug("originAffected: {originAffected} currentAffected: {currentAffected}", originAffected, currentAffected); |
| 51 | + |
| 52 | + var isNullCompensation = IsNullCompensation(this.Op, originAffected); |
| 53 | + var isDuplicateOrPend = IsDuplicateOrPend(currentAffected); |
| 54 | + |
| 55 | + if (isNullCompensation || isDuplicateOrPend) |
| 56 | + { |
| 57 | + Logger?.LogInformation("Will not exec busiCall, isNullCompensation={isNullCompensation}, isDuplicateOrPend={isDuplicateOrPend}", isNullCompensation, isDuplicateOrPend); |
| 58 | + tx.Commit(); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + await busiCall.Invoke(tx); |
| 63 | + tx.Commit(); |
| 64 | + } |
| 65 | + catch (Exception ex) |
| 66 | + { |
| 67 | + Logger?.LogError(ex, "Call exception"); |
| 68 | + tx.Rollback(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public async Task<string> QueryPrepared(DbConnection db) |
| 73 | + { |
| 74 | + bool isErr = false; |
| 75 | + try |
| 76 | + { |
| 77 | + var tmp = DbUtils.InsertBarrier(db, this.TransType, this.Gid, "00", "msg", "01", "rollback"); |
| 78 | + } |
| 79 | + catch (Exception ex) |
| 80 | + { |
| 81 | + Logger?.LogInformation(ex, "QueryPrepared error"); |
| 82 | + isErr = true; |
| 83 | + } |
| 84 | + |
| 85 | + var reason = string.Empty; |
| 86 | + |
| 87 | + if (!isErr) |
| 88 | + { |
| 89 | + var sql = string.Format("select reason from {0} where gid=@gid and branch_id=@branch_id and op=@op and barrier_id=@barrier_id", Constant.Barrier.TABLE_NAME); |
| 90 | + |
| 91 | + reason = await db.QueryFirstOrDefaultAsync( |
| 92 | + sql, |
| 93 | + new { gid = this.Gid, branch_id = this.BranchID, op = this.Op, barrier_id = this.BarrierID }); |
| 94 | + } |
| 95 | + |
| 96 | + if (reason.Equals("rollback")) return "FAILURE"; |
| 97 | + |
| 98 | + return string.Empty; |
| 99 | + } |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// 空补偿 |
| 103 | + /// </summary> |
| 104 | + /// <param name="op"></param> |
| 105 | + /// <param name="originAffected"></param> |
| 106 | + /// <returns></returns> |
| 107 | + private bool IsNullCompensation(string op, int originAffected) |
| 108 | + => (op.Equals(Constant.BranchCancel) || op.Equals(Constant.Request.BRANCH_COMPENSATE)) && originAffected > 0; |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// 这个是重复请求或者悬挂 |
| 112 | + /// </summary> |
| 113 | + /// <param name="currentAffected"></param> |
| 114 | + /// <returns></returns> |
| 115 | + private bool IsDuplicateOrPend(int currentAffected) |
| 116 | + => currentAffected == 0; |
| 117 | + |
| 118 | + public bool IsInValid() |
| 119 | + { |
| 120 | + return string.IsNullOrWhiteSpace(this.TransType) |
| 121 | + || string.IsNullOrWhiteSpace(this.Gid) |
| 122 | + || string.IsNullOrWhiteSpace(this.BranchID) |
| 123 | + || string.IsNullOrWhiteSpace(this.Op); |
| 124 | + } |
| 125 | + |
| 126 | + public override string ToString() |
| 127 | + => $"transInfo: {TransType} {Gid} {BranchID} {Op}"; |
| 128 | + } |
| 129 | + |
| 130 | + internal class BarrierStatic |
| 131 | + { |
| 132 | + internal static readonly Dictionary<string, string> TypeDict = new Dictionary<string, string>() |
| 133 | + { |
| 134 | + { Constant.BranchCancel, Constant.BranchTry }, |
| 135 | + { Constant.Request.BRANCH_COMPENSATE, Constant.Request.BRANCH_ACTION }, |
| 136 | + }; |
| 137 | + } |
| 138 | +} |
0 commit comments