@SuppressWarnings("squid:S135")
  private CrossCompactionTaskResource executeTaskResourceSelection(
      CrossSpaceCompactionCandidate candidate) throws IOException {
    CrossCompactionTaskResource taskResource = new CrossCompactionTaskResource();

    while (candidate.hasNextSplit()) {
      CrossSpaceCompactionCandidate.CrossCompactionTaskResourceSplit split = candidate.nextSplit();
      TsFileResource unseqFile = split.unseqFile.resource;
      List<TsFileResource> targetSeqFiles =
          split.seqFiles.stream().map(c -> c.resource).collect(Collectors.toList());

      if (!split.atLeastOneSeqFileSelected) {
        LOGGER.debug("Unseq file {} does not overlap with any seq files.", unseqFile);
        TsFileResourceCandidate latestSealedSeqFile =
            getLatestSealedSeqFile(candidate.getSeqFileCandidates());
        if (latestSealedSeqFile == null) {
          break;
        }
        if (!latestSealedSeqFile.selected) {
          targetSeqFiles.add(latestSealedSeqFile.resource);
          latestSealedSeqFile.markAsSelected();
        }
      }

      List<TsFileResource> newSelectedSeqResources = new ArrayList<>(taskResource.getSeqFiles());
      newSelectedSeqResources.addAll(targetSeqFiles);
      List<TsFileResource> newSelectedUnseqResources =
          new ArrayList<>(taskResource.getUnseqFiles());
      newSelectedUnseqResources.add(unseqFile);

      long roughEstimatedMemoryCost =
          compactionEstimator.roughEstimateCrossCompactionMemory(
              context, newSelectedSeqResources, newSelectedUnseqResources);
      long memoryCost =
          CompactionEstimateUtils.shouldUseRoughEstimatedResult(roughEstimatedMemoryCost)
              ? roughEstimatedMemoryCost
              : compactionEstimator.estimateCrossCompactionMemory(
                  newSelectedSeqResources, newSelectedUnseqResources);
      if (!canAddToTaskResource(taskResource, unseqFile, targetSeqFiles, memoryCost)) {
        break;
      }
      taskResource.putResources(unseqFile, targetSeqFiles, memoryCost);
      LOGGER.debug(
          "Adding a new unseqFile {} and seqFiles {} as candidates, new cost {}, total cost {}",
          unseqFile,
          targetSeqFiles,
          memoryCost,
          taskResource.getTotalMemoryCost());
    }
    taskResource.sortSeqFiles(candidate.getSeqFiles());
    return taskResource;
  }
In a preset time (30 seconds), for each unseqFile, find the list of seqFiles that overlap with
it and have not been selected by the file selector of this compaction task. After finding each
unseqFile and its corresponding overlap seqFile list, estimate the additional memory overhead
that may be added by compacting them (preferably using the loop estimate), and if it does not
exceed the memory overhead preset by the system for the compaction thread, put them into the
selectedSeqFiles and selectedUnseqFiles.

@throws IOException in prepare next split
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/compaction/selector/impl/RewriteCrossSpaceCompactionSelector.java