Search in sources :

Example 1 with LBHttpSolrClient

use of org.apache.solr.client.solrj.impl.LBHttpSolrClient in project gora by apache.

the class SolrStore method initialize.

/**
   * Initialize the data store by reading the credentials, setting the client's properties up and
   * reading the mapping file. Initialize is called when then the call to
   * {@link org.apache.gora.store.DataStoreFactory#createDataStore} is made.
   *
   * @param keyClass
   * @param persistentClass
   * @param properties
   */
@Override
public void initialize(Class<K> keyClass, Class<T> persistentClass, Properties properties) {
    super.initialize(keyClass, persistentClass, properties);
    try {
        String mappingFile = DataStoreFactory.getMappingFile(properties, this, DEFAULT_MAPPING_FILE);
        mapping = readMapping(mappingFile);
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    }
    SolrClientUrl = DataStoreFactory.findProperty(properties, this, SOLR_URL_PROPERTY, null);
    solrConfig = DataStoreFactory.findProperty(properties, this, SOLR_CONFIG_PROPERTY, null);
    solrSchema = DataStoreFactory.findProperty(properties, this, SOLR_SCHEMA_PROPERTY, null);
    solrJServerImpl = DataStoreFactory.findProperty(properties, this, SOLR_SOLRJSERVER_IMPL, "http");
    serverUserAuth = DataStoreFactory.findBooleanProperty(properties, this, SOLR_SERVER_USER_AUTH, "false");
    if (serverUserAuth) {
        serverUsername = DataStoreFactory.findProperty(properties, this, SOLR_SERVER_USERNAME, null);
        serverPassword = DataStoreFactory.findProperty(properties, this, SOLR_SERVER_PASSWORD, null);
    }
    LOG.info("Using Solr server at " + SolrClientUrl);
    String solrJServerType = ((solrJServerImpl == null || solrJServerImpl.equals("")) ? "http" : solrJServerImpl);
    // HttpSolrClient - denoted by "http" in properties
    if (solrJServerType.toLowerCase(Locale.getDefault()).equals("http")) {
        LOG.info("Using HttpSolrClient Solrj implementation.");
        this.adminServer = new HttpSolrClient(SolrClientUrl);
        this.server = new HttpSolrClient(SolrClientUrl + "/" + mapping.getCoreName());
        if (serverUserAuth) {
            HttpClientUtil.setBasicAuth((DefaultHttpClient) ((HttpSolrClient) adminServer).getHttpClient(), serverUsername, serverPassword);
            HttpClientUtil.setBasicAuth((DefaultHttpClient) ((HttpSolrClient) server).getHttpClient(), serverUsername, serverPassword);
        }
    // CloudSolrClient - denoted by "cloud" in properties
    } else if (solrJServerType.toLowerCase(Locale.getDefault()).equals("cloud")) {
        LOG.info("Using CloudSolrClient Solrj implementation.");
        this.adminServer = new CloudSolrClient(SolrClientUrl);
        this.server = new CloudSolrClient(SolrClientUrl + "/" + mapping.getCoreName());
        if (serverUserAuth) {
            HttpClientUtil.setBasicAuth((DefaultHttpClient) ((CloudSolrClient) adminServer).getLbClient().getHttpClient(), serverUsername, serverPassword);
            HttpClientUtil.setBasicAuth((DefaultHttpClient) ((CloudSolrClient) server).getLbClient().getHttpClient(), serverUsername, serverPassword);
        }
    } else if (solrJServerType.toLowerCase(Locale.getDefault()).equals("concurrent")) {
        LOG.info("Using ConcurrentUpdateSolrClient Solrj implementation.");
        this.adminServer = new ConcurrentUpdateSolrClient(SolrClientUrl, 1000, 10);
        this.server = new ConcurrentUpdateSolrClient(SolrClientUrl + "/" + mapping.getCoreName(), 1000, 10);
    // LBHttpSolrClient - denoted by "loadbalance" in properties
    } else if (solrJServerType.toLowerCase(Locale.getDefault()).equals("loadbalance")) {
        LOG.info("Using LBHttpSolrClient Solrj implementation.");
        String[] solrUrlElements = StringUtils.split(SolrClientUrl);
        try {
            this.adminServer = new LBHttpSolrClient(solrUrlElements);
        } catch (MalformedURLException e) {
            LOG.error(e.getMessage());
            throw new RuntimeException(e);
        }
        try {
            this.server = new LBHttpSolrClient(solrUrlElements + "/" + mapping.getCoreName());
        } catch (MalformedURLException e) {
            LOG.error(e.getMessage());
            throw new RuntimeException(e);
        }
        if (serverUserAuth) {
            HttpClientUtil.setBasicAuth((DefaultHttpClient) ((LBHttpSolrClient) adminServer).getHttpClient(), serverUsername, serverPassword);
            HttpClientUtil.setBasicAuth((DefaultHttpClient) ((LBHttpSolrClient) server).getHttpClient(), serverUsername, serverPassword);
        }
    }
    if (autoCreateSchema) {
        createSchema();
    }
    String batchSizeString = DataStoreFactory.findProperty(properties, this, SOLR_BATCH_SIZE_PROPERTY, null);
    if (batchSizeString != null) {
        try {
            batchSize = Integer.parseInt(batchSizeString);
        } catch (NumberFormatException nfe) {
            LOG.warn("Invalid batch size '{}', using default {}", batchSizeString, DEFAULT_BATCH_SIZE);
        }
    }
    batch = new ArrayList<>(batchSize);
    String commitWithinString = DataStoreFactory.findProperty(properties, this, SOLR_COMMIT_WITHIN_PROPERTY, null);
    if (commitWithinString != null) {
        try {
            commitWithin = Integer.parseInt(commitWithinString);
        } catch (NumberFormatException nfe) {
            LOG.warn("Invalid commit within '{}' , using default {}", commitWithinString, DEFAULT_COMMIT_WITHIN);
        }
    }
    String resultsSizeString = DataStoreFactory.findProperty(properties, this, SOLR_RESULTS_SIZE_PROPERTY, null);
    if (resultsSizeString != null) {
        try {
            resultsSize = Integer.parseInt(resultsSizeString);
        } catch (NumberFormatException nfe) {
            LOG.warn("Invalid results size '{}' , using default {}", resultsSizeString, DEFAULT_RESULTS_SIZE);
        }
    }
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) LBHttpSolrClient(org.apache.solr.client.solrj.impl.LBHttpSolrClient) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ConcurrentUpdateSolrClient(org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrClient) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) LBHttpSolrClient(org.apache.solr.client.solrj.impl.LBHttpSolrClient) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient)

Example 2 with LBHttpSolrClient

use of org.apache.solr.client.solrj.impl.LBHttpSolrClient 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 3 with LBHttpSolrClient

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

the class TestLBHttpSolrClient method testTwoServers.

public void testTwoServers() throws Exception {
    LBHttpSolrClient client = getLBHttpSolrClient(httpClient, solr[0].getUrl(), solr[1].getUrl());
    client.setAliveCheckInterval(500);
    SolrQuery solrQuery = new SolrQuery("*:*");
    QueryResponse resp = null;
    solr[0].jetty.stop();
    solr[0].jetty = null;
    resp = client.query(solrQuery);
    String name = resp.getResults().get(0).getFieldValue("name").toString();
    Assert.assertEquals("solr/collection11", name);
    resp = client.query(solrQuery);
    name = resp.getResults().get(0).getFieldValue("name").toString();
    Assert.assertEquals("solr/collection11", name);
    solr[1].jetty.stop();
    solr[1].jetty = null;
    solr[0].startJetty();
    Thread.sleep(1200);
    try {
        resp = client.query(solrQuery);
    } catch (SolrServerException e) {
        // try again after a pause in case the error is lack of time to start server
        Thread.sleep(3000);
        resp = client.query(solrQuery);
    }
    name = resp.getResults().get(0).getFieldValue("name").toString();
    Assert.assertEquals("solr/collection10", name);
}
Also used : QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) LBHttpSolrClient(org.apache.solr.client.solrj.impl.LBHttpSolrClient)

Example 4 with LBHttpSolrClient

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

the class TestLBHttpSolrClient method testReliability.

public void testReliability() throws Exception {
    String[] s = new String[solr.length];
    for (int i = 0; i < solr.length; i++) {
        s[i] = solr[i].getUrl();
    }
    CloseableHttpClient myHttpClient = HttpClientUtil.createClient(null);
    try {
        LBHttpSolrClient client = getLBHttpSolrClient(myHttpClient, s);
        client.setConnectionTimeout(500);
        client.setSoTimeout(500);
        client.setAliveCheckInterval(500);
        // Kill a server and test again
        solr[1].jetty.stop();
        solr[1].jetty = null;
        // query the servers
        for (String value : s) client.query(new SolrQuery("*:*"));
        // Start the killed server once again
        solr[1].startJetty();
        // Wait for the alive check to complete
        waitForServer(30, client, 3, "solr1");
    } finally {
        HttpClientUtil.close(myHttpClient);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) LBHttpSolrClient(org.apache.solr.client.solrj.impl.LBHttpSolrClient)

Example 5 with LBHttpSolrClient

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

the class TestLBHttpSolrClient method testSimple.

public void testSimple() throws Exception {
    String[] s = new String[solr.length];
    for (int i = 0; i < solr.length; i++) {
        s[i] = solr[i].getUrl();
    }
    LBHttpSolrClient client = getLBHttpSolrClient(httpClient, s);
    client.setAliveCheckInterval(500);
    SolrQuery solrQuery = new SolrQuery("*:*");
    Set<String> names = new HashSet<>();
    QueryResponse resp = null;
    for (String value : s) {
        resp = client.query(solrQuery);
        assertEquals(10, resp.getResults().getNumFound());
        names.add(resp.getResults().get(0).getFieldValue("name").toString());
    }
    assertEquals(3, names.size());
    // Kill a server and test again
    solr[1].jetty.stop();
    solr[1].jetty = null;
    names.clear();
    for (String value : s) {
        resp = client.query(solrQuery);
        assertEquals(10, resp.getResults().getNumFound());
        names.add(resp.getResults().get(0).getFieldValue("name").toString());
    }
    assertEquals(2, names.size());
    assertFalse(names.contains("solr1"));
    // Start the killed server once again
    solr[1].startJetty();
    // Wait for the alive check to complete
    Thread.sleep(1200);
    names.clear();
    for (String value : s) {
        resp = client.query(solrQuery);
        assertEquals(10, resp.getResults().getNumFound());
        names.add(resp.getResults().get(0).getFieldValue("name").toString());
    }
    assertEquals(3, names.size());
}
Also used : QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) LBHttpSolrClient(org.apache.solr.client.solrj.impl.LBHttpSolrClient) HashSet(java.util.HashSet)

Aggregations

LBHttpSolrClient (org.apache.solr.client.solrj.impl.LBHttpSolrClient)6 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)1 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)1 ConcurrentUpdateSolrClient (org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrClient)1 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)1 Builder (org.apache.solr.client.solrj.impl.LBHttpSolrClient.Builder)1 QueryRequest (org.apache.solr.client.solrj.request.QueryRequest)1 CoreContainer (org.apache.solr.core.CoreContainer)1 HttpShardHandlerFactory (org.apache.solr.handler.component.HttpShardHandlerFactory)1 ShardHandlerFactory (org.apache.solr.handler.component.ShardHandlerFactory)1