Skip to content

Commit

Permalink
[ #95 ] Working branch for Apache Solr 4.10.4
Browse files Browse the repository at this point in the history
  • Loading branch information
agazzarini committed Nov 5, 2019
1 parent 921b572 commit 58da4cc
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
under the License.
-->
<config>
<luceneMatchVersion>6.3.0</luceneMatchVersion>
<luceneMatchVersion>LATEST</luceneMatchVersion>
<dataDir>${solr.data.dir:}/${solr.core.name:}</dataDir>
<directoryFactory name="DirectoryFactory" class="${solr.directoryFactory:solr.NRTCachingDirectoryFactory}"/>
<codecFactory class="solr.SchemaCodecFactory"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
under the License.
-->
<config>
<luceneMatchVersion>6.3.0</luceneMatchVersion>
<dataDir>${solr.data.dir:}/${solr.core.name:}</dataDir>
<luceneMatchVersion>LATEST</luceneMatchVersion>
<dataDir>${solr.data.dir:data}/${solr.core.name:}</dataDir>
<directoryFactory name="DirectoryFactory" class="${solr.directoryFactory:solr.NRTCachingDirectoryFactory}"/>
<codecFactory class="solr.SchemaCodecFactory"/>
<schemaFactory class="ClassicIndexSchemaFactory"/>
Expand Down
2 changes: 1 addition & 1 deletion rre-maven-plugin/rre-maven-external-solr-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>rre-maven-external-solr-plugin</artifactId>
<version>7.6.0</version>
<version>4.10.4</version>
<packaging>maven-plugin</packaging>
<name>RRE - Maven External Apache Solr Plugin</name>
<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion rre-maven-plugin/rre-maven-solr-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>rre-maven-solr-plugin</artifactId>
<version>7.4.0</version>
<version>4.10.4</version>
<packaging>maven-plugin</packaging>
<name>RRE - Maven Apache Solr Plugin</name>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<artifactId>rre-search-platform-external-solr-impl</artifactId>
<name>RRE - External Solr search platform binding</name>

<version>7.6.0</version>
<version>4.10.4</version>

<dependencies>
<dependency>
Expand All @@ -25,6 +25,12 @@
<artifactId>solr-solrj</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging-api</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-test-framework</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public QueryOrSearchResponse executeQuery(String indexName, String queryString,
}

return of(clientManager.getSolrClient(indexName)
.query(indexSettingsMap.get(indexName).getCollectionName(), query, SolrRequest.METHOD.POST))
.query(query, SolrRequest.METHOD.POST))
.map(response ->
new QueryOrSearchResponse(
response.getResults().getNumFound(),
Expand Down Expand Up @@ -169,7 +169,7 @@ static class SolrSettings {
@JsonProperty("socketTimeoutMillis")
private final Integer socketTimeout;

public SolrSettings(@JsonProperty("baseUrls") List<String> baseUrls,
SolrSettings(@JsonProperty("baseUrls") List<String> baseUrls,
@JsonProperty("collectionName") String collectionName,
@JsonProperty("zkHosts") List<String> zkHosts,
@JsonProperty("zkChroot") String zkChroot,
Expand All @@ -195,31 +195,31 @@ private void validate() throws IllegalArgumentException {
}
}

public List<String> getBaseUrls() {
List<String> getBaseUrls() {
return baseUrls;
}

public String getCollectionName() {
return collectionName;
}

public List<String> getZkHosts() {
List<String> getZkHosts() {
return zkHosts;
}

public Optional<String> getZkChroot() {
return Optional.ofNullable(zkChroot);
}

public boolean hasZookeeperSettings() {
boolean hasZookeeperSettings() {
return zkHosts != null && !zkHosts.isEmpty();
}

public Integer getConnectionTimeout() {
Integer getConnectionTimeout() {
return connectionTimeout;
}

public Integer getSocketTimeout() {
Integer getSocketTimeout() {
return socketTimeout;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
*/
package io.sease.rre.search.api.impl;

import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.impl.SolrClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.impl.LBHttpSolrServer;

import java.io.Closeable;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.StringJoiner;

import static java.util.Optional.ofNullable;

/**
* Manager class for Solr Clients in use when connecting to external Solr instances.
Expand All @@ -35,9 +35,7 @@
*/
class SolrClientManager implements Closeable {

private static final Logger LOGGER = LoggerFactory.getLogger(SolrClientManager.class);

private final Map<String, SolrClient> indexClients = new HashMap<>();
private final Map<String, SolrServer> indexClients = new HashMap<>();

/**
* Build a SolrClient instance, associating it with a specific target index
Expand All @@ -49,41 +47,36 @@ class SolrClientManager implements Closeable {
* containing the client connection details.
*/
void buildSolrClient(String targetIndexName, ExternalApacheSolr.SolrSettings settings) {
final SolrClient client;
SolrServer client;

if (settings.hasZookeeperSettings()) {
final CloudSolrClient.Builder builder = new CloudSolrClient.Builder(settings.getZkHosts(), settings.getZkChroot());
client = applyTimeoutSettings(builder, settings).build();
final StringJoiner joiner = new StringJoiner(",");
settings.getZkHosts().forEach(joiner::add);

final CloudSolrServer solr = new CloudSolrServer(joiner.toString());
client = solr;
} else if (settings.getBaseUrls().size() > 1) {
final CloudSolrClient.Builder builder = new CloudSolrClient.Builder(settings.getBaseUrls());
client = applyTimeoutSettings(builder, settings).build();
try {
final LBHttpSolrServer solr = new LBHttpSolrServer(settings.getBaseUrls().toArray(new String[0]));
solr.setConnectionTimeout(settings.getConnectionTimeout());
solr.setSoTimeout(settings.getSocketTimeout());
client = solr;
} catch (Exception exception) {
HttpSolrServer solr = new HttpSolrServer(settings.getBaseUrls().get(0));
solr.setConnectionTimeout(settings.getConnectionTimeout());
solr.setSoTimeout(settings.getSocketTimeout());
client = solr;
}
} else {
final HttpSolrClient.Builder builder = new HttpSolrClient.Builder(settings.getBaseUrls().get(0));
client = applyTimeoutSettings(builder, settings).build();
HttpSolrServer solr = new HttpSolrServer(settings.getBaseUrls().iterator().next());
ofNullable(settings.getConnectionTimeout()).ifPresent(solr::setConnectionTimeout);
ofNullable(settings.getSocketTimeout()).ifPresent(solr::setSoTimeout);
client = solr;
}

indexClients.put(targetIndexName, client);
}

/**
* Apply the timeout settings using methods common to all SolrClientBuilder
* implementations.
*
* @param builder the SolrClientBuilder.
* @param settings the SolrSettings, containing the (optional) timeout settings.
* @param <C> the type of SolrClientBuilder in use.
* @return the SolrClientBuilder with the timeout settings applied.
*/
private <C extends SolrClientBuilder> C applyTimeoutSettings(C builder, ExternalApacheSolr.SolrSettings settings) {
if (settings.getConnectionTimeout() != null) {
builder.withConnectionTimeout(settings.getConnectionTimeout());
}
if (settings.getSocketTimeout() != null) {
builder.withSocketTimeout(settings.getSocketTimeout());
}
return builder;
}

/**
* Get the SolrClient for a specific target index.
*
Expand All @@ -92,20 +85,14 @@ private <C extends SolrClientBuilder> C applyTimeoutSettings(C builder, External
* @return the client, or {@code null} if no client has been set for the
* target index.
*/
SolrClient getSolrClient(String targetIndexName) {
SolrServer getSolrClient(String targetIndexName) {
return indexClients.get(targetIndexName);
}

/**
* Ensure that all of the index clients are closed.
*/
public void close() {
indexClients.values().forEach(c -> {
try {
c.close();
} catch (IOException e) {
LOGGER.error("Caught IOException closing client: {}", e.getMessage());
}
});
indexClients.values().forEach(SolrServer::shutdown);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
*/
package io.sease.rre.search.api.impl;

import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.cloud.MiniSolrCloudCluster;
import org.apache.solr.cloud.SolrCloudTestCase;
import org.apache.solr.client.solrj.impl.CloudSolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
Expand All @@ -28,8 +26,6 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -66,30 +62,7 @@ public void buildsHttpSolrClientForSingleHost() {
clientManager.buildSolrClient(TARGET_INDEX, settings);

assertNotNull(clientManager.getSolrClient(TARGET_INDEX));
assertTrue(clientManager.getSolrClient(TARGET_INDEX) instanceof HttpSolrClient);
}

@Test
public void buildsCloudSolrClientForMultipleHosts() throws Exception {
// Set a dummy log directory, to stop Solr complaining at start-up
System.setProperty("solr.log.dir", tempFolder.newFolder("logs").getAbsolutePath());
// Build a mini cluster to test with - cannot initialise CloudSolrClient without something to connect to
MiniSolrCloudCluster cluster = new SolrCloudTestCase.Builder(2, tempFolder.newFolder().toPath())
.build();
// Get the base URLs - doing it this way since jsr.getBaseUrl() doesn't return a valid URL
List<String> baseUrls = cluster.getJettySolrRunners().stream()
.map(jsr -> "http://localhost:" + jsr.getLocalPort() + "/solr")
.collect(Collectors.toList());

ExternalApacheSolr.SolrSettings settings = new ExternalApacheSolr.SolrSettings(baseUrls,
"test", null, null, null, null);

clientManager.buildSolrClient(TARGET_INDEX, settings);

assertNotNull(clientManager.getSolrClient(TARGET_INDEX));
assertTrue(clientManager.getSolrClient(TARGET_INDEX) instanceof CloudSolrClient);

cluster.shutdown();
assertTrue(clientManager.getSolrClient(TARGET_INDEX) instanceof HttpSolrServer);
}

@Test
Expand All @@ -100,6 +73,6 @@ public void buildsCloudSolrClientForZkHosts() {
clientManager.buildSolrClient(TARGET_INDEX, settings);

assertNotNull(clientManager.getSolrClient(TARGET_INDEX));
assertTrue(clientManager.getSolrClient(TARGET_INDEX) instanceof CloudSolrClient);
assertTrue(clientManager.getSolrClient(TARGET_INDEX) instanceof CloudSolrServer);
}
}
14 changes: 13 additions & 1 deletion rre-search-platform/rre-search-platform-solr-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>rre-search-platform-solr-impl</artifactId>
<version>7.4.0</version>
<version>4.10.4</version>
<name>RRE - Apache Solr platform binding</name>
<dependencies>
<dependency>
Expand All @@ -45,6 +45,18 @@
<artifactId>solr-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.10.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Loading

0 comments on commit 58da4cc

Please sign in to comment.