/** * This constructor is designed to make it easy for JNI embedded applications to setup the entire * solr environment with a simple interface. It takes three parameters: <code>instanceDir:</code> * The solr instance directory. If null, it will check the standard places first * (JNDI,properties,"solr" directory) <code>dataDir:</code> where the index is stored. <code> * loggingPath:</code> Path to a java.util.logging.config.file. If the path represents an absolute * path or is relative to the CWD, it will use that. Next it will try a path relative to the * instanceDir. If none of these files exist, it will error. */ public DirectSolrConnection(String instanceDir, String dataDir, String loggingPath) { // If a loggingPath is specified, try using that (this needs to happen first) if (loggingPath != null) { File loggingConfig = new File(loggingPath); if (!loggingConfig.exists() && instanceDir != null) { loggingConfig = new File(new File(instanceDir), loggingPath); } if (loggingConfig.exists()) { System.setProperty("java.util.logging.config.file", loggingConfig.getAbsolutePath()); } else { throw new SolrException( SolrException.ErrorCode.SERVER_ERROR, "can not find logging file: " + loggingConfig); } } if (instanceDir == null) { instanceDir = SolrResourceLoader.locateInstanceDir(); } // Initialize try { CoreContainer cores = new CoreContainer(new SolrResourceLoader(instanceDir)); SolrConfig solrConfig = new SolrConfig(instanceDir, SolrConfig.DEFAULT_CONF_FILE, null); CoreDescriptor dcore = new CoreDescriptor(cores, "", solrConfig.getResourceLoader().getInstanceDir()); IndexSchema indexSchema = new IndexSchema(solrConfig, instanceDir + "/conf/schema.xml", null); core = new SolrCore(null, dataDir, solrConfig, indexSchema, dcore); cores.register("", core, false); parser = new SolrRequestParsers(solrConfig); } catch (Exception ee) { throw new RuntimeException(ee); } }
/** * Handle "ALIAS" action * * @param req * @param rsp * @return true if a modification has resulted that requires persistance of the CoreContainer * configuration. */ @Deprecated protected boolean handleAliasAction(SolrQueryRequest req, SolrQueryResponse rsp) { SolrParams params = req.getParams(); String name = params.get(CoreAdminParams.OTHER); String cname = params.get(CoreAdminParams.CORE); boolean doPersist = false; if (cname.equals(name)) return doPersist; SolrCore core = coreContainer.getCore(cname); if (core != null) { doPersist = coreContainer.isPersistent(); coreContainer.register(name, core, false); // no core.close() since each entry in the cores map should increase the ref } return doPersist; }
private SolrServer createSolrServer(File indexPath) throws ParserConfigurationException, IOException, SAXException { SolrConfig config = new SolrConfig(null, null, getClass().getResourceAsStream(SOLR_CONFIG)); IndexSchema schema = new IndexSchema(config, null, getClass().getResourceAsStream(SOLR_SCHEMA)); CoreContainer coreContainer = new CoreContainer(); SolrCore core = new SolrCore( "EMS", indexPath.getAbsolutePath(), config, schema, new CoreDescriptor(coreContainer, "EMS", SOLR_BASE)); coreContainer.register("EMS", core, false); return new EmbeddedSolrServer(coreContainer, "EMS"); }
/** * Handle "RENAME" Action * * @param req * @param rsp * @return true if a modification has resulted that requires persistance of the CoreContainer * configuration. * @throws SolrException */ protected boolean handleRenameAction(SolrQueryRequest req, SolrQueryResponse rsp) throws SolrException { SolrParams params = req.getParams(); String name = params.get(CoreAdminParams.OTHER); String cname = params.get(CoreAdminParams.CORE); boolean doPersist = false; if (cname.equals(name)) return doPersist; SolrCore core = coreContainer.getCore(cname); if (core != null) { doPersist = coreContainer.isPersistent(); coreContainer.register(name, core, false); coreContainer.remove(cname); core.close(); } return doPersist; }
/** * Handle 'CREATE' action. * * @param req * @param rsp * @return true if a modification has resulted that requires persistance of the CoreContainer * configuration. * @throws SolrException in case of a configuration error. */ protected boolean handleCreateAction(SolrQueryRequest req, SolrQueryResponse rsp) throws SolrException { try { SolrParams params = req.getParams(); String name = params.get(CoreAdminParams.NAME); CoreDescriptor dcore = new CoreDescriptor(coreContainer, name, params.get(CoreAdminParams.INSTANCE_DIR)); // fillup optional parameters String opts = params.get(CoreAdminParams.CONFIG); if (opts != null) dcore.setConfigName(opts); opts = params.get(CoreAdminParams.SCHEMA); if (opts != null) dcore.setSchemaName(opts); opts = params.get(CoreAdminParams.DATA_DIR); if (opts != null) dcore.setDataDir(opts); // Process all property.name=value parameters and set them as name=value core properties Properties coreProperties = new Properties(); Iterator<String> parameterNamesIterator = params.getParameterNamesIterator(); while (parameterNamesIterator.hasNext()) { String parameterName = parameterNamesIterator.next(); if (parameterName.startsWith(CoreAdminParams.PROPERTY_PREFIX)) { String parameterValue = params.get(parameterName); String propertyName = parameterName.substring(CoreAdminParams.PROPERTY_PREFIX.length()); // skip prefix coreProperties.put(propertyName, parameterValue); } } dcore.setCoreProperties(coreProperties); SolrCore core = coreContainer.create(dcore); coreContainer.register(name, core, false); rsp.add("core", core.getName()); return coreContainer.isPersistent(); } catch (Exception ex) { throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "Error executing default implementation of CREATE", ex); } }
public void enable(SolrClientListener listener) throws IOException, ParserConfigurationException, SAXException { if (enabled) { return; } String configLocation = "solrconfig.xml"; String schemaLocation = "schema.xml"; if (multiMasterProperties != null) { configLocation = (String) multiMasterProperties.get(PROP_SOLR_CONFIG); schemaLocation = (String) multiMasterProperties.get(PROP_SOLR_SCHEMA); } // Note that the following property could be set through JVM level // arguments too LOGGER.debug("Logger for Embedded Solr is in {slinghome}/log/solr.log at level INFO"); Configuration logConfiguration = getLogConfiguration(); // create a log configuration if none was found. leave alone any found // configurations // so that modifications will persist between server restarts if (logConfiguration == null) { logConfiguration = configurationAdmin.createFactoryConfiguration( "org.apache.sling.commons.log.LogManager.factory.config", null); Dictionary<String, Object> loggingProperties = new Hashtable<String, Object>(); loggingProperties.put("org.apache.sling.commons.log.level", "INFO"); loggingProperties.put("org.apache.sling.commons.log.file", "logs/solr.log"); loggingProperties.put("org.apache.sling.commons.log.names", "org.apache.solr"); // add this property to give us something unique to re-find this // configuration loggingProperties.put(LOGGER_KEY, LOGGER_VAL); logConfiguration.update(loggingProperties); } System.setProperty("solr.solr.home", solrHome); File solrHomeFile = new File(solrHome); File coreDir = new File(solrHomeFile, "nakamura"); // File coreConfigDir = new File(solrHomeFile,"conf"); deployFile(solrHomeFile, "solr.xml"); // deployFile(coreConfigDir,"solrconfig.xml"); // deployFile(coreConfigDir,"schema.xml"); ClassLoader contextClassloader = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader()); ClosableInputSource schemaSource = null; ClosableInputSource configSource = null; try { NakamuraSolrResourceLoader loader = new NakamuraSolrResourceLoader(solrHome, this.getClass().getClassLoader()); coreContainer = new CoreContainer(loader); configSource = getStream(configLocation); schemaSource = getStream(schemaLocation); SolrConfig config = new NakamuraSolrConfig(loader, configLocation, configSource); IndexSchema schema = new IndexSchema(config, null, schemaSource); nakamuraCore = new SolrCore("nakamura", coreDir.getAbsolutePath(), config, schema, null); coreContainer.register("nakamura", nakamuraCore, false); server = new EmbeddedSolrServer(coreContainer, "nakamura"); LoggerFactory.getLogger(this.getClass()) .info("Contans cores {} ", coreContainer.getCoreNames()); } finally { Thread.currentThread().setContextClassLoader(contextClassloader); safeClose(schemaSource); safeClose(configSource); } this.enabled = true; this.listener = listener; }
public void testFlowBadFromStart() throws Exception { // reused state Map<String, Exception> failures = null; Collection<String> cores = null; Exception fail = null; init("bad_flow"); // start with two collections: one valid, and one broken File solrXml = new File(solrHome, "solr.xml"); FileUtils.write(solrXml, BAD_SOLR_XML, IOUtils.CHARSET_UTF_8.toString()); // our "ok" collection FileUtils.copyFile( getFile("solr/collection1/conf/solrconfig-basic.xml"), FileUtils.getFile(solrHome, "col_ok", "conf", "solrconfig.xml")); FileUtils.copyFile( getFile("solr/collection1/conf/schema-minimal.xml"), FileUtils.getFile(solrHome, "col_ok", "conf", "schema.xml")); // our "bad" collection ignoreException(Pattern.quote("DummyMergePolicy")); FileUtils.copyFile( getFile("solr/collection1/conf/bad-mp-solrconfig.xml"), FileUtils.getFile(solrHome, "col_bad", "conf", "solrconfig.xml")); FileUtils.copyFile( getFile("solr/collection1/conf/schema-minimal.xml"), FileUtils.getFile(solrHome, "col_bad", "conf", "schema.xml")); // ----- // init the CoreContainer with the mix of ok/bad cores cc.load(solrHome.getAbsolutePath(), solrXml); // check that we have the cores we expect cores = cc.getCoreNames(); assertNotNull("core names is null", cores); assertEquals("wrong number of cores", 1, cores.size()); assertTrue("col_ok not found", cores.contains("col_ok")); // check that we have the failures we expect failures = cc.getCoreInitFailures(); assertNotNull("core failures is a null map", failures); assertEquals("wrong number of core failures", 1, failures.size()); fail = failures.get("col_bad"); assertNotNull("null failure for test core", fail); assertTrue( "init failure doesn't mention problem: " + fail.getMessage(), 0 < fail.getMessage().indexOf("DummyMergePolicy")); // check that we get null accessing a non-existent core assertNull(cc.getCore("does_not_exist")); // check that we get a 500 accessing the core with an init failure try { SolrCore c = cc.getCore("col_bad"); fail("Failed to get Exception on accessing core with init failure"); } catch (SolrException ex) { assertEquals(500, ex.code()); // double wrapped String cause = ex.getCause().getCause().getMessage(); assertTrue( "getCore() ex cause doesn't mention init fail: " + cause, 0 < cause.indexOf("DummyMergePolicy")); } // ----- // "fix" the bad collection FileUtils.copyFile( getFile("solr/collection1/conf/solrconfig-basic.xml"), FileUtils.getFile(solrHome, "col_bad", "conf", "solrconfig.xml")); final CoreDescriptor fixed = new CoreDescriptor(cc, "col_bad", "col_bad"); cc.register("col_bad", cc.create(fixed), false); // check that we have the cores we expect cores = cc.getCoreNames(); assertNotNull("core names is null", cores); assertEquals("wrong number of cores", 2, cores.size()); assertTrue("col_ok not found", cores.contains("col_ok")); assertTrue("col_bad not found", cores.contains("col_bad")); // check that we have the failures we expect failures = cc.getCoreInitFailures(); assertNotNull("core failures is a null map", failures); assertEquals("wrong number of core failures", 0, failures.size()); // ----- // try to add a collection with a path that doesn't exist final CoreDescriptor bogus = new CoreDescriptor(cc, "bogus", "bogus_path"); try { ignoreException(Pattern.quote("bogus_path")); cc.create(bogus); fail("bogus inst dir failed to trigger exception from create"); } catch (SolrException e) { assertTrue( "init exception doesn't mention bogus dir: " + e.getCause().getCause().getMessage(), 0 < e.getCause().getCause().getMessage().indexOf("bogus_path")); } // check that we have the cores we expect cores = cc.getCoreNames(); assertNotNull("core names is null", cores); assertEquals("wrong number of cores", 2, cores.size()); assertTrue("col_ok not found", cores.contains("col_ok")); assertTrue("col_bad not found", cores.contains("col_bad")); // check that we have the failures we expect failures = cc.getCoreInitFailures(); assertNotNull("core failures is a null map", failures); assertEquals("wrong number of core failures", 1, failures.size()); fail = failures.get("bogus"); assertNotNull("null failure for test core", fail); assertTrue( "init failure doesn't mention problem: " + fail.getCause().getMessage(), 0 < fail.getCause().getMessage().indexOf("bogus_path")); // check that we get null accessing a non-existent core assertNull(cc.getCore("does_not_exist")); // check that we get a 500 accessing the core with an init failure try { SolrCore c = cc.getCore("bogus"); fail("Failed to get Exception on accessing core with init failure"); } catch (SolrException ex) { assertEquals(500, ex.code()); // double wrapped String cause = ex.getCause().getCause().getMessage(); assertTrue( "getCore() ex cause doesn't mention init fail: " + cause, 0 < cause.indexOf("bogus_path")); } // ----- // register bogus as an alias for col_ok and confirm failure goes away cc.register("bogus", cc.getCore("col_ok"), false); // check that we have the cores we expect cores = cc.getCoreNames(); assertNotNull("core names is null", cores); assertEquals("wrong number of cores", 3, cores.size()); assertTrue("col_ok not found", cores.contains("col_ok")); assertTrue("col_bad not found", cores.contains("col_bad")); assertTrue("bogus not found", cores.contains("bogus")); // check that we have the failures we expect failures = cc.getCoreInitFailures(); assertNotNull("core failures is a null map", failures); assertEquals("wrong number of core failures", 0, failures.size()); // ----- // break col_bad's config and try to RELOAD to add failure final long col_bad_old_start = getCoreStartTime(cc, "col_bad"); FileUtils.write( FileUtils.getFile(solrHome, "col_bad", "conf", "solrconfig.xml"), "This is giberish, not valid XML <", IOUtils.CHARSET_UTF_8.toString()); try { ignoreException(Pattern.quote("SAX")); cc.reload("col_bad"); fail("corrupt solrconfig.xml failed to trigger exception from reload"); } catch (SolrException e) { assertTrue( "We're supposed to have a wrapped SAXParserException here, but we don't", e.getCause() instanceof SAXParseException); SAXParseException se = (SAXParseException) e.getCause(); assertTrue( "reload exception doesn't refer to slrconfig.xml " + se.getSystemId(), 0 < se.getSystemId().indexOf("solrconfig.xml")); } assertEquals( "Failed core reload should not have changed start time", col_bad_old_start, getCoreStartTime(cc, "col_bad")); // check that we have the cores we expect cores = cc.getCoreNames(); assertNotNull("core names is null", cores); assertEquals("wrong number of cores", 3, cores.size()); assertTrue("col_ok not found", cores.contains("col_ok")); assertTrue("col_bad not found", cores.contains("col_bad")); assertTrue("bogus not found", cores.contains("bogus")); // check that we have the failures we expect failures = cc.getCoreInitFailures(); assertNotNull("core failures is a null map", failures); assertEquals("wrong number of core failures", 1, failures.size()); fail = failures.get("col_bad"); assertNotNull("null failure for test core", fail); assertTrue("init failure isn't SAXParseException", fail instanceof SAXParseException); assertTrue( "init failure doesn't mention problem: " + fail.toString(), 0 < ((SAXParseException) fail).getSystemId().indexOf("solrconfig.xml")); // ---- // fix col_bad's config (again) and RELOAD to fix failure FileUtils.copyFile( getFile("solr/collection1/conf/solrconfig-basic.xml"), FileUtils.getFile(solrHome, "col_bad", "conf", "solrconfig.xml")); cc.reload("col_bad"); assertTrue( "Core reload should have changed start time", col_bad_old_start < getCoreStartTime(cc, "col_bad")); // check that we have the cores we expect cores = cc.getCoreNames(); assertNotNull("core names is null", cores); assertEquals("wrong number of cores", 3, cores.size()); assertTrue("col_ok not found", cores.contains("col_ok")); assertTrue("col_bad not found", cores.contains("col_bad")); assertTrue("bogus not found", cores.contains("bogus")); // check that we have the failures we expect failures = cc.getCoreInitFailures(); assertNotNull("core failures is a null map", failures); assertEquals("wrong number of core failures", 0, failures.size()); }