Apache Lucene open-source search software
SOLR-5903: SolrCore implements Closeable
Alan Woodward1dba8a6f6b1c
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 0877c41058d..13907c3d1c1 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -237,6 +237,9 @@ Other Changes* SOLR-5228: Don't require <field> or <dynamicField> be inside of <fields> -- orthat <fieldType> be inside of <types>. (Erick Erickson)+* SOLR-5903: SolrCore implements Closeable, cut over to using try-with-resources+ where possible. (Alan Woodward)+================== 4.7.1 ==================Versions of Major Componentsdiff --git a/solr/core/src/java/org/apache/solr/cloud/ElectionContext.java b/solr/core/src/java/org/apache/solr/cloud/ElectionContext.javaindex effddfdd353..6b9e0c7eb89 100644--- a/solr/core/src/java/org/apache/solr/cloud/ElectionContext.java+++ b/solr/core/src/java/org/apache/solr/cloud/ElectionContext.java@@ -1,9 +1,5 @@package org.apache.solr.cloud;-import java.io.IOException;-import java.util.Map;-import java.util.concurrent.TimeUnit;-import org.apache.lucene.search.MatchAllDocsQuery;import org.apache.solr.common.SolrException;import org.apache.solr.common.SolrException.ErrorCode;@@ -29,6 +25,10 @@ import org.apache.zookeeper.KeeperException.NodeExistsException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;+import java.io.IOException;+import java.util.Map;+import java.util.concurrent.TimeUnit;+/** Licensed to the Apache Software Foundation (ASF) under one or more* contributor license agreements. See the NOTICE file distributed with@@ -194,12 +194,9 @@ final class ShardLeaderElectionContext extends ShardLeaderElectionContextBase {if (!weAreReplacement) {waitForReplicasToComeUp(weAreReplacement, leaderVoteWait);}-- SolrCore core = null;- try {-- core = cc.getCore(coreName);-++ try (SolrCore core = cc.getCore(coreName)) {+if (core == null) {cancelElection();throw new SolrException(ErrorCode.SERVER_ERROR,@@ -287,10 +284,6 @@ final class ShardLeaderElectionContext extends ShardLeaderElectionContextBase {log.info("I am the new leader: "+ ZkCoreNodeProps.getCoreUrl(leaderProps) + " " + shardId);core.getCoreDescriptor().getCloudDescriptor().setLeader(true);- } finally {- if (core != null) {- core.close();- }}boolean success = false;try {@@ -299,8 +292,8 @@ final class ShardLeaderElectionContext extends ShardLeaderElectionContextBase {} catch (Exception e) {SolrException.log(log, "There was a problem trying to register as the leader", e);- try {- core = cc.getCore(coreName);+ try (SolrCore core = cc.getCore(coreName)) {+if (core == null) {throw new SolrException(ErrorCode.SERVER_ERROR,"Fatal Error, SolrCore not found:" + coreName + " in "@@ -312,16 +305,8 @@ final class ShardLeaderElectionContext extends ShardLeaderElectionContextBase {// we could not publish ourselves as leader - rejoin electionrejoinLeaderElection(leaderSeqPath, core);} finally {- try {- if (!success) {- cancelElection();- }- } finally {- if (core != null) {- core.close();- }- }-+ if (!success)+ cancelElection();}}diff --git a/solr/core/src/java/org/apache/solr/cloud/RecoveryStrategy.java b/solr/core/src/java/org/apache/solr/cloud/RecoveryStrategy.javaindex 895109a889e..4ee6a36d2ea 100644--- a/solr/core/src/java/org/apache/solr/cloud/RecoveryStrategy.java+++ b/solr/core/src/java/org/apache/solr/cloud/RecoveryStrategy.java@@ -17,14 +17,6 @@ package org.apache.solr.cloud;* limitations under the License.*/-import java.io.IOException;-import java.util.ArrayList;-import java.util.Arrays;-import java.util.Collections;-import java.util.List;-import java.util.concurrent.ExecutionException;-import java.util.concurrent.Future;-import org.apache.http.client.methods.HttpUriRequest;import org.apache.lucene.search.MatchAllDocsQuery;import org.apache.lucene.store.Directory;@@ -66,6 +58,14 @@ import org.apache.zookeeper.KeeperException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;+import java.io.IOException;+import java.util.ArrayList;+import java.util.Arrays;+import java.util.Collections;+import java.util.List;+import java.util.concurrent.ExecutionException;+import java.util.concurrent.Future;+public class RecoveryStrategy extends Thread implements ClosableThread {private static final int MAX_RETRIES = 500;private static final int INTERRUPTED = MAX_RETRIES + 1;@@ -216,14 +216,15 @@ public class RecoveryStrategy extends Thread implements ClosableThread {@Overridepublic void run() {- SolrCore core = cc.getCore(coreName);- if (core == null) {- SolrException.log(log, "SolrCore not found - cannot recover:" + coreName);- return;- }// set request info for logging- try {+ try (SolrCore core = cc.getCore(coreName)) {++ if (core == null) {+ SolrException.log(log, "SolrCore not found - cannot recover:" + coreName);+ return;+ }+SolrQueryRequest req = new LocalSolrQueryRequest(core, new ModifiableSolrParams());SolrQueryResponse rsp = new SolrQueryResponse();SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));@@ -243,7 +244,6 @@ public class RecoveryStrategy extends Thread implements ClosableThread {"", e);}} finally {- if (core != null) core.close();SolrRequestInfo.clearRequestInfo();}}diff --git a/solr/core/src/java/org/apache/solr/cloud/ZkController.java b/solr/core/src/java/org/apache/solr/cloud/ZkController.javaindex 3d88104f833..1bd3acfd7bd 100644--- a/solr/core/src/java/org/apache/solr/cloud/ZkController.java+++ b/solr/core/src/java/org/apache/solr/cloud/ZkController.java@@ -17,28 +17,6 @@ package org.apache.solr.cloud;* limitations under the License.*/-import java.io.File;-import java.io.IOException;-import java.io.UnsupportedEncodingException;-import java.net.InetAddress;-import java.net.NetworkInterface;-import java.net.URLEncoder;-import java.net.UnknownHostException;-import java.util.ArrayList;-import java.util.Collection;-import java.util.Collections;-import java.util.Enumeration;-import java.util.HashMap;-import java.util.Iterator;-import java.util.List;-import java.util.Locale;-import java.util.Map;-import java.util.Properties;-import java.util.Set;-import java.util.concurrent.Future;-import java.util.concurrent.TimeUnit;-import java.util.concurrent.TimeoutException;-import org.apache.commons.io.FileUtils;import org.apache.commons.lang.StringUtils;import org.apache.solr.client.solrj.impl.HttpSolrServer;@@ -74,6 +52,28 @@ import org.apache.zookeeper.data.Stat;import org.slf4j.Logger;import org.slf4j.LoggerFactory;+import java.io.File;+import java.io.IOException;+import java.io.UnsupportedEncodingException;+import java.net.InetAddress;+import java.net.NetworkInterface;+import java.net.URLEncoder;+import java.net.UnknownHostException;+import java.util.ArrayList;+import java.util.Collection;+import java.util.Collections;+import java.util.Enumeration;+import java.util.HashMap;+import java.util.Iterator;+import java.util.List;+import java.util.Locale;+import java.util.Map;+import java.util.Properties;+import java.util.Set;+import java.util.concurrent.Future;+import java.util.concurrent.TimeUnit;+import java.util.concurrent.TimeoutException;+/*** Handle ZooKeeper interactions.*@@ -802,13 +802,9 @@ public final class ZkController {String ourUrl = ZkCoreNodeProps.getCoreUrl(baseUrl, coreName);log.info("We are " + ourUrl + " and leader is " + leaderUrl);boolean isLeader = leaderUrl.equals(ourUrl);-- SolrCore core = null;- try {- core = cc.getCore(desc.getName());+ try (SolrCore core = cc.getCore(desc.getName())) {-// recover from local transaction log and wait for it to complete before// going active// TODO: should this be moved to another thread? To recoveryStrat?@@ -837,12 +833,7 @@ public final class ZkController {publish(desc, ZkStateReader.ACTIVE);}}- } finally {- if (core != null) {- core.close();- }}-// make sure we have an update cluster state right awayzkStateReader.updateClusterState(true);@@ -1028,16 +1019,10 @@ public final class ZkController {*/public void publish(final CoreDescriptor cd, final String state, boolean updateLastState, boolean forcePublish) throws KeeperException, InterruptedException {if (!forcePublish) {- SolrCore core = cc.getCore(cd.getName());- if (core == null) {- return;- }- try {- if (core.isClosed()) {+ try (SolrCore core = cc.getCore(cd.getName())) {+ if (core == null || core.isClosed()) {return;}- } finally {- core.close();}}String collection = cd.getCloudDescriptor().getCollectionName();diff --git a/solr/core/src/java/org/apache/solr/core/CoreContainer.java b/solr/core/src/java/org/apache/solr/core/CoreContainer.javaindex 56673d1e8cf..6d862454143 100644--- a/solr/core/src/java/org/apache/solr/core/CoreContainer.java+++ b/solr/core/src/java/org/apache/solr/core/CoreContainer.java@@ -692,18 +692,13 @@ public class CoreContainer {}public void rename(String name, String toName) {- SolrCore core = getCore(name);- try {+ try (SolrCore core = getCore(name)) {if (core != null) {registerCore(false, toName, core, false);name = checkDefault(name);SolrCore old = solrCores.remove(name, false);coresLocator.rename(this, old.getCoreDescriptor(), core.getCoreDescriptor());}- } finally {- if (core != null) {- core.close();- }}}diff --git a/solr/core/src/java/org/apache/solr/core/SolrCore.java b/solr/core/src/java/org/apache/solr/core/SolrCore.javaindex a89b2de09ef..3109fd1be6c 100644--- a/solr/core/src/java/org/apache/solr/core/SolrCore.java+++ b/solr/core/src/java/org/apache/solr/core/SolrCore.java@@ -99,6 +99,7 @@ import org.slf4j.LoggerFactory;import org.xml.sax.SAXException;import javax.xml.parsers.ParserConfigurationException;+import java.io.Closeable;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;@@ -137,7 +138,7 @@ import java.util.concurrent.locks.ReentrantLock;/****/-public final class SolrCore implements SolrInfoMBean {+public final class SolrCore implements SolrInfoMBean, Closeable {public static final String version="1.0";// These should *only* be used for debugging or monitoring purposesdiff --git a/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java b/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.javaindex e283a0da841..6309325f25a 100644--- a/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java+++ b/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java@@ -34,7 +34,6 @@ import org.apache.solr.common.SolrException.ErrorCode;import org.apache.solr.common.cloud.ClusterState;import org.apache.solr.common.cloud.DocCollection;import org.apache.solr.common.cloud.DocRouter;-import org.apache.solr.common.cloud.Replica;import org.apache.solr.common.cloud.Slice;import org.apache.solr.common.cloud.ZkNodeProps;import org.apache.solr.common.cloud.ZkStateReader;@@ -856,9 +855,8 @@ public class CoreAdminHandler extends RequestHandlerBase {if (cname == null) {cname = "";}- SolrCore core = null;- try {- core = coreContainer.getCore(cname);+ try (SolrCore core = coreContainer.getCore(cname)) {+if (core != null) {// try to publish as recovering right awaytry {@@ -877,11 +875,6 @@ public class CoreAdminHandler extends RequestHandlerBase {} else {SolrException.log(log, "Could not find core to call recovery:" + cname);}- } finally {- // no recoveryStrat close for now- if (core != null) {- core.close();- }}}};@@ -903,10 +896,10 @@ public class CoreAdminHandler extends RequestHandlerBase {if (cname == null) {throw new IllegalArgumentException(CoreAdminParams.CORE + " is required");}- SolrCore core = null;+SyncStrategy syncStrategy = null;- try {- core = coreContainer.getCore(cname);+ try (SolrCore core = coreContainer.getCore(cname)) {+if (core != null) {syncStrategy = new SyncStrategy(core.getCoreDescriptor().getCoreContainer());@@ -942,9 +935,6 @@ public class CoreAdminHandler extends RequestHandlerBase {}} finally {// no recoveryStrat close for now- if (core != null) {- core.close();- }if (syncStrategy != null) {syncStrategy.close();}@@ -977,9 +967,7 @@ public class CoreAdminHandler extends RequestHandlerBase {boolean live = false;int retry = 0;while (true) {- SolrCore core = null;- try {- core = coreContainer.getCore(cname);+ try (SolrCore core = coreContainer.getCore(cname)) {if (core == null && retry == 30) {throw new SolrException(ErrorCode.BAD_REQUEST, "core not found:"+ cname);@@ -1098,10 +1086,6 @@ public class CoreAdminHandler extends RequestHandlerBase {throw new SolrException(ErrorCode.SERVER_ERROR, null, e);}}- } finally {- if (core != null) {- core.close();- }}Thread.sleep(1000);}@@ -1113,9 +1097,10 @@ public class CoreAdminHandler extends RequestHandlerBase {private void handleRequestApplyUpdatesAction(SolrQueryRequest req, SolrQueryResponse rsp) {SolrParams params = req.getParams();String cname = params.get(CoreAdminParams.NAME, "");- SolrCore core = coreContainer.getCore(cname);log.info("Applying buffered updates on core: " + cname);- try {+ try (SolrCore core = coreContainer.getCore(cname)) {+ if (core == null)+ throw new SolrException(ErrorCode.BAD_REQUEST, "Core [" + cname + "] not found");UpdateLog updateLog = core.getUpdateHandler().getUpdateLog();if (updateLog.getState() != UpdateLog.State.BUFFERING) {throw new SolrException(ErrorCode.SERVER_ERROR, "Core " + cname + " not in buffering state");@@ -1145,8 +1130,6 @@ public class CoreAdminHandler extends RequestHandlerBase {throw new SolrException(ErrorCode.SERVER_ERROR, "Could not apply buffered updates", e);} finally {if (req != null) req.close();- if (core != null)- core.close();}}@@ -1154,9 +1137,11 @@ public class CoreAdminHandler extends RequestHandlerBase {private void handleRequestBufferUpdatesAction(SolrQueryRequest req, SolrQueryResponse rsp) {SolrParams params = req.getParams();String cname = params.get(CoreAdminParams.NAME, "");- SolrCore core = coreContainer.getCore(cname);log.info("Starting to buffer updates on core:" + cname);- try {++ try (SolrCore core = coreContainer.getCore(cname)) {+ if (core == null)+ throw new SolrException(ErrorCode.BAD_REQUEST, "Core [" + cname + "] does not exist");UpdateLog updateLog = core.getUpdateHandler().getUpdateLog();if (updateLog.getState() != UpdateLog.State.ACTIVE) {throw new SolrException(ErrorCode.SERVER_ERROR, "Core " + cname + " not in active state");@@ -1171,8 +1156,6 @@ public class CoreAdminHandler extends RequestHandlerBase {throw new SolrException(ErrorCode.SERVER_ERROR, "Could not start buffering updates", e);} finally {if (req != null) req.close();- if (core != null)- core.close();}}@@ -1204,9 +1187,8 @@ public class CoreAdminHandler extends RequestHandlerBase {info.add("isLoaded", "false");}} else {- SolrCore core = cores.getCore(cname);- if (core != null) {- try {+ try (SolrCore core = cores.getCore(cname)) {+ if (core != null) {info.add("name", core.getName());info.add("isDefaultCore", core.getName().equals(cores.getDefaultCoreName()));info.add("instanceDir", normalizePath(core.getResourceLoader().getInstanceDir()));@@ -1227,8 +1209,6 @@ public class CoreAdminHandler extends RequestHandlerBase {searcher.decref();}}- } finally {- core.close();}}}diff --git a/solr/core/src/test/org/apache/solr/AnalysisAfterCoreReloadTest.java b/solr/core/src/test/org/apache/solr/AnalysisAfterCoreReloadTest.javaindex 41a691f7605..a7284272fc4 100644--- a/solr/core/src/test/org/apache/solr/AnalysisAfterCoreReloadTest.java+++ b/solr/core/src/test/org/apache/solr/AnalysisAfterCoreReloadTest.java@@ -17,9 +17,6 @@ package org.apache.solr;* limitations under the License.*/-import java.io.File;-import java.io.IOException;-import org.apache.commons.io.FileUtils;import org.apache.solr.client.solrj.SolrQuery;import org.apache.solr.client.solrj.SolrServer;@@ -29,8 +26,11 @@ import org.apache.solr.client.solrj.request.QueryRequest;import org.apache.solr.client.solrj.request.UpdateRequest;import org.apache.solr.common.SolrInputDocument;import org.apache.solr.core.SolrCore;-import org.junit.BeforeClass;import org.junit.AfterClass;+import org.junit.BeforeClass;++import java.io.File;+import java.io.IOException;public class AnalysisAfterCoreReloadTest extends SolrTestCaseJ4 {@@ -116,26 +116,20 @@ public class AnalysisAfterCoreReloadTest extends SolrTestCaseJ4 {}private void overwriteStopwords(String stopwords) throws IOException {- SolrCore core = h.getCoreContainer().getCore(collection);- try {+ try (SolrCore core = h.getCoreContainer().getCore(collection)) {String configDir = core.getResourceLoader().getConfigDir();FileUtils.moveFile(new File(configDir, "stopwords.txt"), new File(configDir, "stopwords.txt.bak"));File file = new File(configDir, "stopwords.txt");FileUtils.writeStringToFile(file, stopwords, "UTF-8");- } finally {- core.close();}}@Overridepublic void tearDown() throws Exception {- SolrCore core = h.getCoreContainer().getCore(collection);String configDir;- try {+ try (SolrCore core = h.getCoreContainer().getCore(collection)) {configDir = core.getResourceLoader().getConfigDir();- } finally {- core.close();}super.tearDown();if (new File(configDir, "stopwords.txt.bak").exists()) {diff --git a/solr/core/src/test/org/apache/solr/core/CoreContainerCoreInitFailuresTest.java b/solr/core/src/test/org/apache/solr/core/CoreContainerCoreInitFailuresTest.javaindex 84a65735a3e..78fc6cf2919 100644--- a/solr/core/src/test/org/apache/solr/core/CoreContainerCoreInitFailuresTest.java+++ b/solr/core/src/test/org/apache/solr/core/CoreContainerCoreInitFailuresTest.java@@ -343,13 +343,9 @@ public class CoreContainerCoreInitFailuresTest extends SolrTestCaseJ4 {}- private final long getCoreStartTime(final CoreContainer cc,- final String name) {- SolrCore tmp = cc.getCore(name);- try {+ private long getCoreStartTime(final CoreContainer cc, final String name) {+ try (SolrCore tmp = cc.getCore(name)) {return tmp.getStartTime();- } finally {- tmp.close();}}diff --git a/solr/core/src/test/org/apache/solr/core/TestCoreDiscovery.java b/solr/core/src/test/org/apache/solr/core/TestCoreDiscovery.javaindex 7648b44f072..4bda2adb723 100644--- a/solr/core/src/test/org/apache/solr/core/TestCoreDiscovery.java+++ b/solr/core/src/test/org/apache/solr/core/TestCoreDiscovery.java@@ -142,28 +142,27 @@ public class TestCoreDiscovery extends SolrTestCaseJ4 {TestLazyCores.checkInCores(cc, "core1");TestLazyCores.checkNotInCores(cc, "lazy1", "core2", "collection1");- SolrCore core1 = cc.getCore("core1");+ // force loading of core2 and lazy1 by getting them from the CoreContainer+ try (SolrCore core1 = cc.getCore("core1");+ SolrCore core2 = cc.getCore("core2");+ SolrCore lazy1 = cc.getCore("lazy1")) {- // Let's assert we did the right thing for implicit properties too.- CoreDescriptor desc = core1.getCoreDescriptor();- assertEquals("core1", desc.getName());+ // Let's assert we did the right thing for implicit properties too.+ CoreDescriptor desc = core1.getCoreDescriptor();+ assertEquals("core1", desc.getName());- // This is too long and ugly to put in. Besides, it varies.- assertNotNull(desc.getInstanceDir());+ // This is too long and ugly to put in. Besides, it varies.+ assertNotNull(desc.getInstanceDir());- // Prove we're ignoring this even though it's set in the properties file- assertFalse("InstanceDir should be ignored", desc.getInstanceDir().contains("totallybogus"));+ // Prove we're ignoring this even though it's set in the properties file+ assertFalse("InstanceDir should be ignored", desc.getInstanceDir().contains("totallybogus"));- assertEquals("core1", desc.getDataDir());- assertEquals("solrconfig-minimal.xml", desc.getConfigName());- assertEquals("schema-tiny.xml", desc.getSchemaName());+ assertEquals("core1", desc.getDataDir());+ assertEquals("solrconfig-minimal.xml", desc.getConfigName());+ assertEquals("schema-tiny.xml", desc.getSchemaName());- SolrCore core2 = cc.getCore("core2");- SolrCore lazy1 = cc.getCore("lazy1");- TestLazyCores.checkInCores(cc, "core1", "core2", "lazy1");- core1.close();- core2.close();- lazy1.close();+ TestLazyCores.checkInCores(cc, "core1", "core2", "lazy1");+ }} finally {cc.shutdown();@@ -208,13 +207,10 @@ public class TestCoreDiscovery extends SolrTestCaseJ4 {addCoreWithProps(makeCorePropFile("core2", false, false, "dataDir=core2"),new File(alt, "core2" + File.separator + CorePropertiesLocator.PROPERTIES_FILENAME));CoreContainer cc = init();- try {- SolrCore core1 = cc.getCore("core1");- SolrCore core2 = cc.getCore("core2");+ try (SolrCore core1 = cc.getCore("core1");+ SolrCore core2 = cc.getCore("core2")) {assertNotNull(core1);assertNotNull(core2);- core1.close();- core2.close();} finally {cc.shutdown();if (alt.exists()) FileUtils.deleteDirectory(alt);@@ -231,13 +227,10 @@ public class TestCoreDiscovery extends SolrTestCaseJ4 {addCoreWithProps(makeCorePropFile("core2", false, false),new File(noCoreDir, "core2" + File.separator + CorePropertiesLocator.PROPERTIES_FILENAME));CoreContainer cc = init();- try {- SolrCore core1 = cc.getCore("core1");- SolrCore core2 = cc.getCore("core2");+ try (SolrCore core1 = cc.getCore("core1");+ SolrCore core2 = cc.getCore("core2")) {assertNotNull(core1);assertNotNull(core2);- core1.close();- core2.close();} finally {cc.shutdown();if (noCoreDir.exists()) FileUtils.deleteDirectory(noCoreDir);diff --git a/solr/core/src/test/org/apache/solr/core/TestJmxIntegration.java b/solr/core/src/test/org/apache/solr/core/TestJmxIntegration.javaindex a837d5662ff..444983ebed8 100644--- a/solr/core/src/test/org/apache/solr/core/TestJmxIntegration.java+++ b/solr/core/src/test/org/apache/solr/core/TestJmxIntegration.java@@ -16,19 +16,25 @@*/package org.apache.solr.core;-import java.lang.management.ManagementFactory;-import java.util.*;-import javax.management.*;--import org.apache.lucene.util.Constants;import org.apache.solr.core.JmxMonitoredMap.SolrDynamicMBean;import org.apache.solr.util.AbstractSolrTestCase;-import org.junit.Assume;import org.junit.AfterClass;import org.junit.BeforeClass;import org.junit.Ignore;import org.junit.Test;+import javax.management.AttributeNotFoundException;+import javax.management.MBeanAttributeInfo;+import javax.management.MBeanInfo;+import javax.management.MBeanServer;+import javax.management.MalformedObjectNameException;+import javax.management.ObjectInstance;+import javax.management.ObjectName;+import java.lang.management.ManagementFactory;+import java.util.Hashtable;+import java.util.Map;+import java.util.Set;+/*** Test for JMX Integration*@@ -156,8 +162,7 @@ public class TestJmxIntegration extends AbstractSolrTestCase {Set<ObjectInstance> newBeans = mbeanServer.queryMBeans(null, null);int newNumberOfObjects = 0;int registrySize = 0;- SolrCore core = h.getCoreContainer().getCore(coreName);- try {+ try (SolrCore core = h.getCoreContainer().getCore(coreName)) {registrySize = core.getInfoRegistry().size();for (ObjectInstance bean : newBeans) {try {@@ -168,8 +173,6 @@ public class TestJmxIntegration extends AbstractSolrTestCase {// expected}}- } finally {- core.close();}log.info("After Reload: Size of infoRegistry: " + registrySize + " MBeans: " + newNumberOfObjects);diff --git a/solr/core/src/test/org/apache/solr/core/TestSolrXmlPersistence.java b/solr/core/src/test/org/apache/solr/core/TestSolrXmlPersistence.javaindex 2977e7d631a..ea0fb3c1909 100644--- a/solr/core/src/test/org/apache/solr/core/TestSolrXmlPersistence.java+++ b/solr/core/src/test/org/apache/solr/core/TestSolrXmlPersistence.java@@ -410,15 +410,10 @@ public class TestSolrXmlPersistence extends SolrTestCaseJ4 {SolrXMLCoresLocator.NonPersistingLocator locator= (SolrXMLCoresLocator.NonPersistingLocator) cores.getCoresLocator();- String instDir = null;- {- SolrCore template = null;- try {- template = cores.getCore("collection1");- instDir = template.getCoreDescriptor().getRawInstanceDir();- } finally {- if (null != template) template.close();- }+ String instDir;+ try (SolrCore template = cores.getCore("collection1")) {+ assertNotNull(template);+ instDir = template.getCoreDescriptor().getRawInstanceDir();}final File instDirFile = new File(cores.getSolrHome(), instDir);diff --git a/solr/core/src/test/org/apache/solr/handler/admin/CoreAdminHandlerTest.java b/solr/core/src/test/org/apache/solr/handler/admin/CoreAdminHandlerTest.javaindex aee061726a4..5954e773ab1 100644--- a/solr/core/src/test/org/apache/solr/handler/admin/CoreAdminHandlerTest.java+++ b/solr/core/src/test/org/apache/solr/handler/admin/CoreAdminHandlerTest.java@@ -144,15 +144,10 @@ public class CoreAdminHandlerTest extends SolrTestCaseJ4 {final CoreAdminHandler admin = new CoreAdminHandler(cores);- String instDir = null;- {- SolrCore template = null;- try {- template = cores.getCore("collection1");- instDir = template.getCoreDescriptor().getInstanceDir();- } finally {- if (null != template) template.close();- }+ String instDir;+ try (SolrCore template = cores.getCore("collection1")) {+ assertNotNull(template);+ instDir = template.getCoreDescriptor().getInstanceDir();}final File instDirFile = new File(instDir);diff --git a/solr/core/src/test/org/apache/solr/handler/admin/CoreAdminRequestStatusTest.java b/solr/core/src/test/org/apache/solr/handler/admin/CoreAdminRequestStatusTest.javaindex 5081f817a38..091398b4ba6 100644--- a/solr/core/src/test/org/apache/solr/handler/admin/CoreAdminRequestStatusTest.java+++ b/solr/core/src/test/org/apache/solr/handler/admin/CoreAdminRequestStatusTest.java@@ -48,15 +48,10 @@ public class CoreAdminRequestStatusTest extends SolrTestCaseJ4{final CoreAdminHandler admin = new CoreAdminHandler(cores);- String instDir = null;- {- SolrCore template = null;- try {- template = cores.getCore("collection1");- instDir = template.getCoreDescriptor().getInstanceDir();- } finally {- if (null != template) template.close();- }+ String instDir;+ try (SolrCore template = cores.getCore("collection1")) {+ assertNotNull(template);+ instDir = template.getCoreDescriptor().getInstanceDir();}final File instDirFile = new File(instDir);diff --git a/solr/core/src/test/org/apache/solr/handler/admin/CoreMergeIndexesAdminHandlerTest.java b/solr/core/src/test/org/apache/solr/handler/admin/CoreMergeIndexesAdminHandlerTest.javaindex 4c521cb085f..c9ceeb582a5 100644--- a/solr/core/src/test/org/apache/solr/handler/admin/CoreMergeIndexesAdminHandlerTest.java+++ b/solr/core/src/test/org/apache/solr/handler/admin/CoreMergeIndexesAdminHandlerTest.java@@ -34,7 +34,6 @@ import org.junit.rules.TestRule;import java.io.File;import java.io.IOException;-import java.util.Map;public class CoreMergeIndexesAdminHandlerTest extends SolrTestCaseJ4 {@@ -80,8 +79,7 @@ public class CoreMergeIndexesAdminHandlerTest extends SolrTestCaseJ4 {final CoreAdminHandler admin = new CoreAdminHandler(cores);- SolrCore core = cores.getCore("collection1");- try {+ try (SolrCore core = cores.getCore("collection1")) {FailingDirectoryFactory dirFactory = (FailingDirectoryFactory)core.getDirectoryFactory();try {@@ -102,8 +100,6 @@ public class CoreMergeIndexesAdminHandlerTest extends SolrTestCaseJ4 {unIgnoreException(FAILING_MSG);}dirFactory.fail = false;- } finally {- core.close();}// cleanupdiff --git a/solr/core/src/test/org/apache/solr/schema/ChangedSchemaMergeTest.java b/solr/core/src/test/org/apache/solr/schema/ChangedSchemaMergeTest.javaindex 6a09ef8ebc5..d172cb3be76 100644--- a/solr/core/src/test/org/apache/solr/schema/ChangedSchemaMergeTest.java+++ b/solr/core/src/test/org/apache/solr/schema/ChangedSchemaMergeTest.java@@ -75,8 +75,7 @@ public class ChangedSchemaMergeTest extends SolrTestCaseJ4 {public void testOptimizeDiffSchemas() throws Exception {// load up a core (why not put it on disk?)CoreContainer cc = init();- SolrCore changed = cc.getCore("changed");- try {+ try (SolrCore changed = cc.getCore("changed")) {// add some documentsaddDoc(changed, "id", "1", "which", "15", "text", "some stuff with which");@@ -98,7 +97,6 @@ public class ChangedSchemaMergeTest extends SolrTestCaseJ4 {changed.getUpdateHandler().commit(new CommitUpdateCommand(req, false));changed.getUpdateHandler().commit(new CommitUpdateCommand(req, true));} finally {- if (changed != null) changed.close();if (cc != null) cc.shutdown();}}diff --git a/solr/core/src/test/org/apache/solr/schema/DocValuesMultiTest.java b/solr/core/src/test/org/apache/solr/schema/DocValuesMultiTest.javaindex 32e655fa356..95d7c4b4a36 100644--- a/solr/core/src/test/org/apache/solr/schema/DocValuesMultiTest.java+++ b/solr/core/src/test/org/apache/solr/schema/DocValuesMultiTest.java@@ -17,8 +17,6 @@ package org.apache.solr.schema;* limitations under the License.*/-import java.io.IOException;-import org.apache.lucene.index.AtomicReader;import org.apache.lucene.index.FieldInfo.DocValuesType;import org.apache.lucene.index.FieldInfos;@@ -30,6 +28,8 @@ import org.apache.solr.search.SolrIndexSearcher;import org.apache.solr.util.RefCounted;import org.junit.BeforeClass;+import java.io.IOException;+@SuppressCodecs({"Lucene40", "Lucene41"})public class DocValuesMultiTest extends SolrTestCaseJ4 {@@ -46,8 +46,7 @@ public class DocValuesMultiTest extends SolrTestCaseJ4 {public void testDocValues() throws IOException {assertU(adoc("id", "1", "floatdv", "4.5", "intdv", "-1", "intdv", "3", "stringdv", "value1", "stringdv", "value2"));assertU(commit());- SolrCore core = h.getCoreInc();- try {+ try (SolrCore core = h.getCoreInc()) {final RefCounted<SolrIndexSearcher> searcherRef = core.openNewSearcher(true, true);final SolrIndexSearcher searcher = searcherRef.get();try {@@ -66,8 +65,6 @@ public class DocValuesMultiTest extends SolrTestCaseJ4 {} finally {searcherRef.decref();}- } finally {- core.close();}}diff --git a/solr/core/src/test/org/apache/solr/schema/DocValuesTest.java b/solr/core/src/test/org/apache/solr/schema/DocValuesTest.javaindex 99d98e017d5..a4693a85b62 100644--- a/solr/core/src/test/org/apache/solr/schema/DocValuesTest.java+++ b/solr/core/src/test/org/apache/solr/schema/DocValuesTest.java@@ -17,8 +17,6 @@ package org.apache.solr.schema;* limitations under the License.*/-import java.io.IOException;-import org.apache.lucene.index.AtomicReader;import org.apache.lucene.index.FieldInfo.DocValuesType;import org.apache.lucene.index.FieldInfos;@@ -29,6 +27,8 @@ import org.apache.solr.search.SolrIndexSearcher;import org.apache.solr.util.RefCounted;import org.junit.BeforeClass;+import java.io.IOException;+public class DocValuesTest extends SolrTestCaseJ4 {@BeforeClass@@ -44,8 +44,7 @@ public class DocValuesTest extends SolrTestCaseJ4 {public void testDocValues() throws IOException {assertU(adoc("id", "1"));assertU(commit());- SolrCore core = h.getCoreInc();- try {+ try (SolrCore core = h.getCoreInc()) {final RefCounted<SolrIndexSearcher> searcherRef = core.openNewSearcher(true, true);final SolrIndexSearcher searcher = searcherRef.get();try {@@ -84,8 +83,6 @@ public class DocValuesTest extends SolrTestCaseJ4 {} finally {searcherRef.decref();}- } finally {- core.close();}}diff --git a/solr/core/src/test/org/apache/solr/schema/ModifyConfFileTest.java b/solr/core/src/test/org/apache/solr/schema/ModifyConfFileTest.javaindex e4c4e270b10..0fe67441000 100644--- a/solr/core/src/test/org/apache/solr/schema/ModifyConfFileTest.java+++ b/solr/core/src/test/org/apache/solr/schema/ModifyConfFileTest.java@@ -67,10 +67,9 @@ public class ModifyConfFileTest extends SolrTestCaseJ4 {public void testConfigWrite() throws Exception {final CoreContainer cc = init();- try {+ try (SolrCore core = cc.getCore("core1")) {//final CoreAdminHandler admin = new CoreAdminHandler(cc);- SolrCore core = cc.getCore("core1");SolrQueryResponse rsp = new SolrQueryResponse();SolrRequestHandler handler = core.getRequestHandler("/admin/fileedit");@@ -147,7 +146,6 @@ public class ModifyConfFileTest extends SolrTestCaseJ4 {assertTrue("Velocity should be a directory", (boolean)velocity.get("directory"));- core.close();} finally {cc.shutdown();if (solrHomeDirectory.exists()) {diff --git a/solr/core/src/test/org/apache/solr/update/SolrCmdDistributorTest.java b/solr/core/src/test/org/apache/solr/update/SolrCmdDistributorTest.javaindex 07ccd71324e..6b3d04a4251 100644--- a/solr/core/src/test/org/apache/solr/update/SolrCmdDistributorTest.java+++ b/solr/core/src/test/org/apache/solr/update/SolrCmdDistributorTest.java@@ -17,15 +17,6 @@ package org.apache.solr.update;* limitations under the License.*/-import java.io.File;-import java.io.IOException;-import java.util.ArrayList;-import java.util.Collections;-import java.util.List;-import java.util.concurrent.atomic.AtomicInteger;--import javax.xml.parsers.ParserConfigurationException;-import org.apache.lucene.index.LogDocMergePolicy;import org.apache.solr.BaseDistributedSearchTestCase;import org.apache.solr.client.solrj.SolrQuery;@@ -56,6 +47,14 @@ import org.apache.solr.update.processor.DistributedUpdateProcessor;import org.junit.BeforeClass;import org.xml.sax.SAXException;+import javax.xml.parsers.ParserConfigurationException;+import java.io.File;+import java.io.IOException;+import java.util.ArrayList;+import java.util.Collections;+import java.util.List;+import java.util.concurrent.atomic.AtomicInteger;+public class SolrCmdDistributorTest extends BaseDistributedSearchTestCase {private AtomicInteger id = new AtomicInteger();@@ -279,23 +278,26 @@ public class SolrCmdDistributorTest extends BaseDistributedSearchTestCase {final AtomicInteger commits = new AtomicInteger();for(JettySolrRunner jetty : jettys) {CoreContainer cores = ((SolrDispatchFilter) jetty.getDispatchFilter().getFilter()).getCores();- SolrCore core = cores.getCore("collection1");- try {+ try (SolrCore core = cores.getCore("collection1")) {core.getUpdateHandler().registerCommitCallback(new SolrEventListener() {@Override- public void init(NamedList args) {}+ public void init(NamedList args) {+ }+@Override- public void postSoftCommit() {}+ public void postSoftCommit() {+ }+@Overridepublic void postCommit() {commits.incrementAndGet();}+@Overridepublic void newSearcher(SolrIndexSearcher newSearcher,- SolrIndexSearcher currentSearcher) {}+ SolrIndexSearcher currentSearcher) {+ }});- } finally {- core.close();}}params = new ModifiableSolrParams();diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/MergeIndexesEmbeddedTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/MergeIndexesEmbeddedTest.javaindex 75239ab2ed1..8b1fbf4901b 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/MergeIndexesEmbeddedTest.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/MergeIndexesEmbeddedTest.java@@ -58,9 +58,8 @@ public class MergeIndexesEmbeddedTest extends MergeIndexesExampleTestBase {@Overrideprotected String getIndexDirCore1() {- SolrCore core1 = cores.getCore("core1");- String indexDir = core1.getIndexDir();- core1.close();- return indexDir;+ try (SolrCore core1 = cores.getCore("core1")) {+ return core1.getIndexDir();+ }}}diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/request/TestCoreAdmin.java b/solr/solrj/src/test/org/apache/solr/client/solrj/request/TestCoreAdmin.javaindex ccf7eb74106..27c48bfffda 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/request/TestCoreAdmin.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/request/TestCoreAdmin.java@@ -85,15 +85,9 @@ public class TestCoreAdmin extends AbstractEmbeddedSolrServerTestCase {CoreAdminResponse response = req.process(server);assertThat((String) response.getResponse().get("core"), is("corewithconfigset"));- SolrCore core = null;- try {- core = cores.getCore("corewithconfigset");+ try (SolrCore core = cores.getCore("corewithconfigset")) {assertThat(core, is(notNullValue()));}- finally {- if (core != null)- core.close();- }}@@ -135,23 +129,19 @@ public class TestCoreAdmin extends AbstractEmbeddedSolrServerTestCase {// Show that the newly-created core has values for load on startup and transient different than defaults due to the// above.+ File logDir;+ try (SolrCore coreProveIt = cores.getCore("collection1");+ SolrCore core = cores.getCore("newcore")) {- SolrCore coreProveIt = cores.getCore("collection1");- SolrCore core = cores.getCore("newcore");-- assertTrue(core.getCoreDescriptor().isTransient());- assertFalse(coreProveIt.getCoreDescriptor().isTransient());+ assertTrue(core.getCoreDescriptor().isTransient());+ assertFalse(coreProveIt.getCoreDescriptor().isTransient());- assertFalse(core.getCoreDescriptor().isLoadOnStartup());- assertTrue(coreProveIt.getCoreDescriptor().isLoadOnStartup());+ assertFalse(core.getCoreDescriptor().isLoadOnStartup());+ assertTrue(coreProveIt.getCoreDescriptor().isLoadOnStartup());- File logDir;- try {logDir = new File(core.getUpdateHandler().getUpdateLog().getLogDir());- } finally {- coreProveIt.close();- core.close();}+assertEquals(new File(dataDir, "ulog" + File.separator + "tlog").getAbsolutePath(), logDir.getAbsolutePath());server.shutdown();diff --git a/solr/test-framework/src/java/org/apache/solr/cloud/ChaosMonkey.java b/solr/test-framework/src/java/org/apache/solr/cloud/ChaosMonkey.javaindex 487d327f870..4c8b7dc8b84 100644--- a/solr/test-framework/src/java/org/apache/solr/cloud/ChaosMonkey.java+++ b/solr/test-framework/src/java/org/apache/solr/cloud/ChaosMonkey.java@@ -17,15 +17,6 @@ package org.apache.solr.cloud;* limitations under the License.*/-import java.net.InetSocketAddress;-import java.util.ArrayList;-import java.util.List;-import java.util.Map;-import java.util.Random;-import java.util.concurrent.atomic.AtomicInteger;--import javax.servlet.Filter;-import org.apache.lucene.util.LuceneTestCase;import org.apache.solr.client.solrj.SolrServer;import org.apache.solr.client.solrj.embedded.JettySolrRunner;@@ -43,6 +34,14 @@ import org.eclipse.jetty.servlet.FilterHolder;import org.slf4j.Logger;import org.slf4j.LoggerFactory;+import javax.servlet.Filter;+import java.net.InetSocketAddress;+import java.util.ArrayList;+import java.util.List;+import java.util.Map;+import java.util.Random;+import java.util.concurrent.atomic.AtomicInteger;+/*** The monkey can stop random or specific jetties used with SolrCloud.*@@ -381,19 +380,17 @@ public class ChaosMonkey {monkeyLog("selected jetty not running correctly - skip");return null;}- SolrCore core = cores.getCore(leader.getStr(ZkStateReader.CORE_NAME_PROP));- if (core == null) {- monkeyLog("selected jetty not running correctly - skip");- return null;- }+// cluster state can be stale - also go by our 'near real-time' is leader propboolean rtIsLeader;- try {+ try (SolrCore core = cores.getCore(leader.getStr(ZkStateReader.CORE_NAME_PROP))) {+ if (core == null) {+ monkeyLog("selected jetty not running correctly - skip");+ return null;+ }rtIsLeader = core.getCoreDescriptor().getCloudDescriptor().isLeader();- } finally {- core.close();}-+boolean isLeader = leader.getStr(ZkStateReader.NODE_NAME_PROP).equals(jetties.get(index).nodeName)|| rtIsLeader;if (!aggressivelyKillLeaders && isLeader) {diff --git a/solr/test-framework/src/java/org/apache/solr/util/TestHarness.java b/solr/test-framework/src/java/org/apache/solr/util/TestHarness.javaindex 187b578870a..059805359b7 100644--- a/solr/test-framework/src/java/org/apache/solr/util/TestHarness.java+++ b/solr/test-framework/src/java/org/apache/solr/util/TestHarness.java@@ -232,22 +232,19 @@ public class TestHarness extends BaseTestHarness {* @return The XML response to the update*/public String update(String xml) {- SolrCore core = getCoreInc();- DirectSolrConnection connection = new DirectSolrConnection(core);- SolrRequestHandler handler = core.getRequestHandler("/update");- // prefer the handler mapped to /update, but use our generic backup handler- // if that lookup fails- if (handler == null) {- handler = updater;- }- try {+ try (SolrCore core = getCoreInc()) {+ DirectSolrConnection connection = new DirectSolrConnection(core);+ SolrRequestHandler handler = core.getRequestHandler("/update");+ // prefer the handler mapped to /update, but use our generic backup handler+ // if that lookup fails+ if (handler == null) {+ handler = updater;+ }return connection.request(handler, null, xml);} catch (SolrException e) {throw (SolrException)e;} catch (Exception e) {throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);- } finally {- core.close();}}@@ -292,8 +289,7 @@ public class TestHarness extends BaseTestHarness {* @see LocalSolrQueryRequest*/public String query(String handler, SolrQueryRequest req) throws Exception {- SolrCore core = getCoreInc();- try {+ try (SolrCore core = getCoreInc()) {SolrQueryResponse rsp = new SolrQueryResponse();SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));core.execute(core.getRequestHandler(handler),req,rsp);@@ -310,23 +306,19 @@ public class TestHarness extends BaseTestHarness {} finally {req.close();SolrRequestInfo.clearRequestInfo();- core.close();}}/** It is the users responsibility to close the request object when done with it.* This method does not set/clear SolrRequestInfo */public SolrQueryResponse queryAndResponse(String handler, SolrQueryRequest req) throws Exception {- SolrCore core = getCoreInc();- try {+ try (SolrCore core = getCoreInc()) {SolrQueryResponse rsp = new SolrQueryResponse();- core.execute(core.getRequestHandler(handler),req,rsp);+ core.execute(core.getRequestHandler(handler), req, rsp);if (rsp.getException() != null) {throw rsp.getException();}return rsp;- } finally {- core.close();}}