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

The following examples show how to use org.apache.rocketmq.common.sysflag.MessageSysFlag#BORNHOST_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: CommitLog.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
/**
 * According to receive certain message or offset storage time if an error occurs, it returns -1
 */
public long pickupStoreTimestamp(final long offset, final int size) {
    if (offset >= this.getMinOffset()) {
        SelectMappedBufferResult result = this.getMessage(offset, size);
        if (null != result) {
            try {
                int sysFlag = result.getByteBuffer().getInt(MessageDecoder.SYSFLAG_POSITION);
                int bornhostLength = (sysFlag & MessageSysFlag.BORNHOST_V6_FLAG) == 0 ? 8 : 20;
                int msgStoreTimePos = 4 + 4 + 4 + 4 + 4 + 8 + 8 + 4 + 8 + bornhostLength;
                return result.getByteBuffer().getLong(msgStoreTimePos);
            } finally {
                result.release();
            }
        }
    }

    return -1;
}
 
Example 3
Source File: PullMessageProcessor.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private byte[] readGetMessageResult(final GetMessageResult getMessageResult, final String group, final String topic,
        final int queueId) {
        final ByteBuffer byteBuffer = ByteBuffer.allocate(getMessageResult.getBufferTotalSize());

        long storeTimestamp = 0;
        try {
            List<ByteBuffer> messageBufferList = getMessageResult.getMessageBufferList();
            for (ByteBuffer bb : messageBufferList) {

                byteBuffer.put(bb);
                int sysFlag = bb.getInt(MessageDecoder.SYSFLAG_POSITION);
//                bornhost has the IPv4 ip if the MessageSysFlag.BORNHOST_V6_FLAG bit of sysFlag is 0
//                IPv4 host = ip(4 byte) + port(4 byte); IPv6 host = ip(16 byte) + port(4 byte)
                int bornhostLength = (sysFlag & MessageSysFlag.BORNHOST_V6_FLAG) == 0 ? 8 : 20;
                int msgStoreTimePos = 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
                storeTimestamp = bb.getLong(msgStoreTimePos);
            }
        } finally {
            getMessageResult.release();
        }

        this.brokerController.getBrokerStatsManager().recordDiskFallBehindTime(group, topic, queueId, this.brokerController.getMessageStore().now() - storeTimestamp);
        return byteBuffer.array();
    }
 
Example 4
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 5
Source File: CommitLog.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private boolean isMappedFileMatchedRecover(final MappedFile mappedFile) {
    ByteBuffer byteBuffer = mappedFile.sliceByteBuffer();

    int magicCode = byteBuffer.getInt(MessageDecoder.MESSAGE_MAGIC_CODE_POSTION);
    if (magicCode != MESSAGE_MAGIC_CODE) {
        return false;
    }

    int sysFlag = byteBuffer.getInt(MessageDecoder.SYSFLAG_POSITION);
    int bornhostLength = (sysFlag & MessageSysFlag.BORNHOST_V6_FLAG) == 0 ? 8 : 20;
    int msgStoreTimePos = 4 + 4 + 4 + 4 + 4 + 8 + 8 + 4 + 8 + bornhostLength;
    long storeTimestamp = byteBuffer.getLong(msgStoreTimePos);
    if (0 == storeTimestamp) {
        return false;
    }

    if (this.defaultMessageStore.getMessageStoreConfig().isMessageIndexEnable()
        && this.defaultMessageStore.getMessageStoreConfig().isMessageIndexSafe()) {
        if (storeTimestamp <= this.defaultMessageStore.getStoreCheckpoint().getMinTimestampIndex()) {
            log.info("find check timestamp, {} {}",
                storeTimestamp,
                UtilAll.timeMillisToHumanString(storeTimestamp));
            return true;
        }
    } else {
        if (storeTimestamp <= this.defaultMessageStore.getStoreCheckpoint().getMinTimestamp()) {
            log.info("find check timestamp, {} {}",
                storeTimestamp,
                UtilAll.timeMillisToHumanString(storeTimestamp));
            return true;
        }
    }

    return false;
}
 
Example 6
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 7
Source File: MessageExt.java    From rocketmq with Apache License 2.0 votes vote down vote up
public void setBornHostV6Flag() { this.sysFlag = this.sysFlag | MessageSysFlag.BORNHOST_V6_FLAG; }