Search in sources :

Example 86 with CloudSolrClient

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

the class TestCollectionStateWatchers method testWaitForStateWatcherIsRetainedOnPredicateFailure.

@Test
public void testWaitForStateWatcherIsRetainedOnPredicateFailure() throws Exception {
    CloudSolrClient client = cluster.getSolrClient();
    CollectionAdminRequest.createCollection("falsepredicate", "config", 4, 1).processAndWait(client, MAX_WAIT_TIMEOUT);
    client.waitForState("falsepredicate", MAX_WAIT_TIMEOUT, TimeUnit.SECONDS, (n, c) -> DocCollection.isFullyActive(n, c, 4, 1));
    final CountDownLatch firstCall = new CountDownLatch(1);
    // stop a node, then add a watch waiting for all nodes to be back up
    JettySolrRunner node1 = cluster.stopJettySolrRunner(random().nextInt(cluster.getJettySolrRunners().size()));
    Future<Boolean> future = waitInBackground("falsepredicate", MAX_WAIT_TIMEOUT, TimeUnit.SECONDS, (liveNodes, collectionState) -> {
        firstCall.countDown();
        return DocCollection.isFullyActive(liveNodes, collectionState, 4, 1);
    });
    // first, stop another node; the watch should not be fired after this!
    JettySolrRunner node2 = cluster.stopJettySolrRunner(random().nextInt(cluster.getJettySolrRunners().size()));
    // now start them both back up
    cluster.startJettySolrRunner(node1);
    assertTrue("CollectionStateWatcher not called after 30 seconds", firstCall.await(MAX_WAIT_TIMEOUT, TimeUnit.SECONDS));
    cluster.startJettySolrRunner(node2);
    Boolean result = future.get();
    assertTrue("Did not see a fully active cluster after 30 seconds", result);
}
Also used : JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) CountDownLatch(java.util.concurrent.CountDownLatch) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) Test(org.junit.Test)

Example 87 with CloudSolrClient

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

the class TestCollectionStateWatchers method testWaitForStateChecksCurrentState.

@Test
public void testWaitForStateChecksCurrentState() throws Exception {
    CloudSolrClient client = cluster.getSolrClient();
    CollectionAdminRequest.createCollection("waitforstate", "config", 1, 1).processAndWait(client, MAX_WAIT_TIMEOUT);
    client.waitForState("waitforstate", MAX_WAIT_TIMEOUT, TimeUnit.SECONDS, (n, c) -> DocCollection.isFullyActive(n, c, 1, 1));
    // several goes, to check that we're not getting delayed state changes
    for (int i = 0; i < 10; i++) {
        try {
            client.waitForState("waitforstate", 1, TimeUnit.SECONDS, (n, c) -> DocCollection.isFullyActive(n, c, 1, 1));
        } catch (TimeoutException e) {
            fail("waitForState should return immediately if the predicate is already satisfied");
        }
    }
}
Also used : CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 88 with CloudSolrClient

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

the class TestCollectionStateWatchers method testPredicateFailureTimesOut.

@Test
public void testPredicateFailureTimesOut() throws Exception {
    CloudSolrClient client = cluster.getSolrClient();
    expectThrows(TimeoutException.class, () -> {
        client.waitForState("nosuchcollection", 1, TimeUnit.SECONDS, ((liveNodes, collectionState) -> false));
    });
    waitFor("Watchers for collection should be removed after timeout", 1, TimeUnit.SECONDS, () -> client.getZkStateReader().getStateWatchers("nosuchcollection").isEmpty());
}
Also used : AfterClass(org.junit.AfterClass) Logger(org.slf4j.Logger) BeforeClass(org.junit.BeforeClass) MethodHandles(java.lang.invoke.MethodHandles) SolrCloudTestCase(org.apache.solr.cloud.SolrCloudTestCase) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) Callable(java.util.concurrent.Callable) Test(org.junit.Test) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Future(java.util.concurrent.Future) ExecutorUtil(org.apache.solr.common.util.ExecutorUtil) ExecutorService(java.util.concurrent.ExecutorService) JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) CollectionAdminRequest(org.apache.solr.client.solrj.request.CollectionAdminRequest) Before(org.junit.Before) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) Test(org.junit.Test)

Example 89 with CloudSolrClient

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

the class ShardSplitTest method splitByRouteKeyTest.

private void splitByRouteKeyTest() throws Exception {
    log.info("Starting splitByRouteKeyTest");
    String collectionName = "splitByRouteKeyTest";
    int numShards = 4;
    int replicationFactor = 2;
    int maxShardsPerNode = (((numShards * replicationFactor) / getCommonCloudSolrClient().getZkStateReader().getClusterState().getLiveNodes().size())) + 1;
    HashMap<String, List<Integer>> collectionInfos = new HashMap<>();
    try (CloudSolrClient client = createCloudClient(null)) {
        Map<String, Object> props = Utils.makeMap(REPLICATION_FACTOR, replicationFactor, MAX_SHARDS_PER_NODE, maxShardsPerNode, NUM_SLICES, numShards);
        createCollection(collectionInfos, collectionName, props, client);
    }
    List<Integer> list = collectionInfos.get(collectionName);
    checkForCollection(collectionName, list, null);
    waitForRecoveriesToFinish(false);
    String url = getUrlFromZk(getCommonCloudSolrClient().getZkStateReader().getClusterState(), collectionName);
    try (HttpSolrClient collectionClient = getHttpSolrClient(url)) {
        String splitKey = "b!";
        ClusterState clusterState = cloudClient.getZkStateReader().getClusterState();
        final DocRouter router = clusterState.getCollection(collectionName).getRouter();
        Slice shard1 = clusterState.getSlice(collectionName, SHARD1);
        DocRouter.Range shard1Range = shard1.getRange() != null ? shard1.getRange() : router.fullRange();
        final List<DocRouter.Range> ranges = ((CompositeIdRouter) router).partitionRangeByKey(splitKey, shard1Range);
        final int[] docCounts = new int[ranges.size()];
        int uniqIdentifier = (1 << 12);
        int splitKeyDocCount = 0;
        for (int i = 100; i <= 200; i++) {
            // See comment in ShardRoutingTest for hash distribution
            String shardKey = "" + (char) ('a' + (i % 26));
            String idStr = shardKey + "!" + i;
            collectionClient.add(getDoc(id, idStr, "n_ti", (shardKey + "!").equals(splitKey) ? uniqIdentifier : i));
            int idx = getHashRangeIdx(router, ranges, idStr);
            if (idx != -1) {
                docCounts[idx]++;
            }
            if (splitKey.equals(shardKey + "!"))
                splitKeyDocCount++;
        }
        for (int i = 0; i < docCounts.length; i++) {
            int docCount = docCounts[i];
            log.info("Shard {} docCount = {}", "shard1_" + i, docCount);
        }
        log.info("Route key doc count = {}", splitKeyDocCount);
        collectionClient.commit();
        for (int i = 0; i < 3; i++) {
            try {
                splitShard(collectionName, null, null, splitKey);
                break;
            } catch (HttpSolrClient.RemoteSolrException e) {
                if (e.code() != 500) {
                    throw e;
                }
                log.error("SPLITSHARD failed. " + (i < 2 ? " Retring split" : ""), e);
                if (i == 2) {
                    fail("SPLITSHARD was not successful even after three tries");
                }
            }
        }
        waitForRecoveriesToFinish(collectionName, false);
        SolrQuery solrQuery = new SolrQuery("*:*");
        assertEquals("DocCount on shard1_0 does not match", docCounts[0], collectionClient.query(solrQuery.setParam("shards", "shard1_0")).getResults().getNumFound());
        assertEquals("DocCount on shard1_1 does not match", docCounts[1], collectionClient.query(solrQuery.setParam("shards", "shard1_1")).getResults().getNumFound());
        assertEquals("DocCount on shard1_2 does not match", docCounts[2], collectionClient.query(solrQuery.setParam("shards", "shard1_2")).getResults().getNumFound());
        solrQuery = new SolrQuery("n_ti:" + uniqIdentifier);
        assertEquals("shard1_0 must have 0 docs for route key: " + splitKey, 0, collectionClient.query(solrQuery.setParam("shards", "shard1_0")).getResults().getNumFound());
        assertEquals("Wrong number of docs on shard1_1 for route key: " + splitKey, splitKeyDocCount, collectionClient.query(solrQuery.setParam("shards", "shard1_1")).getResults().getNumFound());
        assertEquals("shard1_2 must have 0 docs for route key: " + splitKey, 0, collectionClient.query(solrQuery.setParam("shards", "shard1_2")).getResults().getNumFound());
    }
}
Also used : ClusterState(org.apache.solr.common.cloud.ClusterState) HashMap(java.util.HashMap) SolrQuery(org.apache.solr.client.solrj.SolrQuery) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) CompositeIdRouter(org.apache.solr.common.cloud.CompositeIdRouter) Slice(org.apache.solr.common.cloud.Slice) DocRouter(org.apache.solr.common.cloud.DocRouter) List(java.util.List) ArrayList(java.util.ArrayList)

Example 90 with CloudSolrClient

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

the class TestAuthenticationFramework method collectionCreateSearchDelete.

public void collectionCreateSearchDelete(MiniSolrCloudCluster miniCluster) throws Exception {
    final String collectionName = "testcollection";
    final CloudSolrClient cloudSolrClient = miniCluster.getSolrClient();
    assertNotNull(miniCluster.getZkServer());
    List<JettySolrRunner> jettys = miniCluster.getJettySolrRunners();
    assertEquals(NUM_SERVERS, jettys.size());
    for (JettySolrRunner jetty : jettys) {
        assertTrue(jetty.isRunning());
    }
    // create collection
    log.info("#### Creating a collection");
    final String asyncId = (random().nextBoolean() ? null : "asyncId(" + collectionName + ".create)=" + random().nextInt());
    createCollection(miniCluster, collectionName, asyncId);
    ZkStateReader zkStateReader = miniCluster.getSolrClient().getZkStateReader();
    AbstractDistribZkTestBase.waitForRecoveriesToFinish(collectionName, zkStateReader, true, true, 330);
    // modify/query collection
    log.info("#### updating a querying collection");
    cloudSolrClient.setDefaultCollection(collectionName);
    SolrInputDocument doc = new SolrInputDocument();
    doc.setField("id", "1");
    cloudSolrClient.add(doc);
    cloudSolrClient.commit();
    SolrQuery query = new SolrQuery();
    query.setQuery("*:*");
    QueryResponse rsp = cloudSolrClient.query(query);
    assertEquals(1, rsp.getResults().getNumFound());
    // delete the collection we created earlier
    CollectionAdminRequest.deleteCollection(collectionName).process(miniCluster.getSolrClient());
    // create it again
    String asyncId2 = (random().nextBoolean() ? null : "asyncId(" + collectionName + ".create)=" + random().nextInt());
    createCollection(miniCluster, collectionName, asyncId2);
    AbstractDistribZkTestBase.waitForRecoveriesToFinish(collectionName, zkStateReader, true, true, 330);
    // check that there's no left-over state
    assertEquals(0, cloudSolrClient.query(new SolrQuery("*:*")).getResults().getNumFound());
    cloudSolrClient.add(doc);
    cloudSolrClient.commit();
    assertEquals(1, cloudSolrClient.query(new SolrQuery("*:*")).getResults().getNumFound());
}
Also used : ZkStateReader(org.apache.solr.common.cloud.ZkStateReader) SolrInputDocument(org.apache.solr.common.SolrInputDocument) JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrQuery(org.apache.solr.client.solrj.SolrQuery) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient)

Aggregations

CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)140 Test (org.junit.Test)52 ArrayList (java.util.ArrayList)40 SolrQuery (org.apache.solr.client.solrj.SolrQuery)30 HashMap (java.util.HashMap)26 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)25 SolrInputDocument (org.apache.solr.common.SolrInputDocument)25 CollectionAdminRequest (org.apache.solr.client.solrj.request.CollectionAdminRequest)24 Slice (org.apache.solr.common.cloud.Slice)24 JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)22 List (java.util.List)21 ZkStateReader (org.apache.solr.common.cloud.ZkStateReader)21 Map (java.util.Map)20 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)20 QueryRequest (org.apache.solr.client.solrj.request.QueryRequest)19 NamedList (org.apache.solr.common.util.NamedList)18 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)17 Replica (org.apache.solr.common.cloud.Replica)17 SolrRequest (org.apache.solr.client.solrj.SolrRequest)15 DocCollection (org.apache.solr.common.cloud.DocCollection)15