Copilot commented on code in PR #4073: URL: https://github.com/apache/solr/pull/4073#discussion_r2724122040
########## solr/core/src/java/org/apache/solr/response/BuiltInResponseWriterRegistry.java: ########## @@ -0,0 +1,115 @@ +/* + * 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.response; + +import static org.apache.solr.handler.admin.MetricsHandler.OPEN_METRICS_WT; +import static org.apache.solr.handler.admin.MetricsHandler.PROMETHEUS_METRICS_WT; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import org.apache.solr.common.params.CommonParams; + +/** + * Registry for built-in response writers in Solr. + * + * <p>Manages a minimal set of essential response writers that are always available, regardless of + * core configuration or ImplicitPlugins.json settings. These writers are primarily used by + * admin/container-level requests that have no associated SolrCore. + * + * <p>Built-in writers include essential formats needed by admin APIs and core functionality: + * javabin, json, xml, prometheus, and openmetrics. + * + * <p>For core-specific requests that need access to the full set of response writers (including + * csv, geojson, graphml, smile, etc.), use the SolrCore's response writer registry which is loaded + * from ImplicitPlugins.json and supports ConfigOverlay customizations. + * + * @since 11.0 + */ +public class BuiltInResponseWriterRegistry { + Review Comment: BuiltInResponseWriterRegistry is a pure static utility/registry but it has a public implicit constructor. Add a private constructor to prevent accidental instantiation. ```suggestion private BuiltInResponseWriterRegistry() { // Prevent instantiation } ``` ########## solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java: ########## @@ -735,8 +736,9 @@ protected void logAndFlushAdminRequest(SolrQueryResponse solrResp) throws IOExce solrResp.getToLogAsString("[admin]")); } } + // Admin requests have no core, use built-in writers QueryResponseWriter respWriter = - SolrCore.DEFAULT_RESPONSE_WRITERS.get(solrReq.getParams().get(CommonParams.WT)); + BuiltInResponseWriterRegistry.getWriter(solrReq.getParams().get(CommonParams.WT)); if (respWriter == null) respWriter = getResponseWriter(); Review Comment: HttpSolrCall.logAndFlushAdminRequest() still checks `if (respWriter == null)` after calling BuiltInResponseWriterRegistry.getWriter(). That method currently never returns null (it always falls back to "standard"), so this branch is dead code and can be removed (or change getWriter() contract if null is actually expected). ```suggestion ``` ########## solr/core/src/java/org/apache/solr/core/SolrCore.java: ########## @@ -3148,11 +3116,49 @@ default String getContentType() { } /** - * Configure the query response writers. There will always be a default writer; additional writers - * may also be configured. + * Gets a response writer suitable for admin/container-level requests. + * + * @param writerName the writer name, or null for default + * @return the response writer, never null + * @deprecated Use {@link + * org.apache.solr.response.BuiltInResponseWriterRegistry#getWriter(String)} instead. + */ + @Deprecated + public static QueryResponseWriter getAdminResponseWriter(String writerName) { + return org.apache.solr.response.BuiltInResponseWriterRegistry.getWriter(writerName); + } + + /** + * Initializes query response writers. Response writers from {@code ImplicitPlugins.json} may also + * be configured. */ private void initWriters() { - responseWriters.init(DEFAULT_RESPONSE_WRITERS, this); + // Build default writers map from implicit plugins + Map<String, QueryResponseWriter> defaultWriters = new HashMap<>(); + + // Load writers from ImplicitPlugins.json + List<PluginInfo> implicitWriters = getImplicitResponseWriters(); + for (PluginInfo info : implicitWriters) { + try { + QueryResponseWriter writer = + createInstance( + info.className, + QueryResponseWriter.class, + "queryResponseWriter", + null, + getResourceLoader()); Review Comment: SolrCore.initWriters() instantiates implicit response writers via createInstance(...) but does not run plugin initialization (init args / PluginInfoInitialized / NamedListInitializedPlugin) like other Solr plugins do (see SolrCore.createInitInstance / SolrCore.initPlugin). This can break response writers that expect initialization from PluginInfo. After instantiating, call SolrCore.initPlugin(info, writer) (or use createInitInstance with a PluginInfo) before registering it. ```suggestion getResourceLoader()); initPlugin(info, writer); ``` ########## changelog/unreleased/admin-response-writers-minimal-set.yml: ########## @@ -0,0 +1,22 @@ +# See https://github.com/apache/solr/blob/main/dev-docs/changelog.adoc +title: Introduce minimal ADMIN_RESPONSE_WRITERS for admin/container-level requests. Admin requests now use a minimal set of 6 response writers (javabin, json, xml, prometheus, openmetrics, standard) instead of the full 14-writer DEFAULT_RESPONSE_WRITERS map. Core-specific requests continue to use ImplicitPlugins.json with ConfigOverlay support. +type: changed +authors: + - name: Eric Pugh +links: + - name: GitHub Discussion + url: https://github.com/apache/solr/discussions +notes: | + Admin/container-level requests (e.g., /admin/cores, /admin/collections) have no associated SolrCore + and cannot use the core's response writer registry loaded from ImplicitPlugins.json. Previously, + these requests used DEFAULT_RESPONSE_WRITERS (14 entries). Now they use ADMIN_RESPONSE_WRITERS (6 entries) + containing only the formats actually needed by admin APIs: + - javabin (SolrJ clients) + - json (Admin UI) + - xml (backward compatibility) + - prometheus/openmetrics (metrics endpoint) + - standard (alias for json) + + This provides clearer separation of concerns and better documentation of requirements. Core-specific + requests are unaffected and continue to use the full ImplicitPlugins.json system with ConfigOverlay support. + DEFAULT_RESPONSE_WRITERS remains available but is now deprecated for internal use. Review Comment: The changelog entry refers to an "ADMIN_RESPONSE_WRITERS" name and says "DEFAULT_RESPONSE_WRITERS remains available". In this PR, DEFAULT_RESPONSE_WRITERS is removed from SolrCore and replaced by BuiltInResponseWriterRegistry (and a deprecated SolrCore.getAdminResponseWriter method). Please update this changelog text to match the actual public/internal API surface introduced by the PR so release notes aren’t misleading. ########## solr/core/src/java/org/apache/solr/core/SolrCore.java: ########## @@ -3148,11 +3116,49 @@ default String getContentType() { } /** - * Configure the query response writers. There will always be a default writer; additional writers - * may also be configured. + * Gets a response writer suitable for admin/container-level requests. + * + * @param writerName the writer name, or null for default + * @return the response writer, never null + * @deprecated Use {@link + * org.apache.solr.response.BuiltInResponseWriterRegistry#getWriter(String)} instead. + */ + @Deprecated + public static QueryResponseWriter getAdminResponseWriter(String writerName) { + return org.apache.solr.response.BuiltInResponseWriterRegistry.getWriter(writerName); + } + + /** + * Initializes query response writers. Response writers from {@code ImplicitPlugins.json} may also + * be configured. */ private void initWriters() { - responseWriters.init(DEFAULT_RESPONSE_WRITERS, this); + // Build default writers map from implicit plugins + Map<String, QueryResponseWriter> defaultWriters = new HashMap<>(); + + // Load writers from ImplicitPlugins.json + List<PluginInfo> implicitWriters = getImplicitResponseWriters(); + for (PluginInfo info : implicitWriters) { + try { + QueryResponseWriter writer = + createInstance( + info.className, + QueryResponseWriter.class, + "queryResponseWriter", + null, Review Comment: SolrCore.initWriters() passes `core=null` into createInstance(...) when loading core-level response writers. If any QueryResponseWriter has a SolrCore constructor (or otherwise expects a non-null core), this will either instantiate with a null core or skip the core-aware constructor path. Use `this` (the current SolrCore) instead of null when creating core-specific writers. ```suggestion this, ``` ########## solr/core/src/java/org/apache/solr/core/SolrCore.java: ########## @@ -3148,11 +3116,49 @@ default String getContentType() { } /** - * Configure the query response writers. There will always be a default writer; additional writers - * may also be configured. + * Gets a response writer suitable for admin/container-level requests. + * + * @param writerName the writer name, or null for default + * @return the response writer, never null + * @deprecated Use {@link + * org.apache.solr.response.BuiltInResponseWriterRegistry#getWriter(String)} instead. + */ + @Deprecated + public static QueryResponseWriter getAdminResponseWriter(String writerName) { + return org.apache.solr.response.BuiltInResponseWriterRegistry.getWriter(writerName); + } + + /** + * Initializes query response writers. Response writers from {@code ImplicitPlugins.json} may also + * be configured. */ private void initWriters() { - responseWriters.init(DEFAULT_RESPONSE_WRITERS, this); + // Build default writers map from implicit plugins + Map<String, QueryResponseWriter> defaultWriters = new HashMap<>(); + + // Load writers from ImplicitPlugins.json + List<PluginInfo> implicitWriters = getImplicitResponseWriters(); + for (PluginInfo info : implicitWriters) { + try { + QueryResponseWriter writer = + createInstance( + info.className, + QueryResponseWriter.class, + "queryResponseWriter", + null, + getResourceLoader()); + defaultWriters.put(info.name, writer); + } catch (Exception e) { + log.warn("Failed to load implicit response writer: {}", info.name, e); + } + } + + // Add special filestream writer (custom implementation) + defaultWriters.put(ReplicationAPIBase.FILE_STREAM, getFileStreamWriter()); + + // Initialize with the built defaults + responseWriters.init(defaultWriters, this); + // configure the default response writer; this one should never be null if (responseWriters.getDefault() == null) responseWriters.setDefault("standard"); } Review Comment: SolrCore.initWriters() swallows exceptions when loading implicit response writers (logs warn and continues). If the "standard" writer (or any required default) fails to load, responseWriters may end up with no default (PluginBag#setDefault is a no-op when the name is missing), violating the “Never null” contract of getQueryResponseWriter(..) and potentially causing NPEs later. Consider failing fast when required writers can’t be loaded, or explicitly ensure a default writer is present (e.g., fall back to a built-in JSON writer) before continuing startup. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
