Skip to content

Commit 23cbd46

Browse files
committed
check input list is 'all master' or 'all slave', can't be mix
1 parent d4344cc commit 23cbd46

3 files changed

Lines changed: 74 additions & 16 deletions

File tree

src/full_check/client/address.go

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,52 @@ func HandleAddress(address, password, authType string) ([]string, error) {
3333
role = RoleMaster
3434
}
3535

36-
// create client to fetch
37-
client, err := NewRedisClient(RedisHost{
38-
Addr: []string{clusterList[0]},
39-
Password: password,
40-
Authtype: authType,
41-
}, 0)
36+
return fetchNodeList(clusterList[0], password, authType, role)
37+
} else {
38+
clusterList := strings.Split(address, AddressClusterSplitter)
39+
if len(clusterList) <= 1 {
40+
return clusterList, nil
41+
}
42+
43+
// fetch master
44+
masterList, err := fetchNodeList(clusterList[0], password, authType, common.TypeMaster)
4245
if err != nil {
43-
return nil, fmt.Errorf("fetch cluster info failed[%v]", err)
46+
return nil, err
47+
}
48+
// compare master list equal
49+
if common.CompareUnorderedList(masterList, clusterList) {
50+
return clusterList, nil
4451
}
4552

46-
if addressList, err := common.GetAllClusterNode(client.conn, role, "address"); err != nil {
47-
return nil, fmt.Errorf("fetch cluster node failed[%v]", err)
48-
} else {
49-
return addressList, nil
53+
slaveList, err := fetchNodeList(clusterList[0], password, authType, common.TypeSlave)
54+
if err != nil {
55+
return nil, err
56+
}
57+
// compare slave list equal
58+
if common.CompareUnorderedList(slaveList, clusterList) {
59+
return clusterList, nil
5060
}
61+
62+
return nil, fmt.Errorf("if type isn't cluster, should only used 1 node. if type is cluster, " +
63+
"input list should be all master or all slave: 'master1;master2;master3...' or " +
64+
"'slave1;slave2;slave3...'")
65+
}
66+
}
67+
68+
func fetchNodeList(oneNode, password, authType, role string) ([]string, error) {
69+
// create client to fetch
70+
client, err := NewRedisClient(RedisHost{
71+
Addr: []string{oneNode},
72+
Password: password,
73+
Authtype: authType,
74+
}, 0)
75+
if err != nil {
76+
return nil, fmt.Errorf("fetch cluster info failed[%v]", err)
77+
}
78+
79+
if addressList, err := common.GetAllClusterNode(client.conn, role, "address"); err != nil {
80+
return nil, fmt.Errorf("fetch cluster node failed[%v]", err)
5181
} else {
52-
clusterList := strings.Split(address, AddressClusterSplitter)
53-
return clusterList, nil
82+
return addressList, nil
5483
}
55-
}
84+
}

src/full_check/common/command.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,29 @@ func GetAllClusterNode(client redigo.Conn, role string, choose string) ([]string
129129
}
130130

131131
return result, nil
132+
}
133+
134+
// compare two unordered list. return true means equal.
135+
func CompareUnorderedList(a, b []string) bool {
136+
if len(a) != len(b) {
137+
return false
138+
}
139+
140+
if len(a) == 0 {
141+
return true
142+
}
143+
144+
setA := map[string]struct{}{}
145+
146+
for _, x := range a {
147+
setA[x] = struct{}{}
148+
}
149+
150+
for _, x := range b {
151+
if _, ok := setA[x]; !ok {
152+
return false
153+
}
154+
}
155+
156+
return true
132157
}

src/full_check/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,18 @@ func main() {
101101
}
102102

103103
sourceAddressList, err := client.HandleAddress(conf.Opts.SourceAddr, conf.Opts.SourcePassword, conf.Opts.SourceAuthType)
104-
if len(sourceAddressList) > 1 && conf.Opts.SourceDBType != 1 {
104+
if err != nil {
105+
panic(common.Logger.Errorf("source address[%v] illegal[%v]", conf.Opts.SourceAddr, err))
106+
} else if len(sourceAddressList) > 1 && conf.Opts.SourceDBType != 1 {
105107
panic(common.Logger.Errorf("looks like the source is cluster? please set sourcedbtype"))
106108
} else if len(sourceAddressList) == 0 {
107109
panic(common.Logger.Errorf("input source address is empty"))
108110
}
109111

110112
targetAddressList, err := client.HandleAddress(conf.Opts.TargetAddr, conf.Opts.TargetPassword, conf.Opts.TargetAuthType)
111-
if len(targetAddressList) > 1 && conf.Opts.TargetDBType != 1 {
113+
if err != nil {
114+
panic(common.Logger.Errorf("target address[%v] illegal[%v]", conf.Opts.TargetAddr, err))
115+
} else if len(targetAddressList) > 1 && conf.Opts.TargetDBType != 1 {
112116
panic(common.Logger.Errorf("looks like the target is cluster? please set targetdbtype"))
113117
} else if len(targetAddressList) == 0 {
114118
panic(common.Logger.Errorf("input target address is empty"))

0 commit comments

Comments
 (0)