源码分析一AppendMessageCallback.doAppend
- commitlog直接由一条条消息外加文件尾空白填充构成
- 每条消息格式于AppendMessageCallback.doAppend展示
public AppendMessageResult doAppend(final long fileFromOffset, final ByteBuffer byteBuffer, final int maxBlank,
final MessageExtBrokerInner msgInner) {
long wroteOffset = fileFromOffset + byteBuffer.position();
int sysflag = msgInner.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);
this.resetByteBuffer(storeHostHolder, storeHostLength);
String msgId;
if ((sysflag & MessageSysFlag.STOREHOSTADDRESS_V6_FLAG) == 0) {
msgId = MessageDecoder.createMessageId(this.msgIdMemory, msgInner.getStoreHostBytes(storeHostHolder), wroteOffset);
} else {
msgId = MessageDecoder.createMessageId(this.msgIdV6Memory, msgInner.getStoreHostBytes(storeHostHolder), wroteOffset);
}
keyBuilder.setLength(0);
keyBuilder.append(msgInner.getTopic());
keyBuilder.append('-');
keyBuilder.append(msgInner.getQueueId());
String key = keyBuilder.toString();
Long queueOffset = CommitLog.this.topicQueueTable.get(key);
if (null == queueOffset) {
queueOffset = 0L;
CommitLog.this.topicQueueTable.put(key, queueOffset);
}
final int tranType = MessageSysFlag.getTransactionValue(msgInner.getSysFlag());
switch (tranType) {
case MessageSysFlag.TRANSACTION_PREPARED_TYPE:
case MessageSysFlag.TRANSACTION_ROLLBACK_TYPE:
queueOffset = 0L;
break;
case MessageSysFlag.TRANSACTION_NOT_TYPE:
case MessageSysFlag.TRANSACTION_COMMIT_TYPE:
default:
break;
}
final byte[] propertiesData =
msgInner.getPropertiesString() == null ? null : msgInner.getPropertiesString().getBytes(MessageDecoder.CHARSET_UTF8);
final int propertiesLength = propertiesData == null ? 0 : propertiesData.length;
if (propertiesLength > Short.MAX_VALUE) {
log.warn("putMessage message properties length too long. length={}", propertiesData.length);
return new AppendMessageResult(AppendMessageStatus.PROPERTIES_SIZE_EXCEEDED);
}
final byte[] topicData = msgInner.getTopic().getBytes(MessageDecoder.CHARSET_UTF8);
final int topicLength = topicData.length;
final int bodyLength = msgInner.getBody() == null ? 0 : msgInner.getBody().length;
final int msgLen = calMsgLength(msgInner.getSysFlag(), bodyLength, topicLength, propertiesLength);
if (msgLen > this.maxMessageSize) {
CommitLog.log.warn("message size exceeded, msg total size: " + msgLen + ", msg body size: " + bodyLength
+ ", maxMessageSize: " + this.maxMessageSize);
return new AppendMessageResult(AppendMessageStatus.MESSAGE_SIZE_EXCEEDED);
}
if ((msgLen + END_FILE_MIN_BLANK_LENGTH) > maxBlank) {
this.resetByteBuffer(this.msgStoreItemMemory, maxBlank);
this.msgStoreItemMemory.putInt(maxBlank);
this.msgStoreItemMemory.putInt(CommitLog.BLANK_MAGIC_CODE);
final long beginTimeMills = CommitLog.this.defaultMessageStore.now();
byteBuffer.put(this.msgStoreItemMemory.array(), 0, maxBlank);
return new AppendMessageResult(AppendMessageStatus.END_OF_FILE, wroteOffset, maxBlank, msgId, msgInner.getStoreTimestamp(),
queueOffset, CommitLog.this.defaultMessageStore.now() - beginTimeMills);
}
this.resetByteBuffer(msgStoreItemMemory, msgLen);
this.msgStoreItemMemory.putInt(msgLen);
this.msgStoreItemMemory.putInt(CommitLog.MESSAGE_MAGIC_CODE);
this.msgStoreItemMemory.putInt(msgInner.getBodyCRC());
this.msgStoreItemMemory.putInt(msgInner.getQueueId());
this.msgStoreItemMemory.putInt(msgInner.getFlag());
this.msgStoreItemMemory.putLong(queueOffset);
this.msgStoreItemMemory.putLong(fileFromOffset + byteBuffer.position());
this.msgStoreItemMemory.putInt(msgInner.getSysFlag());
this.msgStoreItemMemory.putLong(msgInner.getBornTimestamp());
this.resetByteBuffer(bornHostHolder, bornHostLength);
this.msgStoreItemMemory.put(msgInner.getBornHostBytes(bornHostHolder));
this.msgStoreItemMemory.putLong(msgInner.getStoreTimestamp());
this.resetByteBuffer(storeHostHolder, storeHostLength);
this.msgStoreItemMemory.put(msgInner.getStoreHostBytes(storeHostHolder));
this.msgStoreItemMemory.putInt(msgInner.getReconsumeTimes());
this.msgStoreItemMemory.putLong(msgInner.getPreparedTransactionOffset());
this.msgStoreItemMemory.putInt(bodyLength);
if (bodyLength > 0)
this.msgStoreItemMemory.put(msgInner.getBody());
this.msgStoreItemMemory.put((byte) topicLength);
this.msgStoreItemMemory.put(topicData);
this.msgStoreItemMemory.putShort((short) propertiesLength);
if (propertiesLength > 0)
this.msgStoreItemMemory.put(propertiesData);
final long beginTimeMills = CommitLog.this.defaultMessageStore.now();
byteBuffer.put(this.msgStoreItemMemory.array(), 0, msgLen);
AppendMessageResult result = new AppendMessageResult(AppendMessageStatus.PUT_OK, wroteOffset, msgLen, msgId,
msgInner.getStoreTimestamp(), queueOffset, CommitLog.this.defaultMessageStore.now() - beginTimeMills);
switch (tranType) {
case MessageSysFlag.TRANSACTION_PREPARED_TYPE:
case MessageSysFlag.TRANSACTION_ROLLBACK_TYPE:
break;
case MessageSysFlag.TRANSACTION_NOT_TYPE:
case MessageSysFlag.TRANSACTION_COMMIT_TYPE:
CommitLog.this.topicQueueTable.put(key, ++queueOffset);
break;
default:
break;
}
return result;
}
commitlog消息体格式
名称 | 解释 | 长度 |
---|
TOTALSIZE | 该消息条目总长度 | 4字节 |
MAGICCODE | 魔数固定0xdaa320a7 | 4字节 |
BODYCRC | 消息体CRC校验码 | 4字节 |
QUEUEID | 消息消费队列ID | 4字节 |
FLAG | RocketMQ不做处理供应用程序使用 | 4字节 |
QUEUEOFFSET | 消息在消费队列的偏移量 | 8字节 |
PHYSICALOFFSET | 消息在CommitLog文件中的偏移量 | 8字节 |
SYSFLAG | 消息系统Flag 例如是否压缩 是否是事务 | 8字节 |
BORNTIMESTAMP | 消息生产者调用消息发送API的时间戳 | 8字节 |
BORNHOST | 消息发送者IP,端口号 | 8字节(ip6则20字节) |
STORETIMESTAMP | 消息存储时间戳 | 8字节 |
STOREHOSTADDRESS | Broker服务器IP+端口号 | 8字节 (ip6则20字节) |
RECONSUMETIMES | 消息重试次数 | 4字节 |
Prepared Transaction Offset | 事务消息物理偏移 | 8字节 |
BodyLength | 消息体长度 | 4字节 |
Body | 消息体内容,长度为bodyLength中存储的值 | 变长字节 |
TopicLength | 主题存储长度表示主题名不超过255字符 | 1字节 |
Topic | 主题名 | 变长字节 |
PropertiesLength | 表示消息属性长度不超过65536字符 | 2字节 |
Properties | 消息属性,长度为PropertiesLength中存储的值 | 变长字节 |
commitlog_consumequeue一图解
- 每一个绿色表示一个文件,左边commitlog文件,右边consumequeue文件
- commitlog文件大小1G,尾部填充空白BLANK_MAGIC_CODE
- consumequeue文件20byte*30万 = 5.72M
- consumequeue一条数据20字节,由消息commitlog偏移量_消息大小_taghashCode构成
![在这里插入图片描述](https://img-blog.csdnimg.cn/c57549d409a049578ea415900651069a.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bKB5pyI5Lq6,size_20,color_FFFFFF,t_70,g_se,x_16)
commitlog一图解二
- commitlog结构构造如下[其中BornHostBytes和StoreHostBytes非ip6则8字节,一般8字节]
![在这里插入图片描述](https://img-blog.csdnimg.cn/394a2c5cf42e48b6b1dbb37acf7328f9.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5bKB5pyI5Lq6,size_20,color_FFFFFF,t_70,g_se,x_16)