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



##########
File path: solr/core/src/test/org/apache/solr/handler/ReplicationTestHelper.java
##########
@@ -0,0 +1,317 @@
+package org.apache.solr.handler;
+
+import org.apache.lucene.util.LuceneTestCase;
+import org.apache.solr.SolrTestCaseJ4;
+import org.apache.solr.client.solrj.SolrClient;
+import org.apache.solr.client.solrj.SolrServerException;
+import org.apache.solr.client.solrj.embedded.JettyConfig;
+import org.apache.solr.client.solrj.embedded.JettySolrRunner;
+import org.apache.solr.client.solrj.impl.HttpSolrClient;
+import org.apache.solr.client.solrj.request.QueryRequest;
+import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.common.SolrDocumentList;
+import org.apache.solr.common.SolrInputDocument;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.util.FileUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.*;
+import java.lang.invoke.MethodHandles;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Properties;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+public final class ReplicationTestHelper {
+
+    private static final Logger log = 
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+    public static final String CONF_DIR = "solr"
+            + File.separator + "collection1" + File.separator + "conf"
+            + File.separator;
+
+
+    public static JettySolrRunner createAndStartJetty(SolrInstance instance) 
throws Exception {
+        FileUtils.copyFile(new File(SolrTestCaseJ4.TEST_HOME(), "solr.xml"), 
new File(instance.getHomeDir(), "solr.xml"));
+        Properties nodeProperties = new Properties();
+        nodeProperties.setProperty("solr.data.dir", instance.getDataDir());
+        JettyConfig jettyConfig = 
JettyConfig.builder().setContext("/solr").setPort(0).build();
+        JettySolrRunner jetty = new JettySolrRunner(instance.getHomeDir(), 
nodeProperties, jettyConfig);
+        jetty.start();
+        return jetty;
+    }
+
+    public static HttpSolrClient createNewSolrClient(String baseUrl) {
+        try {
+            // setup the client...
+            HttpSolrClient client = SolrTestCaseJ4.getHttpSolrClient(baseUrl, 
15000, 90000);
+            return client;
+        }
+        catch (Exception ex) {
+            throw new RuntimeException(ex);
+        }
+    }
+
+    public static int index(SolrClient s, Object... fields) throws Exception {
+        SolrInputDocument doc = new SolrInputDocument();
+        for (int i = 0; i < fields.length; i += 2) {
+            doc.addField((String) (fields[i]), fields[i + 1]);
+        }
+        return s.add(doc).getStatus();
+    }
+
+    /**
+     * character copy of file using UTF-8. If port is non-null, will be 
substituted any time "TEST_PORT" is found.
+     */
+    private static void copyFile(File src, File dst, Integer port, boolean 
internalCompression) throws IOException {
+        try (BufferedReader in = new BufferedReader(new InputStreamReader(new 
FileInputStream(src), StandardCharsets.UTF_8));
+             Writer out = new OutputStreamWriter(new FileOutputStream(dst), 
StandardCharsets.UTF_8)) {
+
+            for (String line = in.readLine(); null != line; line = 
in.readLine()) {
+                if (null != port) {
+                    line = line.replace("TEST_PORT", port.toString());
+                }
+                line = line.replace("COMPRESSION", internalCompression ? 
"internal" : "false");
+                out.write(line);
+            }
+        }
+    }
+
+    public static void assertVersions(SolrClient client1, SolrClient client2) 
throws Exception {
+        NamedList<Object> details = getDetails(client1);
+        @SuppressWarnings({"unchecked"})
+        ArrayList<NamedList<Object>> commits = (ArrayList<NamedList<Object>>) 
details.get("commits");
+        Long maxVersionClient1 = getVersion(client1);
+        Long maxVersionClient2 = getVersion(client2);
+
+        if (maxVersionClient1 > 0 && maxVersionClient2 > 0) {
+            assertEquals(maxVersionClient1, maxVersionClient2);
+        }
+
+        // check vs /replication?command=indexversion call
+        ModifiableSolrParams params = new ModifiableSolrParams();
+        params.set("qt", ReplicationHandler.PATH);
+        params.set("_trace", "assertVersions");
+        params.set("command", "indexversion");
+        QueryRequest req = new QueryRequest(params);
+        NamedList<Object> resp = client1.request(req);
+        assertReplicationResponseSucceeded(resp);
+        Long version = (Long) resp.get("indexversion");
+        assertEquals(maxVersionClient1, version);
+
+        // check vs /replication?command=indexversion call
+        resp = client2.request(req);
+        assertReplicationResponseSucceeded(resp);
+        version = (Long) resp.get("indexversion");
+        assertEquals(maxVersionClient2, version);
+    }
+
+    @SuppressWarnings({"unchecked"})
+    public static Long getVersion(SolrClient client) throws Exception {
+        NamedList<Object> details;
+        ArrayList<NamedList<Object>> commits;
+        details = getDetails(client);
+        commits = (ArrayList<NamedList<Object>>) details.get("commits");
+        Long maxVersionFollower= 0L;
+        for(NamedList<Object> commit : commits) {
+            Long version = (Long) commit.get("indexVersion");
+            maxVersionFollower = Math.max(version, maxVersionFollower);
+        }
+        return maxVersionFollower;
+    }
+
+    //Simple function to wrap the invocation of replication commands on the 
various
+    //jetty servers.
+    public static void invokeReplicationCommand(String baseUrl, String 
pCommand) throws IOException
+    {
+        //String leaderUrl = buildUrl(pJettyPort) + "/" + 
DEFAULT_TEST_CORENAME + ReplicationHandler.PATH+"?command=" + pCommand;
+        String url = baseUrl + ReplicationHandler.PATH+"?command=" + pCommand;
+        URL u = new URL(url);
+        InputStream stream = u.openStream();
+        stream.close();
+    }
+
+    public  static NamedList<Object> query(String query, SolrClient s) throws 
SolrServerException, IOException {
+        ModifiableSolrParams params = new ModifiableSolrParams();
+
+        params.add("q", query);
+        params.add("sort","id desc");
+
+        QueryResponse qres = s.query(params);
+        return qres.getResponse();
+    }
+
+    /** will sleep up to 30 seconds, looking for expectedDocCount */
+    public static NamedList<Object> rQuery(int expectedDocCount, String query, 
SolrClient client) throws Exception {
+        int timeSlept = 0;
+        NamedList<Object> res = query(query, client);
+        while (expectedDocCount != numFound(res)
+                && timeSlept < 30000) {
+            log.info("Waiting for {} docs", expectedDocCount);
+            timeSlept += 100;
+            Thread.sleep(100);
+            res = query(query, client);
+        }
+        if (log.isInfoEnabled()) {
+            log.info("Waited for {}ms and found {} docs", timeSlept, 
numFound(res));
+        }
+        return res;
+    }
+
+    public static long numFound(NamedList<Object> res) {
+        return ((SolrDocumentList) res.get("response")).getNumFound();
+    }
+
+    public static NamedList<Object> getDetails(SolrClient s) throws Exception {
+        ModifiableSolrParams params = new ModifiableSolrParams();
+        params.set("command","details");
+        params.set("_trace","getDetails");
+        params.set("qt",ReplicationHandler.PATH);
+        QueryRequest req = new QueryRequest(params);
+
+        NamedList<Object> res = s.request(req);
+        assertReplicationResponseSucceeded(res);
+
+        @SuppressWarnings("unchecked") NamedList<Object> details
+                = (NamedList<Object>) res.get("details");
+
+        assertNotNull("null details", details);
+
+        return details;
+    }
+
+    public static NamedList<Object> getIndexVersion(SolrClient s) throws 
Exception {
+
+        ModifiableSolrParams params = new ModifiableSolrParams();
+        params.set("command","indexversion");
+        params.set("_trace","getIndexVersion");
+        params.set("qt",ReplicationHandler.PATH);
+        QueryRequest req = new QueryRequest(params);
+
+        NamedList<Object> res = s.request(req);
+        assertReplicationResponseSucceeded(res);
+
+        return res;
+    }
+
+    public static void assertReplicationResponseSucceeded(NamedList<?> 
response) {
+        assertNotNull("null response from server", response);
+        assertNotNull("Expected replication response to have 'status' field", 
response.get("status"));
+        assertEquals("OK", response.get("status"));
+    }
+
+    public static NamedList<Object> reloadCore(SolrClient s, String core) 
throws Exception {
+
+        ModifiableSolrParams params = new ModifiableSolrParams();
+        params.set("action","reload");
+        params.set("core", core);
+        params.set("qt","/admin/cores");
+        QueryRequest req = new QueryRequest(params);
+
+        try (HttpSolrClient adminClient = adminClient(s)) {
+            NamedList<Object> res = adminClient.request(req);
+            assertNotNull("null response from server", res);
+            return res;
+        }
+    }
+
+    public static HttpSolrClient adminClient(SolrClient client) {
+        String adminUrl = 
((HttpSolrClient)client).getBaseURL().replace("/collection1", "");
+        return SolrTestCaseJ4.getHttpSolrClient(adminUrl);
+    }
+
+
+    public static void pullFromTo(String srcUrl, String destUrl) throws 
IOException {
+        URL url;
+        InputStream stream;
+        String leaderUrl = destUrl
+                + 
ReplicationHandler.PATH+"?wait=true&command=fetchindex&leaderUrl="
+                + srcUrl
+                + ReplicationHandler.PATH;
+        url = new URL(leaderUrl);
+        stream = url.openStream();
+        stream.close();
+    }
+
+    public static class SolrInstance {

Review comment:
       I see that this was copied from another location, so maybe this doesn't 
need to be addressed now.




-- 
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