public HttpClusterStateProvider(List<String> solrUrls, HttpClient httpClient) throws Exception { this.httpClient = httpClient == null? HttpClientUtil.createClient(null): httpClient; this.clientIsInternal = httpClient == null; for (String solrUrl: solrUrls) { urlScheme = solrUrl.startsWith("https")? "https": "http"; try (SolrClient initialClient = new HttpSolrClient.Builder().withBaseSolrUrl(solrUrl).withHttpClient(httpClient).build()) { Set<String> liveNodes = fetchLiveNodes(initialClient); // throws exception if unable to fetch this.liveNodes = liveNodes; liveNodesTimestamp = System.nanoTime(); break; } catch (IOException e) { log.warn("Attempt to fetch live_nodes from " + solrUrl + " failed.", e); } } if (this.liveNodes == null || this.liveNodes.isEmpty()) { throw new RuntimeException("Tried fetching live_nodes using Solr URLs provided, i.e. " + solrUrls + ". However, " + "succeeded in obtaining the cluster state from none of them." + "If you think your Solr cluster is up and is accessible," + " you could try re-creating a new CloudSolrClient using working" + " solrUrl(s) or zkHost(s)."); } }
for (String nodeName: liveNodes) { try (HttpSolrClient client = new HttpSolrClient.Builder(). withBaseSolrUrl(Utils.getBaseUrlForNodeName(nodeName, urlScheme)). withHttpClient(httpClient).build()) {
for (String nodeName: liveNodes) { try (HttpSolrClient client = new HttpSolrClient.Builder(). withBaseSolrUrl(Utils.getBaseUrlForNodeName(nodeName, urlScheme)). withHttpClient(httpClient).build()) { Set<String> liveNodes = fetchLiveNodes(client);
try (HttpSolrClient client = new HttpSolrClient.Builder().withBaseSolrUrl(config.getSolrUrl()).build()) {
synchronized (this) { httpSolrClientBuilder .withBaseSolrUrl(server) .withHttpClient(httpClient); if (connectionTimeout != null) {
@Override public CollectionRef getState(String collection) { for (String nodeName: liveNodes) { try (HttpSolrClient client = new HttpSolrClient.Builder(). withBaseSolrUrl(Utils.getBaseUrlForNodeName(nodeName, urlScheme)). withHttpClient(httpClient).build()) { ClusterState cs = fetchClusterState(client, collection, null); return cs.getCollectionRef(collection); } catch (SolrServerException | IOException e) { log.warn("Attempt to fetch cluster state from " + Utils.getBaseUrlForNodeName(nodeName, urlScheme) + " failed.", e); } catch (RemoteSolrException e) { if ("NOT_FOUND".equals(e.getMetadata("CLUSTERSTATUS"))) { return null; } log.warn("Attempt to fetch cluster state from " + Utils.getBaseUrlForNodeName(nodeName, urlScheme) + " failed.", e); } catch (NotACollectionException e) { // Cluster state for the given collection was not found, could be an alias. // Lets fetch/update our aliases: getAliases(true); return null; } } throw new RuntimeException("Tried fetching cluster state using the node names we knew of, i.e. " + liveNodes +". However, " + "succeeded in obtaining the cluster state from none of them." + "If you think your Solr cluster is up and is accessible," + " you could try re-creating a new CloudSolrClient using working" + " solrUrl(s) or zkHost(s)."); }
@Override public ClusterState getClusterState() throws IOException { for (String nodeName: liveNodes) { try (HttpSolrClient client = new HttpSolrClient.Builder(). withBaseSolrUrl(Utils.getBaseUrlForNodeName(nodeName, urlScheme)). withHttpClient(httpClient).build()) { ClusterState cs = fetchClusterState(client, null, null); return cs; } catch (SolrServerException | RemoteSolrException | IOException e) { log.warn("Attempt to fetch cluster state from " + Utils.getBaseUrlForNodeName(nodeName, urlScheme) + " failed.", e); } catch (NotACollectionException e) { // not possible! (we passed in null for collection so it can't be an alias) throw new RuntimeException("null should never cause NotACollectionException in " + "fetchClusterState() Please report this as a bug!"); } } throw new RuntimeException("Tried fetching cluster state using the node names we knew of, i.e. " + liveNodes +". However, " + "succeeded in obtaining the cluster state from none of them." + "If you think your Solr cluster is up and is accessible," + " you could try re-creating a new CloudSolrClient using working" + " solrUrl(s) or zkHost(s)."); }
@Override public Map<String, Object> getClusterProperties() { for (String nodeName : liveNodes) { try (HttpSolrClient client = new HttpSolrClient.Builder(). withBaseSolrUrl(Utils.getBaseUrlForNodeName(nodeName, urlScheme)). withHttpClient(httpClient).build()) { Map<String, Object> clusterProperties = new HashMap<>(); fetchClusterState(client, null, clusterProperties); return clusterProperties; } catch (SolrServerException | RemoteSolrException | IOException e) { log.warn("Attempt to fetch cluster state from " + Utils.getBaseUrlForNodeName(nodeName, urlScheme) + " failed.", e); } catch (NotACollectionException e) { // not possible! (we passed in null for collection so it can't be an alias) throw new RuntimeException("null should never cause NotACollectionException in " + "fetchClusterState() Please report this as a bug!"); } } throw new RuntimeException("Tried fetching cluster state using the node names we knew of, i.e. " + liveNodes + ". However, " + "succeeded in obtaining the cluster state from none of them." + "If you think your Solr cluster is up and is accessible," + " you could try re-creating a new CloudSolrClient using working" + " solrUrl(s) or zkHost(s)."); }
/** * Creates the datastore. * * @param url The URL of SOLR server * @param layerMapper The document loader. */ public SolrDataStore(URL url, SolrLayerMapper layerMapper) { // TODO: make connection timeouts configurable this.url = url; this.layerMapper = layerMapper; this.solrServer = new HttpSolrClient.Builder() .withBaseSolrUrl(url.toString()) .allowCompression(true) .build(); this.solrServer.setConnectionTimeout(10000); this.solrServer.setFollowRedirects(true); this.solrServer.setSoTimeout(10000); }
public SimpleSolrResponse invoke(String solrNode, String path, SolrParams params) throws IOException, SolrServerException { String url = zkClientClusterStateProvider.getZkStateReader().getBaseUrlForNodeName(solrNode); GenericSolrRequest request = new GenericSolrRequest(SolrRequest.METHOD.POST, path, params); try (HttpSolrClient client = new HttpSolrClient.Builder() .withHttpClient(solrClient.getHttpClient()) .withBaseSolrUrl(url) .withResponseParser(new BinaryResponseParser()) .build()) { NamedList<Object> rsp = client.request(request); request.response.nl = rsp; return request.response; } }
public static SolrClient server(String baseUrl) { return new HttpSolrClient.Builder() .withBaseSolrUrl(baseUrl) .build(); }
private void createHttpSolrClient() { HttpSolrClient.Builder builder = new HttpSolrClient.Builder().withBaseSolrUrl(this.url); if (timeout != null) { builder = builder.withConnectionTimeout(timeout); } if (maxConnections != null) { ModifiableSolrParams params = new ModifiableSolrParams(); params.set(HttpClientUtil.PROP_MAX_CONNECTIONS, maxConnections); builder.withHttpClient(HttpClientUtil.createClient(params)); } this.setSolrClient(builder.build()); }