-
Notifications
You must be signed in to change notification settings - Fork 202
/
outport.go
346 lines (278 loc) · 8.58 KB
/
outport.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
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
package outport
import (
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-core-go/data"
outportcore "github.com/multiversx/mx-chain-core-go/data/outport"
logger "github.com/multiversx/mx-chain-logger-go"
)
var log = logger.GetOrCreate("outport")
const maxTimeForDriverCall = time.Second * 30
const minimumRetrialInterval = time.Millisecond * 10
type outport struct {
mutex sync.RWMutex
drivers []Driver
retrialInterval time.Duration
chanClose chan struct{}
logHandler func(logLevel logger.LogLevel, message string, args ...interface{})
timeForDriverCall time.Duration
messageCounter uint64
}
// NewOutport will create a new instance of proxy
func NewOutport(retrialInterval time.Duration) (*outport, error) {
if retrialInterval < minimumRetrialInterval {
return nil, fmt.Errorf("%w, provided: %d, minimum: %d", ErrInvalidRetrialInterval, retrialInterval, minimumRetrialInterval)
}
return &outport{
drivers: make([]Driver, 0),
mutex: sync.RWMutex{},
retrialInterval: retrialInterval,
chanClose: make(chan struct{}),
logHandler: log.Log,
timeForDriverCall: maxTimeForDriverCall,
}, nil
}
// SaveBlock will save block for every driver
func (o *outport) SaveBlock(args *outportcore.ArgsSaveBlockData) {
o.mutex.RLock()
defer o.mutex.RUnlock()
for _, driver := range o.drivers {
o.saveBlockBlocking(args, driver)
}
}
func (o *outport) monitorCompletionOnDriver(function string, driver Driver) chan struct{} {
counter := atomic.AddUint64(&o.messageCounter, 1)
o.logHandler(logger.LogDebug, "outport.monitorCompletionOnDriver starting",
"function", function, "driver", driverString(driver), "message counter", counter)
ch := make(chan struct{})
go func(startTime time.Time) {
timer := time.NewTimer(o.timeForDriverCall)
select {
case <-ch:
o.logHandler(logger.LogDebug, "outport.monitorCompletionOnDriver ended",
"function", function, "driver", driverString(driver), "message counter", counter, "time", time.Since(startTime))
case <-timer.C:
o.logHandler(logger.LogWarning, "outport.monitorCompletionOnDriver took too long",
"function", function, "driver", driverString(driver), "message counter", counter, "time", o.timeForDriverCall)
}
timer.Stop()
}(time.Now())
return ch
}
func (o *outport) saveBlockBlocking(args *outportcore.ArgsSaveBlockData, driver Driver) {
ch := o.monitorCompletionOnDriver("saveBlockBlocking", driver)
defer close(ch)
for {
err := driver.SaveBlock(args)
if err == nil {
return
}
log.Error("error calling SaveBlock, will retry",
"driver", driverString(driver),
"retrial in", o.retrialInterval,
"error", err)
if o.shouldTerminate() {
return
}
}
}
func (o *outport) shouldTerminate() bool {
select {
case <-o.chanClose:
return true
case <-time.After(o.retrialInterval):
return false
}
}
// RevertIndexedBlock will revert block for every driver
func (o *outport) RevertIndexedBlock(header data.HeaderHandler, body data.BodyHandler) {
o.mutex.RLock()
defer o.mutex.RUnlock()
for _, driver := range o.drivers {
o.revertIndexedBlockBlocking(header, body, driver)
}
}
func (o *outport) revertIndexedBlockBlocking(header data.HeaderHandler, body data.BodyHandler, driver Driver) {
ch := o.monitorCompletionOnDriver("revertIndexedBlockBlocking", driver)
defer close(ch)
for {
err := driver.RevertIndexedBlock(header, body)
if err == nil {
return
}
log.Error("error calling RevertIndexedBlock, will retry",
"driver", driverString(driver),
"retrial in", o.retrialInterval,
"error", err)
if o.shouldTerminate() {
return
}
}
}
// SaveRoundsInfo will save rounds information for every driver
func (o *outport) SaveRoundsInfo(roundsInfo []*outportcore.RoundInfo) {
o.mutex.RLock()
defer o.mutex.RUnlock()
for _, driver := range o.drivers {
o.saveRoundsInfoBlocking(roundsInfo, driver)
}
}
func (o *outport) saveRoundsInfoBlocking(roundsInfo []*outportcore.RoundInfo, driver Driver) {
ch := o.monitorCompletionOnDriver("saveRoundsInfoBlocking", driver)
defer close(ch)
for {
err := driver.SaveRoundsInfo(roundsInfo)
if err == nil {
return
}
log.Error("error calling SaveRoundsInfo, will retry",
"driver", driverString(driver),
"retrial in", o.retrialInterval,
"error", err)
if o.shouldTerminate() {
return
}
}
}
// SaveValidatorsPubKeys will save validators public keys for every driver
func (o *outport) SaveValidatorsPubKeys(validatorsPubKeys map[uint32][][]byte, epoch uint32) {
o.mutex.RLock()
defer o.mutex.RUnlock()
for _, driver := range o.drivers {
o.saveValidatorsPubKeysBlocking(validatorsPubKeys, epoch, driver)
}
}
func (o *outport) saveValidatorsPubKeysBlocking(validatorsPubKeys map[uint32][][]byte, epoch uint32, driver Driver) {
ch := o.monitorCompletionOnDriver("saveValidatorsPubKeysBlocking", driver)
defer close(ch)
for {
err := driver.SaveValidatorsPubKeys(validatorsPubKeys, epoch)
if err == nil {
return
}
log.Error("error calling SaveValidatorsPubKeys, will retry",
"driver", driverString(driver),
"retrial in", o.retrialInterval,
"error", err)
if o.shouldTerminate() {
return
}
}
}
// SaveValidatorsRating will save validators rating for every driver
func (o *outport) SaveValidatorsRating(indexID string, infoRating []*outportcore.ValidatorRatingInfo) {
o.mutex.RLock()
defer o.mutex.RUnlock()
for _, driver := range o.drivers {
o.saveValidatorsRatingBlocking(indexID, infoRating, driver)
}
}
func (o *outport) saveValidatorsRatingBlocking(indexID string, infoRating []*outportcore.ValidatorRatingInfo, driver Driver) {
ch := o.monitorCompletionOnDriver("saveValidatorsRatingBlocking", driver)
defer close(ch)
for {
err := driver.SaveValidatorsRating(indexID, infoRating)
if err == nil {
return
}
log.Error("error calling SaveValidatorsRating, will retry",
"driver", driverString(driver),
"retrial in", o.retrialInterval,
"error", err)
if o.shouldTerminate() {
return
}
}
}
// SaveAccounts will save accounts for every driver
func (o *outport) SaveAccounts(blockTimestamp uint64, acc map[string]*outportcore.AlteredAccount, shardID uint32) {
o.mutex.RLock()
defer o.mutex.RUnlock()
for _, driver := range o.drivers {
o.saveAccountsBlocking(blockTimestamp, acc, shardID, driver)
}
}
func (o *outport) saveAccountsBlocking(blockTimestamp uint64, acc map[string]*outportcore.AlteredAccount, shardID uint32, driver Driver) {
ch := o.monitorCompletionOnDriver("saveAccountsBlocking", driver)
defer close(ch)
for {
err := driver.SaveAccounts(blockTimestamp, acc, shardID)
if err == nil {
return
}
log.Error("error calling SaveAccounts, will retry",
"driver", driverString(driver),
"retrial in", o.retrialInterval,
"error", err)
if o.shouldTerminate() {
return
}
}
}
// FinalizedBlock will call all the drivers that a block is finalized
func (o *outport) FinalizedBlock(headerHash []byte) {
o.mutex.RLock()
defer o.mutex.RUnlock()
for _, driver := range o.drivers {
o.finalizedBlockBlocking(headerHash, driver)
}
}
func (o *outport) finalizedBlockBlocking(headerHash []byte, driver Driver) {
ch := o.monitorCompletionOnDriver("finalizedBlockBlocking", driver)
defer close(ch)
for {
err := driver.FinalizedBlock(headerHash)
if err == nil {
return
}
log.Error("error calling FinalizedBlock, will retry",
"driver", driverString(driver),
"retrial in", o.retrialInterval,
"error", err)
if o.shouldTerminate() {
return
}
}
}
// Close will close all the drivers that are in outport
func (o *outport) Close() error {
close(o.chanClose)
o.mutex.RLock()
defer o.mutex.RUnlock()
var err error
for _, driver := range o.drivers {
errClose := driver.Close()
if errClose != nil {
log.Error("cannot close driver", "error", errClose.Error())
err = errClose
}
}
return err
}
// HasDrivers returns true if there is at least one driver in the outport
func (o *outport) HasDrivers() bool {
o.mutex.RLock()
defer o.mutex.RUnlock()
return len(o.drivers) != 0
}
// SubscribeDriver can subscribe a driver to the outport
func (o *outport) SubscribeDriver(driver Driver) error {
if check.IfNil(driver) {
return ErrNilDriver
}
o.mutex.Lock()
o.drivers = append(o.drivers, driver)
o.mutex.Unlock()
log.Debug("outport.SubscribeDriver new driver added", "driver", driverString(driver))
return nil
}
func driverString(driver Driver) string {
return fmt.Sprintf("%T", driver)
}
// IsInterfaceNil returns true if there is no value under the interface
func (o *outport) IsInterfaceNil() bool {
return o == nil
}