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 2023/11/15 18:13:15 UTC

(solr) branch branch_9x updated: SOLR-16397: Tweak v2 'renamecore' API to be more REST-ful (#2072)

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 77fc70764e2 SOLR-16397: Tweak v2 'renamecore' API to be more REST-ful (#2072)
77fc70764e2 is described below

commit 77fc70764e2cba177ac43e0307c329e3d0be901f
Author: Sanjay Dutt <sa...@gmail.com>
AuthorDate: Wed Nov 15 23:16:17 2023 +0530

    SOLR-16397: Tweak v2 'renamecore' API to be more REST-ful (#2072)
    
    This commit changes the v2 "renamecore" API to be more in line
    with the REST-ful design we're targeting for Solr's v2 APIs.
    
    Following these changes, the v2 API now appears as:
      1POST /api/cores/coreName/rename {...}`
    
    (Although not shown above, the 'rename' command specifier
    no longer appears in the request body.)
    
    This commit also converts the API to the new JAX-RS framework.
    
    ---------
    
    Co-authored-by: iamsanjay <sa...@yahoo.com>
    Co-authored-by: Jason Gerlowski <ge...@apache.org>
---
 solr/CHANGES.txt                                   |  3 +
 .../solr/client/api/endpoint/RenameCoreApi.java    | 39 +++++++++++++
 .../client/api/model/RenameCoreRequestBody.java    | 30 ++++++++++
 .../solr/handler/admin/CoreAdminHandler.java       |  6 +-
 .../solr/handler/admin/CoreAdminOperation.java     | 17 ++++--
 .../apache/solr/handler/admin/api/RenameCore.java  | 66 ++++++++++++++++++++++
 .../solr/handler/admin/TestCoreAdminApis.java      |  6 --
 .../configuration-guide/pages/coreadmin-api.adoc   | 39 ++++++++++---
 8 files changed, 184 insertions(+), 22 deletions(-)

diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index dcd141fab54..54f002e8a10 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -31,6 +31,9 @@ Improvements
 
 * SOLR-17041: Make CommitTracker currentTlogSize lazy (Alex Deparvu)
 
+* SOLR-16397: The rename-core v2 endpoint has been updated to be more REST-ful.
+  RENAME is now available at `POST /api/cores/coreName/rename` (Sanjay Dutt via Jason Gerlowski)
+
 Optimizations
 ---------------------
 (No changes)
diff --git a/solr/api/src/java/org/apache/solr/client/api/endpoint/RenameCoreApi.java b/solr/api/src/java/org/apache/solr/client/api/endpoint/RenameCoreApi.java
new file mode 100644
index 00000000000..115505fd894
--- /dev/null
+++ b/solr/api/src/java/org/apache/solr/client/api/endpoint/RenameCoreApi.java
@@ -0,0 +1,39 @@
+/*
+ * 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.api.endpoint;
+
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.parameters.RequestBody;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import org.apache.solr.client.api.model.RenameCoreRequestBody;
+import org.apache.solr.client.api.model.SolrJerseyResponse;
+
+/** V2 API definition for renaming a Solr core. */
+@Path("/cores/{coreName}/rename")
+public interface RenameCoreApi {
+  @POST
+  @Operation(
+      summary = "The RENAME action changes the name of a Solr core",
+      tags = {"cores"})
+  SolrJerseyResponse renameCore(
+      @PathParam("coreName") String coreName,
+      @RequestBody(description = "Additional properties related to the core renaming")
+          RenameCoreRequestBody requestBody)
+      throws Exception;
+}
diff --git a/solr/api/src/java/org/apache/solr/client/api/model/RenameCoreRequestBody.java b/solr/api/src/java/org/apache/solr/client/api/model/RenameCoreRequestBody.java
new file mode 100644
index 00000000000..55037117fe8
--- /dev/null
+++ b/solr/api/src/java/org/apache/solr/client/api/model/RenameCoreRequestBody.java
@@ -0,0 +1,30 @@
+/*
+ * 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.api.model;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import io.swagger.v3.oas.annotations.media.Schema;
+
+public class RenameCoreRequestBody {
+  @Schema(description = "The new name for the Solr core.", required = true)
+  @JsonProperty
+  public String to;
+
+  @Schema(description = "Request ID to track this action which will be processed asynchronously.")
+  @JsonProperty
+  public String async;
+}
diff --git a/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java b/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java
index 60c83d10d15..6ea7fe8e7aa 100644
--- a/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java
@@ -64,7 +64,7 @@ import org.apache.solr.handler.admin.api.OverseerOperationAPI;
 import org.apache.solr.handler.admin.api.PrepareCoreRecoveryAPI;
 import org.apache.solr.handler.admin.api.RejoinLeaderElectionAPI;
 import org.apache.solr.handler.admin.api.ReloadCore;
-import org.apache.solr.handler.admin.api.RenameCoreAPI;
+import org.apache.solr.handler.admin.api.RenameCore;
 import org.apache.solr.handler.admin.api.RequestApplyCoreUpdatesAPI;
 import org.apache.solr.handler.admin.api.RequestBufferUpdatesAPI;
 import org.apache.solr.handler.admin.api.RequestCoreCommandStatusAPI;
@@ -383,7 +383,6 @@ public class CoreAdminHandler extends RequestHandlerBase implements PermissionNa
     apis.addAll(AnnotatedApi.getApis(new CreateCoreAPI(this)));
     apis.addAll(AnnotatedApi.getApis(new RejoinLeaderElectionAPI(this)));
     apis.addAll(AnnotatedApi.getApis(new OverseerOperationAPI(this)));
-    apis.addAll(AnnotatedApi.getApis(new RenameCoreAPI(this)));
     apis.addAll(AnnotatedApi.getApis(new MergeIndexesAPI(this)));
     apis.addAll(AnnotatedApi.getApis(new SplitCoreAPI(this)));
     apis.addAll(AnnotatedApi.getApis(new RequestCoreCommandStatusAPI(this)));
@@ -406,7 +405,8 @@ public class CoreAdminHandler extends RequestHandlerBase implements PermissionNa
         RestoreCore.class,
         ReloadCore.class,
         UnloadCore.class,
-        SwapCores.class);
+        SwapCores.class,
+        RenameCore.class);
   }
 
   public interface CoreAdminOp {
diff --git a/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminOperation.java b/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminOperation.java
index c8947803400..1f6deee32ca 100644
--- a/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminOperation.java
+++ b/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminOperation.java
@@ -61,6 +61,7 @@ import java.util.Map;
 import org.apache.solr.client.api.endpoint.SwapCoresApi;
 import org.apache.solr.client.api.model.ListCoreSnapshotsResponse;
 import org.apache.solr.client.api.model.ReloadCoreRequestBody;
+import org.apache.solr.client.api.model.RenameCoreRequestBody;
 import org.apache.solr.client.api.model.SolrJerseyResponse;
 import org.apache.solr.client.api.model.SwapCoresRequestBody;
 import org.apache.solr.client.api.model.UnloadCoreRequestBody;
@@ -79,6 +80,7 @@ import org.apache.solr.core.SolrCore;
 import org.apache.solr.handler.admin.CoreAdminHandler.CoreAdminOp;
 import org.apache.solr.handler.admin.api.CoreSnapshot;
 import org.apache.solr.handler.admin.api.ReloadCore;
+import org.apache.solr.handler.admin.api.RenameCore;
 import org.apache.solr.handler.admin.api.SwapCores;
 import org.apache.solr.handler.admin.api.UnloadCore;
 import org.apache.solr.handler.api.V2ApiUtils;
@@ -172,12 +174,15 @@ public enum CoreAdminOperation implements CoreAdminOp {
       RENAME,
       it -> {
         SolrParams params = it.req.getParams();
-        String name = params.required().get(CoreAdminParams.OTHER);
-        String cname = params.required().get(CoreAdminParams.CORE);
-
-        if (cname.equals(name)) return;
-
-        it.handler.coreContainer.rename(cname, name);
+        final String cname = params.required().get(CoreAdminParams.CORE);
+        final String name = params.required().get(CoreAdminParams.OTHER);
+        final var renameCoreRequestBody = new RenameCoreRequestBody();
+        renameCoreRequestBody.to = name;
+        final var renameCoreApi =
+            new RenameCore(
+                it.handler.coreContainer, it.handler.getCoreAdminAsyncTracker(), it.req, it.rsp);
+        SolrJerseyResponse response = renameCoreApi.renameCore(cname, renameCoreRequestBody);
+        V2ApiUtils.squashIntoSolrResponseWithoutHeader(it.rsp, response);
       }),
 
   MERGEINDEXES_OP(MERGEINDEXES, new MergeIndexesOp()),
diff --git a/solr/core/src/java/org/apache/solr/handler/admin/api/RenameCore.java b/solr/core/src/java/org/apache/solr/handler/admin/api/RenameCore.java
new file mode 100644
index 00000000000..03b96b57659
--- /dev/null
+++ b/solr/core/src/java/org/apache/solr/handler/admin/api/RenameCore.java
@@ -0,0 +1,66 @@
+/*
+ * 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.security.PermissionNameProvider.Name.CORE_EDIT_PERM;
+
+import javax.inject.Inject;
+import org.apache.solr.client.api.endpoint.RenameCoreApi;
+import org.apache.solr.client.api.model.RenameCoreRequestBody;
+import org.apache.solr.client.api.model.SolrJerseyResponse;
+import org.apache.solr.core.CoreContainer;
+import org.apache.solr.handler.admin.CoreAdminHandler;
+import org.apache.solr.jersey.PermissionName;
+import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.response.SolrQueryResponse;
+
+/**
+ * V2 API for renaming an existing Solr core.
+ *
+ * <p>The new API (POST /v2/cores/coreName/rename is equivalent to the v1 /admin/cores?action=rename
+ * command.
+ */
+public class RenameCore extends CoreAdminAPIBase implements RenameCoreApi {
+  @Inject
+  public RenameCore(
+      CoreContainer coreContainer,
+      CoreAdminHandler.CoreAdminAsyncTracker coreAdminAsyncTracker,
+      SolrQueryRequest solrQueryRequest,
+      SolrQueryResponse solrQueryResponse) {
+    super(coreContainer, coreAdminAsyncTracker, solrQueryRequest, solrQueryResponse);
+  }
+
+  @PermissionName(CORE_EDIT_PERM)
+  @Override
+  public SolrJerseyResponse renameCore(String coreName, RenameCoreRequestBody requestBody)
+      throws Exception {
+    ensureRequiredParameterProvided("coreName", coreName);
+    ensureRequiredRequestBodyProvided(requestBody);
+    ensureRequiredParameterProvided("to", requestBody.to);
+    SolrJerseyResponse solrJerseyResponse = instantiateJerseyResponse(SolrJerseyResponse.class);
+    if (coreName.equals(requestBody.to)) return solrJerseyResponse;
+    return handlePotentiallyAsynchronousTask(
+        solrJerseyResponse,
+        coreName,
+        requestBody.async,
+        "rename",
+        () -> {
+          coreContainer.rename(coreName, requestBody.to);
+          return solrJerseyResponse;
+        });
+  }
+}
diff --git a/solr/core/src/test/org/apache/solr/handler/admin/TestCoreAdminApis.java b/solr/core/src/test/org/apache/solr/handler/admin/TestCoreAdminApis.java
index 825cb996a5e..8a8ac0652bf 100644
--- a/solr/core/src/test/org/apache/solr/handler/admin/TestCoreAdminApis.java
+++ b/solr/core/src/test/org/apache/solr/handler/admin/TestCoreAdminApis.java
@@ -57,12 +57,6 @@ public class TestCoreAdminApis extends SolrTestCaseJ4 {
     Object[] params = calls.get("create");
     assertEquals("hello", params[0]);
     assertEquals(fromJSONString("{schema : schema.xml}"), params[2]);
-
-    TestCollectionAPIs.makeCall(
-        apiBag, "/cores/core1", SolrRequest.METHOD.POST, "{rename:{to: core2}}");
-    params = calls.get("rename");
-    assertEquals("core1", params[0]);
-    assertEquals("core2", params[1]);
   }
 
   @SuppressWarnings({"unchecked"})
diff --git a/solr/solr-ref-guide/modules/configuration-guide/pages/coreadmin-api.adoc b/solr/solr-ref-guide/modules/configuration-guide/pages/coreadmin-api.adoc
index f2cf158ce95..b0af69e2d93 100644
--- a/solr/solr-ref-guide/modules/configuration-guide/pages/coreadmin-api.adoc
+++ b/solr/solr-ref-guide/modules/configuration-guide/pages/coreadmin-api.adoc
@@ -349,7 +349,30 @@ This parameter is required in v1, and part of the url in the v2 API.
 
 The `RENAME` action changes the name of a Solr core.
 
-`admin/cores?action=RENAME&core=_core-name_&other=_other-core-name_`
+[.dynamic-tabs]
+--
+[example.tab-pane#v1coreadmin-rename]
+====
+[.tab-label]*V1 API*
+[source,bash]
+----
+curl -X GET "http://localhost:8983/solr/admin/cores?action=RENAME&core=currentCoreName&other=newCoreName"
+----
+====
+
+[example.tab-pane#v2coreadmin-rename]
+====
+[.tab-label]*V2 API*
+[source,bash]
+----
+curl -X POST http://localhost:8983/api/cores/currentCoreName/rename -H 'Content-Type: application/json' -d '
+  {
+    "to": "newCoreName"
+  }
+'
+----
+====
+--
 
 === RENAME Parameters
 
@@ -360,9 +383,10 @@ The `RENAME` action changes the name of a Solr core.
 s|Required |Default: none
 |===
 +
-The name of the Solr core to be renamed.
+The name of an existing Solr core to rename.
+Specified as a query parameter if making a v1 request, or as a path parameter if using the v2 API.
 
-`other`::
+`other` (v1), `to` (v2)::
 +
 [%autowidth,frame=none]
 |===
@@ -370,6 +394,7 @@ s|Required |Default: none
 |===
 +
 The new name for the Solr core.
+Specified as a query parameter if making a v1 request, or as a property in the request body if using the v2 API.
 If the persistent attribute of `<solr>` is `true`, the new name will be written to `solr.xml` as the `name` attribute of the `<core>` attribute.
 
 `async`::
@@ -381,7 +406,6 @@ If the persistent attribute of `<solr>` is `true`, the new name will be written
 +
 Request ID to track this action which will be processed asynchronously.
 
-
 [[coreadmin-swap]]
 == SWAP
 
@@ -402,19 +426,20 @@ Each core will be known by the name of the other, after the swap.
 ----
 ====
 
-[example.tab-pane#v2coreadmin-reload]
+[example.tab-pane#v2coreadmin-swap]
 ====
 [.tab-label]*V2 API*
 
 [source,bash]
 ----
-`curl -X POST http://localhost:8983/api/cores/_core-name_/swap -H 'Content-Type: application/json' -d '
+curl -X POST http://localhost:8983/api/cores/_core-name_/swap -H 'Content-Type: application/json' -d '
   {
     "with": "_other-core-name_"
   }
-'`
+'
 ----
 ====
+--
 
 [IMPORTANT]
 ====