commitLog 刪除文件的策略
指定時間到了,磁盤不足,人工刪除,滿足任一條件,判斷文件是否過期或者磁盤嚴重不足(超過 85%),是則刪除,一批次最多刪除 10 個文件。
有個地方需要注意,mmap 寫文件,windows 觀察,發現文件的修改時間戳一直不變,linux 還未驗證。
commitLog,consumeQueue,indexFile 的刪除策略如下圖:
commitLog 尾部是有空洞的,當一個消息在當前文件放不下時,rocketmq 認為下一個文件一定能放下該消息,消息不會分隔保存。
commitLog 文件尾部存在至少 8 字節的空洞。
一般情況的尾部組成:maxBlank,BLANK_MAGIC_CODE,隨機的內容
// org.apache.rocketmq.store.CommitLog.DefaultAppendMessageCallback#doAppend if ((msgLen + END_FILE_MIN_BLANK_LENGTH) > maxBlank) { this.resetByteBuffer(this.msgStoreItemMemory, maxBlank); // 1 TOTALSIZE this.msgStoreItemMemory.putInt(maxBlank); // 2 MAGICCODE this.msgStoreItemMemory.putInt(CommitLog.BLANK_MAGIC_CODE); // 3 The remaining space may be any value // Here the length of the specially set maxBlank 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); }