Search in sources :

Example 66 with SolrQuery

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

the class BasicHttpSolrClientTest method testTimeout.

@Test
public void testTimeout() throws Exception {
    SolrQuery q = new SolrQuery("*:*");
    try (HttpSolrClient client = getHttpSolrClient(jetty.getBaseUrl().toString() + "/slow/foo")) {
        client.setSoTimeout(2000);
        client.query(q, METHOD.GET);
        fail("No exception thrown.");
    } catch (SolrServerException e) {
        assertTrue(e.getMessage().contains("Timeout"));
    }
}
Also used : SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 67 with SolrQuery

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

the class ConcurrentUpdateSolrClientTest method testConcurrentCollectionUpdate.

@Test
public void testConcurrentCollectionUpdate() throws Exception {
    int cussThreadCount = 2;
    int cussQueueSize = 100;
    int numDocs = 100;
    int numRunnables = 5;
    int expected = numDocs * numRunnables;
    try (ConcurrentUpdateSolrClient concurrentClient = (new ConcurrentUpdateSolrClient.Builder(jetty.getBaseUrl().toString())).withQueueSize(cussQueueSize).withThreadCount(cussThreadCount).build()) {
        concurrentClient.setPollQueueTime(0);
        // ensure it doesn't block where there's nothing to do yet
        concurrentClient.blockUntilFinished();
        // Delete all existing documents.
        concurrentClient.deleteByQuery("collection1", "*:*");
        int poolSize = 5;
        ExecutorService threadPool = ExecutorUtil.newMDCAwareFixedThreadPool(poolSize, new SolrjNamedThreadFactory("testCUSS"));
        for (int r = 0; r < numRunnables; r++) threadPool.execute(new SendDocsRunnable(String.valueOf(r), numDocs, concurrentClient, "collection1"));
        // ensure all docs are sent
        threadPool.awaitTermination(5, TimeUnit.SECONDS);
        threadPool.shutdown();
        concurrentClient.commit("collection1");
        assertEquals(expected, concurrentClient.query("collection1", new SolrQuery("*:*")).getResults().getNumFound());
        // wait until all requests are processed by CUSS 
        concurrentClient.blockUntilFinished();
        concurrentClient.shutdownNow();
    }
    try (ConcurrentUpdateSolrClient concurrentClient = (new ConcurrentUpdateSolrClient.Builder(jetty.getBaseUrl().toString() + "/collection1")).withQueueSize(cussQueueSize).withThreadCount(cussThreadCount).build()) {
        assertEquals(expected, concurrentClient.query(new SolrQuery("*:*")).getResults().getNumFound());
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) SolrjNamedThreadFactory(org.apache.solr.common.util.SolrjNamedThreadFactory) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 68 with SolrQuery

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

the class TestCoreAdmin method testCoreSwap.

@Test
public void testCoreSwap() throws Exception {
    // index marker docs to core0
    SolrClient cli0 = getSolrCore0();
    SolrInputDocument d = new SolrInputDocument("id", "core0-0");
    cli0.add(d);
    d = new SolrInputDocument("id", "core0-1");
    cli0.add(d);
    cli0.commit();
    // index a marker doc to core1
    SolrClient cli1 = getSolrCore1();
    d = new SolrInputDocument("id", "core1-0");
    cli1.add(d);
    cli1.commit();
    // initial state assertions
    SolrQuery q = new SolrQuery("*:*");
    QueryResponse rsp = cli0.query(q);
    SolrDocumentList docs = rsp.getResults();
    assertEquals(2, docs.size());
    docs.forEach(doc -> {
        assertTrue(doc.toString(), doc.getFieldValue("id").toString().startsWith("core0-"));
    });
    rsp = cli1.query(q);
    docs = rsp.getResults();
    assertEquals(1, docs.size());
    docs.forEach(doc -> {
        assertTrue(doc.toString(), doc.getFieldValue("id").toString().startsWith("core1-"));
    });
    // assert initial metrics
    SolrMetricManager metricManager = cores.getMetricManager();
    String core0RegistryName = SolrCoreMetricManager.createRegistryName(false, null, null, null, "core0");
    String core1RegistryName = SolrCoreMetricManager.createRegistryName(false, null, null, null, "core1");
    MetricRegistry core0Registry = metricManager.registry(core0RegistryName);
    MetricRegistry core1Registry = metricManager.registry(core1RegistryName);
    // 2 docs + 1 commit
    assertEquals(3, core0Registry.counter("UPDATE./update.requests").getCount());
    // 1 doc + 1 commit
    assertEquals(2, core1Registry.counter("UPDATE./update.requests").getCount());
    // swap
    CoreAdminRequest.swapCore("core0", "core1", getSolrAdmin());
    // assert state after swap
    cli0 = getSolrCore0();
    cli1 = getSolrCore1();
    rsp = cli0.query(q);
    docs = rsp.getResults();
    assertEquals(1, docs.size());
    docs.forEach(doc -> {
        assertTrue(doc.toString(), doc.getFieldValue("id").toString().startsWith("core1-"));
    });
    rsp = cli1.query(q);
    docs = rsp.getResults();
    assertEquals(2, docs.size());
    docs.forEach(doc -> {
        assertTrue(doc.toString(), doc.getFieldValue("id").toString().startsWith("core0-"));
    });
    core0Registry = metricManager.registry(core0RegistryName);
    core1Registry = metricManager.registry(core1RegistryName);
    assertEquals(2, core0Registry.counter("UPDATE./update.requests").getCount());
    assertEquals(3, core1Registry.counter("UPDATE./update.requests").getCount());
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrClient(org.apache.solr.client.solrj.SolrClient) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) MetricRegistry(com.codahale.metrics.MetricRegistry) SolrMetricManager(org.apache.solr.metrics.SolrMetricManager) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 69 with SolrQuery

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

the class PeerSyncReplicationTest method test.

@Test
public void test() throws Exception {
    handle.clear();
    handle.put("timestamp", SKIPVAL);
    waitForThingsToLevelOut(30);
    del("*:*");
    // index enough docs and commit to establish frame of reference for PeerSync
    for (int i = 0; i < 100; i++) {
        indexDoc(id, docId, i1, 50, tlong, 50, t1, "document number " + docId++);
    }
    commit();
    waitForThingsToLevelOut(30);
    try {
        checkShardConsistency(false, true);
        long cloudClientDocs = cloudClient.query(new SolrQuery("*:*")).getResults().getNumFound();
        assertEquals(docId, cloudClientDocs);
        CloudJettyRunner initialLeaderJetty = shardToLeaderJetty.get("shard1");
        List<CloudJettyRunner> otherJetties = getOtherAvailableJetties(initialLeaderJetty);
        CloudJettyRunner neverLeader = otherJetties.get(otherJetties.size() - 1);
        otherJetties.remove(neverLeader);
        // first shutdown a node that will never be a leader
        forceNodeFailures(singletonList(neverLeader));
        // node failure and recovery via PeerSync
        log.info("Forcing PeerSync");
        CloudJettyRunner nodePeerSynced = forceNodeFailureAndDoPeerSync(false);
        // add a few more docs
        indexDoc(id, docId, i1, 50, tlong, 50, t1, "document number " + docId++);
        indexDoc(id, docId, i1, 50, tlong, 50, t1, "document number " + docId++);
        commit();
        cloudClientDocs = cloudClient.query(new SolrQuery("*:*")).getResults().getNumFound();
        assertEquals(docId, cloudClientDocs);
        // now shutdown all other nodes except for 'nodeShutDownForFailure'
        otherJetties.remove(nodePeerSynced);
        forceNodeFailures(otherJetties);
        waitForThingsToLevelOut(30);
        checkShardConsistency(false, true);
        // now shutdown the original leader
        log.info("Now shutting down initial leader");
        forceNodeFailures(singletonList(initialLeaderJetty));
        log.info("Updating mappings from zk");
        waitForNewLeader(cloudClient, "shard1", (Replica) initialLeaderJetty.client.info, new TimeOut(15, SECONDS));
        updateMappingsFromZk(jettys, clients, true);
        assertEquals("PeerSynced node did not become leader", nodePeerSynced, shardToLeaderJetty.get("shard1"));
        // bring up node that was down all along, and let it PeerSync from the node that was forced to PeerSynce  
        bringUpDeadNodeAndEnsureNoReplication(neverLeader, false);
        waitTillNodesActive();
        checkShardConsistency(false, true);
        // bring back all the nodes including initial leader 
        // (commented as reports Maximum concurrent create/delete watches above limit violation and reports thread leaks)
        /*for(int i = 0 ; i < nodesDown.size(); i++) {
        bringUpDeadNodeAndEnsureNoReplication(shardToLeaderJetty.get("shard1"), neverLeader, false);
      }
      checkShardConsistency(false, true);*/
        // make sure leader has not changed after bringing initial leader back
        assertEquals(nodePeerSynced, shardToLeaderJetty.get("shard1"));
        // assert metrics
        MetricRegistry registry = nodePeerSynced.jetty.getCoreContainer().getMetricManager().registry("solr.core.collection1");
        Map<String, Metric> metrics = registry.getMetrics();
        assertTrue("REPLICATION.time present", metrics.containsKey("REPLICATION.time"));
        assertTrue("REPLICATION.errors present", metrics.containsKey("REPLICATION.errors"));
        Timer timer = (Timer) metrics.get("REPLICATION.time");
        assertEquals(1L, timer.getCount());
        Counter counter = (Counter) metrics.get("REPLICATION.errors");
        assertEquals(0L, counter.getCount());
        success = true;
    } finally {
        System.clearProperty("solr.disableFingerprint");
    }
}
Also used : Counter(com.codahale.metrics.Counter) Timer(com.codahale.metrics.Timer) TimeOut(org.apache.solr.util.TimeOut) MetricRegistry(com.codahale.metrics.MetricRegistry) Metric(com.codahale.metrics.Metric) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 70 with SolrQuery

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

the class RecoveryZkTest method assertShardConsistency.

private void assertShardConsistency(Slice shard, boolean expectDocs) throws Exception {
    List<Replica> replicas = shard.getReplicas(r -> r.getState() == Replica.State.ACTIVE);
    long[] numCounts = new long[replicas.size()];
    int i = 0;
    for (Replica replica : replicas) {
        try (HttpSolrClient client = new HttpSolrClient.Builder(replica.getCoreUrl()).withHttpClient(cluster.getSolrClient().getHttpClient()).build()) {
            numCounts[i] = client.query(new SolrQuery("*:*").add("distrib", "false")).getResults().getNumFound();
            i++;
        }
    }
    for (int j = 1; j < replicas.size(); j++) {
        if (numCounts[j] != numCounts[j - 1])
            // TODO improve this!
            fail("Mismatch in counts between replicas");
        if (numCounts[j] == 0 && expectDocs)
            fail("Expected docs on shard " + shard.getName() + " but found none");
    }
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) Replica(org.apache.solr.common.cloud.Replica) SolrQuery(org.apache.solr.client.solrj.SolrQuery)

Aggregations

SolrQuery (org.apache.solr.client.solrj.SolrQuery)327 Test (org.junit.Test)174 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)129 SolrServerException (org.apache.solr.client.solrj.SolrServerException)53 SolrInputDocument (org.apache.solr.common.SolrInputDocument)52 SolrDocument (org.apache.solr.common.SolrDocument)47 ArrayList (java.util.ArrayList)46 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)45 IOException (java.io.IOException)36 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)33 SolrDocumentList (org.apache.solr.common.SolrDocumentList)33 SolrClient (org.apache.solr.client.solrj.SolrClient)27 LinearModel (org.apache.solr.ltr.model.LinearModel)24 Map (java.util.Map)23 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)23 Replica (org.apache.solr.common.cloud.Replica)23 Slice (org.apache.solr.common.cloud.Slice)23 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)21 JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)19 SolrException (org.apache.solr.common.SolrException)18