From 04058779a95f8f2b4c7857b9d521edd8c6f730b0 Mon Sep 17 00:00:00 2001 From: Felipe Andrade Date: Sat, 27 May 2023 10:18:34 -0700 Subject: [PATCH] small fixes --- proxyd/proxyd/consensus_poller.go | 12 +++++------- proxyd/proxyd/metrics.go | 7 ++++++- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/proxyd/proxyd/consensus_poller.go b/proxyd/proxyd/consensus_poller.go index 07edc31..9e1c5eb 100644 --- a/proxyd/proxyd/consensus_poller.go +++ b/proxyd/proxyd/consensus_poller.go @@ -212,9 +212,6 @@ func NewConsensusPoller(bg *BackendGroup, opts ...ConsensusOpt) *ConsensusPoller ctx, cancelFunc := context.WithCancel(context.Background()) state := make(map[*Backend]*backendState, len(bg.Backends)) - for _, be := range bg.Backends { - state[be] = &backendState{} - } cp := &ConsensusPoller{ cancelFunc: cancelFunc, @@ -239,6 +236,7 @@ func NewConsensusPoller(bg *BackendGroup, opts ...ConsensusOpt) *ConsensusPoller cp.asyncHandler = NewPollerAsyncHandler(ctx, cp) } + cp.Reset() cp.asyncHandler.Init() return cp @@ -291,11 +289,11 @@ func (cp *ConsensusPoller) UpdateBackend(ctx context.Context, be *Backend) { log.Warn("error updating backend - finalized block", "name", be.Name, "err", err) } + // just for readability oldFinalized := bs.finalizedBlockNumber oldSafe := bs.safeBlockNumber - updateDelay := time.Since(bs.lastUpdate) - RecordConsensusBackendUpdateDelay(be, updateDelay) + RecordConsensusBackendUpdateDelay(be, bs.lastUpdate) changed := cp.setBackendState(be, peerCount, inSync, latestBlockNumber, latestBlockHash, @@ -314,7 +312,7 @@ func (cp *ConsensusPoller) UpdateBackend(ctx context.Context, be *Backend) { "latestBlockHash", latestBlockHash, "finalizedBlockNumber", finalizedBlockNumber, "safeBlockNumber", safeBlockNumber, - "updateDelay", updateDelay) + "lastUpdate", bs.lastUpdate) } // sanity check for latest, safe and finalized block tags @@ -658,7 +656,7 @@ func (cp *ConsensusPoller) getConsensusCandidates() map[*Backend]*backendState { lagging := make([]*Backend, 0, len(candidates)) for be, bs := range candidates { // check if backend is lagging behind the highest block - if bs.latestBlockNumber < highestLatestBlock && uint64(highestLatestBlock-bs.latestBlockNumber) > cp.maxBlockLag { + if uint64(highestLatestBlock-bs.latestBlockNumber) > cp.maxBlockLag { lagging = append(lagging, be) } } diff --git a/proxyd/proxyd/metrics.go b/proxyd/proxyd/metrics.go index 3d8bfac..1d9602c 100644 --- a/proxyd/proxyd/metrics.go +++ b/proxyd/proxyd/metrics.go @@ -482,7 +482,12 @@ func RecordConsensusBackendInSync(b *Backend, inSync bool) { consensusInSyncBackend.WithLabelValues(b.Name).Set(boolToFloat64(inSync)) } -func RecordConsensusBackendUpdateDelay(b *Backend, delay time.Duration) { +func RecordConsensusBackendUpdateDelay(b *Backend, lastUpdate time.Time) { + // avoid recording the delay for the first update + if lastUpdate.IsZero() { + return + } + delay := time.Since(lastUpdate) consensusUpdateDelayBackend.WithLabelValues(b.Name).Set(float64(delay.Milliseconds())) }