Repository: apache/kafka
File: storage/src/main/java/org/apache/kafka/storage/internals/log/AbstractIndex.java (Java)

Code before:
     * Trim this segment to fit just the valid entries, deleting all trailing unwritten bytes from
     * the file.
     */
    public void trimToValidSize() throws IOException {
        lock.lock();
        try {
            if (mmap != null) {
                resize(entrySize() * entries);
            }
        } finally {
            lock.unlock();
        }
    }

    /**
     * The number of bytes actually used by this index
     */
    public int sizeInBytes() {
        return entrySize() * entries;
    }

    public void close() throws IOException {
        trimToValidSize();
        closeHandler();
    }

    public void closeHandler() {
        // On JVM, a memory mapping is typically unmapped by garbage collector.
        // However, in some cases it can pause application threads(STW) for a long moment reading metadata from a physical disk.
        // To prevent this, we forcefully cleanup memory mapping within proper execution which never affects API responsiveness.
        // See https://issues.apache.org/jira/browse/KAFKA-4614 for the details.
        lock.lock();
        try {
            safeForceUnmap();
        } finally {
            lock.unlock();
        }
    }

    /**
     * Remove all the entries from the index and resize the index to the max index size.
     */
    public void reset() throws IOException {
        truncate();
        resize(maxIndexSize);
    }

    /**
     * Get offset relative to base offset of this index
     * @throws IndexOffsetOverflowException
     */

Review comment (suggestion):
```java
inLockThrows(() -> inRemapWriteLockThrows(this::safeForceUnmap));
```

Code after:
    public void trimToValidSize() throws IOException {
        inLockThrows(() -> {
            if (mmap != null) {
                resize(entrySize() * entries);
            }
        });
    }

    /**
     * The number of bytes actually used by this index
     */
    public int sizeInBytes() {
        return entrySize() * entries;
    }

    public void close() throws IOException {
        trimToValidSize();
        closeHandler();
    }

    public void closeHandler() {
        // On JVM, a memory mapping is typically unmapped by garbage collector.
        // However, in some cases it can pause application threads(STW) for a long moment reading metadata from a physical disk.
        // To prevent this, we forcefully cleanup memory mapping within proper execution which never affects API responsiveness.
        // See https://issues.apache.org/jira/browse/KAFKA-4614 for the details.
        inLockThrows(() ->
                inRemapWriteLockThrows(() -> {
                    safeForceUnmap();
                }));
    }

    /**
     * Remove all the entries from the index and resize the index to the max index size.
     */
    public void reset() throws IOException {
        truncate();
        resize(maxIndexSize);
    }

    /**
     * Get offset relative to base offset of this index
     * @throws IndexOffsetOverflowException
     */
    public int relativeOffset(long offset) {
        OptionalInt relativeOffset = toRelative(offset);
        return relativeOffset.orElseThrow(() -> new IndexOffsetOverflowException(
            "Integer overflow for offset: " + offset + " (" + file.getAbsoluteFile() + ")"));
    }

    /**
     * Check if a particular offset is valid to be appended to this index.