-
Notifications
You must be signed in to change notification settings - Fork 699
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
SOLR-17381 SolrJ fix to fetch entire ClusterState if asked #2853
Merged
+147
−88
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fe9e043
Allow the possibility to fetch entire cluster state
aparnasuresh85 f40fafc
Merge remote-tracking branch 'upstream/main' into address-testfailure
aparnasuresh85 cf1bc04
Refactor codepath to fetch entire cluster status vs a single collection
aparnasuresh85 fce0b7d
refactor
aparnasuresh85 182b449
rename variable
aparnasuresh85 ca217ce
tidy
dsmiley 63dc069
* Optimization remove O(N^2) on collections
dsmiley 5a3978e
Merge branch 'refs/heads/main' into fork/aparnasuresh85/address-testf…
dsmiley 7f8412d
* inline fillPRS to getDocCollectionFromObjects
dsmiley 3cf330f
Remove name-value pairs that shouldn't be in the final result.
dsmiley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,8 +98,8 @@ public ClusterState.CollectionRef getState(String collection) { | |
for (String nodeName : liveNodes) { | ||
String baseUrl = Utils.getBaseUrlForNodeName(nodeName, urlScheme); | ||
try (SolrClient client = getSolrClient(baseUrl)) { | ||
ClusterState cs = fetchClusterState(client, collection, null); | ||
return cs.getCollectionRef(collection); | ||
DocCollection docCollection = fetchCollectionState(client, collection); | ||
return new ClusterState.CollectionRef(docCollection); | ||
} catch (SolrServerException | IOException e) { | ||
log.warn( | ||
"Attempt to fetch cluster state from {} failed.", | ||
|
@@ -128,30 +128,12 @@ public ClusterState.CollectionRef getState(String collection) { | |
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private ClusterState fetchClusterState( | ||
SolrClient client, String collection, Map<String, Object> clusterProperties) | ||
private ClusterState fetchClusterState(SolrClient client, Map<String, Object> clusterProperties) | ||
throws SolrServerException, IOException, NotACollectionException { | ||
SimpleOrderedMap<?> cluster = | ||
submitClusterStateRequest(client, collection, ClusterStateRequestType.FETCH_COLLECTION); | ||
submitClusterStateRequest(client, null, ClusterStateRequestType.FETCH_CLUSTER_STATE); | ||
|
||
Map<String, Object> collectionsMap; | ||
if (collection != null) { | ||
collectionsMap = | ||
Collections.singletonMap( | ||
collection, ((NamedList<?>) cluster.get("collections")).get(collection)); | ||
} else { | ||
collectionsMap = ((NamedList<?>) cluster.get("collections")).asMap(10); | ||
} | ||
int znodeVersion; | ||
Map<String, Object> collFromStatus = (Map<String, Object>) (collectionsMap).get(collection); | ||
if (collection != null && collFromStatus == null) { | ||
throw new NotACollectionException(); // probably an alias | ||
} | ||
if (collection != null) { // can be null if alias | ||
znodeVersion = (int) collFromStatus.get("znodeVersion"); | ||
} else { | ||
znodeVersion = -1; | ||
} | ||
Map<String, Object> collectionsMap = ((NamedList<?>) cluster.get("collections")).asMap(10); | ||
|
||
ClusterState cs = new ClusterState(this.liveNodes, new HashMap<>()); | ||
List<String> liveNodesList = (List<String>) cluster.get("live_nodes"); | ||
|
@@ -163,14 +145,11 @@ private ClusterState fetchClusterState( | |
} | ||
|
||
for (Map.Entry<String, Object> e : collectionsMap.entrySet()) { | ||
@SuppressWarnings("rawtypes") | ||
Map m = (Map) e.getValue(); | ||
Long creationTimeMillisFromClusterStatus = (Long) m.get("creationTimeMillis"); | ||
Instant creationTime = | ||
creationTimeMillisFromClusterStatus == null | ||
? Instant.EPOCH | ||
: Instant.ofEpochMilli(creationTimeMillisFromClusterStatus); | ||
cs = cs.copyWith(e.getKey(), fillPrs(znodeVersion, e, creationTime, m)); | ||
String collectionName = e.getKey(); | ||
Map<String, Object> collStateMap = (Map<String, Object>) e.getValue(); | ||
cs = | ||
cs.copyWith( | ||
collectionName, getDocCollectionFromObjects(collectionName, collStateMap, -1)); | ||
} | ||
|
||
if (clusterProperties != null) { | ||
|
@@ -182,6 +161,37 @@ private ClusterState fetchClusterState( | |
return cs; | ||
} | ||
|
||
private DocCollection getDocCollectionFromObjects( | ||
String collectionName, Map<String, Object> collStateMap, int zNodeVersion) { | ||
|
||
Long creationTimeMillis = (Long) collStateMap.get("creationTimeMillis"); | ||
Instant creationTime = | ||
creationTimeMillis == null | ||
? Instant.EPOCH | ||
: Instant.ofEpochMilli(creationTimeMillis); | ||
return fillPrs(collectionName, collStateMap, creationTime, zNodeVersion); | ||
} | ||
|
||
private DocCollection fetchCollectionState(SolrClient client, String collection) | ||
throws SolrServerException, IOException, NotACollectionException { | ||
|
||
SimpleOrderedMap<?> cluster = | ||
submitClusterStateRequest(client, collection, ClusterStateRequestType.FETCH_COLLECTION); | ||
|
||
Map<String, Object> collectionsMap = | ||
Collections.singletonMap( | ||
collection, ((NamedList<?>) cluster.get("collections")).get(collection)); | ||
|
||
int znodeVersion = -1; | ||
@SuppressWarnings("unchecked") | ||
Map<String, Object> collStateMap = (Map<String, Object>) (collectionsMap).get(collection); | ||
if (collStateMap == null) { | ||
throw new NotACollectionException(); // probably an alias | ||
} | ||
znodeVersion = (int) collStateMap.get("znodeVersion"); | ||
return getDocCollectionFromObjects(collection, collStateMap, znodeVersion); | ||
} | ||
|
||
private SimpleOrderedMap<?> submitClusterStateRequest( | ||
SolrClient client, String collection, ClusterStateRequestType requestType) | ||
throws SolrServerException, IOException { | ||
|
@@ -198,30 +208,35 @@ private SimpleOrderedMap<?> submitClusterStateRequest( | |
} else if (requestType == ClusterStateRequestType.FETCH_NODE_ROLES) { | ||
params.set("roles", "true"); | ||
} | ||
|
||
params.set("includeAll", "false"); | ||
if (requestType == ClusterStateRequestType.FETCH_CLUSTER_STATE) { | ||
params.set("includeAll", "true"); | ||
} else { | ||
params.set("includeAll", "false"); | ||
} | ||
params.set("prs", "true"); | ||
QueryRequest request = new QueryRequest(params); | ||
request.setPath("/admin/collections"); | ||
return (SimpleOrderedMap<?>) client.request(request).get("cluster"); | ||
} | ||
|
||
@SuppressWarnings({"rawtypes", "unchecked"}) | ||
@SuppressWarnings({"unchecked"}) | ||
private DocCollection fillPrs( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I inlined |
||
int znodeVersion, Map.Entry<String, Object> e, Instant creationTime, Map m) { | ||
String collectionName, | ||
Map<String, Object> collStateMap, | ||
Instant creationTime, | ||
int znodeVersion) { | ||
DocCollection.PrsSupplier prsSupplier = null; | ||
if (m.containsKey("PRS")) { | ||
Map prs = (Map) m.remove("PRS"); | ||
if (collStateMap.containsKey("PRS")) { | ||
Map<String, Object> prs = (Map<String, Object>) collStateMap.remove("PRS"); | ||
prsSupplier = | ||
() -> | ||
new PerReplicaStates( | ||
(String) prs.get("path"), | ||
(Integer) prs.get("cversion"), | ||
(List<String>) prs.get("states")); | ||
} | ||
|
||
return ClusterState.collectionFromObjects( | ||
e.getKey(), m, znodeVersion, creationTime, prsSupplier); | ||
collectionName, collStateMap, znodeVersion, creationTime, prsSupplier); | ||
} | ||
|
||
@Override | ||
|
@@ -346,8 +361,8 @@ public ClusterState getClusterState() { | |
for (String nodeName : liveNodes) { | ||
String baseUrl = Utils.getBaseUrlForNodeName(nodeName, urlScheme); | ||
try (SolrClient client = getSolrClient(baseUrl)) { | ||
return fetchClusterState(client, null, null); | ||
} catch (SolrServerException | SolrClient.RemoteSolrException | IOException e) { | ||
return fetchClusterState(client, null); | ||
} catch (SolrServerException | RemoteSolrException | IOException e) { | ||
log.warn("Attempt to fetch cluster state from {} failed.", baseUrl, e); | ||
} catch (NotACollectionException e) { | ||
// not possible! (we passed in null for collection, so it can't be an alias) | ||
|
@@ -376,7 +391,7 @@ public Map<String, Object> getClusterProperties() { | |
SimpleOrderedMap<?> cluster = | ||
submitClusterStateRequest(client, null, ClusterStateRequestType.FETCH_CLUSTER_PROP); | ||
return (Map<String, Object>) cluster.get("properties"); | ||
} catch (SolrServerException | SolrClient.RemoteSolrException | IOException e) { | ||
} catch (SolrServerException | RemoteSolrException | IOException e) { | ||
log.warn("Attempt to fetch cluster state from {} failed.", baseUrl, e); | ||
} | ||
} | ||
|
@@ -428,6 +443,7 @@ private enum ClusterStateRequestType { | |
FETCH_LIVE_NODES, | ||
FETCH_CLUSTER_PROP, | ||
FETCH_NODE_ROLES, | ||
FETCH_COLLECTION | ||
FETCH_COLLECTION, | ||
FETCH_CLUSTER_STATE | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling copyWith in a loop of collections is
O(N^2)
(N=#collections); right? Not a problem introduced in this PR but I just want to confer with you on this problem.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it is indeed O(N^2).