mirror of https://github.com/apache/kafka.git
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:
parent
e3817cac89
commit
77215eded7
|
@ -166,20 +166,25 @@ public class LazyIndex<T extends AbstractIndex> {
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public T get() throws IOException {
|
public T get() throws IOException {
|
||||||
if (indexWrapper instanceof IndexValue<?>)
|
IndexWrapper wrapper = indexWrapper;
|
||||||
return ((IndexValue<T>) indexWrapper).index;
|
if (wrapper instanceof IndexValue<?>)
|
||||||
else if (indexWrapper instanceof IndexFile) {
|
return ((IndexValue<T>) wrapper).index;
|
||||||
|
else {
|
||||||
lock.lock();
|
lock.lock();
|
||||||
try {
|
try {
|
||||||
IndexFile indexFile = (IndexFile) indexWrapper;
|
if (indexWrapper instanceof IndexValue<?>)
|
||||||
IndexValue<T> indexValue = new IndexValue<>(loadIndex(indexFile.file));
|
return ((IndexValue<T>) indexWrapper).index;
|
||||||
indexWrapper = indexValue;
|
else if (indexWrapper instanceof IndexFile) {
|
||||||
return indexValue.index;
|
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 {
|
} finally {
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
}
|
}
|
||||||
} else
|
}
|
||||||
throw new IllegalStateException("Unexpected type for indexWrapper " + indexWrapper.getClass());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateParentDir(File parentDir) {
|
public void updateParentDir(File parentDir) {
|
||||||
|
|
Loading…
Reference in New Issue