/// <summary> /// Commits a transaction /// Initialize and recover from previous failure if necessary. /// See document for Init for details /// Use new thread to do two phrase commit on the transaction /// /// </summary> /// <param name="context"></param> /// <param name="rms"></param> /// <returns></returns> public static CommitedTransaction Commit(Transaction context, ResourceManagerList rms) { StartUp(); CommitedTransaction trans = new CommitedTransaction(context, rms); lock (syncRoot) { if (!committedTransactions.ContainsKey(context)) { committedTransactions.Add(context, trans); } else { trans = committedTransactions[context]; } } ThreadPool.QueueUserWorkItem(o => { if (trans.State < CommitState.Committed) { trans.State = CommitState.Committed; trans.StartCommit(PrepareFail, CommitFail); } }); return trans; }
public void CommitRecoveryTest() { string log = "12345678-1234-1234-1234-123456789012\tCommitted\ttest4,\r12345678-1234-1234-1234-123456789013\tPrepared\ttest1,\r12345678-1234-1234-1234-123456789014\tNone\ttest2,\r12345678-1234-1234-1234-123456789012\tRollbacked\ttest3,"; using (StreamWriter sw = new StreamWriter(TwoPhaseCommit_Accessor.LogFileName)) { sw.Write(log); } TwoPhaseCommit_Accessor.isInitialized = false; Transaction context = new Transaction(); ResourceManagerList rms = new ResourceManagerList(MyRMTest.MockRM()); CommitedTransaction actual; actual = TwoPhaseCommit.Commit(context, rms); if (!actual.DoneEvent.WaitOne(2000)) { Assert.Fail("Timeout in commit"); } using (StreamReader sr = new StreamReader(TwoPhaseCommit_Accessor.LogFileName)) { log = sr.ReadToEnd(); } string expected = "12345678-1234-1234-1234-123456789012\tRollbacked\ttest4,\r12345678-1234-1234-1234-123456789013\tDone\ttest1,\r" + context.Id.ToString("d") + "\tDone\ttest,\r"; Assert.AreEqual(expected, log); }
/// <summary> /// Commits a transaction /// Initialize and recover from previous failure if necessary. /// See document for Init for details /// Use new thread to do two phrase commit on the transaction /// /// </summary> /// <param name="context"></param> /// <param name="rms"></param> /// <returns></returns> public static CommitedTransaction Commit(Transaction context, ResourceManagerList rms) { StartUp(); CommitedTransaction trans = new CommitedTransaction(context, rms); lock (syncRoot) { if (!committedTransactions.ContainsKey(context)) { committedTransactions.Add(context, trans); } else { trans = committedTransactions[context]; } } ThreadPool.QueueUserWorkItem(o => { if (trans.State < CommitState.Committed) { trans.State = CommitState.Committed; trans.StartCommit(PrepareFail, CommitFail); } }); return(trans); }
public void StartCommitTest() { Transaction context = new Transaction(); ResourceManagerList rms = new ResourceManagerList(MyRMTest.MockRM()); rms.Add(MyRMTest.MockRM()); CommitedTransaction_Accessor target = new CommitedTransaction_Accessor(context, rms); CommitedTransaction_Accessor.stepTimeout = 1000; target.StartCommit(); Assert.IsTrue(target.DoneEvent.WaitOne(1000)); }
public CommitedTransaction(Transaction context, ResourceManagerList rms) { this.ResouceManagers = rms; this.Context = context; this.State = CommitState.None; this.rmCommitStates = new List<CommitState>(); for (int i = 0; i < rms.ResourceManagers.Count; i++) { rmCommitStates.Add(CommitState.None); } }
public CommitedTransaction(Transaction context, ResourceManagerList rms) { this.ResouceManagers = rms; this.Context = context; this.State = CommitState.None; this.rmCommitStates = new List <CommitState>(); for (int i = 0; i < rms.ResourceManagers.Count; i++) { rmCommitStates.Add(CommitState.None); } }
public void SetStateTest() { Transaction context = new Transaction(); ResourceManagerList rms = new ResourceManagerList(MyRMTest.MockRM()); rms.Add(MyRMTest.MockRM()); CommitedTransaction_Accessor target = new CommitedTransaction_Accessor(context, rms); CommitState state = CommitState.Prepared; target.SetState(0, state); Assert.IsFalse(target.stepWaitEvent.WaitOne(300)); target.SetState(1, state); Assert.IsTrue(target.stepWaitEvent.WaitOne(1000)); }
public void StartCommitTestWithOneFailPrepare() { Transaction context = new Transaction(); MockRM failRm = new MockRM(); failRm.PrepareResponse = TP.XaResponse.XAER_RMERR; ResourceManagerList rms = new ResourceManagerList(failRm); rms.Add(MyRMTest.MockRM()); CommitedTransaction_Accessor.expBackoff = 1; CommitedTransaction_Accessor target = new CommitedTransaction_Accessor(context, rms); CommitedTransaction_Accessor.stepTimeout = 100; target.State = CommitState.Committed; target.StartCommit(); Assert.AreEqual(string.Format("2PC:Prepare {0}:MockRM\r2PC:Prepare {0}:test\r2PC:Rollback {0}:MockRM\r2PC:Rollback {0}:test\r", context.Id), target.Message); }
public static CommitedTransaction FromString(string input) { string[] items = input.Split(new char[] { '\t' }, StringSplitOptions.None); Transaction trans = new Transaction(); trans.Id = new Guid(items[0]); string[] rms = items[2].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); ResourceManagerList rml = new ResourceManagerList(); foreach (string s in rms) { RM rm = new MyRM.MyRM(); rm.SetName(s); rml.Add(rm); } CommitedTransaction result = new CommitedTransaction(trans, rml); switch (items[1]) { case "None": result.State = CommitState.None; break; case "Committed": result.State = CommitState.Committed; break; case "Prepared": result.State = CommitState.Prepared; break; case "Done": result.State = CommitState.Done; break; } for (int i = 0; i < result.rmCommitStates.Count; i++) { result.rmCommitStates[i] = result.State; } return(result); }
public void CommitTestWithoutOldLog() { File.Delete(TwoPhaseCommit_Accessor.LogFileName); Transaction context = new Transaction(); ResourceManagerList rms = new ResourceManagerList(MyRMTest.MockRM()); CommitedTransaction actual; actual = TwoPhaseCommit.Commit(context, rms); if (!actual.DoneEvent.WaitOne(1000)) { Assert.Fail("Timeout in commit"); } string log; using (var sr = new StreamReader(TwoPhaseCommit_Accessor.LogFileName)) { log = sr.ReadToEnd(); } string expected = context.Id.ToString("d") + "\tDone\ttest,\r"; Assert.IsTrue(log.Contains(expected)); }
public static CommitedTransaction FromString(string input) { string[] items = input.Split(new char[] { '\t' }, StringSplitOptions.None); Transaction trans = new Transaction(); trans.Id = new Guid(items[0]); string[] rms = items[2].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); ResourceManagerList rml = new ResourceManagerList(); foreach (string s in rms) { RM rm = new MyRM.MyRM(); rm.SetName(s); rml.Add(rm); } CommitedTransaction result = new CommitedTransaction(trans, rml); switch (items[1]) { case "None": result.State = CommitState.None; break; case "Committed": result.State = CommitState.Committed; break; case "Prepared": result.State = CommitState.Prepared; break; case "Done": result.State = CommitState.Done; break; } for (int i = 0; i < result.rmCommitStates.Count; i++) { result.rmCommitStates[i] = result.State; } return result; }
public void ToStringTest() { Transaction context = new Transaction(); ResourceManagerList rms = new ResourceManagerList(MyRMTest.MockRM()); rms.Add(MyRMTest.MockRM()); CommitedTransaction_Accessor target = new CommitedTransaction_Accessor(context, rms); string expected = context.Id.ToString() + "\tNone\ttest,test,"; string actual; actual = target.ToString(); Assert.AreEqual(expected, actual); }