-
Notifications
You must be signed in to change notification settings - Fork 4
/
vdelegatedrequestfinished.go
263 lines (219 loc) · 7.51 KB
/
vdelegatedrequestfinished.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
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
//go:generate sm-uml-gen -f $GOFILE
package handlers
import (
"context"
"github.com/insolar/assured-ledger/ledger-core/conveyor"
"github.com/insolar/assured-ledger/ledger-core/conveyor/smachine"
"github.com/insolar/assured-ledger/ledger-core/insolar/contract/isolation"
"github.com/insolar/assured-ledger/ledger-core/log"
"github.com/insolar/assured-ledger/ledger-core/reference"
"github.com/insolar/assured-ledger/ledger-core/rms"
"github.com/insolar/assured-ledger/ledger-core/vanilla/injector"
"github.com/insolar/assured-ledger/ledger-core/vanilla/throw"
"github.com/insolar/assured-ledger/ledger-core/virtual/descriptor"
"github.com/insolar/assured-ledger/ledger-core/virtual/memorycache"
memoryCacheAdapter "github.com/insolar/assured-ledger/ledger-core/virtual/memorycache/adapter"
"github.com/insolar/assured-ledger/ledger-core/virtual/object"
)
type SMVDelegatedRequestFinished struct {
// input arguments
Meta *rms.Meta
Payload *rms.VDelegatedRequestFinished
objectSharedState object.SharedStateAccessor
objectReadyToWork smachine.SyncLink
// dependencies
objectCatalog object.Catalog
pulseSlot *conveyor.PulseSlot
memoryCache memoryCacheAdapter.MemoryCache
}
type stateIsNotReady struct {
*log.Msg `txt:"State is not ready"`
Object reference.Holder
}
type unexpectedVDelegateRequestFinished struct {
*log.Msg `txt:"Unexpected VDelegateRequestFinished"`
Object reference.Holder
Request reference.Holder
Ordered bool
}
type noLatestStateTolerableVDelegateRequestFinished struct {
*log.Msg `txt:"Tolerable VDelegateRequestFinished on Empty object has no LatestState"`
Object reference.Holder
Request reference.Holder
}
var dSMVDelegatedRequestFinishedInstance smachine.StateMachineDeclaration = &dSMVDelegatedRequestFinished{}
type dSMVDelegatedRequestFinished struct {
smachine.StateMachineDeclTemplate
}
func (*dSMVDelegatedRequestFinished) InjectDependencies(sm smachine.StateMachine, _ smachine.SlotLink, injector injector.DependencyInjector) {
s := sm.(*SMVDelegatedRequestFinished)
injector.MustInject(&s.objectCatalog)
injector.MustInject(&s.pulseSlot)
injector.MustInject(&s.memoryCache)
}
func (*dSMVDelegatedRequestFinished) GetInitStateFor(sm smachine.StateMachine) smachine.InitFunc {
s := sm.(*SMVDelegatedRequestFinished)
return s.Init
}
/* -------- Instance ------------- */
func (s *SMVDelegatedRequestFinished) GetStateMachineDeclaration() smachine.StateMachineDeclaration {
return dSMVDelegatedRequestFinishedInstance
}
func (s *SMVDelegatedRequestFinished) Init(ctx smachine.InitializationContext) smachine.StateUpdate {
if s.pulseSlot.State() != conveyor.Present {
ctx.Log().Warn("stop processing VDelegatedRequestFinished since we are not in present pulse")
return ctx.Stop()
}
ctx.SetDefaultMigration(s.migrationDefault)
return ctx.Jump(s.stepGetObject)
}
func (s *SMVDelegatedRequestFinished) migrationDefault(ctx smachine.MigrationContext) smachine.StateUpdate {
ctx.Log().Trace("stop processing SMVDelegatedRequestFinished since pulse was changed")
return ctx.Stop()
}
func (s *SMVDelegatedRequestFinished) stepGetObject(ctx smachine.ExecutionContext) smachine.StateUpdate {
s.objectSharedState = s.objectCatalog.GetOrCreate(ctx, s.Payload.Callee.GetValue())
var (
semaphoreReadyToWork smachine.SyncLink
)
action := func(state *object.SharedState) {
semaphoreReadyToWork = state.ReadyToWork
}
switch s.objectSharedState.Prepare(action).TryUse(ctx).GetDecision() {
case smachine.NotPassed:
return ctx.WaitShared(s.objectSharedState.SharedDataLink).ThenRepeat()
case smachine.Impossible:
panic(throw.NotImplemented())
case smachine.Passed:
// go further
default:
panic(throw.Impossible())
}
s.objectReadyToWork = semaphoreReadyToWork
return ctx.Jump(s.awaitObjectReady)
}
func (s *SMVDelegatedRequestFinished) awaitObjectReady(ctx smachine.ExecutionContext) smachine.StateUpdate {
if ctx.AcquireForThisStep(s.objectReadyToWork).IsNotPassed() {
return ctx.Sleep().ThenRepeat()
}
return ctx.Jump(s.stepProcess)
}
func (s *SMVDelegatedRequestFinished) stepProcess(ctx smachine.ExecutionContext) smachine.StateUpdate {
setStateFunc := func(data interface{}) (wakeup bool) {
state := data.(*object.SharedState)
if !state.IsReady() {
ctx.Log().Trace(stateIsNotReady{Object: s.Payload.Callee.GetValue()})
return false
}
s.updateSharedState(ctx, state)
return false
}
switch s.objectSharedState.PrepareAccess(setStateFunc).TryUse(ctx).GetDecision() {
case smachine.Passed:
case smachine.NotPassed:
return ctx.WaitShared(s.objectSharedState.SharedDataLink).ThenRepeat()
default:
panic(throw.Impossible())
}
s.updateMemoryCache(ctx)
return ctx.Stop()
}
func (s *SMVDelegatedRequestFinished) updateSharedState(
ctx smachine.ExecutionContext,
state *object.SharedState,
) {
var (
objectRef = s.Payload.Callee.GetValue()
requestRef = s.Payload.CallOutgoing.GetValue()
)
// Update object state.
if s.hasLatestState() {
state.SetDescriptorDirty(s.latestState())
s.updateObjectState(state)
} else if s.Payload.CallFlags.GetInterference() == isolation.CallTolerable &&
s.Payload.CallType == rms.CallTypeConstructor &&
state.GetState() == object.Empty {
ctx.Log().Warn(noLatestStateTolerableVDelegateRequestFinished{
Object: objectRef,
Request: requestRef,
})
state.SetState(object.Missing)
}
pendingList := state.PendingTable.GetList(s.Payload.CallFlags.GetInterference())
if !pendingList.Finish(requestRef) {
panic(throw.E("delegated request was not registered", struct {
Object string
Request string
}{
Object: objectRef.String(),
Request: requestRef.String(),
}))
}
switch s.Payload.CallFlags.GetInterference() {
case isolation.CallIntolerable:
if state.PreviousExecutorUnorderedPendingCount == 0 {
ctx.Log().Warn(unexpectedVDelegateRequestFinished{
Object: objectRef,
Request: requestRef,
Ordered: false,
})
}
case isolation.CallTolerable:
if state.PreviousExecutorOrderedPendingCount == 0 {
ctx.Log().Warn(unexpectedVDelegateRequestFinished{
Object: objectRef,
Request: requestRef,
Ordered: true,
})
}
if pendingList.CountActive() == 0 {
// If we do not have pending ordered, release sync object.
if !ctx.CallBargeInWithParam(state.SignalOrderedPendingFinished, nil) {
ctx.Log().Warn("SignalOrderedPendingFinished BargeIn receive false")
}
}
}
}
func (s *SMVDelegatedRequestFinished) updateObjectState(state *object.SharedState) {
switch state.GetState() {
case object.Empty:
state.SetState(object.HasState)
case object.HasState:
// ok
default:
panic(throw.Impossible())
}
}
func (s *SMVDelegatedRequestFinished) updateMemoryCache(ctx smachine.ExecutionContext) {
if !s.hasLatestState() {
return
}
objectDescriptor := s.latestState()
if objectDescriptor.IsEmpty() {
return
}
s.memoryCache.PrepareAsync(ctx, func(ctx context.Context, svc memorycache.Service) smachine.AsyncResultFunc {
err := svc.Set(ctx, objectDescriptor.State(), objectDescriptor)
return func(ctx smachine.AsyncResultContext) {
if err != nil {
ctx.Log().Error("failed to set dirty memory", err)
}
}
}).WithoutAutoWakeUp().Start()
}
func (s *SMVDelegatedRequestFinished) hasLatestState() bool {
return s.Payload.LatestState != nil
}
func (s *SMVDelegatedRequestFinished) latestState() descriptor.Object {
state := s.Payload.LatestState
if state == nil {
panic(throw.IllegalState())
}
return descriptor.NewObject(
s.Payload.Callee.GetValue(),
state.Reference.GetValue().GetLocal(),
state.Class.GetValue(),
state.Memory.GetBytes(),
state.Deactivated,
)
}