-
Notifications
You must be signed in to change notification settings - Fork 2k
/
TransactionInfo.cs
184 lines (154 loc) · 5.56 KB
/
TransactionInfo.cs
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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Orleans.Serialization;
using Orleans.Transactions.Abstractions;
namespace Orleans.Transactions
{
[Serializable]
public class TransactionInfo : ITransactionInfo
{
public TransactionInfo()
{
this.Participants = new Dictionary<ParticipantId, AccessCounter>(ParticipantId.Comparer);
this.joined = new ConcurrentQueue<TransactionInfo>();
}
public TransactionInfo(Guid id, DateTime timeStamp, DateTime priority, bool readOnly = false)
: this()
{
this.TransactionId = id;
this.IsReadOnly = readOnly;
this.TimeStamp = timeStamp;
this.Priority = priority;
}
/// <summary>
/// Constructor used when TransactionInfo is transferred to a request
/// </summary>
/// <param name="other"></param>
public TransactionInfo(TransactionInfo other)
: this()
{
this.TransactionId = other.TransactionId;
this.IsReadOnly = other.IsReadOnly;
this.TimeStamp = other.TimeStamp;
this.Priority = other.Priority;
}
public string Id => TransactionId.ToString();
public Guid TransactionId { get; }
public DateTime TimeStamp { get; set; }
public DateTime Priority { get; set; }
public bool IsReadOnly { get; }
public byte[] OriginalException { get; set; }
// counts how many writes were done per each accessed resource
// zero means the resource was only read
public Dictionary<ParticipantId, AccessCounter> Participants { get; }
[NonSerialized]
public int PendingCalls;
[NonSerialized]
private readonly ConcurrentQueue<TransactionInfo> joined;
public ITransactionInfo Fork()
{
PendingCalls++;
return new TransactionInfo(this);
}
public void Join(ITransactionInfo x)
{
this.joined.Enqueue((TransactionInfo)x);
}
public OrleansTransactionAbortedException MustAbort(SerializationManager sm)
{
if (OriginalException != null)
{
var reader = new BinaryTokenStreamReader(OriginalException);
return sm.Deserialize<OrleansTransactionAbortedException>(reader);
}
else if (PendingCalls != 0)
{
return new OrleansOrphanCallException(TransactionId.ToString(), PendingCalls);
}
else
{
return null;
}
}
public void RecordException(Exception e, SerializationManager sm)
{
if (OriginalException == null)
{
var exception = (e as OrleansTransactionAbortedException)
?? new OrleansTransactionAbortedException(TransactionId.ToString(), e);
var writer = new BinaryTokenStreamWriter();
sm.Serialize(exception, writer);
OriginalException = writer.ToByteArray();
}
}
/// <summary>
/// Reconciles all pending calls that have join the transaction.
/// </summary>
/// <returns>true if there are no orphans, false otherwise</returns>
public void ReconcilePending()
{
TransactionInfo transactionInfo;
while (this.joined.TryDequeue(out transactionInfo))
{
Union(transactionInfo);
PendingCalls--;
}
}
private void Union(TransactionInfo other)
{
if (OriginalException == null)
{
OriginalException = other.OriginalException;
}
// Take sum of write counts
foreach (KeyValuePair<ParticipantId, AccessCounter> participant in other.Participants)
{
if (!this.Participants.TryGetValue(participant.Key, out var existing))
{
this.Participants[participant.Key] = participant.Value;
}
else
{
this.Participants[participant.Key] = existing + participant.Value;
}
}
// take max of timestamp
if (TimeStamp < other.TimeStamp)
TimeStamp = other.TimeStamp;
}
public void RecordRead(ParticipantId id, DateTime minTime)
{
this.Participants.TryGetValue(id, out AccessCounter count);
count.Reads++;
this.Participants[id] = count;
if (minTime > TimeStamp)
{
TimeStamp = minTime;
}
}
public void RecordWrite(ParticipantId id, DateTime minTime)
{
this.Participants.TryGetValue(id, out AccessCounter count);
count.Writes++;
this.Participants[id] = count;
if (minTime > TimeStamp)
{
TimeStamp = minTime;
}
}
/// <summary>
/// For verbose tracing and debugging.
/// </summary>
public override string ToString()
{
return string.Join("",
$"{TransactionId} {TimeStamp:o}",
(IsReadOnly ? " RO" : ""),
(OriginalException != null ? " Aborting" : ""),
$" {{{string.Join(" ", this.Participants.Select(kvp => $"{kvp.Key}:{kvp.Value.Reads},{kvp.Value.Writes}"))}}}"
);
}
}
}