diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index 4df1d991792..2738d622c4b 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -77,6 +77,8 @@ Bug Fixes * SOLR-14347: Autoscaling placement wrong when concurrent replica placements are calculated. (ab) +* SOLR-14128: Improve distributed locking around managed schema upgrade process. (ab) + Other Changes --------------------- * SOLR-14197: SolrResourceLoader: marked many methods as deprecated, and in some cases rerouted exiting logic to avoid 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 44d4c8e3ce7..9e7edc889ed 100644 --- a/solr/core/src/java/org/apache/solr/schema/ManagedIndexSchemaFactory.java +++ b/solr/core/src/java/org/apache/solr/schema/ManagedIndexSchemaFactory.java @@ -321,30 +321,32 @@ public class ManagedIndexSchemaFactory extends IndexSchemaFactory implements Sol * and no exception will be thrown. */ private void zkUgradeToManagedSchema() { - schema.persistManagedSchemaToZooKeeper(true); // Only create, don't update it if it already exists - - // After successfully persisting the managed schema, rename the non-managed - // schema znode by appending UPGRADED_SCHEMA_EXTENSION to its name. - if (resourceName.equals(managedSchemaResourceName)) { log.info("On upgrading to managed schema, did not rename non-managed schema " + resourceName + " because it's the same as the managed schema's name."); - } else { - // Rename the non-managed schema znode in ZooKeeper - ZkSolrResourceLoader zkLoader = (ZkSolrResourceLoader)loader; - ZkController zkController = zkLoader.getZkController(); - SolrZkClient zkClient = zkController.getZkClient(); - final String nonManagedSchemaPath = zkLoader.getConfigSetZkPath() + "/" + resourceName; - final String lockPath = nonManagedSchemaPath + ".lock"; - boolean locked = false; + return; + } + final ZkSolrResourceLoader zkLoader = (ZkSolrResourceLoader)loader; + final ZkController zkController = zkLoader.getZkController(); + final SolrZkClient zkClient = zkController.getZkClient(); + final String lockPath = zkLoader.getConfigSetZkPath() + "/schemaUpgrade.lock"; + boolean locked = false; + try { + try { + zkClient.makePath(lockPath, null, CreateMode.EPHEMERAL, null, true, true); + locked = true; + } catch (Exception e) { + // some other node already started the upgrade, or an error occurred - bail out + return; + } + schema.persistManagedSchemaToZooKeeper(true); // Only create, don't update it if it already exists + + // After successfully persisting the managed schema, rename the non-managed + // schema znode by appending UPGRADED_SCHEMA_EXTENSION to its name. + + // Rename the non-managed schema znode in ZooKeeper + final String nonManagedSchemaPath = zkLoader.getConfigSetZkPath() + "/" + resourceName; try { - try { - zkClient.makePath(lockPath, null, CreateMode.EPHEMERAL, null, true, true); - locked = true; - } catch (Exception e) { - // some other node already started the upgrade, or an error occurred - bail out - return; - } ZkCmdExecutor zkCmdExecutor = new ZkCmdExecutor(zkController.getClientTimeout()); if (zkController.pathExists(nonManagedSchemaPath)) { // First, copy the non-managed schema znode content to the upgraded schema znode @@ -361,14 +363,14 @@ public class ManagedIndexSchemaFactory extends IndexSchemaFactory implements Sol } } - // Set the resource name to the managed schema so that the CoreAdminHandler returns a findable filename + // Set the resource name to the managed schema so that the CoreAdminHandler returns a findable filename schema.setResourceName(managedSchemaResourceName); log.info("After upgrading to managed schema in ZooKeeper, renamed the non-managed schema " - + nonManagedSchemaPath + " to " + upgradedSchemaPath); + + nonManagedSchemaPath + " to " + upgradedSchemaPath); } else { log.info("After upgrading to managed schema in ZooKeeper, the non-managed schema " - + nonManagedSchemaPath + " no longer exists."); + + nonManagedSchemaPath + " no longer exists."); } } catch (Exception e) { if (e instanceof InterruptedException) { @@ -376,16 +378,16 @@ public class ManagedIndexSchemaFactory extends IndexSchemaFactory implements Sol } final String msg = "Error persisting managed schema resource " + managedSchemaResourceName; log.warn(msg, e); // Log as warning and suppress the exception - } finally { - if (locked) { - // unlock - try { - zkClient.delete(lockPath, -1, true); - } catch (KeeperException.NoNodeException nne) { - // ignore - someone else deleted it - } catch (Exception e) { - log.warn("Unable to delete schema upgrade lock file " + lockPath, e); - } + } + } finally { + if (locked) { + // unlock + try { + zkClient.delete(lockPath, -1, true); + } catch (KeeperException.NoNodeException nne) { + // ignore - someone else deleted it + } catch (Exception e) { + log.warn("Unable to delete schema upgrade lock file " + lockPath, e); } } }