/// <summary> /// TM dies before receiving Done from all RMs, on recovery, TM should recommit /// </summary> public void TMFailsBeforeReceivingAllDone2PC() { StartUp(); Transaction t = WorkflowControl.Start(); TransactionManager.SelfDestruct(false, true); WorkflowControl.AddCars(t, "Seattle", 10, 100); ThreadPool.QueueUserWorkItem(o => WorkflowControl.Commit(t)); // TM shall be kill, now restart it Console.Write("TM is killed, next is to restart it"); Pause(); StartTM(); // Wait to make sure all recover job is done // shall see the transaction recommits. t = WorkflowControl.Start(); string actual = PrintCars(); Assert.AreEqual("Vegas,1,3;NewYork,10,30;", actual); actual = PrintRooms(); Assert.AreEqual("Vegas,2,1;NewYork,20,10;", actual); actual = PrintFlights(); Assert.AreEqual("SEA->LAX,10,2;LAX->LAV,12,3;SEA->JFK,8,4;", actual); actual = PrintCustomers(); Assert.AreEqual("", actual); }
/// <summary> /// Add an itinerary, kill RM before commit. Restart the RMs and you can see nothing changed (still c1 state). Demo shadow copy works after failure /// </summary> public void UncommittedScenario() { StartUp(); Console.WriteLine("Flight RM shall terminate before transaction commits. "); Customer c = new Customer("12345678-1234-1234-1234-123456789012"); FlightsRM.SelfDestruct(2, 0, 0, 0); string[] flights = new string[] { "SEA->LAX", "LAX->LAV" }; try { WorkflowControl.ReserveItinerary(c, flights, "Vegas", true, true); } catch (WebException e) { // Expected error because one of the RM will be selfdestroyed. Console.WriteLine(e.Message); } Console.WriteLine("Restarting flight RM"); StartFlightsRM(); Transaction t = WorkflowControl.Start(); string actual = WorkflowControl.QueryItinerary(t, c); Console.WriteLine(actual); Assert.AreEqual("", actual); actual = PrintCars(); Assert.AreEqual("Vegas,1,3;NewYork,10,30;", actual); actual = PrintRooms(); Assert.AreEqual("Vegas,2,1;NewYork,20,10;", actual); actual = PrintFlights(); Assert.AreEqual("SEA->LAX,10,2;LAX->LAV,12,3;SEA->JFK,8,4;", actual); actual = PrintCustomers(); Assert.AreEqual("", actual); }
/// <summary> /// All RM prepared, one RM dies before receiving Commit, on recovery, the RM should recover the transaction, WC shouldn’t notice this /// </summary> public void ReCommitAfterRmFailTransaction2PC() { StartUp(); CarsRM.SelfDestruct(0, 0, 1, 0); Customer c = new Customer("12345678-1234-1234-1234-123456789012"); string[] flights = new string[] { "SEA->LAX", "LAX->LAV" }; ThreadPool.QueueUserWorkItem(o => { Thread.Sleep(1000); CarsRM.SelfDestruct(0, 0, 0, 0); }); WorkflowControl.ReserveItinerary(c, flights, "Vegas", true, true); // We shall see some commit retry message from TM but the transaction shall success. Transaction t = WorkflowControl.Start(); string actual = WorkflowControl.QueryItinerary(t, c); Console.WriteLine(actual); Assert.AreEqual("F:SEA->LAX,F:LAX->LAV,C:Vegas,R:Vegas", actual); actual = PrintCars(); Assert.AreEqual("Vegas,0,3;NewYork,10,30;", actual); actual = PrintRooms(); Assert.AreEqual("Vegas,1,1;NewYork,20,10;", actual); actual = PrintFlights(); Assert.AreEqual("SEA->LAX,9,2;LAX->LAV,11,3;SEA->JFK,8,4;", actual); actual = PrintCustomers(); Assert.AreEqual("12345678-1234-1234-1234-123456789012;", actual); }
/// <summary> /// All RMs returns prepared, except one fails to prepare, the transaction should abort /// </summary> public void RollbackAfterRmFailTransaction2PC() { StartUp(); CarsRM.SelfDestruct(0, 1, 0, 0); Customer c = new Customer("12345678-1234-1234-1234-123456789012"); string[] flights = new string[] { "SEA->LAX", "LAX->LAV" }; WorkflowControl.ReserveItinerary(c, flights, "Vegas", true, true); // We shall see some rollback message from TM. Console.WriteLine("One RM failed at prepare. TM will rollback all prepared RMs"); Pause(); Transaction t = WorkflowControl.Start(); string actual = WorkflowControl.QueryItinerary(t, c); Console.WriteLine(actual); Assert.AreEqual("", actual); actual = PrintCars(); Assert.AreEqual("Vegas,1,3;NewYork,10,30;", actual); actual = PrintRooms(); Assert.AreEqual("Vegas,2,1;NewYork,20,10;", actual); actual = PrintFlights(); Assert.AreEqual("SEA->LAX,10,2;LAX->LAV,12,3;SEA->JFK,8,4;", actual); actual = PrintCustomers(); Assert.AreEqual("", actual); }
/// <summary> /// T1, T2 write R1, T1 commit, T2 abort, see only T1's change in. Demo abort. /// </summary> public void ConcurrentCommitAbort() { StartUp(); Transaction t1 = WorkflowControl.Start(); Transaction t2 = WorkflowControl.Start(); AutoResetEvent sync1 = new AutoResetEvent(false); AutoResetEvent sync2 = new AutoResetEvent(false); ThreadPool.QueueUserWorkItem(o => { WorkflowControl.AddCars(t1, "Seattle", 1, 3); WorkflowControl.Commit(t1); sync1.Set(); }); ThreadPool.QueueUserWorkItem(o => { WorkflowControl.AddCars(t2, "Seattle", 2, 2); WorkflowControl.Abort(t2); sync2.Set(); }); // here is just to make sure t2 commits before t1, so the price will be 2. sync1.WaitOne(); sync2.WaitOne(); string actual = PrintCars(); Assert.AreEqual("Vegas,1,3;NewYork,10,30;Seattle,1,3;", actual); }
private void InitInventory() { Transaction t = WorkflowControl.Start(); WorkflowControl.AddCars(t, "Vegas", 1, 3); WorkflowControl.AddRooms(t, "Vegas", 2, 1); WorkflowControl.AddCars(t, "NewYork", 10, 30); WorkflowControl.AddRooms(t, "NewYork", 20, 10); WorkflowControl.AddSeats(t, "SEA->LAX", 10, 2); WorkflowControl.AddSeats(t, "LAX->LAV", 12, 3); WorkflowControl.AddSeats(t, "SEA->JFK", 8, 4); WorkflowControl.Commit(t); }
/// <summary> /// Submit an itinerary with car, flight and hotel. Show the values are updated. Demo Read, write and commit. /// </summary> public void AddOneItinerary() { StartUp(); Customer c = new Customer("12345678-1234-1234-1234-123456789012"); string[] flights = new string[] { "SEA->LAX", "LAX->LAV" }; WorkflowControl.ReserveItinerary(c, flights, "Vegas", true, true); Transaction t = WorkflowControl.Start(); string actual = WorkflowControl.QueryItinerary(t, c); Console.WriteLine(actual); Assert.AreEqual("F:SEA->LAX,F:LAX->LAV,C:Vegas,R:Vegas", actual); actual = PrintCars(); Assert.AreEqual("Vegas,0,3;NewYork,10,30;", actual); actual = PrintRooms(); Assert.AreEqual("Vegas,1,1;NewYork,20,10;", actual); actual = PrintFlights(); Assert.AreEqual("SEA->LAX,9,2;LAX->LAV,11,3;SEA->JFK,8,4;", actual); actual = PrintCustomers(); Assert.AreEqual("12345678-1234-1234-1234-123456789012;", actual); }