RocketMQ storage articles - MapedFileQueue

MapedFileQueue

The application layer accesses the commitlog and consumequeue files by operating the MapedFile class through the MappFileQueue, thereby indirectly operating the files on the disk; the MappFileQueue is composed of multiple MapedFile queues, and the result of this class is shown in the following figure.

The list of functions is as follows:

1. Get files updated after a certain point in time (getMapedFileByTime)

The method getMapedFileByTime(final long timestamp) traverses the MapedFile list. If the update timestamp of the file is greater than the timestamp of a certain time point, the MapedFile object is returned. After the traversal is still not found, the last MapedFile object in the list is returned.

Clean up the files after the file at the specified offset (truncateDirtyFiles)

Method truncateDirtyFiles(long offset): Traverse the MapedFile list, each MapedFile object corresponds to a file of a fixed size, if the starting offset of the file is fileFromOffset<=offset<=fileFromOffset+fileSize, it means the specified position offset If the offset falls on the file, set the writepostion and commitPosition of the corresponding MapedFile object to offset%fileSize. If the starting offset of the file, fileFromOffset>offset, is the file after the hit file, delete these files. And cleared from the MapedFile list of the MappFileQueue.

Get or create the last file (getLastMapedFile)

Get the last MapedFile object from the MapedFile list, if the list is empty or the file corresponding to the last object is full, create a new file (ie a new MapedFile object); if there is the last file (corresponding to the last MapedFile object) ) and is not full, the last MapedFile object is returned directly.
If the list is empty, the filename of the new file created (that is, the fileFromOffset value) is 0; if the last file is full, the filename of the new file is equal to the fileFromOffset+fileSize of the last file;
if the broker starts initialization A background service thread (AllocateMapedFileService service) will be created, and the AllocateMapedFileService.putRequestAndReturnMapedFile method will be called. In this method, the file path of the next file, the path of the next file, and the file size will be used to initialize the AllocateRequest object and put it into the service. In the requestQueue:PriorityBlockingQueue variable of the thread, the thread monitors the requestQueue queue in the background. If there is an AllocateRequest object in the queue, the MapedFile object is created using the variable value of the object (that is, the corresponding physical file is generated in the disk), and the into the MapedFile variable of the AllocateRequest object, and continue to create the next new file after the next new file. If the MappeFile object is created directly in the current thread, only a new file is created.
Finally, the created or returned MapedFile object is stored in the MapedFile list of MapedFileQueue, and the MapedFile object is returned to the caller.

Get the last file in the list (getLastMapedFile2)

?
1
2
<code>从MapedFile列表中获取最后一个MapFile对象;若列表为空则返回 null
</code>

统计内存的数据还有多少未持久化(howMuchFallBehind)

调用getLastMapedFile方法获取Mapped队列中最后一个MapedFile对象,计算得出未刷盘的消息大小,计算公式为:最后一个MapedFile对象fileFromOffset+写入位置wrotepostion-commitedWhere(上次刷盘的位置)。

获取MapedFile队列中最小Offset值(getMinOffset)

先获取MapedFile队列中第一个MapedFile对象,再取该对象的fileFromOffset值(即文件的名字值),该值即为最小offset值;若MapedFile列表为空,则直接返回-1;

获取MapedFile队列中最大Offset值(getMaxOffset)

获取Mapped队列中最后一个MapedFile对象,将最后一个MapedFile对象fileFromOffset加上写入位置wrotepostion值即为最大offset值。

删除某类文件中的最后一个文件(deleteLastMaped)

例如commit类型的文件下面有多个固定大小(1G)的文件,即对应在MapedFile列表中有多个MapedFile对象,若要删除最后一个文件,首先从磁盘中删除物理文件,然后从列表中删除最后一个MapedFile对象。

根据指定的offset找到所在文件(findMapedFileByOffset)

方法findMapedFileByOffset(final long offset, final boolean returnFirstOnNotFound):首先找到MapedFile队列中的第一个MapedFile对象,取该对象的fileFromOffset值;然后指定的offset减去fileFromOffset值再除以fileSize得到文件在MapedFile队列中的序列号index,最后根据index值从列表中获取MapedFile对象。若index值获取对象时出错误(index<0或者大于列表的总数),则根据returnFirstOnNotFound参数决定是返回null或者第一个MapedFile对象。

MapedFile队列中的消息刷盘(commit)

首先根据commitWhere值(上次刷盘的位置)在队列中找到所处的文件对象MapedFile,即调用findMapedFileByOffset方法;然后调用MapedFile对象的commit方法完成消息刷盘操作;最后利用MapedFile对象的fileFromOffset值加上这次刷盘的消息大小得到的总和更新commitWhere值,作为下次刷盘的开始位置,同时更新MapedFileQueue对象的存储时间戳(storeTimestamp)。

 

 

https://www.2cto.com/kf/201708/666767.html

 

https://www.cnblogs.com/guazi/p/6836112.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326522081&siteId=291194637