praste commented on a change in pull request #238:
URL: https://github.com/apache/solr/pull/238#discussion_r709527443



##########
File path: 
solr/core/src/java/org/apache/solr/handler/admin/HealthCheckHandler.java
##########
@@ -135,6 +153,80 @@ public void handleRequestBody(SolrQueryRequest req, 
SolrQueryResponse rsp) throw
     rsp.add(STATUS, OK);
   }
 
+  private void healthCheckLegacyMode(SolrQueryRequest req, SolrQueryResponse 
rsp) {
+    Integer maxGenerationLag = 
req.getParams().getInt(HealthCheckRequest.PARAM_MAX_GENERATION_LAG);
+    List<String> laggingCoresInfo = new ArrayList<>();
+    boolean allCoresAreInSync = true;
+
+    // check only if max generation lag is specified
+    if(maxGenerationLag != null) {

Review comment:
       👍 

##########
File path: 
solr/core/src/java/org/apache/solr/handler/admin/HealthCheckHandler.java
##########
@@ -135,6 +153,80 @@ public void handleRequestBody(SolrQueryRequest req, 
SolrQueryResponse rsp) throw
     rsp.add(STATUS, OK);
   }
 
+  private void healthCheckLegacyMode(SolrQueryRequest req, SolrQueryResponse 
rsp) {
+    Integer maxGenerationLag = 
req.getParams().getInt(HealthCheckRequest.PARAM_MAX_GENERATION_LAG);
+    List<String> laggingCoresInfo = new ArrayList<>();
+    boolean allCoresAreInSync = true;
+
+    // check only if max generation lag is specified
+    if(maxGenerationLag != null) {
+      for(SolrCore core : coreContainer.getCores()) {
+        ReplicationHandler replicationHandler =
+          (ReplicationHandler) core.getRequestHandler(ReplicationHandler.PATH);
+        // if maxGeneration lag is not specified don't check if follower is in 
sync.
+        if(replicationHandler.isFollower()) {
+          boolean isCoreInSync =
+            generationLagFromLeader(core, replicationHandler, 
maxGenerationLag, laggingCoresInfo);
+
+          allCoresAreInSync &= isCoreInSync;
+        }
+      }
+    }
+
+    if(allCoresAreInSync) {
+      rsp.add("message",
+        String.format(Locale.ROOT, "All the followers are in sync with leader 
(within maxGenerationLag: %d) " +
+          "or all the cores are acting as leader", maxGenerationLag));
+      rsp.add(STATUS, OK);
+    } else {
+      rsp.add("message",
+        String.format(Locale.ROOT,"Cores violating maxGenerationLag:%d.\n%s", 
maxGenerationLag,
+          String.join(",\n", laggingCoresInfo)));
+      rsp.add(STATUS, FAILURE);
+    }
+  }
+
+  private boolean generationLagFromLeader(final SolrCore core, 
ReplicationHandler replicationHandler,
+                                          int maxGenerationLag, List<String> 
laggingCoresInfo) {
+    IndexFetcher indexFetcher = null;
+    try {
+      // may not be the best way to get leader's replicableCommit
+      @SuppressWarnings({"rawtypes"})
+      NamedList follower =
+        
ReplicationHandler.getObjectWithBackwardCompatibility(replicationHandler.getInitArgs(),
 "follower",
+                  "slave");
+      indexFetcher = new IndexFetcher(follower, replicationHandler, core);
+
+      @SuppressWarnings({"rawtypes"})
+      NamedList replicableCommitOnLeader = indexFetcher.getLatestVersion();
+      long leaderGeneration = (Long) replicableCommitOnLeader.get(GENERATION);
+
+      // Get our own commit and generation from the commit
+      IndexCommit commit = core.getDeletionPolicy().getLatestCommit();
+      if(commit != null) {
+        long followerGeneration = commit.getGeneration();
+        long generationDiff = leaderGeneration - followerGeneration;
+
+        // generationDiff should be within the threshold and generationDiff 
shouldn't be negative

Review comment:
       👍 

##########
File path: 
solr/core/src/test/org/apache/solr/handler/TestHealthCheckHandlerLegacyMode.java
##########
@@ -0,0 +1,208 @@
+/*
+ * 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.
+ */
+package org.apache.solr.handler;
+
+import org.apache.lucene.util.LuceneTestCase.Slow;
+import org.apache.solr.SolrTestCaseJ4;
+import org.apache.solr.SolrTestCaseJ4.SuppressSSL;
+import org.apache.solr.client.solrj.embedded.JettySolrRunner;
+import org.apache.solr.client.solrj.impl.HttpSolrClient;
+import org.apache.solr.client.solrj.request.HealthCheckRequest;
+import org.apache.solr.common.params.CommonParams;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.util.TestInjection;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.IOException;
+
+import static org.apache.solr.handler.ReplicationTestHelper.*;
+
+
+/**
+ * Test for HealthCheckHandler in legacy mode
+ *
+ *
+ */
+@Slow
+@SuppressSSL     // Currently unknown why SSL does not work with this test
+public class TestHealthCheckHandlerLegacyMode extends SolrTestCaseJ4 {
+    HttpSolrClient leaderClientHealthCheck, followerClientHealthCheck;
+
+    JettySolrRunner leaderJetty, followerJetty;
+    HttpSolrClient leaderClient, followerClient;
+    ReplicationTestHelper.SolrInstance leader = null, follower = null;
+
+    private static String context = "/solr";
+
+    static int nDocs = 500;
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+
+        systemSetPropertySolrDisableUrlAllowList("true");
+
+        leader = new 
ReplicationTestHelper.SolrInstance(createTempDir("solr-instance").toFile(), 
"leader", null);
+        leader.setUp();
+        leaderJetty = ReplicationTestHelper.createAndStartJetty(leader);
+        leaderClient = 
ReplicationTestHelper.createNewSolrClient(buildUrl(leaderJetty.getLocalPort(), 
context) + "/" + DEFAULT_TEST_CORENAME);
+        leaderClientHealthCheck = 
ReplicationTestHelper.createNewSolrClient(buildUrl(leaderJetty.getLocalPort(), 
context));
+
+        follower = new SolrInstance(createTempDir("solr-instance").toFile(), 
"follower", leaderJetty.getLocalPort());
+        follower.setUp();
+        followerJetty = createAndStartJetty(follower);
+        followerClient = 
ReplicationTestHelper.createNewSolrClient(buildUrl(followerJetty.getLocalPort(),
 context) + "/" + DEFAULT_TEST_CORENAME);
+        followerClientHealthCheck = 
ReplicationTestHelper.createNewSolrClient(buildUrl(followerJetty.getLocalPort(),
 context));
+
+        System.setProperty("solr.indexfetcher.sotimeout2", "45000");
+    }
+
+    public void clearIndexWithReplication() throws Exception {
+        if (numFound(ReplicationTestHelper.query("*:*", leaderClient)) != 0) {
+            leaderClient.deleteByQuery("*:*");
+            leaderClient.commit();
+            // wait for replication to sync & verify
+            assertEquals(0, numFound(rQuery(0, "*:*", followerClient)));
+        }
+    }
+
+    @Override
+    @After
+    public void tearDown() throws Exception {
+        super.tearDown();
+        if (null != leaderJetty) {
+            leaderJetty.stop();
+            leaderJetty = null;
+        }
+        if (null != followerJetty) {
+            followerJetty.stop();
+            followerJetty = null;
+        }
+        if (null != leaderClient) {
+            leaderClient.close();
+            leaderClient = null;
+        }
+        if (null != followerClient) {
+            followerClient.close();
+            followerClient = null;
+        }
+        if (null != leaderClientHealthCheck) {
+            leaderClientHealthCheck.close();
+            leaderClientHealthCheck = null;
+        }
+
+        if (null != followerClientHealthCheck) {
+            followerClientHealthCheck.close();
+            followerClientHealthCheck = null;
+        }
+        System.clearProperty("solr.indexfetcher.sotimeout");
+    }
+
+
+    @Test
+    // keep this
+    public void doTestHealthCheckWithReplication() throws Exception {
+
+        TestInjection.delayBeforeFollowerCommitRefresh = random().nextInt(10);
+
+        // stop replication so that the follower doesn't pull the index
+        invokeReplicationCommand(buildUrl(followerJetty.getLocalPort(), 
context) + "/" + DEFAULT_TEST_CORENAME, "disablepoll");
+
+        nDocs--;

Review comment:
       👍 

##########
File path: 
solr/core/src/test/org/apache/solr/handler/TestHealthCheckHandlerLegacyMode.java
##########
@@ -0,0 +1,208 @@
+/*
+ * 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.
+ */
+package org.apache.solr.handler;
+
+import org.apache.lucene.util.LuceneTestCase.Slow;
+import org.apache.solr.SolrTestCaseJ4;
+import org.apache.solr.SolrTestCaseJ4.SuppressSSL;
+import org.apache.solr.client.solrj.embedded.JettySolrRunner;
+import org.apache.solr.client.solrj.impl.HttpSolrClient;
+import org.apache.solr.client.solrj.request.HealthCheckRequest;
+import org.apache.solr.common.params.CommonParams;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.util.TestInjection;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.IOException;
+
+import static org.apache.solr.handler.ReplicationTestHelper.*;
+
+
+/**
+ * Test for HealthCheckHandler in legacy mode
+ *
+ *
+ */
+@Slow
+@SuppressSSL     // Currently unknown why SSL does not work with this test
+public class TestHealthCheckHandlerLegacyMode extends SolrTestCaseJ4 {
+    HttpSolrClient leaderClientHealthCheck, followerClientHealthCheck;
+
+    JettySolrRunner leaderJetty, followerJetty;
+    HttpSolrClient leaderClient, followerClient;
+    ReplicationTestHelper.SolrInstance leader = null, follower = null;
+
+    private static String context = "/solr";
+
+    static int nDocs = 500;
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+
+        systemSetPropertySolrDisableUrlAllowList("true");

Review comment:
       ok




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org
For additional commands, e-mail: issues-h...@solr.apache.org

Reply via email to