public Optional<FileRecords.TimestampAndOffset> findOffsetByTimestamp(long timestampMs, long startingOffset) throws IOException {
        // Get the index entry with a timestamp less than or equal to the target timestamp
        TimestampOffset timestampOffset = timeIndex().lookup(timestampMs);
        int position = offsetIndex().lookup(Math.max(timestampOffset.offset, startingOffset)).position;

        // Search the timestamp
        return Optional.ofNullable(log.searchForTimestamp(timestampMs, position, startingOffset));
    }
Search the message offset based on timestamp and offset.

This method returns an option of TimestampOffset. The returned value is determined using the following ordered list of rules:

- If all the messages in the segment have smaller offsets, return Empty
- If all the messages in the segment have smaller timestamps, return Empty
- If all the messages in the segment have larger timestamps, or no message in the segment has a timestamp
  the returned the offset will be max(the base offset of the segment, startingOffset) and the timestamp will be Message.NoTimestamp.
- Otherwise, return an option of TimestampOffset. The offset is the offset of the first message whose timestamp
  is greater than or equals to the target timestamp and whose offset is greater than or equals to the startingOffset.

This method only returns Empty when 1) all messages' offset < startOffing or 2) the log is not empty, but we did not
see any message when scanning the log from the indexed position. The latter could happen if the log is truncated
after we get the indexed position but before we scan the log from there. In this case we simply return Empty and the
caller will need to check on the truncated log and maybe retry or even do the search on another log segment.

@param timestampMs The timestamp to search for.
@param startingOffset The starting offset to search.
@return the timestamp and offset of the first message that meets the requirements. Empty will be returned if there is no such message.
storage/src/main/java/org/apache/kafka/storage/internals/log/LogSegment.java