Apache Lucene open-source search software
SOLR-4459: The Replication 'index move' rather than copy optimization doesn't kick in when using NRTCachingDirectory or the rate limiting feature.
Mark Robert Miller3a4103d19b34
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex de7a966f71e..5040b74b530 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -107,7 +107,7 @@ Bug Fixes* SOLR-3926: Solr should support better way of finding active sorts (Eirik Lygre viaErick Erickson)-* SOLR-4342: Fix DataImportHandler stats to be a prper Map (hossman)+* SOLR-4342: Fix DataImportHandler stats to be a proper Map (hossman)* SOLR-3967: langid.enforceSchema option checks source field instead of target field (janhoy)@@ -125,6 +125,10 @@ Bug Fixes* SOLR-4463: Fix SolrCoreState reference counting. (Mark Miller)+* SOLR-4459: The Replication 'index move' rather than copy optimization doesn't+ kick in when using NRTCachingDirectory or the rate limiting feature.+ (Mark Miller)+Optimizations----------------------diff --git a/solr/core/src/java/org/apache/solr/core/DirectoryFactory.java b/solr/core/src/java/org/apache/solr/core/DirectoryFactory.javaindex 20507247bd1..3180ed225e6 100644--- a/solr/core/src/java/org/apache/solr/core/DirectoryFactory.java+++ b/solr/core/src/java/org/apache/solr/core/DirectoryFactory.java@@ -103,6 +103,10 @@ public abstract class DirectoryFactory implements NamedListInitializedPlugin,/*** Override for more efficient moves.*+ * Intended for use with replication - use+ * carefully - some Directory wrappers will+ * cache files for example.+ ** @throws IOException If there is a low-level I/O error.*/public void move(Directory fromDir, Directory toDir, String fileName, IOContext ioContext) throws IOException {diff --git a/solr/core/src/java/org/apache/solr/core/StandardDirectoryFactory.java b/solr/core/src/java/org/apache/solr/core/StandardDirectoryFactory.javaindex fd547a5074b..085cb8e0adc 100644--- a/solr/core/src/java/org/apache/solr/core/StandardDirectoryFactory.java+++ b/solr/core/src/java/org/apache/solr/core/StandardDirectoryFactory.java@@ -23,6 +23,8 @@ import org.apache.commons.io.FileUtils;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.RateLimitedDirectoryWrapper;/*** Directory provider which mimics original Solr@@ -69,15 +71,29 @@ public class StandardDirectoryFactory extends CachingDirectoryFactory {/*** Override for more efficient moves.*+ * Intended for use with replication - use+ * carefully - some Directory wrappers will+ * cache files for example.+ *+ * This implementation works with two wrappers:+ * NRTCachingDirectory and RateLimitedDirectoryWrapper.+ *+ * You should first {@link Directory#sync(java.util.Collection)} any file that will be+ * moved or avoid cached files through settings.+ ** @throws IOException* If there is a low-level I/O error.*/@Overridepublic void move(Directory fromDir, Directory toDir, String fileName, IOContext ioContext)throws IOException {- if (fromDir instanceof FSDirectory && toDir instanceof FSDirectory) {- File dir1 = ((FSDirectory) fromDir).getDirectory();- File dir2 = ((FSDirectory) toDir).getDirectory();++ Directory baseFromDir = getBaseDir(fromDir);+ Directory baseToDir = getBaseDir(fromDir);++ if (baseFromDir instanceof FSDirectory && baseToDir instanceof FSDirectory) {+ File dir1 = ((FSDirectory) baseFromDir).getDirectory();+ File dir2 = ((FSDirectory) baseToDir).getDirectory();File indexFileInTmpDir = new File(dir1, fileName);File indexFileInIndex = new File(dir2, fileName);boolean success = indexFileInTmpDir.renameTo(indexFileInIndex);@@ -89,4 +105,18 @@ public class StandardDirectoryFactory extends CachingDirectoryFactory {super.move(fromDir, toDir, fileName, ioContext);}+ // special hack to work with NRTCachingDirectory and RateLimitedDirectoryWrapper+ private Directory getBaseDir(Directory dir) {+ Directory baseDir;+ if (dir instanceof NRTCachingDirectory) {+ baseDir = ((NRTCachingDirectory)dir).getDelegate();+ } else if (dir instanceof RateLimitedDirectoryWrapper) {+ baseDir = ((RateLimitedDirectoryWrapper)dir).getDelegate();+ } else {+ baseDir = dir;+ }++ return baseDir;+ }+}