2021-04-15 12:06:08 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package verify
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"runtime"
|
2023-05-16 13:45:21 +01:00
|
|
|
"sync"
|
|
|
|
"time"
|
2021-04-15 12:06:08 +01:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/common/memory"
|
2023-05-16 13:45:21 +01:00
|
|
|
"storj.io/storj/satellite/metabase/rangedloop"
|
2021-04-15 12:06:08 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// ProgressObserver counts and prints progress of metabase loop.
|
|
|
|
type ProgressObserver struct {
|
|
|
|
Log *zap.Logger
|
|
|
|
|
2023-05-16 13:45:21 +01:00
|
|
|
mu sync.Mutex
|
2021-04-15 12:06:08 +01:00
|
|
|
ProgressPrintFrequency int64
|
2023-05-16 13:45:21 +01:00
|
|
|
RemoteSegmentCount int64
|
|
|
|
InlineSegmentCount int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start is called at the beginning of each segment loop.
|
|
|
|
func (progress *ProgressObserver) Start(context.Context, time.Time) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fork creates a Partial to process a chunk of all the segments. It is
|
|
|
|
// called after Start. It is not called concurrently.
|
|
|
|
func (progress *ProgressObserver) Fork(context.Context) (rangedloop.Partial, error) {
|
|
|
|
return progress, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join is called for each partial returned by Fork.
|
|
|
|
func (progress *ProgressObserver) Join(context.Context, rangedloop.Partial) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finish is called after all segments are processed by all observers.
|
|
|
|
func (progress *ProgressObserver) Finish(context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process is called repeatedly with batches of segments.
|
|
|
|
func (progress *ProgressObserver) Process(ctx context.Context, segments []rangedloop.Segment) error {
|
|
|
|
progress.mu.Lock()
|
|
|
|
defer progress.mu.Unlock()
|
2021-04-15 12:06:08 +01:00
|
|
|
|
2023-05-16 13:45:21 +01:00
|
|
|
for _, segment := range segments {
|
|
|
|
if segment.Inline() {
|
|
|
|
progress.InlineSegmentCount++
|
|
|
|
} else {
|
|
|
|
progress.RemoteSegmentCount++
|
|
|
|
}
|
|
|
|
if (progress.RemoteSegmentCount+progress.InlineSegmentCount)%progress.ProgressPrintFrequency == 0 {
|
|
|
|
progress.Report()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2021-04-15 12:06:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Report reports the current progress.
|
|
|
|
func (progress *ProgressObserver) Report() {
|
|
|
|
progress.Log.Debug("progress",
|
|
|
|
zap.Int64("remote segments", progress.RemoteSegmentCount),
|
|
|
|
zap.Int64("inline segments", progress.InlineSegmentCount),
|
|
|
|
)
|
|
|
|
|
|
|
|
var m runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&m)
|
|
|
|
progress.Log.Debug("memory",
|
|
|
|
zap.String("Alloc", memory.Size(int64(m.Alloc)).String()),
|
|
|
|
zap.String("TotalAlloc", memory.Size(int64(m.TotalAlloc)).String()),
|
|
|
|
zap.String("Sys", memory.Size(int64(m.Sys)).String()),
|
|
|
|
zap.Uint32("NumGC", m.NumGC),
|
|
|
|
)
|
|
|
|
}
|