-
Notifications
You must be signed in to change notification settings - Fork 199
/
scheduledProcessor.go
222 lines (185 loc) · 6.02 KB
/
scheduledProcessor.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
package spos
import (
"sync"
"time"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go-core/data"
"github.com/ElrondNetwork/elrond-go/ntp"
"github.com/ElrondNetwork/elrond-go/process"
)
type processingStatus int
const (
processingNotStarted processingStatus = 0
processingError processingStatus = 1
inProgress processingStatus = 2
processingOK processingStatus = 3
stopped processingStatus = 4
processingNotStartedString = "processing not started"
processingErrorString = "processing error"
inProgressString = "processing in progress"
processingOKString = "processing OK"
stoppedString = "processing stopped"
unexpectedString = "unexpected"
processingCheckStep = 10 * time.Millisecond
)
// String returns human readable processing status
func (ps processingStatus) String() string {
switch ps {
case processingNotStarted:
return processingNotStartedString
case processingError:
return processingErrorString
case inProgress:
return inProgressString
case processingOK:
return processingOKString
case stopped:
return stoppedString
default:
return unexpectedString
}
}
// ScheduledProcessorWrapperArgs holds the arguments required to instantiate the pipelineExecution
type ScheduledProcessorWrapperArgs struct {
SyncTimer ntp.SyncTimer
Processor process.ScheduledBlockProcessor
RoundTimeDurationHandler process.RoundTimeDurationHandler
}
type scheduledProcessorWrapper struct {
syncTimer ntp.SyncTimer
processor process.ScheduledBlockProcessor
roundTimeDurationHandler process.RoundTimeDurationHandler
startTime time.Time
status processingStatus
sync.RWMutex
stopExecution chan struct{}
oneInstance sync.RWMutex
}
// NewScheduledProcessorWrapper creates a new processor for scheduled transactions
func NewScheduledProcessorWrapper(args ScheduledProcessorWrapperArgs) (*scheduledProcessorWrapper, error) {
if check.IfNil(args.SyncTimer) {
return nil, process.ErrNilSyncTimer
}
if check.IfNil(args.Processor) {
return nil, process.ErrNilBlockProcessor
}
if check.IfNil(args.RoundTimeDurationHandler) {
return nil, process.ErrNilRoundTimeDurationHandler
}
return &scheduledProcessorWrapper{
syncTimer: args.SyncTimer,
processor: args.Processor,
roundTimeDurationHandler: args.RoundTimeDurationHandler,
status: processingNotStarted,
stopExecution: make(chan struct{}, 1),
}, nil
}
// ForceStopScheduledExecutionBlocking forces the execution to stop
func (sp *scheduledProcessorWrapper) ForceStopScheduledExecutionBlocking() {
status := sp.getStatus()
if status != inProgress {
return
}
sp.stopExecution <- struct{}{}
for status == inProgress {
time.Sleep(processingCheckStep)
status = sp.getStatus()
}
sp.setStatus(stopped)
sp.emptyStopChannel()
}
func (sp *scheduledProcessorWrapper) waitForProcessingResultWithTimeout() processingStatus {
status := sp.getStatus()
for status == inProgress {
remainingExecutionTime := sp.computeRemainingProcessingTime()
select {
case <-time.After(remainingExecutionTime):
status = sp.getStatus()
return status
case <-time.After(processingCheckStep):
status = sp.getStatus()
}
}
return status
}
// IsProcessedOKWithTimeout returns true if the scheduled processing was finalized without error
// Function is blocking until the allotted time for processing is finished
func (sp *scheduledProcessorWrapper) IsProcessedOKWithTimeout() bool {
status := sp.waitForProcessingResultWithTimeout()
processedOK := status == processingOK
log.Debug("scheduledProcessorWrapper.IsProcessedOKWithTimeout", "status", status.String())
return processedOK
}
// StartScheduledProcessing starts the scheduled processing
func (sp *scheduledProcessorWrapper) StartScheduledProcessing(header data.HeaderHandler, body data.BodyHandler, startTime time.Time) {
if !header.HasScheduledSupport() {
log.Debug("scheduled processing not supported")
sp.setStatus(processingOK)
return
}
sp.oneInstance.Lock()
sp.setStatus(inProgress)
log.Debug("scheduledProcessorWrapper.StartScheduledProcessing - scheduled processing has been started")
go func() {
defer sp.oneInstance.Unlock()
errSchExec := sp.processScheduledMiniBlocks(header, body, startTime)
if errSchExec != nil {
log.Debug("scheduledProcessorWrapper.processScheduledMiniBlocks",
"err", errSchExec.Error())
sp.setStatus(processingError)
return
}
log.Debug("scheduledProcessorWrapper.StartScheduledProcessing - scheduled processing has finished OK")
sp.setStatus(processingOK)
}()
}
// IsInterfaceNil returns true if there is no value under the interface
func (sp *scheduledProcessorWrapper) IsInterfaceNil() bool {
return sp == nil
}
func (sp *scheduledProcessorWrapper) emptyStopChannel() {
for {
select {
case <-sp.stopExecution:
default:
return
}
}
}
func (sp *scheduledProcessorWrapper) computeRemainingProcessingTimeStoppable() time.Duration {
select {
case <-sp.stopExecution:
return 0
default:
return sp.computeRemainingProcessingTime()
}
}
func (sp *scheduledProcessorWrapper) computeRemainingProcessingTime() time.Duration {
currTime := sp.syncTimer.CurrentTime()
sp.RLock()
elapsedTime := currTime.Sub(sp.startTime)
sp.RUnlock()
if elapsedTime < 0 {
return 0
}
return sp.roundTimeDurationHandler.TimeDuration() - elapsedTime
}
func (sp *scheduledProcessorWrapper) getStatus() processingStatus {
sp.RLock()
defer sp.RUnlock()
return sp.status
}
func (sp *scheduledProcessorWrapper) setStatus(status processingStatus) {
sp.Lock()
defer sp.Unlock()
sp.status = status
}
func (sp *scheduledProcessorWrapper) processScheduledMiniBlocks(header data.HeaderHandler, body data.BodyHandler, startTime time.Time) error {
sp.Lock()
sp.startTime = startTime
sp.Unlock()
haveTime := func() time.Duration {
return sp.computeRemainingProcessingTimeStoppable()
}
return sp.processor.ProcessScheduledBlock(header, body, haveTime)
}