private long[] readLastFileInfoAndRepairIt() {
    File[] walFiles = WALFileUtils.listAllWALFiles(logDirectory);
    if (walFiles == null || walFiles.length == 0) {
      return new long[] {0L, 0L};
    }
    // get last search index from last wal file
    WALFileUtils.ascSortByVersionId(walFiles);
    File lastWALFile = walFiles[walFiles.length - 1];
    long lastVersionId = WALFileUtils.parseVersionId(lastWALFile.getName());
    long lastSearchIndex = WALFileUtils.parseStartSearchIndex(lastWALFile.getName());
    WALMetaData metaData = new WALMetaData(lastSearchIndex, new ArrayList<>(), new HashSet<>());
    WALFileStatus fileStatus = WALFileStatus.CONTAINS_NONE_SEARCH_INDEX;
    try (WALReader walReader = new WALReader(lastWALFile, true)) {
      while (walReader.hasNext()) {
        WALEntry walEntry = walReader.next();
        long searchIndex = DEFAULT_SEARCH_INDEX;
        if (walEntry.getType().needSearch()) {
          SearchNode searchNode = (SearchNode) walEntry.getValue();
          if (searchNode.getSearchIndex() != SearchNode.NO_CONSENSUS_INDEX) {
            searchIndex = searchNode.getSearchIndex();
            lastSearchIndex = Math.max(lastSearchIndex, searchNode.getSearchIndex());
            fileStatus = WALFileStatus.CONTAINS_SEARCH_INDEX;
          }
        }
        metaData.setTruncateOffSet(walReader.getWALCurrentReadOffset());
        metaData.add(walEntry.serializedSize(), searchIndex, walEntry.getMemTableId());
      }
    } catch (Exception e) {
      logger.warn("Fail to read wal logs from {}, skip them", lastWALFile, e);
    }
    // make sure last wal file is correct
    repairWalFileIfBroken(lastWALFile, metaData);
    // rename last wal file when file status are inconsistent
    if (WALFileUtils.parseStatusCode(lastWALFile.getName()) != fileStatus) {
      String targetName =
          WALFileUtils.getLogFileName(
              WALFileUtils.parseVersionId(lastWALFile.getName()),
              WALFileUtils.parseStartSearchIndex(lastWALFile.getName()),
              fileStatus);
      if (!lastWALFile.renameTo(SystemFileFactory.INSTANCE.getFile(logDirectory, targetName))) {
        logger.error("Fail to rename file {} to {}", lastWALFile, targetName);
      }
    }
    return new long[] {lastVersionId, lastSearchIndex};
  }
This task is responsible for the recovery of one wal node.
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/recover/WALNodeRecoverTask.java