-
Notifications
You must be signed in to change notification settings - Fork 3
/
carosol.pml
225 lines (184 loc) · 6.18 KB
/
carosol.pml
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
#define PARTICIPANT_NUM 3
// Only 1 coordinator, 1 client
mtype{Active, Prepared, Committed, Aborted, VoteAbort, Failed}
chan clientChannelsFromParticipant = [1] of {byte};
chan clientChannelsFromCoordinator = [1] of {bool};
chan participantChannelsFromClient[PARTICIPANT_NUM] = [2] of {byte}; // TID
chan participantChannelsFromCoordinator[PARTICIPANT_NUM] = [1] of {bool};
chan coordinatorChannelFromClient = [1] of {byte}; // first time read: participant count; second time read: commit/abort
chan coordinatorChannelFromParticipant = [PARTICIPANT_NUM] of {bool, byte}; // decision, ID
chan coordinatorChannelAck = [PARTICIPANT_NUM] of {byte}; // to receive acknowledgement from participant
byte participant_num = 0;
mtype Coordinator_state;
mtype Client_state;
mtype Participant_state[PARTICIPANT_NUM];
mtype Recover_state[PARTICIPANT_NUM];
byte participants_involved[PARTICIPANT_NUM] = 0; // when the ith participant received a message, participant_involved[i] will be set to 1.
active proctype Coordinator(){
bool receivedDecision;
byte clientDecision;
byte participantCount;
bool finalDecision = true;
byte participantID;
Coordinator_state = Active;
do
:: Coordinator_state == Active ->
coordinatorChannelFromClient ? participantCount;
byte i = participantCount;
do
:: i > 0 ->
atomic
{
coordinatorChannelFromParticipant ? receivedDecision, participantID;
if
:: receivedDecision == false -> finalDecision = false; i--;
:: else -> i--; /* This P agrees to commit*/
fi
}
:: else -> break;
od;
Coordinator_state = Prepared;
:: Coordinator_state == Prepared ->
coordinatorChannelFromClient ? clientDecision;
if
:: clientDecision == 0 ->
finalDecision = false;
:: else -> skip;
fi
if
:: finalDecision -> Coordinator_state = Committed;
:: else -> Coordinator_state = Aborted;
fi
:: Coordinator_state == Committed || Coordinator_state == Aborted ->
clientChannelsFromCoordinator ! finalDecision;
i = 0;
do
::i < PARTICIPANT_NUM ->
if
:: participants_involved[i] -> participantChannelsFromCoordinator[i] ! finalDecision;
:: else -> skip;
fi
i++;
::else ->
break;
od;
:: len(coordinatorChannelAck) == participantCount -> break;
od;
}
active proctype Client()
{
byte i = 0;
byte numSent = 0;
byte TID = _pid;
byte receiveMsg;
bool finalDecision;
Client_state = Active;
L1: do
:: Client_state == Active;
do
:: i < PARTICIPANT_NUM ->
if
:: participantChannelsFromClient[i] ! TID;participants_involved[i] = 1; i++; numSent++;
:: i++;
fi
:: i >= PARTICIPANT_NUM -> break;
od;
coordinatorChannelFromClient ! numSent;
byte temp = numSent;
do
:: numSent > 0 ->
clientChannelsFromParticipant ? receiveMsg;
assert(receiveMsg == TID);
numSent--;
:: else -> break;
od;
if
:: coordinatorChannelFromClient ! 1;
:: coordinatorChannelFromClient ! 0; Client_state = VoteAbort; goto L1;
fi
clientChannelsFromCoordinator ? finalDecision;
if
:: finalDecision -> Client_state = Committed;
:: else -> Client_state = Aborted;
fi
break;
:: Client_state == VoteAbort;
clientChannelsFromCoordinator ? finalDecision;
assert(finalDecision == false);
Client_state = Aborted;
break;
od;
}
proctype Participant(byte id)
{
byte receiveTID;
byte receiveClientId;
participant_num++;
bool finalDecision;
atomic{
Participant_state[id] = Active;
Recover_state[id] = Participant_state[id]
}
do
:: Participant_state[id] == Active ->
atomic{
participantChannelsFromClient[id] ? receiveTID;
clientChannelsFromParticipant ! receiveTID;
if
::Participant_state[id] = Prepared; coordinatorChannelFromParticipant ! true, id;
::Participant_state[id] = VoteAbort; coordinatorChannelFromParticipant ! false, id;
fi
Recover_state[id] = Participant_state[id];
}
:: Participant_state[id] == Prepared || Participant_state[id] == VoteAbort ->
atomic{
participantChannelsFromCoordinator[id] ? finalDecision;
if
:: Participant_state[id] == Failed -> Participant_state[id] = Recover_state[id];
:: else -> skip;
fi
coordinatorChannelAck ! id;
assert(!(Participant_state[id] == VoteAbort && finalDecision));
if
:: Participant_state[id] == Prepared && finalDecision -> Participant_state[id] = Committed;
:: else -> Participant_state[id] = Aborted;
fi
Recover_state[id] = Participant_state[id];
}
:: Participant_state[id] == Failed -> Participant_state[id] = Recover_state[id];
od;
}
proctype failParticipant(byte id)
{
atomic {
// change state
Participant_state[id] = Failed;
// clear channel
byte garbage_TID;
bool garbage_decision;
do
:: nempty(participantChannelsFromClient[id]) -> participantChannelsFromClient[id] ? garbage_TID;
:: nempty(participantChannelsFromCoordinator[id]) -> participantChannelsFromCoordinator[id] ? garbage_decision;
:: empty(participantChannelsFromCoordinator[id]) && empty(participantChannelsFromClient[id]) -> break;
od;
}
}
init
{
atomic
{
byte i = 0;
do
:: i < PARTICIPANT_NUM -> run Participant(i); i++;
:: i >= PARTICIPANT_NUM -> break;
od;
//This should reset a participant(simulates a failure), and should break some invariance
run failParticipant(0);
}
}
ltl client_Committed_Co {[] ( (Client_state == Committed) -> (Coordinator_state == Committed))}
ltl client_Committed_P0_involved {[] ( (Client_state == Committed && participants_involved[0] == 1) ->
((Participant_state[0] == Committed || Participant_state[0] == Prepared || Participant_state[0] == Failed) && <> (Participant_state[0] == Committed)) )}
ltl client_Committed_P0_not_involved {[] ( (Client_state == Committed && participants_involved[0] == 0) -> (Participant_state[0] != Committed) )}
ltl client_Aborted_P0_involved {[] ( (Client_state == Aborted && participants_involved[0] == 1) -> <> (Participant_state[0] == Aborted ) )}
ltl P0_committed {[] ( (Participant_state[0] == Committed) -> ( Coordinator_state == Committed && Client_state != Aborted && <> (Client_state == Committed) ) )}