|
5 | 5 | import group25.Utils.XMLPersistor; |
6 | 6 | import static group25.Utils.AnsiColors.RED; |
7 | 7 | import static group25.Utils.AnsiColors.BLUE; |
| 8 | +import static group25.Utils.AnsiColors.GREEN; |
8 | 9 |
|
9 | 10 | import java.util.*; |
10 | 11 |
|
@@ -62,78 +63,80 @@ private void crashIf(CrashMode cm) { |
62 | 63 | } |
63 | 64 |
|
64 | 65 | public void vote(int xid) throws RemoteException { |
65 | | - System.out.println(getName() + " Voting"); |
66 | | - crashIf(CrashMode.RM_BEFORE_DECIDING_VOTE); |
| 66 | + System.out.println(getName() + " got vote request."); |
| 67 | + |
| 68 | + // do this all in a new thread since we want vote() to return immediately in the TM |
67 | 69 | new Thread(() -> { |
| 70 | + crashIf(CrashMode.RM_BEFORE_DECIDING_VOTE); |
68 | 71 | if (!transactionExists(xid)) { |
69 | 72 | try { |
70 | | - // TODO log ABORT maybe |
| 73 | + // TODO log abort |
71 | 74 | crashIf(CrashMode.RM_AFTER_DECIDING_VOTE); |
| 75 | + System.out.println("Transaction not found. Voting no."); |
72 | 76 | middlewareRM.receiveVote(xid, false, this.m_name); |
73 | | - abort(xid); |
74 | 77 | crashIf(CrashMode.RM_AFTER_VOTING); |
75 | | - return; |
76 | 78 | } catch (RemoteException e) { |
77 | | - // TODO handle this |
78 | | - e.printStackTrace(); |
| 79 | + System.out.println("Could not send vote to TM"); |
79 | 80 | } |
80 | 81 | return; |
81 | 82 | } |
82 | 83 |
|
83 | | - // get global lock |
84 | 84 | boolean gotLock = globalLock.lock(xid); |
85 | 85 | if (!gotLock) { |
86 | 86 | try { |
87 | | - // TODO log ABORT maybe |
| 87 | + // TODO log abort |
88 | 88 | crashIf(CrashMode.RM_AFTER_DECIDING_VOTE); |
89 | | - new Thread(() -> { |
90 | | - try { |
91 | | - middlewareRM.receiveVote(xid, false, this.m_name); |
92 | | - } catch (Exception e) { |
93 | | - } |
94 | | - }).start(); |
95 | | - |
96 | 89 | abort(xid); |
| 90 | + System.out.println("Could not get global RM persistence lock. Voting no."); |
| 91 | + middlewareRM.receiveVote(xid, false, this.m_name); |
97 | 92 | crashIf(CrashMode.RM_AFTER_VOTING); |
98 | 93 | } catch (RemoteException e) { |
99 | | - // TODO handle this |
100 | | - e.printStackTrace(); |
| 94 | + System.out.println("Could not send vote to TM"); |
101 | 95 | } |
| 96 | + return; |
102 | 97 | } |
103 | 98 |
|
| 99 | + // all good, we will vote YES |
104 | 100 | updateThenPersistGlobalState(xid); |
105 | | - // TODO log YES |
106 | | - crashIf(CrashMode.RM_AFTER_DECIDING_VOTE); |
107 | | - new Thread(() -> { |
| 101 | + |
| 102 | + // TODO log YES |
| 103 | + crashIf(CrashMode.RM_AFTER_DECIDING_VOTE); |
| 104 | + |
| 105 | + // uncertainty phase. Send Yes, and wait for decision |
| 106 | + boolean sentVote = false; |
| 107 | + new Thread(() -> { |
| 108 | + System.out.println(GREEN.colorString("Voting yes.")); |
| 109 | + try { |
| 110 | + middlewareRM.receiveVote(xid, true, this.m_name); |
| 111 | + return; |
| 112 | + } catch (InvalidTransactionException ite) { |
| 113 | + System.out.println("TM says invalid transaction. Abort."); |
108 | 114 | try { |
109 | | - middlewareRM.receiveVote(xid, true, this.m_name); |
110 | | - } |
111 | | - catch (InvalidTransactionException ite) { |
112 | | - System.out.println("Invalid transaction! Cannot vote."); |
| 115 | + abort(xid); |
| 116 | + return; |
| 117 | + } catch (RemoteException e) { /* can't happen, do nothing */ } |
| 118 | + } catch (RemoteException re) { |
| 119 | + System.out.println("Could not send vote to Coordinator. Sending again."); |
| 120 | + while (true) { // keep sending indefinitely |
113 | 121 | try { |
114 | | - abort(xid); |
115 | | - } catch (RemoteException e) { } |
116 | | - } catch (RemoteException e) { |
117 | | - System.out.println("Could not send vote request to Coordinator. Sending again."); |
118 | | - while (true) { |
119 | | - try { |
120 | | - middlewareRM.receiveVote(xid, true, this.m_name); |
121 | | - break; |
122 | | - } catch (InvalidTransactionException ee) { |
123 | | - try { |
124 | | - abort(xid); |
125 | | - break; |
126 | | - } catch (RemoteException e1) { } |
127 | | - } catch (RemoteException eee) { |
128 | | - System.out.println("Could not send vote request to Coordinator. Sending again."); |
129 | | - } |
| 122 | + System.out.println(GREEN.colorString("Try again to vote yes.")); |
| 123 | + middlewareRM.receiveVote(xid, true, this.m_name); |
| 124 | + return; |
| 125 | + } catch (InvalidTransactionException ite) { |
| 126 | + System.out.println("Invalid transaction! Abort."); |
130 | 127 | try { |
| 128 | + abort(xid); |
| 129 | + return; |
| 130 | + } catch (RemoteException e) { } |
| 131 | + } catch (RemoteException re2) { |
| 132 | + System.out.println("Could not send vote request to Coordinator. Sending again."); |
| 133 | + } |
| 134 | + try { |
131 | 135 | Thread.sleep(2000); |
132 | 136 | } catch (InterruptedException e1) { /* do nothing */} |
133 | | - } |
134 | 137 | } |
135 | | - }).start(); |
136 | | - crashIf(CrashMode.RM_AFTER_VOTING); |
| 138 | + } |
| 139 | + }).start(); |
137 | 140 | }).start(); |
138 | 141 | } |
139 | 142 |
|
|
0 commit comments