Search in sources :

Example 56 with HttpSolrClient

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

the class TestSolrJErrorHandling method testHttpURLConnection.

@Test
public void testHttpURLConnection() throws Exception {
    // sometimes succeeds with this size, but larger can cause OOM from command line
    String bodyString = getJsonDocs(200000);
    HttpSolrClient client = (HttpSolrClient) getSolrClient();
    String urlString = client.getBaseURL() + "/update";
    HttpURLConnection conn = null;
    URL url = new URL(urlString);
    conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setDoOutput(true);
    conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
    OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), StandardCharsets.UTF_8);
    writer.write(bodyString);
    writer.flush();
    int code = 1;
    try {
        code = conn.getResponseCode();
    } catch (Throwable th) {
        log.error("ERROR DURING conn.getResponseCode():", th);
    }
    /***
 java.io.IOException: Error writing to server
 at __randomizedtesting.SeedInfo.seed([2928C6EE314CD076:947A81A74F582526]:0)
 at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:665)
 at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:677)
 at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1533)
 at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1440)
 at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
 */
    log.info("CODE=" + code);
    InputStream is;
    if (code == 200) {
        is = conn.getInputStream();
    } else {
        log.info("Attempting to get error stream.");
        is = conn.getErrorStream();
        if (is == null) {
            log.info("Can't get error stream... try input stream?");
            is = conn.getInputStream();
        }
    }
    String rbody = IOUtils.toString(is, StandardCharsets.UTF_8);
    log.info("RESPONSE BODY:" + rbody);
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) OutputStreamWriter(java.io.OutputStreamWriter) URL(java.net.URL) Test(org.junit.Test)

Example 57 with HttpSolrClient

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

the class TestSolrJErrorHandling method testWithXml.

@Test
public void testWithXml() throws Exception {
    HttpSolrClient client = (HttpSolrClient) getSolrClient();
    client.setRequestWriter(new RequestWriter());
    // delete everything!
    client.deleteByQuery("*:*");
    doIt(client);
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) RequestWriter(org.apache.solr.client.solrj.request.RequestWriter) BinaryRequestWriter(org.apache.solr.client.solrj.impl.BinaryRequestWriter) Test(org.junit.Test)

Example 58 with HttpSolrClient

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

the class TestV2Request method testHttpSolrClient.

@Test
public void testHttpSolrClient() throws Exception {
    HttpSolrClient solrClient = new HttpSolrClient.Builder(cluster.getJettySolrRunner(0).getBaseUrl().toString()).build();
    doTest(solrClient);
    solrClient.close();
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) Test(org.junit.Test)

Example 59 with HttpSolrClient

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

the class ReplaceNodeTest method test.

@Test
public void test() throws Exception {
    cluster.waitForAllNodes(5000);
    String coll = "replacenodetest_coll";
    log.info("total_jettys: " + cluster.getJettySolrRunners().size());
    CloudSolrClient cloudClient = cluster.getSolrClient();
    Set<String> liveNodes = cloudClient.getZkStateReader().getClusterState().getLiveNodes();
    ArrayList<String> l = new ArrayList<>(liveNodes);
    Collections.shuffle(l, random());
    String emptyNode = l.remove(0);
    String node2bdecommissioned = l.get(0);
    CollectionAdminRequest.Create create;
    // NOTE: always using the createCollection that takes in 'int' for all types of replicas, so we never
    // have to worry about null checking when comparing the Create command with the final Slices
    create = pickRandom(CollectionAdminRequest.createCollection(coll, "conf1", 5, 2, 0, 0), CollectionAdminRequest.createCollection(coll, "conf1", 5, 1, 1, 0), CollectionAdminRequest.createCollection(coll, "conf1", 5, 0, 1, 1), CollectionAdminRequest.createCollection(coll, "conf1", 5, 1, 0, 1), CollectionAdminRequest.createCollection(coll, "conf1", 5, 0, 2, 0));
    create.setCreateNodeSet(StrUtils.join(l, ',')).setMaxShardsPerNode(3);
    cloudClient.request(create);
    log.info("excluded_node : {}  ", emptyNode);
    new CollectionAdminRequest.ReplaceNode(node2bdecommissioned, emptyNode).processAsync("000", cloudClient);
    CollectionAdminRequest.RequestStatus requestStatus = CollectionAdminRequest.requestStatus("000");
    boolean success = false;
    for (int i = 0; i < 200; i++) {
        CollectionAdminRequest.RequestStatusResponse rsp = requestStatus.process(cloudClient);
        if (rsp.getRequestStatus() == RequestStatusState.COMPLETED) {
            success = true;
            break;
        }
        assertFalse(rsp.getRequestStatus() == RequestStatusState.FAILED);
        Thread.sleep(50);
    }
    assertTrue(success);
    try (HttpSolrClient coreclient = getHttpSolrClient(cloudClient.getZkStateReader().getBaseUrlForNodeName(node2bdecommissioned))) {
        CoreAdminResponse status = CoreAdminRequest.getStatus(null, coreclient);
        assertTrue(status.getCoreStatus().size() == 0);
    }
    //let's do it back
    new CollectionAdminRequest.ReplaceNode(emptyNode, node2bdecommissioned).setParallel(Boolean.TRUE).processAsync("001", cloudClient);
    requestStatus = CollectionAdminRequest.requestStatus("001");
    for (int i = 0; i < 200; i++) {
        CollectionAdminRequest.RequestStatusResponse rsp = requestStatus.process(cloudClient);
        if (rsp.getRequestStatus() == RequestStatusState.COMPLETED) {
            success = true;
            break;
        }
        assertFalse(rsp.getRequestStatus() == RequestStatusState.FAILED);
        Thread.sleep(50);
    }
    assertTrue(success);
    try (HttpSolrClient coreclient = getHttpSolrClient(cloudClient.getZkStateReader().getBaseUrlForNodeName(emptyNode))) {
        CoreAdminResponse status = CoreAdminRequest.getStatus(null, coreclient);
        assertEquals("Expecting no cores but found some: " + status.getCoreStatus(), 0, status.getCoreStatus().size());
    }
    DocCollection collection = cloudClient.getZkStateReader().getClusterState().getCollection(coll);
    assertEquals(create.getNumShards().intValue(), collection.getSlices().size());
    for (Slice s : collection.getSlices()) {
        assertEquals(create.getNumNrtReplicas().intValue(), s.getReplicas(EnumSet.of(Replica.Type.NRT)).size());
        assertEquals(create.getNumTlogReplicas().intValue(), s.getReplicas(EnumSet.of(Replica.Type.TLOG)).size());
        assertEquals(create.getNumPullReplicas().intValue(), s.getReplicas(EnumSet.of(Replica.Type.PULL)).size());
    }
}
Also used : ArrayList(java.util.ArrayList) CollectionAdminRequest(org.apache.solr.client.solrj.request.CollectionAdminRequest) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) Slice(org.apache.solr.common.cloud.Slice) DocCollection(org.apache.solr.common.cloud.DocCollection) CoreAdminResponse(org.apache.solr.client.solrj.response.CoreAdminResponse) Test(org.junit.Test)

Example 60 with HttpSolrClient

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

the class ReplicationFactorTest method sendNonDirectUpdateRequestReplica.

@SuppressWarnings("rawtypes")
protected void sendNonDirectUpdateRequestReplica(Replica replica, UpdateRequest up, int expectedRf, String collection) throws Exception {
    ZkCoreNodeProps zkProps = new ZkCoreNodeProps(replica);
    String url = zkProps.getBaseUrl() + "/" + collection;
    try (HttpSolrClient solrServer = getHttpSolrClient(url)) {
        NamedList resp = solrServer.request(up);
        NamedList hdr = (NamedList) resp.get("responseHeader");
        Integer batchRf = (Integer) hdr.get(UpdateRequest.REPFACT);
        assertTrue("Expected rf=" + expectedRf + " for batch but got " + batchRf + "; clusterState: " + printClusterStateInfo(), batchRf == expectedRf);
    }
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) ZkCoreNodeProps(org.apache.solr.common.cloud.ZkCoreNodeProps) NamedList(org.apache.solr.common.util.NamedList)

Aggregations

HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)187 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)48 SolrClient (org.apache.solr.client.solrj.SolrClient)46 Test (org.junit.Test)46 SolrQuery (org.apache.solr.client.solrj.SolrQuery)38 ArrayList (java.util.ArrayList)35 Replica (org.apache.solr.common.cloud.Replica)34 SolrInputDocument (org.apache.solr.common.SolrInputDocument)31 IOException (java.io.IOException)26 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)25 Slice (org.apache.solr.common.cloud.Slice)25 QueryRequest (org.apache.solr.client.solrj.request.QueryRequest)24 SolrException (org.apache.solr.common.SolrException)23 SolrServerException (org.apache.solr.client.solrj.SolrServerException)21 JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)20 NamedList (org.apache.solr.common.util.NamedList)19 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)18 DocCollection (org.apache.solr.common.cloud.DocCollection)18 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)15 ZkCoreNodeProps (org.apache.solr.common.cloud.ZkCoreNodeProps)15