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

epugh pushed a commit to branch branch_9x
in repository https://gitbox.apache.org/repos/asf/solr.git


The following commit(s) were added to refs/heads/branch_9x by this push:
     new a8031a35003 SOLR-13880: Add test that creates/reloads/deletes 
collections (#1105)
a8031a35003 is described below

commit a8031a350034a5e0e0c7f362cb5691e45c6dc275
Author: Eric Pugh <[email protected]>
AuthorDate: Sun Apr 30 08:54:58 2023 -0400

    SOLR-13880: Add test that creates/reloads/deletes collections (#1105)
---
 .../api/collections/CollectionReloadTest.java      | 115 ++++++++++++++++++++-
 1 file changed, 114 insertions(+), 1 deletion(-)

diff --git 
a/solr/core/src/test/org/apache/solr/cloud/api/collections/CollectionReloadTest.java
 
b/solr/core/src/test/org/apache/solr/cloud/api/collections/CollectionReloadTest.java
index cb2ee8e4b24..1eca0e5ee17 100644
--- 
a/solr/core/src/test/org/apache/solr/cloud/api/collections/CollectionReloadTest.java
+++ 
b/solr/core/src/test/org/apache/solr/cloud/api/collections/CollectionReloadTest.java
@@ -16,10 +16,20 @@
  */
 package org.apache.solr.cloud.api.collections;
 
+import com.carrotsearch.randomizedtesting.annotations.Repeat;
+import java.io.IOException;
 import java.lang.invoke.MethodHandles;
+import java.util.Locale;
 import java.util.concurrent.TimeUnit;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
 import org.apache.solr.SolrTestCaseJ4.SuppressSSL;
+import org.apache.solr.client.solrj.SolrServerException;
+import org.apache.solr.client.solrj.impl.CloudLegacySolrClient;
 import org.apache.solr.client.solrj.request.CollectionAdminRequest;
+import org.apache.solr.client.solrj.response.CollectionAdminResponse;
 import org.apache.solr.cloud.SolrCloudTestCase;
 import org.apache.solr.common.cloud.Replica;
 import org.apache.solr.common.util.RetryUtil;
@@ -60,7 +70,7 @@ public class CollectionReloadTest extends SolrCloudTestCase {
         1000,
         TimeUnit.MILLISECONDS,
         () -> {
-          long restartTime = 0;
+          long restartTime;
           try {
             restartTime = getCoreStatus(leader).getCoreStartTime().getTime();
           } catch (Exception e) {
@@ -86,4 +96,107 @@ public class CollectionReloadTest extends SolrCloudTestCase 
{
 
     log.info("testReloadedLeaderStateAfterZkSessionLoss succeeded ... shutting 
down now!");
   }
+
+  @Repeat(iterations = 2)
+  public void testCreateReloadDeleteAllNrt() throws Exception {
+    testCreateReloadDelete("testCreateReloadDeleteAllNrt", 3, 0, 0);
+  }
+
+  @Repeat(iterations = 2)
+  public void testCreateReloadDeleteAllTlog() throws Exception {
+    testCreateReloadDelete("testCreateReloadDeleteAllTlog", 0, 3, 0);
+  }
+
+  @Repeat(iterations = 2)
+  public void testCreateReloadDeletePull() throws Exception {
+    testCreateReloadDelete("testCreateReloadDeletePull", 0, 1, 2);
+  }
+
+  private void testCreateReloadDelete(
+      String collectionName, int nrtReplicas, int tlogReplicas, int 
pullReplicas) throws Exception {
+    int numShards = 3;
+    createCollection(numShards, nrtReplicas, tlogReplicas, pullReplicas, 
collectionName);
+    boolean reloaded = false;
+    while (true) {
+      waitForState(
+          "Timeout waiting for collection " + collectionName,
+          collectionName,
+          clusterShape(numShards, numShards * (nrtReplicas + tlogReplicas + 
pullReplicas)));
+      if (reloaded) {
+        break;
+      } else {
+        // reload
+        assertSuccessfulAdminRequest(
+            CollectionAdminRequest.reloadCollection(collectionName)
+                .process(cluster.getSolrClient()));
+        reloaded = true;
+      }
+    }
+    assertSuccessfulAdminRequest(
+        
CollectionAdminRequest.deleteCollection(collectionName).process(cluster.getSolrClient()));
+  }
+
+  private void assertSuccessfulAdminRequest(CollectionAdminResponse response) {
+    assertEquals("Unexpected response status", 0, response.getStatus());
+    assertTrue("Unsuccessful response: " + response, response.isSuccess());
+  }
+
+  private void createCollection(
+      int numShards, int nrtReplicas, int tlogReplicas, int pullReplicas, 
String collectionName)
+      throws SolrServerException, IOException {
+    switch (random().nextInt(3)) {
+      case 0:
+        log.info("Creating collection with SolrJ");
+        // Sometimes use SolrJ
+        assertSuccessfulAdminRequest(
+            CollectionAdminRequest.createCollection(
+                    collectionName, "conf", numShards, nrtReplicas, 
tlogReplicas, pullReplicas)
+                .process(cluster.getSolrClient()));
+        break;
+      case 1:
+        log.info("Creating collection with V1 API");
+        // Sometimes use v1 API
+        String url =
+            String.format(
+                Locale.ROOT,
+                
"%s/admin/collections?action=CREATE&name=%s&collection.configName=%s&numShards=%s&maxShardsPerNode=%s&nrtReplicas=%s&tlogReplicas=%s&pullReplicas=%s",
+                cluster.getRandomJetty(random()).getBaseUrl(),
+                collectionName,
+                "conf",
+                numShards,
+                100, // maxShardsPerNode
+                nrtReplicas,
+                tlogReplicas,
+                pullReplicas);
+        HttpGet createCollectionGet = new HttpGet(url);
+        ((CloudLegacySolrClient) cluster.getSolrClient())
+            .getHttpClient()
+            .execute(createCollectionGet);
+        break;
+      case 2:
+        log.info("Creating collection with V2 API");
+        // Sometimes use V2 API
+        url = cluster.getRandomJetty(random()).getBaseUrl().toString() + 
"/____v2/c";
+        String requestBody =
+            String.format(
+                Locale.ROOT,
+                "{create:{name:%s, config:%s, numShards:%s, 
maxShardsPerNode:%s, nrtReplicas:%s, tlogReplicas:%s, pullReplicas:%s}}",
+                collectionName,
+                "conf",
+                numShards, // numShards
+                100, // maxShardsPerNode
+                nrtReplicas,
+                tlogReplicas,
+                pullReplicas);
+        HttpPost createCollectionPost = new HttpPost(url);
+        createCollectionPost.setHeader("Content-type", "application/json");
+        createCollectionPost.setEntity(new StringEntity(requestBody));
+        HttpResponse httpResponse =
+            ((CloudLegacySolrClient) cluster.getSolrClient())
+                .getHttpClient()
+                .execute(createCollectionPost);
+        assertEquals(200, httpResponse.getStatusLine().getStatusCode());
+        break;
+    }
+  }
 }

Reply via email to