Skip to content

Commit

Permalink
chore: better log and better error
Browse files Browse the repository at this point in the history
  • Loading branch information
aajkl committed Jul 4, 2024
1 parent 068d4ad commit 718a905
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 31 deletions.
8 changes: 4 additions & 4 deletions internal/eru/resources/gpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package resources
import (
"context"
"encoding/json"
"errors"
"fmt"
"os/exec"
"strings"
Expand All @@ -17,6 +16,7 @@ import (
mapset "github.com/deckarep/golang-set/v2"
"github.com/patrickmn/go-cache"

"github.com/cockroachdb/errors"
"github.com/jaypipes/ghw"
"github.com/projecteru2/core/log"
"github.com/projecteru2/yavirt/configs"
Expand Down Expand Up @@ -176,16 +176,16 @@ func (g *GPUManager) monitor(ctx context.Context) {
func fetchGPUInfoFromHardware() (map[string]*SingleTypeGPUs, error) {
pci, err := ghw.PCI()
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to get pci info")
}

cmdOut, err := execCommand("lshw", "-quiet", "-json", "-C", "display").Output()
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to run lshw")
}
params := []map[string]any{}
if err = json.Unmarshal(cmdOut, &params); err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to unmarshal lshw output")
}
// map format:
//
Expand Down
12 changes: 7 additions & 5 deletions internal/eru/resources/storage.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package resources

import (
"os/exec"
"context"
"path"
"strings"

"github.com/cockroachdb/errors"
"github.com/dustin/go-humanize"
stotypes "github.com/projecteru2/resource-storage/storage/types"
"github.com/projecteru2/yavirt/pkg/sh"
)

type StorageManager struct {
Expand All @@ -29,11 +31,11 @@ func FetchStorage() (*stotypes.NodeResource, error) {
vols := stotypes.Volumes{}
// use df to fetch volume information

cmdOut, err := exec.Command("df", "-h").Output()
so, se, err := sh.ExecInOut(context.TODO(), nil, nil, "df", "-h")
if err != nil {
return nil, err
return nil, errors.Wrapf(err, "failed to run df: %s", string(se))
}
lines := strings.Split(string(cmdOut), "\n")
lines := strings.Split(string(so), "\n")
for _, line := range lines {
parts := strings.Fields(line)
if len(parts) != 6 {
Expand All @@ -42,7 +44,7 @@ func FetchStorage() (*stotypes.NodeResource, error) {
var size uint64
size, err = humanize.ParseBytes(parts[1])
if err != nil {
return nil, err
return nil, errors.Wrapf(err, "failed to parse size %s", parts[1])
}
mountPoint := parts[len(parts)-1]
if path.Base(mountPoint) == "eru" {
Expand Down
6 changes: 3 additions & 3 deletions internal/rpc/grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ func New(cfg *configs.Config, svc service.Service, quit chan struct{}) (*GRPCSer
return nil, err
}
if tlsCredentials != nil {
logger.Infof(context.TODO(), "grpc server tls enable.")
logger.Info(context.TODO(), "grpc server tls enabled")
opts = append(opts, grpc.Creds(tlsCredentials))
}
if cfg.Auth.Username != "" {
logger.Infof(context.TODO(), "grpc server auth enable.")
logger.Info(context.TODO(), "grpc server auth enabled")
auth := auth.NewAuth(cfg.Auth)
opts = append(opts, grpc.StreamInterceptor(auth.StreamInterceptor))
opts = append(opts, grpc.UnaryInterceptor(auth.UnaryInterceptor))
logger.Infof(context.TODO(), "username %s password %s", cfg.Auth.Username, cfg.Auth.Password)
logger.Debugf(context.TODO(), "username %s password %s", cfg.Auth.Username, cfg.Auth.Password)
}
srv := &GRPCServer{
server: grpc.NewServer(opts...),
Expand Down
14 changes: 7 additions & 7 deletions internal/service/boar/boar.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,24 @@ func New(ctx context.Context, cfg *configs.Config, t *testing.T) (br *Boar, err

resMgr, err := resources.Setup(ctx, cfg, t)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to setup resources")
}
cols = append(cols, resMgr.GetMetricsCollector())

br.Host, err = models.LoadHost()
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to load host")
}
br.pool, err = newTaskPool(cfg.MaxConcurrency)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to new task pool")
}
if err := idgen.Setup(br.Host.ID); err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to setup idgen")
}

if err = store.Setup(configs.Conf, t); err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to setup store")
}
go br.watchers.Run(ctx)
if err := vmcache.Setup(ctx, cfg, br.watchers); err != nil {
Expand All @@ -110,11 +110,11 @@ func New(ctx context.Context, cfg *configs.Config, t *testing.T) (br *Boar, err
return br, errors.Newf("invalid bind addr %s", cfg.BindGRPCAddr)
}
grpcPort := parts[1]
endpoint := fmt.Sprintf( //nolint
eruEndpoint := fmt.Sprintf( //nolint
"virt-grpc://%s:%s@%s:%s",
cfg.Auth.Username, cfg.Auth.Password, cfg.Host.Addr, grpcPort,
)
br.agt, err = agent.NewManager(ctx, br, &cfg.Eru, endpoint, t)
br.agt, err = agent.NewManager(ctx, br, &cfg.Eru, eruEndpoint, t)
if err != nil {
return br, errors.Wrap(err, "failed to setup agent")
}
Expand Down
4 changes: 2 additions & 2 deletions internal/utils/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (ws *Watchers) Stop() {
}

func (ws *Watchers) Run(ctx context.Context) {
defer log.Infof(ctx, "watchers loop has done")
defer log.Info(ctx, "watchers loop has done")

for {
select {
Expand All @@ -74,7 +74,7 @@ func (ws *Watchers) Watched(event types.Event) {
}

func (ws *Watchers) Notify(event types.Event) {
defer log.Infof(context.TODO(), "watchers notification has done")
defer log.Info(context.TODO(), "watchers notification has done")

stopped := []int64{}

Expand Down
4 changes: 2 additions & 2 deletions internal/virt/guest/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ func (v *bot) Migrate() error {
func (v *bot) Boot(ctx context.Context) error {
logger := log.WithFunc("Boot").WithField("guest", v.guest.ID)

logger.Infof(ctx, "Boot: stage1 -> Domain boot...")
logger.Info(ctx, "Boot: stage1 -> Domain boot...")
if err := v.dom.Boot(ctx); err != nil {
return err
}

logger.Infof(ctx, "Boot: stage2 -> Waiting GA...")
logger.Info(ctx, "Boot: stage2 -> Waiting GA...")
if err := v.waitGA(ctx); err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/virt/guest/guest.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,10 +735,10 @@ func (g *Guest) Destroy(ctx context.Context, force bool) (<-chan error, error) {

func (g *Guest) ProcessDestroy(ctx context.Context, force bool) error {
logger := log.WithFunc("Guest.ProcessDestroy").WithField("guest", g.ID)
logger.Infof(ctx, "begin to destroy guest")
logger.Info(ctx, "begin to destroy guest")
return g.botOperate(func(bot Bot) error {
if err := bot.Undefine(); err != nil {
logger.Errorf(ctx, err, "failed to undefine guest")
logger.Error(ctx, err, "failed to undefine guest")
return errors.Wrap(err, "")
}
// delete cloud-init iso
Expand All @@ -747,7 +747,7 @@ func (g *Guest) ProcessDestroy(ctx context.Context, force bool) error {

// try best behavior
if err := g.DeleteNetwork(); err != nil {
logger.Errorf(ctx, err, "failed to delete network")
logger.Error(ctx, err, "failed to delete network")
}

for _, vol := range g.Vols {
Expand Down
12 changes: 7 additions & 5 deletions yavirtd.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func startHTTPServer(cfg *configs.Config) {
ReadHeaderTimeout: 5 * time.Second,
}
if err := server.ListenAndServe(); err != nil {
log.Errorf(context.TODO(), err, "start http failed")
log.Error(context.TODO(), err, "start http failed")
}
}

Expand Down Expand Up @@ -140,6 +140,7 @@ func Run(c *cli.Context) error {

br, err := boar.New(ctx, &configs.Conf, nil)
if err != nil {
log.Error(c.Context, err, "failed to create boar")
return err
}
defer br.Close()
Expand All @@ -151,6 +152,7 @@ func Run(c *cli.Context) error {
quit := make(chan struct{})
grpcSrv, err := grpcserver.New(&configs.Conf, br, quit)
if err != nil {
log.Error(c.Context, err, "failed to create grpc server")
return err
}

Expand All @@ -160,17 +162,17 @@ func Run(c *cli.Context) error {
go func() {
defer close(errExitCh)
if err := grpcSrv.Serve(); err != nil {
log.Errorf(c.Context, err, "failed to start grpc server")
log.Error(c.Context, err, "failed to start grpc server")
metrics.IncrError()
}
}()
log.Infof(c.Context, "[main] all servers are running")
log.Info(c.Context, "[main] all servers are running")

select {
case <-ctx.Done():
log.Infof(c.Context, "[main] interrupt by signal")
log.Info(c.Context, "[main] interrupt by signal")
case <-errExitCh:
log.Warnf(c.Context, "[main] server exit abnormally.")
log.Warn(c.Context, "[main] server exit abnormally.")
}
close(quit)

Expand Down

0 comments on commit 718a905

Please sign in to comment.