From 718a90555a21e9caf4798ab72051511ca08ec1bc Mon Sep 17 00:00:00 2001 From: aajkl Date: Thu, 4 Jul 2024 13:11:02 +0800 Subject: [PATCH] chore: better log and better error --- internal/eru/resources/gpu.go | 8 ++++---- internal/eru/resources/storage.go | 12 +++++++----- internal/rpc/grpc_server.go | 6 +++--- internal/service/boar/boar.go | 14 +++++++------- internal/utils/watcher.go | 4 ++-- internal/virt/guest/bot.go | 4 ++-- internal/virt/guest/guest.go | 6 +++--- yavirtd.go | 12 +++++++----- 8 files changed, 35 insertions(+), 31 deletions(-) diff --git a/internal/eru/resources/gpu.go b/internal/eru/resources/gpu.go index 58c0e46..ab211ff 100644 --- a/internal/eru/resources/gpu.go +++ b/internal/eru/resources/gpu.go @@ -3,7 +3,6 @@ package resources import ( "context" "encoding/json" - "errors" "fmt" "os/exec" "strings" @@ -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" @@ -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, ¶ms); err != nil { - return nil, err + return nil, errors.Wrap(err, "failed to unmarshal lshw output") } // map format: // diff --git a/internal/eru/resources/storage.go b/internal/eru/resources/storage.go index a6f2818..f9e7e21 100644 --- a/internal/eru/resources/storage.go +++ b/internal/eru/resources/storage.go @@ -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 { @@ -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 { @@ -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" { diff --git a/internal/rpc/grpc_server.go b/internal/rpc/grpc_server.go index d026f42..b5d2cb6 100644 --- a/internal/rpc/grpc_server.go +++ b/internal/rpc/grpc_server.go @@ -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...), diff --git a/internal/service/boar/boar.go b/internal/service/boar/boar.go index d9eab02..f48c63f 100644 --- a/internal/service/boar/boar.go +++ b/internal/service/boar/boar.go @@ -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 { @@ -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") } diff --git a/internal/utils/watcher.go b/internal/utils/watcher.go index 9d18d06..8758d8d 100644 --- a/internal/utils/watcher.go +++ b/internal/utils/watcher.go @@ -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 { @@ -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{} diff --git a/internal/virt/guest/bot.go b/internal/virt/guest/bot.go index 289fbfe..4101648 100644 --- a/internal/virt/guest/bot.go +++ b/internal/virt/guest/bot.go @@ -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 } diff --git a/internal/virt/guest/guest.go b/internal/virt/guest/guest.go index 27a520a..c81cd4d 100644 --- a/internal/virt/guest/guest.go +++ b/internal/virt/guest/guest.go @@ -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 @@ -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 { diff --git a/yavirtd.go b/yavirtd.go index c16f556..e5cf5fe 100644 --- a/yavirtd.go +++ b/yavirtd.go @@ -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") } } @@ -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() @@ -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 } @@ -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)