You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ma...@apache.org on 2021/01/12 22:37:59 UTC

[lucene-solr] 01/03: @1251 Cleanup some of the managed schema dangling threads.

This is an automated email from the ASF dual-hosted git repository.

markrmiller pushed a commit to branch reference_impl_dev
in repository https://gitbox.apache.org/repos/asf/lucene-solr.git

commit 388f7169fdb8b647787f11c7ceb29c080e549b66
Author: markrmiller@gmail.com <ma...@gmail.com>
AuthorDate: Sat Jan 2 21:28:01 2021 -0600

    @1251 Cleanup some of the managed schema dangling threads.
---
 .../solr/schema/ManagedIndexSchemaFactory.java     |  4 +--
 .../apache/solr/schema/ZkIndexSchemaReader.java    | 31 +++++++----------
 .../AddSchemaFieldsUpdateProcessorFactory.java     | 40 ++++++++++------------
 3 files changed, 32 insertions(+), 43 deletions(-)

diff --git a/solr/core/src/java/org/apache/solr/schema/ManagedIndexSchemaFactory.java b/solr/core/src/java/org/apache/solr/schema/ManagedIndexSchemaFactory.java
index fadd6e9..7fa89fb 100644
--- a/solr/core/src/java/org/apache/solr/schema/ManagedIndexSchemaFactory.java
+++ b/solr/core/src/java/org/apache/solr/schema/ManagedIndexSchemaFactory.java
@@ -61,6 +61,8 @@ public class ManagedIndexSchemaFactory extends IndexSchemaFactory implements Sol
 
   private volatile SolrCore core;
 
+  private ReentrantLock schemaUpdateLock = new ReentrantLock();
+
   public String getManagedSchemaResourceName() { return managedSchemaResourceName; }
   private volatile SolrConfig config;
   private volatile ResourceLoader loader;
@@ -397,8 +399,6 @@ public class ManagedIndexSchemaFactory extends IndexSchemaFactory implements Sol
       }
     }
   }
-
-  private ReentrantLock schemaUpdateLock = new ReentrantLock();
   public ReentrantLock getSchemaUpdateLock() { return schemaUpdateLock; }
 
   @Override
diff --git a/solr/core/src/java/org/apache/solr/schema/ZkIndexSchemaReader.java b/solr/core/src/java/org/apache/solr/schema/ZkIndexSchemaReader.java
index 7bb6c18..261f3df 100644
--- a/solr/core/src/java/org/apache/solr/schema/ZkIndexSchemaReader.java
+++ b/solr/core/src/java/org/apache/solr/schema/ZkIndexSchemaReader.java
@@ -42,7 +42,7 @@ import java.util.concurrent.locks.ReentrantLock;
 /** Keeps a ManagedIndexSchema up-to-date when changes are made to the serialized managed schema in ZooKeeper */
 public class ZkIndexSchemaReader implements OnReconnect {
   private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
-  private volatile ManagedIndexSchemaFactory managedIndexSchemaFactory;
+  private final ManagedIndexSchemaFactory managedIndexSchemaFactory;
   private volatile SolrZkClient zkClient;
   private final String managedSchemaPath;
   private final String uniqueCoreId; // used in equals impl to uniquely identify the core that we're dependent on
@@ -148,35 +148,32 @@ public class ZkIndexSchemaReader implements OnReconnect {
 
   // package visibility for test purposes
   public IndexSchema updateSchema(Watcher watcher, int version) throws KeeperException, InterruptedException {
-    ManagedIndexSchema newSchema = null;
-    ReentrantLock lock = null;
+    ManagedIndexSchema newSchema;
+    ReentrantLock  lock = getSchemaUpdateLock();
+    lock.lock();
     try {
-      lock = getSchemaUpdateLock();
-      lock.lock();
       Stat stat = new Stat();
-
+      createSchemaWatcher();
       Stat exists = zkClient.exists(managedSchemaPath, schemaWatcher, true);
       if (exists == null) {
         log.info("{} does not exist yet, watching ...}", managedSchemaPath);
         return null;
-      } else {
-        createSchemaWatcher();
       }
 
       int existsVersion = exists.getVersion();
 
       int v = managedIndexSchemaFactory.getSchema().getSchemaZkVersion();
 
+      if (version > -1) {
+        v = version;
+      }
+
       log.info("Retrieved schema version {} from Zookeeper, existing={}", existsVersion, v);
 
       if (v >= existsVersion) {
         log.info("Old schema version {} is >= found version {}", v, existsVersion);
-        exists = zkClient.exists(managedSchemaPath, this.schemaWatcher, true);
-        if (v >= exists.getVersion()) {
-          return null;
-        } else {
-          createSchemaWatcher();
-        }
+
+        return null;
       }
 
       long start = System.nanoTime();
@@ -190,13 +187,11 @@ public class ZkIndexSchemaReader implements OnReconnect {
 
       long stop = System.nanoTime();
       log.info("Finished refreshing schema in {} ms", TimeUnit.MILLISECONDS.convert(stop - start, TimeUnit.NANOSECONDS));
-    } catch (NullPointerException e) {
-      throw new AlreadyClosedException();
     } catch (Exception e) {
       log.error("Exception updating schema", e);
       return null;
     } finally {
-      if (lock != null) lock.unlock();
+      if (lock != null && lock.isHeldByCurrentThread()) lock.unlock();
     }
     return newSchema;
   }
@@ -208,8 +203,6 @@ public class ZkIndexSchemaReader implements OnReconnect {
   @Override
   public void command() {
     try {
-      // setup a new watcher to get notified when the managed schema changes
-      createSchemaWatcher();
       // force update now as the schema may have changed while our zk session was expired
       updateSchema(schemaWatcher, -1);
     } catch (Exception exc) {
diff --git a/solr/core/src/java/org/apache/solr/update/processor/AddSchemaFieldsUpdateProcessorFactory.java b/solr/core/src/java/org/apache/solr/update/processor/AddSchemaFieldsUpdateProcessorFactory.java
index f3a7d45..a6152f3 100644
--- a/solr/core/src/java/org/apache/solr/update/processor/AddSchemaFieldsUpdateProcessorFactory.java
+++ b/solr/core/src/java/org/apache/solr/update/processor/AddSchemaFieldsUpdateProcessorFactory.java
@@ -24,6 +24,7 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.locks.ReentrantLock;
 import java.util.stream.Collectors;
 
 import org.apache.solr.common.SolrException;
@@ -424,7 +425,7 @@ public class AddSchemaFieldsUpdateProcessorFactory extends UpdateRequestProcesso
           }
           if (newFields.isEmpty() && newCopyFields.isEmpty()) {
             // nothing to do - no fields will be added - exit from the retry loop
-            log.debug("No fields or copyFields to add to the schema.");
+            if (log.isDebugEnabled()) log.debug("No fields or copyFields to add to the schema.");
             break;
           } else if (isImmutableConfigSet(core)) {
             final String message = "This ConfigSet is immutable.";
@@ -469,12 +470,18 @@ public class AddSchemaFieldsUpdateProcessorFactory extends UpdateRequestProcesso
             }
           }
           if (null != newSchema) {
-            boolean success = ((ManagedIndexSchema) newSchema).persistManagedSchema(false);
-            if (log.isDebugEnabled()) log.debug("Successfully added field(s) and copyField(s) to the schema.");
-            if (success) {
-              core.setLatestSchema(newSchema);
-              cmd.getReq().updateSchemaToLatest();
-              break; // success - exit from the retry loop
+            ReentrantLock lock = ((ManagedIndexSchema) newSchema).getManagedIndexSchemaFactory().getSchemaUpdateLock();
+            lock.lock();
+            try {
+              boolean success = ((ManagedIndexSchema) newSchema).persistManagedSchema(false);
+              if (log.isDebugEnabled()) log.debug("Successfully added field(s) and copyField(s) to the schema.");
+              if (success) {
+                core.setLatestSchema(newSchema);
+                cmd.getReq().updateSchemaToLatest();
+                break; // success - exit from the retry loop
+              }
+            } finally {
+              lock.unlock();
             }
           } else {
             throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Failed to add fields and/or copyFields.");
@@ -485,25 +492,14 @@ public class AddSchemaFieldsUpdateProcessorFactory extends UpdateRequestProcesso
         } catch (ManagedIndexSchema.SchemaChangedInZkException e) {
           log.info("Schema changed while processing request ...");
           try {
-             ((ManagedIndexSchema) newSchema).getManagedIndexSchemaFactory().getZkIndexSchemaReader().updateSchema(null, -1);
+            ((ManagedIndexSchema) newSchema).getManagedIndexSchemaFactory().getZkIndexSchemaReader().updateSchema(null, -1);
+            cmd.getReq().updateSchemaToLatest();
           } catch (KeeperException.SessionExpiredException keeperException) {
             throw new SolrException(SERVER_ERROR, keeperException);
           } catch (Exception e1) {
-            log.error("", e1);
+            log.error("Exception updating schema", e1);
+            throw new SolrException(SERVER_ERROR, e1);
           }
-//          if (newSchema != null) {
-//
-//            cmd.getReq().updateSchemaToLatest(newSchema);
-//            cmd.getReq().getCore().setLatestSchema(newSchema);
-//            newSchema.postReadInform();
-//            newSchema.refreshAnalyzers();
-//            break;
-//          } else {
-            cmd.getReq().updateSchemaToLatest();
-//          cmd.getReq().getSchema().refreshAnalyzers();
-//            cmd.getReq().getSchema().postReadInform();
-
-        //  }
         }
       }