@@ -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+ }
0 commit comments