-
Notifications
You must be signed in to change notification settings - Fork 4
/
working_table.go
193 lines (154 loc) · 4.13 KB
/
working_table.go
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
package callregistry
import (
"github.com/insolar/assured-ledger/ledger-core/insolar/contract/isolation"
"github.com/insolar/assured-ledger/ledger-core/pulse"
"github.com/insolar/assured-ledger/ledger-core/reference"
"github.com/insolar/assured-ledger/ledger-core/rms"
"github.com/insolar/assured-ledger/ledger-core/vanilla/throw"
)
type WorkingTable struct {
requests [isolation.InterferenceFlagCount - 1]*WorkingList
results map[reference.Global]CallSummary
}
func NewWorkingTable() WorkingTable {
var rt WorkingTable
for i := len(rt.requests) - 1; i >= 0; i-- {
rt.requests[i] = newWorkingList()
}
rt.results = make(map[reference.Global]CallSummary)
return rt
}
func (wt WorkingTable) GetList(flag isolation.InterferenceFlag) *WorkingList {
if flag > 0 && int(flag) <= len(wt.requests) {
return wt.requests[flag-1]
}
panic(throw.IllegalValue())
}
func (wt WorkingTable) GetResults() map[reference.Global]CallSummary {
return wt.results
}
// Add adds reference.Global
// returns true if added and false if already exists
func (wt WorkingTable) Add(flag isolation.InterferenceFlag, ref reference.Global) bool {
return wt.GetList(flag).add(ref)
}
func (wt WorkingTable) SetActive(flag isolation.InterferenceFlag, ref reference.Global) bool {
if ok := wt.GetList(flag).setActive(ref); ok {
wt.results[ref] = CallSummary{}
return true
}
return false
}
func (wt WorkingTable) Finish(
flag isolation.InterferenceFlag,
ref reference.Global,
result *rms.VCallResult,
) bool {
if ok := wt.GetList(flag).finish(ref); ok {
_, ok := wt.results[ref]
if !ok {
panic(throw.IllegalState())
}
wt.results[ref] = CallSummary{Result: result}
return true
}
return false
}
func (wt *WorkingTable) Len() int {
size := 0
for _, list := range wt.requests[1:] {
size += list.Count()
}
return size
}
type WorkingRequestState int
const (
RequestUnknown WorkingRequestState = iota
RequestStarted
RequestProcessing
RequestFinished
)
type WorkingList struct {
earliestActivePulse pulse.Number
countActive int
countFinish int
requests map[reference.Global]WorkingRequestState
activity map[rms.PulseNumber]uint
}
func newWorkingList() *WorkingList {
return &WorkingList{
requests: make(map[reference.Global]WorkingRequestState),
activity: make(map[rms.PulseNumber]uint),
}
}
type CallSummary struct {
Result *rms.VCallResult
}
func (rl *WorkingList) GetState(ref reference.Global) WorkingRequestState {
return rl.requests[ref]
}
func (rl *WorkingList) add(ref reference.Global) bool {
if _, exist := rl.requests[ref]; exist {
return false
}
rl.requests[ref] = RequestStarted
return true
}
func (rl *WorkingList) setActive(ref reference.Global) bool {
if rl.requests[ref] != RequestStarted {
return false
}
rl.requests[ref] = RequestProcessing
rl.listActivity(ref.GetLocal().GetPulseNumber())
return true
}
func (rl *WorkingList) finish(ref reference.Global) bool {
state := rl.GetState(ref)
if state == RequestUnknown || state == RequestFinished {
return false
}
rl.requests[ref] = RequestFinished
rl.countFinish++
rl.delistActivity(ref.GetLocal().GetPulseNumber())
return true
}
func (rl *WorkingList) listActivity(requestPN pulse.Number) {
rl.countActive++
rl.activity[requestPN]++
if rl.earliestActivePulse == pulse.Unknown || requestPN < rl.earliestActivePulse {
rl.earliestActivePulse = requestPN
}
}
func (rl *WorkingList) delistActivity(requestPN pulse.Number) {
rl.countActive--
count := rl.activity[requestPN]
if count > 1 {
rl.activity[requestPN] = count - 1
} else {
delete(rl.activity, requestPN)
if requestPN == rl.earliestActivePulse {
rl.earliestActivePulse = rl.minActivePulse()
}
}
}
func (rl *WorkingList) minActivePulse() pulse.Number {
min := pulse.Unknown
for p := range rl.activity {
if min == pulse.Unknown || p < min {
min = p
}
}
return min
}
func (rl *WorkingList) Count() int {
return len(rl.requests)
}
func (rl *WorkingList) CountFinish() int {
return rl.countFinish
}
func (rl *WorkingList) CountActive() int {
return rl.countActive
}
func (rl *WorkingList) EarliestPulse() pulse.Number {
return rl.earliestActivePulse
}