(Author: Yuan Tian)
Recovery are performed at the granularity of the storage group, and the entry point for recovery is the recover() of the StorageGroupProcessor
Recovery Process Of Storage Group
First get all the data files ending with .tsfile in the storage group, return TsFileResource, there are several file lists as follows
- Sequence Files
- 0.10 version tsfiles(sealed/unsealed)
- 0.9 version tsfiles(sealed)
- Unsequence Files
- 0.10 version tsfiles(sealed/unsealed)
- 0.9 version tsfiles(sealed)
- Sequence Files
If there exists 0.9 version TsFile in the storage group, add the old version's sequence and unsequence files to
upgradeSeqFileList
andupgradeSeqFileList
respectively for upgrade and query.Group sequence and unsequence files according to partition --
Map<Long, List<TsFileResource>>
To recover the sequential files of each partition, take the sequential TsFile of each partition obtained in the previous step as a parameter, and call
recoverTsFiles
to recover. This method will put the restored sequence TsFile into thesequenceFileTreeSet
in the form of TsFileResource. If the TsFile is the last one of this partition and it is not sealed, construct aTsFileProcessor
object for it and add it toworkSequenceTsFileProcessors
. The specific details of this method will be explained in the next section.To recover the disordered files of each partition, take the unsequential TsFile of each partition obtained in the previous step as a parameter, and call
recoverTsFiles
to recover. This method will put the restored unsequence TsFile into theunSequenceFileList
in the form of TsFileResource. if the TsFile is the last one in this partition and it is not sealed, aTsFileProcessor
object must be constructed for it and added toworkUnsequenceTsFileProcessors
. The specific details of this method will be explained in the next section.Traverse the
sequenceFileTreeSet
andunSequenceFileList
obtained in the previous two steps respectively, and update the version number corresponding to the partitionCheck whether there is a Modification file during the merge, and call the
RecoverMergeTask.recoverMerge
method to recover the mergeCall the
updateLastestFlushedTime()
method to update thelatestTimeForEachDevice
,partitionLatestFlushedTimeForEachDevice
andglobalLatestFlushedTimeForEachDevice
with sequential tsfile of version 0.9latestTimeForEachDevice
records the latest timestamp under each partition that all devices have been inserted into (including unflushed and flushed)partitionLatestFlushedTimeForEachDevice
records the latest timestamp of all devices under each partition that has been flushed. It is used to determine whether a newly inserted point is out of order.globalLatestFlushedTimeForEachDevice
records the latest timestamp of all devices that have been flushed (a summary of the latest timestamps of each partition)
Finally traverse
sequenceFileTreeSet
, and use the restored sequence file to updatelatestTimeForEachDevice
,partitionLatestFlushedTimeForEachDevice
andglobalLatestFlushedTimeForEachDevice
again
Recover a TsFile(Seq/Unseq) of each partiton
- org.apache.iotdb.db.engine.storagegroup.StorageGroupProcessor.recoverTsFiles
This method is mainly responsible for traversing all TsFiles passed in and recovering them one by one.
Construct a
TsFileRecoverPerformer
object to recover the TsFile. The recovery logic is encapsulated in therecover()
method ofTsFileRecoverPerformer
(details will be explained in the next section), which will return a restoredRestorableTsFileIOWriter
Object.- If the recovery process fails, record the log and skip the tsfile
If the TsFile file is not the last file, or the TsFile file is the last file, but has been closed or marked as closed, just set the
closed
attribute of theTsFileResource
object corresponding to the TsFile in memory totrue
.If the TsFile file can continue to be written, it means that this is the last TsFile of this partition, and it is unsealed, and it will continue to remain unsealed. You need to construct a
TsFileProcessor
object for it and place it inworkSequenceTsFileProcessors
orworkUnsequenceTsFileProcessors
.Finally, put the corresponding
TsFileResource
object in the memory of the restored TsFile intosequenceFileTreeSet
orunSequenceFileList
Details about recovering a TsFile
- org.apache.iotdb.db.writelog.recover.TsFileRecoverPerformer.recover
This method is mainly responsible for the recovery of each specific TsFile file.
First use the tsfile to construct a
RestorableTsFileIOWriter
object. In the construction method ofRestorableTsFileIOWriter
, the content of the tsfile will be checked and truncated if necessary- If there is nothing in this file, write
MAGIC_STRING
andVERSION_NUMBER
for it, and return directly. At this time,crashed
isfalse
, andcanWrite
istrue
; - If there is content in this file, construct a
TsFileSequenceReader
object to parse the content, call theselfCheck
method, truncate the incomplete content and initializetruncatedSize
toHeaderLength
- If the content of the file is complete (have a complete header of
MAGIC_STRING
andVERSION_NUMBER
, and a tail ofMAGIC_STRING
), returnTsFileCheckStatus.COMPLETE_FILE
- If the file length is less than
HeaderLength(len(MAGIC_STRING) + len(VERSION_NUMBER))
, or the content of the file header is notMAGIC_STRING + VERSION_NUMBER
, returnINCOMPATIBLE_FILE
- If the file length is exactly equal to
HeaderLength
, and the file content isMAGIC_STRING + VERSION_NUMBER
, then retunrHeaderLength
- If the file length is greater than
HeaderLength
and the file header is legal, but there is noMAGIC_STRING
at the end of the file, it means that the file is incomplete and needs to be truncated. Read fromVERSION_NUMBER
position, read out the data in the following chunk, and recover the ChunkMetadata based on the data in the chunk. If you encounterCHUNK_GROUP_FOOTER
, it means that the entire ChunkGroup is complete. UpdatetruncatedSize
to the current position - Return
truncatedSize
- If the content of the file is complete (have a complete header of
- truncated the file according to the returned
truncatedSize
- If
truncatedSize
is equal toTsFileCheckStatus.COMPLETE_FILE
, setcrashed
andcanWrite
tofalse
, and close the output stream of the file - If
truncatedSize
is equal toTsFileCheckStatus.INCOMPATIBLE_FILE
, the output stream of the file is closed and an exception is thrown - Otherwise, set
crashed
andcanWrite
totrue
and truncated the file totruncatedSize
- If
- If there is nothing in this file, write
Judge whether the file is complete by the returned RestorableTsFileIOWriter
If the TsFile file is complete
- If the resource file corresponding to the TsFile exists, the resource file is deserialized (including the minimum and maximum timestamps of each device in the tsfile), and the file version number is restored
- If the resource file corresponding to the TsFile does not exist, regenerate the resource file and persist it to disk.
- Return the generated
RestorableTsFileIOWriter
If TsFile is incomplete
- Call
recoverResourceFromWriter
to recover the resource information through the ChunkMetadata information inRestorableTsFileIOWriter
- Call the
redoLogs
method to write the data in one or more wal files corresponding to this file to a temporary Memtable and persist to this incomplete TsFile- For sequential files, skip WALs whose timestamp is less than or equal to the current resource
- For unsequential files, redo all WAL, it is possible to repeatedly write to ChunkGroup of multiple devices
- If the TsFile is not the last TsFile of the current partition, or there is a
.closing
file in the TsFile, call theendFile()
method ofRestorableTsFileIOWriter
to seal the file, delete the.closing
file and generates resource file for it.
- Call