Search in sources :

Example 1 with RedisRuntimeException

use of com.ctrip.xpipe.redis.core.exception.RedisRuntimeException in project x-pipe by ctripcorp.

the class BulkStringParser method read.

@Override
public RedisClientProtocol<InOutPayload> read(ByteBuf byteBuf) {
    switch(bulkStringState) {
        case READING_EOF_MARK:
            eofJudger = readEOfMark(byteBuf);
            if (eofJudger == null) {
                return null;
            }
            logger.debug("[read]{}", eofJudger);
            if (bulkStringParserListener != null) {
                bulkStringParserListener.onEofType(eofJudger.getEofType());
            }
            bulkStringState = BULK_STRING_STATE.READING_CONTENT;
            payload.startInput();
        case READING_CONTENT:
            int readerIndex = byteBuf.readerIndex();
            JudgeResult result = eofJudger.end(byteBuf.slice());
            int length = 0;
            try {
                length = payload.in(byteBuf.slice(readerIndex, result.getReadLen()));
                if (length != result.getReadLen()) {
                    throw new IllegalStateException(String.format("expected readLen:%d, but real:%d", result.getReadLen(), length));
                }
            } catch (IOException e) {
                logger.error("[read][exception]" + payload, e);
                throw new RedisRuntimeException("[write to payload exception]" + payload, e);
            }
            byteBuf.readerIndex(readerIndex + length);
            if (result.isEnd()) {
                int truncate = eofJudger.truncate();
                try {
                    if (truncate > 0) {
                        payload.endInputTruncate(truncate);
                    } else {
                        payload.endInput();
                    }
                } catch (IOException e) {
                    throw new RedisRuntimeException("[write to payload truncate exception]" + payload, e);
                }
                bulkStringState = BULK_STRING_STATE.READING_CR;
            } else {
                break;
            }
        case READING_CR:
            if (byteBuf.readableBytes() == 0) {
                return new BulkStringParser(payload);
            }
            byte data1 = byteBuf.getByte(byteBuf.readerIndex());
            if (data1 == '\r') {
                byteBuf.readByte();
                bulkStringState = BULK_STRING_STATE.READING_LF;
            } else {
                return new BulkStringParser(payload);
            }
        case READING_LF:
            if (byteBuf.readableBytes() == 0) {
                return null;
            }
            data1 = byteBuf.getByte(byteBuf.readerIndex());
            if (data1 == '\n') {
                byteBuf.readByte();
                bulkStringState = BULK_STRING_STATE.END;
            }
            return new BulkStringParser(payload);
        case END:
            return new BulkStringParser(payload);
        default:
            break;
    }
    return null;
}
Also used : IOException(java.io.IOException) JudgeResult(com.ctrip.xpipe.redis.core.protocal.protocal.BulkStringEofJudger.JudgeResult) RedisRuntimeException(com.ctrip.xpipe.redis.core.exception.RedisRuntimeException)

Example 2 with RedisRuntimeException

use of com.ctrip.xpipe.redis.core.exception.RedisRuntimeException in project x-pipe by ctripcorp.

the class DefaultRedisClient method handleArray.

private String[] handleArray(Object[] result) {
    String[] strArray = new String[result.length];
    int index = 0;
    for (Object param : result) {
        if (param instanceof String) {
            strArray[index] = (String) param;
        } else if (param instanceof ByteArrayOutputStreamPayload) {
            byte[] bytes = ((ByteArrayOutputStreamPayload) param).getBytes();
            strArray[index] = new String(bytes, Codec.defaultCharset);
        } else {
            throw new RedisRuntimeException("request unkonwn, can not be transformed to string!");
        }
        index++;
    }
    return strArray;
}
Also used : ByteArrayOutputStreamPayload(com.ctrip.xpipe.payload.ByteArrayOutputStreamPayload) RedisRuntimeException(com.ctrip.xpipe.redis.core.exception.RedisRuntimeException)

Example 3 with RedisRuntimeException

use of com.ctrip.xpipe.redis.core.exception.RedisRuntimeException in project x-pipe by ctripcorp.

the class AbstractPsync method handleRedisResponse.

protected void handleRedisResponse(Channel channel, String psync) throws IOException {
    if (logger.isInfoEnabled()) {
        logger.info("[handleRedisResponse]{}, {}, {}", ChannelUtil.getDesc(channel), this, psync);
    }
    String[] split = splitSpace(psync);
    if (split.length == 0) {
        throw new RedisRuntimeException("wrong reply:" + psync);
    }
    if (split[0].equalsIgnoreCase(FULL_SYNC)) {
        if (split.length != 3) {
            throw new RedisRuntimeException("unknown reply:" + psync);
        }
        replId = split[1];
        masterRdbOffset = Long.parseLong(split[2]);
        logger.debug("[readRedisResponse]{}, {}, {}, {}", ChannelUtil.getDesc(channel), this, replId, masterRdbOffset);
        psyncState = PSYNC_STATE.READING_RDB;
        doOnFullSync();
    } else if (split[0].equalsIgnoreCase(PARTIAL_SYNC)) {
        psyncState = PSYNC_STATE.READING_COMMANDS;
        String newReplId = null;
        if (split.length >= 2 && split[1].length() == RedisProtocol.RUN_ID_LENGTH) {
            newReplId = split[1];
        }
        doOnContinue(newReplId);
    } else {
        throw new RedisRuntimeException("unknown reply:" + psync);
    }
}
Also used : RedisRuntimeException(com.ctrip.xpipe.redis.core.exception.RedisRuntimeException)

Aggregations

RedisRuntimeException (com.ctrip.xpipe.redis.core.exception.RedisRuntimeException)3 ByteArrayOutputStreamPayload (com.ctrip.xpipe.payload.ByteArrayOutputStreamPayload)1 JudgeResult (com.ctrip.xpipe.redis.core.protocal.protocal.BulkStringEofJudger.JudgeResult)1 IOException (java.io.IOException)1