@SuppressWarnings("squid:S3776")
  private static boolean handleWhenAllSourceFilesExist(
      List<TsFileResource> targetResourceList,
      List<TsFileResource> sourceSeqResourceList,
      List<TsFileResource> sourceUnseqResourceList,
      TsFileManager tsFileManager,
      long timePartition,
      boolean isTargetSequence,
      String fullStorageGroupName)
      throws IOException {
    TsFileResourceList unseqTsFileResourceList =
        tsFileManager.getOrCreateUnsequenceListByTimePartition(timePartition);
    TsFileResourceList seqTsFileResourceList =
        tsFileManager.getOrCreateSequenceListByTimePartition(timePartition);

    // delete compaction mods files
    CompactionUtils.deleteCompactionModsFile(sourceSeqResourceList, sourceUnseqResourceList);

    boolean removeAllTargetFile = true;
    tsFileManager.writeLock("CompactionExceptionHandler");
    try {
      for (TsFileResource targetTsFile : targetResourceList) {
        if (targetTsFile == null) {
          // target file has been deleted due to empty after compaction
          continue;
        }
        // delete target file
        targetTsFile.writeLock();
        if (!targetTsFile.remove()) {
          LOGGER.error(
              "{} [Compaction][Exception] fail to delete target tsfile {} when handling exception",
              fullStorageGroupName,
              targetTsFile);
          removeAllTargetFile = false;
        }
        targetTsFile.writeUnlock();

        // remove target tsfile resource in memory
        if (targetTsFile.isFileInList()) {
          if (isTargetSequence) {
            seqTsFileResourceList.remove(targetTsFile);
          } else {
            unseqTsFileResourceList.remove(targetTsFile);
          }
          TsFileResourceManager.getInstance().removeTsFileResource(targetTsFile);
        }
      }

      // recover source tsfile resource in memory
      for (TsFileResource tsFileResource : sourceSeqResourceList) {
        if (!tsFileResource.isFileInList()) {
          seqTsFileResourceList.keepOrderInsert(tsFileResource);
          TsFileResourceManager.getInstance().registerSealedTsFileResource(tsFileResource);
        }
      }
      for (TsFileResource tsFileResource : sourceUnseqResourceList) {
        if (!tsFileResource.isFileInList()) {
          unseqTsFileResourceList.keepOrderInsert(tsFileResource);
          TsFileResourceManager.getInstance().registerSealedTsFileResource(tsFileResource);
        }
      }
    } finally {
      tsFileManager.writeUnlock();
    }
    return removeAllTargetFile;
  }
When all source files exists: (1) delete compaction mods files (2) delete target files, tmp
target files and its corresponding files (3) recover memory. To avoid triggering OOM again
under OOM errors, we do not check whether the target files are complete.

@throws IOException if the deletion of compaction mods file failed or tsfile name is incorrect
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/compaction/execute/exception/CompactionExceptionHandler.java