Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SolrRequest.getParams never null; and clarify mutability #3140

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions solr/core/src/java/org/apache/solr/cli/ExportTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@
import org.apache.solr.common.cloud.Slice;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.CursorMarkParams;
import org.apache.solr.common.params.MapSolrParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.CollectionUtil;
import org.apache.solr.common.util.ExecutorUtil;
import org.apache.solr.common.util.JavaBinCodec;
Expand Down Expand Up @@ -251,7 +251,7 @@ void fetchUniqueKey() throws SolrServerException, IOException {
new GenericSolrRequest(
SolrRequest.METHOD.GET,
"/schema/uniquekey",
new MapSolrParams(Collections.singletonMap("collection", coll)))
SolrParams.of("collection", coll))
.setRequiresCollection(true));
uniqueKey = (String) response.get("uniqueKey");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ private void commitOnLeader(String leaderBaseUrl, String coreName)
throws SolrServerException, IOException {
try (SolrClient client = recoverySolrClientBuilder(leaderBaseUrl, coreName).build()) {
UpdateRequest ureq = new UpdateRequest();
ureq.setParams(new ModifiableSolrParams());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was clearly a hack to work around it's weird mutability situation. Now not needed here and elsewhere.

// ureq.getParams().set(DistributedUpdateProcessor.COMMIT_END_POINT, true);
// ureq.getParams().set(UpdateParams.OPEN_SEARCHER, onlyLeaderIndexes);
// Why do we need to open searcher if "onlyLeaderIndexes"?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ static UpdateResponse softCommit(String baseUrl, String coreName)
.withSocketTimeout(120000, TimeUnit.MILLISECONDS)
.build()) {
UpdateRequest ureq = new UpdateRequest();
ureq.setParams(new ModifiableSolrParams());
ureq.setAction(AbstractUpdateRequest.ACTION.COMMIT, false, true, true);
return ureq.process(client);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throw
}
try {
SolrParams params = req.getParams();
assert params != null;
UpdateRequestProcessorChain processorChain = req.getCore().getUpdateProcessorChain(params);

UpdateRequestProcessor processor = processorChain.createProcessor(req, rsp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ static Long getCheckSum(Checksum checksum, Path f) {
private volatile IndexFetcher currentIndexFetcher;

public IndexFetchResult doFetch(SolrParams solrParams, boolean forceReplication) {
String leaderUrl = solrParams == null ? null : solrParams.get(LEADER_URL, null);
String leaderUrl = solrParams.get(LEADER_URL, null);
if (!indexFetchLock.tryLock()) return IndexFetchResult.LOCK_OBTAIN_FAILED;
if (core.getCoreContainer().isShutDown()) {
log.warn("I was asked to replicate but CoreContainer is shutting down");
Expand Down Expand Up @@ -1196,7 +1196,7 @@ private void setupPolling(String intervalStr) {
try {
log.debug("Polling for index modifications");
markScheduledExecutionStart();
IndexFetchResult fetchResult = doFetch(null, false);
IndexFetchResult fetchResult = doFetch(SolrParams.of(), false);
if (pollListener != null) pollListener.onComplete(core, fetchResult);
} catch (Exception e) {
log.error("Exception in fetching index", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
Expand Down Expand Up @@ -53,10 +52,6 @@ public static void addExperimentalFormatWarning(SolrQueryResponse rsp) {
public static boolean handleCommit(
SolrQueryRequest req, UpdateRequestProcessor processor, SolrParams params, boolean force)
throws IOException {
if (params == null) {
params = new MapSolrParams(new HashMap<>());
}

boolean optimize = params.getBool(UpdateParams.OPTIMIZE, false);
boolean commit = params.getBool(UpdateParams.COMMIT, false);
boolean softCommit = params.getBool(UpdateParams.SOFT_COMMIT, false);
Expand Down Expand Up @@ -97,8 +92,6 @@ public static void validateCommitParams(SolrParams params) {

/** Modify UpdateCommand based on request parameters */
public static void updateCommit(CommitUpdateCommand cmd, SolrParams params) {
if (params == null) return;

cmd.openSearcher = params.getBool(UpdateParams.OPEN_SEARCHER, cmd.openSearcher);
cmd.waitSearcher = params.getBool(UpdateParams.WAIT_SEARCHER, cmd.waitSearcher);
cmd.softCommit = params.getBool(UpdateParams.SOFT_COMMIT, cmd.softCommit);
Expand All @@ -114,13 +107,7 @@ public static void updateCommit(CommitUpdateCommand cmd, SolrParams params) {
public static boolean handleRollback(
SolrQueryRequest req, UpdateRequestProcessor processor, SolrParams params, boolean force)
throws IOException {
if (params == null) {
params = new MapSolrParams(new HashMap<>());
}

boolean rollback = params.getBool(UpdateParams.ROLLBACK, false);

if (rollback || force) {
if (force || params.getBool(UpdateParams.ROLLBACK, false)) {
RollbackUpdateCommand cmd = new RollbackUpdateCommand(req);
processor.processRollback(cmd);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.apache.solr.common.SolrException.ErrorCode;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.MapSolrParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.params.UpdateParams;
import org.apache.solr.common.util.ContentStream;
Expand Down Expand Up @@ -124,15 +123,8 @@ public void init(NamedList<?> args) {
}

protected void setAssumeContentType(String ct) {
if (invariants == null) {
Map<String, String> map = new HashMap<>();
map.put(UpdateParams.ASSUME_CONTENT_TYPE, ct);
invariants = new MapSolrParams(map);
} else {
ModifiableSolrParams params = new ModifiableSolrParams(invariants);
params.set(UpdateParams.ASSUME_CONTENT_TYPE, ct);
invariants = params;
}
invariants =
SolrParams.wrapDefaults(SolrParams.of(UpdateParams.ASSUME_CONTENT_TYPE, ct), invariants);
}

private Map<String, ContentStreamLoader> pathVsLoaders = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ public static void squashIntoNamedListWithoutHeader(
}

public static String getMediaTypeFromWtParam(SolrParams params, String defaultMediaType) {
if (params == null) {
dsmiley marked this conversation as resolved.
Show resolved Hide resolved
return defaultMediaType;
}
final String wtParam = params.get(WT);
if (StrUtils.isBlank(wtParam)) return defaultMediaType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ public SampleDocuments parseDocsFromStream(
return SampleDocuments.NONE;
}

if (params == null) {
params = new ModifiableSolrParams();
}

Long streamSize = stream.getSize();
if (streamSize != null && streamSize > MAX_STREAM_SIZE) {
throw new SolrException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static boolean disallowPartialResults(SolrParams params) {
return !allowPartialResults(params);
}

/** returns the current request parameters */
/** The parameters for this request; never null. Use {@link #setParams(SolrParams)} to change. */
SolrParams getParams();

/**
Expand All @@ -88,8 +88,8 @@ static boolean disallowPartialResults(SolrParams params) {
Iterable<ContentStream> getContentStreams();

/**
* Returns the original request parameters. As this does not normally include configured defaults
* it's more suitable for logging.
* The original request parameters; never null. As this does not normally include configured
* defaults, it's more suitable for logging.
*/
SolrParams getOriginalParams();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.apache.solr.api.ApiBag;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.SolrParams;
Expand Down Expand Up @@ -62,8 +63,8 @@ public abstract class SolrQueryRequestBase implements SolrQueryRequest, Closeabl
public SolrQueryRequestBase(SolrCore core, SolrParams params, RTimerTree requestTimer) {
this.core = core;
this.schema = null == core ? null : core.getLatestSchema();
this.params = this.origParams = params;
this.requestTimer = requestTimer;
this.params = this.origParams = Objects.requireNonNull(params);
this.requestTimer = Objects.requireNonNull(requestTimer);
this.startTime = System.currentTimeMillis();
}

Expand All @@ -90,7 +91,7 @@ public SolrParams getOriginalParams() {

@Override
public void setParams(SolrParams params) {
this.params = params;
this.params = Objects.requireNonNull(params);
}

// Get the start time of this request in milliseconds
Expand Down
12 changes: 7 additions & 5 deletions solr/core/src/java/org/apache/solr/search/QParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.apache.lucene.search.Query;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.CommonParams;
Expand Down Expand Up @@ -62,10 +63,11 @@ public abstract class QParser {
/**
* Constructor for the QParser
*
* @param qstr The part of the query string specific to this parser
* @param localParams The set of parameters that are specific to this QParser. See
* @param qstr The query string to be parsed
* @param localParams Params scoped to this query, customizing the parsing. Null if none. Most
* params the parser needs are resolved here first, overlaying the request. See
* https://solr.apache.org/guide/solr/latest/query-guide/local-params.html
* @param params The rest of the {@link org.apache.solr.common.params.SolrParams}
* @param params Params for the request, taken from {@link SolrQueryRequest#getParams()}; not null
* @param req The original {@link org.apache.solr.request.SolrQueryRequest}.
*/
public QParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
Expand Down Expand Up @@ -97,7 +99,7 @@ public QParser(String qstr, SolrParams localParams, SolrParams params, SolrQuery
}
}

this.params = params;
this.params = Objects.requireNonNull(params);
this.req = req;
}

Expand Down Expand Up @@ -160,7 +162,7 @@ public SolrParams getParams() {
}

public void setParams(SolrParams params) {
this.params = params;
this.params = Objects.requireNonNull(params);
}

public SolrQueryRequest getReq() {
Expand Down
6 changes: 5 additions & 1 deletion solr/core/src/java/org/apache/solr/search/QParserPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ public abstract class QParserPlugin implements NamedListInitializedPlugin, SolrI
standardPlugins = Collections.unmodifiableMap(map);
}

/** return a {@link QParser} */
/**
* Creates the {@link QParser}.
*
* @see QParser#QParser(String, SolrParams, SolrParams, SolrQueryRequest)
*/
public abstract QParser createParser(
String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ public class IgnoreCommitOptimizeUpdateProcessorFactory extends UpdateRequestPro

@Override
public void init(final NamedList<?> args) {
SolrParams params = (args != null) ? args.toSolrParams() : null;
if (params == null) {
if (args == null) {
errorCode = ErrorCode.FORBIDDEN; // default is 403 error
responseMsg = DEFAULT_RESPONSE_MSG;
ignoreOptimizeOnly = false;
return;
}
SolrParams params = args.toSolrParams();

ignoreOptimizeOnly = params.getBool("ignoreOptimizeOnly", false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.cloud.Replica;
import org.apache.solr.common.cloud.ZkStateReader;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -241,8 +240,6 @@ protected void indexDoc(Object... fields) throws IOException, SolrServerExceptio

UpdateRequest ureq = new UpdateRequest();
ureq.add(doc);
ModifiableSolrParams params = new ModifiableSolrParams();
ureq.setParams(params);
ureq.process(cloudClient);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.cloud.Replica;
import org.apache.solr.common.cloud.ZkStateReader;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.core.CoreContainer;
import org.apache.solr.metrics.SolrMetricManager;
import org.junit.Test;
Expand Down Expand Up @@ -369,8 +368,6 @@ protected void indexDoc(Object... fields) throws IOException, SolrServerExceptio

UpdateRequest ureq = new UpdateRequest();
ureq.add(doc);
ModifiableSolrParams params = new ModifiableSolrParams();
ureq.setParams(params);
ureq.process(cloudClient);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,11 +625,8 @@ protected long addDocAndGetVersion(Object... fields) throws Exception {
SolrInputDocument doc = new SolrInputDocument();
addFields(doc, fields);

ModifiableSolrParams params = new ModifiableSolrParams();
params.add("versions", "true");

UpdateRequest ureq = new UpdateRequest();
ureq.setParams(params);
ureq.getParams().set("versions", true);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With a clear mutability story, the requirement here is now easy with this one-liner. (same elsewhere)

ureq.add(doc);
UpdateResponse resp;

Expand All @@ -647,10 +644,10 @@ protected long addDocAndGetVersion(Object... fields) throws Exception {

protected long deleteDocAndGetVersion(
String id, ModifiableSolrParams params, boolean deleteByQuery) throws Exception {
params.add("versions", "true");

UpdateRequest ureq = new UpdateRequest();
ureq.setParams(params);
ureq.getParams().add(params);
Comment on lines -653 to +648
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose to call SolrParams.add(SolrParams) here but could have called setParams. A nice effect of this is that we copy the params instead of use in-place. Previously this deleteDocAndGetVersion method actually added params to its argument, which is very bad taste.

ureq.getParams().set("versions", true);

if (deleteByQuery) {
ureq.deleteByQuery("id:" + id);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void setup() throws IOException {

@Test
public void testJson() throws Exception {
loadTestDocs(null, new File(exampleDir, "films/films.json"), 500, 500);
loadTestDocs(SolrParams.of(), new File(exampleDir, "films/films.json"), 500, 500);
}

@Test
Expand All @@ -78,7 +78,7 @@ public void testCsv() throws Exception {

@Test
public void testSolrXml() throws Exception {
loadTestDocs(null, new File(exampleDir, "films/films.xml"), 1000, 1000);
loadTestDocs(SolrParams.of(), new File(exampleDir, "films/films.xml"), 1000, 1000);
}

protected List<SolrInputDocument> loadTestDocs(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ public void read(SolrQueryRequest req, SolrQueryResponse rsp) {

private static SolrQueryResponse v2ApiInvoke(
ApiBag bag, String uri, String method, SolrParams params, InputStream payload) {
if (params == null) params = new ModifiableSolrParams();
SolrQueryResponse rsp = new SolrQueryResponse();
HashMap<String, String> templateVals = new HashMap<>();
Api[] currentApi = new Api[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import jakarta.ws.rs.core.MediaType;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.junit.Test;

public class V2ApiUtilsTest extends SolrTestCaseJ4 {
Expand All @@ -44,9 +45,7 @@ public void testReadsDisableV2ApiSysprop() {

@Test
public void testConvertsWtToMediaTypeString() {
assertEquals(
"someDefault",
V2ApiUtils.getMediaTypeFromWtParam(new ModifiableSolrParams(), "someDefault"));
assertEquals("someDefault", V2ApiUtils.getMediaTypeFromWtParam(SolrParams.of(), "someDefault"));

var params = new ModifiableSolrParams();
params.add("wt", "json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ public MockAuthorizationContext(Map<String, Object> values) {
@Override
public SolrParams getParams() {
SolrParams params = (SolrParams) values.get("params");
return params == null ? new MapSolrParams(new HashMap<>()) : params;
return params == null ? SolrParams.of() : params;
}

@Override
Expand Down
Loading
Loading