Java Code Examples for org.apache.rocketmq.common.message.MessageDecoder#createMessageId()

The following examples show how to use org.apache.rocketmq.common.message.MessageDecoder#createMessageId() . 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 DDMQ 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) {
    if (messageExtBatch.isMultiTopic()) {
        return doAppendMultiTopic(fileFromOffset, byteBuffer, maxBlank, 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();
    this.resetByteBuffer(hostHolder, 8);
    ByteBuffer storeHostBytes = messageExtBatch.getStoreHostBytes(hostHolder);
    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 = MessageDecoder.createMessageId(this.msgIdMemory, 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 2
Source File: DefaultMessageStore.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
public Map<String, Long> getMessageIds(final String topic, final int queueId, long minOffset, long maxOffset,
    SocketAddress storeHost) {
    Map<String, Long> messageIds = new HashMap<String, Long>();
    if (this.shutdown) {
        return messageIds;
    }

    ConsumeQueue consumeQueue = findConsumeQueue(topic, queueId);
    if (consumeQueue != null) {
        minOffset = Math.max(minOffset, consumeQueue.getMinOffsetInQueue());
        maxOffset = Math.min(maxOffset, consumeQueue.getMaxOffsetInQueue());

        if (maxOffset == 0) {
            return messageIds;
        }

        long nextOffset = minOffset;
        while (nextOffset < maxOffset) {
            SelectMappedBufferResult bufferConsumeQueue = consumeQueue.getIndexBuffer(nextOffset);
            if (bufferConsumeQueue != null) {
                try {
                    int i = 0;
                    for (; i < bufferConsumeQueue.getSize(); i += ConsumeQueue.CQ_STORE_UNIT_SIZE) {
                        long offsetPy = bufferConsumeQueue.getByteBuffer().getLong();
                        final ByteBuffer msgIdMemory = ByteBuffer.allocate(MessageDecoder.MSG_ID_LENGTH);
                        String msgId =
                            MessageDecoder.createMessageId(msgIdMemory, MessageExt.socketAddress2ByteBuffer(storeHost), offsetPy);
                        messageIds.put(msgId, nextOffset++);
                        if (nextOffset > maxOffset) {
                            return messageIds;
                        }
                    }
                } finally {

                    bufferConsumeQueue.release();
                }
            } else {
                return messageIds;
            }
        }
    }
    return messageIds;
}
 
Example 3
Source File: CommitLog.java    From rocketmq-4.3.0 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 写的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();
//            缓存topic、队列和offset信息
            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();
            this.resetByteBuffer(hostHolder, 8);
            ByteBuffer storeHostBytes = messageExtBatch.getStoreHostBytes(hostHolder);
            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 = MessageDecoder.createMessageId(this.msgIdMemory, 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: DefaultMessageStore.java    From rocketmq-4.3.0 with Apache License 2.0 4 votes vote down vote up
public Map<String, Long> getMessageIds(final String topic, final int queueId, long minOffset, long maxOffset,
    SocketAddress storeHost) {
    Map<String, Long> messageIds = new HashMap<String, Long>();
    if (this.shutdown) {
        return messageIds;
    }

    ConsumeQueue consumeQueue = findConsumeQueue(topic, queueId);
    if (consumeQueue != null) {
        minOffset = Math.max(minOffset, consumeQueue.getMinOffsetInQueue());
        maxOffset = Math.min(maxOffset, consumeQueue.getMaxOffsetInQueue());

        if (maxOffset == 0) {
            return messageIds;
        }

        long nextOffset = minOffset;
        while (nextOffset < maxOffset) {
            SelectMappedBufferResult bufferConsumeQueue = consumeQueue.getIndexBuffer(nextOffset);
            if (bufferConsumeQueue != null) {
                try {
                    int i = 0;
                    for (; i < bufferConsumeQueue.getSize(); i += ConsumeQueue.CQ_STORE_UNIT_SIZE) {
                        long offsetPy = bufferConsumeQueue.getByteBuffer().getLong();
                        final ByteBuffer msgIdMemory = ByteBuffer.allocate(MessageDecoder.MSG_ID_LENGTH);
                        String msgId =
                            MessageDecoder.createMessageId(msgIdMemory, MessageExt.socketAddress2ByteBuffer(storeHost), offsetPy);
                        messageIds.put(msgId, nextOffset++);
                        if (nextOffset > maxOffset) {
                            return messageIds;
                        }
                    }
                } finally {

                    bufferConsumeQueue.release();
                }
            } else {
                return messageIds;
            }
        }
    }
    return messageIds;
}
 
Example 5
Source File: CommitLog.java    From rocketmq-read with Apache License 2.0 4 votes vote down vote up
@Override
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();
    this.resetByteBuffer(hostHolder, 8);
    ByteBuffer storeHostBytes = messageExtBatch.getStoreHostBytes(hostHolder);
    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 = MessageDecoder.createMessageId(this.msgIdMemory, 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 6
Source File: DefaultMessageStore.java    From rocketmq-read with Apache License 2.0 4 votes vote down vote up
public Map<String, Long> getMessageIds(final String topic, final int queueId, long minOffset, long maxOffset,
    SocketAddress storeHost) {
    Map<String, Long> messageIds = new HashMap<String, Long>();
    if (this.shutdown) {
        return messageIds;
    }

    ConsumeQueue consumeQueue = findConsumeQueue(topic, queueId);
    if (consumeQueue != null) {
        minOffset = Math.max(minOffset, consumeQueue.getMinOffsetInQueue());
        maxOffset = Math.min(maxOffset, consumeQueue.getMaxOffsetInQueue());

        if (maxOffset == 0) {
            return messageIds;
        }

        long nextOffset = minOffset;
        while (nextOffset < maxOffset) {
            SelectMappedBufferResult bufferConsumeQueue = consumeQueue.getIndexBuffer(nextOffset);
            if (bufferConsumeQueue != null) {
                try {
                    int i = 0;
                    for (; i < bufferConsumeQueue.getSize(); i += ConsumeQueue.CQ_STORE_UNIT_SIZE) {
                        long offsetPy = bufferConsumeQueue.getByteBuffer().getLong();
                        final ByteBuffer msgIdMemory = ByteBuffer.allocate(MessageDecoder.MSG_ID_LENGTH);
                        String msgId =
                            MessageDecoder.createMessageId(msgIdMemory, MessageExt.socketAddress2ByteBuffer(storeHost), offsetPy);
                        messageIds.put(msgId, nextOffset++);
                        if (nextOffset > maxOffset) {
                            return messageIds;
                        }
                    }
                } finally {

                    bufferConsumeQueue.release();
                }
            } else {
                return messageIds;
            }
        }
    }
    return messageIds;
}
 
Example 7
Source File: DefaultMessageStore.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
public Map<String, Long> getMessageIds(final String topic, final int queueId, long minOffset, long maxOffset, SocketAddress storeHost) {
    Map<String, Long> messageIds = new HashMap<String, Long>();
    if (this.shutdown) {
        return messageIds;
    }

    ConsumeQueue consumeQueue = findConsumeQueue(topic, queueId);
    if (consumeQueue != null) {
        minOffset = Math.max(minOffset, consumeQueue.getMinOffsetInQueue());
        maxOffset = Math.min(maxOffset, consumeQueue.getMaxOffsetInQueue());

        if (maxOffset == 0) {
            return messageIds;
        }

        long nextOffset = minOffset;
        while (nextOffset < maxOffset) {
            SelectMappedBufferResult bufferConsumeQueue = consumeQueue.getIndexBuffer(nextOffset);
            if (bufferConsumeQueue != null) {
                try {
                    int i = 0;
                    for (; i < bufferConsumeQueue.getSize(); i += ConsumeQueue.CQ_STORE_UNIT_SIZE) {
                        long offsetPy = bufferConsumeQueue.getByteBuffer().getLong();
                        final ByteBuffer msgIdMemory = ByteBuffer.allocate(MessageDecoder.MSG_ID_LENGTH);
                        String msgId =
                            MessageDecoder.createMessageId(msgIdMemory, MessageExt.socketAddress2ByteBuffer(storeHost), offsetPy);
                        messageIds.put(msgId, nextOffset++);
                        if (nextOffset > maxOffset) {
                            return messageIds;
                        }
                    }
                } finally {

                    bufferConsumeQueue.release();
                }
            } else {
                return messageIds;
            }
        }
    }
    return messageIds;
}
 
Example 8
Source File: CommitLog.java    From DDMQ 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) {
    if (messageExtBatch.isMultiTopic()) {
        return doAppendMultiTopic(fileFromOffset, byteBuffer, maxBlank, 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();
    this.resetByteBuffer(hostHolder, 8);
    ByteBuffer storeHostBytes = messageExtBatch.getStoreHostBytes(hostHolder);
    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 = MessageDecoder.createMessageId(this.msgIdMemory, 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 9
Source File: DefaultMessageStore.java    From DDMQ with Apache License 2.0 4 votes vote down vote up
public Map<String, Long> getMessageIds(final String topic, final int queueId, long minOffset, long maxOffset,
    SocketAddress storeHost) {
    Map<String, Long> messageIds = new HashMap<String, Long>();
    if (this.shutdown) {
        return messageIds;
    }

    ConsumeQueue consumeQueue = findConsumeQueue(topic, queueId);
    if (consumeQueue != null) {
        minOffset = Math.max(minOffset, consumeQueue.getMinOffsetInQueue());
        maxOffset = Math.min(maxOffset, consumeQueue.getMaxOffsetInQueue());

        if (maxOffset == 0) {
            return messageIds;
        }

        long nextOffset = minOffset;
        while (nextOffset < maxOffset) {
            SelectMappedBufferResult bufferConsumeQueue = consumeQueue.getIndexBuffer(nextOffset);
            if (bufferConsumeQueue != null) {
                try {
                    int i = 0;
                    for (; i < bufferConsumeQueue.getSize(); i += ConsumeQueue.CQ_STORE_UNIT_SIZE) {
                        long offsetPy = bufferConsumeQueue.getByteBuffer().getLong();
                        final ByteBuffer msgIdMemory = ByteBuffer.allocate(MessageDecoder.MSG_ID_LENGTH);
                        String msgId =
                            MessageDecoder.createMessageId(msgIdMemory, MessageExt.socketAddress2ByteBuffer(storeHost), offsetPy);
                        messageIds.put(msgId, nextOffset++);
                        if (nextOffset > maxOffset) {
                            return messageIds;
                        }
                    }
                } finally {

                    bufferConsumeQueue.release();
                }
            } else {
                return messageIds;
            }
        }
    }
    return messageIds;
}
 
Example 10
Source File: CommitLog.java    From rocketmq-all-4.1.0-incubating 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();//可以标记byteBuffer中的一个特定position。用于reset恢复
    //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();
    this.resetByteBuffer(hostHolder, 8);
    ByteBuffer storeHostBytes = messageExtBatch.getStoreHostBytes(hostHolder);
    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.mark()标记时的position。
            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 = MessageDecoder.createMessageId(this.msgIdMemory, 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 11
Source File: DefaultMessageStore.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 4 votes vote down vote up
public Map<String, Long> getMessageIds(final String topic, final int queueId, long minOffset, long maxOffset, SocketAddress storeHost) {
    Map<String, Long> messageIds = new HashMap<String, Long>();
    if (this.shutdown) {
        return messageIds;
    }

    ConsumeQueue consumeQueue = findConsumeQueue(topic, queueId);
    if (consumeQueue != null) {
        minOffset = Math.max(minOffset, consumeQueue.getMinOffsetInQueue());
        maxOffset = Math.min(maxOffset, consumeQueue.getMaxOffsetInQueue());

        if (maxOffset == 0) {
            return messageIds;
        }

        long nextOffset = minOffset;
        while (nextOffset < maxOffset) {
            SelectMappedBufferResult bufferConsumeQueue = consumeQueue.getIndexBuffer(nextOffset);
            if (bufferConsumeQueue != null) {
                try {
                    int i = 0;
                    for (; i < bufferConsumeQueue.getSize(); i += ConsumeQueue.CQ_STORE_UNIT_SIZE) {
                        long offsetPy = bufferConsumeQueue.getByteBuffer().getLong();
                        final ByteBuffer msgIdMemory = ByteBuffer.allocate(MessageDecoder.MSG_ID_LENGTH);
                        String msgId =
                            MessageDecoder.createMessageId(msgIdMemory, MessageExt.socketAddress2ByteBuffer(storeHost), offsetPy);
                        messageIds.put(msgId, nextOffset++);
                        if (nextOffset > maxOffset) {
                            return messageIds;
                        }
                    }
                } finally {

                    bufferConsumeQueue.release();
                }
            } else {
                return messageIds;
            }
        }
    }
    return messageIds;
}
 
Example 12
Source File: DefaultMessageStore.java    From rocketmq_trans_message with Apache License 2.0 4 votes vote down vote up
public Map<String, Long> getMessageIds(final String topic, final int queueId, long minOffset, long maxOffset, SocketAddress storeHost) {
    Map<String, Long> messageIds = new HashMap<String, Long>();
    if (this.shutdown) {
        return messageIds;
    }

    ConsumeQueue consumeQueue = findConsumeQueue(topic, queueId);
    if (consumeQueue != null) {
        minOffset = Math.max(minOffset, consumeQueue.getMinOffsetInQueue());
        maxOffset = Math.min(maxOffset, consumeQueue.getMaxOffsetInQueue());

        if (maxOffset == 0) {
            return messageIds;
        }

        long nextOffset = minOffset;
        while (nextOffset < maxOffset) {
            SelectMappedBufferResult bufferConsumeQueue = consumeQueue.getIndexBuffer(nextOffset);
            if (bufferConsumeQueue != null) {
                try {
                    int i = 0;
                    for (; i < bufferConsumeQueue.getSize(); i += ConsumeQueue.CQ_STORE_UNIT_SIZE) {
                        long offsetPy = bufferConsumeQueue.getByteBuffer().getLong();
                        final ByteBuffer msgIdMemory = ByteBuffer.allocate(MessageDecoder.MSG_ID_LENGTH);
                        String msgId =
                                MessageDecoder.createMessageId(msgIdMemory, MessageExt.socketAddress2ByteBuffer(storeHost), offsetPy);
                        messageIds.put(msgId, nextOffset++);
                        if (nextOffset > maxOffset) {
                            return messageIds;
                        }
                    }
                } finally {

                    bufferConsumeQueue.release();
                }
            } else {
                return messageIds;
            }
        }
    }
    return messageIds;
}
 
Example 13
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 14
Source File: DefaultMessageStore.java    From rocketmq with Apache License 2.0 4 votes vote down vote up
public Map<String, Long> getMessageIds(final String topic, final int queueId, long minOffset, long maxOffset,
    SocketAddress storeHost) {
    Map<String, Long> messageIds = new HashMap<String, Long>();
    if (this.shutdown) {
        return messageIds;
    }

    ConsumeQueue consumeQueue = findConsumeQueue(topic, queueId);
    if (consumeQueue != null) {
        minOffset = Math.max(minOffset, consumeQueue.getMinOffsetInQueue());
        maxOffset = Math.min(maxOffset, consumeQueue.getMaxOffsetInQueue());

        if (maxOffset == 0) {
            return messageIds;
        }

        long nextOffset = minOffset;
        while (nextOffset < maxOffset) {
            SelectMappedBufferResult bufferConsumeQueue = consumeQueue.getIndexBuffer(nextOffset);
            if (bufferConsumeQueue != null) {
                try {
                    int i = 0;
                    for (; i < bufferConsumeQueue.getSize(); i += ConsumeQueue.CQ_STORE_UNIT_SIZE) {
                        long offsetPy = bufferConsumeQueue.getByteBuffer().getLong();
                        InetSocketAddress inetSocketAddress = (InetSocketAddress) storeHost;
                        int msgIdLength = (inetSocketAddress.getAddress() instanceof Inet6Address) ? 16 + 4 + 8 : 4 + 4 + 8;
                        final ByteBuffer msgIdMemory = ByteBuffer.allocate(msgIdLength);
                        String msgId =
                            MessageDecoder.createMessageId(msgIdMemory, MessageExt.socketAddress2ByteBuffer(storeHost), offsetPy);
                        messageIds.put(msgId, nextOffset++);
                        if (nextOffset > maxOffset) {
                            return messageIds;
                        }
                    }
                } finally {

                    bufferConsumeQueue.release();
                }
            } else {
                return messageIds;
            }
        }
    }
    return messageIds;
}