-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathTransaction.cs
More file actions
428 lines (389 loc) · 9.53 KB
/
Transaction.cs
File metadata and controls
428 lines (389 loc) · 9.53 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
using System;
using System.Threading.Tasks;
using Waher.Events;
using Waher.Runtime.Threading;
namespace Waher.Runtime.Transactions
{
/// <summary>
/// Abstract base class for transactions.
/// </summary>
public abstract class Transaction : ITransaction
{
private readonly Guid id = Guid.NewGuid();
private TransactionState state = TransactionState.Created;
private MultiReadSingleWriteObject synchObject;
private object tag;
/// <summary>
/// Abstract base class for transactions.
/// </summary>
public Transaction()
{
this.synchObject = new MultiReadSingleWriteObject(this);
}
/// <summary>
/// Abstract base class for transactions.
/// </summary>
/// <param name="Id">ID of transaction.</param>
public Transaction(Guid Id)
: this()
{
this.id = Id;
}
/// <summary>
/// ID of transaction
/// </summary>
public Guid Id => this.id;
/// <summary>
/// Transaction state.
/// </summary>
public TransactionState State => this.state;
/// <summary>
/// Caller can use this property to tag the transaction with information.
/// </summary>
public object Tag
{
get => this.tag;
set => this.tag = value;
}
/// <summary>
/// Disposes of the transaction.
/// </summary>
public void Dispose()
{
this.synchObject?.Dispose();
this.synchObject = null;
}
/// <summary>
/// Sets a new state for the transaction object.
/// </summary>
/// <param name="NewState"></param>
private Task SetStateLocked(TransactionState NewState)
{
if (this.state != NewState)
{
this.state = NewState;
return this.Raise(this.StateChanged);
}
else
return Task.CompletedTask;
}
/// <summary>
/// Raises an event.
/// </summary>
/// <param name="EventHandler">Event handler for event.</param>
protected async Task Raise(EventHandlerAsync<TransactionEventArgs> EventHandler)
{
if (!(EventHandler is null))
await EventHandler.Raise(this, new TransactionEventArgs(this));
}
/// <summary>
/// Event raised when the transaction state has changed.
/// </summary>
public event EventHandlerAsync<TransactionEventArgs> StateChanged;
/// <summary>
/// Prepares the transaction for execution. This step can be used for validation and authorization of the transaction.
/// It must not change the underlying state.
/// </summary>
/// <returns>If the preparation phase is OK or not.</returns>
/// <exception cref="TransactionException">If the transaction not in the <see cref="TransactionState.Created"/> state.</exception>
public async Task<bool> Prepare()
{
await this.synchObject.BeginWrite();
try
{
this.AssertState(TransactionState.Created);
await this.SetStateLocked(TransactionState.Preparing);
try
{
if (await this.DoPrepare())
{
await this.SetStateLocked(TransactionState.Prepared);
try
{
await this.AfterPrepare();
}
catch (Exception ex)
{
Log.Exception(ex);
}
return true;
}
else
{
await this.SetStateLocked(TransactionState.Error);
return false;
}
}
catch (Exception ex)
{
Log.Exception(ex);
await this.SetStateLocked(TransactionState.Error);
return false;
}
}
finally
{
await this.synchObject.EndWrite();
}
}
/// <summary>
/// Performs actual preparation.
/// </summary>
/// <returns>If the preparation phase is OK or not.</returns>
protected abstract Task<bool> DoPrepare();
/// <summary>
/// Is called after preparation has completed.
/// </summary>
protected virtual Task AfterPrepare()
{
return Task.CompletedTask;
}
private void AssertState(params TransactionState[] States)
{
foreach (TransactionState State in States)
{
if (this.state == State)
return;
}
throw new TransactionException(this, "Unexpected state: " + this.state.ToString());
}
/// <summary>
/// Executes the transaction.
/// </summary>
/// <returns>If the transaction was executed correctly or not.</returns>
/// <exception cref="TransactionException">If the transaction not in the <see cref="TransactionState.Prepared"/> state.</exception>
public async Task<bool> Execute()
{
await this.synchObject.BeginWrite();
try
{
this.AssertState(TransactionState.Prepared);
await this.SetStateLocked(TransactionState.Executing);
try
{
if (await this.DoExecute())
{
await this.SetStateLocked(TransactionState.Executed);
try
{
await this.AfterExecute();
}
catch (Exception ex)
{
Log.Exception(ex);
}
return true;
}
else
{
await this.SetStateLocked(TransactionState.Error);
return false;
}
}
catch (Exception ex)
{
Log.Exception(ex);
await this.SetStateLocked(TransactionState.Error);
return false;
}
}
finally
{
await this.synchObject.EndWrite();
}
}
/// <summary>
/// Performs actual execution.
/// </summary>
/// <returns>If the transaction was executed correctly or not.</returns>
protected abstract Task<bool> DoExecute();
/// <summary>
/// Is called after execution has completed.
/// </summary>
protected virtual Task AfterExecute()
{
return Task.CompletedTask;
}
/// <summary>
/// Commits any changes made during the execution phase.
/// </summary>
/// <returns>If the transaction was successfully committed.</returns>
/// <exception cref="TransactionException">If the transaction not in the <see cref="TransactionState.Executed"/> state.</exception>
public async Task<bool> Commit()
{
await this.synchObject.BeginWrite();
try
{
this.AssertState(TransactionState.Executed);
await this.SetStateLocked(TransactionState.Committing);
try
{
if (await this.DoCommit())
{
await this.SetStateLocked(TransactionState.Committed);
try
{
await this.AfterCommit();
}
catch (Exception ex)
{
Log.Exception(ex);
}
return true;
}
else
{
await this.SetStateLocked(TransactionState.Error);
return false;
}
}
catch (Exception ex)
{
Log.Exception(ex);
await this.SetStateLocked(TransactionState.Error);
return false;
}
}
finally
{
await this.synchObject.EndWrite();
}
}
/// <summary>
/// Performs actual commit.
/// </summary>
/// <returns>If the transaction was successfully committed.</returns>
protected abstract Task<bool> DoCommit();
/// <summary>
/// Is called after preparation has completed.
/// </summary>
protected virtual Task AfterCommit()
{
return Task.CompletedTask;
}
/// <summary>
/// Rolls back any changes made during the execution phase.
/// </summary>
/// <returns>If the transaction was successfully rolled back.</returns>
/// <exception cref="TransactionException">If the transaction not in the <see cref="TransactionState.Executed"/>
/// or <see cref="TransactionState.Error"/> states.</exception>
public async Task<bool> Rollback()
{
await this.synchObject.BeginWrite();
try
{
this.AssertState(TransactionState.Executed, TransactionState.Error);
await this.SetStateLocked(TransactionState.RollingBack);
try
{
if (await this.DoRollback())
{
await this.SetStateLocked(TransactionState.RolledBack);
try
{
await this.AfterRollback();
}
catch (Exception ex)
{
Log.Exception(ex);
}
return true;
}
else
{
await this.SetStateLocked(TransactionState.Error);
return false;
}
}
catch (Exception ex)
{
Log.Exception(ex);
await this.SetStateLocked(TransactionState.Error);
return false;
}
}
finally
{
await this.synchObject.EndWrite();
}
}
/// <summary>
/// Performs actual rollback.
/// </summary>
/// <returns>If the transaction was successfully rolled back.</returns>
protected abstract Task<bool> DoRollback();
/// <summary>
/// Is called after preparation has completed.
/// </summary>
protected virtual Task AfterRollback()
{
return Task.CompletedTask;
}
/// <summary>
/// Calls <paramref name="Action"/> when the transaction is idle.
/// </summary>
/// <param name="Action">Callback method.</param>
public async Task WhenIdle(Func<Task> Action)
{
await this.synchObject.BeginWrite();
try
{
await Action();
}
finally
{
await this.synchObject.EndWrite();
}
}
/// <summary>
/// Aborts the transaction.
/// </summary>
public async Task Abort()
{
await this.synchObject.BeginWrite();
try
{
switch (this.state)
{
case TransactionState.Created:
case TransactionState.Preparing:
case TransactionState.Prepared:
break;
case TransactionState.Executing:
case TransactionState.Executed:
case TransactionState.Error:
await this.SetStateLocked(TransactionState.RollingBack);
try
{
await this.DoRollback();
await this.SetStateLocked(TransactionState.RolledBack);
try
{
await this.AfterRollback();
}
catch (Exception ex)
{
Log.Exception(ex);
}
}
catch (Exception ex)
{
Log.Exception(ex);
await this.SetStateLocked(TransactionState.Error);
}
break;
case TransactionState.Committing:
case TransactionState.Committed:
break;
case TransactionState.RollingBack:
case TransactionState.RolledBack:
break;
}
}
finally
{
await this.synchObject.EndWrite();
}
}
}
}