-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
client_http.go
682 lines (536 loc) · 18.2 KB
/
client_http.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
// Copyright 2024 The Erigon Authors
// This file is part of Erigon.
//
// Erigon is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Erigon is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Erigon. If not, see <http://www.gnu.org/licenses/>.
package heimdall
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"path"
"sort"
"strconv"
"strings"
"time"
"github.com/erigontech/erigon-lib/log/v3"
"github.com/erigontech/erigon-lib/metrics"
)
var (
// ErrShutdownDetected is returned if a shutdown was detected
ErrShutdownDetected = errors.New("shutdown detected")
ErrNoResponse = errors.New("got a nil response")
ErrNotSuccessfulResponse = errors.New("error while fetching data from Heimdall")
ErrNotInRejectedList = errors.New("milestoneId doesn't exist in rejected list")
ErrNotInMilestoneList = errors.New("milestoneId doesn't exist in Heimdall")
ErrNotInCheckpointList = errors.New("checkpontId doesn't exist in Heimdall")
ErrBadGateway = errors.New("bad gateway")
ErrServiceUnavailable = errors.New("service unavailable")
TransientErrors = []error{
ErrBadGateway,
ErrServiceUnavailable,
context.DeadlineExceeded,
}
)
const (
StateEventsFetchLimit = 50
SpansFetchLimit = 150
CheckpointsFetchLimit = 10_000
apiHeimdallTimeout = 10 * time.Second
retryBackOff = time.Second
maxRetries = 5
)
var _ Client = &HttpClient{}
type HttpClient struct {
urlString string
handler httpRequestHandler
retryBackOff time.Duration
maxRetries int
closeCh chan struct{}
logger log.Logger
}
type HttpRequest struct {
handler httpRequestHandler
url *url.URL
start time.Time
}
type HttpClientOption func(*HttpClient)
func WithHttpRequestHandler(handler httpRequestHandler) HttpClientOption {
return func(client *HttpClient) {
client.handler = handler
}
}
func WithHttpRetryBackOff(retryBackOff time.Duration) HttpClientOption {
return func(client *HttpClient) {
client.retryBackOff = retryBackOff
}
}
func WithHttpMaxRetries(maxRetries int) HttpClientOption {
return func(client *HttpClient) {
client.maxRetries = maxRetries
}
}
func NewHttpClient(urlString string, logger log.Logger, opts ...HttpClientOption) *HttpClient {
c := &HttpClient{
urlString: urlString,
logger: logger,
handler: &http.Client{Timeout: apiHeimdallTimeout},
retryBackOff: retryBackOff,
maxRetries: maxRetries,
closeCh: make(chan struct{}),
}
for _, opt := range opts {
opt(c)
}
return c
}
const (
fetchStateSyncEventsFormat = "from-id=%d&to-time=%d&limit=%d"
fetchStateSyncEventsPath = "clerk/event-record/list"
fetchStateSyncEvent = "clerk/event-record/%s"
fetchCheckpoint = "/checkpoints/%s"
fetchCheckpointCount = "/checkpoints/count"
fetchCheckpointList = "/checkpoints/list"
fetchCheckpointListQueryFormat = "page=%d&limit=%d"
fetchMilestoneAt = "/milestone/%d"
fetchMilestoneLatest = "/milestone/latest"
fetchMilestoneCount = "/milestone/count"
fetchLastNoAckMilestone = "/milestone/lastNoAck"
fetchNoAckMilestone = "/milestone/noAck/%s"
fetchMilestoneID = "/milestone/ID/%s"
fetchSpanFormat = "bor/span/%d"
fetchSpanLatest = "bor/latest-span"
fetchSpanListFormat = "page=%d&limit=%d" // max limit = 150
fetchSpanListPath = "bor/span/list"
)
func (c *HttpClient) FetchStateSyncEvents(ctx context.Context, fromID uint64, to time.Time, limit int) ([]*EventRecordWithTime, error) {
eventRecords := make([]*EventRecordWithTime, 0)
for {
url, err := stateSyncListURL(c.urlString, fromID, to.Unix())
if err != nil {
return nil, err
}
c.logger.Trace(heimdallLogPrefix("Fetching state sync events"), "queryParams", url.RawQuery)
reqCtx := withRequestType(ctx, stateSyncRequest)
response, err := FetchWithRetry[StateSyncEventsResponse](reqCtx, c, url, c.logger)
if err != nil {
if errors.Is(err, ErrNoResponse) {
// for more info check https://github.com/maticnetwork/heimdall/pull/993
c.logger.Warn(
heimdallLogPrefix("check heimdall logs to see if it is in sync - no response when querying state sync events"),
"path", url.Path,
"queryParams", url.RawQuery,
)
}
return nil, err
}
if response == nil || response.Result == nil {
// status 204
break
}
eventRecords = append(eventRecords, response.Result...)
if len(response.Result) < StateEventsFetchLimit || (limit > 0 && len(eventRecords) >= limit) {
break
}
fromID += uint64(StateEventsFetchLimit)
}
sort.SliceStable(eventRecords, func(i, j int) bool {
return eventRecords[i].ID < eventRecords[j].ID
})
return eventRecords, nil
}
func (c *HttpClient) FetchStateSyncEvent(ctx context.Context, id uint64) (*EventRecordWithTime, error) {
url, err := stateSyncURL(c.urlString, id)
if err != nil {
return nil, err
}
ctx = withRequestType(ctx, stateSyncRequest)
isRecoverableError := func(err error) bool {
return !strings.Contains(err.Error(), "could not get state record; No record found")
}
response, err := FetchWithRetryEx[StateSyncEventResponse](ctx, c, url, isRecoverableError, c.logger)
if err != nil {
if strings.Contains(err.Error(), "could not get state record; No record found") {
return nil, ErrEventRecordNotFound
}
return nil, err
}
return &response.Result, nil
}
func (c *HttpClient) FetchLatestSpan(ctx context.Context) (*Span, error) {
url, err := latestSpanURL(c.urlString)
if err != nil {
return nil, err
}
ctx = withRequestType(ctx, spanRequest)
response, err := FetchWithRetry[SpanResponse](ctx, c, url, c.logger)
if err != nil {
return nil, err
}
return &response.Result, nil
}
func (c *HttpClient) FetchSpan(ctx context.Context, spanID uint64) (*Span, error) {
url, err := spanURL(c.urlString, spanID)
if err != nil {
return nil, fmt.Errorf("%w, spanID=%d", err, spanID)
}
ctx = withRequestType(ctx, spanRequest)
response, err := FetchWithRetry[SpanResponse](ctx, c, url, c.logger)
if err != nil {
return nil, fmt.Errorf("%w, spanID=%d", err, spanID)
}
return &response.Result, nil
}
func (c *HttpClient) FetchSpans(ctx context.Context, page uint64, limit uint64) ([]*Span, error) {
url, err := spanListURL(c.urlString, page, limit)
if err != nil {
return nil, err
}
ctx = withRequestType(ctx, checkpointListRequest)
response, err := FetchWithRetry[SpanListResponse](ctx, c, url, c.logger)
if err != nil {
return nil, err
}
return response.Result, nil
}
// FetchCheckpoint fetches the checkpoint from heimdall
func (c *HttpClient) FetchCheckpoint(ctx context.Context, number int64) (*Checkpoint, error) {
url, err := checkpointURL(c.urlString, number)
if err != nil {
return nil, err
}
ctx = withRequestType(ctx, checkpointRequest)
response, err := FetchWithRetry[CheckpointResponse](ctx, c, url, c.logger)
if err != nil {
return nil, err
}
return &response.Result, nil
}
func (c *HttpClient) FetchCheckpoints(ctx context.Context, page uint64, limit uint64) ([]*Checkpoint, error) {
url, err := checkpointListURL(c.urlString, page, limit)
if err != nil {
return nil, err
}
ctx = withRequestType(ctx, checkpointListRequest)
response, err := FetchWithRetry[CheckpointListResponse](ctx, c, url, c.logger)
if err != nil {
return nil, err
}
return response.Result, nil
}
func isInvalidMilestoneIndexError(err error) bool {
return errors.Is(err, ErrNotSuccessfulResponse) &&
strings.Contains(err.Error(), "Invalid milestone index")
}
// FetchMilestone fetches a milestone from heimdall
func (c *HttpClient) FetchMilestone(ctx context.Context, number int64) (*Milestone, error) {
url, err := milestoneURL(c.urlString, number)
if err != nil {
return nil, err
}
ctx = withRequestType(ctx, milestoneRequest)
isRecoverableError := func(err error) bool {
if !isInvalidMilestoneIndexError(err) {
return true
}
if number == -1 {
// -1 means fetch latest, which should be retried
return true
}
firstNum, err := c.FetchFirstMilestoneNum(ctx)
if err != nil {
c.logger.Warn(
heimdallLogPrefix("issue fetching milestone count when deciding if invalid index err is recoverable"),
"err", err,
)
return false
}
// if number is within expected non pruned range then it should be retried
return firstNum <= number && number <= firstNum+milestonePruneNumber-1
}
response, err := FetchWithRetryEx[MilestoneResponse](ctx, c, url, isRecoverableError, c.logger)
if err != nil {
if isInvalidMilestoneIndexError(err) {
return nil, fmt.Errorf("%w: number %d", ErrNotInMilestoneList, number)
}
return nil, err
}
response.Result.Id = MilestoneId(number)
return &response.Result, nil
}
// FetchCheckpointCount fetches the checkpoint count from heimdall
func (c *HttpClient) FetchCheckpointCount(ctx context.Context) (int64, error) {
url, err := checkpointCountURL(c.urlString)
if err != nil {
return 0, err
}
ctx = withRequestType(ctx, checkpointCountRequest)
response, err := FetchWithRetry[CheckpointCountResponse](ctx, c, url, c.logger)
if err != nil {
return 0, err
}
return response.Result.Result, nil
}
// FetchMilestoneCount fetches the milestone count from heimdall
func (c *HttpClient) FetchMilestoneCount(ctx context.Context) (int64, error) {
url, err := milestoneCountURL(c.urlString)
if err != nil {
return 0, err
}
ctx = withRequestType(ctx, milestoneCountRequest)
response, err := FetchWithRetry[MilestoneCountResponse](ctx, c, url, c.logger)
if err != nil {
return 0, err
}
return response.Result.Count, nil
}
// Heimdall keeps only this amount of latest milestones
// https://github.com/maticnetwork/heimdall/blob/master/helper/config.go#L141
const milestonePruneNumber = int64(100)
func (c *HttpClient) FetchFirstMilestoneNum(ctx context.Context) (int64, error) {
count, err := c.FetchMilestoneCount(ctx)
if err != nil {
return 0, err
}
var first int64
if count < milestonePruneNumber {
first = 1
} else {
first = count - milestonePruneNumber + 1
}
return first, nil
}
// FetchLastNoAckMilestone fetches the last no-ack-milestone from heimdall
func (c *HttpClient) FetchLastNoAckMilestone(ctx context.Context) (string, error) {
url, err := lastNoAckMilestoneURL(c.urlString)
if err != nil {
return "", err
}
ctx = withRequestType(ctx, milestoneLastNoAckRequest)
response, err := FetchWithRetry[MilestoneLastNoAckResponse](ctx, c, url, c.logger)
if err != nil {
return "", err
}
return response.Result.Result, nil
}
// FetchNoAckMilestone fetches the last no-ack-milestone from heimdall
func (c *HttpClient) FetchNoAckMilestone(ctx context.Context, milestoneID string) error {
url, err := noAckMilestoneURL(c.urlString, milestoneID)
if err != nil {
return err
}
ctx = withRequestType(ctx, milestoneNoAckRequest)
response, err := FetchWithRetry[MilestoneNoAckResponse](ctx, c, url, c.logger)
if err != nil {
return err
}
if !response.Result.Result {
return fmt.Errorf("%w: milestoneID %q", ErrNotInRejectedList, milestoneID)
}
return nil
}
// FetchMilestoneID fetches the bool result from Heimdall whether the ID corresponding
// to the given milestone is in process in Heimdall
func (c *HttpClient) FetchMilestoneID(ctx context.Context, milestoneID string) error {
url, err := milestoneIDURL(c.urlString, milestoneID)
if err != nil {
return err
}
ctx = withRequestType(ctx, milestoneIDRequest)
response, err := FetchWithRetry[MilestoneIDResponse](ctx, c, url, c.logger)
if err != nil {
return err
}
if !response.Result.Result {
return fmt.Errorf("%w: milestoneID %q", ErrNotInMilestoneList, milestoneID)
}
return nil
}
// FetchWithRetry returns data from heimdall with retry
func FetchWithRetry[T any](ctx context.Context, client *HttpClient, url *url.URL, logger log.Logger) (*T, error) {
return FetchWithRetryEx[T](ctx, client, url, nil, logger)
}
// FetchWithRetryEx returns data from heimdall with retry
func FetchWithRetryEx[T any](
ctx context.Context,
client *HttpClient,
url *url.URL,
isRecoverableError func(error) bool,
logger log.Logger,
) (result *T, err error) {
attempt := 0
// create a new ticker for retrying the request
ticker := time.NewTicker(client.retryBackOff)
defer ticker.Stop()
for attempt < client.maxRetries {
attempt++
request := &HttpRequest{handler: client.handler, url: url, start: time.Now()}
result, err = Fetch[T](ctx, request, logger)
if err == nil {
return result, nil
}
// 503 (Service Unavailable) is thrown when an endpoint isn't activated
// yet in heimdall. E.g. when the hard fork hasn't hit yet but heimdall
// is upgraded.
if errors.Is(err, ErrServiceUnavailable) {
client.logger.Debug(heimdallLogPrefix("service unavailable at the moment"), "path", url.Path, "queryParams", url.RawQuery, "attempt", attempt, "err", err)
return nil, err
}
if (isRecoverableError != nil) && !isRecoverableError(err) {
return nil, err
}
client.logger.Warn(heimdallLogPrefix("an error while fetching"), "path", url.Path, "queryParams", url.RawQuery, "attempt", attempt, "err", err)
select {
case <-ctx.Done():
client.logger.Debug(heimdallLogPrefix("request canceled"), "reason", ctx.Err(), "path", url.Path, "queryParams", url.RawQuery, "attempt", attempt)
return nil, ctx.Err()
case <-client.closeCh:
client.logger.Debug(heimdallLogPrefix("shutdown detected, terminating request"), "path", url.Path, "queryParams", url.RawQuery)
return nil, ErrShutdownDetected
case <-ticker.C:
// retry
}
}
return nil, err
}
// Fetch fetches response from heimdall
func Fetch[T any](ctx context.Context, request *HttpRequest, logger log.Logger) (*T, error) {
isSuccessful := false
defer func() {
if metrics.EnabledExpensive {
sendMetrics(ctx, request.start, isSuccessful)
}
}()
result := new(T)
body, err := internalFetchWithTimeout(ctx, request.handler, request.url, logger)
if err != nil {
return nil, err
}
if len(body) == 0 {
return nil, ErrNoResponse
}
err = json.Unmarshal(body, result)
if err != nil {
return nil, err
}
isSuccessful = true
return result, nil
}
func spanURL(urlString string, spanID uint64) (*url.URL, error) {
return makeURL(urlString, fmt.Sprintf(fetchSpanFormat, spanID), "")
}
func spanListURL(urlString string, page, limit uint64) (*url.URL, error) {
return makeURL(urlString, fetchSpanListPath, fmt.Sprintf(fetchSpanListFormat, page, limit))
}
func latestSpanURL(urlString string) (*url.URL, error) {
return makeURL(urlString, fetchSpanLatest, "")
}
func stateSyncListURL(urlString string, fromID uint64, to int64) (*url.URL, error) {
queryParams := fmt.Sprintf(fetchStateSyncEventsFormat, fromID, to, StateEventsFetchLimit)
return makeURL(urlString, fetchStateSyncEventsPath, queryParams)
}
func stateSyncURL(urlString string, id uint64) (*url.URL, error) {
return makeURL(urlString, fmt.Sprintf(fetchStateSyncEvent, strconv.FormatUint(id, 10)), "")
}
func checkpointURL(urlString string, number int64) (*url.URL, error) {
var url string
if number == -1 {
url = fmt.Sprintf(fetchCheckpoint, "latest")
} else {
url = fmt.Sprintf(fetchCheckpoint, strconv.FormatInt(number, 10))
}
return makeURL(urlString, url, "")
}
func checkpointCountURL(urlString string) (*url.URL, error) {
return makeURL(urlString, fetchCheckpointCount, "")
}
func checkpointListURL(urlString string, page uint64, limit uint64) (*url.URL, error) {
return makeURL(urlString, fetchCheckpointList, fmt.Sprintf(fetchCheckpointListQueryFormat, page, limit))
}
func milestoneURL(urlString string, number int64) (*url.URL, error) {
if number == -1 {
return makeURL(urlString, fetchMilestoneLatest, "")
}
return makeURL(urlString, fmt.Sprintf(fetchMilestoneAt, number), "")
}
func milestoneCountURL(urlString string) (*url.URL, error) {
return makeURL(urlString, fetchMilestoneCount, "")
}
func lastNoAckMilestoneURL(urlString string) (*url.URL, error) {
return makeURL(urlString, fetchLastNoAckMilestone, "")
}
func noAckMilestoneURL(urlString string, id string) (*url.URL, error) {
return makeURL(urlString, fmt.Sprintf(fetchNoAckMilestone, id), "")
}
func milestoneIDURL(urlString string, id string) (*url.URL, error) {
return makeURL(urlString, fmt.Sprintf(fetchMilestoneID, id), "")
}
func makeURL(urlString, rawPath, rawQuery string) (*url.URL, error) {
u, err := url.Parse(urlString)
if err != nil {
return nil, err
}
u.Path = path.Join(u.Path, rawPath)
u.RawQuery = rawQuery
return u, err
}
// internal fetch method
func internalFetch(ctx context.Context, handler httpRequestHandler, u *url.URL, logger log.Logger) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}
logger.Trace(heimdallLogPrefix("http client get request"), "uri", u.RequestURI())
res, err := handler.Do(req)
if err != nil {
return nil, err
}
defer func() {
_ = res.Body.Close()
}()
if res.StatusCode == http.StatusServiceUnavailable {
return nil, fmt.Errorf("%w: url='%s', status=%d", ErrServiceUnavailable, u.String(), res.StatusCode)
}
if res.StatusCode == http.StatusBadGateway {
return nil, fmt.Errorf("%w: url='%s', status=%d", ErrBadGateway, u.String(), res.StatusCode)
}
// unmarshall data from buffer
if res.StatusCode == 204 {
return nil, nil
}
// get response
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
// check status code
if res.StatusCode != 200 {
return nil, fmt.Errorf("%w: url='%s', status=%d, body='%s'", ErrNotSuccessfulResponse, u.String(), res.StatusCode, string(body))
}
return body, nil
}
func internalFetchWithTimeout(ctx context.Context, handler httpRequestHandler, url *url.URL, logger log.Logger) ([]byte, error) {
ctx, cancel := context.WithTimeout(ctx, apiHeimdallTimeout)
defer cancel()
// request data once
return internalFetch(ctx, handler, url, logger)
}
// Close sends a signal to stop the running process
func (c *HttpClient) Close() {
close(c.closeCh)
c.handler.CloseIdleConnections()
}