Search in sources :

Example 1 with Builder

use of org.apache.solr.client.solrj.impl.HttpSolrClient.Builder in project lucene-solr by apache.

the class HttpSolrClientBuilderTest method testDefaultsToBinaryResponseParserWhenNoneProvided.

@Test
public void testDefaultsToBinaryResponseParserWhenNoneProvided() throws IOException {
    try (HttpSolrClient createdClient = new Builder(ANY_BASE_SOLR_URL).build()) {
        final ResponseParser usedParser = createdClient.getParser();
        assertTrue(usedParser instanceof BinaryResponseParser);
    }
}
Also used : ResponseParser(org.apache.solr.client.solrj.ResponseParser) Builder(org.apache.solr.client.solrj.impl.HttpSolrClient.Builder) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) Test(org.junit.Test)

Example 2 with Builder

use of org.apache.solr.client.solrj.impl.HttpSolrClient.Builder in project lucene-solr by apache.

the class IndexFetcher method getLatestVersion.

/**
   * Gets the latest commit version and generation from the master
   */
@SuppressWarnings("unchecked")
NamedList getLatestVersion() throws IOException {
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set(COMMAND, CMD_INDEX_VERSION);
    params.set(CommonParams.WT, JAVABIN);
    params.set(CommonParams.QT, ReplicationHandler.PATH);
    QueryRequest req = new QueryRequest(params);
    // TODO modify to use shardhandler
    try (HttpSolrClient client = new Builder(masterUrl).withHttpClient(myHttpClient).build()) {
        client.setSoTimeout(soTimeout);
        client.setConnectionTimeout(connTimeout);
        return client.request(req);
    } catch (SolrServerException e) {
        throw new SolrException(ErrorCode.SERVER_ERROR, e.getMessage(), e);
    }
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) Builder(org.apache.solr.client.solrj.impl.HttpSolrClient.Builder) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrException(org.apache.solr.common.SolrException)

Example 3 with Builder

use of org.apache.solr.client.solrj.impl.HttpSolrClient.Builder in project lucene-solr by apache.

the class SolrEntityProcessor method firstInit.

@Override
protected void firstInit(Context context) {
    super.firstInit(context);
    try {
        String serverPath = context.getResolvedEntityAttribute(SOLR_SERVER);
        if (serverPath == null) {
            throw new DataImportHandlerException(DataImportHandlerException.SEVERE, "SolrEntityProcessor: parameter 'url' is required");
        }
        HttpClient client = getHttpClient();
        URL url = new URL(serverPath);
        // (wt="javabin|xml") default is javabin
        if ("xml".equals(context.getResolvedEntityAttribute(CommonParams.WT))) {
            // TODO: it doesn't matter for this impl when passing a client currently, but we should close this!
            solrClient = new Builder(url.toExternalForm()).withHttpClient(client).withResponseParser(new XMLResponseParser()).build();
            LOG.info("using XMLResponseParser");
        } else {
            // TODO: it doesn't matter for this impl when passing a client currently, but we should close this!
            solrClient = new Builder(url.toExternalForm()).withHttpClient(client).build();
            LOG.info("using BinaryResponseParser");
        }
    } catch (MalformedURLException e) {
        throw new DataImportHandlerException(DataImportHandlerException.SEVERE, e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HttpClient(org.apache.http.client.HttpClient) Builder(org.apache.solr.client.solrj.impl.HttpSolrClient.Builder) XMLResponseParser(org.apache.solr.client.solrj.impl.XMLResponseParser) URL(java.net.URL)

Example 4 with Builder

use of org.apache.solr.client.solrj.impl.HttpSolrClient.Builder in project lucene-solr by apache.

the class ZkController method waitForLeaderToSeeDownState.

private ZkCoreNodeProps waitForLeaderToSeeDownState(CoreDescriptor descriptor, final String coreZkNodeName) {
    // try not to wait too long here - if we are waiting too long, we should probably
    // move along and join the election
    CloudDescriptor cloudDesc = descriptor.getCloudDescriptor();
    String collection = cloudDesc.getCollectionName();
    String shard = cloudDesc.getShardId();
    ZkCoreNodeProps leaderProps = null;
    int retries = 2;
    for (int i = 0; i < retries; i++) {
        try {
            if (isClosed) {
                throw new SolrException(ErrorCode.SERVICE_UNAVAILABLE, "We have been closed");
            }
            // go straight to zk, not the cloud state - we want current info
            leaderProps = getLeaderProps(collection, shard, 5000);
            break;
        } catch (Exception e) {
            SolrException.log(log, "There was a problem finding the leader in zk", e);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e1) {
                Thread.currentThread().interrupt();
            }
            if (i == retries - 1) {
                throw new SolrException(ErrorCode.SERVER_ERROR, "There was a problem finding the leader in zk");
            }
        }
    }
    String leaderBaseUrl = leaderProps.getBaseUrl();
    String leaderCoreName = leaderProps.getCoreName();
    String myCoreNodeName = cloudDesc.getCoreNodeName();
    String myCoreName = descriptor.getName();
    String ourUrl = ZkCoreNodeProps.getCoreUrl(getBaseUrl(), myCoreName);
    boolean isLeader = leaderProps.getCoreUrl().equals(ourUrl);
    if (!isLeader && !SKIP_AUTO_RECOVERY) {
        // detect if this core is in leader-initiated recovery and if so, 
        // then we don't need the leader to wait on seeing the down state
        Replica.State lirState = null;
        try {
            lirState = getLeaderInitiatedRecoveryState(collection, shard, myCoreNodeName);
        } catch (Exception exc) {
            log.error("Failed to determine if replica " + myCoreNodeName + " is in leader-initiated recovery due to: " + exc, exc);
        }
        if (lirState != null) {
            log.debug("Replica " + myCoreNodeName + " is already in leader-initiated recovery, so not waiting for leader to see down state.");
        } else {
            log.info("Replica " + myCoreNodeName + " NOT in leader-initiated recovery, need to wait for leader to see down state.");
            try (HttpSolrClient client = new Builder(leaderBaseUrl).build()) {
                client.setConnectionTimeout(15000);
                client.setSoTimeout(120000);
                WaitForState prepCmd = new WaitForState();
                prepCmd.setCoreName(leaderCoreName);
                prepCmd.setNodeName(getNodeName());
                prepCmd.setCoreNodeName(coreZkNodeName);
                prepCmd.setState(Replica.State.DOWN);
                // let's retry a couple times - perhaps the leader just went down,
                // or perhaps he is just not quite ready for us yet
                retries = 2;
                for (int i = 0; i < retries; i++) {
                    if (isClosed) {
                        throw new SolrException(ErrorCode.SERVICE_UNAVAILABLE, "We have been closed");
                    }
                    try {
                        client.request(prepCmd);
                        break;
                    } catch (Exception e) {
                        // if the core container is shutdown, don't wait
                        if (cc.isShutDown()) {
                            throw new SolrException(ErrorCode.SERVICE_UNAVAILABLE, "Core container is shutdown.");
                        }
                        Throwable rootCause = SolrException.getRootCause(e);
                        if (rootCause instanceof IOException) {
                            // if there was a communication error talking to the leader, see if the leader is even alive
                            if (!zkStateReader.getClusterState().liveNodesContain(leaderProps.getNodeName())) {
                                throw new SolrException(ErrorCode.SERVICE_UNAVAILABLE, "Node " + leaderProps.getNodeName() + " hosting leader for " + shard + " in " + collection + " is not live!");
                            }
                        }
                        SolrException.log(log, "There was a problem making a request to the leader", e);
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e1) {
                            Thread.currentThread().interrupt();
                        }
                        if (i == retries - 1) {
                            throw new SolrException(ErrorCode.SERVER_ERROR, "There was a problem making a request to the leader");
                        }
                    }
                }
            } catch (IOException e) {
                SolrException.log(log, "Error closing HttpSolrClient", e);
            }
        }
    }
    return leaderProps;
}
Also used : WaitForState(org.apache.solr.client.solrj.request.CoreAdminRequest.WaitForState) ZkCoreNodeProps(org.apache.solr.common.cloud.ZkCoreNodeProps) Builder(org.apache.solr.client.solrj.impl.HttpSolrClient.Builder) IOException(java.io.IOException) Replica(org.apache.solr.common.cloud.Replica) TimeoutException(java.util.concurrent.TimeoutException) SolrException(org.apache.solr.common.SolrException) ZooKeeperException(org.apache.solr.common.cloud.ZooKeeperException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SessionExpiredException(org.apache.zookeeper.KeeperException.SessionExpiredException) ConnectionLossException(org.apache.zookeeper.KeeperException.ConnectionLossException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) SolrCoreInitializationException(org.apache.solr.core.SolrCoreInitializationException) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SolrException(org.apache.solr.common.SolrException)

Example 5 with Builder

use of org.apache.solr.client.solrj.impl.HttpSolrClient.Builder in project lucene-solr by apache.

the class HttpShardHandler method submit.

@Override
public void submit(final ShardRequest sreq, final String shard, final ModifiableSolrParams params) {
    // do this outside of the callable for thread safety reasons
    final List<String> urls = getURLs(shard);
    Callable<ShardResponse> task = () -> {
        ShardResponse srsp = new ShardResponse();
        if (sreq.nodeName != null) {
            srsp.setNodeName(sreq.nodeName);
        }
        srsp.setShardRequest(sreq);
        srsp.setShard(shard);
        SimpleSolrResponse ssr = new SimpleSolrResponse();
        srsp.setSolrResponse(ssr);
        long startTime = System.nanoTime();
        try {
            // use default (currently javabin)
            params.remove(CommonParams.WT);
            params.remove(CommonParams.VERSION);
            QueryRequest req = makeQueryRequest(sreq, params, shard);
            req.setMethod(SolrRequest.METHOD.POST);
            // if there are no shards available for a slice, urls.size()==0
            if (urls.size() == 0) {
                // all of the servers for a shard are down.
                throw new SolrException(SolrException.ErrorCode.SERVICE_UNAVAILABLE, "no servers hosting shard: " + shard);
            }
            if (urls.size() <= 1) {
                String url = urls.get(0);
                srsp.setShardAddress(url);
                try (SolrClient client = new Builder(url).withHttpClient(httpClient).build()) {
                    ssr.nl = client.request(req);
                }
            } else {
                LBHttpSolrClient.Rsp rsp = httpShardHandlerFactory.makeLoadBalancedRequest(req, urls);
                ssr.nl = rsp.getResponse();
                srsp.setShardAddress(rsp.getServer());
            }
        } catch (ConnectException cex) {
            //????
            srsp.setException(cex);
        } catch (Exception th) {
            srsp.setException(th);
            if (th instanceof SolrException) {
                srsp.setResponseCode(((SolrException) th).code());
            } else {
                srsp.setResponseCode(-1);
            }
        }
        ssr.elapsedTime = TimeUnit.MILLISECONDS.convert(System.nanoTime() - startTime, TimeUnit.NANOSECONDS);
        return transfomResponse(sreq, srsp, shard);
    };
    try {
        if (shard != null) {
            MDC.put("ShardRequest.shards", shard);
        }
        if (urls != null && !urls.isEmpty()) {
            MDC.put("ShardRequest.urlList", urls.toString());
        }
        pending.add(completionService.submit(task));
    } finally {
        MDC.remove("ShardRequest.shards");
        MDC.remove("ShardRequest.urlList");
    }
}
Also used : QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrClient(org.apache.solr.client.solrj.SolrClient) LBHttpSolrClient(org.apache.solr.client.solrj.impl.LBHttpSolrClient) Builder(org.apache.solr.client.solrj.impl.HttpSolrClient.Builder) SolrException(org.apache.solr.common.SolrException) SolrException(org.apache.solr.common.SolrException) ConnectException(java.net.ConnectException) ExecutionException(java.util.concurrent.ExecutionException) ConnectException(java.net.ConnectException)

Aggregations

Builder (org.apache.solr.client.solrj.impl.HttpSolrClient.Builder)6 SolrException (org.apache.solr.common.SolrException)3 IOException (java.io.IOException)2 SolrClient (org.apache.solr.client.solrj.SolrClient)2 SolrServerException (org.apache.solr.client.solrj.SolrServerException)2 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)2 QueryRequest (org.apache.solr.client.solrj.request.QueryRequest)2 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ConnectException (java.net.ConnectException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 UnknownHostException (java.net.UnknownHostException)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 HttpClient (org.apache.http.client.HttpClient)1 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)1 ResponseParser (org.apache.solr.client.solrj.ResponseParser)1 SolrQuery (org.apache.solr.client.solrj.SolrQuery)1 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)1