Apache Solr open-source search software
SOLR-695 -- modifying MultiCore implementaion to treat "singlecore" as a CoreCollection with only one Core
Ryan McKinley5ddfe45731ee
diff --git a/client/java/solrj/src/org/apache/solr/client/solrj/embedded/EmbeddedSolrServer.java b/client/java/solrj/src/org/apache/solr/client/solrj/embedded/EmbeddedSolrServer.javaindex 8a57e5f3e03..e62b629269f 100644--- a/client/java/solrj/src/org/apache/solr/client/solrj/embedded/EmbeddedSolrServer.java+++ b/client/java/solrj/src/org/apache/solr/client/solrj/embedded/EmbeddedSolrServer.java@@ -67,9 +67,13 @@ public class EmbeddedSolrServer extends SolrServerthrow new NullPointerException("SolrCore instance required");}this.core = core;- this.multicore = null;- this.coreName = null;-+ if (core.getCoreDescriptor() != null) {+ this.multicore = core.getCoreDescriptor().getMultiCore();+ this.coreName = core.getCoreDescriptor().getName();+ } else {+ this.multicore = null;+ this.coreName = null;+ }_parser = new SolrRequestParsers( null );}@@ -80,11 +84,7 @@ public class EmbeddedSolrServer extends SolrServer}this.core = null;this.multicore = multicore;- this.coreName = coreName;- SolrCore c = multicore.getCore( coreName );- if( c == null ) {- throw new RuntimeException( "Unknown core: "+coreName );- }+ this.coreName = coreName == null? "" : coreName;_parser = new SolrRequestParsers( null );}@@ -99,14 +99,16 @@ public class EmbeddedSolrServer extends SolrServer// Check for multicore actionSolrCore core = this.core;- if( core == null ) {+ if( core == null )core = multicore.getCore( coreName );- if( core == null ) {- throw new SolrException( SolrException.ErrorCode.SERVER_ERROR,- "Unknown core: "+coreName );- }+ // solr-647+ //else+ // core = core.open();+ if( core == null ) {+ throw new SolrException( SolrException.ErrorCode.SERVER_ERROR,+ coreName == null? "No core": "No such core: " + coreName );}-+SolrParams params = request.getParams();if( params == null ) {params = new ModifiableSolrParams();@@ -125,15 +127,16 @@ public class EmbeddedSolrServer extends SolrServer// Perhaps the path is to manage the coresif( handler == null &&multicore != null &&- path.equals( multicore.getAdminPath() ) &&- multicore.isEnabled() ) {+ path.equals( multicore.getAdminPath() ) ) {handler = multicore.getMultiCoreHandler();}}if( handler == null ) {+ // solr-647+ // core.close();throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "unknown handler: "+path );}-+try {SolrQueryRequest req = _parser.buildRequestFrom( core, params, request.getContentStreams() );req.getContext().put( "path", path );@@ -154,6 +157,10 @@ public class EmbeddedSolrServer extends SolrServercatch( Exception ex ) {throw new SolrServerException( ex );}+ finally {+ // solr-647+ // core.close();+ }}/**diff --git a/src/java/org/apache/solr/core/CoreContainer.java b/src/java/org/apache/solr/core/CoreContainer.javaindex 3fd77dc7c29..2eb334c60de 100644--- a/src/java/org/apache/solr/core/CoreContainer.java+++ b/src/java/org/apache/solr/core/CoreContainer.java@@ -52,9 +52,9 @@ public class CoreContainerprotected static Logger log = Logger.getLogger(CoreContainer.class.getName());protected final Map<String, CoreDescriptor> cores = new LinkedHashMap<String, CoreDescriptor>();- protected boolean enabled = false;protected boolean persistent = false;protected String adminPath = null;+ protected String managementPath = null;protected CoreAdminHandler coreAdminHandler = null;protected File configFile = null;protected String libDir = null;@@ -63,7 +63,83 @@ public class CoreContainerprotected java.lang.ref.WeakReference<SolrCore> adminCore = null;public CoreContainer() {+ }++ // Helper class to initialize the CoreContainer+ public static class Initializer {+ protected String pathPrefix = null; // strip this from the beginning of a path+ protected String solrConfigFilename = null;+ protected boolean abortOnConfigurationError = true;+ protected String managementPath = null;++ public String getPathPrefix() {+ return pathPrefix;+ }++ public void setPathPrefix(String pathPrefix) {+ this.pathPrefix = pathPrefix;+ }++ public boolean isAbortOnConfigurationError() {+ return abortOnConfigurationError;+ }++ public void setAbortOnConfigurationError(boolean abortOnConfigurationError) {+ this.abortOnConfigurationError = abortOnConfigurationError;+ }++ public String getSolrConfigFilename() {+ return solrConfigFilename;+ }++ public void setSolrConfigFilename(String solrConfigFilename) {+ this.solrConfigFilename = solrConfigFilename;+ }+ public String getManagementPath() {+ return managementPath;+ }++ public void setManagementPath(String managementPath) {+ this.managementPath = managementPath;+ }++ // core container instantiation+ public CoreContainer initialize() throws IOException, ParserConfigurationException, SAXException {+ CoreContainer cores = null;+ String instanceDir = SolrResourceLoader.locateInstanceDir();+ File fconf = new File(instanceDir, solrConfigFilename == null? "solr.xml": solrConfigFilename);+ log.info("looking for solr.xml: " + fconf.getAbsolutePath());++ if (fconf.exists()) {+ cores = new CoreContainer();+ cores.load(instanceDir, fconf);+ abortOnConfigurationError = false;+ // if any core aborts on startup, then abort+ for (SolrCore c : cores.getCores()) {+ if (c.getSolrConfig().getBool("abortOnConfigurationError", false)) {+ abortOnConfigurationError = true;+ break;+ }+ }+ solrConfigFilename = cores.getConfigFile().getName();+ } else {+ // perform compatibility init+ cores = new CoreContainer();+ cores.loader = new SolrResourceLoader(instanceDir);+ SolrConfig cfg = solrConfigFilename == null ? new SolrConfig() : new SolrConfig(solrConfigFilename);+ CoreDescriptor dcore = new CoreDescriptor(cores);+ dcore.init("", cfg.getResourceLoader().getInstanceDir());+ SolrCore singlecore = new SolrCore(null, null, cfg, null, dcore);+ dcore.setCore(singlecore);+ abortOnConfigurationError = cfg.getBool(+ "abortOnConfigurationError", abortOnConfigurationError);+ cores.register(dcore);+ cores.setPersistent(false);+ solrConfigFilename = cfg.getName();+ }+ return cores;+ }}/**@@ -101,8 +177,8 @@ public class CoreContainerpersistent = cfg.getBool( "solr/@persistent", false );libDir = cfg.get( "solr/@sharedLib", null);-adminPath = cfg.get( "solr/cores/@adminPath", null );+ managementPath = cfg.get("solr/cores/@managementPath", null );if (libDir != null) {// relative dir to conf@@ -151,8 +227,6 @@ public class CoreContainertry { cfgis.close(); } catch (Exception xany) {}}}-- setEnabled(true);}/**@@ -160,12 +234,11 @@ public class CoreContainer*/public void shutdown() {synchronized(cores) {- for(Map.Entry<String,CoreDescriptor> e : cores.entrySet()) {- SolrCore core = e.getValue().getCore();- if (core == null) continue;- String key = e.getKey();- if (core.getName().equals(key))- core.close();+ for(CoreDescriptor descriptor : cores.values()) {+ SolrCore core = descriptor.getCore();+ if( core != null ) {+ core.close();+ }}cores.clear();}@@ -176,6 +249,7 @@ public class CoreContainershutdown();}+ // ---------------- CoreDescriptor related methods ---------------/*** Registers a SolrCore descriptor in the registry.* @param descr the Solr core descriptor@@ -187,7 +261,6 @@ public class CoreContainer}String name = descr.getName();if( name == null ||- name.length() < 1 ||name.indexOf( '/' ) >= 0 ||name.indexOf( '\\' ) >= 0 ){throw new RuntimeException( "Invalid core name: "+name );@@ -206,31 +279,6 @@ public class CoreContainerreturn old;}}--- /**- * Swaps two SolrCore descriptors.- * @param c0- * @param c1- */- public void swap(CoreDescriptor c0, CoreDescriptor c1) {- if( c0 == null || c1 == null ) {- throw new RuntimeException( "Can not swap a null core." );- }- synchronized( cores ) {- String n0 = c0.getName();- String n1 = c1.getName();- cores.put(n0, c1);- cores.put(n1, c0);- c0.setName( n1 );- if (c0.getCore() != null)- c0.getCore().setName(n1);- c1.setName( n0 );- if (c1.getCore() != null)- c1.getCore().setName(n0);- }- log.info( "swaped: "+c0.getName() + " with " + c1.getName() );- }/*** Creates a new core based on a descriptor.@@ -260,37 +308,7 @@ public class CoreContainerCoreDescriptor old = this.register(dcore);return core;}-- /**- * Recreates a SolrCore.- * While the new core is loading, requests will continue to be dispatched to- * and processed by the old core- *- * @param dcore the SolrCore to reload- * @throws ParserConfigurationException- * @throws IOException- * @throws SAXException- */- public void reload(CoreDescriptor dcore) throws ParserConfigurationException, IOException, SAXException {- create(new CoreDescriptor(dcore));- }- // TODO? -- add some kind of hook to close the core after all references are- // gone... is finalize() enough?- public void remove( String name ) {- synchronized(cores) {- CoreDescriptor dcore = cores.remove( name );- if (dcore == null) {- return;- }-- SolrCore core = dcore.getCore();- if (core != null) {- core.close();- }- }- }-/*** @return a Collection of registered SolrCores*/@@ -304,7 +322,10 @@ public class CoreContainer}return l;}-++ /**+ * @return a Collection of registered CoreDescriptors+ */public Collection<CoreDescriptor> getDescriptors() {java.util.List<CoreDescriptor> l = new java.util.ArrayList<CoreDescriptor>();synchronized (cores) {@@ -312,55 +333,104 @@ public class CoreContainer}return l;}-- public SolrCore getCore(String name) {- CoreDescriptor dcore = getDescriptor( name );- return (dcore == null) ? null : dcore.getCore();- }-++ /**+ * @return the CoreDescriptor registered under that name+ */public CoreDescriptor getDescriptor(String name) {synchronized(cores) {return cores.get( name );}}-- // all of the following properties aren't synchronized- // but this should be OK since they normally won't be changed rapidly- public boolean isEnabled() {- return enabled;- }- public void setEnabled(boolean enabled) {- synchronized(this) {- this.enabled = enabled;- }- }- public boolean isPersistent() {- return persistent;+ // ---------------- Core name related methods ---------------+ /**+ * Recreates a SolrCore.+ * While the new core is loading, requests will continue to be dispatched to+ * and processed by the old core+ *+ * @param name the name of the SolrCore to reload+ * @throws ParserConfigurationException+ * @throws IOException+ * @throws SAXException+ */++ public void reload(String name) throws ParserConfigurationException, IOException, SAXException {+ synchronized(cores) {+ CoreDescriptor dcore = cores.get(name);+ if (dcore == null)+ throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "No such core: " + name );+ create(new CoreDescriptor(dcore));+ }}+- public void setPersistent(boolean persistent) {- synchronized(this) {- this.persistent = persistent;+ /**+ * Swaps two SolrCore descriptors.+ * @param c0+ * @param c1+ */+ public void swap(String n0, String n1) {+ if( n0 == null || n1 == null ) {+ throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "Can not swap unnamed cores." );+ }+ synchronized( cores ) {+ CoreDescriptor c0 = cores.get(n0);+ if (c0 == null)+ throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "No such core: " + n0 );+ CoreDescriptor c1 = cores.get(n1);+ if (c1 == null)+ throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "No such core: " + n1 );+ cores.put(n0, c1);+ cores.put(n1, c0);+ c0.setName( n1 );+ if (c0.getCore() != null)+ c0.getCore().setName(n1);+ c1.setName( n0 );+ if (c1.getCore() != null)+ c1.getCore().setName(n0);+ log.info( "swaped: "+c0.getName() + " with " + c1.getName() );}}- public String getAdminPath() {- return adminPath;+ /** Removes & closes a registered core. */+ public void remove( String name ) {+ synchronized(cores) {+ CoreDescriptor dcore = cores.remove( name );+ if (dcore == null) {+ return;+ }++ SolrCore core = dcore.getCore();+ if (core != null) {+ core.close();+ }+ }}+- public void setAdminPath(String adminPath) {- synchronized (this) {- this.adminPath = adminPath;+ /** Gets a core by name and increase its refcount.+ * @see SolrCore.open() @see SolrCore.close()+ * @param name the core name+ * @return the core if found+ */+ public SolrCore getCore(String name) {+ synchronized(cores) {+ CoreDescriptor dcore = cores.get(name);+ SolrCore core = null;+ if (dcore != null)+ core = dcore.getCore();+ return core;+// solr-647+// if (core != null)+// return core.open();+// return null;}}-+/**- * Sets the preferred core used to handle CoreContainer admin tasks.- * Note that getAdminCore is not symmetrical to this method since- * it will always return an opened SolrCore.- * This however can be useful implementing a "metacore" (a core of cores).+ * Sets the preferred core used to handle MultiCore admin tasks.*/public void setAdminCore(SolrCore core) {synchronized (cores) {@@ -369,18 +439,23 @@ public class CoreContainer}/**- * Gets a core to handle CoreContainer admin tasks (@see SolrDispatchFilter).- * This makes the best attempt to reuse the same opened SolrCore across calls.+ * Ensures there is a valid core to handle MultiCore admin taks and+ * increase its refcount.+ * @return the acquired admin core, null if no core is available*/public SolrCore getAdminCore() {synchronized (cores) {SolrCore core = adminCore != null ? adminCore.get() : null;- if (core == null || core.isClosed()) {+// solr-647+// if (core != null)+// core = core.open();+ if (core == null) {for (CoreDescriptor descr : this.cores.values()) {core = descr.getCore();- if (core == null || core.isClosed()) {- core = null;- } else {+// solr-647+// if (core != null)+// core = core.open();+ if (core != null) {break;}}@@ -390,14 +465,15 @@ public class CoreContainer}}+ // ---------------- Multicore self related methods ---------------/**- * Creates a CoreAdminHandler for this CoreContainer.+ * Creates a CoreAdminHandler for this MultiCore.* @return a CoreAdminHandler*/protected CoreAdminHandler createMultiCoreHandler() {return new CoreAdminHandler() {@Override- public CoreContainer getMultiCore() {+ public CoreContainer getCoreContainer() {return CoreContainer.this;}};@@ -407,28 +483,72 @@ public class CoreContainerreturn coreAdminHandler;}+ // all of the following properties aren't synchronized+ // but this should be OK since they normally won't be changed rapidly+ public boolean isPersistent() {+ return persistent;+ }++ public void setPersistent(boolean persistent) {+ this.persistent = persistent;+ }++ public String getAdminPath() {+ return adminPath;+ }++ public void setAdminPath(String adminPath) {+ this.adminPath = adminPath;+ }+++ public String getManagementPath() {+ return managementPath;+ }++ /**+ * Sets the alternate path for multicore handling:+ * This is used in case there is a registered unnamed core (aka name is "") to+ * declare an alternate way of accessing named cores.+ * This can also be used in a pseudo single-core environment so admins can prepare+ * a new version before swapping.+ * @param adminPath+ */+ public void setManagementPath(String path) {+ this.managementPath = path;+ }+public File getConfigFile() {return configFile;}- /** Persists the multicore config file. */+/** Persists the multicore config file in multicore.xml. */public void persist() {+ persistFile(null);+ }++ /** Persists the multicore config file in a user provided file. */+ public void persistFile(File file) {File tmpFile = null;try {// write in temp first- tmpFile = File.createTempFile("solr", ".xml", configFile.getParentFile());- java.io.FileOutputStream out = new java.io.FileOutputStream(tmpFile);+ if (file == null) {+ file = tmpFile = File.createTempFile("solr", ".xml", configFile.getParentFile());+ }+ java.io.FileOutputStream out = new java.io.FileOutputStream(file);synchronized(cores) {Writer writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));persist(writer);writer.flush();writer.close();out.close();- // rename over origin or copy it it this fails- if (tmpFile.renameTo(configFile))- tmpFile = null;- else- fileCopy(tmpFile, configFile);+ // rename over origin or copy it this fails+ if (tmpFile != null) {+ if (tmpFile.renameTo(configFile))+ tmpFile = null;+ else+ fileCopy(tmpFile, configFile);+ }}}catch(java.io.FileNotFoundException xnf) {@@ -515,8 +635,13 @@ public class CoreContainerfos = new FileOutputStream(dest);fcin = fis.getChannel();fcout = fos.getChannel();- // do the file copy- fcin.transferTo(0, fcin.size(), fcout);+ // do the file copy 32Mb at a time+ final int MB32 = 32*1024*1024;+ long size = fcin.size();+ long position = 0;+ while (position < size) {+ position += fcin.transferTo(position, MB32, fcout);+ }}catch(IOException xio) {xforward = xio;diff --git a/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java b/src/java/org/apache/solr/handler/admin/CoreAdminHandler.javaindex b753d5cd988..86cd42c72cb 100644--- a/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java+++ b/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java@@ -57,20 +57,20 @@ public abstract class CoreAdminHandler extends RequestHandlerBase}/**- * The instance of multicore this handler handles.+ * The instance of CoreContainer this handler handles.* This should be the CoreContainer instance that created this handler.* @return a CoreContainer instance*/- public abstract CoreContainer getMultiCore();+ public abstract CoreContainer getCoreContainer();@Overridepublic void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception{- // Make sure the manager is enabled- CoreContainer manager = getMultiCore();- if( !manager.isEnabled() ) {+ // Make sure the cores is enabled+ CoreContainer cores = getCoreContainer();+ if( cores == null ) {throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,- "CoreContainer support must be enabled at startup." );+ "Core container instance missing" );}boolean do_persist = false;@@ -86,112 +86,96 @@ public abstract class CoreAdminHandler extends RequestHandlerBase"Unknown 'action' value. Use: "+MultiCoreAction.values() );}}+ String cname = params.get( MultiCoreParams.CORE );- SolrCore core = null;- // Handle a core creation- //---------------------------------------------------------- if (action == MultiCoreAction.CREATE) {- CoreDescriptor dcore = new CoreDescriptor(manager);- dcore.init(params.get(MultiCoreParams.NAME),- params.get(MultiCoreParams.INSTANCE_DIR));-- // fillup optional parameters- String opts = params.get(MultiCoreParams.CONFIG);- if (opts != null)- dcore.setConfigName(opts);-- opts = params.get(MultiCoreParams.SCHEMA);- if (opts != null)- dcore.setSchemaName(opts);-- core = manager.create(dcore);- rsp.add("core", core.getName());- do_persist = manager.isPersistent();- }- else {- // Select the core- String cname = params.get( MultiCoreParams.CORE );- if( cname != null ) {- core = manager.getCore(cname);- if( core == null ) {- throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,- "Unknown core: "+cname );- }- }+ switch(action) {+ case CREATE: {+ CoreDescriptor dcore = new CoreDescriptor(cores);+ dcore.init(params.get(MultiCoreParams.NAME),+ params.get(MultiCoreParams.INSTANCE_DIR));- // Handle a Status Request- //---------------------------------------------------------- if( action == MultiCoreAction.STATUS ) {- do_persist = false; // no state change+ // fillup optional parameters+ String opts = params.get(MultiCoreParams.CONFIG);+ if (opts != null)+ dcore.setConfigName(opts);++ opts = params.get(MultiCoreParams.SCHEMA);+ if (opts != null)+ dcore.setSchemaName(opts);++ SolrCore core = cores.create(dcore);+ rsp.add("core", core.getName());+ do_persist = cores.isPersistent();+ break;+ }++ case STATUS: {NamedList<Object> status = new SimpleOrderedMap<Object>();- if( core == null ) {- for (CoreDescriptor d : manager.getDescriptors()) {- status.add(d.getName(), getCoreStatus( d.getCore() ) );+ if( cname == null ) {+ for (CoreDescriptor d : cores.getDescriptors()) {+ cname = d.getName();+ status.add(d.getName(), getCoreStatus( cores, cname ) );}}else {- status.add(core.getName(), getCoreStatus(core) );+ status.add(cname, getCoreStatus( cores, cname ) );}rsp.add( "status", status );+ do_persist = false; // no state change+ break;++ }++ case PERSIST: {+ do_persist = true;+ break;+ }++ case RELOAD: {+ cores.reload( cname );+ do_persist = false; // no change on reload+ break;+ }++ case SWAP: {+ do_persist = params.getBool(MultiCoreParams.PERSISTENT, cores.isPersistent());+ String with = required.get( MultiCoreParams.WITH );+ cores.swap( cname, with );+ break;}- else if (core == null) {++ default: {throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,- "Action '"+action+"' requires a core name." );- }- else {- // Handle all other- //---------------------------------------------------------- do_persist = params.getBool(MultiCoreParams.PERSISTENT, manager.isPersistent());- switch( action ) {- case RELOAD: {- manager.reload( manager.getDescriptor( core.getName() ) );- do_persist = false; // no change on reload- break;- }-- case SWAP: {- String name = required.get( MultiCoreParams.WITH );- CoreDescriptor swap = manager.getDescriptor( name );-- if( swap == null ) {- throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,- "Unknown core: "+name );- }- manager.swap( manager.getDescriptor( core.getName() ), swap );- break;- }-- case PERSIST: {- do_persist = true;- break;- }-- default: {- throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,- "TODO: IMPLEMENT: " + action );- }- } // switch+ "TODO: IMPLEMENT: " + action );}- }-+ } // switch+// Should we persist the changes?if (do_persist) {- manager.persist();- rsp.add("saved", manager.getConfigFile().getAbsolutePath());+ cores.persist();+ rsp.add("saved", cores.getConfigFile().getAbsolutePath());}}- private static NamedList<Object> getCoreStatus( SolrCore core ) throws IOException+ private static NamedList<Object> getCoreStatus( CoreContainer cores, String cname ) throws IOException{NamedList<Object> info = new SimpleOrderedMap<Object>();- info.add( "name", core.getName() );- info.add( "instanceDir", core.getResourceLoader().getInstanceDir() );- info.add( "dataDir", core.getDataDir() );- info.add( "startTime", new Date( core.getStartTime() ) );- info.add( "uptime", System.currentTimeMillis()-core.getStartTime() );- RefCounted<SolrIndexSearcher> searcher = core.getSearcher();- info.add( "index", LukeRequestHandler.getIndexInfo( searcher.get().getReader(), false ) );- searcher.decref();+ SolrCore core = cores.getCore(cname);+ if (core != null) {+ try {+ info.add("name", core.getName());+ info.add("instanceDir", core.getResourceLoader().getInstanceDir());+ info.add("dataDir", core.getDataDir());+ info.add("startTime", new Date(core.getStartTime()));+ info.add("uptime", System.currentTimeMillis() - core.getStartTime());+ RefCounted<SolrIndexSearcher> searcher = core.getSearcher();+ info.add("index", LukeRequestHandler.getIndexInfo(searcher.get().getReader(), false));+ searcher.decref();+ } finally {+ // solr-647+ // core.close();+ }+ }return info;}diff --git a/src/webapp/src/org/apache/solr/servlet/SolrDispatchFilter.java b/src/webapp/src/org/apache/solr/servlet/SolrDispatchFilter.javaindex 6f16a771239..3f9d2962b8f 100644--- a/src/webapp/src/org/apache/solr/servlet/SolrDispatchFilter.java+++ b/src/webapp/src/org/apache/solr/servlet/SolrDispatchFilter.java@@ -17,7 +17,6 @@package org.apache.solr.servlet;-import java.io.File;import java.io.IOException;import java.io.PrintWriter;import java.io.StringWriter;@@ -50,47 +49,25 @@ public class SolrDispatchFilter implements Filter{final Logger log = Logger.getLogger(SolrDispatchFilter.class.getName());- protected CoreDescriptor singleCoreDescriptor;-protected CoreContainer cores;protected String pathPrefix = null; // strip this from the beginning of a pathprotected String abortErrorMessage = null;- protected final WeakHashMap<SolrCore, SolrRequestParsers> parsers = new WeakHashMap<SolrCore, SolrRequestParsers>();protected String solrConfigFilename = null;+ protected final WeakHashMap<SolrCore, SolrRequestParsers> parsers = new WeakHashMap<SolrCore, SolrRequestParsers>();public void init(FilterConfig config) throws ServletException{log.info("SolrDispatchFilter.init()");boolean abortOnConfigurationError = true;+ CoreContainer.Initializer init = createInitializer();try {// web.xml configuration- this.pathPrefix = config.getInitParameter( "path-prefix" );- this.solrConfigFilename = config.getInitParameter("solrconfig-filename");-- // cores instantiation- this.cores = initMultiCore(config);-- if(cores != null && cores.isEnabled() ) {- abortOnConfigurationError = false;- singleCoreDescriptor = null;- // if any core aborts on startup, then abort- for( SolrCore c : cores.getCores() ) {- if( c.getSolrConfig().getBool( "abortOnConfigurationError",false) ) {- abortOnConfigurationError = true;- break;- }- }- }- else {- SolrConfig cfg = this.solrConfigFilename == null? new SolrConfig() : new SolrConfig(this.solrConfigFilename);- singleCoreDescriptor = new CoreDescriptor((CoreContainer)null);- singleCoreDescriptor.init("",cfg.getResourceLoader().getInstanceDir());- SolrCore singlecore = new SolrCore( null, null, cfg, null, singleCoreDescriptor);- singleCoreDescriptor.setCore(singlecore);- abortOnConfigurationError = cfg.getBool(- "abortOnConfigurationError", abortOnConfigurationError);- }+ init.setPathPrefix(config.getInitParameter( "path-prefix" ));+ init.setSolrConfigFilename(config.getInitParameter("solrconfig-filename"));++ this.cores = init.initialize();+ abortOnConfigurationError = init.isAbortOnConfigurationError();log.info("user.dir=" + System.getProperty("user.dir"));}catch( Throwable t ) {@@ -108,11 +85,7 @@ public class SolrDispatchFilter implements Filterout.println( "Check your log files for more detailed information on what may be wrong.\n" );out.println( "If you want solr to continue after configuration errors, change: \n");out.println( " <abortOnConfigurationError>false</abortOnConfigurationError>\n" );- if (cores != null && cores.isEnabled()) {- out.println( "in solr.xml\n" );- } else {- out.println( "in solrconfig.xml\n" );- }+ out.println( "in "+init.getSolrConfigFilename()+"\n" );for( Throwable t : SolrConfig.severeErrors ) {out.println( "-------------------------------------------------------------" );@@ -132,33 +105,16 @@ public class SolrDispatchFilter implements Filterlog.info("SolrDispatchFilter.init() done");}- /**- * Initialize the cores instance.- * @param config the filter configuration- * @return the cores instance or null- * @throws java.lang.Exception- */- protected CoreContainer initMultiCore(FilterConfig config) throws Exception {- CoreContainer mcore = new CoreContainer();- String instanceDir = SolrResourceLoader.locateInstanceDir();- File fconf = new File(instanceDir, "solr.xml");- log.info("looking for solr.xml: " + fconf.getAbsolutePath());- if (fconf.exists()) {- mcore.load(instanceDir, fconf);- }- return mcore;+ /** Method to override to change how CoreContainer initialization is performed. */+ protected CoreContainer.Initializer createInitializer() {+ return new CoreContainer.Initializer();}--+public void destroy() {if (cores != null) {- cores.shutdown();+ cores.shutdown();cores = null;- }- if( singleCoreDescriptor != null ) {- singleCoreDescriptor.getCore().close();- singleCoreDescriptor = null;- }+ }}public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {@@ -172,8 +128,11 @@ public class SolrDispatchFilter implements FilterHttpServletResponse resp = (HttpServletResponse)response;SolrRequestHandler handler = null;SolrQueryRequest solrReq = null;-+ SolrCore core = null;+ String corename = "";try {+ // put the core container in request attribute+ req.setAttribute("org.apache.solr.CoreContainer", cores);String path = req.getServletPath();if( req.getPathInfo() != null ) {// this lets you handle /update/commit when /update is a servlet@@ -182,41 +141,42 @@ public class SolrDispatchFilter implements Filterif( pathPrefix != null && path.startsWith( pathPrefix ) ) {path = path.substring( pathPrefix.length() );}-+ // check for management path+ String alternate = cores.getManagementPath();+ if (alternate != null && path.startsWith(alternate)) {+ path = path.substring(0, alternate.length());+ }+ // unused feature ?int idx = path.indexOf( ':' );if( idx > 0 ) {// save the portion after the ':' for a 'handler' path parameterpath = path.substring( 0, idx );}- // By default use the single core. If cores is enabled, look for one.- final SolrCore core;- if (cores != null && cores.isEnabled()) {- req.setAttribute("org.apache.solr.CoreContainer", cores);-- // if this is the multi-core admin page, it will handle it- if( path.equals( cores.getAdminPath() ) ) {- handler = cores.getMultiCoreHandler();- // pick a core to use for output generation- core = cores.getAdminCore();- if( core == null ) {- throw new RuntimeException( "Can not find a valid core for the cores admin handler" );- }- } else {- //otherwise, we should find a core from the path- idx = path.indexOf( "/", 1 );- if( idx > 1 ) {- // try to get the corename as a request parameter first- String corename = path.substring( 1, idx );+ // Check for the core admin page+ if( path.equals( cores.getAdminPath() ) ) {+ handler = cores.getMultiCoreHandler();+ // pick a core to use for output generation+ core = cores.getAdminCore();+ if( core == null ) {+ throw new RuntimeException( "Can not find a valid core for the multicore admin handler" );+ }+ }+ else {+ //otherwise, we should find a core from the path+ idx = path.indexOf( "/", 1 );+ if( idx > 1 ) {+ // try to get the corename as a request parameter first+ corename = path.substring( 1, idx );+ core = cores.getCore(corename);+ if (core != null) {path = path.substring( idx );- core = cores.getCore( corename );- } else {- core = null;}}- }- else {- core = singleCoreDescriptor.getCore();+ if (core == null) {+ corename = "";+ core = cores.getCore("");+ }}// With a valid core...@@ -250,7 +210,7 @@ public class SolrDispatchFilter implements Filter}}- // With a valid handler and a valid core...+ // With a valid handler and a valid core...if( handler != null ) {// if not a /select, create the requestif( solrReq == null ) {@@ -309,20 +269,19 @@ public class SolrDispatchFilter implements Filterelse {req.setAttribute("org.apache.solr.SolrCore", core);// Modify the request so each core gets its own /admin- if( singleCoreDescriptor == null && path.startsWith( "/admin" ) ) {+ if( path.startsWith( "/admin" ) ) {req.getRequestDispatcher( pathPrefix == null ? path : pathPrefix + path ).forward( request, response );return;}}}- if( core == null ) {- req.setAttribute("org.apache.solr.NoCoreSet", Boolean.TRUE );- }log.fine("no handler or core retrieved for " + path + ", follow through...");- } catch (Throwable ex) {+ }+ catch (Throwable ex) {sendError( (HttpServletResponse)response, ex );return;- } finally {+ }+ finally {if( solrReq != null ) {solrReq.close();}diff --git a/src/webapp/web/index.jsp b/src/webapp/web/index.jspindex b026c895c50..66ec69e61d4 100644--- a/src/webapp/web/index.jsp+++ b/src/webapp/web/index.jsp@@ -28,9 +28,11 @@<a href="."><img border="0" align="right" height="61" width="142" src="admin/solr-head.gif" alt="Solr"/></a><%- org.apache.solr.core.CoreContainer multicore = (org.apache.solr.core.CoreContainer)request.getAttribute("org.apache.solr.CoreContainer");- if(multicore != null && multicore.isEnabled() ) {- for( org.apache.solr.core.SolrCore core : multicore.getCores() ) {%>+ org.apache.solr.core.CoreContainer cores = (org.apache.solr.core.CoreContainer)request.getAttribute("org.apache.solr.CoreContainer");+ if( cores != null+ && cores.getCores().size() > 0 // HACK! check that we have valid names...+ && cores.getCores().iterator().next().getName() != null ) {+ for( org.apache.solr.core.SolrCore core : cores.getCores() ) {%><a href="<%= core.getName() %>/admin/">Admin <%= core.getName() %></a><br/><% }} else { %><a href="admin/">Solr Admin</a>