You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ab...@apache.org on 2019/06/11 12:10:53 UTC

[lucene-solr] branch master updated: Improve this test - instead of waiting a fixed amount of time wait until an update arrives. Also, deal with partial updates.

This is an automated email from the ASF dual-hosted git repository.

ab pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucene-solr.git


The following commit(s) were added to refs/heads/master by this push:
     new e2832b3  Improve this test - instead of waiting a fixed amount of time wait until an update arrives. Also, deal with partial updates.
e2832b3 is described below

commit e2832b37d7e130103370fd3c854cf2cff3aecf7c
Author: Andrzej Bialecki <ab...@apache.org>
AuthorDate: Tue Jun 11 14:09:18 2019 +0200

    Improve this test - instead of waiting a fixed amount of time wait until an update arrives.
    Also, deal with partial updates.
---
 .../apache/solr/util/MockSearchableSolrClient.java | 15 ++++++
 .../metrics/rrd/SolrRrdBackendFactoryTest.java     | 53 ++++++++++++++++++++++
 2 files changed, 68 insertions(+)

diff --git a/solr/core/src/java/org/apache/solr/util/MockSearchableSolrClient.java b/solr/core/src/java/org/apache/solr/util/MockSearchableSolrClient.java
index c4d1d0c..646d008 100644
--- a/solr/core/src/java/org/apache/solr/util/MockSearchableSolrClient.java
+++ b/solr/core/src/java/org/apache/solr/util/MockSearchableSolrClient.java
@@ -22,6 +22,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Objects;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
 
 import org.apache.solr.client.solrj.SolrClient;
 import org.apache.solr.client.solrj.SolrRequest;
@@ -41,6 +42,9 @@ import org.apache.solr.common.util.NamedList;
 public class MockSearchableSolrClient extends SolrClient {
   public Map<String, Map<String, SolrInputDocument>> docs = new ConcurrentHashMap<>();
 
+  private AtomicLong numUpdates = new AtomicLong();
+  private AtomicLong numQueries = new AtomicLong();
+
   public void clear() {
     docs.clear();
   }
@@ -64,6 +68,7 @@ public class MockSearchableSolrClient extends SolrClient {
           String id = (String) doc.getFieldValue("id");
           Objects.requireNonNull(id, doc.toString());
           docs.computeIfAbsent(collection, c -> new LinkedHashMap<>()).put(id, doc);
+          numUpdates.incrementAndGet();
         });
       }
     } else if (request instanceof QueryRequest) {
@@ -75,6 +80,7 @@ public class MockSearchableSolrClient extends SolrClient {
       final SolrDocumentList lst = new SolrDocumentList();
       if (query != null) {
         if (query.startsWith("{!term f=id}") || query.startsWith("id:")) {
+          numQueries.incrementAndGet();
           String id;
           if (query.startsWith("{!")) {
             id = query.substring(12);
@@ -92,6 +98,7 @@ public class MockSearchableSolrClient extends SolrClient {
             }
           }
         } else if (query.equals("*:*")) {
+          numQueries.incrementAndGet();
           Map<String, SolrInputDocument> collDocs = docs.get(collection);
           if (collDocs != null) {
             lst.setNumFound(collDocs.size());
@@ -110,6 +117,14 @@ public class MockSearchableSolrClient extends SolrClient {
     return res;
   }
 
+  public long getNumUpdates() {
+    return numUpdates.get();
+  }
+
+  public long getNumQueries() {
+    return numQueries.get();
+  }
+
   @Override
   public void close() throws IOException {
 
diff --git a/solr/core/src/test/org/apache/solr/metrics/rrd/SolrRrdBackendFactoryTest.java b/solr/core/src/test/org/apache/solr/metrics/rrd/SolrRrdBackendFactoryTest.java
index 955c256..2c7ea9d 100644
--- a/solr/core/src/test/org/apache/solr/metrics/rrd/SolrRrdBackendFactoryTest.java
+++ b/solr/core/src/test/org/apache/solr/metrics/rrd/SolrRrdBackendFactoryTest.java
@@ -20,6 +20,7 @@ package org.apache.solr.metrics.rrd;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
 
 import org.apache.solr.SolrTestCaseJ4;
 import org.apache.solr.common.SolrInputDocument;
@@ -29,6 +30,7 @@ import org.apache.solr.common.util.TimeSource;
 import org.apache.solr.common.util.Utils;
 import org.apache.solr.util.LogLevel;
 import org.apache.solr.util.MockSearchableSolrClient;
+import org.apache.solr.util.TimeOut;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -86,16 +88,25 @@ public class SolrRrdBackendFactoryTest extends SolrTestCaseJ4 {
   public void testBasic() throws Exception {
     long startTime = 1000000000;
     RrdDb db = new RrdDb(createDef(startTime), factory);
+    long lastNumUpdates = solrClient.getNumUpdates();
     List<Pair<String, Long>> list = factory.list(100);
     assertEquals(list.toString(), 1, list.size());
     assertEquals(list.toString(), "foo", list.get(0).first());
+
     timeSource.sleep(4000);
+    lastNumUpdates = waitForUpdates(lastNumUpdates);
+
+    // wait until updates stop coming - the first update could have been partial
+    lastNumUpdates = waitForUpdatesToStop(lastNumUpdates);
+
     // there should be one sync data
     assertEquals(solrClient.docs.toString(), 1, solrClient.docs.size());
     String id = SolrRrdBackendFactory.ID_PREFIX + SolrRrdBackendFactory.ID_SEP + "foo";
     SolrInputDocument doc = solrClient.docs.get(CollectionAdminParams.SYSTEM_COLL).get(id);
     long timestamp = (Long)doc.getFieldValue("timestamp_l");
+
     timeSource.sleep(4000);
+
     SolrInputDocument newDoc = solrClient.docs.get(CollectionAdminParams.SYSTEM_COLL).get(id);
     assertEquals(newDoc.toString(), newDoc, doc);
     // make sure the update doesn't race with the sampling boundaries
@@ -110,6 +121,8 @@ public class SolrRrdBackendFactoryTest extends SolrTestCaseJ4 {
       lastTime = lastTime + 60;
     }
     timeSource.sleep(3000);
+    lastNumUpdates = waitForUpdates(lastNumUpdates);
+
     newDoc = solrClient.docs.get(CollectionAdminParams.SYSTEM_COLL).get(id);
     assertFalse(newDoc.toString(), newDoc.equals(doc));
     long newTimestamp = (Long)newDoc.getFieldValue("timestamp_l");
@@ -138,6 +151,8 @@ public class SolrRrdBackendFactoryTest extends SolrTestCaseJ4 {
     assertEquals(list.toString(), 1, list.size());
     assertEquals(list.toString(), "foo", list.get(0).first());
 
+    lastNumUpdates = solrClient.getNumUpdates();
+
     // re-open read-write
     db = new RrdDb("solr:foo", factory);
     s = db.createSample();
@@ -146,6 +161,8 @@ public class SolrRrdBackendFactoryTest extends SolrTestCaseJ4 {
     s.setValue("two", 100);
     s.update();
     timeSource.sleep(3000);
+    lastNumUpdates = waitForUpdates(lastNumUpdates);
+
     // should update
     timestamp = newTimestamp;
     doc = newDoc;
@@ -200,4 +217,40 @@ public class SolrRrdBackendFactoryTest extends SolrTestCaseJ4 {
     return Utils.toJSONString(map);
   }
 
+  private long waitForUpdates(long lastNumUpdates) throws Exception {
+    TimeOut timeOut = new TimeOut(30, TimeUnit.SECONDS, timeSource);
+    while (!timeOut.hasTimedOut()) {
+      timeOut.sleep(1000);
+      if (solrClient.getNumUpdates() > lastNumUpdates) {
+        return solrClient.getNumUpdates();
+      }
+    }
+    if (solrClient.getNumUpdates() > lastNumUpdates) {
+      return solrClient.getNumUpdates();
+    }
+    throw new Exception("time out waiting for updates");
+  }
+
+
+  private long waitForUpdatesToStop(long lastNumUpdates) throws Exception {
+    TimeOut timeOut = new TimeOut(30, TimeUnit.SECONDS, timeSource);
+    int stopped = 0;
+    while (!timeOut.hasTimedOut()) {
+      timeOut.sleep(1000);
+      if (solrClient.getNumUpdates() > lastNumUpdates) {
+        stopped = 0;
+        lastNumUpdates = solrClient.getNumUpdates();
+        continue;
+      } else {
+        stopped++;
+        if (stopped > 2) {
+          return lastNumUpdates;
+        }
+      }
+      timeOut.sleep(1000);
+    }
+    throw new Exception("time out waiting for updates");
+  }
+
+
 }