@Override
    protected void onTruncateSuffix(final long lastIndexKept) throws RocksDBException, IOException {
        List<SegmentFile> destroyedFiles = null;
        this.writeLock.lock();
        try {
            final int keptFileIndex = binarySearchFileIndexByLogIndex(lastIndexKept);
            int toIndex = binarySearchFileIndexByLogIndex(getLastLogIndex());

            if (keptFileIndex < 0) {
                // When all the segments contain logs that index is greater than lastIndexKept,
                // truncate all segments.
                if (!this.segments.isEmpty()) {
                    final long firstLogIndex = getFirstSegmentWithoutLock().getFirstLogIndex();
                    if (firstLogIndex > lastIndexKept) {
                        final List<SegmentFile> removedFiles = this.segments.subList(0, this.segments.size());
                        destroyedFiles = new ArrayList<>(removedFiles);
                        removedFiles.clear();
                    }
                    LOG.info(
                        "Truncating all segments in {} because the first log index {} is greater than lastIndexKept={}",
                        this.segmentsPath, firstLogIndex, lastIndexKept);
                }

                LOG.warn("Segment file not found by logIndex={} to be truncate_suffix, current segments:\n{}.",
                    lastIndexKept, descSegments());
                return;
            }

            if (toIndex < 0) {
                toIndex = this.segments.size() - 1;
            }

            // Destroyed files after keptFile
            final List<SegmentFile> removedFiles = this.segments.subList(keptFileIndex + 1, toIndex + 1);
            destroyedFiles = new ArrayList<>(removedFiles);
            removedFiles.clear();

            // Process logs in keptFile(firstLogIndex=lastIndexKept)
            final SegmentFile keptFile = this.segments.get(keptFileIndex);
            if (keptFile.isBlank()) {
                return;
            }
            int logWrotePos = -1; // The truncate position in keptFile.

            // Try to find the right position to be truncated.
            {
                // First, find in right [lastIndexKept + 1, getLastLogIndex()]
                long nextIndex = lastIndexKept + 1;
                final long endIndex = Math.min(getLastLogIndex(), keptFile.getLastLogIndex());
                while (nextIndex <= endIndex) {
                    final byte[] data = getValueFromRocksDB(getKeyBytes(nextIndex));
                    if (data != null) {
                        if (data.length == LOCATION_METADATA_SIZE) {
                            if (!isMetadata(data)) {
                                // Stored in rocksdb directly.
                                nextIndex++;
                                continue;
                            }
                            logWrotePos = getWrotePosition(data);
                            break;
                        } else {
                            // Stored in rocksdb directly.
                            nextIndex++;
                        }
                    } else {
                        // No more data.
                        break;
                    }
                }
            }

            // Not found in [lastIndexKept + 1, getLastLogIndex()]
            if (logWrotePos < 0) {
                // Second, try to find in left  [firstLogIndex, lastIndexKept) when lastIndexKept is not stored in segments.
                final byte[] keptData = getValueFromRocksDB(getKeyBytes(lastIndexKept));
                // The kept log's data is not stored in segments.
                if (!isMetadata(keptData)) {
                    //lastIndexKept's log is stored in rocksdb directly, try to find the first previous log that stored in segment.
                    long prevIndex = lastIndexKept - 1;
                    final long startIndex = keptFile.getFirstLogIndex();
                    while (prevIndex >= startIndex) {
                        final byte[] data = getValueFromRocksDB(getKeyBytes(prevIndex));
                        if (data != null) {
                            if (data.length == LOCATION_METADATA_SIZE) {
                                if (!isMetadata(data)) {
                                    // Stored in rocksdb directly.
                                    prevIndex--;
                                    continue;
                                }
                                // Found the position.
                                logWrotePos = getWrotePosition(data);
                                final byte[] logData = onDataGet(prevIndex, data);
                                // Skip this log, it should be kept(logs that are less than lastIndexKept should be kept).
                                // Fine the next log position.
                                logWrotePos += SegmentFile.getWriteBytes(logData);
                                break;
                            } else {
                                // Stored in rocksdb directly.
                                prevIndex--;
                            }
                        } else {
                            LOG.warn(
                                "Log entry not found at index={} when truncating logs suffix from lastIndexKept={}.",
                                prevIndex, lastIndexKept);
                            prevIndex--;
                        }
                    }
                }
            }

            if (logWrotePos >= 0 && logWrotePos < keptFile.getSize()) {
                // Truncate the file from wrotePos and set it's lastLogIndex=lastIndexKept.
                keptFile.truncateSuffix(logWrotePos, lastIndexKept, isSync());
            }
            // Finally, do checkpoint.
            doCheckpoint();

        } finally {
            this.writeLock.unlock();
            if (destroyedFiles != null) {
                for (final SegmentFile segmentFile : destroyedFiles) {
                    segmentFile.destroy();
                }
            }
        }
    }
It's wrote position is from start(HEADER_SIZE) but it's not the last file, SHOULD not happen.
jraft-core/src/main/java/com/alipay/sofa/jraft/storage/log/RocksDBSegmentLogStorage.java