forked from OffchainLabs/nitro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_das_reader_aggregator.go
377 lines (334 loc) · 13.3 KB
/
simple_das_reader_aggregator.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
// Copyright 2021-2022, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE
package das
import (
"context"
"errors"
"fmt"
"math"
"strings"
"sync"
"time"
flag "github.com/spf13/pflag"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/offchainlabs/nitro/arbstate/daprovider"
"github.com/offchainlabs/nitro/das/dastree"
"github.com/offchainlabs/nitro/util/pretty"
"github.com/offchainlabs/nitro/util/stopwaiter"
)
// Most of the time we will use the SimpleDASReaderAggregator only to aggregate
// RestfulDasClients, so the configuration and factory function are given more
// specific names.
type RestfulClientAggregatorConfig struct {
Enable bool `koanf:"enable"`
Urls []string `koanf:"urls"`
OnlineUrlList string `koanf:"online-url-list"`
OnlineUrlListFetchInterval time.Duration `koanf:"online-url-list-fetch-interval"`
Strategy string `koanf:"strategy"`
StrategyUpdateInterval time.Duration `koanf:"strategy-update-interval"`
WaitBeforeTryNext time.Duration `koanf:"wait-before-try-next"`
MaxPerEndpointStats int `koanf:"max-per-endpoint-stats"`
SimpleExploreExploitStrategy SimpleExploreExploitStrategyConfig `koanf:"simple-explore-exploit-strategy"`
SyncToStorage SyncToStorageConfig `koanf:"sync-to-storage"`
}
var DefaultRestfulClientAggregatorConfig = RestfulClientAggregatorConfig{
Urls: []string{},
OnlineUrlList: "",
OnlineUrlListFetchInterval: 1 * time.Hour,
Strategy: "simple-explore-exploit",
StrategyUpdateInterval: 10 * time.Second,
WaitBeforeTryNext: 2 * time.Second,
MaxPerEndpointStats: 20,
SimpleExploreExploitStrategy: DefaultSimpleExploreExploitStrategyConfig,
SyncToStorage: DefaultSyncToStorageConfig,
}
type SimpleExploreExploitStrategyConfig struct {
ExploreIterations uint32 `koanf:"explore-iterations"`
ExploitIterations uint32 `koanf:"exploit-iterations"`
}
var DefaultSimpleExploreExploitStrategyConfig = SimpleExploreExploitStrategyConfig{
ExploreIterations: 20,
ExploitIterations: 1000,
}
func RestfulClientAggregatorConfigAddOptions(prefix string, f *flag.FlagSet) {
f.Bool(prefix+".enable", DefaultRestfulClientAggregatorConfig.Enable, "enable retrieval of sequencer batch data from a list of remote REST endpoints; if other DAS storage types are enabled, this mode is used as a fallback")
f.StringSlice(prefix+".urls", DefaultRestfulClientAggregatorConfig.Urls, "list of URLs including 'http://' or 'https://' prefixes and port numbers to REST DAS endpoints; additive with the online-url-list option")
f.String(prefix+".online-url-list", DefaultRestfulClientAggregatorConfig.OnlineUrlList, "a URL to a list of URLs of REST das endpoints that is checked at startup; additive with the url option")
f.Duration(prefix+".online-url-list-fetch-interval", DefaultRestfulClientAggregatorConfig.OnlineUrlListFetchInterval, "time interval to periodically fetch url list from online-url-list")
f.String(prefix+".strategy", DefaultRestfulClientAggregatorConfig.Strategy, "strategy to use to determine order and parallelism of calling REST endpoint URLs; valid options are 'simple-explore-exploit'")
f.Duration(prefix+".strategy-update-interval", DefaultRestfulClientAggregatorConfig.StrategyUpdateInterval, "how frequently to update the strategy with endpoint latency and error rate data")
f.Duration(prefix+".wait-before-try-next", DefaultRestfulClientAggregatorConfig.WaitBeforeTryNext, "time to wait until trying the next set of REST endpoints while waiting for a response; the next set of REST endpoints is determined by the strategy selected")
f.Int(prefix+".max-per-endpoint-stats", DefaultRestfulClientAggregatorConfig.MaxPerEndpointStats, "number of stats entries (latency and success rate) to keep for each REST endpoint; controls whether strategy is faster or slower to respond to changing conditions")
SimpleExploreExploitStrategyConfigAddOptions(prefix+".simple-explore-exploit-strategy", f)
SyncToStorageConfigAddOptions(prefix+".sync-to-storage", f)
}
func SimpleExploreExploitStrategyConfigAddOptions(prefix string, f *flag.FlagSet) {
f.Uint32(prefix+".explore-iterations", DefaultSimpleExploreExploitStrategyConfig.ExploreIterations, "number of consecutive GetByHash calls to the aggregator where each call will cause it to randomly select from REST endpoints until one returns successfully, before switching to exploit mode")
f.Uint32(prefix+".exploit-iterations", DefaultSimpleExploreExploitStrategyConfig.ExploitIterations, "number of consecutive GetByHash calls to the aggregator where each call will cause it to select from REST endpoints in order of best latency and success rate, before switching to explore mode")
}
func NewRestfulClientAggregator(ctx context.Context, config *RestfulClientAggregatorConfig) (*SimpleDASReaderAggregator, error) {
a := SimpleDASReaderAggregator{
config: config,
stats: make(map[daprovider.DASReader]readerStats),
}
combinedUrls := make(map[string]bool)
for _, url := range config.Urls {
combinedUrls[url] = true
}
if config.OnlineUrlList != DefaultRestfulClientAggregatorConfig.OnlineUrlList {
onlineUrls, err := RestfulServerURLsFromList(ctx, config.OnlineUrlList)
if err != nil {
return nil, err
}
for _, url := range onlineUrls {
combinedUrls[url] = true
}
}
if len(combinedUrls) == 0 {
return nil, errors.New("no URLs were specified with either of rest-aggregator.urls or rest-aggregator.online-url-list")
}
urls := make([]string, 0, len(combinedUrls))
for url := range combinedUrls {
urls = append(urls, url)
}
log.Info("REST Aggregator URLs", "urls", urls)
for _, url := range urls {
reader, err := NewRestfulDasClientFromURL(url)
if err != nil {
return nil, err
}
a.readers = append(a.readers, reader)
a.stats[reader] = make([]readerStat, 0, config.MaxPerEndpointStats)
}
a.statMessages = make(chan readerStatMessage, len(config.Urls)*2)
switch strings.ToLower(config.Strategy) {
case "simple-explore-exploit":
a.strategy = &simpleExploreExploitStrategy{
exploreIterations: config.SimpleExploreExploitStrategy.ExploreIterations,
exploitIterations: config.SimpleExploreExploitStrategy.ExploitIterations,
}
case "testing-sequential":
a.strategy = &testingSequentialStrategy{}
default:
return nil, fmt.Errorf("unknown RestfulClientAggregator strategy '%s', use --help to see available strategies", config.Strategy)
}
a.strategy.update(a.readers, a.stats)
return &a, nil
}
type readerStats []readerStat
// Return the mean latency, weighted inversely by the ratio of successes : total attempts
func (s *readerStats) successRatioWeightedMeanLatency() time.Duration {
successes, totalAttempts := 0.0, 0.0
var totalLatency time.Duration
for _, stat := range *s {
if stat.success {
successes++
totalLatency += stat.latency
}
totalAttempts++
}
if successes == 0 {
return time.Duration(math.MaxInt64)
}
avgLatency := float64(totalLatency) / successes
successRatio := successes / totalAttempts
return time.Duration(avgLatency / successRatio)
}
type readerStat struct {
latency time.Duration
success bool
}
type readerStatMessage struct {
readerStat
reader daprovider.DASReader
}
type SimpleDASReaderAggregator struct {
stopwaiter.StopWaiter
config *RestfulClientAggregatorConfig
readersMutex sync.RWMutex
// readers and stats are only to be updated by the stats goroutine
readers []daprovider.DASReader
stats map[daprovider.DASReader]readerStats
strategy aggregatorStrategy
statMessages chan readerStatMessage
}
func (a *SimpleDASReaderAggregator) GetByHash(ctx context.Context, hash common.Hash) ([]byte, error) {
a.readersMutex.RLock()
defer a.readersMutex.RUnlock()
log.Trace("das.SimpleDASReaderAggregator.GetByHash", "key", pretty.PrettyHash(hash), "this", a)
type dataErrorPair struct {
data []byte
err error
}
results := make(chan dataErrorPair, len(a.readers))
subCtx, cancel := context.WithCancel(ctx)
defer cancel()
go func() {
si := a.strategy.newInstance()
for readers := si.nextReaders(); len(readers) != 0 && subCtx.Err() == nil; readers = si.nextReaders() {
wg := sync.WaitGroup{}
waitChan := make(chan interface{})
for _, reader := range readers {
wg.Add(1)
go func(reader daprovider.DASReader) {
defer wg.Done()
data, err := a.tryGetByHash(subCtx, hash, reader)
if err != nil && errors.Is(ctx.Err(), context.Canceled) {
// Don't record a stats data point when a different
// client returned faster than this one.
return
}
results <- dataErrorPair{data, err}
}(reader)
}
go func() {
wg.Wait()
close(waitChan)
}()
select {
case <-subCtx.Done():
return
case <-time.After(a.config.WaitBeforeTryNext):
case <-waitChan:
// Yield to give the collector a chance to run in case a request succeeded
time.Sleep(10 * time.Millisecond)
}
}
}()
var errorCollection []error
for i := 0; i < len(a.readers); i++ {
select {
case <-ctx.Done():
return nil, ctx.Err()
case result := <-results:
if result.err != nil {
errorCollection = append(errorCollection, result.err)
} else {
return result.data, nil
}
}
}
return nil, fmt.Errorf("data wasn't able to be retrieved from any DAS Reader: %v", errorCollection)
}
func (a *SimpleDASReaderAggregator) tryGetByHash(
ctx context.Context, hash common.Hash, reader daprovider.DASReader,
) ([]byte, error) {
stat := readerStatMessage{reader: reader}
stat.success = false
start := time.Now()
result, err := reader.GetByHash(ctx, hash)
if err == nil {
if dastree.ValidHash(hash, result) {
stat.success = true
} else {
err = fmt.Errorf("SimpleDASReaderAggregator got result from reader(%v) not matching hash", reader)
}
}
stat.latency = time.Since(start)
select {
case a.statMessages <- stat:
// Non-blocking write to stat channel
default:
log.Warn("SimpleDASReaderAggregator stats processing goroutine is backed up, dropping", "dropped stats", stat)
}
return result, err
}
func (a *SimpleDASReaderAggregator) Start(ctx context.Context) {
a.StopWaiter.Start(ctx, a)
onlineUrlsChan := StartRestfulServerListFetchDaemon(a.StopWaiter.GetContext(), a.config.OnlineUrlList, a.config.OnlineUrlListFetchInterval)
updateRestfulDasClients := func(urls []string) {
a.readersMutex.Lock()
defer a.readersMutex.Unlock()
combinedUrls := a.config.Urls
combinedUrls = append(combinedUrls, urls...)
combinedReaders := make(map[daprovider.DASReader]bool)
for _, url := range combinedUrls {
reader, err := NewRestfulDasClientFromURL(url)
if err != nil {
return
}
combinedReaders[reader] = true
}
a.readers = make([]daprovider.DASReader, 0, len(combinedUrls))
// Update reader and add newly added stats
for reader := range combinedReaders {
a.readers = append(a.readers, reader)
if _, ok := a.stats[reader]; ok {
continue
}
a.stats[reader] = make([]readerStat, 0, a.config.MaxPerEndpointStats)
}
// Delete stats for removed reader
for reader := range a.stats {
if combinedReaders[reader] {
continue
}
delete(a.stats, reader)
}
}
a.StopWaiter.LaunchThread(func(innerCtx context.Context) {
updateStrategyTicker := time.NewTicker(a.config.StrategyUpdateInterval)
defer updateStrategyTicker.Stop()
for {
select {
case <-innerCtx.Done():
return
case stat := <-a.statMessages:
a.stats[stat.reader] = append(a.stats[stat.reader], stat.readerStat)
statsLen := len(a.stats[stat.reader])
if statsLen > a.config.MaxPerEndpointStats {
a.stats[stat.reader] = a.stats[stat.reader][statsLen-a.config.MaxPerEndpointStats:]
}
case <-updateStrategyTicker.C:
// Strategy update happens in same goroutine as updates to the stats
// to avoid needing extra synchronization.
a.strategy.update(a.readers, a.stats)
case onlineUrls := <-onlineUrlsChan:
updateRestfulDasClients(onlineUrls)
}
}
})
}
func (a *SimpleDASReaderAggregator) Close(ctx context.Context) error {
a.StopWaiter.StopOnly()
waitChan, err := a.StopWaiter.GetWaitChannel()
if err != nil {
return err
}
select {
case <-ctx.Done():
return ctx.Err()
case <-waitChan:
return nil
}
}
func (a *SimpleDASReaderAggregator) String() string {
return fmt.Sprintf("das.SimpleDASReaderAggregator{%v}", a.config.Urls)
}
func (a *SimpleDASReaderAggregator) HealthCheck(ctx context.Context) error {
return nil
}
func (a *SimpleDASReaderAggregator) ExpirationPolicy(ctx context.Context) (daprovider.ExpirationPolicy, error) {
a.readersMutex.RLock()
defer a.readersMutex.RUnlock()
if len(a.readers) == 0 {
return -1, errors.New("no DataAvailabilityService present")
}
expectedExpirationPolicy, err := a.readers[0].ExpirationPolicy(ctx)
if err != nil {
return -1, err
}
// Even if a single service is different from the rest,
// then whole aggregator will be considered for mixed expiration timeout policy.
for _, serv := range a.readers {
ep, err := serv.ExpirationPolicy(ctx)
if err != nil {
return -1, err
}
if ep != expectedExpirationPolicy {
return daprovider.MixedTimeout, nil
}
}
return expectedExpirationPolicy, nil
}