Search in sources :

Example 1 with JettySolrRunner

use of org.apache.solr.client.solrj.embedded.JettySolrRunner in project YCSB by brianfrankcooper.

the class SolrClientTest method getDB.

@Override
protected DB getDB(Properties props) {
    instance = new SolrClient();
    // Use the first Solr server in the cluster.
    // Doesn't matter if there are more since requests will be forwarded properly by Solr.
    JettySolrRunner jettySolrRunner = miniSolrCloudCluster.getJettySolrRunners().get(0);
    String solrBaseUrl = String.format("http://localhost:%s%s", jettySolrRunner.getLocalPort(), jettySolrRunner.getBaseUrl());
    props.setProperty("solr.base.url", solrBaseUrl);
    instance.setProperties(props);
    try {
        instance.init();
    } catch (Exception error) {
        assumeNoException(error);
    }
    return instance;
}
Also used : JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) Assume.assumeNoException(org.junit.Assume.assumeNoException)

Example 2 with JettySolrRunner

use of org.apache.solr.client.solrj.embedded.JettySolrRunner in project jackrabbit-oak by apache.

the class EmbeddedSolrServerProvider method createSolrServer.

private SolrServer createSolrServer() throws Exception {
    log.info("creating new embedded solr server with config: {}", solrServerConfiguration);
    String solrHomePath = solrServerConfiguration.getSolrHomePath();
    String coreName = solrServerConfiguration.getCoreName();
    EmbeddedSolrServerConfiguration.HttpConfiguration httpConfiguration = solrServerConfiguration.getHttpConfiguration();
    if (solrHomePath != null && coreName != null) {
        checkSolrConfiguration(solrHomePath, coreName);
        if (httpConfiguration != null) {
            if (log.isInfoEnabled()) {
                log.info("starting embedded Solr server with http bindings");
            }
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            Thread.currentThread().setContextClassLoader(JettySolrRunner.class.getClassLoader());
            Integer httpPort = httpConfiguration.getHttpPort();
            String context = httpConfiguration.getContext();
            JettySolrRunner jettySolrRunner = null;
            try {
                jettySolrRunner = new JettySolrRunner(solrHomePath, context, httpPort, "solrconfig.xml", "schema.xml", true);
                if (log.isInfoEnabled()) {
                    log.info("Jetty runner instantiated");
                }
                jettySolrRunner.start(true);
                if (log.isInfoEnabled()) {
                    log.info("Jetty runner started");
                }
            } catch (Exception t) {
                if (log.isErrorEnabled()) {
                    log.error("an error has occurred while starting Solr Jetty", t);
                }
            } finally {
                if (jettySolrRunner != null && !jettySolrRunner.isRunning()) {
                    try {
                        jettySolrRunner.stop();
                        if (log.isInfoEnabled()) {
                            log.info("Jetty runner stopped");
                        }
                    } catch (Exception e) {
                        if (log.isErrorEnabled()) {
                            log.error("error while stopping the Jetty runner", e);
                        }
                    }
                }
                Thread.currentThread().setContextClassLoader(classLoader);
            }
            if (log.isInfoEnabled()) {
                log.info("starting HTTP Solr server");
            }
            return new HttpWithJettySolrServer(SolrServerConfigurationDefaults.LOCAL_BASE_URL + ':' + httpPort + context + '/' + coreName, jettySolrRunner);
        } else {
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            Thread.currentThread().setContextClassLoader(CoreContainer.class.getClassLoader());
            CoreContainer coreContainer = new CoreContainer(solrHomePath);
            try {
                if (!coreContainer.isLoaded(coreName)) {
                    coreContainer.load();
                }
            } catch (Exception e) {
                log.error("cannot load core {}, shutting down embedded Solr..", coreName, e);
                try {
                    coreContainer.shutdown();
                } catch (Exception se) {
                    log.error("could not shutdown embedded Solr", se);
                }
                return null;
            } finally {
                Thread.currentThread().setContextClassLoader(classLoader);
            }
            EmbeddedSolrServer server = new EmbeddedSolrServer(coreContainer, coreName);
            if (server.ping().getStatus() == 0) {
                return server;
            } else {
                throw new IOException("the embedded Solr server is not alive");
            }
        }
    } else {
        throw new Exception("SolrServer configuration proprties not set");
    }
}
Also used : EmbeddedSolrServerConfiguration(org.apache.jackrabbit.oak.plugins.index.solr.configuration.EmbeddedSolrServerConfiguration) CoreContainer(org.apache.solr.core.CoreContainer) JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) IOException(java.io.IOException) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) IOException(java.io.IOException)

Example 3 with JettySolrRunner

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

the class HttpPartitionTest method testRf2.

protected void testRf2() throws Exception {
    // create a collection that has 1 shard but 2 replicas
    String testCollectionName = "c8n_1x2";
    createCollectionRetry(testCollectionName, 1, 2, 1);
    cloudClient.setDefaultCollection(testCollectionName);
    sendDoc(1);
    Replica notLeader = ensureAllReplicasAreActive(testCollectionName, "shard1", 1, 2, maxWaitSecsToSeeAllActive).get(0);
    // ok, now introduce a network partition between the leader and the replica
    SocketProxy proxy = getProxyForReplica(notLeader);
    proxy.close();
    // indexing during a partition
    sendDoc(2);
    // Have the partition last at least 1 sec
    // While this gives the impression that recovery is timing related, this is
    // really only
    // to give time for the state to be written to ZK before the test completes.
    // In other words,
    // without a brief pause, the test finishes so quickly that it doesn't give
    // time for the recovery process to kick-in
    Thread.sleep(sleepMsBeforeHealPartition);
    proxy.reopen();
    List<Replica> notLeaders = ensureAllReplicasAreActive(testCollectionName, "shard1", 1, 2, maxWaitSecsToSeeAllActive);
    sendDoc(3);
    // sent 3 docs in so far, verify they are on the leader and replica
    assertDocsExistInAllReplicas(notLeaders, testCollectionName, 1, 3);
    // Get the max version from the replica core to make sure it gets updated after recovery (see SOLR-7625)
    JettySolrRunner replicaJetty = getJettyOnPort(getReplicaPort(notLeader));
    CoreContainer coreContainer = replicaJetty.getCoreContainer();
    ZkCoreNodeProps replicaCoreNodeProps = new ZkCoreNodeProps(notLeader);
    String coreName = replicaCoreNodeProps.getCoreName();
    Long maxVersionBefore = null;
    try (SolrCore core = coreContainer.getCore(coreName)) {
        assertNotNull("Core '" + coreName + "' not found for replica: " + notLeader.getName(), core);
        UpdateLog ulog = core.getUpdateHandler().getUpdateLog();
        maxVersionBefore = ulog.getCurrentMaxVersion();
    }
    assertNotNull("max version bucket seed not set for core " + coreName, maxVersionBefore);
    log.info("Looked up max version bucket seed " + maxVersionBefore + " for core " + coreName);
    // now up the stakes and do more docs
    int numDocs = TEST_NIGHTLY ? 1000 : 100;
    boolean hasPartition = false;
    for (int d = 0; d < numDocs; d++) {
        // create / restore partition every 100 docs
        if (d % 10 == 0) {
            if (hasPartition) {
                proxy.reopen();
                hasPartition = false;
            } else {
                if (d >= 10) {
                    proxy.close();
                    hasPartition = true;
                    Thread.sleep(sleepMsBeforeHealPartition);
                }
            }
        }
        // 4 is offset as we've already indexed 1-3
        sendDoc(d + 4);
    }
    // restore connectivity if lost
    if (hasPartition) {
        proxy.reopen();
    }
    notLeaders = ensureAllReplicasAreActive(testCollectionName, "shard1", 1, 2, maxWaitSecsToSeeAllActive);
    try (SolrCore core = coreContainer.getCore(coreName)) {
        assertNotNull("Core '" + coreName + "' not found for replica: " + notLeader.getName(), core);
        Long currentMaxVersion = core.getUpdateHandler().getUpdateLog().getCurrentMaxVersion();
        log.info("After recovery, looked up NEW max version bucket seed " + currentMaxVersion + " for core " + coreName + ", was: " + maxVersionBefore);
        assertTrue("max version bucket seed not updated after recovery!", currentMaxVersion > maxVersionBefore);
    }
    // verify all docs received
    assertDocsExistInAllReplicas(notLeaders, testCollectionName, 1, numDocs + 3);
    log.info("testRf2 succeeded ... deleting the " + testCollectionName + " collection");
    // try to clean up
    attemptCollectionDelete(cloudClient, testCollectionName);
}
Also used : CoreContainer(org.apache.solr.core.CoreContainer) ZkCoreNodeProps(org.apache.solr.common.cloud.ZkCoreNodeProps) JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) SolrCore(org.apache.solr.core.SolrCore) UpdateLog(org.apache.solr.update.UpdateLog) Replica(org.apache.solr.common.cloud.Replica)

Example 4 with JettySolrRunner

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

the class DeleteInactiveReplicaTest method deleteInactiveReplicaTest.

@Test
public void deleteInactiveReplicaTest() throws Exception {
    String collectionName = "delDeadColl";
    int replicationFactor = 2;
    int numShards = 2;
    int maxShardsPerNode = ((((numShards + 1) * replicationFactor) / cluster.getJettySolrRunners().size())) + 1;
    CollectionAdminRequest.createCollection(collectionName, "conf", numShards, replicationFactor).setMaxShardsPerNode(maxShardsPerNode).process(cluster.getSolrClient());
    waitForState("Expected a cluster of 2 shards and 2 replicas", collectionName, (n, c) -> {
        return DocCollection.isFullyActive(n, c, numShards, replicationFactor);
    });
    DocCollection collectionState = getCollectionState(collectionName);
    Slice shard = getRandomShard(collectionState);
    Replica replica = getRandomReplica(shard);
    JettySolrRunner jetty = cluster.getReplicaJetty(replica);
    cluster.stopJettySolrRunner(jetty);
    waitForState("Expected replica " + replica.getName() + " on down node to be removed from cluster state", collectionName, (n, c) -> {
        Replica r = c.getReplica(replica.getCoreName());
        return r == null || r.getState() != Replica.State.ACTIVE;
    });
    log.info("Removing replica {}/{} ", shard.getName(), replica.getName());
    CollectionAdminRequest.deleteReplica(collectionName, shard.getName(), replica.getName()).process(cluster.getSolrClient());
    waitForState("Expected deleted replica " + replica.getName() + " to be removed from cluster state", collectionName, (n, c) -> {
        return c.getReplica(replica.getCoreName()) == null;
    });
    cluster.startJettySolrRunner(jetty);
    log.info("restarted jetty");
    CoreContainer cc = jetty.getCoreContainer();
    CoreContainer.CoreLoadFailure loadFailure = cc.getCoreInitFailures().get(replica.getCoreName());
    assertNotNull("Deleted core was still loaded!", loadFailure);
    assertTrue("Unexpected load failure message: " + loadFailure.exception.getMessage(), loadFailure.exception.getMessage().contains("does not exist in shard"));
    // Check that we can't create a core with no coreNodeName
    try (SolrClient queryClient = getHttpSolrClient(jetty.getBaseUrl().toString())) {
        Exception e = expectThrows(Exception.class, () -> {
            CoreAdminRequest.Create createRequest = new CoreAdminRequest.Create();
            createRequest.setCoreName("testcore");
            createRequest.setCollection(collectionName);
            createRequest.setShardId("shard2");
            queryClient.request(createRequest);
        });
        assertTrue("Unexpected error message: " + e.getMessage(), e.getMessage().contains("coreNodeName missing"));
    }
}
Also used : JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) CoreAdminRequest(org.apache.solr.client.solrj.request.CoreAdminRequest) Replica(org.apache.solr.common.cloud.Replica) CoreContainer(org.apache.solr.core.CoreContainer) SolrClient(org.apache.solr.client.solrj.SolrClient) Slice(org.apache.solr.common.cloud.Slice) DocCollection(org.apache.solr.common.cloud.DocCollection) Test(org.junit.Test)

Example 5 with JettySolrRunner

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

the class CollectionsAPIDistributedZkTest method testCreateNodeSet.

@Test
public void testCreateNodeSet() throws Exception {
    JettySolrRunner jetty1 = cluster.getRandomJetty(random());
    JettySolrRunner jetty2 = cluster.getRandomJetty(random());
    List<String> baseUrls = ImmutableList.of(jetty1.getBaseUrl().toString(), jetty2.getBaseUrl().toString());
    CollectionAdminRequest.createCollection("nodeset_collection", "conf", 2, 1).setCreateNodeSet(baseUrls.get(0) + "," + baseUrls.get(1)).process(cluster.getSolrClient());
    DocCollection collectionState = getCollectionState("nodeset_collection");
    for (Replica replica : collectionState.getReplicas()) {
        String replicaUrl = replica.getCoreUrl();
        boolean matchingJetty = false;
        for (String jettyUrl : baseUrls) {
            if (replicaUrl.startsWith(jettyUrl))
                matchingJetty = true;
        }
        if (matchingJetty == false)
            fail("Expected replica to be on " + baseUrls + " but was on " + replicaUrl);
    }
}
Also used : JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) DocCollection(org.apache.solr.common.cloud.DocCollection) Replica(org.apache.solr.common.cloud.Replica) Test(org.junit.Test)

Aggregations

JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)137 Test (org.junit.Test)52 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)29 Replica (org.apache.solr.common.cloud.Replica)28 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)25 SolrInputDocument (org.apache.solr.common.SolrInputDocument)20 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)20 File (java.io.File)19 SolrQuery (org.apache.solr.client.solrj.SolrQuery)19 DocCollection (org.apache.solr.common.cloud.DocCollection)18 Slice (org.apache.solr.common.cloud.Slice)18 IOException (java.io.IOException)15 ArrayList (java.util.ArrayList)15 Properties (java.util.Properties)15 SolrClient (org.apache.solr.client.solrj.SolrClient)15 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)15 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)15 CoreContainer (org.apache.solr.core.CoreContainer)14 BeforeClass (org.junit.BeforeClass)14 ClusterState (org.apache.solr.common.cloud.ClusterState)13