public int executeCompaction() throws InterruptedException {
    if (!isCompactionSelecting.compareAndSet(false, true)) {
      return 0;
    }
    CompactionScheduleContext context = new CompactionScheduleContext();
    try {
      List<Long> timePartitions = new ArrayList<>(tsFileManager.getTimePartitions());
      // Sort the time partition from largest to smallest
      timePartitions.sort(Comparator.reverseOrder());

      // schedule insert compaction
      int[] submitCountOfTimePartitions = executeInsertionCompaction(timePartitions, context);

      // schedule the other compactions
      for (int i = 0; i < timePartitions.size(); i++) {
        boolean skipOtherCompactionSchedule =
            submitCountOfTimePartitions[i] > 0
                && !config
                    .getDataRegionConsensusProtocolClass()
                    .equals(ConsensusFactory.IOT_CONSENSUS_V2);
        if (skipOtherCompactionSchedule) {
          continue;
        }
        long timePartition = timePartitions.get(i);
        CompactionScheduler.sharedLockCompactionSelection();
        try {
          CompactionScheduler.scheduleCompaction(tsFileManager, timePartition, context);
        } finally {
          context.clearTimePartitionDeviceInfoCache();
          CompactionScheduler.sharedUnlockCompactionSelection();
        }
      }
      if (context.hasSubmitTask()) {
        CompactionMetrics.getInstance().updateCompactionTaskSelectionNum(context);
      }
    } catch (InterruptedException e) {
      throw e;
    } catch (Throwable e) {
      logger.error("Meet error in compaction schedule.", e);
    } finally {
      isCompactionSelecting.set(false);
    }
    return context.getSubmitCompactionTaskNum();
  }
Put the memtable back to the MemTablePool and make the metadata in writer visible
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/DataRegion.java