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")
|
||||
public T get() throws IOException {
|
||||
IndexWrapper wrapper = indexWrapper;
|
||||
if (wrapper instanceof IndexValue<?>)
|
||||
return ((IndexValue<T>) wrapper).index;
|
||||
else {
|
||||
lock.lock();
|
||||
try {
|
||||
if (indexWrapper instanceof IndexValue<?>)
|
||||
return ((IndexValue<T>) indexWrapper).index;
|
||||
else if (indexWrapper instanceof IndexFile) {
|
||||
lock.lock();
|
||||
try {
|
||||
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) {
|
||||
|
|
Loading…
Reference in New Issue