This is an automated email from the ASF dual-hosted git repository. dsmiley pushed a commit to branch branch_10x in repository https://gitbox.apache.org/repos/asf/solr.git
commit dad6ee50e2b5bf3fc77363e9f8a47edd0b43c001 Author: Serhiy Bzhezytskyy <[email protected]> AuthorDate: Wed Sep 9 04:16:52 2026 +0300 SOLR-18364: Remove deprecated SolrCore.isWriterLocked(Directory) (#4784) (cherry picked from commit bc9d070c666be4fc536b10da881664be96b1afb4) --- .../src/java/org/apache/solr/core/SolrCore.java | 56 +++------------------- .../org/apache/solr/update/SolrIndexWriter.java | 12 +++++ 2 files changed, 18 insertions(+), 50 deletions(-) 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 2fae19650a7..fe3bf4c9fe8 100644 --- a/solr/core/src/java/org/apache/solr/core/SolrCore.java +++ b/solr/core/src/java/org/apache/solr/core/SolrCore.java @@ -80,7 +80,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);
