Beispiel #1
0
func commandStats(q queue.Queue, tokens []string, r *bufio.Reader, w *bufio.Writer) error {

	fields := len(tokens)
	if fields != 1 && fields != 2 {
		fmt.Fprint(w, CLIENT_ERROR_BADCMD_FORMAT)
		return errors.NotValidf("mc tokens %v ", tokens)
	}

	if fields == 1 {
		// TODO: implement stats command
		fmt.Fprint(w, END)
	} else if fields == 2 && strings.EqualFold(tokens[1], "queue") {
		accumulationInfos, err := q.AccumulationStatus()
		if err != nil {
			fmt.Fprintf(w, "%s %s\r\n", ENGINE_ERROR_PREFIX, err)
			return nil
		}
		for _, accumulationInfo := range accumulationInfos {
			fmt.Fprintf(w, "%s %s.%s %d/%d \r\n", "STAT",
				accumulationInfo.Group,
				accumulationInfo.Queue,
				accumulationInfo.Total,
				accumulationInfo.Consumed)
		}
		fmt.Fprint(w, END)
	} else {
		fmt.Fprint(w, ERROR)
	}
	return nil
}
Beispiel #2
0
func commandGet(q queue.Queue, tokens []string, r *bufio.Reader, w *bufio.Writer) error {

	fields := len(tokens)
	if fields < 2 {
		fmt.Fprint(w, ERROR)
		return errors.NotValidf("mc tokens %v ", tokens)
	}
	cmd := tokens[0]
	keyValues := make([]pair, 0)
	for _, key := range tokens[1:] {
		k := strings.Split(key, ".")
		queue := k[0]
		group := defaultGroup
		if len(k) > 1 {
			group = k[0]
			queue = k[1]
		}

		id, data, flag, err := q.RecvMessage(queue, group)
		if err != nil {
			if errors.Cause(err) == kafka.ErrTimeout {
				w.WriteString(END)
			} else {
				fmt.Fprintf(w, "%s %s\r\n", ENGINE_ERROR_PREFIX, err)
			}
			return nil
		}
		// eset data : idlen id data
		if strings.EqualFold(cmd, EGET_NAME) {
			idLen := (byte)(len(id))
			esetData := make([]byte, 0)
			esetData = append(esetData, idLen)
			esetData = append(esetData, ([]byte)(id)...)
			data = append(esetData, data...)
		}

		keyValues = append(keyValues, pair{
			key:   key,
			queue: queue,
			group: group,
			id:    id,
			value: data,
			flag:  flag,
		})
	}

	for _, kv := range keyValues {
		fmt.Fprintf(w, "%s %s %d %d\r\n", VALUE, kv.key, kv.flag, len(kv.value))
		w.Write(kv.value)
		w.WriteString("\r\n")
		if strings.EqualFold(cmd, GET_NAME) {
			q.AckMessage(kv.queue, kv.group, kv.id)
		}
	}
	w.WriteString(END)
	return nil
}
Beispiel #3
0
func commandACK(q queue.Queue, tokens []string, r *bufio.Reader, w *bufio.Writer) error {

	var noreply string

	fields := len(tokens)
	if fields != 5 && fields != 6 {
		fmt.Fprint(w, CLIENT_ERROR_BADCMD_FORMAT)
		return errors.NotValidf("mc tokens %v ", tokens)
	}

	key := tokens[1]
	if fields == 6 {
		noreply = tokens[5]
	}

	// for ack command, flag is unused
	_, err := strconv.ParseUint(tokens[2], 10, 32)
	if err != nil {
		fmt.Fprint(w, CLIENT_ERROR_BADCMD_FORMAT)
		return errors.Trace(err)
	}

	length, err := strconv.ParseUint(tokens[4], 10, 32)
	if err != nil {
		fmt.Fprint(w, CLIENT_ERROR_BADCMD_FORMAT)
		return errors.Trace(err)
	}

	id := make([]byte, length)
	_, err = io.ReadAtLeast(r, id, int(length))
	if err != nil {
		fmt.Fprint(w, CLIENT_ERROR_BAD_DATACHUNK)
		return errors.Trace(err)
	}
	r.ReadString('\n')

	keys := strings.Split(key, ".")
	group := defaultGroup
	queue := keys[0]
	if len(keys) > 1 {
		group = keys[0]
		queue = keys[1]
	}

	err = q.AckMessage(queue, group, string(id))
	if err != nil {
		fmt.Fprintf(w, "%s %s\r\n", ENGINE_ERROR_PREFIX, err)
		return nil
	}

	if NOREPLY == noreply {
		return nil
	}

	fmt.Fprint(w, STORED)
	return nil
}
Beispiel #4
0
func commandSet(q queue.Queue, tokens []string, r *bufio.Reader, w *bufio.Writer) error {

	var noreply string

	fields := len(tokens)
	if fields != 5 && fields != 6 {
		fmt.Fprint(w, CLIENT_ERROR_BADCMD_FORMAT)
		return errors.NotValidf("mc tokens %v ", tokens)
	}
	cmd := tokens[0]
	key := tokens[1]
	if fields == 6 {
		noreply = tokens[5]
	}

	flag, err := strconv.ParseUint(tokens[2], 10, 32)
	if err != nil {
		fmt.Fprint(w, CLIENT_ERROR_BADCMD_FORMAT)
		return errors.Trace(err)
	}

	length, err := strconv.ParseUint(tokens[4], 10, 32)
	if err != nil {
		fmt.Fprint(w, CLIENT_ERROR_BADCMD_FORMAT)
		return errors.Trace(err)
	}

	data := make([]byte, length)
	_, err = io.ReadAtLeast(r, data, int(length))
	if err != nil {
		fmt.Fprint(w, CLIENT_ERROR_BAD_DATACHUNK)
		return errors.Trace(err)
	}
	r.ReadString('\n')

	keys := strings.Split(key, ".")
	group := defaultGroup
	queue := keys[0]
	if len(keys) > 1 {
		group = keys[0]
		queue = keys[1]
	}

	id, err := q.SendMessage(queue, group, data, flag)
	if err != nil {
		fmt.Fprintf(w, "%s %s\r\n", ENGINE_ERROR_PREFIX, err)
		return nil
	}

	if NOREPLY == noreply {
		return nil
	}

	// if eset command, return message id
	if strings.EqualFold(cmd, ESET_NAME) {
		fmt.Fprint(w, id+" "+STORED)
		return nil
	}
	fmt.Fprint(w, STORED)
	return nil
}