From c7362262e868b9311e94c954e6e1c9f866776ed2 Mon Sep 17 00:00:00 2001 From: Paulo Costa Date: Wed, 13 Jul 2016 10:44:24 -0300 Subject: [PATCH] SOLR-9303: Refactor CloudSolrClient for extensibility CloudSolrClient.sendRequest() is currently written as one big chunk of code, making it difficult to customize it on a subclass. --- .../client/solrj/impl/CloudSolrClient.java | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java index 876f7f86cbad..7274ba802a3d 100644 --- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java +++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/CloudSolrClient.java @@ -1144,7 +1144,6 @@ protected NamedList sendRequest(SolrRequest request, String collection) ClusterState clusterState = zkStateReader.getClusterState(); boolean sendToLeaders = false; - List replicas = null; if (request instanceof IsUpdateRequest) { if (request instanceof UpdateRequest) { @@ -1154,13 +1153,25 @@ protected NamedList sendRequest(SolrRequest request, String collection) } } sendToLeaders = true; - replicas = new ArrayList<>(); } + List urlList = findRequestUrls(request, collection, clusterState, sendToLeaders); + + return executeHttpRequest(request, urlList); + } + + protected List findRequestUrls(SolrRequest request, String collection, ClusterState clusterState, + boolean sendToLeaders) throws SolrServerException { SolrParams reqParams = request.getParams(); if (reqParams == null) { reqParams = new ModifiableSolrParams(); } + + List replicas = null; + if (sendToLeaders) { + replicas = new ArrayList<>(); + } + List theUrlList = new ArrayList<>(); if (ADMIN_PATHS.contains(request.getPath())) { Set liveNodes = clusterState.getLiveNodes(); @@ -1209,8 +1220,11 @@ protected NamedList sendRequest(SolrRequest request, String collection) for (ZkNodeProps nodeProps : slice.getReplicasMap().values()) { ZkCoreNodeProps coreNodeProps = new ZkCoreNodeProps(nodeProps); String node = coreNodeProps.getNodeName(); - if (!liveNodes.contains(coreNodeProps.getNodeName()) - || Replica.State.getState(coreNodeProps.getState()) != Replica.State.ACTIVE) continue; + + if (!isNodeAvailable(coreNodeProps, liveNodes, request)) { + continue; + } + if (nodes.put(node, nodeProps) == null) { if (!sendToLeaders || coreNodeProps.isLeader()) { String url; @@ -1265,13 +1279,17 @@ protected NamedList sendRequest(SolrRequest request, String collection) "Could not find a healthy node to handle the request."); } } + return theUrlList; + } + protected NamedList executeHttpRequest(SolrRequest request, List theUrlList) + throws SolrServerException, IOException { LBHttpSolrClient.Req req = new LBHttpSolrClient.Req(request, theUrlList); LBHttpSolrClient.Rsp rsp = lbClient.request(req); return rsp.getResponse(); } - private Set getCollectionNames(ClusterState clusterState, + protected Set getCollectionNames(ClusterState clusterState, String collection) { // Extract each comma separated collection name and store in a List. List rawCollectionsList = StrUtils.splitSmart(collection, ",", true); @@ -1295,6 +1313,11 @@ private Set getCollectionNames(ClusterState clusterState, return collectionNames; } + protected boolean isNodeAvailable(ZkCoreNodeProps coreNodeProps, Set liveNodes, SolrRequest request) { + return liveNodes.contains(coreNodeProps.getNodeName()) + && Replica.State.getState(coreNodeProps.getState()) == Replica.State.ACTIVE; + } + @Override public void close() throws IOException { if (zkStateReader != null) {