code: static Map<Long, Long> fenceOut(List<LedgerMetadata> ledgers, BookKeeper bookKeeper, BookKeeperConfig config, String traceObjectId) throws DurableDataLogException {
          // Fence out the ledgers, in descending order. During the process, we need to determine whether the ledgers we
          // fenced out actually have any data in them, and update the LedgerMetadata accordingly.
          // We need to fence out at least MIN_FENCE_LEDGER_COUNT ledgers that are not empty to properly ensure we fenced
          // the log correctly and identify any empty ledgers (Since this algorithm is executed upon every recovery, any
          // empty ledgers should be towards the end of the Log).
          int nonEmptyCount = 0;
          val result = new HashMap<Long, Long>();
          val iterator = ledgers.listIterator(ledgers.size());
          while (iterator.hasPrevious() && (nonEmptyCount < MIN_FENCE_LEDGER_COUNT)) {
              LedgerMetadata ledgerMetadata = iterator.previous();
              LedgerHandle handle = openFence(ledgerMetadata.getLedgerId(), bookKeeper, config);
              if (handle.getLastAddConfirmed() != NO_ENTRY_ID) {
                  // Non-empty.
                  nonEmptyCount++;
              }
  
              if (ledgerMetadata.getStatus() == LedgerMetadata.Status.Unknown) {
                  // We did not know the status of this Ledger before, but now we do.
                  result.put(ledgerMetadata.getLedgerId(), handle.getLastAddConfirmed());
              }
  
              close(handle);
              log.info("{}: Fenced out Ledger {}.", traceObjectId, ledgerMetadata);
          }
  
          return result;
      }
docs: Fences out a Log made up of the given ledgers.
  
  @param ledgers       An ordered list of LedgerMetadata objects representing all the Ledgers in the log.
  @param bookKeeper    A reference to the BookKeeper client to use.
  @param config        Configuration to use.
  @param traceObjectId Used for logging.
  @return A Map of LedgerId to LastAddConfirmed for those Ledgers that were fenced out and had a different
  LastAddConfirmed than what their LedgerMetadata was indicating.
  @throws DurableDataLogException If an exception occurred. The causing exception is wrapped inside it.