[refactoring] careful close indexes storage after a failed attempt

+ there are >1 attempt to open an indexing storages (to overcome possible errors), but failed storage wasn't closed carefully enough -> hopefully, fixed
+ comments/TODOs

GitOrigin-RevId: 06dcf580c13593fabbca09d775cbee18914e14ae
This commit is contained in:
Ruslan Cheremin
2024-09-25 17:07:44 +00:00
committed by intellij-monorepo-bot
parent 97615afac1
commit e482d582cb
3 changed files with 32 additions and 15 deletions
@@ -487,15 +487,16 @@ public final class FileBasedIndexImpl extends FileBasedIndexEx {
@NotNull IndexConfiguration state,
@NotNull IndexVersionRegistrationSink registrationStatusSink,
@NotNull IntSet dirtyFiles) throws Exception {
ID<K, V> name = extension.getName();
ID<K, V> indexId = extension.getName();
InputFilter inputFilter = extension.getInputFilter();
UpdatableIndex<K, V, FileContent, ?> index = null;
VfsAwareIndexStorageLayout<K, V> layout = null;
int attemptCount = 2;
for (int attempt = 0; attempt < attemptCount; attempt++) {
try {
VfsAwareIndexStorageLayout<K, V> layout = IndexStorageLayoutLocator.getLayout(extension);
layout = IndexStorageLayoutLocator.getLayout(extension);
index = createIndex(extension, layout);
for (FileBasedIndexInfrastructureExtension infrastructureExtension : FileBasedIndexInfrastructureExtension.EP_NAME.getExtensionList()) {
@@ -505,38 +506,48 @@ public final class FileBasedIndexImpl extends FileBasedIndexEx {
}
}
state.registerIndex(name,
state.registerIndex(indexId,
index,
inputFilter,
version + GlobalIndexFilter.getFiltersVersion(name));
version + GlobalIndexFilter.getFiltersVersion(indexId));
break;
}
catch (Exception e) {
boolean lastAttempt = attempt == attemptCount - 1;
//close the (half-)opened index first:
if (index != null) {
try {
index.dispose();
}
catch (Throwable t) {
LOG.error(t);
}
}
try {
VfsAwareIndexStorageLayout<K, V> layout = IndexStorageLayoutLocator.getLayout(extension);
if (layout == null) {
layout = IndexStorageLayoutLocator.getLayout(extension);
}
layout.clearIndexData();
}
catch (Exception layoutEx) {
LOG.error(layoutEx);
catch (Throwable t) {
LOG.error(t);
}
for (FileBasedIndexInfrastructureExtension ext : FileBasedIndexInfrastructureExtension.EP_NAME.getExtensionList()) {
try {
ext.resetPersistentState(name);
ext.resetPersistentState(indexId);
}
catch (Exception extEx) {
LOG.error(extEx);
}
}
registrationStatusSink.setIndexVersionDiff(name, new IndexVersion.IndexVersionDiff.CorruptedRebuild(version));
IndexVersion.rewriteVersion(name, version);
IndexStatisticGroup.reportIndexRebuild(name, e, true);
registrationStatusSink.setIndexVersionDiff(indexId, new IndexVersion.IndexVersionDiff.CorruptedRebuild(version));
IndexVersion.rewriteVersion(indexId, version);
IndexStatisticGroup.reportIndexRebuild(indexId, e, true);
boolean lastAttempt = (attempt == attemptCount - 1);
if (lastAttempt) {
state.registerIndexInitializationProblem(name, e);
state.registerIndexInitializationProblem(indexId, e);
if (extension instanceof CustomImplementationFileBasedIndexExtension) {
((CustomImplementationFileBasedIndexExtension<?, ?>)extension).handleInitializationError(e);
}
@@ -1261,7 +1272,7 @@ public final class FileBasedIndexImpl extends FileBasedIndexEx {
try {
FileBasedIndex fileBasedIndex = app.getServiceIfCreated(FileBasedIndex.class);
if (fileBasedIndex instanceof FileBasedIndexImpl fileBasedIndexImpl) {
if(calledByShutdownHook) {
if (calledByShutdownHook) {
//prevent unregistering the task from ShutDownTracker if we're already called from ShutDownTracker:
// (unregister fails if ShutDownTracker's executing is already triggered)
fileBasedIndexImpl.myShutDownTask = null;
@@ -22,6 +22,7 @@ import java.io.IOException;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
/** In-memory index, with persistent index as a 'backend' storage -- so it is not really 'transient' */
@Internal
public class TransientFileContentIndex<Key, Value, FileCachedData extends VfsAwareMapReduceIndex.IndexerIdHolder>
extends VfsAwareMapReduceIndex<Key, Value, FileCachedData> {
@@ -44,6 +45,7 @@ public class TransientFileContentIndex<Key, Value, FileCachedData extends VfsAwa
@Override
public void clearIndexData() {
//TODO why we don't clear TransientChangesIndexStorage's in-memory cache?
indexStorageLayout.clearIndexData();
}
@@ -29,5 +29,9 @@ public interface InvertedIndex<Key, Value, Input> {
void clear() throws StorageException;
/**
* TODO RC: the interface doesn't extend {@link com.intellij.openapi.Disposable}, so the name is misleading -- the method is
* better renamed to .close() with interface extending (Auto){@link java.io.Closeable}
*/
void dispose();
}