Skip to content

SOLR-17625 NamedList.findRecursive-> _get #3355

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

Merged
merged 13 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 3 additions & 1 deletion solr/core/src/java/org/apache/solr/cli/AssertTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileOwnerAttributeView;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
Expand Down Expand Up @@ -280,7 +281,8 @@ public int assertSolrNotRunning(String url, String credentials) throws Exception
System.nanoTime() + TimeUnit.NANOSECONDS.convert(timeoutMs, TimeUnit.MILLISECONDS);
try (SolrClient solrClient = CLIUtils.getSolrClient(url, credentials)) {
NamedList<Object> response = solrClient.request(new HealthCheckRequest());
Integer statusCode = (Integer) response.findRecursive("responseHeader", "status");
Object value = response._get(List.of(new String[] {"responseHeader", "status"}), null);
Integer statusCode = (Integer) value;
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't declare new variables merely to cast.

CLIUtils.checkCodeForAuthError(statusCode);
} catch (IOException | SolrServerException e) {
log.debug("Opening connection to {} failed, Solr does not seem to be running", url, e);
Expand Down
4 changes: 3 additions & 1 deletion solr/core/src/java/org/apache/solr/cli/ConfigTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.solr.cli;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.MissingArgumentException;
Expand Down Expand Up @@ -124,7 +125,8 @@ public void runImpl(CommandLine cli) throws Exception {
try (SolrClient solrClient =
CLIUtils.getSolrClient(solrUrl, cli.getOptionValue(CommonCLIOptions.CREDENTIALS_OPTION))) {
NamedList<Object> result = SolrCLI.postJsonToSolr(solrClient, updatePath, jsonBody);
Integer statusCode = (Integer) result.findRecursive("responseHeader", "status");
Object value1 = result._get(List.of(new String[] {"responseHeader", "status"}), null);
Integer statusCode = (Integer) value1;
if (statusCode == 0) {
if (value != null) {
echo("Successfully " + action + " " + property + " to " + value);
Expand Down
12 changes: 9 additions & 3 deletions solr/core/src/java/org/apache/solr/cli/HealthcheckTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,15 @@ protected void runCloudTool(CloudSolrClient cloudSolrClient, CommandLine cli) th
solrClient.request(
new GenericSolrRequest(
SolrRequest.METHOD.GET, CommonParams.SYSTEM_INFO_PATH));
uptime = SolrCLI.uptime((Long) systemInfo.findRecursive("jvm", "jmx", "upTimeMS"));
String usedMemory = (String) systemInfo.findRecursive("jvm", "memory", "used");
String totalMemory = (String) systemInfo.findRecursive("jvm", "memory", "total");
Object value2 =
systemInfo._get(List.of(new String[] {"jvm", "jmx", "upTimeMS"}), null);
uptime = SolrCLI.uptime((Long) value2);
Object value1 =
systemInfo._get(List.of(new String[] {"jvm", "memory", "used"}), null);
String usedMemory = (String) value1;
Object value =
systemInfo._get(List.of(new String[] {"jvm", "memory", "total"}), null);
String totalMemory = (String) value;
memory = usedMemory + " of " + totalMemory;
}

Expand Down
23 changes: 15 additions & 8 deletions solr/core/src/java/org/apache/solr/cli/StatusTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,17 @@ public static Map<String, Object> reportStatus(NamedList<Object> info, SolrClien

String solrHome = (String) info.get("solr_home");
status.put("solr_home", solrHome != null ? solrHome : "?");
status.put("version", info.findRecursive("lucene", "solr-impl-version"));
status.put("startTime", info.findRecursive("jvm", "jmx", "startTime").toString());
status.put("uptime", SolrCLI.uptime((Long) info.findRecursive("jvm", "jmx", "upTimeMS")));

String usedMemory = (String) info.findRecursive("jvm", "memory", "used");
String totalMemory = (String) info.findRecursive("jvm", "memory", "total");
Object value4 = info._get(List.of(new String[] {"lucene", "solr-impl-version"}), null);
status.put("version", value4);
Object value3 = info._get(List.of(new String[] {"jvm", "jmx", "startTime"}), null);
status.put("startTime", value3.toString());
Object value2 = info._get(List.of(new String[] {"jvm", "jmx", "upTimeMS"}), null);
status.put("uptime", SolrCLI.uptime((Long) value2));

Object value1 = info._get(List.of(new String[] {"jvm", "memory", "used"}), null);
String usedMemory = (String) value1;
Object value = info._get(List.of(new String[] {"jvm", "memory", "total"}), null);
String totalMemory = (String) value;
status.put("memory", usedMemory + " of " + totalMemory);

// if this is a Solr in solrcloud mode, gather some basic cluster info
Expand All @@ -344,11 +349,13 @@ private static Map<String, String> getCloudStatus(SolrClient solrClient, String
// TODO add booleans to request just what we want; not everything
NamedList<Object> json = solrClient.request(new CollectionAdminRequest.ClusterStatus());

List<String> liveNodes = (List<String>) json.findRecursive("cluster", "live_nodes");
Object value1 = json._get(List.of(new String[] {"cluster", "live_nodes"}), null);
List<String> liveNodes = (List<String>) value1;
cloudStatus.put("liveNodes", String.valueOf(liveNodes.size()));

// TODO get this as a metric from the metrics API instead, or something else.
var collections = (Map<String, Object>) json.findRecursive("cluster", "collections");
Object value = json._get(List.of(new String[] {"cluster", "collections"}), null);
var collections = (Map<String, Object>) value;
cloudStatus.put("collections", String.valueOf(collections.size()));

return cloudStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -862,14 +862,18 @@ public static void checkDiskSpace(
SolrRequest.METHOD.GET, "/admin/metrics", SolrRequest.SolrRequestType.ADMIN, params)
.process(cloudManager.getSolrClient());

Number size = (Number) rsp.getResponse().findRecursive("metrics", indexSizeMetricName);
NamedList<Object> entries1 = rsp.getResponse();
Copy link
Contributor

Choose a reason for hiding this comment

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

again, no need to declare another variable

Copy link
Contributor

Choose a reason for hiding this comment

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

still an issue

Object value1 = entries1._get(List.of(new String[] {"metrics", indexSizeMetricName}), null);
Number size = (Number) value1;
if (size == null) {
log.warn("cannot verify information for parent shard leader");
return;
}
double indexSize = size.doubleValue();

Number freeSize = (Number) rsp.getResponse().findRecursive("metrics", freeDiskSpaceMetricName);
NamedList<Object> entries = rsp.getResponse();
Object value = entries._get(List.of(new String[] {"metrics", freeDiskSpaceMetricName}), null);
Number freeSize = (Number) value;
if (freeSize == null) {
log.warn("missing node disk space information for parent shard leader");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -609,15 +609,20 @@ private void processComponents(
if (resp == null) {
return false;
}
Object recursive = resp.findRecursive("responseHeader", "partialResults");
Object value1 =
resp._get(
List.of(new String[] {"responseHeader", "partialResults"}), null);
Object recursive = value1;
if (recursive != null) {
Object message =
"[Shard:"
+ response.getShardAddress()
+ "]"
+ resp.findRecursive(
"responseHeader",
RESPONSE_HEADER_PARTIAL_RESULTS_DETAILS_KEY);
Object value =
resp._get(
List.of(
new String[] {
"responseHeader",
RESPONSE_HEADER_PARTIAL_RESULTS_DETAILS_KEY
}),
null);
Object message = "[Shard:" + response.getShardAddress() + "]" + value;
detailMesg.compareAndSet(null, message); // first one, ingore rest
}
return recursive != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ public Map<String, SolrPackageInstance> getPackagesDeployedAsClusterLevelPlugins
NamedList<Object> response =
solrClient.request(
new GenericV2SolrRequest(SolrRequest.METHOD.GET, PackageUtils.CLUSTERPROPS_PATH));
Integer statusCode = (Integer) response.findRecursive("responseHeader", "status");
Object value = response._get(List.of(new String[] {"responseHeader", "status"}), null);
Integer statusCode = (Integer) value;
if (statusCode == null || statusCode == ErrorCode.NOT_FOUND.code) {
// Cluster props doesn't exist, that means there are no cluster level plugins installed.
result = Collections.emptyMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -620,19 +620,29 @@ public void testColStatus() throws Exception {
CollectionAdminResponse rsp = req.process(cluster.getSolrClient());
assertEquals(0, rsp.getStatus());
assertNotNull(rsp.getResponse().get(collectionName));
assertNotNull(rsp.getResponse().findRecursive(collectionName, "properties"));
final var collPropMap =
(Map<String, Object>) rsp.getResponse().findRecursive(collectionName, "properties");
NamedList<Object> entries5 = rsp.getResponse();
Object value5 = entries5._get(List.of(new String[] {collectionName, "properties"}), null);
assertNotNull(value5);
NamedList<Object> entries4 = rsp.getResponse();
Object value4 = entries4._get(List.of(new String[] {collectionName, "properties"}), null);
final var collPropMap = (Map<String, Object>) value4;
assertEquals("conf2", collPropMap.get("configName"));
assertEquals(2L, collPropMap.get("nrtReplicas"));
assertEquals("0", collPropMap.get("tlogReplicas"));
assertEquals("0", collPropMap.get("pullReplicas"));
assertEquals(
2, ((NamedList<Object>) rsp.getResponse().findRecursive(collectionName, "shards")).size());
assertNotNull(rsp.getResponse().findRecursive(collectionName, "shards", "shard1", "leader"));
NamedList<Object> entries3 = rsp.getResponse();
Object value3 = entries3._get(List.of(new String[] {collectionName, "shards"}), null);
assertEquals(2, ((NamedList<Object>) value3).size());
NamedList<Object> entries2 = rsp.getResponse();
Object value2 =
entries2._get(List.of(new String[] {collectionName, "shards", "shard1", "leader"}), null);
assertNotNull(value2);
// Ensure more advanced info is not returned
assertNull(
rsp.getResponse().findRecursive(collectionName, "shards", "shard1", "leader", "segInfos"));
NamedList<Object> entries1 = rsp.getResponse();
Object value1 =
entries1._get(
List.of(new String[] {collectionName, "shards", "shard1", "leader", "segInfos"}), null);
assertNull(value1);

// Returns segment metadata iff requested
req = CollectionAdminRequest.collectionStatus(collectionName);
Expand Down Expand Up @@ -687,9 +697,10 @@ public void testColStatus() throws Exception {
req.setWithSizeInfo(true);
rsp = req.process(cluster.getSolrClient());
assertEquals(0, rsp.getStatus());
NamedList<Object> entries = rsp.getResponse();
Object value = entries._get(List.of(new String[] {collectionName, "schemaNonCompliant"}), null);
@SuppressWarnings({"unchecked"})
List<Object> nonCompliant =
(List<Object>) rsp.getResponse().findRecursive(collectionName, "schemaNonCompliant");
List<Object> nonCompliant = (List<Object>) value;
assertEquals(nonCompliant.toString(), 1, nonCompliant.size());
assertTrue(nonCompliant.toString(), nonCompliant.contains("(NONE)"));
@SuppressWarnings({"unchecked"})
Expand Down
10 changes: 6 additions & 4 deletions solr/core/src/test/org/apache/solr/cloud/TestPullReplica.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.apache.solr.common.cloud.Replica;
import org.apache.solr.common.cloud.Slice;
import org.apache.solr.common.cloud.ZkStateReader;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.TimeSource;
import org.apache.solr.core.CoreDescriptor;
import org.apache.solr.core.SolrCore;
Expand Down Expand Up @@ -315,12 +316,13 @@ public void testAddDocs() throws Exception {
SolrQuery req = new SolrQuery("qt", "/admin/plugins", "stats", "true");
QueryResponse statsResponse = pullReplicaClient.query(req);
// The adds gauge metric should be null for pull replicas since they don't process adds
NamedList<Object> entries = (statsResponse.getResponse());
Object value =
entries._get(
List.of(new String[] {"plugins", "UPDATE", "updateHandler", "stats"}), null);
assertNull(
"Replicas shouldn't process the add document request: " + statsResponse,
((Map<String, Object>)
(statsResponse.getResponse())
.findRecursive("plugins", "UPDATE", "updateHandler", "stats"))
.get("UPDATE.updateHandler.adds"));
((Map<String, Object>) value).get("UPDATE.updateHandler.adds"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apache.solr.common.cloud.DocCollection;
import org.apache.solr.common.cloud.Replica;
import org.apache.solr.common.cloud.Slice;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.util.SecurityJson;
import org.junit.BeforeClass;
import org.junit.Test;
Expand Down Expand Up @@ -153,10 +154,9 @@ public void testPKIAuthWorksForPullReplication() throws Exception {

@SuppressWarnings("unchecked")
private Object getUpdateHandlerMetric(QueryResponse statsResponse, String metric) {
return ((Map<String, Object>)
statsResponse
.getResponse()
.findRecursive("plugins", "UPDATE", "updateHandler", "stats"))
.get(metric);
NamedList<Object> entries = statsResponse.getResponse();
Object value =
entries._get(List.of(new String[] {"plugins", "UPDATE", "updateHandler", "stats"}), null);
return ((Map<String, Object>) value).get(metric);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.solr.client.solrj.response.CollectionAdminResponse;
import org.apache.solr.common.cloud.LiveNodesPredicate;
import org.apache.solr.common.cloud.ZkStateReader;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.embedded.JettySolrRunner;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -237,15 +238,21 @@ public boolean matches(SortedSet<String> oldLiveNodes, SortedSet<String> newLive
* <p>The update happens in org.apache.solr.cloud.Overseer.ClusterStateUpdater.processQueueItem()
*/
private int getNumLeaderOperations(CollectionAdminResponse resp) {
return (int) resp.getResponse().findRecursive("overseer_operations", "leader", "requests");
NamedList<Object> entries = resp.getResponse();
Object value =
entries._get(List.of(new String[] {"overseer_operations", "leader", "requests"}), null);
return (int) value;
}

/**
* "state" stats are when Overseer processes a {@link
* org.apache.solr.cloud.overseer.OverseerAction#STATE} message that sets replica properties
*/
private int getNumStateOperations(CollectionAdminResponse resp) {
return (int) resp.getResponse().findRecursive("overseer_operations", "state", "requests");
NamedList<Object> entries = resp.getResponse();
Object value =
entries._get(List.of(new String[] {"overseer_operations", "state", "requests"}), null);
return (int) value;
}

private String getOverseerLeader() throws IOException, SolrServerException {
Expand Down
9 changes: 5 additions & 4 deletions solr/core/src/test/org/apache/solr/cloud/TestTlogReplica.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,16 +286,17 @@ public void testAddDocs() throws Exception {
"qt", "/admin/plugins",
"stats", "true");
QueryResponse statsResponse = tlogReplicaClient.query(req);
NamedList<Object> entries = (statsResponse.getResponse());
Object value =
entries._get(
List.of(new String[] {"plugins", "UPDATE", "updateHandler", "stats"}), null);
assertEquals(
"Append replicas should recive all updates. Replica: "
+ r
+ ", response: "
+ statsResponse,
1L,
((Map<String, Object>)
(statsResponse.getResponse())
.findRecursive("plugins", "UPDATE", "updateHandler", "stats"))
.get("UPDATE.updateHandler.cumulativeAdds.count"));
((Map<String, Object>) value).get("UPDATE.updateHandler.cumulativeAdds.count"));
break;
} catch (AssertionError e) {
if (t.hasTimedOut()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.apache.solr.client.solrj.SolrClient;
Expand Down Expand Up @@ -79,7 +80,8 @@ public void test() {
NamedList<Object> createResponse = null;
try {
createResponse = sendStatusRequestWithRetry(params, MAX_WAIT_TIMEOUT_SECONDS);
message = (String) createResponse.findRecursive("status", "msg");
Object value = createResponse._get(List.of(new String[] {"status", "msg"}), null);
message = (String) value;
} catch (SolrServerException | IOException e) {
log.error("error sending request", e);
}
Expand Down Expand Up @@ -122,7 +124,8 @@ public void test() {
NamedList<Object> splitResponse = null;
try {
splitResponse = sendStatusRequestWithRetry(params, MAX_WAIT_TIMEOUT_SECONDS);
message = (String) splitResponse.findRecursive("status", "msg");
Object value = splitResponse._get(List.of(new String[] {"status", "msg"}), null);
message = (String) value;
} catch (SolrServerException | IOException e) {
log.error("error sending request", e);
}
Expand Down Expand Up @@ -154,7 +157,8 @@ public void test() {

try {
NamedList<Object> response = sendStatusRequestWithRetry(params, MAX_WAIT_TIMEOUT_SECONDS);
message = (String) response.findRecursive("status", "msg");
Object value = response._get(List.of(new String[] {"status", "msg"}), null);
message = (String) value;
} catch (SolrServerException | IOException e) {
log.error("error sending request", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -798,9 +798,10 @@ public TokenStream create(TokenStream input) {
@SuppressWarnings({"rawtypes"})
NamedList<NamedList> result = handler.analyzeValues(request, fieldType, "fieldNameUnused");
// just test that we see "900" in the flags attribute here
Object value =
result._get(List.of(new String[] {"index", CustomTokenFilter.class.getName()}), null);
@SuppressWarnings({"unchecked", "rawtypes"})
List<NamedList> tokenInfoList =
(List<NamedList>) result.findRecursive("index", CustomTokenFilter.class.getName());
List<NamedList> tokenInfoList = (List<NamedList>) value;
// '1' from CustomTokenFilter plus 900 from CustomFlagsAttributeImpl.
assertEquals(
901,
Expand Down
Loading
Loading