Exemple #1
0
// ROLE
func RoleCmd(s Session, args [][]byte) (redis.Resp, error) {
	if len(args) != 0 {
		return toRespErrorf("len(args) = %d, expect = 0", len(args))
	}

	c, _ := s.(*conn)
	if c == nil {
		return nil, errors.New("invalid connection")
	}

	ay := redis.NewArray()
	if masterAddr := c.h.masterAddr.Get(); masterAddr == "" {
		// master
		ay.Append(redis.NewBulkBytesWithString("master"))
		c.h.repl.RLock()
		defer c.h.repl.RUnlock()

		ay.Append(redis.NewInt(c.h.repl.masterOffset))
		slaves := redis.NewArray()
		for slave, _ := range c.h.repl.slaves {
			a := redis.NewArray()
			if addr := slave.nc.RemoteAddr(); addr == nil {
				continue
			} else {
				a.Append(redis.NewBulkBytesWithString(strings.Split(addr.String(), ":")[0]))
			}
			a.Append(redis.NewBulkBytesWithString(fmt.Sprintf("%d", slave.listeningPort.Get())))
			a.Append(redis.NewBulkBytesWithString(fmt.Sprintf("%d", slave.syncOffset.Get())))
			slaves.Append(a)
		}

		ay.Append(slaves)
	} else {
		// slave
		ay.Append(redis.NewBulkBytesWithString("slave"))
		seps := strings.Split(masterAddr, ":")
		if len(seps) == 2 {
			port, err := strconv.ParseInt(seps[1], 10, 16)
			if err != nil {
				return toRespError(err)
			}
			ay.Append(redis.NewBulkBytesWithString(seps[0]))
			ay.Append(redis.NewInt(int64(port)))
		} else {
			return toRespErrorf("invalid master addr, must ip:port, but %s", masterAddr)
		}
		ay.Append(redis.NewBulkBytesWithString(c.h.masterConnState.Get()))
		ay.Append(redis.NewInt(c.h.syncOffset.Get()))
	}
	return ay, nil
}
Exemple #2
0
// SLOTSHASHKEY key [key...]
func SlotsHashKeyCmd(s Session, args [][]byte) (redis.Resp, error) {
	resp := redis.NewArray()
	for _, key := range args {
		_, slot := store.HashKeyToSlot(key)
		resp.AppendInt(int64(slot))
	}
	return resp, nil
}
Exemple #3
0
// SLOTSINFO [start [count]]
func SlotsInfoCmd(s Session, args [][]byte) (redis.Resp, error) {
	if m, err := s.Store().SlotsInfo(s.DB(), args); err != nil {
		return toRespError(err)
	} else {
		resp := redis.NewArray()
		for i := uint32(0); i < store.MaxSlotNum; i++ {
			v, ok := m[i]
			if ok {
				s := redis.NewArray()
				s.AppendInt(int64(i))
				s.AppendInt(v)
				resp.Append(s)
			}
		}
		return resp, nil
	}
}
Exemple #4
0
func doMigrate(addr string, timeout time.Duration, db uint32, bins []*rdb.BinEntry) error {
	c, err := getSockConn(addr, timeout)
	if err != nil {
		log.Warningf("connect to %s failed, timeout = %d, err = %s", addr, timeout, err)
		return errors.Trace(err)
	}
	defer putSockConn(addr, c)

	cmd1 := redis.NewArray()
	cmd1.AppendBulkBytes([]byte("select"))
	cmd1.AppendBulkBytes([]byte(FormatUint(uint64(db))))

	if err := c.DoMustOK(cmd1, timeout); err != nil {
		log.Warningf("command select failed, addr = %s, db = %d, err = %s", addr, db, err)
		return errors.Trace(err)
	}
	log.Debugf("command select ok, addr = %s, db = %d, err = %s", addr, db, err)

	cmd2 := redis.NewArray()
	cmd2.AppendBulkBytes([]byte("slotsrestore"))
	for _, bin := range bins {
		cmd2.AppendBulkBytes(bin.Key)
		ttlms := int64(0)
		if bin.ExpireAt != 0 {
			if v, ok := ExpireAtToTTLms(int64(bin.ExpireAt)); ok && v > 0 {
				ttlms = v
			} else {
				ttlms = 1
			}
		}
		cmd2.AppendBulkBytes([]byte(FormatInt(ttlms)))
		cmd2.AppendBulkBytes(bin.Value)
	}

	if err := c.DoMustOK(cmd2, timeout); err != nil {
		log.Warningf("command restore failed, addr = %s, db = %d, len(bins) = %d, err = %s", addr, db, len(bins), err)
		return errors.Trace(err)
	} else {
		log.Debugf("command restore ok, addr = %s, db = %d, len(bins) = %d", addr, db, len(bins))
		return nil
	}
}
Exemple #5
0
// HVALS key
func HValsCmd(s Session, args [][]byte) (redis.Resp, error) {
	if a, err := s.Store().HVals(s.DB(), args); err != nil {
		return toRespError(err)
	} else {
		resp := redis.NewArray()
		for _, v := range a {
			resp.AppendBulkBytes(v)
		}
		return resp, nil
	}
}
Exemple #6
0
// SLOTSMGRTTAGSLOT host port timeout slot
func SlotsMgrtTagSlotCmd(s Session, args [][]byte) (redis.Resp, error) {
	if n, err := s.Store().SlotsMgrtTagSlot(s.DB(), args); err != nil {
		return toRespError(err)
	} else {
		resp := redis.NewArray()
		resp.AppendInt(n)
		if n != 0 {
			resp.AppendInt(1)
		} else {
			resp.AppendInt(0)
		}
		return resp, nil
	}
}
Exemple #7
0
func RegisterArrayReply(name string, f CommandArrayFunc, flag CommandFlag) {
	v := func(s Session, args [][]byte) (redis.Resp, error) {
		r, err := f(s, args)
		if err != nil {
			return toRespError(err)
		}
		ay := redis.NewArray()
		for _, b := range r {
			ay.AppendBulkBytes(b)
		}
		return ay, nil
	}

	register(name, v, flag)
}