Apache Solr open-source search software
SOLR-15182: replica API conversion to annotations (#429)
Jason Gerlowskibf3976529e06
diff --git a/solr/core/src/java/org/apache/solr/handler/admin/BaseHandlerApiSupport.java b/solr/core/src/java/org/apache/solr/handler/admin/BaseHandlerApiSupport.javaindex 9d65f814d60..4ca7f41424b 100644--- a/solr/core/src/java/org/apache/solr/handler/admin/BaseHandlerApiSupport.java+++ b/solr/core/src/java/org/apache/solr/handler/admin/BaseHandlerApiSupport.java@@ -30,8 +30,8 @@ import com.google.common.collect.ImmutableList;import org.apache.solr.api.Api;import org.apache.solr.api.ApiSupport;import org.apache.solr.client.solrj.SolrRequest;-import org.apache.solr.client.solrj.request.CollectionApiMapping.CommandMeta;-import org.apache.solr.client.solrj.request.CollectionApiMapping.V2EndPoint;+import org.apache.solr.client.solrj.request.ApiMapping.CommandMeta;+import org.apache.solr.client.solrj.request.ApiMapping.V2EndPoint;import org.apache.solr.common.SolrException;import org.apache.solr.common.params.SolrParams;import org.apache.solr.common.util.CommandOperation;diff --git a/solr/core/src/java/org/apache/solr/handler/admin/CollectionHandlerApi.java b/solr/core/src/java/org/apache/solr/handler/admin/CollectionHandlerApi.javadeleted file mode 100644index fe856df7b0e..00000000000--- a/solr/core/src/java/org/apache/solr/handler/admin/CollectionHandlerApi.java+++ /dev/null@@ -1,77 +0,0 @@-/*- * 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;--import java.util.Arrays;-import java.util.Collection;-import java.util.EnumMap;-import java.util.List;-import java.util.Map;--import org.apache.solr.client.solrj.request.CollectionApiMapping;-import org.apache.solr.client.solrj.request.CollectionApiMapping.CommandMeta;-import org.apache.solr.client.solrj.request.CollectionApiMapping.Meta;-import org.apache.solr.client.solrj.request.CollectionApiMapping.V2EndPoint;-import org.apache.solr.handler.admin.CollectionsHandler.CollectionOperation;-import org.apache.solr.request.SolrQueryRequest;-import org.apache.solr.response.SolrQueryResponse;--public class CollectionHandlerApi extends BaseHandlerApiSupport {-- final CollectionsHandler handler;- static Collection<ApiCommand> apiCommands = createCollMapping();-- private static Collection<ApiCommand> createCollMapping() {- Map<Meta, ApiCommand> result = new EnumMap<>(Meta.class);-- for (Meta meta : Meta.values()) {- for (CollectionOperation op : CollectionOperation.values()) {- if (op.action == meta.action) {- result.put(meta, new ApiCommand() {- @Override- public CommandMeta meta() {- return meta;- }-- @Override- public void invoke(SolrQueryRequest req, SolrQueryResponse rsp, BaseHandlerApiSupport apiHandler) throws Exception {- ((CollectionHandlerApi) apiHandler).handler.invokeAction(req, rsp, ((CollectionHandlerApi) apiHandler).handler.coreContainer, op.action, op);- }- });- }- }- }-- return result.values();- }-- public CollectionHandlerApi(CollectionsHandler handler) {- this.handler = handler;- }-- @Override- protected Collection<ApiCommand> getCommands() {- return apiCommands;- }-- @Override- protected List<V2EndPoint> getEndPoints() {- return Arrays.asList(CollectionApiMapping.EndPoint.values());- }--}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.javaindex 0fae3708518..db0753308f4 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@@ -20,7 +20,6 @@ import com.google.common.collect.ImmutableList;import com.google.common.collect.ImmutableSet;import org.apache.commons.io.IOUtils;import org.apache.commons.lang3.StringUtils;-import org.apache.solr.api.Api;import org.apache.solr.client.solrj.SolrResponse;import org.apache.solr.client.solrj.impl.HttpSolrClient;import org.apache.solr.client.solrj.impl.HttpSolrClient.Builder;@@ -109,15 +108,8 @@ import java.util.stream.Collectors;import static org.apache.solr.client.solrj.response.RequestStatusState.*;import static org.apache.solr.cloud.Overseer.QUEUE_OPERATION;-import static org.apache.solr.cloud.api.collections.CollectionHandlingUtils.CREATE_NODE_SET;-import static org.apache.solr.cloud.api.collections.CollectionHandlingUtils.CREATE_NODE_SET_EMPTY;-import static org.apache.solr.cloud.api.collections.CollectionHandlingUtils.CREATE_NODE_SET_SHUFFLE;-import static org.apache.solr.cloud.api.collections.CollectionHandlingUtils.NUM_SLICES;-import static org.apache.solr.cloud.api.collections.CollectionHandlingUtils.ONLY_ACTIVE_NODES;-import static org.apache.solr.cloud.api.collections.CollectionHandlingUtils.ONLY_IF_DOWN;import static org.apache.solr.cloud.api.collections.CollectionHandlingUtils.REQUESTID;-import static org.apache.solr.cloud.api.collections.CollectionHandlingUtils.SHARDS_PROP;-import static org.apache.solr.cloud.api.collections.CollectionHandlingUtils.SHARD_UNIQUE;+import static org.apache.solr.cloud.api.collections.CollectionHandlingUtils.*;import static org.apache.solr.cloud.api.collections.RoutedAlias.CREATE_COLLECTION_PREFIX;import static org.apache.solr.common.SolrException.ErrorCode.BAD_REQUEST;import static org.apache.solr.common.cloud.DocCollection.DOC_ROUTER;@@ -139,7 +131,6 @@ public class CollectionsHandler extends RequestHandlerBase implements Permissionprivate static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());protected final CoreContainer coreContainer;- private final CollectionHandlerApi v2Handler;private final Optional<DistributedCollectionConfigSetCommandRunner> distributedCollectionConfigSetCommandRunner;public CollectionsHandler() {@@ -156,7 +147,6 @@ public class CollectionsHandler extends RequestHandlerBase implements Permission*/public CollectionsHandler(final CoreContainer coreContainer) {this.coreContainer = coreContainer;- v2Handler = new CollectionHandlerApi(this);distributedCollectionConfigSetCommandRunner = coreContainer != null ? coreContainer.getDistributedCollectionCommandRunner() : Optional.empty();}@@ -1564,11 +1554,6 @@ public class CollectionsHandler extends RequestHandlerBase implements Permission}- @Override- public Collection<Api> getApis() {- return v2Handler.getApis();- }-@Overridepublic Boolean registerV2() {return Boolean.TRUE;diff --git a/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandlerApi.java b/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandlerApi.javaindex cb2623d3c01..ca14ba2fd01 100644--- a/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandlerApi.java+++ b/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandlerApi.java@@ -22,8 +22,8 @@ import java.util.Collection;import java.util.EnumMap;import java.util.Map;-import org.apache.solr.client.solrj.request.CollectionApiMapping.CommandMeta;-import org.apache.solr.client.solrj.request.CollectionApiMapping.V2EndPoint;+import org.apache.solr.client.solrj.request.ApiMapping.CommandMeta;+import org.apache.solr.client.solrj.request.ApiMapping.V2EndPoint;import org.apache.solr.client.solrj.request.CoreApiMapping;import org.apache.solr.request.SolrQueryRequest;import org.apache.solr.response.SolrQueryResponse;diff --git a/solr/core/src/java/org/apache/solr/handler/admin/api/DeleteReplicaAPI.java b/solr/core/src/java/org/apache/solr/handler/admin/api/DeleteReplicaAPI.javanew file mode 100644index 00000000000..0b895b0351a--- /dev/null+++ b/solr/core/src/java/org/apache/solr/handler/admin/api/DeleteReplicaAPI.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 static org.apache.solr.client.solrj.SolrRequest.METHOD.DELETE;+import static org.apache.solr.common.params.CommonParams.ACTION;+import static org.apache.solr.common.params.CoreAdminParams.REPLICA;+import static org.apache.solr.common.params.CoreAdminParams.SHARD;+import static org.apache.solr.handler.ClusterAPI.wrapParams;+import static org.apache.solr.security.PermissionNameProvider.Name.COLL_EDIT_PERM;++/**+ * V2 API for deleting an existing replica from a shard.+ *+ * This API (DELETE /v2/collections/collectionName/shards/shardName/replicaName is analogous to the v1+ * /admin/collections?action=DELETEREPLICA command.+ */+public class DeleteReplicaAPI {++ private final CollectionsHandler collectionsHandler;++ public DeleteReplicaAPI(CollectionsHandler collectionsHandler) {+ this.collectionsHandler = collectionsHandler;+ }++ @EndPoint(+ path = {"/c/{collection}/shards/{shard}/{replica}", "/collections/{collection}/shards/{shard}/{replica}"},+ method = DELETE,+ permission = COLL_EDIT_PERM)+ public void deleteReplica(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {+ req = wrapParams(req,+ ACTION, CollectionParams.CollectionAction.DELETEREPLICA.toString(),+ ZkStateReader.COLLECTION_PROP, req.getPathTemplateValues().get(ZkStateReader.COLLECTION_PROP),+ SHARD, req.getPathTemplateValues().get(SHARD),+ REPLICA, req.getPathTemplateValues().get(REPLICA));++ 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 0da03a63ed4..25718f271d1 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@@ -51,5 +51,7 @@ public class ApiRegistrar {apiBag.registerObject(new DeleteShardAPI(collectionsHandler));apiBag.registerObject(new SyncShardAPI(collectionsHandler));apiBag.registerObject(new ForceLeaderAPI(collectionsHandler));+ // really this is a replica API, but since there's only 1 API on the replica path, it's included here for simplicity.+ apiBag.registerObject(new DeleteReplicaAPI(collectionsHandler));}}diff --git a/solr/core/src/test/org/apache/solr/handler/admin/TestApiFramework.java b/solr/core/src/test/org/apache/solr/handler/admin/TestApiFramework.javaindex c9d355cd754..b5f4c7ec6d0 100644--- a/solr/core/src/test/org/apache/solr/handler/admin/TestApiFramework.java+++ b/solr/core/src/test/org/apache/solr/handler/admin/TestApiFramework.java@@ -137,10 +137,6 @@ public class TestApiFramework extends SolrTestCaseJ4 {api = V2HttpCall.getApiInfo(containerHandlers, "/collections/hello/shards/shard1/replica1", "DELETE",null, parts);- assertConditions(api.getSpec(), Map.of(- "/methods[0]", "DELETE",- "/url/params/onlyIfDown/type", "boolean"- ));assertEquals("hello", parts.get("collection"));assertEquals("shard1", parts.get("shard"));assertEquals("replica1", parts.get("replica"));diff --git a/solr/core/src/test/org/apache/solr/handler/admin/TestCollectionAPIs.java b/solr/core/src/test/org/apache/solr/handler/admin/TestCollectionAPIs.javaindex d660178398a..4ef36f8a845 100644--- a/solr/core/src/test/org/apache/solr/handler/admin/TestCollectionAPIs.java+++ b/solr/core/src/test/org/apache/solr/handler/admin/TestCollectionAPIs.java@@ -17,14 +17,6 @@package org.apache.solr.handler.admin;-import java.lang.invoke.MethodHandles;-import java.util.Arrays;-import java.util.Collection;-import java.util.Collections;-import java.util.HashMap;-import java.util.List;-import java.util.Map;-import org.apache.solr.SolrTestCaseJ4;import org.apache.solr.api.Api;import org.apache.solr.api.ApiBag;@@ -51,6 +43,13 @@ import org.junit.Test;import org.slf4j.Logger;import org.slf4j.LoggerFactory;+import java.lang.invoke.MethodHandles;+import java.util.Arrays;+import java.util.Collections;+import java.util.HashMap;+import java.util.List;+import java.util.Map;+import static org.apache.solr.client.solrj.SolrRequest.METHOD.DELETE;import static org.apache.solr.client.solrj.SolrRequest.METHOD.POST;import static org.apache.solr.cloud.Overseer.QUEUE_OPERATION;@@ -90,8 +89,6 @@ public class TestCollectionAPIs extends SolrTestCaseJ4 {apiBag.registerObject(collectionsAPI.collectionsCommands);ApiRegistrar.registerCollectionApis(apiBag, collectionsHandler);ApiRegistrar.registerShardApis(apiBag, collectionsHandler);- Collection<Api> apis = collectionsHandler.getApis();- for (Api api : apis) apiBag.register(api, Collections.emptyMap());ClusterAPI clusterAPI = new ClusterAPI(collectionsHandler,null);apiBag.registerObject(clusterAPI);diff --git a/solr/core/src/test/org/apache/solr/handler/admin/api/V2ShardsAPIMappingTest.java b/solr/core/src/test/org/apache/solr/handler/admin/api/V2ShardsAPIMappingTest.javaindex b7da82babfe..07b7e81849a 100644--- a/solr/core/src/test/org/apache/solr/handler/admin/api/V2ShardsAPIMappingTest.java+++ b/solr/core/src/test/org/apache/solr/handler/admin/api/V2ShardsAPIMappingTest.java@@ -36,17 +36,18 @@ import org.junit.BeforeClass;import org.junit.Test;import org.mockito.ArgumentCaptor;-import java.util.Collections;-import java.util.HashMap;-import java.util.List;-import java.util.Map;+import java.util.*;import static org.apache.solr.SolrTestCaseJ4.assumeWorkingMockito;+import static org.apache.solr.cloud.api.collections.CollectionHandlingUtils.ONLY_IF_DOWN;import static org.apache.solr.common.cloud.ZkStateReader.*;import static org.apache.solr.common.params.CollectionAdminParams.*;+import static org.apache.solr.common.params.CollectionAdminParams.COLLECTION;+import static org.apache.solr.common.params.CollectionParams.NAME;import static org.apache.solr.common.params.CommonAdminParams.*;import static org.apache.solr.common.params.CommonParams.*;-import static org.apache.solr.common.params.CoreAdminParams.SHARD;+import static org.apache.solr.common.params.CommonParams.ACTION;+import static org.apache.solr.common.params.CoreAdminParams.*;import static org.junit.Assert.assertEquals;import static org.mockito.ArgumentMatchers.any;import static org.mockito.Mockito.mock;@@ -138,7 +139,7 @@ public class V2ShardsAPIMappingTest {"}}}");assertEquals(CollectionParams.CollectionAction.SPLITSHARD.lowerName, v1Params.get(ACTION));- assertEquals("collName", v1Params.get(CollectionAdminParams.COLLECTION));+ assertEquals("collName", v1Params.get(COLLECTION));assertEquals("shard1", v1Params.get(SHARD_ID_PROP));assertEquals("someRangeValues", v1Params.get(CoreAdminParams.RANGES));assertEquals("someSplitKey", v1Params.get(SPLIT_KEY));@@ -173,7 +174,7 @@ public class V2ShardsAPIMappingTest {"}}}");assertEquals(CollectionParams.CollectionAction.CREATESHARD.lowerName, v1Params.get(ACTION));- assertEquals("collName", v1Params.get(CollectionAdminParams.COLLECTION));+ assertEquals("collName", v1Params.get(COLLECTION));assertEquals("shard1", v1Params.get(SHARD_ID_PROP));assertEquals("foo,bar,baz", v1Params.get(CREATE_NODE_SET_PARAM));assertEquals(true, v1Params.getPrimitiveBool(FOLLOW_ALIASES));@@ -210,7 +211,7 @@ public class V2ShardsAPIMappingTest {"}}}");assertEquals(CollectionParams.CollectionAction.ADDREPLICA.lowerName, v1Params.get(ACTION));- assertEquals("collName", v1Params.get(CollectionAdminParams.COLLECTION));+ assertEquals("collName", v1Params.get(COLLECTION));assertEquals("shard1", v1Params.get(SHARD_ID_PROP));assertEquals("someRouteValue", v1Params.get("_route_"));assertEquals("someNodeValue", v1Params.get("node"));@@ -228,6 +229,31 @@ public class V2ShardsAPIMappingTest {assertEquals("bar1", v1Params.get("property.bar"));}+ // really this is a replica API, but since there's only 1 API on the replica path, it's included here for simplicity.+ @Test+ public void testDeleteReplicaAllProperties() throws Exception {+ final ModifiableSolrParams v2QueryParams = new ModifiableSolrParams();+ v2QueryParams.add("deleteIndex", "true");+ v2QueryParams.add("deleteDataDir", "true");+ v2QueryParams.add("deleteInstanceDir", "true");+ v2QueryParams.add("followAliases", "true");+ v2QueryParams.add("count", "4");+ v2QueryParams.add("onlyIfDown", "true");+ final SolrParams v1Params = captureConvertedV1Params("/collections/collName/shards/shard1/someReplica",+ "DELETE", v2QueryParams);++ assertEquals(CollectionParams.CollectionAction.DELETEREPLICA.lowerName, v1Params.get(ACTION).toLowerCase(Locale.ROOT));+ assertEquals("collName", v1Params.get(COLLECTION));+ assertEquals("shard1", v1Params.get(SHARD_ID_PROP));+ assertEquals("someReplica", v1Params.get(REPLICA));+ assertEquals("true", v1Params.get(DELETE_INDEX));+ assertEquals("true", v1Params.get(DELETE_DATA_DIR));+ assertEquals("true", v1Params.get(DELETE_INSTANCE_DIR));+ assertEquals("true", v1Params.get(FOLLOW_ALIASES));+ assertEquals("4", v1Params.get(COUNT_PROP));+ assertEquals("true", v1Params.get(ONLY_IF_DOWN));+ }+private SolrParams captureConvertedV1Params(String path, String method, SolrParams queryParams) throws Exception {return captureConvertedV1Params(path, method, queryParams, null);}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/request/ApiMapping.java b/solr/solrj/src/java/org/apache/solr/client/solrj/request/ApiMapping.javanew file mode 100644index 00000000000..6ffffee81ef--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/request/ApiMapping.java@@ -0,0 +1,77 @@+/*+ * 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;++import org.apache.solr.client.solrj.SolrRequest;+import org.apache.solr.common.util.CommandOperation;++import java.util.*;++/**+ * Types and utilities used by many ApiMapping classes.+ */+public class ApiMapping {+ public interface V2EndPoint {++ String getSpecName();+ }++ public interface CommandMeta {+ String getName();++ /**+ * the http method supported by this command+ */+ SolrRequest.METHOD getHttpMethod();++ V2EndPoint getEndPoint();++ default Iterator<String> getParamNamesIterator(CommandOperation op) {+ return getParamNames_(op, CommandMeta.this).iterator();+ }++ /** Given a v1 param, return the v2 attribute (possibly a dotted path). */+ default String getParamSubstitute(String name) {+ return name;+ }+ }++ private static Collection<String> getParamNames_(CommandOperation op, CommandMeta command) {+ Object o = op.getCommandData();+ if (o instanceof Map) {+ @SuppressWarnings("unchecked")+ Map<String,Object> map = (Map<String, Object>) o;+ List<String> result = new ArrayList<>();+ collectKeyNames(map, result, "");+ return result;+ } else {+ return Collections.emptySet();+ }+ }++ @SuppressWarnings({"unchecked"})+ public static void collectKeyNames(Map<String, Object> map, List<String> result, String prefix) {+ for (Map.Entry<String, Object> e : map.entrySet()) {+ if (e.getValue() instanceof Map) {+ collectKeyNames((Map<String, Object>) e.getValue(), result, prefix + e.getKey() + ".");+ } else {+ result.add(prefix + e.getKey());+ }+ }+ }+}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.javadeleted file mode 100644index 3981c08855d..00000000000--- a/solr/solrj/src/java/org/apache/solr/client/solrj/request/CollectionApiMapping.java+++ /dev/null@@ -1,220 +0,0 @@-/*- * 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;---import org.apache.solr.client.solrj.SolrRequest;-import org.apache.solr.common.params.CollectionParams.CollectionAction;-import org.apache.solr.common.util.CommandOperation;-import org.apache.solr.common.util.Pair;--import java.util.*;-import java.util.stream.Stream;--import static org.apache.solr.client.solrj.SolrRequest.METHOD.DELETE;-import static org.apache.solr.client.solrj.request.CollectionApiMapping.EndPoint.PER_COLLECTION_PER_SHARD_PER_REPLICA_DELETE;-import static org.apache.solr.common.params.CollectionParams.CollectionAction.DELETEREPLICA;--/**- * Stores the mapping of v1 API parameters to v2 API parameters- * for the collection API and the configset API.- */-public class CollectionApiMapping {-- public enum Meta implements CommandMeta {- DELETE_REPLICA(PER_COLLECTION_PER_SHARD_PER_REPLICA_DELETE, DELETE, DELETEREPLICA);-- public final String commandName;- public final EndPoint endPoint;- public final SolrRequest.METHOD method;- public final CollectionAction action;-- //bi-directional mapping of v1 http param name to v2 json attribute- public final Map<String, String> paramsToAttrs; // v1 -> v2- public final Map<String, String> attrsToParams; // v2 -> v1- //mapping of old prefix to new for instance properties.a=val can be substituted with property:{a:val}- public final Map<String, String> prefixParamsToAttrs; // v1 -> v2-- public SolrRequest.METHOD getMethod() {- return method;- }--- Meta(EndPoint endPoint, SolrRequest.METHOD method, CollectionAction action) {- this(endPoint, method, action, null, null);- }-- Meta(EndPoint endPoint, SolrRequest.METHOD method, CollectionAction action,- String commandName,- Map<String, String> paramsToAttrs) {- this(endPoint, method, action, commandName, paramsToAttrs, Collections.emptyMap());- }-- Meta(EndPoint endPoint, SolrRequest.METHOD method, CollectionAction action,- String commandName,- Map<String, String> paramsToAttrs,- Map<String, String> prefixParamsToAttrs) {- this.action = action;- this.commandName = commandName;- this.endPoint = endPoint;- this.method = method;-- this.paramsToAttrs = paramsToAttrs == null ? Collections.emptyMap() : Collections.unmodifiableMap(paramsToAttrs);- this.attrsToParams = Collections.unmodifiableMap(reverseMap(this.paramsToAttrs));- this.prefixParamsToAttrs = prefixParamsToAttrs == null ? Collections.emptyMap() : Collections.unmodifiableMap(prefixParamsToAttrs);- }-- private static Map<String, String> reverseMap(Map<String, String> input) { // swap keys and values- Map<String, String> attrToParams = new HashMap<>(input.size());- for (Map.Entry<String, String> entry :input.entrySet()) {- final String existing = attrToParams.put(entry.getValue(), entry.getKey());- if (existing != null) {- throw new IllegalArgumentException("keys and values must collectively be unique");- }- }- return attrToParams;- }-- @Override- public String getName() {- return commandName;- }-- @Override- public SolrRequest.METHOD getHttpMethod() {- return method;- }-- @Override- public V2EndPoint getEndPoint() {- return endPoint;- }-- // Returns iterator of v1 "params".- @Override- public Iterator<String> getParamNamesIterator(CommandOperation op) {- Collection<String> paramNames = getParamNames_(op, this);- Stream<String> pStream = paramNames.stream();- if (!attrsToParams.isEmpty()) {- pStream = pStream.map(paramName -> attrsToParams.getOrDefault(paramName, paramName));- }- if (!prefixParamsToAttrs.isEmpty()) {- pStream = pStream.map(paramName -> {- for (Map.Entry<String, String> e : prefixParamsToAttrs.entrySet()) {- final String prefixV1 = e.getKey();- final String prefixV2 = e.getValue();- if (paramName.startsWith(prefixV2)) {- return prefixV1 + paramName.substring(prefixV2.length()); // replace- }- }- return paramName;- });- }- return pStream.iterator();- }-- // returns params (v1) from an underlying v2, with param (v1) input- @Override- public String getParamSubstitute(String param) {//input is v1- for (Map.Entry<String, String> e : prefixParamsToAttrs.entrySet()) {- final String prefixV1 = e.getKey();- final String prefixV2 = e.getValue();- if (param.startsWith(prefixV1)) {- return prefixV2 + param.substring(prefixV1.length()); // replace- }- }- return paramsToAttrs.getOrDefault(param, param);- }-- // TODO document!- public Object getReverseParamSubstitute(String param) {//input is v1- for (Map.Entry<String, String> e : prefixParamsToAttrs.entrySet()) {- final String prefixV1 = e.getKey();- final String prefixV2 = e.getValue();- if (param.startsWith(prefixV1)) {- return new Pair<>(prefixV2.substring(0, prefixV2.length() - 1), param.substring(prefixV1.length()));- }- }- return paramsToAttrs.getOrDefault(param, param);- }-- }-- public enum EndPoint implements V2EndPoint {- PER_COLLECTION_PER_SHARD_PER_REPLICA_DELETE("collections.collection.shards.shard.replica.delete");- final String specName;--- EndPoint(String specName) {- this.specName = specName;- }-- @Override- public String getSpecName() {- return specName;- }- }-- public interface V2EndPoint {-- String getSpecName();- }-- private static Collection<String> getParamNames_(CommandOperation op, CommandMeta command) {- Object o = op.getCommandData();- if (o instanceof Map) {- @SuppressWarnings("unchecked")- Map<String,Object> map = (Map<String, Object>) o;- List<String> result = new ArrayList<>();- collectKeyNames(map, result, "");- return result;- } else {- return Collections.emptySet();- }- }-- @SuppressWarnings({"unchecked"})- public static void collectKeyNames(Map<String, Object> map, List<String> result, String prefix) {- for (Map.Entry<String, Object> e : map.entrySet()) {- if (e.getValue() instanceof Map) {- collectKeyNames((Map<String, Object>) e.getValue(), result, prefix + e.getKey() + ".");- } else {- result.add(prefix + e.getKey());- }- }- }- public interface CommandMeta {- String getName();-- /**- * the http method supported by this command- */- SolrRequest.METHOD getHttpMethod();-- V2EndPoint getEndPoint();-- default Iterator<String> getParamNamesIterator(CommandOperation op) {- return getParamNames_(op, CommandMeta.this).iterator();- }-- /** Given a v1 param, return the v2 attribute (possibly a dotted path). */- default String getParamSubstitute(String name) {- return name;- }- }-}diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/request/CoreApiMapping.java b/solr/solrj/src/java/org/apache/solr/client/solrj/request/CoreApiMapping.javaindex 74dd466dfe4..89ae69165cd 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/request/CoreApiMapping.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/request/CoreApiMapping.java@@ -22,7 +22,7 @@ import java.util.Collections;import java.util.Map;import org.apache.solr.client.solrj.SolrRequest;-import org.apache.solr.client.solrj.request.CollectionApiMapping.CommandMeta;+import org.apache.solr.client.solrj.request.ApiMapping.CommandMeta;import org.apache.solr.common.params.CoreAdminParams.CoreAdminAction;import static org.apache.solr.client.solrj.SolrRequest.METHOD.GET;@@ -83,7 +83,7 @@ public class CoreApiMapping {}@Override- public CollectionApiMapping.V2EndPoint getEndPoint() {+ public ApiMapping.V2EndPoint getEndPoint() {return endPoint;}@@ -95,7 +95,7 @@ public class CoreApiMapping {}- public enum EndPoint implements CollectionApiMapping.V2EndPoint {+ public enum EndPoint implements ApiMapping.V2EndPoint {CORES_STATUS("cores.Status"),CORES_COMMANDS("cores.Commands"),PER_CORE_COMMANDS("cores.core.Commands"),diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/request/V1toV2ApiMapper.java b/solr/solrj/src/java/org/apache/solr/client/solrj/request/V1toV2ApiMapper.javadeleted file mode 100644index 27ede3feb52..00000000000--- a/solr/solrj/src/java/org/apache/solr/client/solrj/request/V1toV2ApiMapper.java+++ /dev/null@@ -1,145 +0,0 @@-/*- * 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;--import java.util.EnumMap;-import java.util.HashMap;-import java.util.Iterator;-import java.util.LinkedHashMap;-import java.util.List;-import java.util.Map;--import org.apache.solr.client.solrj.SolrRequest;-import org.apache.solr.common.MapWriter;-import org.apache.solr.common.params.CollectionParams.CollectionAction;-import org.apache.solr.common.params.CoreAdminParams;-import org.apache.solr.common.params.SolrParams;-import org.apache.solr.common.util.JsonSchemaValidator;-import org.apache.solr.common.util.Pair;-import org.apache.solr.common.util.Template;-import org.apache.solr.common.util.Utils;-import org.apache.solr.common.util.ValidatingJsonMap;--import static org.apache.solr.common.util.ValidatingJsonMap.NOT_NULL;---public class V1toV2ApiMapper {-- private static EnumMap<CollectionAction, ActionInfo> mapping = new EnumMap<>(CollectionAction.class);-- static {- for (CollectionApiMapping.Meta meta : CollectionApiMapping.Meta.values()) {- if (meta.action != null) mapping.put(meta.action, new ActionInfo(meta));- }- }-- private static class ActionInfo {- CollectionApiMapping.Meta meta;- String path;- Template template;--- JsonSchemaValidator validator;-- ActionInfo(CollectionApiMapping.Meta meta) {- this.meta = meta;- }-- //do this lazily because , it makes no sense if this is not used- synchronized void setPath() {- if (path == null) {- ValidatingJsonMap m = Utils.getSpec(meta.getEndPoint().getSpecName()).getSpec();- Object o = Utils.getObjectByPath(m, false, "url/paths");-- String result = null;- if (o instanceof List) {//choose the shortest path- for (Object s : (List) o) {- if (result == null || s.toString().length() < result.length()) result = s.toString();- }- } else if (o instanceof String) {- result = (String) o;- }- path = result;- template = new Template(path, Template.BRACES_PLACEHOLDER_PATTERN);-- validator = new JsonSchemaValidator(m.getMap("commands", NOT_NULL).getMap(meta.commandName, NOT_NULL));- }- }-- public V2Request.Builder convert(SolrParams paramsV1) {- String[] list = new String[template.variables.size()];- MapWriter data = serializeToV2Format(paramsV1, list);- Map<String, Object> o = data.toMap(new LinkedHashMap<>());- return new V2Request.Builder(template.apply(s -> {- int idx = template.variables.indexOf(s);- return list[idx];- }))- .withMethod(meta.getHttpMethod())- .withPayload(o);-- }-- private MapWriter serializeToV2Format(SolrParams paramsV1, String[] list) {- return ew -> ew.put(meta.commandName, (MapWriter) ew1 -> {- Iterator<String> iter = paramsV1.getParameterNamesIterator();- Map<String, Map<String, String>> subProperties = null;- while (iter.hasNext()) {- String key = iter.next();- if (CoreAdminParams.ACTION.equals(key)) continue;- Object substitute = meta.getReverseParamSubstitute(key);- int idx = template.variables.indexOf(substitute);- if (idx > -1) {- String val = paramsV1.get(key);- if (val == null) throw new RuntimeException("null value is not valid for " + key);- list[idx] = val;- continue;- }- if (substitute instanceof Pair) {//this is a nested object- @SuppressWarnings("unchecked")- Pair<String, String> p = (Pair<String, String>) substitute;- if (subProperties == null) subProperties = new HashMap<>();- subProperties.computeIfAbsent(p.first(), s -> new HashMap<>()).put(p.second(), paramsV1.get(key));- } else {- Object val = paramsV1.get(key);- ew1.put(substitute.toString(), val);- }- }- if (subProperties != null) {- for (Map.Entry<String, Map<String, String>> e : subProperties.entrySet()) {- ew1.put(e.getKey(), e.getValue());- }- }- });- }- }--- public static V2Request.Builder convert(CollectionAdminRequest<?> request) {- ActionInfo info = mapping.get(request.action);- if (info == null) throw new RuntimeException("Unsupported action :" + request.action);-- if (info.meta.getHttpMethod() == SolrRequest.METHOD.POST) {- if (info.path == null) info.setPath();- return info.convert(request.getParams());- }-- return null;- }---}diff --git a/solr/solrj/src/resources/apispec/collections.collection.shards.shard.replica.delete.json b/solr/solrj/src/resources/apispec/collections.collection.shards.shard.replica.delete.jsondeleted file mode 100644index 16efecb92a0..00000000000--- a/solr/solrj/src/resources/apispec/collections.collection.shards.shard.replica.delete.json+++ /dev/null@@ -1,39 +0,0 @@-{- "documentation": "https://lucene.apache.org/solr/guide/replica-management.html#deletereplica",- "description": "Deletes a replica. If the responding node is up, the core is unloaded, the entry removed from the collection's state.json, and the instanceDir and dataDir removed. If the node is not up, the entry for the replica is removed from state.json; if the nodes comes up later, the replica is automatically de-registered.",- "methods": [- "DELETE"- ],- "url": {- "paths": [- "/collections/{collection}/shards/{shard}/{replica}",- "/c/{collection}/shards/{shard}/{replica}"- ],- "params": {- "onlyIfDown": {- "type": "boolean",- "default": "false",- "description": "When set to 'true', no action will be taken if the replica is active."- },- "deleteIndex": {- "type": "boolean",- "default": "true",- "description": "By default Solr will delete the index of the replica that is deleted. Set this to false to prevent the index directory from being deleted."- },- "deleteDataDir": {- "type": "boolean",- "default": "true",- "description": "By default Solr will delete the dataDir of the replica that is deleted. Set this to false to prevent the data directory from being deleted."- },- "deleteInstanceDir": {- "type": "boolean",- "default": "true",- "description": "By default Solr will delete the entire instanceDir of the replica that is deleted. Set this to false to prevent the instance directory from being deleted."- },- "async":{- "type":"string",- "description":"Defines a request ID that can be used to track this action after it's submitted. The action will be processed asynchronously when this is defined."- }- }- }-}