protected SolrServer getEmbeddedSolrServer(String solrCore) throws IOException, ParserConfigurationException, SAXException, SolrServerException { String solrHomeDir = System.getProperty(SOLR_HOME); File home = new File(solrHomeDir); File f = new File(home, "solr.xml"); CoreContainer container = new CoreContainer(); container.load(); return new EmbeddedSolrServer(container, solrCore); }
// Write out the cores' config files, both bad schema files, bad config files as well as some good // cores. private CoreContainer initGoodAndBad( List<String> goodCores, List<String> badSchemaCores, List<String> badConfigCores) throws Exception { // Don't pollute the log with exception traces when they're expected. ignoreException(Pattern.quote("SAXParseException")); if (solrHomeDirectory.exists()) { FileUtils.deleteDirectory(solrHomeDirectory); } assertTrue("Failed to mkdirs workDir", solrHomeDirectory.mkdirs()); // Create the cores that should be fine. for (String coreName : goodCores) { File coreRoot = new File(solrHomeDirectory, coreName); copyMinConf(coreRoot, "name=" + coreName); } // Collect the files that we'll write to the config directories. String top = SolrTestCaseJ4.TEST_HOME() + "/collection1/conf"; String min_schema = FileUtils.readFileToString(new File(top, "schema-tiny.xml"), Charsets.UTF_8.toString()); String min_config = FileUtils.readFileToString( new File(top, "solrconfig-minimal.xml"), Charsets.UTF_8.toString()); String rand_snip = FileUtils.readFileToString( new File(top, "solrconfig.snippet.randomindexconfig.xml"), Charsets.UTF_8.toString()); // Now purposely mess up the config files, introducing stupid syntax errors. String bad_config = min_config.replace("<requestHandler", "<reqsthalr"); String bad_schema = min_schema.replace("<field", "<filed"); // Create the cores with bad configs for (String coreName : badConfigCores) { writeCustomConfig(coreName, bad_config, min_schema, rand_snip); } // Create the cores with bad schemas. for (String coreName : badSchemaCores) { writeCustomConfig(coreName, min_config, bad_schema, rand_snip); } // Write the solr.xml file. Cute how minimal it can be now.... File solrXml = new File(solrHomeDirectory, "solr.xml"); FileUtils.write(solrXml, "<solr/>", Charsets.UTF_8.toString()); SolrResourceLoader loader = new SolrResourceLoader(solrHomeDirectory.getAbsolutePath()); ConfigSolrXml config = (ConfigSolrXml) ConfigSolr.fromFile(loader, solrXml); CoresLocator locator = new CorePropertiesLocator(solrHomeDirectory.getAbsolutePath()); // OK this should succeed, but at the end we should have recorded a series of errors. final CoreContainer cores = new CoreContainer(loader, config, locator); cores.load(); return cores; }
/** * Create a new CoreContainer and load its cores * * @param solrHome the solr home directory * @param configFile the file containing this container's configuration * @return a loaded CoreContainer */ public static CoreContainer createAndLoad(Path solrHome, Path configFile) { SolrResourceLoader loader = new SolrResourceLoader(solrHome); CoreContainer cc = new CoreContainer(SolrXmlConfig.fromFile(loader, configFile)); try { cc.load(); } catch (Exception e) { cc.shutdown(); throw e; } return cc; }
/** * Creates a {@link CoreContainer} object which has all the Solr cores. * * @param context The application's context. Required. * @return A {@link CoreContainer} object which has all the Solr cores. * @throws IOException If the Solr home cannot be created. * @throws ParserConfigurationException If the any of the Solr XML files are corrupted. * @throws SAXException If the any of the Solr XML files are corrupted. */ @Bean public static CoreContainer solrCores(final ApplicationContext context) throws IOException, ParserConfigurationException, SAXException { notNull(context, "The application's context is required."); Environment env = context.getEnvironment(); final File solrHome = findSolrHome(context); final File dataDir = findSolrDataDir(env); final Map<String, String> coreDefs = new LinkedHashMap<String, String>(); CoreContainer cores = new CoreContainer(solrHome.getAbsolutePath()) { @Override public SolrCore create(final CoreDescriptor coreDescriptor) { String coreName = coreDescriptor.getName(); if (coreName.length() == 0) { coreName = getDefaultCoreName(); } Properties properties = new Properties(); String coreDataDir = new File(dataDir, coreName).getAbsolutePath(); coreDefs.put(coreName, coreDataDir); // default data dir properties.setProperty(CoreDescriptor.CORE_DATADIR, coreDataDir); return super.create( new CoreDescriptor( coreDescriptor.getCoreContainer(), coreDescriptor.getName(), coreDescriptor.getInstanceDir(), properties)); } }; // Initialize cores cores.load(); logger.info("Solr home directory: {}", solrHome); for (Entry<String, String> core : coreDefs.entrySet()) { logger.info(" core: {}, dataDir: {}", core.getKey(), core.getValue()); } return cores; }
protected static void setup(String solrHome, String coreName) throws Exception { System.setProperty("solr.solr.home", solrHome); File index = new File(solrHome + "/" + coreName + "/data/index/"); if (index.exists()) { FileUtils.cleanDirectory(index); } File tlog = new File(solrHome + "/" + coreName + "/data/tlog/"); if (tlog.exists()) { FileUtils.cleanDirectory(tlog); } coreContainer = new CoreContainer(solrHome); coreContainer.load(); solr = new EmbeddedSolrServer(coreContainer, coreName); solrCore = coreContainer.getCore(coreName); try { solr.deleteByQuery("*:*"); solr.commit(); } catch (SolrException e) { // might get "no such core" in the multi-core config } }
private CoreContainer init() throws Exception { if (solrHomeDirectory.exists()) { FileUtils.deleteDirectory(solrHomeDirectory); } assertTrue("Failed to mkdirs workDir", solrHomeDirectory.mkdirs()); for (int idx = 1; idx < 10; ++idx) { copyMinConf(new File(solrHomeDirectory, "collection" + idx)); } SolrResourceLoader loader = new SolrResourceLoader(solrHomeDirectory.getAbsolutePath()); File solrXml = new File(solrHomeDirectory, "solr.xml"); FileUtils.write(solrXml, LOTS_SOLR_XML, Charsets.UTF_8.toString()); ConfigSolrXmlOld config = (ConfigSolrXmlOld) ConfigSolr.fromFile(loader, solrXml); CoresLocator locator = new SolrXMLCoresLocator.NonPersistingLocator(LOTS_SOLR_XML, config); final CoreContainer cores = new CoreContainer(loader, config, locator); cores.load(); return cores; }
public void testFlowWithEmpty() throws Exception { // reused state Map<String, Exception> failures = null; Collection<String> cores = null; Exception fail = null; init("empty_flow"); // solr.xml File solrXml = new File(solrHome, "solr.xml"); FileUtils.write(solrXml, EMPTY_SOLR_XML, IOUtils.CHARSET_UTF_8.toString()); // ---- // init the CoreContainer 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", 0, cores.size()); // 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", 0, cores.size()); // 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")); } // let the test end here, with some recorded failures, and let cleanUp() // verify that there is no problem shuting down CoreContainer with known // SolrCore failures }
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()); }