From a4a80dfc99b08eab44ae4f0bbcfd8dd3370c78f0 Mon Sep 17 00:00:00 2001 From: eliaporciani Date: Mon, 14 Jan 2019 11:19:03 +0100 Subject: [PATCH] [SEARCH-1364] Modified waitForDocCountCore and dependencies. Cherry picket from 2.0 to master --- .../solr/AbstractAlfrescoDistributedTest.java | 115 ++++++++---------- ...ibutedExplicitShardRoutingTrackerTest.java | 8 +- 2 files changed, 60 insertions(+), 63 deletions(-) diff --git a/search-services/alfresco-search/src/test/java/org/alfresco/solr/AbstractAlfrescoDistributedTest.java b/search-services/alfresco-search/src/test/java/org/alfresco/solr/AbstractAlfrescoDistributedTest.java index 4fbde66ac..37242d955 100644 --- a/search-services/alfresco-search/src/test/java/org/alfresco/solr/AbstractAlfrescoDistributedTest.java +++ b/search-services/alfresco-search/src/test/java/org/alfresco/solr/AbstractAlfrescoDistributedTest.java @@ -259,8 +259,9 @@ public abstract class AbstractAlfrescoDistributedTest extends SolrTestCaseJ4 cores.addAll(getJettyCores(jettyShards)); long begin = System.currentTimeMillis(); - for (SolrCore core : cores) { - waitForDocCountCore(core, query, count, waitMillis, begin); + + for (SolrClient client : getAllClients()) { + waitForDocCountCore(client, luceneToSolrQuery(query), count, waitMillis, begin); } } @@ -268,8 +269,8 @@ public abstract class AbstractAlfrescoDistributedTest extends SolrTestCaseJ4 { List cores = getJettyCores(jettyShards); long begin = System.currentTimeMillis(); - for (SolrCore core : cores) { - waitForDocCountCore(core, query, count, waitMillis, begin); + for (SolrClient client : getClusterClients()) { + waitForDocCountCore(client, luceneToSolrQuery(query), count, waitMillis, begin); } } @@ -300,6 +301,14 @@ public abstract class AbstractAlfrescoDistributedTest extends SolrTestCaseJ4 return clients; } + + public List getJettyClients() + { + List clients = new ArrayList(); + clients.addAll(jettyClients.values()); + return clients; + } + /** * Waits for the doc count on the first core available, then checks all the Shards match. * @param query @@ -310,10 +319,9 @@ public abstract class AbstractAlfrescoDistributedTest extends SolrTestCaseJ4 public void waitForDocCount(Query query, int count, long waitMillis) throws Exception { long begin = System.currentTimeMillis(); - List cores = getJettyCores(jettyContainers.values()); //TODO: Support multiple cores per jetty - SolrCore controlCore = cores.get(0); //Get the first one - waitForDocCountCore(controlCore, query, count, waitMillis, begin); + SolrClient controlClient = getJettyClients().get(0); //Get the first one + waitForDocCountCore(controlClient, luceneToSolrQuery(query), count, waitMillis, begin); waitForShardsCount(query, count, waitMillis, begin); } @@ -339,6 +347,9 @@ public abstract class AbstractAlfrescoDistributedTest extends SolrTestCaseJ4 private String fixTermQuery(String query) { int i = query.lastIndexOf(":"); + if (i == -1){ + return query + " "; + } String[] operands = {query.substring(0, i), query.substring(i+1)}; String s1 = operands[0].replaceAll("\\:", "\\\\:") @@ -348,12 +359,8 @@ public abstract class AbstractAlfrescoDistributedTest extends SolrTestCaseJ4 return s1 + ":" + operands[1] + " "; } - /** - * - * @param query - * @return - */ - private SolrQuery luceneToSolrQuery(Query query) + + public SolrQuery luceneToSolrQuery(Query query) { String[] terms = query.toString().split(" "); String solrQueryString = new String(); @@ -478,42 +485,39 @@ public abstract class AbstractAlfrescoDistributedTest extends SolrTestCaseJ4 public int assertNodesPerShardGreaterThan(int count, boolean ignoreZero) throws Exception { int shardHit = 0; + List clients = getJettyClients(); List cores = getJettyCores(jettyShards); - Query query = new TermQuery(new Term(FIELD_DOC_TYPE, SolrInformationServer.DOC_TYPE_NODE)); + SolrQuery query = luceneToSolrQuery(new TermQuery(new Term(FIELD_DOC_TYPE, SolrInformationServer.DOC_TYPE_NODE))); StringBuilder error = new StringBuilder(); - for (SolrCore core : cores) + for (int i = 0; i < clients.size(); ++i) { - RefCounted refCounted = null; - try + + SolrCore core = cores.get(i); + SolrClient client = clients.get(i); + + + QueryResponse response = client.query(query); + int totalHits = (int) response.getResults().getNumFound(); + + + if(totalHits > 0) { - refCounted = core.getSearcher(); - SolrIndexSearcher searcher = refCounted.get(); - TopDocs topDocs = searcher.search(query, 10); - if(topDocs.totalHits > 0) - { - shardHit++; - } - if(topDocs.totalHits < count) - { - if (ignoreZero && topDocs.totalHits == 0) - { - log.info(core.getName()+": have zero hits "); - } - else - { - error.append(" "+core.getName()+": "); - error.append("Expected nodes per shard greater than "+count+" found "+topDocs.totalHits+" : "+query.toString()); - } - } - log.info(core.getName()+": Hits "+topDocs.totalHits); + shardHit++; } - finally + if(totalHits < count) { - if (refCounted != null) + if (ignoreZero && totalHits == 0) { - refCounted.decref(); + log.info(core.getName()+": have zero hits "); + } + else + { + error.append(" "+core.getName()+": "); + error.append("Expected nodes per shard greater than "+count+" found "+totalHits+" : "+query.toString()); } } + log.info(core.getName()+": Hits "+totalHits); + } if (error.length() > 0) @@ -588,8 +592,8 @@ public abstract class AbstractAlfrescoDistributedTest extends SolrTestCaseJ4 } } - public void waitForDocCountCore(SolrCore core, - Query query, + public void waitForDocCountCore(SolrClient client, + SolrQuery query, long expectedNumFound, long waitMillis, long startMillis) @@ -600,26 +604,15 @@ public abstract class AbstractAlfrescoDistributedTest extends SolrTestCaseJ4 int increment = 1; while(new Date().getTime() < timeout) { - RefCounted refCounted = null; - boolean refCountedDecremented = false; - try { - refCounted = core.getSearcher(); - SolrIndexSearcher searcher = refCounted.get(); - TopDocs topDocs = searcher.search(query, 10); - totalHits = topDocs.totalHits; - refCounted.decref(); - refCountedDecremented = true; - if (topDocs.totalHits == expectedNumFound) { - return; - } else { - Thread.sleep(500 * increment++); - } - } finally { - if(refCounted!=null && !refCountedDecremented) - { - refCounted.decref(); - } + QueryResponse response = client.query(query); + totalHits = (int) response.getResults().getNumFound(); + + if (totalHits == expectedNumFound) { + return; + } else { + Thread.sleep(500 * increment++); } + } throw new Exception("Core:Wait error expected "+expectedNumFound+" found "+totalHits+" : "+query.toString()); } diff --git a/search-services/alfresco-search/src/test/java/org/alfresco/solr/tracker/DistributedExplicitShardRoutingTrackerTest.java b/search-services/alfresco-search/src/test/java/org/alfresco/solr/tracker/DistributedExplicitShardRoutingTrackerTest.java index 20bc99386..c8dd88089 100644 --- a/search-services/alfresco-search/src/test/java/org/alfresco/solr/tracker/DistributedExplicitShardRoutingTrackerTest.java +++ b/search-services/alfresco-search/src/test/java/org/alfresco/solr/tracker/DistributedExplicitShardRoutingTrackerTest.java @@ -28,6 +28,7 @@ import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; import org.apache.lucene.util.LuceneTestCase; import org.apache.solr.SolrTestCaseJ4; +import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.core.SolrCore; import org.junit.Rule; import org.junit.Test; @@ -97,6 +98,7 @@ public class DistributedExplicitShardRoutingTrackerTest extends AbstractAlfresco Query contentQuery = new TermQuery(new Term("content@s___t@{http://www.alfresco.org/model/content/1.0}content", "world")); Query aclQuery = new TermQuery(new Term(FIELD_DOC_TYPE, SolrInformationServer.DOC_TYPE_ACL)); List shards = getJettyCores(jettyShards); + List shardClients = getClusterClients(); long begin = System.currentTimeMillis(); //Acls go to all cores @@ -106,13 +108,15 @@ public class DistributedExplicitShardRoutingTrackerTest extends AbstractAlfresco waitForShardsCount(contentQuery,numNodes,20000, begin); begin = System.currentTimeMillis(); - for (SolrCore core : shards) + for (int i = 0; i < shardClients.size(); ++i) { + SolrCore core = shards.get(i); + SolrClient client = shardClients.get(i); switch (core.getName()) { case "shard0": case "shard1": - waitForDocCountCore(core, contentQuery, 500, 1000, begin); + waitForDocCountCore(client, luceneToSolrQuery(contentQuery), 500, 1000, begin); break; default: //ignore other shards because we will check below