private int validateMemoryRecordsAndReturnCount(MemoryRecords memoryRecords) {
        // Confirm that it is one control batch and it is at least one control record
        Iterator<MutableRecordBatch> batches = memoryRecords.batches().iterator();
        if (!batches.hasNext()) {
            throw new IllegalArgumentException("valueCreator didn't create a batch");
        }

        MutableRecordBatch batch = batches.next();
        Integer numberOfRecords = batch.countOrNull();
        if (!batch.isControlBatch()) {
            throw new IllegalArgumentException("valueCreator didn't create a control batch");
        } else if (batch.baseOffset() != nextOffset) {
            throw new IllegalArgumentException(
                String.format(
                    "Expected a base offset of %d but got %d",
                    nextOffset,
                    batch.baseOffset()
                )
            );
        } else if (batch.partitionLeaderEpoch() != epoch) {
            throw new IllegalArgumentException(
                String.format(
                    "Expected a partition leader epoch of %d but got %d",
                    epoch,
                    batch.partitionLeaderEpoch()
                )
            );
        } else if (numberOfRecords == null) {
            throw new IllegalArgumentException("valueCreator didn't create a batch with the count");
        } else if (numberOfRecords < 1) {
            throw new IllegalArgumentException("valueCreator didn't create at least one control record");
        } else if (batches.hasNext()) {
            throw new IllegalArgumentException("valueCreator created more than one batch");
        }

        return numberOfRecords;
    }
Append a control batch from a supplied memory record.

See the {@code valueCreator} parameter description for requirements on this function.

@param valueCreator a function that uses the passed buffer to create the control
       batch that will be appended. The memory records returned must contain one
       control batch and that control batch have at least one record.
@return the last of offset of the records created
raft/src/main/java/org/apache/kafka/raft/internals/BatchAccumulator.java