Skip to content

Commit b9fa9aa

Browse files
authored
chore(sync)!: Rename unrecentHead to outdatedHead in metrics + clean up Head func a bit (#163)
This PR renames a poorly named metric from `unrecent_header` to `outdated_head` which is more descriptive / accurate. It also cleans up a bit of the code inside of `sync_head.go`. This PR is meant to override #162 if we agree that the timeout should not be configurable (see [comment](#155 (comment))
1 parent 06f57bb commit b9fa9aa

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

sync/metrics.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type metrics struct {
2020

2121
syncLoopStarted metric.Int64Counter
2222
trustedPeersOutOfSync metric.Int64Counter
23-
unrecentHeader metric.Int64Counter
23+
outdatedHeader metric.Int64Counter
2424
subjectiveInit metric.Int64Counter
2525

2626
subjectiveHead atomic.Int64
@@ -53,9 +53,9 @@ func newMetrics() (*metrics, error) {
5353
return nil, err
5454
}
5555

56-
unrecentHeader, err := meter.Int64Counter(
57-
"hdr_sync_unrecent_header_counter",
58-
metric.WithDescription("tracks every time Syncer returns an unrecent header"),
56+
outdatedHeader, err := meter.Int64Counter(
57+
"hdr_sync_outdated_head_counter",
58+
metric.WithDescription("tracks every time Syncer returns an outdated head"),
5959
)
6060
if err != nil {
6161
return nil, err
@@ -110,7 +110,7 @@ func newMetrics() (*metrics, error) {
110110
m := &metrics{
111111
syncLoopStarted: syncLoopStarted,
112112
trustedPeersOutOfSync: trustedPeersOutOfSync,
113-
unrecentHeader: unrecentHeader,
113+
outdatedHeader: outdatedHeader,
114114
subjectiveInit: subjectiveInit,
115115
syncLoopDurationHist: syncLoopDurationHist,
116116
syncLoopRunningInst: syncLoopRunningInst,
@@ -148,9 +148,9 @@ func (m *metrics) syncFinished(ctx context.Context) {
148148
})
149149
}
150150

151-
func (m *metrics) unrecentHead(ctx context.Context) {
151+
func (m *metrics) outdatedHead(ctx context.Context) {
152152
m.observe(ctx, func(ctx context.Context) {
153-
m.unrecentHeader.Add(ctx, 1)
153+
m.outdatedHeader.Add(ctx, 1)
154154
})
155155
}
156156

sync/sync_head.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import (
88
"github.com/celestiaorg/go-header"
99
)
1010

11+
// headRequestTimeout is the amount of time the syncer is willing to wait for
12+
// the exchange to request the head of the chain from the network.
13+
var headRequestTimeout = time.Second * 2
14+
1115
// Head returns the Network Head.
1216
//
1317
// Known subjective head is considered network head if it is recent enough(now-timestamp<=blocktime)
@@ -24,27 +28,25 @@ func (s *Syncer[H]) Head(ctx context.Context, _ ...header.HeadOption[H]) (H, err
2428
if isRecent(sbjHead, s.Params.blockTime, s.Params.recencyThreshold) {
2529
return sbjHead, nil
2630
}
27-
// otherwise, request head from the network
28-
// TODO: Besides requesting we should listen for new gossiped headers and cancel request if so
29-
//
30-
// single-flight protection
31-
// ensure only one Head is requested at the time
31+
32+
// single-flight protection ensure only one Head is requested at the time
3233
if !s.getter.Lock() {
3334
// means that other routine held the lock and set the subjective head for us,
3435
// so just recursively get it
3536
return s.Head(ctx)
3637
}
3738
defer s.getter.Unlock()
38-
// limit time to get a recent header
39-
// if we can't get it - give what we have
40-
reqCtx, cancel := context.WithTimeout(ctx, time.Second*2) // TODO(@vgonkivs): make timeout configurable
39+
40+
s.metrics.outdatedHead(s.ctx)
41+
42+
reqCtx, cancel := context.WithTimeout(ctx, headRequestTimeout)
4143
defer cancel()
42-
s.metrics.unrecentHead(s.ctx)
4344
netHead, err := s.getter.Head(reqCtx, header.WithTrustedHead[H](sbjHead))
4445
if err != nil {
4546
log.Warnw("failed to get recent head, returning current subjective", "sbjHead", sbjHead.Height(), "err", err)
4647
return s.subjectiveHead(ctx)
4748
}
49+
4850
// process and validate netHead fetched from trusted peers
4951
// NOTE: We could trust the netHead like we do during 'automatic subjective initialization'
5052
// but in this case our subjective head is not expired, so we should verify netHead

0 commit comments

Comments
 (0)