Search in sources :

Example 66 with QueryRequest

use of org.apache.solr.client.solrj.request.QueryRequest in project lucene-solr by apache.

the class CloudSolrClientTest method testNonRetryableRequests.

@Test
public void testNonRetryableRequests() throws Exception {
    try (CloudSolrClient client = getCloudSolrClient(cluster.getZkServer().getZkAddress())) {
        // important to have one replica on each node
        RequestStatusState state = CollectionAdminRequest.createCollection("foo", "conf", 1, NODE_COUNT).processAndWait(client, 60);
        if (state == RequestStatusState.COMPLETED) {
            AbstractDistribZkTestBase.waitForRecoveriesToFinish("foo", client.getZkStateReader(), true, true, TIMEOUT);
            client.setDefaultCollection("foo");
            Map<String, String> adminPathToMbean = new HashMap<>(CommonParams.ADMIN_PATHS.size());
            adminPathToMbean.put(CommonParams.COLLECTIONS_HANDLER_PATH, CollectionsHandler.class.getName());
            adminPathToMbean.put(CommonParams.CORES_HANDLER_PATH, CoreAdminHandler.class.getName());
            adminPathToMbean.put(CommonParams.CONFIGSETS_HANDLER_PATH, ConfigSetsHandler.class.getName());
            for (String adminPath : adminPathToMbean.keySet()) {
                long errorsBefore = 0;
                for (JettySolrRunner runner : cluster.getJettySolrRunners()) {
                    Long numRequests = getNumRequests(runner.getBaseUrl().toString(), "foo", "ADMIN", adminPathToMbean.get(adminPath), adminPath, true);
                    errorsBefore += numRequests;
                    log.info("Found {} requests to {} on {}", numRequests, adminPath, runner.getBaseUrl());
                }
                ModifiableSolrParams params = new ModifiableSolrParams();
                params.set("qt", adminPath);
                // this should cause an error
                params.set("action", "foobar");
                QueryRequest req = new QueryRequest(params);
                try {
                    NamedList<Object> resp = client.request(req);
                    fail("call to foo for admin path " + adminPath + " should have failed");
                } catch (Exception e) {
                // expected
                }
                long errorsAfter = 0;
                for (JettySolrRunner runner : cluster.getJettySolrRunners()) {
                    Long numRequests = getNumRequests(runner.getBaseUrl().toString(), "foo", "ADMIN", adminPathToMbean.get(adminPath), adminPath, true);
                    errorsAfter += numRequests;
                    log.info("Found {} requests to {} on {}", numRequests, adminPath, runner.getBaseUrl());
                }
                assertEquals(errorsBefore + 1, errorsAfter);
            }
        } else {
            fail("Collection could not be created within 60 seconds");
        }
    }
}
Also used : QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) HashMap(java.util.HashMap) CollectionsHandler(org.apache.solr.handler.admin.CollectionsHandler) JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) CoreAdminHandler(org.apache.solr.handler.admin.CoreAdminHandler) ConfigSetsHandler(org.apache.solr.handler.admin.ConfigSetsHandler) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) TimeoutException(java.util.concurrent.TimeoutException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrException(org.apache.solr.common.SolrException) ExpectedException(org.junit.rules.ExpectedException) IOException(java.io.IOException) RequestStatusState(org.apache.solr.client.solrj.response.RequestStatusState) Test(org.junit.Test)

Example 67 with QueryRequest

use of org.apache.solr.client.solrj.request.QueryRequest in project lucene-solr by apache.

the class GraphExpressionTest method testGraphHandler.

@Test
public void testGraphHandler() throws Exception {
    new UpdateRequest().add(id, "0", "from_s", "bill", "to_s", "jim", "message_t", "Hello jim").add(id, "1", "from_s", "bill", "to_s", "sam", "message_t", "Hello sam").add(id, "2", "from_s", "bill", "to_s", "max", "message_t", "Hello max").add(id, "3", "from_s", "max", "to_s", "kip", "message_t", "Hello kip").add(id, "4", "from_s", "sam", "to_s", "steve", "message_t", "Hello steve").add(id, "5", "from_s", "jim", "to_s", "ann", "message_t", "Hello steve").commit(cluster.getSolrClient(), COLLECTION);
    commit();
    List<JettySolrRunner> runners = cluster.getJettySolrRunners();
    JettySolrRunner runner = runners.get(0);
    String url = runner.getBaseUrl().toString();
    HttpSolrClient client = getHttpSolrClient(url);
    ModifiableSolrParams params = new ModifiableSolrParams();
    String expr = "sort(by=\"node asc\", gatherNodes(collection1, " + "walk=\"bill->from_s\"," + "trackTraversal=\"true\"," + "gather=\"to_s\"))";
    params.add("expr", expr);
    QueryRequest query = new QueryRequest(params);
    query.setPath("/collection1/graph");
    query.setResponseParser(new InputStreamResponseParser("xml"));
    query.setMethod(SolrRequest.METHOD.POST);
    NamedList<Object> genericResponse = client.request(query);
    InputStream stream = (InputStream) genericResponse.get("stream");
    InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
    String xml = readString(reader);
    //Validate the nodes
    String error = h.validateXPath(xml, "//graph/node[1][@id ='jim']", "//graph/node[2][@id ='max']", "//graph/node[3][@id ='sam']");
    if (error != null) {
        throw new Exception(error);
    }
    //Validate the edges
    error = h.validateXPath(xml, "//graph/edge[1][@source ='bill']", "//graph/edge[1][@target ='jim']", "//graph/edge[2][@source ='bill']", "//graph/edge[2][@target ='max']", "//graph/edge[3][@source ='bill']", "//graph/edge[3][@target ='sam']");
    if (error != null) {
        throw new Exception(error);
    }
    client.close();
}
Also used : QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) InputStreamReader(java.io.InputStreamReader) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) InputStreamResponseParser(org.apache.solr.client.solrj.impl.InputStreamResponseParser) JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) InputStream(java.io.InputStream) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) IOException(java.io.IOException) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) Test(org.junit.Test)

Example 68 with QueryRequest

use of org.apache.solr.client.solrj.request.QueryRequest in project lucene-solr by apache.

the class StressHdfsTest method createAndDeleteCollection.

private void createAndDeleteCollection() throws SolrServerException, IOException, Exception, KeeperException, InterruptedException, URISyntaxException {
    boolean overshard = random().nextBoolean();
    int rep;
    int nShards;
    int maxReplicasPerNode;
    if (overshard) {
        nShards = getShardCount() * 2;
        maxReplicasPerNode = 8;
        rep = 1;
    } else {
        nShards = getShardCount() / 2;
        maxReplicasPerNode = 1;
        rep = 2;
        if (nShards == 0)
            nShards = 1;
    }
    createCollection(DELETE_DATA_DIR_COLLECTION, nShards, rep, maxReplicasPerNode);
    waitForRecoveriesToFinish(DELETE_DATA_DIR_COLLECTION, false);
    // data dirs should be in zk, SOLR-8913
    ClusterState clusterState = cloudClient.getZkStateReader().getClusterState();
    Slice slice = clusterState.getSlice(DELETE_DATA_DIR_COLLECTION, "shard1");
    assertNotNull(clusterState.getSlices(DELETE_DATA_DIR_COLLECTION).toString(), slice);
    Collection<Replica> replicas = slice.getReplicas();
    for (Replica replica : replicas) {
        assertNotNull(replica.getProperties().toString(), replica.get("dataDir"));
        assertNotNull(replica.getProperties().toString(), replica.get("ulogDir"));
    }
    cloudClient.setDefaultCollection(DELETE_DATA_DIR_COLLECTION);
    cloudClient.getZkStateReader().forceUpdateCollection(DELETE_DATA_DIR_COLLECTION);
    for (int i = 1; i < nShards + 1; i++) {
        cloudClient.getZkStateReader().getLeaderRetry(DELETE_DATA_DIR_COLLECTION, "shard" + i, 30000);
    }
    // collect the data dirs
    List<String> dataDirs = new ArrayList<>();
    int i = 0;
    for (SolrClient client : clients) {
        try (HttpSolrClient c = getHttpSolrClient(getBaseUrl(client) + "/" + DELETE_DATA_DIR_COLLECTION)) {
            int docCnt = random().nextInt(1000) + 1;
            for (int j = 0; j < docCnt; j++) {
                c.add(getDoc("id", i++, "txt_t", "just some random text for a doc"));
            }
            if (random().nextBoolean()) {
                c.commit();
            } else {
                c.commit(true, true, true);
            }
            c.setConnectionTimeout(30000);
            NamedList<Object> response = c.query(new SolrQuery().setRequestHandler("/admin/system")).getResponse();
            NamedList<Object> coreInfo = (NamedList<Object>) response.get("core");
            String dataDir = (String) ((NamedList<Object>) coreInfo.get("directory")).get("data");
            dataDirs.add(dataDir);
        }
    }
    if (random().nextBoolean()) {
        cloudClient.deleteByQuery("*:*");
        cloudClient.commit();
        assertEquals(0, cloudClient.query(new SolrQuery("*:*")).getResults().getNumFound());
    }
    cloudClient.commit();
    cloudClient.query(new SolrQuery("*:*"));
    // delete collection
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set("action", CollectionAction.DELETE.toString());
    params.set("name", DELETE_DATA_DIR_COLLECTION);
    QueryRequest request = new QueryRequest(params);
    request.setPath("/admin/collections");
    cloudClient.request(request);
    final TimeOut timeout = new TimeOut(10, TimeUnit.SECONDS);
    while (cloudClient.getZkStateReader().getClusterState().hasCollection(DELETE_DATA_DIR_COLLECTION)) {
        if (timeout.hasTimedOut()) {
            throw new AssertionError("Timeout waiting to see removed collection leave clusterstate");
        }
        Thread.sleep(200);
    }
    // check that all dirs are gone
    for (String dataDir : dataDirs) {
        Configuration conf = HdfsTestUtil.getClientConfiguration(dfsCluster);
        conf.setBoolean("fs.hdfs.impl.disable.cache", true);
        FileSystem fs = FileSystem.get(new URI(HdfsTestUtil.getURI(dfsCluster)), conf);
        assertFalse("Data directory exists after collection removal : " + dataDir, fs.exists(new Path(dataDir)));
        fs.close();
    }
}
Also used : Path(org.apache.hadoop.fs.Path) ClusterState(org.apache.solr.common.cloud.ClusterState) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) Configuration(org.apache.hadoop.conf.Configuration) NamedList(org.apache.solr.common.util.NamedList) TimeOut(org.apache.solr.util.TimeOut) ArrayList(java.util.ArrayList) Replica(org.apache.solr.common.cloud.Replica) URI(java.net.URI) SolrQuery(org.apache.solr.client.solrj.SolrQuery) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SolrClient(org.apache.solr.client.solrj.SolrClient) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) Slice(org.apache.solr.common.cloud.Slice) FileSystem(org.apache.hadoop.fs.FileSystem)

Example 69 with QueryRequest

use of org.apache.solr.client.solrj.request.QueryRequest in project lucene-solr by apache.

the class TestHttpShardHandlerFactory method testLoadBalancerRequestsMinMax.

public void testLoadBalancerRequestsMinMax() throws Exception {
    final Path home = Paths.get(TEST_HOME());
    CoreContainer cc = null;
    ShardHandlerFactory factory = null;
    try {
        cc = CoreContainer.createAndLoad(home, home.resolve("solr-shardhandler-loadBalancerRequests.xml"));
        factory = cc.getShardHandlerFactory();
        // test that factory is HttpShardHandlerFactory with expected url reserve fraction
        assertTrue(factory instanceof HttpShardHandlerFactory);
        final HttpShardHandlerFactory httpShardHandlerFactory = ((HttpShardHandlerFactory) factory);
        assertEquals(expectedLoadBalancerRequestsMinimumAbsolute, httpShardHandlerFactory.permittedLoadBalancerRequestsMinimumAbsolute, 0.0);
        assertEquals(expectedLoadBalancerRequestsMaximumFraction, httpShardHandlerFactory.permittedLoadBalancerRequestsMaximumFraction, 0.0);
        // create a dummy request and dummy url list
        final QueryRequest queryRequest = null;
        final List<String> urls = new ArrayList<>();
        for (int ii = 0; ii < 10; ++ii) {
            urls.add(null);
        }
        // create LBHttpSolrClient request
        final LBHttpSolrClient.Req req = httpShardHandlerFactory.newLBHttpSolrClientReq(queryRequest, urls);
        // actual vs. expected test
        final int actualNumServersToTry = req.getNumServersToTry().intValue();
        int expectedNumServersToTry = (int) Math.floor(urls.size() * expectedLoadBalancerRequestsMaximumFraction);
        if (expectedNumServersToTry < expectedLoadBalancerRequestsMinimumAbsolute) {
            expectedNumServersToTry = expectedLoadBalancerRequestsMinimumAbsolute;
        }
        assertEquals("wrong numServersToTry for" + " urls.size=" + urls.size() + " expectedLoadBalancerRequestsMinimumAbsolute=" + expectedLoadBalancerRequestsMinimumAbsolute + " expectedLoadBalancerRequestsMaximumFraction=" + expectedLoadBalancerRequestsMaximumFraction, expectedNumServersToTry, actualNumServersToTry);
    } finally {
        if (factory != null)
            factory.close();
        if (cc != null)
            cc.shutdown();
    }
}
Also used : Path(java.nio.file.Path) HttpShardHandlerFactory(org.apache.solr.handler.component.HttpShardHandlerFactory) ShardHandlerFactory(org.apache.solr.handler.component.ShardHandlerFactory) CoreContainer(org.apache.solr.core.CoreContainer) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) ArrayList(java.util.ArrayList) HttpShardHandlerFactory(org.apache.solr.handler.component.HttpShardHandlerFactory) LBHttpSolrClient(org.apache.solr.client.solrj.impl.LBHttpSolrClient)

Example 70 with QueryRequest

use of org.apache.solr.client.solrj.request.QueryRequest in project lucene-solr by apache.

the class SearchHandlerTest method testZkConnected.

@Test
public void testZkConnected() throws Exception {
    MiniSolrCloudCluster miniCluster = new MiniSolrCloudCluster(5, createTempDir(), buildJettyConfig("/solr"));
    final CloudSolrClient cloudSolrClient = miniCluster.getSolrClient();
    try {
        assertNotNull(miniCluster.getZkServer());
        List<JettySolrRunner> jettys = miniCluster.getJettySolrRunners();
        assertEquals(5, jettys.size());
        for (JettySolrRunner jetty : jettys) {
            assertTrue(jetty.isRunning());
        }
        // create collection
        String collectionName = "testSolrCloudCollection";
        String configName = "solrCloudCollectionConfig";
        miniCluster.uploadConfigSet(SolrTestCaseJ4.TEST_PATH().resolve("collection1/conf"), configName);
        CollectionAdminRequest.createCollection(collectionName, configName, 2, 2).process(miniCluster.getSolrClient());
        QueryRequest req = new QueryRequest();
        QueryResponse rsp = req.process(cloudSolrClient, collectionName);
        assertTrue(rsp.getResponseHeader().getBooleanArg("zkConnected"));
    } finally {
        miniCluster.shutdown();
    }
}
Also used : QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) MiniSolrCloudCluster(org.apache.solr.cloud.MiniSolrCloudCluster) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) Test(org.junit.Test)

Aggregations

QueryRequest (org.apache.solr.client.solrj.request.QueryRequest)112 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)77 SolrRequest (org.apache.solr.client.solrj.SolrRequest)35 NamedList (org.apache.solr.common.util.NamedList)35 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)29 Test (org.junit.Test)28 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)21 ArrayList (java.util.ArrayList)20 HashMap (java.util.HashMap)17 Map (java.util.Map)17 SolrQuery (org.apache.solr.client.solrj.SolrQuery)17 IOException (java.io.IOException)16 SolrException (org.apache.solr.common.SolrException)15 SolrInputDocument (org.apache.solr.common.SolrInputDocument)14 SolrClient (org.apache.solr.client.solrj.SolrClient)13 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)12 List (java.util.List)11 SolrServerException (org.apache.solr.client.solrj.SolrServerException)8 RemoteSolrException (org.apache.solr.client.solrj.impl.HttpSolrClient.RemoteSolrException)8 SolrDocumentList (org.apache.solr.common.SolrDocumentList)8