func BeforePromote(oldMaster string) error { conn, _ := zkhelper.ConnectToZk(*zkAddr) defer conn.Close() groups, err := models.ServerGroups(conn, *productName) if err != nil { log.Errorf("get server groups error %v, give up failover", err) return failover.ErrGiveupFailover } found := false for _, group := range groups { for _, server := range group.Servers { if server.Addr == oldMaster { found = true break } } } if !found { log.Errorf("can not find %s in any groups, give up failover", oldMaster) return failover.ErrGiveupFailover } return nil }
func runListServerGroup() error { groups, err := models.ServerGroups(zkConn, productName) if err != nil { log.Warning(err) return err } b, _ := json.MarshalIndent(groups, " ", " ") fmt.Println(string(b)) return nil }
func getLivingNodeInfos(zkConn zkhelper.Conn) ([]*NodeInfo, error) { groups, err := models.ServerGroups(zkConn, productName) if err != nil { return nil, errors.Trace(err) } slots, err := models.Slots(zkConn, productName) slotMap := make(map[int][]int) for _, slot := range slots { if slot.State.Status == models.SLOT_STATUS_ONLINE { slotMap[slot.GroupId] = append(slotMap[slot.GroupId], slot.Id) } } var ret []*NodeInfo for _, g := range groups { master, err := g.Master(zkConn) if err != nil { return nil, errors.Trace(err) } if master == nil { return nil, errors.Errorf("group %d has no master", g.Id) } out, err := utils.GetRedisConfig(master.Addr, "maxmemory") if err != nil { return nil, errors.Trace(err) } maxMem, err := strconv.ParseInt(out, 10, 64) if err != nil { return nil, errors.Trace(err) } if maxMem <= 0 { return nil, errors.Errorf("redis %s should set maxmemory", master.Addr) } node := &NodeInfo{ GroupId: g.Id, CurSlots: slotMap[g.Id], MaxMemory: maxMem, } ret = append(ret, node) } cnt := 0 for _, info := range ret { cnt += len(info.CurSlots) } if cnt != slot_num { return nil, errors.New("not all slots are online") } return ret, nil }
func Promote(oldMaster string, newMaster string) error { conn, _ := zkhelper.ConnectToZk(*zkAddr) defer conn.Close() groups, err := models.ServerGroups(conn, *productName) if err != nil { return err } var groupId int found := false for _, group := range groups { for _, server := range group.Servers { if server.Addr == oldMaster { groupId = group.Id found = true break } } } if !found { return fmt.Errorf("can not find %s in any groups", oldMaster) } lock := utils.GetZkLock(conn, *productName) lock.Lock(fmt.Sprintf("promote server %+v", newMaster)) defer func() { err := lock.Unlock() if err != nil { log.Warning(err) } }() group, err := models.GetGroup(conn, *productName, groupId) if err != nil { return err } err = group.Promote(conn, newMaster) if err != nil { return err } return nil }