public void free(File fileToReturn, long sizeInBytes, String storeId, boolean isSwapSegment) throws IOException {
    long startTime = System.currentTimeMillis();
    try {
      if (enablePooling && poolState != PoolState.INITIALIZED) {
        logger.info("Freeing segment of size {} from {} before pool is fully initialized", sizeInBytes,
            fileToReturn.getAbsolutePath());
        metrics.diskSpaceAllocatorFreeBeforeInitCount.inc();
      } else {
        logger.info("Freeing {} segment of size {} from {}", isSwapSegment ? "swap" : "store[" + storeId + "]",
            sizeInBytes, fileToReturn.getAbsolutePath());
      }
      // For now, we delete the file and create a new one. Newer linux kernel versions support
      // additional fallocate flags, which will be useful for cleaning up returned files.
      Files.delete(fileToReturn.toPath());
      if (poolState == PoolState.INITIALIZED) {
        if (isSwapSegment) {
          fileToReturn = createReserveFile(sizeInBytes, swapReserveDir);
          swapReserveFiles.add(sizeInBytes, fileToReturn);
        } else {
          File storeReserveDir = new File(reserveDir, STORE_DIR_PREFIX + storeId);
          fileToReturn = createReserveFile(sizeInBytes, storeReserveDir);
          ReserveFileMap sizeToFilesMap = storeReserveFiles.getOrDefault(storeId, new ReserveFileMap());
          sizeToFilesMap.add(sizeInBytes, fileToReturn);
          storeReserveFiles.put(storeId, sizeToFilesMap);
        }
      }
    } finally {
      long elapsedTime = System.currentTimeMillis() - startTime;
      logger.debug("free took {} ms for store[{}]", elapsedTime, storeId);
      metrics.diskSpaceAllocatorFreeTimeMs.update(elapsedTime);
    }
  }
Return a file to the pool. The user must keep track of the size of the file allocated.
@param fileToReturn the file to return to the pool.
@param sizeInBytes the size of the file to return.
@param storeId the id of store from which the segment is released.
@param isSwapSegment {@code true} if segment to return was used for swap purpose in compaction.
                     {@code false} otherwise.
@throws IOException if the file to return does not exist or cannot be cleaned or recreated correctly.
ambry-store/src/main/java/com/github/ambry/store/DiskSpaceAllocator.java