You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@solr.apache.org by ge...@apache.org on 2022/11/28 14:53:06 UTC

[solr] branch branch_9x updated: SOLR-15749: Create v2 equivalent for 'rename' collection API

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

gerlowskija 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 de56a43464c SOLR-15749: Create v2 equivalent for 'rename' collection API
de56a43464c is described below

commit de56a43464c716ab88a8dbe9011e6af92a7602ba
Author: Anakhe Ajayi <93...@users.noreply.github.com>
AuthorDate: Wed Nov 9 19:24:46 2022 +0100

    SOLR-15749: Create v2 equivalent for 'rename' collection API
    
    v2 API is available at `POST /api/collections/collName/rename`
---
 solr/CHANGES.txt                                   |  2 +
 .../solr/handler/admin/CollectionsHandler.java     |  2 +
 .../handler/admin/api/RenameCollectionAPI.java     | 99 ++++++++++++++++++++++
 .../solr/handler/admin/V2ApiMappingTest.java       |  7 ++
 .../admin/api/V2CollectionAPIMappingTest.java      | 18 ++++
 .../pages/collection-management.adoc               | 12 ++-
 .../request/beans/RenameCollectionPayload.java     | 29 +++++++
 7 files changed, 166 insertions(+), 3 deletions(-)

diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 0ea818f6085..d5f82c07b9e 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -47,6 +47,8 @@ Improvements
 * SOLR-11028: A v2 equivalent of the `/admin/collections?action= REPLACE` command is now available at
   `POST /api/cluster/nodes/nodeName/replace`. (Joshua Ouma via Jason Gerlowski)
 
+* SOLR-15479: A v2 equivalent of the RENAME command is now available at `POST /api/collections/collName/rename`. (Anakhe Ajayi via Jason Gerlowski)
+
 Optimizations
 ---------------------
 
diff --git a/solr/core/src/java/org/apache/solr/handler/admin/CollectionsHandler.java b/solr/core/src/java/org/apache/solr/handler/admin/CollectionsHandler.java
index 79467e08348..4a64fc893e9 100644
--- a/solr/core/src/java/org/apache/solr/handler/admin/CollectionsHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/admin/CollectionsHandler.java
@@ -221,6 +221,7 @@ import org.apache.solr.handler.admin.api.ModifyCollectionAPI;
 import org.apache.solr.handler.admin.api.MoveReplicaAPI;
 import org.apache.solr.handler.admin.api.RebalanceLeadersAPI;
 import org.apache.solr.handler.admin.api.ReloadCollectionAPI;
+import org.apache.solr.handler.admin.api.RenameCollectionAPI;
 import org.apache.solr.handler.admin.api.ReplaceNodeAPI;
 import org.apache.solr.handler.admin.api.SetCollectionPropertyAPI;
 import org.apache.solr.handler.admin.api.SplitShardAPI;
@@ -2101,6 +2102,7 @@ public class CollectionsHandler extends RequestHandlerBase implements Permission
     apis.addAll(AnnotatedApi.getApis(new ReloadCollectionAPI(this)));
     apis.addAll(AnnotatedApi.getApis(new SetCollectionPropertyAPI(this)));
     apis.addAll(AnnotatedApi.getApis(new CollectionStatusAPI(this)));
+    apis.addAll(AnnotatedApi.getApis(new RenameCollectionAPI(this)));
     return apis;
   }
 
diff --git a/solr/core/src/java/org/apache/solr/handler/admin/api/RenameCollectionAPI.java b/solr/core/src/java/org/apache/solr/handler/admin/api/RenameCollectionAPI.java
new file mode 100644
index 00000000000..7f92593ff79
--- /dev/null
+++ b/solr/core/src/java/org/apache/solr/handler/admin/api/RenameCollectionAPI.java
@@ -0,0 +1,99 @@
+/*
+ * 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.admin.api;
+
+import static org.apache.solr.client.solrj.SolrRequest.METHOD.POST;
+import static org.apache.solr.common.params.CollectionAdminParams.FOLLOW_ALIASES;
+import static org.apache.solr.common.params.CollectionAdminParams.TARGET;
+import static org.apache.solr.common.params.CommonAdminParams.ASYNC;
+import static org.apache.solr.common.params.CommonParams.ACTION;
+import static org.apache.solr.common.params.CommonParams.NAME;
+import static org.apache.solr.handler.ClusterAPI.wrapParams;
+import static org.apache.solr.security.PermissionNameProvider.Name.COLL_EDIT_PERM;
+
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.MapperFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.io.IOException;
+import java.util.Locale;
+import org.apache.commons.collections4.IterableUtils;
+import org.apache.solr.api.EndPoint;
+import org.apache.solr.client.solrj.request.beans.RenameCollectionPayload;
+import org.apache.solr.common.params.CollectionAdminParams;
+import org.apache.solr.common.params.CollectionParams;
+import org.apache.solr.common.util.ContentStream;
+import org.apache.solr.handler.admin.CollectionsHandler;
+import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.response.SolrQueryResponse;
+import org.apache.solr.util.SolrJacksonAnnotationInspector;
+
+/**
+ * V2 API for "renaming" an existing collection
+ *
+ * <p>This API is analogous to the v1 /admin/collections?action=RENAME command.
+ */
+public class RenameCollectionAPI {
+
+  private final CollectionsHandler collectionsHandler;
+  private static final ObjectMapper REQUEST_BODY_PARSER =
+      SolrJacksonAnnotationInspector.createObjectMapper()
+          .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
+          .disable(MapperFeature.AUTO_DETECT_FIELDS);
+
+  public RenameCollectionAPI(CollectionsHandler collectionsHandler) {
+    this.collectionsHandler = collectionsHandler;
+  }
+
+  @EndPoint(
+      path = {"/collections/{collection}/rename"},
+      method = POST,
+      permission = COLL_EDIT_PERM)
+  public void renameCollection(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
+    final RenameCollectionPayload v2Body = parseRenameParamsFromRequestBody(req);
+
+    req =
+        wrapParams(
+            req,
+            ACTION,
+            CollectionParams.CollectionAction.RENAME.name().toLowerCase(Locale.ROOT),
+            NAME,
+            req.getPathTemplateValues().get(CollectionAdminParams.COLLECTION),
+            TARGET,
+            v2Body.to,
+            ASYNC,
+            v2Body.async,
+            FOLLOW_ALIASES,
+            v2Body.followAliases);
+    collectionsHandler.handleRequestBody(req, rsp);
+  }
+
+  // TODO This is a bit hacky, but it's not worth investing in the request-body parsing code much
+  // here, as it's
+  //  something that's already somewhat built-in when this eventually moves to JAX-RS
+  private RenameCollectionPayload parseRenameParamsFromRequestBody(
+      SolrQueryRequest solrQueryRequest) throws IOException {
+    if (IterableUtils.isEmpty(solrQueryRequest.getContentStreams())) {
+      // An empty request-body is invalid (the 'to' field is required at a minimum), but we'll lean
+      // on the input-validation in CollectionsHandler to
+      // catch this, rather than duplicating the check for that here
+      return new RenameCollectionPayload();
+    }
+
+    final ContentStream cs = solrQueryRequest.getContentStreams().iterator().next();
+    return REQUEST_BODY_PARSER.readValue(cs.getStream(), RenameCollectionPayload.class);
+  }
+}
diff --git a/solr/core/src/test/org/apache/solr/handler/admin/V2ApiMappingTest.java b/solr/core/src/test/org/apache/solr/handler/admin/V2ApiMappingTest.java
index 1cdbdc9f0fb..78ac3dbd342 100644
--- a/solr/core/src/test/org/apache/solr/handler/admin/V2ApiMappingTest.java
+++ b/solr/core/src/test/org/apache/solr/handler/admin/V2ApiMappingTest.java
@@ -22,6 +22,7 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 
 import com.google.common.collect.Maps;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
@@ -32,6 +33,7 @@ import org.apache.solr.api.Api;
 import org.apache.solr.api.ApiBag;
 import org.apache.solr.common.params.SolrParams;
 import org.apache.solr.common.util.CommandOperation;
+import org.apache.solr.common.util.ContentStream;
 import org.apache.solr.common.util.ContentStreamBase;
 import org.apache.solr.handler.RequestHandlerBase;
 import org.apache.solr.request.LocalSolrQueryRequest;
@@ -119,6 +121,11 @@ public abstract class V2ApiMappingTest<T extends RequestHandlerBase> extends Sol
                 new ContentStreamBase.StringStream(v2RequestBody), api.getCommandSchema(), true);
           }
 
+          @Override
+          public Collection<ContentStream> getContentStreams() {
+            return List.of(new ContentStreamBase.StringStream(v2RequestBody));
+          }
+
           @Override
           public Map<String, String> getPathTemplateValues() {
             return parts;
diff --git a/solr/core/src/test/org/apache/solr/handler/admin/api/V2CollectionAPIMappingTest.java b/solr/core/src/test/org/apache/solr/handler/admin/api/V2CollectionAPIMappingTest.java
index 99e2f17c933..cebbaee2227 100644
--- a/solr/core/src/test/org/apache/solr/handler/admin/api/V2CollectionAPIMappingTest.java
+++ b/solr/core/src/test/org/apache/solr/handler/admin/api/V2CollectionAPIMappingTest.java
@@ -19,6 +19,7 @@ package org.apache.solr.handler.admin.api;
 
 import static org.apache.solr.common.params.CollectionAdminParams.COLLECTION;
 import static org.apache.solr.common.params.CollectionAdminParams.COLL_CONF;
+import static org.apache.solr.common.params.CollectionAdminParams.TARGET;
 import static org.apache.solr.common.params.CommonAdminParams.ASYNC;
 import static org.apache.solr.common.params.CommonParams.ACTION;
 import static org.apache.solr.common.params.CommonParams.NAME;
@@ -49,6 +50,7 @@ import org.junit.Test;
  * are never expected in the same request.
  */
 public class V2CollectionAPIMappingTest extends V2ApiMappingTest<CollectionsHandler> {
+
   @Override
   public void populateApiBag() {
     final CollectionsHandler collectionsHandler = getRequestHandler();
@@ -62,6 +64,7 @@ public class V2CollectionAPIMappingTest extends V2ApiMappingTest<CollectionsHand
     apiBag.registerObject(new ReloadCollectionAPI(collectionsHandler));
     apiBag.registerObject(new SetCollectionPropertyAPI(collectionsHandler));
     apiBag.registerObject(new CollectionStatusAPI(collectionsHandler));
+    apiBag.registerObject(new RenameCollectionAPI(collectionsHandler));
   }
 
   @Override
@@ -85,6 +88,21 @@ public class V2CollectionAPIMappingTest extends V2ApiMappingTest<CollectionsHand
     assertEquals("shard2", v1Params.get(SHARD));
   }
 
+  @Test
+  public void testRenameCollectionAllParams() throws Exception {
+    final SolrParams v1Params =
+        captureConvertedV1Params(
+            "/collections/collName/rename",
+            "POST",
+            "{\"to\": \"targetColl\", \"async\": \"requestTrackingId\", \"followAliases\": true}");
+
+    assertEquals("rename", v1Params.get(ACTION));
+    assertEquals("collName", v1Params.get(NAME));
+    assertEquals("targetColl", v1Params.get(TARGET));
+    assertEquals("requestTrackingId", v1Params.get(ASYNC));
+    assertEquals(true, v1Params.getPrimitiveBool("followAliases"));
+  }
+
   @Test
   public void testModifyCollectionAllProperties() throws Exception {
     final SolrParams v1Params =
diff --git a/solr/solr-ref-guide/modules/deployment-guide/pages/collection-management.adoc b/solr/solr-ref-guide/modules/deployment-guide/pages/collection-management.adoc
index 50e4da67bc0..cc330d9f36b 100644
--- a/solr/solr-ref-guide/modules/deployment-guide/pages/collection-management.adoc
+++ b/solr/solr-ref-guide/modules/deployment-guide/pages/collection-management.adoc
@@ -535,8 +535,14 @@ http://localhost:8983/solr/admin/collections?action=RENAME&name=techproducts_v2&
 ====
 [.tab-label]*V2 API*
 
-We do not currently have a V2 equivalent.
-
+[source,bash]
+----
+curl -X POST http://localhost:8983/api/collections/techproducts/rename -H 'Content-Type: application/json' -d '
+  {
+    "to": "new_name"
+  }
+'
+----
 ====
 --
 
@@ -552,7 +558,7 @@ s|Required |Default: none
 +
 Name of the existing SolrCloud collection or an alias that refers to exactly one collection and is not a Routed Alias.
 
-`target`::
+`target` (v1), `to` (v2)::
 +
 [%autowidth,frame=none]
 |===
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/request/beans/RenameCollectionPayload.java b/solr/solrj/src/java/org/apache/solr/client/solrj/request/beans/RenameCollectionPayload.java
new file mode 100644
index 00000000000..c85fd42daee
--- /dev/null
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/request/beans/RenameCollectionPayload.java
@@ -0,0 +1,29 @@
+/*
+ * 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.client.solrj.request.beans;
+
+import org.apache.solr.common.annotation.JsonProperty;
+import org.apache.solr.common.util.ReflectMapWriter;
+
+public class RenameCollectionPayload implements ReflectMapWriter {
+  @JsonProperty public String async;
+
+  @JsonProperty public Boolean followAliases;
+
+  @JsonProperty(required = true)
+  public String to;
+}