etcd-dump-logs: Migrate from zap to log for raw

Signed-off-by: Piotr Tabor <ptab@google.com>
This commit is contained in:
Piotr Tabor 2022-12-29 12:10:45 +01:00
parent 8ec3cbc551
commit 007858dc97
3 changed files with 15 additions and 16 deletions

View File

@ -96,7 +96,7 @@ and output a hex encoded line of binary for each input line`)
if wd == "" { if wd == "" {
wd = walDir(dataDir) wd = walDir(dataDir)
} }
readRaw(lg, index, wd, os.Stdout) readRaw(index, wd, os.Stdout)
} }
} }
@ -114,7 +114,7 @@ func readUsingReadAll(lg *zap.Logger, index *uint64, snapfile *string, dataDir s
walsnap.Index = *index walsnap.Index = *index
} else { } else {
if *snapfile == "" { if *snapfile == "" {
ss := snap.New(zap.NewExample(), snapDir(dataDir)) ss := snap.New(lg, snapDir(dataDir))
snapshot, err = ss.Load() snapshot, err = ss.Load()
} else { } else {
snapshot, err = snap.Read(lg, filepath.Join(snapDir(dataDir), *snapfile)) snapshot, err = snap.Read(lg, filepath.Join(snapDir(dataDir), *snapfile))
@ -381,7 +381,7 @@ func listEntriesType(entrytype string, streamdecoder string, ents []raftpb.Entry
printer(e) printer(e)
if streamdecoder == "" { if streamdecoder == "" {
fmt.Println() fmt.Println()
//continue continue
} }
// if decoder is set, pass the e.Data to stdin and read the stdout from decoder // if decoder is set, pass the e.Data to stdin and read the stdout from decoder

View File

@ -19,11 +19,10 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"path/filepath" "path/filepath"
"go.uber.org/zap"
"go.etcd.io/etcd/api/v3/etcdserverpb" "go.etcd.io/etcd/api/v3/etcdserverpb"
"go.etcd.io/etcd/client/pkg/v3/fileutil" "go.etcd.io/etcd/client/pkg/v3/fileutil"
"go.etcd.io/etcd/pkg/v3/pbutil" "go.etcd.io/etcd/pkg/v3/pbutil"
@ -32,19 +31,20 @@ import (
"go.etcd.io/raft/v3/raftpb" "go.etcd.io/raft/v3/raftpb"
) )
func readRaw(lg *zap.Logger, fromIndex *uint64, waldir string, out io.Writer) { func readRaw(fromIndex *uint64, waldir string, out io.Writer) {
var walReaders []fileutil.FileReader var walReaders []fileutil.FileReader
files, err := ioutil.ReadDir(waldir) files, err := ioutil.ReadDir(waldir)
if err != nil { if err != nil {
lg.Fatal("Failed to read directory.", zap.String("directory", waldir), zap.Error(err)) log.Fatalf("Error: Failed to read directory '%s' error:%v", waldir, err)
} }
for _, finfo := range files { for _, finfo := range files {
if filepath.Ext(finfo.Name()) != ".wal" { if filepath.Ext(finfo.Name()) != ".wal" {
lg.Warn("Ignoring not .wal file", zap.String("filename", finfo.Name())) log.Printf("Warning: Ignoring not .wal file: %s", finfo.Name())
continue
} }
f, err := os.Open(filepath.Join(waldir, finfo.Name())) f, err := os.Open(filepath.Join(waldir, finfo.Name()))
if err != nil { if err != nil {
lg.Fatal("Failed to read file", zap.String("filename", finfo.Name()), zap.Error(err)) log.Printf("Error: Failed to read file: %s . error:%v", finfo.Name(), err)
} }
walReaders = append(walReaders, fileutil.NewFileReader(f)) walReaders = append(walReaders, fileutil.NewFileReader(f))
} }
@ -56,10 +56,10 @@ func readRaw(lg *zap.Logger, fromIndex *uint64, waldir string, out io.Writer) {
err := decoder.Decode(&rec) err := decoder.Decode(&rec)
if err == nil || errors.Is(err, walpb.ErrCRCMismatch) { if err == nil || errors.Is(err, walpb.ErrCRCMismatch) {
if err != nil && !crcDesync { if err != nil && !crcDesync {
lg.Warn("Reading entry failed with CRC error", zap.Error(err)) log.Printf("Error: Reading entry failed with CRC error: %c", err)
crcDesync = true crcDesync = true
} }
printRec(lg, &rec, fromIndex, out) printRec(&rec, fromIndex, out)
if rec.Type == wal.CrcType { if rec.Type == wal.CrcType {
decoder.UpdateCRC(rec.Crc) decoder.UpdateCRC(rec.Crc)
crcDesync = false crcDesync = false
@ -70,13 +70,13 @@ func readRaw(lg *zap.Logger, fromIndex *uint64, waldir string, out io.Writer) {
fmt.Fprintf(out, "EOF: All entries were processed.\n") fmt.Fprintf(out, "EOF: All entries were processed.\n")
break break
} else { } else {
lg.Error("Reading failed", zap.Error(err)) log.Printf("Error: Reading failed: %v", err)
break break
} }
} }
} }
func printRec(lg *zap.Logger, rec *walpb.Record, fromIndex *uint64, out io.Writer) { func printRec(rec *walpb.Record, fromIndex *uint64, out io.Writer) {
switch rec.Type { switch rec.Type {
case wal.MetadataType: case wal.MetadataType:
var metadata etcdserverpb.Metadata var metadata etcdserverpb.Metadata
@ -102,6 +102,6 @@ func printRec(lg *zap.Logger, rec *walpb.Record, fromIndex *uint64, out io.Write
fmt.Fprintf(out, "HardState: %s\n", state.String()) fmt.Fprintf(out, "HardState: %s\n", state.String())
} }
default: default:
lg.Error("Unexpected WAL log type", zap.Int64("type", rec.Type)) log.Printf("Unexpected WAL log type: %d", rec.Type)
} }
} }

View File

@ -18,14 +18,13 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"go.uber.org/zap/zaptest"
) )
func Test_readRaw(t *testing.T) { func Test_readRaw(t *testing.T) {
path := t.TempDir() path := t.TempDir()
mustCreateWalLog(t, path) mustCreateWalLog(t, path)
var out bytes.Buffer var out bytes.Buffer
readRaw(zaptest.NewLogger(t), nil, walDir(path), &out) readRaw(nil, walDir(path), &out)
assert.Equal(t, assert.Equal(t,
`CRC: 0 `CRC: 0
Metadata: Metadata: