KAFKA-14792: Race condition in LazyIndex.get() (#13359)

`LazyIndex.get()` has a race condition that can result in a ClassCastException being thrown in some cases.

This was introduced when `LazyIndex` was rewritten from Scala to Java.

I didn't include a test since it's a bit overkill to add a concurrent test for this.

Reviewers: Jun Rao <junrao@gmail.com>
This commit is contained in:
Ismael Juma 2023-03-07 15:56:24 -08:00 committed by GitHub
parent e3817cac89
commit 77215eded7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 9 deletions

View File

@ -166,20 +166,25 @@ public class LazyIndex<T extends AbstractIndex> {
@SuppressWarnings("unchecked")
public T get() throws IOException {
if (indexWrapper instanceof IndexValue<?>)
return ((IndexValue<T>) indexWrapper).index;
else if (indexWrapper instanceof IndexFile) {
IndexWrapper wrapper = indexWrapper;
if (wrapper instanceof IndexValue<?>)
return ((IndexValue<T>) wrapper).index;
else {
lock.lock();
try {
IndexFile indexFile = (IndexFile) indexWrapper;
IndexValue<T> indexValue = new IndexValue<>(loadIndex(indexFile.file));
indexWrapper = indexValue;
return indexValue.index;
if (indexWrapper instanceof IndexValue<?>)
return ((IndexValue<T>) indexWrapper).index;
else if (indexWrapper instanceof IndexFile) {
IndexFile indexFile = (IndexFile) indexWrapper;
IndexValue<T> indexValue = new IndexValue<>(loadIndex(indexFile.file));
indexWrapper = indexValue;
return indexValue.index;
} else
throw new IllegalStateException("Unexpected type for indexWrapper " + indexWrapper.getClass());
} finally {
lock.unlock();
}
} else
throw new IllegalStateException("Unexpected type for indexWrapper " + indexWrapper.getClass());
}
}
public void updateParentDir(File parentDir) {