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

Code before:
        return this.length;
    }

    public int maxIndexSize() {
        return maxIndexSize;
    }

    public long baseOffset() {
        return baseOffset;
    }

    public void updateParentDir(File parentDir) {
        this.file = new File(parentDir, file.getName());
    }

    /**
     * Reset the size of the memory map and the underneath file. This is used in two kinds of cases: (1) in
     * trimToValidSize() which is called at closing the segment or new segment being rolled; (2) at
     * loading segments from disk or truncating back to an old segment where a new log segment became active;
     * we want to reset the index size to maximum index size to avoid rolling new segment.
     *
     * @param newSize new size of the index file
     * @return a boolean indicating whether the size of the memory map and the underneath file is changed or not.
     */
    public boolean resize(int newSize) throws IOException {
        return inRemapReadWriteLock(() -> {
            int roundedNewSize = roundDownToExactMultiple(newSize, entrySize());

            if (length == roundedNewSize) {
                log.debug("Index {} was not resized because it already has size {}", file.getAbsolutePath(), roundedNewSize);
                return false;
            } else {
                RandomAccessFile raf = new RandomAccessFile(file, "rw");
                try {
                    int position = mmap.position();

                    safeForceUnmap();
                    raf.setLength(roundedNewSize);
                    this.length = roundedNewSize;
                    mmap = raf.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, roundedNewSize);
                    this.maxEntries = mmap.limit() / entrySize();
                    mmap.position(position);
                    log.debug("Resized {} to {}, position is {} and limit is {}", file.getAbsolutePath(), roundedNewSize,
                            mmap.position(), mmap.limit());
                    return true;
                } finally {
                    Utils.closeQuietly(raf, "index file " + file.getName());
                }
            }
        });
    }

Review comment (suggestion):
It's probably clearly to do sth like the following. Ditto for `closeHandler()`.
```
inLockThrows(() ->
      inRemapWriteLockThrows( () -> {
```

Code after:
        return this.length;
    }

    public int maxIndexSize() {
        return maxIndexSize;
    }

    public long baseOffset() {
        return baseOffset;
    }

    public void updateParentDir(File parentDir) {
        this.file = new File(parentDir, file.getName());
    }

    /**
     * Reset the size of the memory map and the underneath file. This is used in two kinds of cases: (1) in
     * trimToValidSize() which is called at closing the segment or new segment being rolled; (2) at
     * loading segments from disk or truncating back to an old segment where a new log segment became active;
     * we want to reset the index size to maximum index size to avoid rolling new segment.
     *
     * @param newSize new size of the index file
     * @return a boolean indicating whether the size of the memory map and the underneath file is changed or not.
     */
    public boolean resize(int newSize) throws IOException {
        return inLockThrows(() ->
                inRemapWriteLockThrows(() -> {
                    int roundedNewSize = roundDownToExactMultiple(newSize, entrySize());

                    if (length == roundedNewSize) {
                        log.debug("Index {} was not resized because it already has size {}", file.getAbsolutePath(), roundedNewSize);
                        return false;
                    } else {
                        RandomAccessFile raf = new RandomAccessFile(file, "rw");
                        try {
                            int position = mmap.position();

                            safeForceUnmap();
                            raf.setLength(roundedNewSize);
                            this.length = roundedNewSize;
                            mmap = raf.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, roundedNewSize);
                            this.maxEntries = mmap.limit() / entrySize();
                            mmap.position(position);
                            log.debug("Resized {} to {}, position is {} and limit is {}", file.getAbsolutePath(), roundedNewSize,
                                    mmap.position(), mmap.limit());
                            return true;
                        } finally {
                            Utils.closeQuietly(raf, "index file " + file.getName());
                        }
                    }
                }));