Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-18364: Remove deprecated SolrCore.isWriterLocked(Directory) (#4784)

Serhiy Bzhezytskyybc9d070c666b
diff --git a/solr/core/src/java/org/apache/solr/core/SolrCore.java b/solr/core/src/java/org/apache/solr/core/SolrCore.java
index 86c67c4121c..16153a3b15e 100644
--- a/solr/core/src/java/org/apache/solr/core/SolrCore.java
+++ b/solr/core/src/java/org/apache/solr/core/SolrCore.java
@@ -81,7 +81,6 @@ import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
-import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.ResourceLoader;
import org.apache.solr.client.solrj.response.JavaBinResponseParser;
import org.apache.solr.cloud.CloudDescriptor;
@@ -854,59 +853,16 @@ public class SolrCore implements SolrInfoBean, Closeable {
this.indexReaderFactory = indexReaderFactory;
}
- // protect via synchronized(SolrCore.class)
- private static Set<String> dirs = new HashSet<>();
-
- /**
- * Returns <code>true</code> iff the index in the named directory is currently locked.
- *
- * @param directory the directory to check for a lock
- * @throws IOException if there is a low-level IO error
- * @deprecated Use of this method can only lead to race conditions. Try to actually obtain a lock
- * instead.
- */
- @Deprecated(since = "7.0")
- private static boolean isWriterLocked(Directory directory) throws IOException {
- try {
- directory.obtainLock(IndexWriter.WRITE_LOCK_NAME).close();
- return false;
- } catch (LockObtainFailedException failed) {
- return true;
- }
- }
-
- void initIndex(boolean passOnPreviousState, boolean reload) throws IOException {
+ /** Also fails fast (LockObtainFailedException) if an existing index directory is locked. */
+ void initIndex(boolean reload) throws IOException {
String indexDir = getNewIndexDir();
boolean indexExists = getDirectoryFactory().exists(indexDir);
- boolean firstTime;
- synchronized (SolrCore.class) {
- firstTime = dirs.add(getDirectoryFactory().normalize(indexDir));
- }
initIndexReaderFactory();
- if (indexExists && firstTime && !passOnPreviousState) {
- final String lockType = getSolrConfig().indexConfig.lockType;
- Directory dir = directoryFactory.get(indexDir, DirContext.DEFAULT, lockType);
- try {
- if (isWriterLocked(dir)) {
- log.error(
- "Solr index directory '{}' is locked (lockType={}). Throwing exception.",
- indexDir,
- lockType);
- throw new LockObtainFailedException(
- "Index dir '"
- + indexDir
- + "' of core '"
- + name
- + "' is already locked. "
- + "The most likely cause is another Solr server (or another solr core in this server) "
- + "also configured to use this directory; other possible causes may be specific to lockType: "
- + lockType);
- }
- } finally {
- directoryFactory.release(dir);
- }
+ if (indexExists) {
+ // Fails fast on a lock conflict (LUCENE-6507/6508); solrCoreState caches the writer.
+ solrCoreState.getIndexWriter(this, false).decref();
}
// Create the index if it doesn't exist.
@@ -1137,7 +1093,7 @@ public class SolrCore implements SolrInfoBean, Closeable {
this.solrDelPolicy = initDeletionPolicy(delPolicy);
this.codec = initCodec(solrConfig, this.schema);
- initIndex(prev != null, reload);
+ initIndex(reload);
initWriters();
qParserPlugins.init(QParserPlugin.standardPlugins, this);
diff --git a/solr/core/src/java/org/apache/solr/update/SolrIndexWriter.java b/solr/core/src/java/org/apache/solr/update/SolrIndexWriter.java
index 8e3fef1cf75..200c85728c0 100644
--- a/solr/core/src/java/org/apache/solr/update/SolrIndexWriter.java
+++ b/solr/core/src/java/org/apache/solr/update/SolrIndexWriter.java
@@ -35,6 +35,7 @@ import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.MergePolicy;
import org.apache.lucene.index.SegmentCommitInfo;
import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.InfoStream;
import org.apache.solr.common.util.IOUtils;
import org.apache.solr.common.util.SuppressForbidden;
@@ -117,6 +118,17 @@ public class SolrIndexWriter extends IndexWriter {
w = new SolrIndexWriter(core, name, path, d, create, schema, config, delPolicy, codec);
w.setDirectoryFactory(directoryFactory);
return w;
+ } catch (LockObtainFailedException e) {
+ throw new LockObtainFailedException(
+ "Index dir '"
+ + path
+ + "' of core '"
+ + core.getName()
+ + "' is already locked. "
+ + "The most likely cause is another Solr server (or another solr core in this server) "
+ + "also configured to use this directory; other possible causes may be specific to lockType: "
+ + config.lockType,
+ e);
} finally {
if (null == w && null != d) {
directoryFactory.doneWithDirectory(d);