forked from yannh/redis-dump-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support dumping from redis clusters (#7)
Signed-off-by: Tamal Saha <[email protected]>
- Loading branch information
Showing
17 changed files
with
884 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package redisdump | ||
|
||
import ( | ||
"net" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/mediocregopher/radix/v3" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func ParseRedisInfo(s string) (map[string]string, error) { | ||
lines := strings.Split(s, "\n") | ||
|
||
info := map[string]string{} | ||
for _, line := range lines { | ||
line = strings.TrimSpace(line) | ||
if line == "" || strings.HasPrefix(line, "#") { | ||
continue | ||
} | ||
key, val, found := strings.Cut(line, ":") | ||
if !found { | ||
continue | ||
} | ||
info[key] = val | ||
} | ||
return info, nil | ||
} | ||
|
||
type Node struct { | ||
Host string | ||
Port int | ||
Slots []Range | ||
} | ||
|
||
type Range struct { | ||
Start, End int | ||
} | ||
|
||
func GetMasterNodeAddresses(s string) ([]Node, error) { | ||
lines := strings.Split(s, "\n") | ||
|
||
var masters []Node | ||
for _, line := range lines { | ||
if strings.Contains(line, "master") { | ||
fields := strings.FieldsFunc(line, func(r rune) bool { | ||
return r == ' ' || r == '@' | ||
}) | ||
|
||
host, port, err := net.SplitHostPort(fields[1]) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to split addr %s", fields[1]) | ||
} | ||
p, err := strconv.Atoi(port) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to parse port in addr %s", fields[1]) | ||
} | ||
m := Node{Host: host, Port: p} | ||
|
||
for i := len(fields) - 1; i >= 0; i-- { | ||
start, end, found := strings.Cut(fields[i], "-") | ||
if !found { | ||
break | ||
} | ||
s, err := strconv.Atoi(start) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to parse slot start for redis master %s with slots %s", m.Host, fields[i]) | ||
} | ||
e, err := strconv.Atoi(end) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to parse slot end for redis master %s with slots %s", m.Host, fields[i]) | ||
} | ||
m.Slots = append(m.Slots, Range{Start: s, End: e}) | ||
} | ||
masters = append(masters, m) | ||
} | ||
} | ||
return masters, nil | ||
} | ||
|
||
func GetHosts(s Host, nWorkers int) ([]Host, error) { | ||
client, err := NewClient(s, nil, nWorkers) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer client.Close() | ||
|
||
var val string | ||
err = client.Do(radix.Cmd(&val, "INFO")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
info, err := ParseRedisInfo(val) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if info["cluster_enabled"] == "0" { | ||
return []Host{s}, nil | ||
} | ||
|
||
err = client.Do(radix.Cmd(&val, "CLUSTER", "nodes")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
masters, err := GetMasterNodeAddresses(val) | ||
if err != nil { | ||
panic(err) | ||
} | ||
hosts := make([]Host, 0, len(masters)) | ||
for _, m := range masters { | ||
scopy := s | ||
scopy.Host = m.Host | ||
scopy.Port = m.Port | ||
hosts = append(hosts, scopy) | ||
} | ||
return hosts, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.