-
Notifications
You must be signed in to change notification settings - Fork 202
/
outport.go
403 lines (325 loc) · 9.87 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package outport
import (
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/multiversx/mx-chain-core-go/core/check"
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
config outportcore.OutportConfig
}
// NewOutport will create a new instance of proxy
func NewOutport(retrialInterval time.Duration, cfg outportcore.OutportConfig) (*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,
config: cfg,
}, nil
}
// SaveBlock will save block for every driver
func (o *outport) SaveBlock(args *outportcore.OutportBlockWithHeaderAndBody) error {
o.mutex.RLock()
defer o.mutex.RUnlock()
if args == nil {
return fmt.Errorf("outport.SaveBlock error: %w", errNilSaveBlockArgs)
}
for _, driver := range o.drivers {
blockData, err := prepareBlockData(args.HeaderDataWithBody, driver)
if err != nil {
return err
}
args.OutportBlock.BlockData = blockData
o.saveBlockBlocking(args.OutportBlock, driver)
}
return nil
}
func prepareBlockData(
headerBodyData *outportcore.HeaderDataWithBody,
driver Driver,
) (*outportcore.BlockData, error) {
if headerBodyData == nil {
return nil, fmt.Errorf("outport.prepareBlockData error: %w", errNilHeaderAndBodyArgs)
}
marshaller := driver.GetMarshaller()
headerBytes, headerType, err := outportcore.GetHeaderBytesAndType(marshaller, headerBodyData.Header)
if err != nil {
return nil, err
}
body, err := outportcore.GetBody(headerBodyData.Body)
if err != nil {
return nil, err
}
return &outportcore.BlockData{
ShardID: headerBodyData.Header.GetShardID(),
HeaderBytes: headerBytes,
HeaderType: string(headerType),
HeaderHash: headerBodyData.HeaderHash,
Body: body,
IntraShardMiniBlocks: headerBodyData.IntraShardMiniBlocks,
}, nil
}
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.OutportBlock, 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(headerDataWithBody *outportcore.HeaderDataWithBody) error {
o.mutex.RLock()
defer o.mutex.RUnlock()
for _, driver := range o.drivers {
blockData, err := prepareBlockData(headerDataWithBody, driver)
if err != nil {
return err
}
o.revertIndexedBlockBlocking(blockData, driver)
}
return nil
}
func (o *outport) revertIndexedBlockBlocking(blockData *outportcore.BlockData, driver Driver) {
ch := o.monitorCompletionOnDriver("revertIndexedBlockBlocking", driver)
defer close(ch)
for {
err := driver.RevertIndexedBlock(blockData)
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.RoundsInfo) {
o.mutex.RLock()
defer o.mutex.RUnlock()
for _, driver := range o.drivers {
o.saveRoundsInfoBlocking(roundsInfo, driver)
}
}
func (o *outport) saveRoundsInfoBlocking(roundsInfo *outportcore.RoundsInfo, 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 *outportcore.ValidatorsPubKeys) {
o.mutex.RLock()
defer o.mutex.RUnlock()
for _, driver := range o.drivers {
o.saveValidatorsPubKeysBlocking(validatorsPubKeys, driver)
}
}
func (o *outport) saveValidatorsPubKeysBlocking(validatorsPubKeys *outportcore.ValidatorsPubKeys, driver Driver) {
ch := o.monitorCompletionOnDriver("saveValidatorsPubKeysBlocking", driver)
defer close(ch)
for {
err := driver.SaveValidatorsPubKeys(validatorsPubKeys)
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(validatorsRating *outportcore.ValidatorsRating) {
o.mutex.RLock()
defer o.mutex.RUnlock()
for _, driver := range o.drivers {
o.saveValidatorsRatingBlocking(validatorsRating, driver)
}
}
func (o *outport) saveValidatorsRatingBlocking(validatorsRating *outportcore.ValidatorsRating, driver Driver) {
ch := o.monitorCompletionOnDriver("saveValidatorsRatingBlocking", driver)
defer close(ch)
for {
err := driver.SaveValidatorsRating(validatorsRating)
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(accounts *outportcore.Accounts) {
o.mutex.RLock()
defer o.mutex.RUnlock()
for _, driver := range o.drivers {
o.saveAccountsBlocking(accounts, driver)
}
}
func (o *outport) saveAccountsBlocking(accounts *outportcore.Accounts, driver Driver) {
ch := o.monitorCompletionOnDriver("saveAccountsBlocking", driver)
defer close(ch)
for {
err := driver.SaveAccounts(accounts)
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(finalizedBlock *outportcore.FinalizedBlock) {
o.mutex.RLock()
defer o.mutex.RUnlock()
for _, driver := range o.drivers {
o.finalizedBlockBlocking(finalizedBlock, driver)
}
}
func (o *outport) finalizedBlockBlocking(finalizedBlock *outportcore.FinalizedBlock, driver Driver) {
ch := o.monitorCompletionOnDriver("finalizedBlockBlocking", driver)
defer close(ch)
for {
err := driver.FinalizedBlock(finalizedBlock)
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
}
callback := func() error {
return driver.SetCurrentSettings(o.config)
}
err := driver.RegisterHandler(callback, outportcore.TopicSettings)
if err != nil {
return err
}
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
}