From 0dcd2f3e658bd3fa8be27d8fa2c801658f0c5344 Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Mon, 15 Sep 2014 00:49:27 +0000 Subject: [PATCH] SOLR-6518: CachingDirectoryFactory subclasses must init directory with NoLockFactory, because the real lock factory gets set later via injectLockFactory git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1624928 13f79535-47bb-0310-9956-ffa450edef68 --- .../solr/core/CachingDirectoryFactory.java | 30 ++++++++++++------- .../solr/core/MMapDirectoryFactory.java | 4 ++- .../solr/core/NIOFSDirectoryFactory.java | 4 ++- .../solr/core/NRTCachingDirectoryFactory.java | 4 ++- .../solr/core/SimpleFSDirectoryFactory.java | 4 ++- .../solr/core/StandardDirectoryFactory.java | 4 ++- .../solr/core/AlternateDirectoryTest.java | 4 ++- .../solr/core/MockFSDirectoryFactory.java | 4 ++- 8 files changed, 40 insertions(+), 18 deletions(-) diff --git a/solr/core/src/java/org/apache/solr/core/CachingDirectoryFactory.java b/solr/core/src/java/org/apache/solr/core/CachingDirectoryFactory.java index 3f2aecd115e..4dfa4e05eb2 100644 --- a/solr/core/src/java/org/apache/solr/core/CachingDirectoryFactory.java +++ b/solr/core/src/java/org/apache/solr/core/CachingDirectoryFactory.java @@ -38,6 +38,7 @@ import org.apache.lucene.store.NoLockFactory; import org.apache.lucene.store.RateLimitedDirectoryWrapper; import org.apache.lucene.store.SimpleFSLockFactory; import org.apache.lucene.store.SingleInstanceLockFactory; +import org.apache.lucene.util.IOUtils; import org.apache.solr.common.SolrException; import org.apache.solr.common.SolrException.ErrorCode; import org.apache.solr.common.util.NamedList; @@ -347,18 +348,25 @@ public abstract class CachingDirectoryFactory extends DirectoryFactory { directory = cacheValue.directory; } - if (directory == null) { + if (directory == null) { directory = create(fullPath, dirContext); - - directory = rateLimit(directory); - - CacheValue newCacheValue = new CacheValue(fullPath, directory); - - injectLockFactory(directory, fullPath, rawLockType); - - byDirectoryCache.put(directory, newCacheValue); - byPathCache.put(fullPath, newCacheValue); - log.info("return new directory for " + fullPath); + boolean success = false; + try { + directory = rateLimit(directory); + + CacheValue newCacheValue = new CacheValue(fullPath, directory); + + injectLockFactory(directory, fullPath, rawLockType); + + byDirectoryCache.put(directory, newCacheValue); + byPathCache.put(fullPath, newCacheValue); + log.info("return new directory for " + fullPath); + success = true; + } finally { + if (!success) { + IOUtils.closeWhileHandlingException(directory); + } + } } else { cacheValue.refCnt++; log.debug("Reusing cached directory: {}", cacheValue); diff --git a/solr/core/src/java/org/apache/solr/core/MMapDirectoryFactory.java b/solr/core/src/java/org/apache/solr/core/MMapDirectoryFactory.java index 0dd2cdb7bb1..81127b9a6cd 100644 --- a/solr/core/src/java/org/apache/solr/core/MMapDirectoryFactory.java +++ b/solr/core/src/java/org/apache/solr/core/MMapDirectoryFactory.java @@ -20,6 +20,7 @@ package org.apache.solr.core; import org.apache.lucene.store.Directory; import org.apache.lucene.store.LockFactory; // javadocs import org.apache.lucene.store.MMapDirectory; +import org.apache.lucene.store.NoLockFactory; import org.apache.solr.common.params.SolrParams; import org.apache.solr.common.util.NamedList; import org.apache.solr.core.DirectoryFactory.DirContext; @@ -59,7 +60,8 @@ public class MMapDirectoryFactory extends StandardDirectoryFactory { @Override protected Directory create(String path, DirContext dirContext) throws IOException { - MMapDirectory mapDirectory = new MMapDirectory(new File(path).toPath(), null, maxChunk); + // we pass NoLockFactory, because the real lock factory is set later by injectLockFactory: + MMapDirectory mapDirectory = new MMapDirectory(new File(path).toPath(), NoLockFactory.getNoLockFactory(), maxChunk); try { mapDirectory.setUseUnmap(unmapHack); } catch (Exception e) { diff --git a/solr/core/src/java/org/apache/solr/core/NIOFSDirectoryFactory.java b/solr/core/src/java/org/apache/solr/core/NIOFSDirectoryFactory.java index 7c6f5ed523a..53bb33e0eef 100644 --- a/solr/core/src/java/org/apache/solr/core/NIOFSDirectoryFactory.java +++ b/solr/core/src/java/org/apache/solr/core/NIOFSDirectoryFactory.java @@ -21,6 +21,7 @@ import java.io.IOException; import org.apache.lucene.store.Directory; import org.apache.lucene.store.NIOFSDirectory; +import org.apache.lucene.store.NoLockFactory; /** @@ -31,7 +32,8 @@ public class NIOFSDirectoryFactory extends StandardDirectoryFactory { @Override protected Directory create(String path, DirContext dirContext) throws IOException { - return new NIOFSDirectory(new File(path).toPath()); + // we pass NoLockFactory, because the real lock factory is set later by injectLockFactory: + return new NIOFSDirectory(new File(path).toPath(), NoLockFactory.getNoLockFactory()); } @Override diff --git a/solr/core/src/java/org/apache/solr/core/NRTCachingDirectoryFactory.java b/solr/core/src/java/org/apache/solr/core/NRTCachingDirectoryFactory.java index 525c428589d..9119af1b7bd 100644 --- a/solr/core/src/java/org/apache/solr/core/NRTCachingDirectoryFactory.java +++ b/solr/core/src/java/org/apache/solr/core/NRTCachingDirectoryFactory.java @@ -23,6 +23,7 @@ import java.io.IOException; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.NRTCachingDirectory; +import org.apache.lucene.store.NoLockFactory; import org.apache.solr.common.params.SolrParams; import org.apache.solr.common.util.NamedList; @@ -51,7 +52,8 @@ public class NRTCachingDirectoryFactory extends StandardDirectoryFactory { @Override protected Directory create(String path, DirContext dirContext) throws IOException { - return new NRTCachingDirectory(FSDirectory.open(new File(path).toPath()), maxMergeSizeMB, maxCachedMB); + // we pass NoLockFactory, because the real lock factory is set later by injectLockFactory: + return new NRTCachingDirectory(FSDirectory.open(new File(path).toPath(), NoLockFactory.getNoLockFactory()), maxMergeSizeMB, maxCachedMB); } @Override diff --git a/solr/core/src/java/org/apache/solr/core/SimpleFSDirectoryFactory.java b/solr/core/src/java/org/apache/solr/core/SimpleFSDirectoryFactory.java index d6835b80046..6c4f06c806b 100644 --- a/solr/core/src/java/org/apache/solr/core/SimpleFSDirectoryFactory.java +++ b/solr/core/src/java/org/apache/solr/core/SimpleFSDirectoryFactory.java @@ -20,6 +20,7 @@ import java.io.File; import java.io.IOException; import org.apache.lucene.store.Directory; +import org.apache.lucene.store.NoLockFactory; import org.apache.lucene.store.SimpleFSDirectory; @@ -31,7 +32,8 @@ public class SimpleFSDirectoryFactory extends StandardDirectoryFactory { @Override protected Directory create(String path, DirContext dirContext) throws IOException { - return new SimpleFSDirectory(new File(path).toPath()); + // we pass NoLockFactory, because the real lock factory is set later by injectLockFactory: + return new SimpleFSDirectory(new File(path).toPath(), NoLockFactory.getNoLockFactory()); } @Override diff --git a/solr/core/src/java/org/apache/solr/core/StandardDirectoryFactory.java b/solr/core/src/java/org/apache/solr/core/StandardDirectoryFactory.java index 6a13516e038..c247add5645 100644 --- a/solr/core/src/java/org/apache/solr/core/StandardDirectoryFactory.java +++ b/solr/core/src/java/org/apache/solr/core/StandardDirectoryFactory.java @@ -24,6 +24,7 @@ import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.IOContext; import org.apache.lucene.store.NRTCachingDirectory; +import org.apache.lucene.store.NoLockFactory; import org.apache.lucene.store.RateLimitedDirectoryWrapper; import org.apache.solr.core.CachingDirectoryFactory.CacheValue; @@ -39,7 +40,8 @@ public class StandardDirectoryFactory extends CachingDirectoryFactory { @Override protected Directory create(String path, DirContext dirContext) throws IOException { - return FSDirectory.open(new File(path).toPath()); + // we pass NoLockFactory, because the real lock factory is set later by injectLockFactory: + return FSDirectory.open(new File(path).toPath(), NoLockFactory.getNoLockFactory()); } @Override diff --git a/solr/core/src/test/org/apache/solr/core/AlternateDirectoryTest.java b/solr/core/src/test/org/apache/solr/core/AlternateDirectoryTest.java index 69729f74c0f..7803df8eb60 100644 --- a/solr/core/src/test/org/apache/solr/core/AlternateDirectoryTest.java +++ b/solr/core/src/test/org/apache/solr/core/AlternateDirectoryTest.java @@ -22,6 +22,7 @@ import java.io.IOException; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.store.Directory; +import org.apache.lucene.store.NoLockFactory; import org.apache.solr.SolrTestCaseJ4; import org.junit.BeforeClass; import org.junit.Test; @@ -58,7 +59,8 @@ public class AlternateDirectoryTest extends SolrTestCaseJ4 { public Directory create(String path, DirContext dirContext) throws IOException { openCalled = true; - return dir = newFSDirectory(new File(path).toPath()); + // we pass NoLockFactory, because the real lock factory is set later by injectLockFactory: + return dir = newFSDirectory(new File(path).toPath(), NoLockFactory.getNoLockFactory()); } } diff --git a/solr/test-framework/src/java/org/apache/solr/core/MockFSDirectoryFactory.java b/solr/test-framework/src/java/org/apache/solr/core/MockFSDirectoryFactory.java index b6b61e6b16a..4db7dd5cd86 100644 --- a/solr/test-framework/src/java/org/apache/solr/core/MockFSDirectoryFactory.java +++ b/solr/test-framework/src/java/org/apache/solr/core/MockFSDirectoryFactory.java @@ -24,6 +24,7 @@ import java.nio.file.Path; import org.apache.lucene.store.Directory; import org.apache.lucene.store.MockDirectoryWrapper; import org.apache.lucene.store.NRTCachingDirectory; +import org.apache.lucene.store.NoLockFactory; import org.apache.lucene.store.RateLimitedDirectoryWrapper; import org.apache.lucene.store.TrackingDirectoryWrapper; import org.apache.lucene.util.LuceneTestCase; @@ -35,7 +36,8 @@ public class MockFSDirectoryFactory extends StandardDirectoryFactory { @Override public Directory create(String path, DirContext dirContext) throws IOException { - Directory dir = LuceneTestCase.newFSDirectory(new File(path).toPath()); + // we pass NoLockFactory, because the real lock factory is set later by injectLockFactory: + Directory dir = LuceneTestCase.newFSDirectory(new File(path).toPath(), NoLockFactory.getNoLockFactory()); // we can't currently do this check because of how // Solr has to reboot a new Directory sometimes when replicating // or rolling back - the old directory is closed and the following