func apiGetSlots() (int, string) { slots, err := models.Slots(safeZkConn, globalEnv.ProductName()) if err != nil { log.ErrorErrorf(err, "Error getting slot info, try init slots first?") return 500, err.Error() } b, err := json.MarshalIndent(slots, " ", " ") return 200, string(b) }
func getLivingNodeInfos(zkConn zkhelper.Conn) ([]*NodeInfo, error) { groups, err := models.ServerGroups(zkConn, globalEnv.ProductName()) if err != nil { return nil, errors.Trace(err) } slots, err := models.Slots(zkConn, globalEnv.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, globalEnv.Password(), "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 != models.DEFAULT_SLOT_NUM { return nil, errors.Errorf("not all slots are online") } return ret, nil }
func apiInitSlots(r *http.Request) (int, string) { r.ParseForm() isForce := false val := r.FormValue("is_force") if len(val) > 0 && (val == "1" || val == "true") { isForce = true } if !isForce { s, _ := models.Slots(safeZkConn, globalEnv.ProductName()) if len(s) > 0 { return 500, "slots already initialized, you may use 'is_force' flag and try again." } } if err := models.InitSlotSet(safeZkConn, globalEnv.ProductName(), models.DEFAULT_SLOT_NUM); err != nil { log.ErrorErrorf(err, "init slot set failed") return 500, err.Error() } return jsonRetSucc() }