Apache Solr open-source search software
SOLR-16503: Use Http2SolrClient in SolrClientCache, SchemaDesigner (#2764)
Sanjay Dutt1bb9b84b6c43
diff --git a/solr/core/src/java/org/apache/solr/core/CoreContainer.java b/solr/core/src/java/org/apache/solr/core/CoreContainer.javaindex c709f4e6a21..d372df6d1bd 100644--- a/solr/core/src/java/org/apache/solr/core/CoreContainer.java+++ b/solr/core/src/java/org/apache/solr/core/CoreContainer.java@@ -834,7 +834,7 @@ public class CoreContainer {solrClientProvider =new HttpSolrClientProvider(cfg.getUpdateShardHandlerConfig(), solrMetricsContext);updateShardHandler.initializeMetrics(solrMetricsContext, "updateShardHandler");- solrClientCache = new SolrClientCache(updateShardHandler.getDefaultHttpClient());+ solrClientCache = new SolrClientCache(solrClientProvider.getSolrClient());Map<String, CacheConfig> cachesConfig = cfg.getCachesConfig();if (cachesConfig.isEmpty()) {diff --git a/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerAPI.java b/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerAPI.javaindex e1589c192c0..d0c7c9d3759 100644--- a/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerAPI.java+++ b/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerAPI.java@@ -353,7 +353,7 @@ public class SchemaDesignerAPI implements SchemaDesignerConstants {}if (textValue != null) {- Map<String, Object> analysis = configSetHelper.analyzeField(configSet, fieldName, textValue);+ var analysis = configSetHelper.analyzeField(configSet, fieldName, textValue);rsp.getValues().addAll(Map.of(idField, docId, fieldName, textValue, "analysis", analysis));}}diff --git a/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerConfigSetHelper.java b/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerConfigSetHelper.javaindex b6afd8a4f11..4a928a4f225 100644--- a/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerConfigSetHelper.java+++ b/solr/core/src/java/org/apache/solr/handler/designer/SchemaDesignerConfigSetHelper.java@@ -18,7 +18,6 @@package org.apache.solr.handler.designer;import static org.apache.solr.common.params.CommonParams.VERSION_FIELD;-import static org.apache.solr.common.util.Utils.fromJSONString;import static org.apache.solr.common.util.Utils.toJavabin;import static org.apache.solr.handler.admin.ConfigSetsHandler.DEFAULT_CONFIGSET_NAME;import static org.apache.solr.handler.designer.SchemaDesignerAPI.getConfigSetZkPath;@@ -31,8 +30,6 @@ import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.lang.invoke.MethodHandles;-import java.net.URI;-import java.net.URISyntaxException;import java.nio.charset.StandardCharsets;import java.nio.file.FileVisitResult;import java.nio.file.Files;@@ -57,33 +54,31 @@ import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;import org.apache.commons.io.FilenameUtils;import org.apache.commons.io.file.PathUtils;-import org.apache.http.HttpResponse;-import org.apache.http.HttpStatus;-import org.apache.http.client.methods.HttpGet;-import org.apache.http.client.methods.HttpPost;-import org.apache.http.client.utils.URIBuilder;-import org.apache.http.client.utils.URLEncodedUtils;-import org.apache.http.entity.ByteArrayEntity;-import org.apache.http.util.EntityUtils;import org.apache.lucene.util.IOSupplier;+import org.apache.solr.client.solrj.SolrRequest;import org.apache.solr.client.solrj.SolrResponse;import org.apache.solr.client.solrj.SolrServerException;-import org.apache.solr.client.solrj.impl.CloudLegacySolrClient;+import org.apache.solr.client.solrj.impl.BinaryResponseParser;import org.apache.solr.client.solrj.impl.CloudSolrClient;+import org.apache.solr.client.solrj.impl.InputStreamResponseParser;+import org.apache.solr.client.solrj.impl.JsonMapResponseParser;import org.apache.solr.client.solrj.request.CollectionAdminRequest;+import org.apache.solr.client.solrj.request.GenericSolrRequest;import org.apache.solr.client.solrj.request.schema.FieldTypeDefinition;import org.apache.solr.client.solrj.request.schema.SchemaRequest;import org.apache.solr.client.solrj.response.schema.SchemaResponse;import org.apache.solr.cloud.ZkConfigSetService;import org.apache.solr.cloud.ZkSolrResourceLoader;import org.apache.solr.common.SolrException;+import org.apache.solr.common.SolrException.ErrorCode;import org.apache.solr.common.SolrInputDocument;import org.apache.solr.common.cloud.DocCollection;import org.apache.solr.common.cloud.Replica;import org.apache.solr.common.cloud.SolrZkClient;import org.apache.solr.common.cloud.ZkMaintenanceUtils;import org.apache.solr.common.cloud.ZkStateReader;-import org.apache.solr.common.params.CommonParams;+import org.apache.solr.common.params.ModifiableSolrParams;+import org.apache.solr.common.util.IOUtils;import org.apache.solr.common.util.NamedList;import org.apache.solr.common.util.SimpleOrderedMap;import org.apache.solr.common.util.Utils;@@ -127,43 +122,20 @@ class SchemaDesignerConfigSetHelper implements SchemaDesignerConstants {Map<String, Object> analyzeField(String configSet, String fieldName, String fieldText)throws IOException {final String mutableId = getMutableId(configSet);- final URI uri;+ var solrParams = new ModifiableSolrParams();+ solrParams.set("analysis.showmatch", true);+ solrParams.set("analysis.fieldname", fieldName);+ solrParams.set("analysis.fieldvalue", "POST");+ var request = new GenericSolrRequest(SolrRequest.METHOD.POST, "/analysis/field", solrParams);+ request.withContent(fieldText.getBytes(StandardCharsets.UTF_8), "text/plain");+ request.setRequiresCollection(true);+ request.setResponseParser(new JsonMapResponseParser());try {- uri =- collectionApiEndpoint(mutableId, "analysis", "field")- .setParameter(CommonParams.WT, CommonParams.JSON)- .setParameter("analysis.showmatch", "true")- .setParameter("analysis.fieldname", fieldName)- .setParameter("analysis.fieldvalue", "POST")- .build();- } catch (URISyntaxException e) {- throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);+ var resp = request.process(cloudClient(), mutableId).getResponse();+ return (Map<String, Object>) resp.get("analysis");+ } catch (SolrServerException e) {+ throw new SolrException(ErrorCode.SERVER_ERROR, e);}-- Map<String, Object> analysis = Collections.emptyMap();- HttpPost httpPost = new HttpPost(uri);- httpPost.setHeader("Content-Type", "text/plain");- httpPost.setEntity(new ByteArrayEntity(fieldText.getBytes(StandardCharsets.UTF_8)));- try {- HttpResponse resp = ((CloudLegacySolrClient) cloudClient()).getHttpClient().execute(httpPost);- int statusCode = resp.getStatusLine().getStatusCode();- if (statusCode != HttpStatus.SC_OK) {- throw new SolrException(- SolrException.ErrorCode.getErrorCode(statusCode),- EntityUtils.toString(resp.getEntity(), StandardCharsets.UTF_8));- }-- Map<String, Object> response =- (Map<String, Object>)- fromJSONString(EntityUtils.toString(resp.getEntity(), StandardCharsets.UTF_8));- if (response != null) {- analysis = (Map<String, Object>) response.get("analysis");- }- } finally {- httpPost.releaseConnection();- }-- return analysis;}List<String> listCollectionsForConfig(String configSet) {@@ -511,40 +483,22 @@ class SchemaDesignerConfigSetHelper implements SchemaDesignerConstants {@SuppressWarnings("unchecked")List<SolrInputDocument> getStoredSampleDocs(final String configSet) throws IOException {- List<SolrInputDocument> docs = null;-- final URI uri;- try {- uri =- collectionApiEndpoint(BLOB_STORE_ID, "blob", configSet + "_sample")- .setParameter(CommonParams.WT, "filestream")- .build();- } catch (URISyntaxException e) {- throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);- }-- HttpGet httpGet = new HttpGet(uri);+ var request = new GenericSolrRequest(SolrRequest.METHOD.GET, "/blob/" + configSet + "_sample");+ request.setRequiresCollection(true);+ request.setResponseParser(new InputStreamResponseParser("filestream"));+ InputStream inputStream = null;try {- HttpResponse entity =- ((CloudLegacySolrClient) cloudClient()).getHttpClient().execute(httpGet);- int statusCode = entity.getStatusLine().getStatusCode();- if (statusCode == HttpStatus.SC_OK) {- byte[] bytes = readAllBytes(() -> entity.getEntity().getContent());- if (bytes.length > 0) {- docs = (List<SolrInputDocument>) Utils.fromJavabin(bytes);- }- } else if (statusCode != HttpStatus.SC_NOT_FOUND) {- byte[] bytes = readAllBytes(() -> entity.getEntity().getContent());- throw new IOException(- "Failed to lookup stored docs for "- + configSet- + " due to: "- + new String(bytes, StandardCharsets.UTF_8));- } // else not found is ok+ var resp = request.process(cloudClient(), BLOB_STORE_ID).getResponse();+ inputStream = (InputStream) resp.get("stream");+ var bytes = inputStream.readAllBytes();+ if (bytes.length > 0) {+ return (List<SolrInputDocument>) Utils.fromJavabin(bytes);+ } else return Collections.emptyList();+ } catch (SolrServerException e) {+ throw new IOException("Failed to lookup stored docs for " + configSet + " due to: " + e);} finally {- httpGet.releaseConnection();+ IOUtils.closeQuietly(inputStream);}- return docs != null ? docs : Collections.emptyList();}void storeSampleDocs(final String configSet, List<SolrInputDocument> docs) throws IOException {@@ -561,26 +515,13 @@ class SchemaDesignerConfigSetHelper implements SchemaDesignerConstants {protected void postDataToBlobStore(CloudSolrClient cloudClient, String blobName, byte[] bytes)throws IOException {- final URI uri;- try {- uri = collectionApiEndpoint(BLOB_STORE_ID, "blob", blobName).build();- } catch (URISyntaxException e) {- throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);- }-- HttpPost httpPost = new HttpPost(uri);+ var request = new GenericSolrRequest(SolrRequest.METHOD.POST, "/blob/" + blobName);+ request.withContent(bytes, BinaryResponseParser.BINARY_CONTENT_TYPE);+ request.setRequiresCollection(true);try {- httpPost.setHeader("Content-Type", "application/octet-stream");- httpPost.setEntity(new ByteArrayEntity(bytes));- HttpResponse resp = ((CloudLegacySolrClient) cloudClient).getHttpClient().execute(httpPost);- int statusCode = resp.getStatusLine().getStatusCode();- if (statusCode != HttpStatus.SC_OK) {- throw new SolrException(- SolrException.ErrorCode.getErrorCode(statusCode),- EntityUtils.toString(resp.getEntity(), StandardCharsets.UTF_8));- }- } finally {- httpPost.releaseConnection();+ request.process(cloudClient, BLOB_STORE_ID);+ } catch (SolrServerException e) {+ throw new SolrException(ErrorCode.SERVER_ERROR, e);}}@@ -607,19 +548,6 @@ class SchemaDesignerConfigSetHelper implements SchemaDesignerConstants {return baseUrl;}- private URIBuilder collectionApiEndpoint(- final String collection, final String... morePathSegments) throws URISyntaxException {- URI baseUrl = new URI(getBaseUrl(collection));- // build up a list of path segments including any path in the base URL, collection, and- // additional segments provided by caller- List<String> path = new ArrayList<>(URLEncodedUtils.parsePathSegments(baseUrl.getPath()));- path.add(collection);- if (morePathSegments != null && morePathSegments.length > 0) {- path.addAll(Arrays.asList(morePathSegments));- }- return new URIBuilder(baseUrl).setPathSegments(path);- }-protected String getManagedSchemaZkPath(final String configSet) {return getConfigSetZkPath(configSet, DEFAULT_MANAGED_SCHEMA_RESOURCE_NAME);}diff --git a/solr/core/src/test/org/apache/solr/handler/designer/TestSchemaDesignerConfigSetHelper.java b/solr/core/src/test/org/apache/solr/handler/designer/TestSchemaDesignerConfigSetHelper.javaindex 7d94f185e44..f1eea1de702 100644--- a/solr/core/src/test/org/apache/solr/handler/designer/TestSchemaDesignerConfigSetHelper.java+++ b/solr/core/src/test/org/apache/solr/handler/designer/TestSchemaDesignerConfigSetHelper.java@@ -36,6 +36,7 @@ import org.apache.solr.client.solrj.request.CollectionAdminRequest;import org.apache.solr.cloud.SolrCloudTestCase;import org.apache.solr.common.SolrInputDocument;import org.apache.solr.common.util.SimpleOrderedMap;+import org.apache.solr.common.util.Utils;import org.apache.solr.core.CoreContainer;import org.apache.solr.core.SolrConfig;import org.apache.solr.schema.FieldType;@@ -283,10 +284,9 @@ public class TestSchemaDesignerConfigSetHelper extends SolrCloudTestCaseMap<String, Object> analysis =helper.analyzeField(configSet, "title", "The Pillars of the Earth");- Map<String, Object> title =- (Map<String, Object>) ((Map<String, Object>) analysis.get("field_names")).get("title");- assertNotNull(title);- List<Object> index = (List<Object>) title.get("index");+ var index =+ (List<Object>)+ Utils.getObjectByPath(analysis, false, List.of("field_names", "title", "index"));assertNotNull(index);assertFalse(index.isEmpty());}