From a5be2e6f7ff625055c6189ae91df65664bf4df3f Mon Sep 17 00:00:00 2001 From: vinllen Date: Sat, 13 Apr 2019 15:24:23 +0800 Subject: [PATCH] v1.2.3: polish log print to print more error info --- ChangeLog | 3 +++ src/redis-shake/common/common.go | 40 ++++++++++++++++++++++++++++---- src/redis-shake/common/utils.go | 23 +++++++++++------- src/redis-shake/sync.go | 6 ++++- 4 files changed, 57 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index f6648d0..c2d4540 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,6 @@ +2019-04-13 Alibaba Cloud. + * version: 1.2.3 + * IMPROVE: polish log print to print more error info. 2019-04-03 Alibaba Cloud. * version: 1.2.2 * BUGFIX: support 5.0 rdb RDB_OPCODE_MODULE_AUX, RDB_OPCODE_IDLE and diff --git a/src/redis-shake/common/common.go b/src/redis-shake/common/common.go index 6bfd69f..b0907cd 100644 --- a/src/redis-shake/common/common.go +++ b/src/redis-shake/common/common.go @@ -1,19 +1,49 @@ package utils -import( - logRotate "gopkg.in/natefinch/lumberjack.v2" +import ( + "net" + "fmt" + "strings" + "pkg/libs/bytesize" + + logRotate "gopkg.in/natefinch/lumberjack.v2" ) -const( +const ( GolangSecurityTime = "2006-01-02T15:04:05Z" // GolangSecurityTime = "2006-01-02 15:04:05" ReaderBufferSize = bytesize.MB * 32 WriterBufferSize = bytesize.MB * 8 ) -var( +var ( Version = "$" LogRotater *logRotate.Logger StartTime string -) \ No newline at end of file +) + +// read until hit the end of RESP: "\r\n" +func ReadRESPEnd(c net.Conn) (string, error) { + var ret string + for { + b := make([]byte, 1) + if _, err := c.Read(b); err != nil { + return "", fmt.Errorf("read error[%v], current return[%s]", err, ret) + } + + ret += string(b) + if strings.HasSuffix(ret, "\r\n") { + break + } + } + return ret, nil +} + +func RemoveRESPEnd(input string) string { + length := len(input) + if length >= 2 { + return input[: length - 2] + } + return input +} \ No newline at end of file diff --git a/src/redis-shake/common/utils.go b/src/redis-shake/common/utils.go index 2d52fea..3f38631 100644 --- a/src/redis-shake/common/utils.go +++ b/src/redis-shake/common/utils.go @@ -21,8 +21,8 @@ import ( "pkg/libs/stats" "pkg/rdb" "pkg/redis" - redigo "github.com/garyburd/redigo/redis" "redis-shake/configure" + redigo "github.com/garyburd/redigo/redis" ) func OpenRedisConn(target, auth_type, passwd string) redigo.Conn { @@ -42,6 +42,7 @@ func OpenNetConn(target, auth_type, passwd string) net.Conn { log.PanicErrorf(err, "cannot connect to '%s'", target) } + log.Infof("try to auth address[%v] with type[%v]", target, auth_type) AuthPassword(c, auth_type, passwd) return c } @@ -88,17 +89,19 @@ func SendPSyncListeningPort(c net.Conn, port int) { if err != nil { log.PanicError(errors.Trace(err), "write replconf listening-port failed") } - var b = make([]byte, 5) - if _, err := io.ReadFull(c, b); err != nil { + + ret, err := ReadRESPEnd(c) + if err != nil { log.PanicError(errors.Trace(err), "read auth response failed") } - if strings.ToUpper(string(b)) != "+OK\r\n" { - log.Panic("repl listening-port failed: ", string(b)) + if strings.ToUpper(ret) != "+OK\r\n" { + log.Panicf("repl listening-port failed[%v]", RemoveRESPEnd(ret)) } } func AuthPassword(c net.Conn, auth_type, passwd string) { if passwd == "" { + log.Infof("input password is empty, skip auth address[%v] with type[%v].", c.RemoteAddr(), auth_type) return } @@ -106,12 +109,13 @@ func AuthPassword(c net.Conn, auth_type, passwd string) { if err != nil { log.PanicError(errors.Trace(err), "write auth command failed") } - var b = make([]byte, 5) - if _, err := io.ReadFull(c, b); err != nil { + + ret, err := ReadRESPEnd(c) + if err != nil { log.PanicError(errors.Trace(err), "read auth response failed") } - if strings.ToUpper(string(b)) != "+OK\r\n" { - log.Panicf("auth failed[%s]", string(b)) + if strings.ToUpper(ret) != "+OK\r\n" { + log.Panicf("auth failed[%v]", RemoveRESPEnd(ret)) } } @@ -125,6 +129,7 @@ func OpenSyncConn(target string, auth_type, passwd string) (net.Conn, <-chan int func waitRdbDump(r io.Reader) <-chan int64 { size := make(chan int64) + // read rdb size go func() { var rsp string for { diff --git a/src/redis-shake/sync.go b/src/redis-shake/sync.go index acf139e..26a597e 100644 --- a/src/redis-shake/sync.go +++ b/src/redis-shake/sync.go @@ -119,7 +119,7 @@ func (cmd *CmdSync) Main() { } defer input.Close() - log.Infof("rdb file = %d\n", nsize) + log.Infof("rdb file size = %d\n", nsize) if sockfile != nil { r, w := pipe.NewFilePipe(int(conf.Options.SockFileSize), sockfile) @@ -134,6 +134,7 @@ func (cmd *CmdSync) Main() { input = r } + // start heartbeat if len(conf.Options.HeartbeatUrl) > 0 { heartbeatCtl := heartbeat.HeartbeatController{ ServerUrl: conf.Options.HeartbeatUrl, @@ -144,9 +145,11 @@ func (cmd *CmdSync) Main() { reader := bufio.NewReaderSize(input, utils.ReaderBufferSize) + // sync rdb base.Status = "full" cmd.SyncRDBFile(reader, target, conf.Options.TargetAuthType, conf.Options.TargetPasswordRaw, nsize) + // sync increment base.Status = "incr" close(cmd.wait_full) cmd.SyncCommand(reader, target, conf.Options.TargetAuthType, conf.Options.TargetPasswordRaw) @@ -183,6 +186,7 @@ func (cmd *CmdSync) SendPSyncCmd(master, auth_type, passwd string) (pipe.Reader, cmd.targetOffset.Set(offset) log.Infof("psync runid = %s offset = %d, fullsync", runid, offset) + // get rdb file size var nsize int64 for nsize == 0 { select {