Java Code Examples for org.apache.rocketmq.common.sysflag.MessageSysFlag#STOREHOSTADDRESS_V6_FLAG

The following examples show how to use org.apache.rocketmq.common.sysflag.MessageSysFlag#STOREHOSTADDRESS_V6_FLAG . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommitLog.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
protected static int calMsgLength(int sysFlag, int bodyLength, int topicLength, int propertiesLength) {
    int bornhostLength = (sysFlag & MessageSysFlag.BORNHOST_V6_FLAG) == 0 ? 8 : 20;
    int storehostAddressLength = (sysFlag & MessageSysFlag.STOREHOSTADDRESS_V6_FLAG) == 0 ? 8 : 20;
    final int msgLen = 4 //TOTALSIZE
        + 4 //MAGICCODE
        + 4 //BODYCRC
        + 4 //QUEUEID
        + 4 //FLAG
        + 8 //QUEUEOFFSET
        + 8 //PHYSICALOFFSET
        + 4 //SYSFLAG
        + 8 //BORNTIMESTAMP
        + bornhostLength //BORNHOST
        + 8 //STORETIMESTAMP
        + storehostAddressLength //STOREHOSTADDRESS
        + 4 //RECONSUMETIMES
        + 8 //Prepared Transaction Offset
        + 4 + (bodyLength > 0 ? bodyLength : 0) //BODY
        + 1 + topicLength //TOPIC
        + 2 + (propertiesLength > 0 ? propertiesLength : 0) //propertiesLength
        + 0;
    return msgLen;
}
 
Example 2
Source File: MessageDecoder.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
/**
 * Just decode properties from msg buffer.
 *
 * @param byteBuffer msg commit log buffer.
 */
public static Map<String, String> decodeProperties(java.nio.ByteBuffer byteBuffer) {
    int sysFlag = byteBuffer.getInt(SYSFLAG_POSITION);
    int bornhostLength = (sysFlag & MessageSysFlag.BORNHOST_V6_FLAG) == 0 ? 8 : 20;
    int storehostAddressLength = (sysFlag & MessageSysFlag.STOREHOSTADDRESS_V6_FLAG) == 0 ? 8 : 20;
    int bodySizePosition = 4 // 1 TOTALSIZE
        + 4 // 2 MAGICCODE
        + 4 // 3 BODYCRC
        + 4 // 4 QUEUEID
        + 4 // 5 FLAG
        + 8 // 6 QUEUEOFFSET
        + 8 // 7 PHYSICALOFFSET
        + 4 // 8 SYSFLAG
        + 8 // 9 BORNTIMESTAMP
        + bornhostLength // 10 BORNHOST
        + 8 // 11 STORETIMESTAMP
        + storehostAddressLength // 12 STOREHOSTADDRESS
        + 4 // 13 RECONSUMETIMES
        + 8; // 14 Prepared Transaction Offset
    int topicLengthPosition = bodySizePosition + 4 + byteBuffer.getInt(bodySizePosition);

    byte topicLength = byteBuffer.get(topicLengthPosition);

    short propertiesLength = byteBuffer.getShort(topicLengthPosition + 1 + topicLength);

    byteBuffer.position(topicLengthPosition + 1 + topicLength + 2);

    if (propertiesLength > 0) {
        byte[] properties = new byte[propertiesLength];
        byteBuffer.get(properties);
        String propertiesString = new String(properties, CHARSET_UTF8);
        Map<String, String> map = string2messageProperties(propertiesString);
        return map;
    }
    return null;
}
 
Example 3
Source File: CommitLog.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
public AppendMessageResult doAppend(final long fileFromOffset, final ByteBuffer byteBuffer, final int maxBlank,
    final MessageExtBatch messageExtBatch) {
    byteBuffer.mark();
    //physical offset
    long wroteOffset = fileFromOffset + byteBuffer.position();
    // Record ConsumeQueue information
    keyBuilder.setLength(0);
    keyBuilder.append(messageExtBatch.getTopic());
    keyBuilder.append('-');
    keyBuilder.append(messageExtBatch.getQueueId());
    String key = keyBuilder.toString();
    Long queueOffset = CommitLog.this.topicQueueTable.get(key);
    if (null == queueOffset) {
        queueOffset = 0L;
        CommitLog.this.topicQueueTable.put(key, queueOffset);
    }
    long beginQueueOffset = queueOffset;
    int totalMsgLen = 0;
    int msgNum = 0;
    msgIdBuilder.setLength(0);
    final long beginTimeMills = CommitLog.this.defaultMessageStore.now();
    ByteBuffer messagesByteBuff = messageExtBatch.getEncodedBuff();

    int sysFlag = messageExtBatch.getSysFlag();
    int storeHostLength = (sysFlag & MessageSysFlag.STOREHOSTADDRESS_V6_FLAG) == 0 ? 4 + 4 : 16 + 4;
    ByteBuffer storeHostHolder = ByteBuffer.allocate(storeHostLength);

    this.resetByteBuffer(storeHostHolder, storeHostLength);
    ByteBuffer storeHostBytes = messageExtBatch.getStoreHostBytes(storeHostHolder);
    messagesByteBuff.mark();
    while (messagesByteBuff.hasRemaining()) {
        // 1 TOTALSIZE
        final int msgPos = messagesByteBuff.position();
        final int msgLen = messagesByteBuff.getInt();
        final int bodyLen = msgLen - 40; //only for log, just estimate it
        // Exceeds the maximum message
        if (msgLen > this.maxMessageSize) {
            CommitLog.log.warn("message size exceeded, msg total size: " + msgLen + ", msg body size: " + bodyLen
                + ", maxMessageSize: " + this.maxMessageSize);
            return new AppendMessageResult(AppendMessageStatus.MESSAGE_SIZE_EXCEEDED);
        }
        totalMsgLen += msgLen;
        // Determines whether there is sufficient free space
        if ((totalMsgLen + END_FILE_MIN_BLANK_LENGTH) > maxBlank) {
            this.resetByteBuffer(this.msgStoreItemMemory, 8);
            // 1 TOTALSIZE
            this.msgStoreItemMemory.putInt(maxBlank);
            // 2 MAGICCODE
            this.msgStoreItemMemory.putInt(CommitLog.BLANK_MAGIC_CODE);
            // 3 The remaining space may be any value
            //ignore previous read
            messagesByteBuff.reset();
            // Here the length of the specially set maxBlank
            byteBuffer.reset(); //ignore the previous appended messages
            byteBuffer.put(this.msgStoreItemMemory.array(), 0, 8);
            return new AppendMessageResult(AppendMessageStatus.END_OF_FILE, wroteOffset, maxBlank, msgIdBuilder.toString(), messageExtBatch.getStoreTimestamp(),
                beginQueueOffset, CommitLog.this.defaultMessageStore.now() - beginTimeMills);
        }
        //move to add queue offset and commitlog offset
        messagesByteBuff.position(msgPos + 20);
        messagesByteBuff.putLong(queueOffset);
        messagesByteBuff.putLong(wroteOffset + totalMsgLen - msgLen);

        storeHostBytes.rewind();
        String msgId;
        if ((sysFlag & MessageSysFlag.STOREHOSTADDRESS_V6_FLAG) == 0) {
            msgId = MessageDecoder.createMessageId(this.msgIdMemory, storeHostBytes, wroteOffset + totalMsgLen - msgLen);
        } else {
            msgId = MessageDecoder.createMessageId(this.msgIdV6Memory, storeHostBytes, wroteOffset + totalMsgLen - msgLen);
        }

        if (msgIdBuilder.length() > 0) {
            msgIdBuilder.append(',').append(msgId);
        } else {
            msgIdBuilder.append(msgId);
        }
        queueOffset++;
        msgNum++;
        messagesByteBuff.position(msgPos + msgLen);
    }

    messagesByteBuff.position(0);
    messagesByteBuff.limit(totalMsgLen);
    byteBuffer.put(messagesByteBuff);
    messageExtBatch.setEncodedBuff(null);
    AppendMessageResult result = new AppendMessageResult(AppendMessageStatus.PUT_OK, wroteOffset, totalMsgLen, msgIdBuilder.toString(),
        messageExtBatch.getStoreTimestamp(), beginQueueOffset, CommitLog.this.defaultMessageStore.now() - beginTimeMills);
    result.setMsgNum(msgNum);
    CommitLog.this.topicQueueTable.put(key, queueOffset);

    return result;
}
 
Example 4
Source File: CommitLog.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
public ByteBuffer encode(final MessageExtBatch messageExtBatch) {
    msgBatchMemory.clear(); //not thread-safe
    int totalMsgLen = 0;
    ByteBuffer messagesByteBuff = messageExtBatch.wrap();

    int sysFlag = messageExtBatch.getSysFlag();
    int bornHostLength = (sysFlag & MessageSysFlag.BORNHOST_V6_FLAG) == 0 ? 4 + 4 : 16 + 4;
    int storeHostLength = (sysFlag & MessageSysFlag.STOREHOSTADDRESS_V6_FLAG) == 0 ? 4 + 4 : 16 + 4;
    ByteBuffer bornHostHolder = ByteBuffer.allocate(bornHostLength);
    ByteBuffer storeHostHolder = ByteBuffer.allocate(storeHostLength);

    while (messagesByteBuff.hasRemaining()) {
        // 1 TOTALSIZE
        messagesByteBuff.getInt();
        // 2 MAGICCODE
        messagesByteBuff.getInt();
        // 3 BODYCRC
        messagesByteBuff.getInt();
        // 4 FLAG
        int flag = messagesByteBuff.getInt();
        // 5 BODY
        int bodyLen = messagesByteBuff.getInt();
        int bodyPos = messagesByteBuff.position();
        int bodyCrc = UtilAll.crc32(messagesByteBuff.array(), bodyPos, bodyLen);
        messagesByteBuff.position(bodyPos + bodyLen);
        // 6 properties
        short propertiesLen = messagesByteBuff.getShort();
        int propertiesPos = messagesByteBuff.position();
        messagesByteBuff.position(propertiesPos + propertiesLen);

        final byte[] topicData = messageExtBatch.getTopic().getBytes(MessageDecoder.CHARSET_UTF8);

        final int topicLength = topicData.length;

        final int msgLen = calMsgLength(messageExtBatch.getSysFlag(), bodyLen, topicLength, propertiesLen);

        // Exceeds the maximum message
        if (msgLen > this.maxMessageSize) {
            CommitLog.log.warn("message size exceeded, msg total size: " + msgLen + ", msg body size: " + bodyLen
                + ", maxMessageSize: " + this.maxMessageSize);
            throw new RuntimeException("message size exceeded");
        }

        totalMsgLen += msgLen;
        // Determines whether there is sufficient free space
        if (totalMsgLen > maxMessageSize) {
            throw new RuntimeException("message size exceeded");
        }

        // 1 TOTALSIZE
        this.msgBatchMemory.putInt(msgLen);
        // 2 MAGICCODE
        this.msgBatchMemory.putInt(CommitLog.MESSAGE_MAGIC_CODE);
        // 3 BODYCRC
        this.msgBatchMemory.putInt(bodyCrc);
        // 4 QUEUEID
        this.msgBatchMemory.putInt(messageExtBatch.getQueueId());
        // 5 FLAG
        this.msgBatchMemory.putInt(flag);
        // 6 QUEUEOFFSET
        this.msgBatchMemory.putLong(0);
        // 7 PHYSICALOFFSET
        this.msgBatchMemory.putLong(0);
        // 8 SYSFLAG
        this.msgBatchMemory.putInt(messageExtBatch.getSysFlag());
        // 9 BORNTIMESTAMP
        this.msgBatchMemory.putLong(messageExtBatch.getBornTimestamp());
        // 10 BORNHOST
        this.resetByteBuffer(bornHostHolder, bornHostLength);
        this.msgBatchMemory.put(messageExtBatch.getBornHostBytes(bornHostHolder));
        // 11 STORETIMESTAMP
        this.msgBatchMemory.putLong(messageExtBatch.getStoreTimestamp());
        // 12 STOREHOSTADDRESS
        this.resetByteBuffer(storeHostHolder, storeHostLength);
        this.msgBatchMemory.put(messageExtBatch.getStoreHostBytes(storeHostHolder));
        // 13 RECONSUMETIMES
        this.msgBatchMemory.putInt(messageExtBatch.getReconsumeTimes());
        // 14 Prepared Transaction Offset, batch does not support transaction
        this.msgBatchMemory.putLong(0);
        // 15 BODY
        this.msgBatchMemory.putInt(bodyLen);
        if (bodyLen > 0)
            this.msgBatchMemory.put(messagesByteBuff.array(), bodyPos, bodyLen);
        // 16 TOPIC
        this.msgBatchMemory.put((byte) topicLength);
        this.msgBatchMemory.put(topicData);
        // 17 PROPERTIES
        this.msgBatchMemory.putShort(propertiesLen);
        if (propertiesLen > 0)
            this.msgBatchMemory.put(messagesByteBuff.array(), propertiesPos, propertiesLen);
    }
    msgBatchMemory.flip();
    return msgBatchMemory;
}
 
Example 5
Source File: MessageExt.java    From rocketmq with Apache License 2.0 votes vote down vote up
public void setStoreHostAddressV6Flag() { this.sysFlag = this.sysFlag | MessageSysFlag.STOREHOSTADDRESS_V6_FLAG; }