func (cs *ClusterState) BuildClusterSnapshot() { // __CC__没什么意义,跟Region区别开即可 cluster := topo.NewCluster("__CC__") for _, ns := range cs.nodeStates { cluster.AddNode(ns.node) } err := cluster.BuildReplicaSets() // 出现这种情况,很可能是启动时节点还不全 if err != nil { log.Info("CLUSTER ", "Build cluster snapshot failed ", err) return } cs.cluster = cluster }
func (self *Inspector) initClusterTopo(seed *topo.Node) (*topo.Cluster, error) { resp, err := redis.ClusterNodesInRegion(seed.Addr(), self.LocalRegion) if err != nil && strings.HasPrefix(err.Error(), "ERR Wrong CLUSTER subcommand or number of arguments") { //server version do not support 'cluster nodes extra [region]' resp, err = redis.ClusterNodes(seed.Addr()) } if err != nil { return nil, err } cluster := topo.NewCluster(self.LocalRegion) var summary topo.SummaryInfo var nodeidx *topo.Node var cnt int lines := strings.Split(resp, "\n") cnt = 0 for _, line := range lines { if strings.HasPrefix(line, "# ") { summary.ReadLine(line) continue } line = strings.TrimSpace(line) if line == "" { continue } node, myself, err := self.buildNode(line) if err == ErrNodeInHandShake || err == ErrNodeNoAddr { continue } // Fix 'cluster nodes extra' & 'cluster nodes extra region' compatiable if node.Region != self.LocalRegion { continue } if err != nil { return nil, err } if node.Ip == "127.0.0.1" { node.Ip = seed.Ip } // 遇到myself,读取该节点的ClusterInfo if myself { info, err := redis.FetchClusterInfo(node.Addr()) if err != nil { return nil, err } node.ClusterInfo = info node.SummaryInfo = summary } cluster.AddNode(node) nodeidx = node cnt++ } if cnt == 1 { if nodeidx.IsMaster() && len(nodeidx.Ranges) == 0 { glog.Infof("Node %s is free node", nodeidx.Addr()) nodeidx.SetFree(true) } } return cluster, nil }