Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 0 additions & 49 deletions migration/state/headstate/committer.go

This file was deleted.

54 changes: 0 additions & 54 deletions migration/state/headstate/counter.go

This file was deleted.

100 changes: 0 additions & 100 deletions migration/state/headstate/ingestor.go

This file was deleted.

58 changes: 58 additions & 0 deletions migration/state/newstate/internal/common/committer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package common

import (
"github.com/NethermindEth/juno/db"
"github.com/NethermindEth/juno/migration/pipeline"
"github.com/NethermindEth/juno/migration/semaphore"
"github.com/NethermindEth/juno/utils/log"
"go.uber.org/zap"
)

type Committer struct {
logger log.StructuredLogger
counter Counter
batchSemaphore semaphore.ResourceSemaphore[db.Batch]
phaseName string
}

var _ pipeline.State[Task, struct{}] = (*Committer)(nil)

func NewCommitter(
logger log.StructuredLogger,
batchSemaphore semaphore.ResourceSemaphore[db.Batch],
phaseName string,
) *Committer {
return &Committer{
logger: logger,
counter: NewCounter(logger, TimeLogRate, phaseName),
batchSemaphore: batchSemaphore,
phaseName: phaseName,
}
}

func (c *Committer) Run(_ int, t Task, _ chan<- struct{}) error {
defer c.batchSemaphore.Put()

fields := make([]zap.Field, 0, 4)
if c.phaseName != "" {
fields = append(fields, zap.String("phase", c.phaseName))
}
fields = append(fields,
zap.Int("completedAddrs", t.CompletedAddrs),
zap.Int("entryCount", t.EntryCount),
zap.Int("batchSize", t.Batch.Size()),
)
c.logger.Debug("writing batch", fields...)

byteSize := uint64(t.Batch.Size())
if err := t.Batch.Write(); err != nil {
return err

Check warning on line 49 in migration/state/newstate/internal/common/committer.go

View check run for this annotation

Codecov / codecov/patch

migration/state/newstate/internal/common/committer.go#L49

Added line #L49 was not covered by tests
}

c.counter.Log(byteSize, t.CompletedAddrs, t.EntryCount)
return nil
}

func (c *Committer) Done(int, chan<- struct{}) error {
return nil
}
14 changes: 14 additions & 0 deletions migration/state/newstate/internal/common/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package common

import (
"time"

"github.com/NethermindEth/juno/db"
)

const (
BatchByteSize = 128 * db.Megabyte
TargetBatchByteSize = 96 * db.Megabyte
IngestorCount = 4
TimeLogRate = 5 * time.Second
)
67 changes: 67 additions & 0 deletions migration/state/newstate/internal/common/counter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package common

import (
"math"
"time"

"github.com/NethermindEth/juno/db"
"github.com/NethermindEth/juno/utils/log"
"go.uber.org/zap"
)

type Counter struct {
logger log.StructuredLogger
timeLogRate time.Duration
phaseName string
start time.Time
size uint64
completedAddrs uint64
entryCount uint64
}

func NewCounter(logger log.StructuredLogger, timeLogRate time.Duration, phaseName string) Counter {
if zl, ok := logger.(*log.ZapLogger); ok {
logger = zl.WithOptions(zap.AddCallerSkip(1))
}
return Counter{
logger: logger,
timeLogRate: timeLogRate,
phaseName: phaseName,
start: time.Now(),
}
}

func (c *Counter) Log(byteSize uint64, completedAddrs, entryCount int) {
c.size += byteSize
c.completedAddrs += uint64(completedAddrs)
c.entryCount += uint64(entryCount)

const cent = 100

now := time.Now()
elapsed := now.Sub(c.start).Seconds()
if elapsed <= c.timeLogRate.Seconds() {
return
}

mbs := float64(c.size) / float64(db.Megabyte)
fields := make([]zap.Field, 0, 8)
if c.phaseName != "" {
fields = append(fields, zap.String("phase", c.phaseName))
}
fields = append(fields,
zap.Float64("MB", math.Round(mbs*cent)/cent),
zap.Float64("MB/s", math.Round(mbs/elapsed*cent)/cent),
zap.Uint64("completedContracts", c.completedAddrs),
zap.Float64("completedContracts/s", float64(c.completedAddrs)/elapsed),
zap.Uint64("entries", c.entryCount),
zap.Float64("entries/s", float64(c.entryCount)/elapsed),
zap.Float64("time", elapsed),
)
Comment on lines +52 to +60

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit (follow-up to @brbrr's earlier rounding request): MB and MB/s are now rounded to 2 decimals, but completedContracts/s, entries/s, and time are still emitted at full float precision — which is most of the noise in the example log line. Consider rounding them the same way:

Suggested change
fields = append(fields,
zap.Float64("MB", math.Round(mbs*cent)/cent),
zap.Float64("MB/s", math.Round(mbs/elapsed*cent)/cent),
zap.Uint64("completedContracts", c.completedAddrs),
zap.Float64("completedContracts/s", float64(c.completedAddrs)/elapsed),
zap.Uint64("entries", c.entryCount),
zap.Float64("entries/s", float64(c.entryCount)/elapsed),
zap.Float64("time", elapsed),
)
fields = append(fields,
zap.Float64("MB", math.Round(mbs*cent)/cent),
zap.Float64("MB/s", math.Round(mbs/elapsed*cent)/cent),
zap.Uint64("completedContracts", c.completedAddrs),
zap.Float64("completedContracts/s", math.Round(float64(c.completedAddrs)/elapsed*cent)/cent),
zap.Uint64("entries", c.entryCount),
zap.Float64("entries/s", math.Round(float64(c.entryCount)/elapsed*cent)/cent),
zap.Float64("time", math.Round(elapsed*cent)/cent),
)

c.logger.Info("write speed", fields...)

c.start = now
c.size = 0
c.completedAddrs = 0
c.entryCount = 0
}
Loading
Loading