Skip to content

Commit 66ae8ef

Browse files
Cleanup consensus metrics (#2815)
1 parent 6c76098 commit 66ae8ef

File tree

3 files changed

+29
-50
lines changed

3 files changed

+29
-50
lines changed

snow/engine/snowman/issuer.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ func (i *issuer) Abandon(ctx context.Context, _ ids.ID) {
4141
i.t.removeFromPending(i.blk)
4242
i.t.addToNonVerifieds(i.blk)
4343
i.t.blocked.Abandon(ctx, blkID)
44-
45-
// Tracks performance statistics
46-
i.t.metrics.numRequests.Set(float64(i.t.blkReqs.Len()))
47-
i.t.metrics.numBlocked.Set(float64(len(i.t.pending)))
48-
i.t.metrics.numBlockers.Set(float64(i.t.blocked.Len()))
4944
}
5045
i.abandoned = true
5146
}

snow/engine/snowman/transitive.go

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func (t *Transitive) Gossip(ctx context.Context) error {
210210
return nil
211211
}
212212

213-
lastAccepted, err := t.GetBlock(ctx, lastAcceptedID)
213+
lastAccepted, err := t.getBlock(ctx, lastAcceptedID)
214214
if err != nil {
215215
t.Ctx.Log.Warn("dropping gossip request",
216216
zap.String("reason", "block couldn't be loaded"),
@@ -296,7 +296,7 @@ func (t *Transitive) Put(ctx context.Context, nodeID ids.NodeID, requestID uint3
296296
if _, err := t.issueFrom(ctx, nodeID, blk, issuedMetric); err != nil {
297297
return err
298298
}
299-
return t.buildBlocks(ctx)
299+
return t.executeDeferredWork(ctx)
300300
}
301301

302302
func (t *Transitive) GetFailed(ctx context.Context, nodeID ids.NodeID, requestID uint32) error {
@@ -319,9 +319,7 @@ func (t *Transitive) GetFailed(ctx context.Context, nodeID ids.NodeID, requestID
319319

320320
// Because the get request was dropped, we no longer expect blkID to be issued.
321321
t.blocked.Abandon(ctx, blkID)
322-
t.metrics.numRequests.Set(float64(t.blkReqs.Len()))
323-
t.metrics.numBlockers.Set(float64(t.blocked.Len()))
324-
return t.buildBlocks(ctx)
322+
return t.executeDeferredWork(ctx)
325323
}
326324

327325
func (t *Transitive) PullQuery(ctx context.Context, nodeID ids.NodeID, requestID uint32, blkID ids.ID, requestedHeight uint64) error {
@@ -335,7 +333,7 @@ func (t *Transitive) PullQuery(ctx context.Context, nodeID ids.NodeID, requestID
335333
return err
336334
}
337335

338-
return t.buildBlocks(ctx)
336+
return t.executeDeferredWork(ctx)
339337
}
340338

341339
func (t *Transitive) PushQuery(ctx context.Context, nodeID ids.NodeID, requestID uint32, blkBytes []byte, requestedHeight uint64) error {
@@ -376,7 +374,7 @@ func (t *Transitive) PushQuery(ctx context.Context, nodeID ids.NodeID, requestID
376374
return err
377375
}
378376

379-
return t.buildBlocks(ctx)
377+
return t.executeDeferredWork(ctx)
380378
}
381379

382380
func (t *Transitive) Chits(ctx context.Context, nodeID ids.NodeID, requestID uint32, preferredID ids.ID, preferredIDAtHeight ids.ID, acceptedID ids.ID) error {
@@ -432,8 +430,7 @@ func (t *Transitive) Chits(ctx context.Context, nodeID ids.NodeID, requestID uin
432430
}
433431

434432
t.blocked.Register(ctx, v)
435-
t.metrics.numBlockers.Set(float64(t.blocked.Len()))
436-
return t.buildBlocks(ctx)
433+
return t.executeDeferredWork(ctx)
437434
}
438435

439436
func (t *Transitive) QueryFailed(ctx context.Context, nodeID ids.NodeID, requestID uint32) error {
@@ -450,8 +447,7 @@ func (t *Transitive) QueryFailed(ctx context.Context, nodeID ids.NodeID, request
450447
requestID: requestID,
451448
},
452449
)
453-
t.metrics.numBlockers.Set(float64(t.blocked.Len()))
454-
return t.buildBlocks(ctx)
450+
return t.executeDeferredWork(ctx)
455451
}
456452

457453
func (*Transitive) Timeout(context.Context) error {
@@ -474,7 +470,7 @@ func (t *Transitive) Notify(ctx context.Context, msg common.Message) error {
474470
case common.PendingTxs:
475471
// the pending txs message means we should attempt to build a block.
476472
t.pendingBuildBlocks++
477-
return t.buildBlocks(ctx)
473+
return t.executeDeferredWork(ctx)
478474
case common.StateSyncDone:
479475
t.Ctx.StateSyncing.Set(false)
480476
return nil
@@ -497,7 +493,7 @@ func (t *Transitive) Start(ctx context.Context, startReqID uint32) error {
497493
return err
498494
}
499495

500-
lastAccepted, err := t.GetBlock(ctx, lastAcceptedID)
496+
lastAccepted, err := t.getBlock(ctx, lastAcceptedID)
501497
if err != nil {
502498
t.Ctx.Log.Error("failed to get last accepted block",
503499
zap.Error(err),
@@ -549,7 +545,7 @@ func (t *Transitive) Start(ctx context.Context, startReqID uint32) error {
549545
return fmt.Errorf("failed to notify VM that consensus is starting: %w",
550546
err)
551547
}
552-
return nil
548+
return t.executeDeferredWork(ctx)
553549
}
554550

555551
func (t *Transitive) HealthCheck(ctx context.Context) (interface{}, error) {
@@ -580,7 +576,19 @@ func (t *Transitive) HealthCheck(ctx context.Context) (interface{}, error) {
580576
return intf, fmt.Errorf("vm: %w ; consensus: %w", vmErr, consensusErr)
581577
}
582578

583-
func (t *Transitive) GetBlock(ctx context.Context, blkID ids.ID) (snowman.Block, error) {
579+
func (t *Transitive) executeDeferredWork(ctx context.Context) error {
580+
if err := t.buildBlocks(ctx); err != nil {
581+
return err
582+
}
583+
584+
t.metrics.numRequests.Set(float64(t.blkReqs.Len()))
585+
t.metrics.numBlocked.Set(float64(len(t.pending)))
586+
t.metrics.numBlockers.Set(float64(t.blocked.Len()))
587+
t.metrics.numNonVerifieds.Set(float64(t.nonVerifieds.Len()))
588+
return nil
589+
}
590+
591+
func (t *Transitive) getBlock(ctx context.Context, blkID ids.ID) (snowman.Block, error) {
584592
if blk, ok := t.pending[blkID]; ok {
585593
return blk, nil
586594
}
@@ -733,7 +741,7 @@ func (t *Transitive) issueFromByID(
733741
blkID ids.ID,
734742
issuedMetric prometheus.Counter,
735743
) (bool, error) {
736-
blk, err := t.GetBlock(ctx, blkID)
744+
blk, err := t.getBlock(ctx, blkID)
737745
if err != nil {
738746
t.sendRequest(ctx, nodeID, blkID, issuedMetric)
739747
return false, nil
@@ -759,7 +767,7 @@ func (t *Transitive) issueFrom(
759767

760768
blkID = blk.Parent()
761769
var err error
762-
blk, err = t.GetBlock(ctx, blkID)
770+
blk, err = t.getBlock(ctx, blkID)
763771

764772
// If we don't have this ancestor, request it from [vdr]
765773
if err != nil || !blk.Status().Fetched() {
@@ -780,10 +788,6 @@ func (t *Transitive) issueFrom(
780788
// dependencies may still be waiting. Therefore, they should abandoned.
781789
t.blocked.Abandon(ctx, blkID)
782790
}
783-
784-
// Tracks performance statistics
785-
t.metrics.numRequests.Set(float64(t.blkReqs.Len()))
786-
t.metrics.numBlockers.Set(float64(t.blocked.Len()))
787791
return issued, t.errs.Err
788792
}
789793

@@ -804,7 +808,7 @@ func (t *Transitive) issueWithAncestors(
804808
return false, err
805809
}
806810
blkID = blk.Parent()
807-
blk, err = t.GetBlock(ctx, blkID)
811+
blk, err = t.getBlock(ctx, blkID)
808812
if err != nil {
809813
status = choices.Unknown
810814
break
@@ -826,7 +830,6 @@ func (t *Transitive) issueWithAncestors(
826830
// We don't have this block and have no reason to expect that we will get it.
827831
// Abandon the block to avoid a memory leak.
828832
t.blocked.Abandon(ctx, blkID)
829-
t.metrics.numBlockers.Set(float64(t.blocked.Len()))
830833
return false, t.errs.Err
831834
}
832835

@@ -869,7 +872,7 @@ func (t *Transitive) issue(
869872

870873
// block on the parent if needed
871874
parentID := blk.Parent()
872-
if parent, err := t.GetBlock(ctx, parentID); err != nil || !(t.Consensus.Decided(parent) || t.Consensus.Processing(parentID)) {
875+
if parent, err := t.getBlock(ctx, parentID); err != nil || !(t.Consensus.Decided(parent) || t.Consensus.Processing(parentID)) {
873876
t.Ctx.Log.Verbo("block waiting for parent to be issued",
874877
zap.Stringer("blkID", blkID),
875878
zap.Stringer("parentID", parentID),
@@ -878,11 +881,6 @@ func (t *Transitive) issue(
878881
}
879882

880883
t.blocked.Register(ctx, i)
881-
882-
// Tracks performance statistics
883-
t.metrics.numRequests.Set(float64(t.blkReqs.Len()))
884-
t.metrics.numBlocked.Set(float64(len(t.pending)))
885-
t.metrics.numBlockers.Set(float64(t.blocked.Len()))
886884
return t.errs.Err
887885
}
888886

@@ -912,9 +910,6 @@ func (t *Transitive) sendRequest(
912910
zap.Stringer("blkID", blkID),
913911
)
914912
t.Sender.SendGet(ctx, nodeID, t.requestID, blkID)
915-
916-
// Tracks performance statistics
917-
t.metrics.numRequests.Set(float64(t.blkReqs.Len()))
918913
}
919914

920915
// Send a query for this block. If push is set to true, blkBytes will be used to
@@ -990,16 +985,14 @@ func (t *Transitive) deliver(
990985
// longer pending
991986
t.removeFromPending(blk)
992987
parentID := blk.Parent()
993-
parent, err := t.GetBlock(ctx, parentID)
988+
parent, err := t.getBlock(ctx, parentID)
994989
// Because the dependency must have been fulfilled by the time this function
995990
// is called - we don't expect [err] to be non-nil. But it is handled for
996991
// completness and future proofing.
997992
if err != nil || !(parent.Status() == choices.Accepted || t.Consensus.Processing(parentID)) {
998993
// if the parent isn't processing or the last accepted block, then this
999994
// block is effectively rejected
1000995
t.blocked.Abandon(ctx, blkID)
1001-
t.metrics.numBlocked.Set(float64(len(t.pending))) // Tracks performance statistics
1002-
t.metrics.numBlockers.Set(float64(t.blocked.Len()))
1003996
return t.errs.Err
1004997
}
1005998

@@ -1012,8 +1005,6 @@ func (t *Transitive) deliver(
10121005
}
10131006
if !blkAdded {
10141007
t.blocked.Abandon(ctx, blkID)
1015-
t.metrics.numBlocked.Set(float64(len(t.pending))) // Tracks performance statistics
1016-
t.metrics.numBlockers.Set(float64(t.blocked.Len()))
10171008
return t.errs.Err
10181009
}
10191010

@@ -1077,11 +1068,6 @@ func (t *Transitive) deliver(
10771068

10781069
// If we should issue multiple queries at the same time, we need to repoll
10791070
t.repoll(ctx)
1080-
1081-
// Tracks performance statistics
1082-
t.metrics.numRequests.Set(float64(t.blkReqs.Len()))
1083-
t.metrics.numBlocked.Set(float64(len(t.pending)))
1084-
t.metrics.numBlockers.Set(float64(t.blocked.Len()))
10851071
return t.errs.Err
10861072
}
10871073

@@ -1108,7 +1094,6 @@ func (t *Transitive) addToNonVerifieds(blk snowman.Block) {
11081094
if t.nonVerifieds.Has(parentID) || t.Consensus.Processing(parentID) {
11091095
t.nonVerifieds.Add(blkID, parentID)
11101096
t.nonVerifiedCache.Put(blkID, blk)
1111-
t.metrics.numNonVerifieds.Set(float64(t.nonVerifieds.Len()))
11121097
}
11131098
}
11141099

@@ -1140,7 +1125,6 @@ func (t *Transitive) addUnverifiedBlockToConsensus(
11401125
issuedMetric.Inc()
11411126
t.nonVerifieds.Remove(blkID)
11421127
t.nonVerifiedCache.Evict(blkID)
1143-
t.metrics.numNonVerifieds.Set(float64(t.nonVerifieds.Len()))
11441128
t.metrics.issuerStake.Observe(float64(t.Validators.GetWeight(t.Ctx.SubnetID, nodeID)))
11451129
t.Ctx.Log.Verbo("adding block to consensus",
11461130
zap.Stringer("nodeID", nodeID),

snow/engine/snowman/voter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (v *voter) getProcessingAncestor(ctx context.Context, initialVote ids.ID) (
107107
// have at our disposal as a best-effort mechanism to find a valid ancestor.
108108
bubbledVote := v.t.nonVerifieds.GetAncestor(initialVote)
109109
for {
110-
blk, err := v.t.GetBlock(ctx, bubbledVote)
110+
blk, err := v.t.getBlock(ctx, bubbledVote)
111111
// If we cannot retrieve the block, drop [vote]
112112
if err != nil {
113113
v.t.Ctx.Log.Debug("dropping vote",

0 commit comments

Comments
 (0)