Search in sources :

Example 31 with CloudSolrClient

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

the class DeleteStatusTest method testDeleteStatusFlush.

@Test
public void testDeleteStatusFlush() throws Exception {
    final CloudSolrClient client = cluster.getSolrClient();
    String id1 = CollectionAdminRequest.createCollection("flush1", "conf1", 1, 1).processAsync(client);
    String id2 = CollectionAdminRequest.createCollection("flush2", "conf1", 1, 1).processAsync(client);
    assertEquals(RequestStatusState.COMPLETED, waitForRequestState(id1, client, MAX_WAIT_TIMEOUT));
    assertEquals(RequestStatusState.COMPLETED, waitForRequestState(id2, client, MAX_WAIT_TIMEOUT));
    CollectionAdminRequest.deleteAllAsyncIds().process(client);
    assertEquals(RequestStatusState.NOT_FOUND, CollectionAdminRequest.requestStatus(id1).process(client).getRequestStatus());
    assertEquals(RequestStatusState.NOT_FOUND, CollectionAdminRequest.requestStatus(id2).process(client).getRequestStatus());
}
Also used : CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) Test(org.junit.Test)

Example 32 with CloudSolrClient

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

the class TestCloudRecovery method leaderRecoverFromLogOnStartupTest.

@Test
public void leaderRecoverFromLogOnStartupTest() throws Exception {
    AtomicInteger countReplayLog = new AtomicInteger(0);
    DirectUpdateHandler2.commitOnClose = false;
    UpdateLog.testing_logReplayFinishHook = countReplayLog::incrementAndGet;
    CloudSolrClient cloudClient = cluster.getSolrClient();
    cloudClient.add(COLLECTION, sdoc("id", "1"));
    cloudClient.add(COLLECTION, sdoc("id", "2"));
    cloudClient.add(COLLECTION, sdoc("id", "3"));
    cloudClient.add(COLLECTION, sdoc("id", "4"));
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set("q", "*:*");
    QueryResponse resp = cloudClient.query(COLLECTION, params);
    assertEquals(0, resp.getResults().getNumFound());
    ChaosMonkey.stop(cluster.getJettySolrRunners());
    assertTrue("Timeout waiting for all not live", ClusterStateUtil.waitForAllReplicasNotLive(cloudClient.getZkStateReader(), 45000));
    ChaosMonkey.start(cluster.getJettySolrRunners());
    assertTrue("Timeout waiting for all live and active", ClusterStateUtil.waitForAllActiveAndLiveReplicas(cloudClient.getZkStateReader(), COLLECTION, 120000));
    resp = cloudClient.query(COLLECTION, params);
    assertEquals(4, resp.getResults().getNumFound());
    // Make sure all nodes is recover from tlog
    if (onlyLeaderIndexes) {
        // Leader election can be kicked off, so 2 tlog replicas will replay its tlog before becoming new leader
        assertTrue(countReplayLog.get() >= 2);
    } else {
        assertEquals(4, countReplayLog.get());
    }
    // check metrics
    int replicationCount = 0;
    int errorsCount = 0;
    int skippedCount = 0;
    for (JettySolrRunner jetty : cluster.getJettySolrRunners()) {
        SolrMetricManager manager = jetty.getCoreContainer().getMetricManager();
        List<String> registryNames = manager.registryNames().stream().filter(s -> s.startsWith("solr.core.")).collect(Collectors.toList());
        for (String registry : registryNames) {
            Map<String, Metric> metrics = manager.registry(registry).getMetrics();
            Timer timer = (Timer) metrics.get("REPLICATION.peerSync.time");
            Counter counter = (Counter) metrics.get("REPLICATION.peerSync.errors");
            Counter skipped = (Counter) metrics.get("REPLICATION.peerSync.skipped");
            replicationCount += timer.getCount();
            errorsCount += counter.getCount();
            skippedCount += skipped.getCount();
        }
    }
    if (onlyLeaderIndexes) {
        assertTrue(replicationCount >= 2);
    } else {
        assertEquals(2, replicationCount);
    }
}
Also used : Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) HashMap(java.util.HashMap) DirectUpdateHandler2(org.apache.solr.update.DirectUpdateHandler2) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ClusterStateUtil(org.apache.solr.common.cloud.ClusterStateUtil) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Counter(com.codahale.metrics.Counter) UpdateLog(org.apache.solr.update.UpdateLog) JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) Before(org.junit.Before) AfterClass(org.junit.AfterClass) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrCore(org.apache.solr.core.SolrCore) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) FileOutputStream(java.io.FileOutputStream) Metric(com.codahale.metrics.Metric) IOException(java.io.IOException) Test(org.junit.Test) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) File(java.io.File) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Timer(com.codahale.metrics.Timer) SolrMetricManager(org.apache.solr.metrics.SolrMetricManager) TestInjection(org.apache.solr.util.TestInjection) CollectionAdminRequest(org.apache.solr.client.solrj.request.CollectionAdminRequest) JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) Counter(com.codahale.metrics.Counter) Timer(com.codahale.metrics.Timer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrMetricManager(org.apache.solr.metrics.SolrMetricManager) Metric(com.codahale.metrics.Metric) Test(org.junit.Test)

Example 33 with CloudSolrClient

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

the class TestCloudRecovery method corruptedLogTest.

@Test
public void corruptedLogTest() throws Exception {
    AtomicInteger countReplayLog = new AtomicInteger(0);
    DirectUpdateHandler2.commitOnClose = false;
    UpdateLog.testing_logReplayFinishHook = countReplayLog::incrementAndGet;
    CloudSolrClient cloudClient = cluster.getSolrClient();
    cloudClient.add(COLLECTION, sdoc("id", "1000"));
    cloudClient.add(COLLECTION, sdoc("id", "1001"));
    for (int i = 0; i < 10; i++) {
        cloudClient.add(COLLECTION, sdoc("id", String.valueOf(i)));
    }
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set("q", "*:*");
    QueryResponse resp = cloudClient.query(COLLECTION, params);
    assertEquals(0, resp.getResults().getNumFound());
    int logHeaderSize = Integer.MAX_VALUE;
    Map<File, byte[]> contentFiles = new HashMap<>();
    for (JettySolrRunner solrRunner : cluster.getJettySolrRunners()) {
        for (SolrCore solrCore : solrRunner.getCoreContainer().getCores()) {
            File tlogFolder = new File(solrCore.getUlogDir(), UpdateLog.TLOG_NAME);
            String[] tLogFiles = tlogFolder.list();
            Arrays.sort(tLogFiles);
            File lastTLogFile = new File(tlogFolder.getAbsolutePath() + "/" + tLogFiles[tLogFiles.length - 1]);
            byte[] tlogBytes = IOUtils.toByteArray(new FileInputStream(lastTLogFile));
            contentFiles.put(lastTLogFile, tlogBytes);
            logHeaderSize = Math.min(tlogBytes.length, logHeaderSize);
        }
    }
    ChaosMonkey.stop(cluster.getJettySolrRunners());
    assertTrue("Timeout waiting for all not live", ClusterStateUtil.waitForAllReplicasNotLive(cloudClient.getZkStateReader(), 45000));
    for (Map.Entry<File, byte[]> entry : contentFiles.entrySet()) {
        byte[] tlogBytes = entry.getValue();
        if (tlogBytes.length <= logHeaderSize)
            continue;
        FileOutputStream stream = new FileOutputStream(entry.getKey());
        int skipLastBytes = Math.max(random().nextInt(tlogBytes.length - logHeaderSize), 2);
        for (int i = 0; i < entry.getValue().length - skipLastBytes; i++) {
            stream.write(tlogBytes[i]);
        }
        stream.close();
    }
    ChaosMonkey.start(cluster.getJettySolrRunners());
    assertTrue("Timeout waiting for all live and active", ClusterStateUtil.waitForAllActiveAndLiveReplicas(cloudClient.getZkStateReader(), COLLECTION, 120000));
    resp = cloudClient.query(COLLECTION, params);
    // Make sure cluster still healthy
    assertTrue(resp.getResults().getNumFound() >= 2);
}
Also used : HashMap(java.util.HashMap) JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) SolrCore(org.apache.solr.core.SolrCore) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) FileInputStream(java.io.FileInputStream) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) FileOutputStream(java.io.FileOutputStream) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 34 with CloudSolrClient

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

the class TestCollectionAPI method testCollectionCreationShardNameValidation.

private void testCollectionCreationShardNameValidation() throws Exception {
    try (CloudSolrClient client = createCloudClient(null)) {
        ModifiableSolrParams params = new ModifiableSolrParams();
        params.set("action", CollectionParams.CollectionAction.CREATE.toString());
        params.set("name", "valid_collection_name");
        params.set("router.name", "implicit");
        params.set("numShards", "1");
        params.set("shards", "invalid@name#with$weird%characters");
        SolrRequest request = new QueryRequest(params);
        request.setPath("/admin/collections");
        try {
            client.request(request);
            fail();
        } catch (RemoteSolrException e) {
            final String errorMessage = e.getMessage();
            assertTrue(errorMessage.contains("Invalid shard"));
            assertTrue(errorMessage.contains("invalid@name#with$weird%characters"));
            assertTrue(errorMessage.contains("shard names must consist entirely of"));
        }
    }
}
Also used : RemoteSolrException(org.apache.solr.client.solrj.impl.HttpSolrClient.RemoteSolrException) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) SolrRequest(org.apache.solr.client.solrj.SolrRequest) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient)

Example 35 with CloudSolrClient

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

the class TestCollectionAPI method test.

@Test
@ShardsFixed(num = 2)
public void test() throws Exception {
    try (CloudSolrClient client = createCloudClient(null)) {
        CollectionAdminRequest.Create req;
        if (useTlogReplicas()) {
            req = CollectionAdminRequest.createCollection(COLLECTION_NAME, "conf1", 2, 0, 1, 1);
        } else {
            req = CollectionAdminRequest.createCollection(COLLECTION_NAME, "conf1", 2, 1, 0, 1);
        }
        req.setMaxShardsPerNode(2);
        client.request(req);
        createCollection(null, COLLECTION_NAME1, 1, 1, 1, client, null, "conf1");
    }
    waitForCollection(cloudClient.getZkStateReader(), COLLECTION_NAME, 2);
    waitForCollection(cloudClient.getZkStateReader(), COLLECTION_NAME1, 1);
    waitForRecoveriesToFinish(COLLECTION_NAME, false);
    waitForRecoveriesToFinish(COLLECTION_NAME1, false);
    listCollection();
    clusterStatusNoCollection();
    clusterStatusWithCollection();
    clusterStatusWithCollectionAndShard();
    clusterStatusWithRouteKey();
    clusterStatusAliasTest();
    clusterStatusRolesTest();
    clusterStatusBadCollectionTest();
    replicaPropTest();
    clusterStatusZNodeVersion();
    testClusterStateMigration();
    testCollectionCreationCollectionNameValidation();
    testCollectionCreationShardNameValidation();
    testAliasCreationNameValidation();
    testShardCreationNameValidation();
}
Also used : CollectionAdminRequest(org.apache.solr.client.solrj.request.CollectionAdminRequest) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) Test(org.junit.Test)

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