Apache Solr open-source search software
SOLR-15732: addressing test failures
Noble Pauldcfdc68e5691
diff --git a/solr/solrj-zookeeper/src/java/org/apache/solr/common/cloud/SolrZkClient.java b/solr/solrj-zookeeper/src/java/org/apache/solr/common/cloud/SolrZkClient.javaindex c85719116cd..9070406d84e 100644--- a/solr/solrj-zookeeper/src/java/org/apache/solr/common/cloud/SolrZkClient.java+++ b/solr/solrj-zookeeper/src/java/org/apache/solr/common/cloud/SolrZkClient.java@@ -440,6 +440,12 @@ public class SolrZkClient implements Closeable {return result;}+ public NodeData getNode(final String path, Watcher watcher, boolean retryOnConnLoss)+ throws KeeperException, InterruptedException {+ Stat stat = new Stat();+ return new NodeData(stat, getData(path, watcher, stat, retryOnConnLoss));+ }+/** Returns node's state */public Stat setData(final String path, final byte data[], final int version, boolean retryOnConnLoss)@@ -1090,4 +1096,15 @@ public class SolrZkClient implements Closeable {});}}++ public static class NodeData {++ public final Stat stat;+ public final byte[] data;++ public NodeData(Stat stat, byte[] data) {+ this.stat = stat;+ this.data = data;+ }+ }}diff --git a/solr/solrj-zookeeper/src/java/org/apache/solr/common/cloud/ZkStateReader.java b/solr/solrj-zookeeper/src/java/org/apache/solr/common/cloud/ZkStateReader.javaindex 9011f750876..37e6d981ff3 100644--- a/solr/solrj-zookeeper/src/java/org/apache/solr/common/cloud/ZkStateReader.java+++ b/solr/solrj-zookeeper/src/java/org/apache/solr/common/cloud/ZkStateReader.java@@ -2252,7 +2252,7 @@ public class ZkStateReader implements SolrCloseable {try {final Stat stat =getZkClient().setData(ALIASES, modAliasesJson, curAliases.getZNodeVersion(), true);- setIfNewer(Aliases.fromJSON(modAliasesJson, stat.getVersion()));+ setIfNewer(new SolrZkClient.NodeData(stat, modAliasesJson));return;} catch (KeeperException.BadVersionException e) {log.debug("{}", e, e);@@ -2292,16 +2292,9 @@ public class ZkStateReader implements SolrCloseable {if (log.isDebugEnabled()) {log.debug("Checking ZK for most up to date Aliases {}", ALIASES);}- Stat stat = zkClient.getZooKeeper().exists(ALIASES, null);- if (stat == null || stat.getVersion() <= aliases.getZNodeVersion()) {- return false;- } else {- stat = new Stat();- }// Call sync() first to ensure the subsequent read (getData) is up to date.zkClient.getZooKeeper().sync(ALIASES, null, null);- final byte[] data = zkClient.getData(ALIASES, null, stat, true);- return setIfNewer(Aliases.fromJSON(data, stat.getVersion()));+ return setIfNewer(zkClient.getNode(ALIASES, null, true));}// ZK Watcher interface@@ -2315,11 +2308,7 @@ public class ZkStateReader implements SolrCloseable {log.debug("Aliases: updating");// re-register the watch- Stat stat = new Stat();- final byte[] data = zkClient.getData(ALIASES, this, stat, true);- // note: it'd be nice to avoid possibly needlessly parsing if we don't update aliases but- // not a big deal- setIfNewer(Aliases.fromJSON(data, stat.getVersion()));+ setIfNewer(zkClient.getNode(ALIASES, this, true));} catch (NoNodeException e) {// /aliases.json will not always exist} catch (KeeperException.ConnectionLossException@@ -2340,22 +2329,29 @@ public class ZkStateReader implements SolrCloseable {* Update the internal aliases reference with a new one, provided that its ZK version has* increased.*- * @param newAliases the potentially newer version of Aliases+ * @param n the node data* @return true if aliases have been updated to a new version, false otherwise*/- private boolean setIfNewer(Aliases newAliases) {- assert newAliases.getZNodeVersion() >= 0;+ private boolean setIfNewer(SolrZkClient.NodeData n) {+ assert n.stat.getVersion() >= 0;synchronized (this) {- int cmp = Integer.compare(aliases.getZNodeVersion(), newAliases.getZNodeVersion());+ int cmp = Integer.compare(aliases.getZNodeVersion(), n.stat.getVersion());if (cmp < 0) {- log.debug("Aliases: cmp={}, new definition is: {}", cmp, newAliases);- aliases = newAliases;+ if (log.isDebugEnabled()) {+ log.debug(+ "Aliases: cmp={}, new definition is: {}",+ cmp,+ Aliases.fromJSON(n.data, n.stat.getVersion()));+ }+ aliases = Aliases.fromJSON(n.data, n.stat.getVersion());this.notifyAll();return true;} else {- log.debug("Aliases: cmp={}, not overwriting ZK version.", cmp);- assert cmp != 0 || Arrays.equals(aliases.toJSON(), newAliases.toJSON())- : aliases + " != " + newAliases;+ if (log.isDebugEnabled()) {+ log.debug("Aliases: cmp={}, not overwriting ZK version.", cmp);+ }+ assert cmp != 0 || Arrays.equals(aliases.toJSON(), n.data)+ : aliases + " != " + Aliases.fromJSON(n.data, n.stat.getVersion());return false;}}