Skip to content

Commit

Permalink
use zap in cmd/bootstrapper (#6237)
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 9, 2024
1 parent f064d5d commit 18904a3
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 43 deletions.
21 changes: 15 additions & 6 deletions cmd/bootstrapper/bootstrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"google.golang.org/grpc/credentials/insecure"

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

const (
Expand Down Expand Up @@ -89,24 +88,34 @@ var cmd = &cobra.Command{
targetEpochs = append(targetEpochs, types.EpochID(epoch))
}

log.JSONLog(true)
lvl, err := zap.ParseAtomicLevel(strings.ToLower(logLevel))
lvl, err := zap.ParseAtomicLevel(logLevel)
if err != nil {
return err
}
logger := log.NewWithLevel("", lvl)

logger, err := zap.Config{
Level: lvl,
Encoding: "json",
EncoderConfig: zap.NewProductionEncoderConfig(),
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
}.Build()
if err != nil {
return fmt.Errorf("creating logger: %w", err)
}

g := NewGenerator(
bitcoinEndpoint,
spacemeshEndpoint,
WithLogger(logger.WithName("generator")),
WithLogger(logger.Named("generator")),
)

ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
if serveUpdate {
srv := NewServer(g, genFallback, port,
WithSrvFilesystem(afero.NewOsFs()),
WithSrvLogger(logger.WithName("server")),
WithSrvLogger(logger.Named("server")),
WithBootstrapEpochs(targetEpochs),
)
return runServer(ctx, srv)
Expand Down
31 changes: 15 additions & 16 deletions cmd/bootstrapper/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import (

pb "github.com/spacemeshos/api/release/go/spacemesh/v1"
"github.com/spf13/afero"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

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

const (
Expand All @@ -35,7 +35,7 @@ func PersistedFilename(epoch types.EpochID, suffix string) string {
}

type Generator struct {
logger log.Log
logger *zap.Logger
fs afero.Fs
client *http.Client
btcEndpoint string
Expand All @@ -44,7 +44,7 @@ type Generator struct {

type Opt func(*Generator)

func WithLogger(logger log.Log) Opt {
func WithLogger(logger *zap.Logger) Opt {
return func(g *Generator) {
g.logger = logger
}
Expand All @@ -64,7 +64,7 @@ func WithHttpClient(c *http.Client) Opt {

func NewGenerator(btcEndpoint, smEndpoint string, opts ...Opt) *Generator {
g := &Generator{
logger: log.NewNop(),
logger: zap.NewNop(),
fs: afero.NewOsFs(),
client: &http.Client{},
btcEndpoint: btcEndpoint,
Expand Down Expand Up @@ -125,7 +125,7 @@ type BitcoinResponse struct {
Hash string `json:"hash"`
}

func (g *Generator) genBeacon(ctx context.Context, logger log.Log) (types.Beacon, error) {
func (g *Generator) genBeacon(ctx context.Context, logger *zap.Logger) (types.Beacon, error) {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
br, err := bitcoinHash(ctx, logger, g.client, g.btcEndpoint)
Expand All @@ -142,26 +142,25 @@ func (g *Generator) genBeacon(ctx context.Context, logger log.Log) (types.Beacon
return beacon, nil
}

func bitcoinHash(ctx context.Context, logger log.Log, client *http.Client, targetUrl string) (*BitcoinResponse, error) {
func bitcoinHash(
ctx context.Context,
logger *zap.Logger,
client *http.Client,
targetUrl string,
) (*BitcoinResponse, error) {
latest, err := queryBitcoin(ctx, client, targetUrl)
if err != nil {
return nil, err
}
logger.With().Info("latest bitcoin block height",
log.Uint64("height", latest.Height),
log.String("hash", latest.Hash),
)
logger.Info("latest bitcoin block height", zap.Uint64("height", latest.Height), zap.String("hash", latest.Hash))
height := latest.Height - confirmation

blockUrl := fmt.Sprintf("%s/blocks/%d", targetUrl, height)
confirmed, err := queryBitcoin(ctx, client, blockUrl)
if err != nil {
return nil, err
}
logger.With().Info("confirmed bitcoin block",
log.Uint64("height", confirmed.Height),
log.String("hash", confirmed.Hash),
)
logger.Info("confirmed bitcoin block", zap.Uint64("height", confirmed.Height), zap.String("hash", confirmed.Hash))
return confirmed, nil
}

Expand Down Expand Up @@ -257,8 +256,8 @@ func (g *Generator) GenUpdate(
if err != nil {
return "", fmt.Errorf("persist epoch update %v: %w", filename, err)
}
g.logger.With().Info("generated update",
log.String("filename", filename),
g.logger.Info("generated update",
zap.String("filename", filename),
)
return filename, nil
}
7 changes: 3 additions & 4 deletions cmd/bootstrapper/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/spacemeshos/go-spacemesh/bootstrap"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/datastore"
"github.com/spacemeshos/go-spacemesh/log/logtest"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/atxs"
)
Expand Down Expand Up @@ -143,7 +142,7 @@ func TestGenerator_Generate(t *testing.T) {
g := NewGenerator(
ts.URL,
cfg.PublicListener,
WithLogger(logtest.New(t)),
WithLogger(zaptest.NewLogger(t)),
WithFilesystem(fs),
WithHttpClient(ts.Client()),
)
Expand All @@ -170,9 +169,9 @@ func TestGenerator_CheckAPI(t *testing.T) {
t.Parallel()
targetEpoch := types.EpochID(3)
db := sql.InMemory()
lg := logtest.New(t)
lg := zaptest.NewLogger(t)
createAtxs(t, db, targetEpoch-1, types.RandomActiveSet(activeSetSize))
cfg, cleanup := launchServer(t, datastore.NewCachedDB(db, lg.Zap()))
cfg, cleanup := launchServer(t, datastore.NewCachedDB(db, lg))
t.Cleanup(cleanup)

fs := afero.NewMemMapFs()
Expand Down
25 changes: 11 additions & 14 deletions cmd/bootstrapper/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (
"time"

"github.com/spf13/afero"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"

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

const fileRegex = "/epoch-(?P<Epoch>[0-9]+)-update-(?P<Suffix>[a-z]+)"
Expand Down Expand Up @@ -45,7 +45,7 @@ func (np *NetworkParam) updateActiveSetTime(targetEpoch types.EpochID) time.Time
type Server struct {
*http.Server
eg errgroup.Group
logger log.Log
logger *zap.Logger
fs afero.Fs
gen *Generator
genFallback bool
Expand All @@ -55,7 +55,7 @@ type Server struct {

type SrvOpt func(*Server)

func WithSrvLogger(logger log.Log) SrvOpt {
func WithSrvLogger(logger *zap.Logger) SrvOpt {
return func(s *Server) {
s.logger = logger
}
Expand All @@ -76,7 +76,7 @@ func WithBootstrapEpochs(epochs []types.EpochID) SrvOpt {
func NewServer(gen *Generator, fallback bool, port int, opts ...SrvOpt) *Server {
s := &Server{
Server: &http.Server{Addr: fmt.Sprintf(":%d", port)},
logger: log.NewNop(),
logger: zap.NewNop(),
fs: afero.NewOsFs(),
gen: gen,
genFallback: fallback,
Expand All @@ -102,7 +102,7 @@ func (s *Server) Start(ctx context.Context, errCh chan error, params *NetworkPar
http.HandleFunc("/", s.handle)
http.HandleFunc("/checkpoint", s.handleCheckpoint)
http.HandleFunc("/updateCheckpoint", s.handleUpdate)
s.logger.With().Info("server starts serving", log.String("addr", ln.Addr().String()))
s.logger.Info("server starts serving", zap.Stringer("addr", ln.Addr()))
if err = s.Serve(ln); err != nil {
errCh <- err
return err
Expand Down Expand Up @@ -170,7 +170,7 @@ func (s *Server) genWithRetry(ctx context.Context, epoch types.EpochID, maxRetri
if err == nil {
return nil
}
s.logger.With().Debug("generate fallback active set retry", log.Err(err))
s.logger.Debug("generate fallback active set retry", zap.Error(err))

retries := 0
backoff := 10 * time.Second
Expand All @@ -180,7 +180,7 @@ func (s *Server) genWithRetry(ctx context.Context, epoch types.EpochID, maxRetri
select {
case <-timer.C:
if err := s.GenFallbackActiveSet(ctx, epoch); err != nil {
s.logger.With().Debug("generate fallback active set retry", log.Err(err))
s.logger.Debug("generate fallback active set retry", zap.Error(err))
retries++
if retries >= maxRetries {
return err
Expand Down Expand Up @@ -245,21 +245,21 @@ func getPartialActiveSet(ctx context.Context, smEndpoint string, targetEpoch typ
}

func (s *Server) Stop(ctx context.Context) {
s.logger.With().Info("shutting down server")
s.logger.Info("shutting down server")
s.Shutdown(ctx)
s.eg.Wait()
}

func (s *Server) handle(w http.ResponseWriter, r *http.Request) {
matches := s.regex.FindStringSubmatch(r.URL.String())
if len(matches) != 3 {
s.logger.With().Error("unrecognized url", log.String("url", r.URL.String()))
s.logger.Error("unrecognized url", zap.Stringer("url", r.URL))
w.WriteHeader(http.StatusNotFound)
return
}
e, err := strconv.Atoi(matches[1])
if err != nil {
s.logger.With().Error("unrecognized url", log.String("url", r.URL.String()), log.Err(err))
s.logger.Error("unrecognized url", zap.Stringer("url", r.URL), zap.Error(err))
w.WriteHeader(http.StatusNotFound)
return
}
Expand Down Expand Up @@ -310,8 +310,5 @@ func (s *Server) handleUpdate(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "save checkpoint err: %v", err)
return
}
s.logger.With().Info("saved checkpoint data",
log.String("data", data),
log.String("filename", filename),
)
s.logger.Info("saved checkpoint data", zap.String("data", data), zap.String("filename", filename))
}
5 changes: 2 additions & 3 deletions cmd/bootstrapper/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/spacemeshos/go-spacemesh/bootstrap"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/datastore"
"github.com/spacemeshos/go-spacemesh/log/logtest"
"github.com/spacemeshos/go-spacemesh/sql"
)

Expand Down Expand Up @@ -65,14 +64,14 @@ func TestServer(t *testing.T) {
g := NewGenerator(
"",
cfg.PublicListener,
WithLogger(logtest.New(t)),
WithLogger(zaptest.NewLogger(t)),
WithFilesystem(fs),
)

epochs := []types.EpochID{types.EpochID(4), types.EpochID(5)}
srv := NewServer(g, false, port,
WithSrvFilesystem(fs),
WithSrvLogger(logtest.New(t)),
WithSrvLogger(zaptest.NewLogger(t)),
WithBootstrapEpochs(epochs),
)
np := &NetworkParam{
Expand Down

0 comments on commit 18904a3

Please sign in to comment.