Apache Solr open-source search software
SOLR-7989: After a new leader is elected it, it should ensure it's state is ACTIVE if it has already registered with ZK.
Mark Robert Miller15ac4b0e8304
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex bb4f979d6d6..19830ac5ada 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -384,6 +384,10 @@ Bug Fixes* SOLR-8262: Comment out /stream handler from sample solrconfig.xml's for security reasons(Joel Bernstein)+* SOLR-7989: After a new leader is elected it should change it's state to ACTIVE even+ if the last published state is something else if it has already registered with ZK.+ (Ishan Chattopadhyaya, Mark Miller via noble)+Optimizations----------------------diff --git a/solr/core/src/java/org/apache/solr/cloud/CloudDescriptor.java b/solr/core/src/java/org/apache/solr/cloud/CloudDescriptor.javaindex 9ed97024ad9..31e2b8f4e65 100644--- a/solr/core/src/java/org/apache/solr/cloud/CloudDescriptor.java+++ b/solr/core/src/java/org/apache/solr/cloud/CloudDescriptor.java@@ -44,6 +44,10 @@ public class CloudDescriptor {volatile String shardParent = null;private volatile boolean isLeader = false;++ // set to true once a core has registered in zk+ // set to false on detecting a session expiration+ private volatile boolean hasRegistered = false;volatile Replica.State lastPublished = Replica.State.ACTIVE;public static final String NUM_SHARDS = "numShards";@@ -77,6 +81,14 @@ public class CloudDescriptor {public void setLeader(boolean isLeader) {this.isLeader = isLeader;}++ public boolean hasRegistered() {+ return hasRegistered;+ }++ public void setHasRegistered(boolean hasRegistered) {+ this.hasRegistered = hasRegistered;+ }public void setShardId(String shardId) {this.shardId = shardId;diff --git a/solr/core/src/java/org/apache/solr/cloud/ElectionContext.java b/solr/core/src/java/org/apache/solr/cloud/ElectionContext.javaindex 2e8c3790f54..941353ea9e3 100644--- a/solr/core/src/java/org/apache/solr/cloud/ElectionContext.java+++ b/solr/core/src/java/org/apache/solr/cloud/ElectionContext.java@@ -1,5 +1,22 @@package org.apache.solr.cloud;+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+import java.io.Closeable;import java.io.IOException;import java.util.ArrayList;@@ -11,6 +28,7 @@ import org.apache.lucene.search.MatchAllDocsQuery;import org.apache.solr.cloud.overseer.OverseerAction;import org.apache.solr.common.SolrException;import org.apache.solr.common.SolrException.ErrorCode;+import org.apache.solr.common.cloud.ClusterState;import org.apache.solr.common.cloud.Replica;import org.apache.solr.common.cloud.Slice;import org.apache.solr.common.cloud.SolrZkClient;@@ -39,23 +57,6 @@ import org.apache.zookeeper.data.Stat;import org.slf4j.Logger;import org.slf4j.LoggerFactory;-/*- * Licensed to the Apache Software Foundation (ASF) under one or more- * contributor license agreements. See the NOTICE file distributed with- * this work for additional information regarding copyright ownership.- * The ASF licenses this file to You under the Apache License, Version 2.0- * (the "License"); you may not use this file except in compliance with- * the License. You may obtain a copy of the License at- *- * http://www.apache.org/licenses/LICENSE-2.0- *- * Unless required by applicable law or agreed to in writing, software- * distributed under the License is distributed on an "AS IS" BASIS,- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.- * See the License for the specific language governing permissions and- * limitations under the License.- */-public abstract class ElectionContext implements Closeable {static Logger log = LoggerFactory.getLogger(ElectionContext.class);final String electionPath;@@ -404,6 +405,7 @@ final class ShardLeaderElectionContext extends ShardLeaderElectionContextBase {super.runLeaderProcess(weAreReplacement, 0);try (SolrCore core = cc.getCore(coreName)) {core.getCoreDescriptor().getCloudDescriptor().setLeader(true);+ publishActiveIfRegisteredAndNotActive(core);}log.info("I am the new leader: " + ZkCoreNodeProps.getCoreUrl(leaderProps) + " " + shardId);@@ -442,6 +444,21 @@ final class ShardLeaderElectionContext extends ShardLeaderElectionContextBase {}}+ public void publishActiveIfRegisteredAndNotActive(SolrCore core) throws KeeperException, InterruptedException {+ if (core.getCoreDescriptor().getCloudDescriptor().hasRegistered()) {+ ZkStateReader zkStateReader = zkController.getZkStateReader();+ zkStateReader.updateClusterState();+ ClusterState clusterState = zkStateReader.getClusterState();+ Replica rep = (clusterState == null) ? null+ : clusterState.getReplica(collection, leaderProps.getStr(ZkStateReader.CORE_NODE_NAME_PROP));+ if (rep != null && rep.getState() != Replica.State.ACTIVE+ && rep.getState() != Replica.State.RECOVERING) {+ log.info("We have become the leader after core registration but are not in an ACTIVE state - publishing ACTIVE");+ zkController.publish(core.getCoreDescriptor(), Replica.State.ACTIVE);+ }+ }+ }+public void checkLIR(String coreName, boolean allReplicasInLine)throws InterruptedException, KeeperException, IOException {if (allReplicasInLine) {diff --git a/solr/core/src/java/org/apache/solr/cloud/ZkController.java b/solr/core/src/java/org/apache/solr/cloud/ZkController.javaindex 178fe02da6b..c2b5fe28c7b 100644--- a/solr/core/src/java/org/apache/solr/cloud/ZkController.java+++ b/solr/core/src/java/org/apache/solr/cloud/ZkController.java@@ -519,6 +519,7 @@ public final class ZkController {if (descriptors != null) {for (CoreDescriptor descriptor : descriptors) {descriptor.getCloudDescriptor().setLeader(false);+ descriptor.getCloudDescriptor().setHasRegistered(false);}}}@@ -973,6 +974,8 @@ public final class ZkController {if (!didRecovery) {publish(desc, Replica.State.ACTIVE);}++ core.getCoreDescriptor().getCloudDescriptor().setHasRegistered(true);}// make sure we have an update cluster state right awaydiff --git a/solr/core/src/test/org/apache/solr/cloud/ForceLeaderTest.java b/solr/core/src/test/org/apache/solr/cloud/ForceLeaderTest.javaindex 9321e9a480a..e2c8ee2e1a4 100644--- a/solr/core/src/test/org/apache/solr/cloud/ForceLeaderTest.java+++ b/solr/core/src/test/org/apache/solr/cloud/ForceLeaderTest.java@@ -1,11 +1,26 @@package org.apache.solr.cloud;+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+import java.io.IOException;import java.util.Collections;import java.util.List;-import org.apache.lucene.util.LuceneTestCase.AwaitsFix;-import org.apache.solr.client.solrj.SolrClient;import org.apache.solr.client.solrj.SolrRequest;import org.apache.solr.client.solrj.SolrRequest.METHOD;@@ -37,24 +52,6 @@ import org.slf4j.LoggerFactory;import static org.apache.solr.common.cloud.ZkStateReader.CORE_NAME_PROP;-/*- * Licensed to the Apache Software Foundation (ASF) under one or more- * contributor license agreements. See the NOTICE file distributed with- * this work for additional information regarding copyright ownership.- * The ASF licenses this file to You under the Apache License, Version 2.0- * (the "License"); you may not use this file except in compliance with- * the License. You may obtain a copy of the License at- *- * http://www.apache.org/licenses/LICENSE-2.0- *- * Unless required by applicable law or agreed to in writing, software- * distributed under the License is distributed on an "AS IS" BASIS,- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.- * See the License for the specific language governing permissions and- * limitations under the License.- */--@AwaitsFix(bugUrl="https://issues.apache.org/jira/browse/SOLR-7989")public class ForceLeaderTest extends HttpPartitionTest {protected static final transient Logger log =LoggerFactory.getLogger(ForceLeaderTest.class);