Beispiel #1
0
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
	}

	conn := CreateCoordConn()
	defer conn.Close()

	if !isForce {
		s, _ := models.Slots(conn, globalEnv.ProductName())
		if len(s) > 0 {
			return 500, "slots already initialized, you may use 'is_force' flag and try again."
		}
	}

	if err := models.InitSlotSet(conn, globalEnv.ProductName(), models.DEFAULT_SLOT_NUM); err != nil {
		log.Warning(err)
		return 500, err.Error()
	}
	return jsonRetSucc()
}
Beispiel #2
0
func apiGetSlots() (int, string) {
	conn := CreateCoordConn()
	defer conn.Close()

	slots, err := models.Slots(conn, globalEnv.ProductName())
	if err != nil {
		log.Warning("Error getting slot info, try init slots first? err: ", err)
		return 500, err.Error()
	}

	b, err := json.MarshalIndent(slots, " ", "  ")
	return 200, string(b)
}
Beispiel #3
0
func getLivingNodeInfos(coordConn zkhelper.Conn) ([]*NodeInfo, error) {
	groups, err := models.ServerGroups(coordConn, globalEnv.ProductName())
	if err != nil {
		return nil, errors.Trace(err)
	}
	slots, err := models.Slots(coordConn, 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(coordConn)
		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", globalEnv.StoreAuth())
		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.New("not all slots are online")
	}
	return ret, nil
}