Concurrent calls to refresh might result in "dangling" searchers, closes #823.

This commit is contained in:
kimchy 2011-04-01 22:43:48 +03:00
parent 1280512097
commit db1dcaded3
1 changed files with 16 additions and 10 deletions

View File

@ -128,6 +128,8 @@ public class RobinEngine extends AbstractIndexShardComponent implements Engine {
private final Object[] dirtyLocks; private final Object[] dirtyLocks;
private final Object refreshMutex = new Object();
private final ApplySettings applySettings = new ApplySettings(); private final ApplySettings applySettings = new ApplySettings();
@Inject public RobinEngine(ShardId shardId, @IndexSettings Settings indexSettings, IndexSettingsService indexSettingsService, @Inject public RobinEngine(ShardId shardId, @IndexSettings Settings indexSettings, IndexSettingsService indexSettingsService,
@ -577,16 +579,20 @@ public class RobinEngine extends AbstractIndexShardComponent implements Engine {
throw new EngineClosedException(shardId); throw new EngineClosedException(shardId);
} }
try { try {
if (dirty || refresh.force()) { // we need to obtain a mutex here, to make sure we don't leave dangling readers
// we eagerly set dirty to false so we won't miss refresh requests // we could have used an AtomicBoolean#compareAndSet, but, then we might miss refresh requests
dirty = false; // compared to on going ones
AcquirableResource<ReaderSearcherHolder> current = nrtResource; synchronized (refreshMutex) {
IndexReader newReader = current.resource().reader().reopen(true); if (dirty || refresh.force()) {
if (newReader != current.resource().reader()) { dirty = false;
ExtendedIndexSearcher indexSearcher = new ExtendedIndexSearcher(newReader); AcquirableResource<ReaderSearcherHolder> current = nrtResource;
indexSearcher.setSimilarity(similarityService.defaultSearchSimilarity()); IndexReader newReader = current.resource().reader().reopen(true);
nrtResource = newAcquirableResource(new ReaderSearcherHolder(indexSearcher)); if (newReader != current.resource().reader()) {
current.markForClose(); ExtendedIndexSearcher indexSearcher = new ExtendedIndexSearcher(newReader);
indexSearcher.setSimilarity(similarityService.defaultSearchSimilarity());
nrtResource = newAcquirableResource(new ReaderSearcherHolder(indexSearcher));
current.markForClose();
}
} }
} }
} catch (AlreadyClosedException e) { } catch (AlreadyClosedException e) {