Skip to content

Commit

Permalink
use zap in bootstrap (#6212)
Browse files Browse the repository at this point in the history
## Motivation

Part of the effort to migrate to zap.
  • Loading branch information
poszu committed Aug 5, 2024
1 parent fc05cbf commit 6e32527
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
27 changes: 14 additions & 13 deletions bootstrap/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

"github.com/santhosh-tekuri/jsonschema/v5"
"github.com/spf13/afero"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"

"github.com/spacemeshos/go-spacemesh/common/types"
Expand Down Expand Up @@ -71,7 +72,7 @@ func DefaultConfig() Config {

type Updater struct {
cfg Config
logger log.Log
logger *zap.Logger
clock layerClock
fs afero.Fs
client *http.Client
Expand All @@ -92,7 +93,7 @@ func WithConfig(cfg Config) Opt {
}
}

func WithLogger(logger log.Log) Opt {
func WithLogger(logger *zap.Logger) Opt {
return func(u *Updater) {
u.logger = logger
}
Expand All @@ -113,7 +114,7 @@ func WithHttpClient(c *http.Client) Opt {
func New(clock layerClock, opts ...Opt) *Updater {
u := &Updater{
cfg: DefaultConfig(),
logger: log.NewNop(),
logger: zap.NewNop(),
clock: clock,
fs: afero.NewOsFs(),
client: &http.Client{},
Expand Down Expand Up @@ -149,7 +150,7 @@ func (u *Updater) Load(ctx context.Context) error {
if err = u.updateAndNotify(ctx, verified); err != nil {
return err
}
u.logger.With().Info("loaded bootstrap file", log.Inline(verified))
u.logger.Info("loaded bootstrap file", zap.Inline(verified))
u.addUpdate(verified.Data.Epoch, verified.Persisted[len(verified.Persisted)-suffixLen:])
}
return nil
Expand All @@ -165,14 +166,14 @@ func (u *Updater) Start() error {
if err := u.Load(ctx); err != nil {
return err
}
u.logger.With().Info("start listening to update",
log.String("source", u.cfg.URL),
log.Duration("interval", u.cfg.Interval),
u.logger.Info("start listening to update",
zap.String("source", u.cfg.URL),
zap.Duration("interval", u.cfg.Interval),
)
for {
if err := u.DoIt(ctx); err != nil {
updateFailureCount.Add(1)
u.logger.With().Debug("failed to get bootstrap update", log.Err(err))
u.logger.Debug("failed to get bootstrap update", zap.Error(err))
}
select {
case <-u.stop:
Expand Down Expand Up @@ -233,10 +234,10 @@ func (u *Updater) DoIt(ctx context.Context) error {
current := u.clock.CurrentLayer().GetEpoch()
defer func() {
if err := u.prune(current); err != nil {
u.logger.With().Error("failed to prune",
log.Context(ctx),
log.Uint32("current epoch", current.Uint32()),
log.Err(err),
u.logger.Error("failed to prune",
log.ZContext(ctx),
zap.Uint32("current epoch", current.Uint32()),
zap.Error(err),
)
}
}()
Expand Down Expand Up @@ -291,7 +292,7 @@ func (u *Updater) checkEpochUpdate(
return nil, false, fmt.Errorf("persist bootstrap %s: %w", filename, err)
}
verified.Persisted = filename
u.logger.WithContext(ctx).With().Info("new bootstrap file", log.Inline(verified))
u.logger.Info("new bootstrap file", log.ZContext(ctx), zap.Inline(verified))
if err = u.updateAndNotify(ctx, verified); err != nil {
return verified, false, err
}
Expand Down
24 changes: 12 additions & 12 deletions bootstrap/updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"go.uber.org/zap/zaptest"
"golang.org/x/sync/errgroup"

"github.com/spacemeshos/go-spacemesh/bootstrap"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/log/logtest"
)

const (
Expand Down Expand Up @@ -196,7 +196,7 @@ func TestLoad(t *testing.T) {
updater := bootstrap.New(
mc,
bootstrap.WithConfig(cfg),
bootstrap.WithLogger(logtest.New(t)),
bootstrap.WithLogger(zaptest.NewLogger(t)),
bootstrap.WithFilesystem(fs),
)
ch, err := updater.Subscribe()
Expand Down Expand Up @@ -248,7 +248,7 @@ func TestLoadedNotDownloadedAgain(t *testing.T) {
updater := bootstrap.New(
mc,
bootstrap.WithConfig(cfg),
bootstrap.WithLogger(logtest.New(t)),
bootstrap.WithLogger(zaptest.NewLogger(t)),
bootstrap.WithFilesystem(fs),
)
ch, err := updater.Subscribe()
Expand Down Expand Up @@ -276,7 +276,7 @@ func TestStartClose(t *testing.T) {
updater := bootstrap.New(
mc,
bootstrap.WithConfig(cfg),
bootstrap.WithLogger(logtest.New(t)),
bootstrap.WithLogger(zaptest.NewLogger(t)),
bootstrap.WithFilesystem(fs),
)
ch, err := updater.Subscribe()
Expand Down Expand Up @@ -322,7 +322,7 @@ func TestPrune(t *testing.T) {
updater := bootstrap.New(
mc,
bootstrap.WithConfig(cfg),
bootstrap.WithLogger(logtest.New(t)),
bootstrap.WithLogger(zaptest.NewLogger(t)),
bootstrap.WithFilesystem(fs),
bootstrap.WithHttpClient(ts.Client()),
)
Expand Down Expand Up @@ -384,7 +384,7 @@ func TestDoIt(t *testing.T) {
updater := bootstrap.New(
mc,
bootstrap.WithConfig(cfg),
bootstrap.WithLogger(logtest.New(t)),
bootstrap.WithLogger(zaptest.NewLogger(t)),
bootstrap.WithFilesystem(fs),
bootstrap.WithHttpClient(ts.Client()),
)
Expand Down Expand Up @@ -421,7 +421,7 @@ func TestEmptyResponse(t *testing.T) {
updater := bootstrap.New(
mc,
bootstrap.WithConfig(cfg),
bootstrap.WithLogger(logtest.New(t)),
bootstrap.WithLogger(zaptest.NewLogger(t)),
bootstrap.WithFilesystem(fs),
bootstrap.WithHttpClient(ts.Client()),
)
Expand Down Expand Up @@ -492,7 +492,7 @@ func TestGetInvalidUpdate(t *testing.T) {
updater := bootstrap.New(
mc,
bootstrap.WithConfig(cfg),
bootstrap.WithLogger(logtest.New(t)),
bootstrap.WithLogger(zaptest.NewLogger(t)),
bootstrap.WithFilesystem(fs),
bootstrap.WithHttpClient(ts.Client()),
)
Expand Down Expand Up @@ -526,7 +526,7 @@ func TestNoNewUpdate(t *testing.T) {
updater := bootstrap.New(
mc,
bootstrap.WithConfig(cfg),
bootstrap.WithLogger(logtest.New(t)),
bootstrap.WithLogger(zaptest.NewLogger(t)),
bootstrap.WithFilesystem(fs),
bootstrap.WithHttpClient(ts.Client()),
)
Expand Down Expand Up @@ -647,7 +647,7 @@ func TestRequiredEpochs(t *testing.T) {
updater := bootstrap.New(
mc,
bootstrap.WithConfig(cfg),
bootstrap.WithLogger(logtest.New(t)),
bootstrap.WithLogger(zaptest.NewLogger(t)),
bootstrap.WithFilesystem(fs),
bootstrap.WithHttpClient(ts.Client()),
)
Expand Down Expand Up @@ -676,7 +676,7 @@ func TestIntegration(t *testing.T) {
updater := bootstrap.New(
mc,
bootstrap.WithConfig(cfg),
bootstrap.WithLogger(logtest.New(t)),
bootstrap.WithLogger(zaptest.NewLogger(t)),
bootstrap.WithFilesystem(fs),
)
ch, err := updater.Subscribe()
Expand Down Expand Up @@ -709,7 +709,7 @@ func TestClose(t *testing.T) {
updater := bootstrap.New(
mc,
bootstrap.WithConfig(cfg),
bootstrap.WithLogger(logtest.New(t)),
bootstrap.WithLogger(zaptest.NewLogger(t)),
bootstrap.WithFilesystem(fs),
)
ch, err := updater.Subscribe()
Expand Down
2 changes: 1 addition & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ func (app *App) initServices(ctx context.Context) error {
app.updater = bootstrap.New(
app.clock,
bootstrap.WithConfig(bscfg),
bootstrap.WithLogger(app.addLogger(BootstrapLogger, lg)),
bootstrap.WithLogger(app.addLogger(BootstrapLogger, lg).Zap()),
)
if app.Config.Certificate.CommitteeSize == 0 {
app.log.With().Warning("certificate committee size is not set, defaulting to hare committee size",
Expand Down

0 comments on commit 6e32527

Please sign in to comment.