[DS-2462] Don't close Solr core container until all users have finished.

Until 8.9, closing an EmbeddedSolrServer closes the CoreContainer, trashing
any other EmbeddedSolrServer instance that uses the same CoreContainer.
Solr's own tests subclass EmbeddedSolrServer to avoid this.  Now we do too.
This commit is contained in:
Mark H. Wood
2021-10-07 09:10:04 -04:00
parent c3a4457ba3
commit a8d1e3ac05

View File

@@ -47,10 +47,12 @@ public class MockSolrServer {
private static final Logger log = LogManager.getLogger(); private static final Logger log = LogManager.getLogger();
/** Shared embedded Solr connections, by name. */ /** Shared embedded Solr connections, by name. */
private static final ConcurrentMap<String, SolrClient> loadedCores = new ConcurrentHashMap<>(); private static final ConcurrentMap<String, SolrClient> loadedCores
= new ConcurrentHashMap<>();
/** Reference counts for each core. */ /** Reference counts for each core. */
private static final ConcurrentMap<String, AtomicLong> usersPerCore = new ConcurrentHashMap<>(); private static final ConcurrentMap<String, AtomicLong> usersPerCore
= new ConcurrentHashMap<>();
/** Container for embedded Solr cores. */ /** Container for embedded Solr cores. */
private static CoreContainer container = null; private static CoreContainer container = null;
@@ -81,7 +83,7 @@ public class MockSolrServer {
/** /**
* Ensure that this instance's core is loaded. Create it if necessary. * Ensure that this instance's core is loaded. Create it if necessary.
*/ */
protected void initSolrServer() { private void initSolrServer() {
solrServer = loadedCores.get(coreName); solrServer = loadedCores.get(coreName);
if (solrServer == null) { if (solrServer == null) {
solrServer = initSolrServerForCore(coreName); solrServer = initSolrServerForCore(coreName);
@@ -103,7 +105,12 @@ public class MockSolrServer {
if (server == null) { if (server == null) {
initSolrContainer(); initSolrContainer();
server = new EmbeddedSolrServer(container, coreName); server = new EmbeddedSolrServer(container, coreName) {
// This ugliness should be fixed in Solr 8.9.
@Override public void close() { // Copied from Solr's own tests
// Do not close shared core container!
}
};
//Start with an empty index //Start with an empty index
try { try {
@@ -123,6 +130,11 @@ public class MockSolrServer {
* Remove all records. * Remove all records.
*/ */
public void reset() { public void reset() {
if (null == solrServer) {
log.warn("reset called with no server connection");
return;
}
try { try {
solrServer.deleteByQuery("*:*"); solrServer.deleteByQuery("*:*");
} catch (SolrServerException | IOException ex) { } catch (SolrServerException | IOException ex) {
@@ -160,7 +172,8 @@ public class MockSolrServer {
private static synchronized void initSolrContainer() { private static synchronized void initSolrContainer() {
if (container == null) { if (container == null) {
Path solrDir = Paths.get(AbstractDSpaceIntegrationTest.getDspaceDir(), "solr"); Path solrDir = Paths.get(AbstractDSpaceIntegrationTest.getDspaceDir(), "solr");
log.info("Initializing SOLR CoreContainer with directory {}", solrDir.toAbsolutePath().toString()); log.info("Initializing SOLR CoreContainer with directory {}",
solrDir.toAbsolutePath().toString());
container = new CoreContainer(solrDir, new Properties()); container = new CoreContainer(solrDir, new Properties());
container.load(); container.load();
log.info("SOLR CoreContainer initialized"); log.info("SOLR CoreContainer initialized");