Skip to content

Commit

Permalink
HSEARCH-4983 More robust writer/reader clearing in IndexAccessorImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere committed Sep 29, 2023
1 parent a8823c5 commit 5fa9a50
Showing 1 changed file with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ public IndexAccessorImpl(EventContext eventContext,
@Override
public void close() throws IOException {
try ( Closer<IOException> closer = new Closer<>() ) {
closer.push( IndexWriterProvider::clear, indexWriterProvider );
// Clear the reader first, as it may depend on the writer (see NearRealTimeIndexReaderProvider).
closer.push( IndexReaderProvider::clear, indexReaderProvider );
closer.push( IndexWriterProvider::clear, indexWriterProvider );
}
}

Expand Down Expand Up @@ -94,8 +95,12 @@ public void validateIndexExists() {
@Override
public void dropIndexIfExisting() {
try {
// Ensure no one is using the directory
clear();
// Clear current writer/readers so that they no longer hold on to the directory.
// We assume that no operation on the directory is happening concurrently,
// so that nobody will try to create new writers/readers concurrently;
// if that happens anyway, either this dropping of the index
// or the concurrent writes/reads will fail.
close();

Directory directory = directoryHolder.get();

Expand All @@ -107,19 +112,18 @@ public void dropIndexIfExisting() {
for ( String file : files ) {
directory.deleteFile( file );
}

// Clear current writer/readers again, in case someone illegally
// tried to perform operations on the directory concurrently:
// that could result into writers/readers pointing to missing files.
// If nobody did anything illegal, this close() call is a noop.
close();
}
catch (IOException | RuntimeException e) {
throw log.unableToDropIndexDirectory( e.getMessage(), eventContext, e );
}
}

private synchronized void clear() throws IOException {
try ( Closer<IOException> closer = new Closer<>() ) {
closer.push( IndexWriterProvider::clear, indexWriterProvider );
closer.push( IndexReaderProvider::clear, indexReaderProvider );
}
}

@Override
public void commit() {
IndexWriterDelegatorImpl delegator = indexWriterProvider.getOrNull();
Expand Down

0 comments on commit 5fa9a50

Please sign in to comment.