Skip to content

Commit ac1f32d

Browse files
authored
op-batcher: introduce ClearAllStateMetrics() and call from channelManager.Clear() (#14780)
* op-batcher: introduce ClearAllStateMetrics() and call from channelManager.Clear() * fix test metrics * use real metrics in test * add godoc
1 parent 7887930 commit ac1f32d

File tree

5 files changed

+41
-6
lines changed

5 files changed

+41
-6
lines changed

op-batcher/batcher/channel_manager.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ func (s *channelManager) Clear(l1OriginLastSubmittedChannel eth.BlockID) {
8383
s.tip = common.Hash{}
8484
s.currentChannel = nil
8585
s.channelQueue = nil
86-
s.metr.RecordChannelQueueLength(0)
86+
87+
// This is particularly important because pendingDABytes metric controls throttling:
88+
s.metr.ClearAllStateMetrics()
89+
8790
s.txChannels = make(map[string]*channel)
8891
}
8992

op-batcher/batcher/channel_manager_test.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func ChannelManager_Clear(t *testing.T, batchType uint) {
122122
// clearing confirmed transactions, and resetting the pendingChannels map
123123
cfg.ChannelTimeout = 10
124124
cfg.InitRatioCompressor(1, derive.Zlib)
125-
m := NewChannelManager(log, metrics.NoopMetrics, cfg, defaultTestRollupConfig)
125+
m := NewChannelManager(log, metrics.NewMetrics("test"), cfg, defaultTestRollupConfig)
126126

127127
// Channel Manager state should be empty by default
128128
require.Empty(m.blocks)
@@ -150,7 +150,6 @@ func ChannelManager_Clear(t *testing.T, batchType uint) {
150150

151151
// Process the blocks
152152
// We should have a pending channel with 1 frame
153-
154153
require.NoError(m.processBlocks())
155154
require.NoError(m.currentChannel.channelBuilder.co.Flush())
156155
require.NoError(m.outputFrames())
@@ -174,6 +173,11 @@ func ChannelManager_Clear(t *testing.T, batchType uint) {
174173
safeL1Origin := eth.BlockID{
175174
Number: 123,
176175
}
176+
177+
// Artificially pump up some metrics which need to be cleared
178+
m.metr.RecordL2BlockInPendingQueue(a)
179+
require.NotZero(m.metr.PendingDABytes())
180+
177181
// Clear the channel manager
178182
m.Clear(safeL1Origin)
179183

@@ -184,6 +188,7 @@ func ChannelManager_Clear(t *testing.T, batchType uint) {
184188
require.Nil(m.currentChannel)
185189
require.Empty(m.channelQueue)
186190
require.Empty(m.txChannels)
191+
require.Zero(m.metr.PendingDABytes())
187192
}
188193

189194
func ChannelManager_TxResend(t *testing.T, batchType uint) {

op-batcher/metrics/metrics.go

+15
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ type Metricer interface {
4444
RecordChannelTimedOut(id derive.ChannelID)
4545
RecordChannelQueueLength(len int)
4646

47+
// ClearAllStateMetrics resets any metrics that track current ChannelManager state
48+
// It should be called when clearing the ChannelManager state.
49+
ClearAllStateMetrics()
50+
4751
RecordBatchTxSubmitted()
4852
RecordBatchTxSuccess()
4953
RecordBatchTxFailed()
@@ -349,6 +353,17 @@ func (m *Metrics) RecordChannelQueueLength(len int) {
349353
m.channelQueueLength.Set(float64(len))
350354
}
351355

356+
// ClearAllStateMetrics clears all state metrics.
357+
//
358+
// This should cover any metric which is a Gauge and is incremented / decremented rather than "set".
359+
// Counter Metrics only ever go up, so they can't be reset and shouldn't be.
360+
// Gauge Metrics which are "set" will get the right value the next time they are updated and don't need to be reset.
361+
func (m *Metrics) ClearAllStateMetrics() {
362+
m.RecordChannelQueueLength(0)
363+
atomic.StoreInt64(&m.pendingDABytes, 0)
364+
m.pendingBlocksBytesCurrent.Set(0)
365+
}
366+
352367
// estimateBatchSize returns the estimated size of the block in a batch both with compression ('daSize') and without
353368
// ('rawSize').
354369
func estimateBatchSize(block *types.Block) (daSize, rawSize uint64) {

op-batcher/metrics/noop.go

+2
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,5 @@ type ThrottlingMetrics struct {
6161
func (nm *ThrottlingMetrics) PendingDABytes() float64 {
6262
return math.MaxFloat64
6363
}
64+
65+
func (*noopMetrics) ClearAllStateMetrics() {}

op-batcher/metrics/test.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,29 @@ type TestMetrics struct {
88
noopMetrics
99
PendingBlocksBytesCurrent float64
1010
ChannelQueueLength int
11+
pendingDABytes float64
1112
}
1213

1314
var _ Metricer = new(TestMetrics)
1415

1516
func (m *TestMetrics) RecordL2BlockInPendingQueue(block *types.Block) {
16-
_, rawSize := estimateBatchSize(block)
17+
daSize, rawSize := estimateBatchSize(block)
1718
m.PendingBlocksBytesCurrent += float64(rawSize)
18-
19+
m.pendingDABytes += float64(daSize)
1920
}
2021
func (m *TestMetrics) RecordL2BlockInChannel(block *types.Block) {
21-
_, rawSize := estimateBatchSize(block)
22+
daSize, rawSize := estimateBatchSize(block)
2223
m.PendingBlocksBytesCurrent -= float64(rawSize)
24+
m.pendingDABytes -= float64(daSize)
2325
}
2426
func (m *TestMetrics) RecordChannelQueueLength(l int) {
2527
m.ChannelQueueLength = l
2628
}
29+
func (m *TestMetrics) PendingDABytes() float64 {
30+
return m.pendingDABytes
31+
}
32+
func (m *TestMetrics) ClearAllStateMetrics() {
33+
m.PendingBlocksBytesCurrent = 0
34+
m.ChannelQueueLength = 0
35+
m.pendingDABytes = 0
36+
}

0 commit comments

Comments
 (0)