void maybeUpdateLeaderEpoch(OptionalInt latestLeaderEpoch) {
        if (latestLeaderEpoch.isPresent()
            && (!currentLeaderEpoch.isPresent() || currentLeaderEpoch.getAsInt() < latestLeaderEpoch.getAsInt())) {
            log.trace("For {}, leader will be updated, currentLeaderEpoch: {}, attemptsWhenLeaderLastChanged:{}, latestLeaderEpoch: {}, current attempt: {}",
                this, currentLeaderEpoch, attemptsWhenLeaderLastChanged, latestLeaderEpoch, attempts);
            attemptsWhenLeaderLastChanged = attempts();
            currentLeaderEpoch = latestLeaderEpoch;
        } else {
            log.trace("For {}, leader wasn't updated, currentLeaderEpoch: {}, attemptsWhenLeaderLastChanged:{}, latestLeaderEpoch: {}, current attempt: {}",
                this, currentLeaderEpoch, attemptsWhenLeaderLastChanged, latestLeaderEpoch, attempts);
        }
    }
It will update the leader to which this batch will be produced for the ongoing attempt, if a newer leader is known.
@param latestLeaderEpoch latest leader's epoch.
clients/src/main/java/org/apache/kafka/clients/producer/internals/ProducerBatch.java