Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

snapshot: use structured logger #9538

Merged
merged 2 commits into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions etcdctl/ctlv3/command/snapshot_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import (
"path/filepath"
"strings"

"github.com/coreos/etcd/pkg/logutil"
"github.com/coreos/etcd/pkg/types"
"github.com/coreos/etcd/snapshot"

"github.com/spf13/cobra"
"go.uber.org/zap"
)

const (
Expand Down Expand Up @@ -96,13 +96,13 @@ func snapshotSaveCommandFunc(cmd *cobra.Command, args []string) {
ExitWithError(ExitBadArgs, err)
}

lg := logutil.NewDiscardLogger()
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
ExitWithError(ExitError, err)
}
lg := zap.NewNop()
if debug {
lg = logutil.NewPackageLogger("github.com/coreos/etcd", "snapshot")
lg = zap.NewExample()
}
sp := snapshot.NewV3(mustClientFromCmd(cmd), lg)

Expand All @@ -120,13 +120,13 @@ func snapshotStatusCommandFunc(cmd *cobra.Command, args []string) {
}
initDisplayFromCmd(cmd)

lg := logutil.NewDiscardLogger()
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
ExitWithError(ExitError, err)
}
lg := zap.NewNop()
if debug {
lg = logutil.NewPackageLogger("github.com/coreos/etcd", "snapshot")
lg = zap.NewExample()
}
sp := snapshot.NewV3(nil, lg)

Expand Down Expand Up @@ -158,13 +158,13 @@ func snapshotRestoreCommandFunc(cmd *cobra.Command, args []string) {
walDir = filepath.Join(dataDir, "member", "wal")
}

lg := logutil.NewDiscardLogger()
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
ExitWithError(ExitError, err)
}
lg := zap.NewNop()
if debug {
lg = logutil.NewPackageLogger("github.com/coreos/etcd", "snapshot")
lg = zap.NewExample()
}
sp := snapshot.NewV3(nil, lg)

Expand Down
51 changes: 31 additions & 20 deletions snapshot/v3_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"github.com/coreos/etcd/mvcc"
"github.com/coreos/etcd/mvcc/backend"
"github.com/coreos/etcd/pkg/fileutil"
"github.com/coreos/etcd/pkg/logutil"
"github.com/coreos/etcd/pkg/types"
"github.com/coreos/etcd/raft"
"github.com/coreos/etcd/raft/raftpb"
Expand All @@ -44,6 +43,7 @@ import (
"github.com/coreos/etcd/wal/walpb"

bolt "github.com/coreos/bbolt"
"go.uber.org/zap"
)

// Manager defines snapshot methods.
Expand Down Expand Up @@ -97,15 +97,16 @@ type RestoreConfig struct {
// NewV3 returns a new snapshot Manager for v3.x snapshot.
// "*clientv3.Client" is only used for "Save" method.
// Otherwise, pass "nil".
func NewV3(cli *clientv3.Client, lg logutil.Logger) Manager {
func NewV3(cli *clientv3.Client, lg *zap.Logger) Manager {
if lg == nil {
lg = logutil.NewDiscardLogger()
lg = zap.NewExample()
}
return &v3Manager{cli: cli, logger: lg}
return &v3Manager{cli: cli, lg: lg}
}

type v3Manager struct {
cli *clientv3.Client
lg *zap.Logger

name string
dbPath string
Expand All @@ -114,7 +115,6 @@ type v3Manager struct {
cl *membership.RaftCluster

skipHashCheck bool
logger logutil.Logger
}

func (s *v3Manager) Save(ctx context.Context, dbPath string) error {
Expand All @@ -124,15 +124,18 @@ func (s *v3Manager) Save(ctx context.Context, dbPath string) error {
os.RemoveAll(partpath)
return fmt.Errorf("could not open %s (%v)", partpath, err)
}
s.logger.Infof("created temporary db file %q", partpath)
s.lg.Info(
"created temporary db file",
zap.String("path", partpath),
)

var rd io.ReadCloser
rd, err = s.cli.Snapshot(ctx)
if err != nil {
os.RemoveAll(partpath)
return err
}
s.logger.Infof("copying from snapshot stream")
s.lg.Info("copying from snapshot stream")
if _, err = io.Copy(f, rd); err != nil {
os.RemoveAll(partpath)
return err
Expand All @@ -146,7 +149,7 @@ func (s *v3Manager) Save(ctx context.Context, dbPath string) error {
return err
}

s.logger.Infof("renaming from %q to %q", partpath, dbPath)
s.lg.Info("rename", zap.String("from", partpath), zap.String("to", dbPath))
if err = os.Rename(partpath, dbPath); err != nil {
os.RemoveAll(partpath)
return fmt.Errorf("could not rename %s to %s (%v)", partpath, dbPath, err)
Expand Down Expand Up @@ -227,24 +230,36 @@ func (s *v3Manager) Restore(dbPath string, cfg RestoreConfig) error {
} else if _, err = os.Stat(walDir); err == nil {
return fmt.Errorf("wal-dir %q exists", walDir)
}
s.logger.Infof("restoring snapshot file %q to data-dir %q, wal-dir %q", dbPath, dataDir, walDir)

s.lg.Info(
"restoring snapshot file",
zap.String("path", dbPath),
zap.String("wal-dir", walDir),
zap.String("data-dir", dataDir),
zap.String("snap-dir", s.snapDir),
)

s.name = cfg.Name
s.dbPath = dbPath
s.walDir = walDir
s.snapDir = filepath.Join(dataDir, "member", "snap")
s.skipHashCheck = cfg.SkipHashCheck

s.logger.Infof("writing snapshot directory %q", s.snapDir)
if err = s.saveDB(); err != nil {
return err
}
s.logger.Infof("writing WAL directory %q and raft snapshot to %q", s.walDir, s.snapDir)
err = s.saveWALAndSnap()
if err == nil {
s.logger.Infof("finished restore %q to data directory %q, wal directory %q", dbPath, dataDir, walDir)
if err = s.saveWALAndSnap(); err != nil {
return err
}
return err

s.lg.Info(
"finished restoring snapshot file",
zap.String("path", dbPath),
zap.String("wal-dir", walDir),
zap.String("data-dir", dataDir),
zap.String("snap-dir", s.snapDir),
)
return nil
}

// saveDB copies the database snapshot to the snapshot directory
Expand Down Expand Up @@ -430,9 +445,5 @@ func (s *v3Manager) saveWALAndSnap() error {
return err
}

err := w.SaveSnapshot(walpb.Snapshot{Index: commit, Term: term})
if err == nil {
s.logger.Infof("wrote WAL snapshot to %q", s.walDir)
}
return err
return w.SaveSnapshot(walpb.Snapshot{Index: commit, Term: term})
}
9 changes: 5 additions & 4 deletions snapshot/v3_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import (

"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/embed"
"github.com/coreos/etcd/pkg/logutil"
"github.com/coreos/etcd/pkg/testutil"
"github.com/coreos/etcd/pkg/types"

"go.uber.org/zap"
)

// TestSnapshotV3RestoreSingle tests single node cluster restoring
Expand All @@ -51,7 +52,7 @@ func TestSnapshotV3RestoreSingle(t *testing.T) {
cfg.InitialCluster = fmt.Sprintf("%s=%s", cfg.Name, pURLs[0].String())
cfg.Dir = filepath.Join(os.TempDir(), fmt.Sprint(time.Now().Nanosecond()))

sp := NewV3(nil, logutil.NewPackageLogger("github.com/coreos/etcd", "snapshot"))
sp := NewV3(nil, zap.NewExample())

err := sp.Restore(dbPath, RestoreConfig{})
if err.Error() != `couldn't find local name "" in the initial cluster configuration` {
Expand Down Expand Up @@ -188,7 +189,7 @@ func createSnapshotFile(t *testing.T, kvs []kv) string {
}
}

sp := NewV3(cli, logutil.NewPackageLogger("github.com/coreos/etcd", "snapshot"))
sp := NewV3(cli, zap.NewExample())
dpPath := filepath.Join(os.TempDir(), fmt.Sprintf("snapshot%d.db", time.Now().Nanosecond()))
if err = sp.Save(context.Background(), dpPath); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -229,7 +230,7 @@ func restoreCluster(t *testing.T, clusterN int, dbPath string) (
cfg.InitialCluster = ics
cfg.Dir = filepath.Join(os.TempDir(), fmt.Sprint(time.Now().Nanosecond()+i))

sp := NewV3(nil, logutil.NewPackageLogger("github.com/coreos/etcd", "snapshot"))
sp := NewV3(nil, zap.NewExample())
if err := sp.Restore(dbPath, RestoreConfig{
Name: cfg.Name,
OutputDataDir: cfg.Dir,
Expand Down