From 2bb5e2d71b2fffb2b9439c53af554564124de236 Mon Sep 17 00:00:00 2001 From: vinllen Date: Tue, 21 May 2019 11:16:52 +0800 Subject: [PATCH] add cluster node info --- src/redis-shake/common/command.go | 68 +++++++++++++++++++++++++++++++ src/redis-shake/sync.go | 2 +- src/vendor/vendor.json | 6 +++ 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/src/redis-shake/common/command.go b/src/redis-shake/common/command.go index 4f71df9..bfc619d 100644 --- a/src/redis-shake/common/command.go +++ b/src/redis-shake/common/command.go @@ -4,8 +4,21 @@ import ( "bytes" "fmt" "strconv" + "redis-shake/configure" ) +type ClusterNodeInfo struct { + Id string + Address string + Flags string + Master string + PingSent string + PongRecv string + ConfigEpoch string + LinkStat string + Slot string +} + func ParseKeyspace(content []byte) (map[int32]int64, error) { if bytes.HasPrefix(content, []byte("# Keyspace")) == false { return nil, fmt.Errorf("invalid info Keyspace: %s", string(content)) @@ -34,4 +47,59 @@ func ParseKeyspace(content []byte) (map[int32]int64, error) { } // end true } // end for return reply, nil +} + +/* + * 10.1.1.1:21331> cluster nodes + * d49a4c7b516b8da222d46a0a589b77f381285977 10.1.1.1:21333@31333 master - 0 1557996786000 3 connected 10923-16383 + * f23ba7be501b2dcd4d6eeabd2d25551513e5c186 10.1.1.1:21336@31336 slave d49a4c7b516b8da222d46a0a589b77f381285977 0 1557996785000 6 connected + * 75fffcd521738606a919607a7ddd52bcd6d65aa8 10.1.1.1:21331@31331 myself,master - 0 1557996784000 1 connected 0-5460 + * da3dd51bb9cb5803d99942e0f875bc5f36dc3d10 10.1.1.1:21332@31332 master - 0 1557996786260 2 connected 5461-10922 + * eff4e654d3cc361a8ec63640812e394a8deac3d6 10.1.1.1:21335@31335 slave da3dd51bb9cb5803d99942e0f875bc5f36dc3d10 0 1557996787261 5 connected + * 486e081f8d47968df6a7e43ef9d3ba93b77d03b2 10.1.1.1:21334@31334 slave 75fffcd521738606a919607a7ddd52bcd6d65aa8 0 1557996785258 4 connected + */ +func ParseClusterNode(content []byte) []*ClusterNodeInfo { + lines := bytes.Split(content, []byte("\r\n")) + ret := make([]*ClusterNodeInfo, 0, len(lines)) + for _, line := range lines { + items := bytes.Split(line, []byte(" ")) + + address := bytes.Split(items[1], []byte{'@'}) + flag := bytes.Split(items[2], []byte{','}) + var role string + if len(flag) > 1 { + role = string(flag[1]) + } else { + role = string(flag[0]) + } + var slot string + if len(items) > 7 { + slot = string(items[7]) + } + ret = append(ret, &ClusterNodeInfo{ + Id: string(items[0]), + Address: string(address[0]), + Flags: role, + Master: string(items[3]), + PingSent: string(items[4]), + PongRecv: string(items[5]), + ConfigEpoch: string(items[6]), + LinkStat: string(items[7]), + Slot: slot, + }) + } + return ret +} + +// needMaster: true(master), false(slave) +func ClusterNodeChoose(input []*ClusterNodeInfo, needMaster bool) []*ClusterNodeInfo { + ret := make([]*ClusterNodeInfo, 0, len(input) / 2) + for _, ele := range input { + if ele.Flags == conf.StandAloneRoleMaster && needMaster { + ret = append(ret, ele) + } else if ele.Flags == conf.StandAloneRoleSlave && !needMaster { + ret = append(ret, ele) + } + } + return ret } \ No newline at end of file diff --git a/src/redis-shake/sync.go b/src/redis-shake/sync.go index 603c2ac..ebbb7dc 100644 --- a/src/redis-shake/sync.go +++ b/src/redis-shake/sync.go @@ -698,7 +698,7 @@ func (ds *dbSyncer) syncCommand(reader *bufio.Reader, target, auth_type, passwd fmt.Fprintf(&b, "dbSyncer[%v] sync: ", ds.id) fmt.Fprintf(&b, " +forward=%-6d", nstat.forward-lstat.forward) fmt.Fprintf(&b, " +nbypass=%-6d", nstat.nbypass-lstat.nbypass) - fmt.Fprintf(&b, " +nbytes=%d", nstat.wbytes-lstat.wbytes) + fmt.Fprintf(&b, " +nbytes=%d", nstat.wbytes-lstat.wbytes) // todo log.Info(b.String()) lstat = nstat } diff --git a/src/vendor/vendor.json b/src/vendor/vendor.json index e4b7d1f..6f13072 100644 --- a/src/vendor/vendor.json +++ b/src/vendor/vendor.json @@ -8,6 +8,12 @@ "revision": "76bd05e8e22f9f8f5e1dd6d3a85b7951da7cce57", "revisionTime": "2017-12-04T08:54:13Z" }, + { + "checksumSHA1": "A7qoimg0p6dsVIQNpjX0uW2UjoM=", + "path": "github.com/chasex/redis-go-cluster", + "revision": "222d81891f1d3fa7cf8b5655020352c3e5b4ec0f", + "revisionTime": "2016-12-07T02:39:22Z" + }, { "checksumSHA1": "1/Kf0ihi6RtfNt0JjAtZLKvfqJY=", "path": "github.com/cupcake/rdb",