void sanityCheckBeforeAndAfterCompactionIndexSegmentOffsets() {
    Set<Offset> seen = new HashSet<>();
    for (Map.Entry<Offset, Offset> entry : beforeAndAfterCompactionIndexSegmentOffsets.entrySet()) {
      // Before should not in the index segment map
      Offset before = entry.getKey();
      Offset after = entry.getValue();
      if (validIndexSegments.get(before) != null) {
        logger.error("Index {}: Before Offset {} shouldn't be in the index segment map", dataDir, before);
        metrics.beforeAndAfterOffsetSanityCheckFailureCount.inc();
        sanityCheckFailed = true;
        return;
      }
      seen.clear();
      seen.add(before);
      seen.add(after);
      // There should be a path from before offset to the current valid index segment.
      IndexSegment segment = validIndexSegments.get(after);
      while (segment == null) {
        after = beforeAndAfterCompactionIndexSegmentOffsets.get(after);
        if (after == null) {
          break;
        }
        if (seen.contains(after)) {
          logger.error("Index {}: loop in the before and after for Offset: {}", dataDir, before);
          sanityCheckFailed = true;
          return;
        } else {
          seen.add(after);
        }
        segment = validIndexSegments.get(after);
      }
      if (segment == null) {
        logger.error("Index {}: No path for Offset {} to reach a valid index segment", dataDir, before);
        metrics.beforeAndAfterOffsetSanityCheckFailureCount.inc();
        sanityCheckFailed = true;
        return;
      }
    }
    logger.info("Index {}: Sanity check for before and after compaction index segment offsets succeeded", dataDir);
    sanityCheckFailed = false;
  }
Sanity check the before and after compaction index segment offsets.
- All before offsets shouldn't point to any existing valid index segment
- All before offsets should have a valid path to an existing valid index segment.
ambry-store/src/main/java/com/github/ambry/store/PersistentIndex.java