Apache Solr open-source search software
SOLR-15351: Rewrite GET /v2/c/collName using annotations (#396)
Jason Gerlowski03af2de912c3
diff --git a/solr/core/src/java/org/apache/solr/handler/admin/api/CollectionStatusAPI.java b/solr/core/src/java/org/apache/solr/handler/admin/api/CollectionStatusAPI.javanew file mode 100644index 00000000000..123d53a3af8--- /dev/null+++ b/solr/core/src/java/org/apache/solr/handler/admin/api/CollectionStatusAPI.java@@ -0,0 +1,61 @@+/*+ * 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 org.apache.solr.api.EndPoint;+import org.apache.solr.common.cloud.ZkStateReader;+import org.apache.solr.common.params.CollectionParams;+import org.apache.solr.handler.admin.CollectionsHandler;+import org.apache.solr.request.SolrQueryRequest;+import org.apache.solr.response.SolrQueryResponse;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++import java.lang.invoke.MethodHandles;++import static org.apache.solr.client.solrj.SolrRequest.METHOD.GET;+import static org.apache.solr.common.params.CommonParams.ACTION;+import static org.apache.solr.common.params.CoreAdminParams.COLLECTION;+import static org.apache.solr.handler.ClusterAPI.wrapParams;+import static org.apache.solr.security.PermissionNameProvider.Name.COLL_READ_PERM;++/**+ * V2 API for displaying basic information about a single collection.+ *+ * This API (GET /v2/collections/collectionName) is analogous to the v1+ * /admin/collections?action=CLUSTERSTATUS&collection=collectionName command.+ */+public class CollectionStatusAPI {+ private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());++ private final CollectionsHandler collectionsHandler;++ public CollectionStatusAPI(CollectionsHandler collectionsHandler) {+ this.collectionsHandler = collectionsHandler;+ }++ @EndPoint(path = {"/c/{collection}", "/collections/{collection}"},+ method = GET,+ permission = COLL_READ_PERM)+ public void getCollectionStatus(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {+ req = wrapParams(req, // 'req' can have a 'shard' param+ ACTION, CollectionParams.CollectionAction.CLUSTERSTATUS.toString(),+ COLLECTION, req.getPathTemplateValues().get(ZkStateReader.COLLECTION_PROP));+ collectionsHandler.handleRequestBody(req, rsp);+ }+}diff --git a/solr/core/src/java/org/apache/solr/handler/api/ApiRegistrar.java b/solr/core/src/java/org/apache/solr/handler/api/ApiRegistrar.javaindex 5e2a240b42f..33d9a25df77 100644--- a/solr/core/src/java/org/apache/solr/handler/api/ApiRegistrar.java+++ b/solr/core/src/java/org/apache/solr/handler/api/ApiRegistrar.java@@ -19,16 +19,7 @@ package org.apache.solr.handler.api;import org.apache.solr.api.ApiBag;import org.apache.solr.handler.admin.CollectionsHandler;-import org.apache.solr.handler.admin.api.AddReplicaPropertyAPI;-import org.apache.solr.handler.admin.api.BalanceShardUniqueAPI;-import org.apache.solr.handler.admin.api.DeleteCollectionAPI;-import org.apache.solr.handler.admin.api.DeleteReplicaPropertyAPI;-import org.apache.solr.handler.admin.api.MigrateDocsAPI;-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.SetCollectionPropertyAPI;+import org.apache.solr.handler.admin.api.*;/*** Registers annotation-based V2 APIs with an {@link ApiBag}@@ -50,5 +41,6 @@ public class ApiRegistrar {apiBag.registerObject(new RebalanceLeadersAPI(collectionsHandler));apiBag.registerObject(new ReloadCollectionAPI(collectionsHandler));apiBag.registerObject(new SetCollectionPropertyAPI(collectionsHandler));+ apiBag.registerObject(new CollectionStatusAPI(collectionsHandler));}}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.javaindex c6b5d709fc7..c2a13f7a589 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@@ -47,6 +47,7 @@ import static org.apache.solr.common.params.CollectionAdminParams.COLL_CONF;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.common.params.CoreAdminParams.SHARD;import static org.mockito.ArgumentMatchers.any;import static org.mockito.Mockito.mock;import static org.mockito.Mockito.verify;@@ -83,6 +84,15 @@ public class V2CollectionAPIMappingTest extends SolrTestCaseJ4 {ApiRegistrar.registerCollectionApis(apiBag, mockCollectionsHandler);}+ @Test+ public void testGetCollectionStatus() throws Exception {+ final SolrParams v1Params = captureConvertedV1Params("/collections/collName", "GET", Map.of(SHARD, new String[]{"shard2"}));++ assertEquals(CollectionParams.CollectionAction.CLUSTERSTATUS.toString(), v1Params.get(ACTION));+ assertEquals("collName", v1Params.get(COLLECTION));+ assertEquals("shard2", v1Params.get(SHARD));+ }+@Testpublic void testModifyCollectionAllProperties() throws Exception {final SolrParams v1Params = captureConvertedV1Params("/collections/collName", "POST",@@ -264,6 +274,33 @@ public class V2CollectionAPIMappingTest extends SolrTestCaseJ4 {};+ api.call(req, rsp);+ verify(mockCollectionsHandler).handleRequestBody(queryRequestCaptor.capture(), any());+ return queryRequestCaptor.getValue().getParams();+ }++ private SolrParams captureConvertedV1Params(String path, String method, Map<String, String[]> queryParams) throws Exception {+ final HashMap<String, String> parts = new HashMap<>();+ final Api api = apiBag.lookup(path, method, parts);+ final SolrQueryResponse rsp = new SolrQueryResponse();+ final LocalSolrQueryRequest req = new LocalSolrQueryRequest(null, queryParams) {+ @Override+ public List<CommandOperation> getCommands(boolean validateInput) {+ return Collections.emptyList();+ }++ @Override+ public Map<String, String> getPathTemplateValues() {+ return parts;+ }++ @Override+ public String getHttpMethod() {+ return method;+ }+ };++api.call(req, rsp);verify(mockCollectionsHandler).handleRequestBody(queryRequestCaptor.capture(), any());return queryRequestCaptor.getValue().getParams();diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/request/CollectionApiMapping.java b/solr/solrj/src/java/org/apache/solr/client/solrj/request/CollectionApiMapping.javaindex 500200d072e..7fe8bed31f1 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/request/CollectionApiMapping.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/request/CollectionApiMapping.java@@ -45,7 +45,6 @@ import static org.apache.solr.common.params.CollectionParams.CollectionAction.*;public class CollectionApiMapping {public enum Meta implements CommandMeta {- GET_A_COLLECTION(COLLECTION_STATE, GET, CLUSTERSTATUS),CREATE_SHARD(PER_COLLECTION_SHARDS_COMMANDS,POST,CREATESHARD,@@ -199,7 +198,6 @@ public class CollectionApiMapping {}public enum EndPoint implements V2EndPoint {- COLLECTION_STATE("collections.collection"),PER_COLLECTION_SHARDS_COMMANDS("collections.collection.shards.Commands"),PER_COLLECTION_PER_SHARD_COMMANDS("collections.collection.shards.shard.Commands"),PER_COLLECTION_PER_SHARD_DELETE("collections.collection.shards.shard.delete"),diff --git a/solr/solrj/src/resources/apispec/collections.collection.json b/solr/solrj/src/resources/apispec/collections.collection.jsondeleted file mode 100644index 4c2d4d46018..00000000000--- a/solr/solrj/src/resources/apispec/collections.collection.json+++ /dev/null@@ -1,19 +0,0 @@-{- "documentation": "https://lucene.apache.org/solr/guide/collection-management.html#list",- "description": "Lists all collections, with details on shards and replicas in each collection.",- "methods": [- "GET"- ],- "url": {- "paths": [- "/collections/{collection}",- "/c/{collection}",- "/collections/{collection}/shards",- "/c/{collection}/shards",- "/collections/{collection}/shards/{shard}",- "/c/{collection}/shards/{shard}",- "/collections/{collection}/shards/{shard}/{replica}",- "/c/{collection}/shards/{shard}/{replica}"- ]- }-}