org.apache.solr.core.SolrXmlConfig Java Examples

The following examples show how to use org.apache.solr.core.SolrXmlConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SolrDispatchFilter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Get the NodeConfig whether stored on disk, in ZooKeeper, etc.
 * This may also be used by custom filters to load relevant configuration.
 * @return the NodeConfig
 */
public static NodeConfig loadNodeConfig(Path solrHome, Properties nodeProperties) {
  if (!StringUtils.isEmpty(System.getProperty("solr.solrxml.location"))) {
    log.warn("Solr property solr.solrxml.location is no longer supported. Will automatically load solr.xml from ZooKeeper if it exists");
  }

  String zkHost = System.getProperty("zkHost");
  if (!StringUtils.isEmpty(zkHost)) {
    int startUpZkTimeOut = Integer.getInteger("waitForZk", 30);
    startUpZkTimeOut *= 1000;
    try (SolrZkClient zkClient = new SolrZkClient(zkHost, startUpZkTimeOut)) {
      if (zkClient.exists("/solr.xml", true)) {
        log.info("solr.xml found in ZooKeeper. Loading...");
        byte[] data = zkClient.getData("/solr.xml", null, null, true);
        return SolrXmlConfig.fromInputStream(solrHome, new ByteArrayInputStream(data), nodeProperties, true);
      }
    } catch (Exception e) {
      throw new SolrException(ErrorCode.SERVER_ERROR, "Error occurred while loading solr.xml from zookeeper", e);
    }
    log.info("Loading solr.xml from SolrHome (not found in ZooKeeper)");
  }

  return SolrXmlConfig.fromSolrHome(solrHome, nodeProperties);
}
 
Example #2
Source File: JvmMetricsTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testHiddenSysProps() throws Exception {
  Path home = Paths.get(TEST_HOME());

  // default config
  String solrXml = FileUtils.readFileToString(Paths.get(home.toString(), "solr.xml").toFile(), "UTF-8");
  NodeConfig config = SolrXmlConfig.fromString(home, solrXml);
  NodeConfig.NodeConfigBuilder.DEFAULT_HIDDEN_SYS_PROPS.forEach(s -> {
    assertTrue(s, config.getMetricsConfig().getHiddenSysProps().contains(s));
  });

  // custom config
  solrXml = FileUtils.readFileToString(home.resolve("solr-hiddensysprops.xml").toFile(), "UTF-8");
  NodeConfig config2 = SolrXmlConfig.fromString(home, solrXml);
  Arrays.asList("foo", "bar", "baz").forEach(s -> {
    assertTrue(s, config2.getMetricsConfig().getHiddenSysProps().contains(s));
  });
}
 
Example #3
Source File: EmbeddedSolrServerProvider.java    From vind with Apache License 2.0 5 votes vote down vote up
@Override
public SolrClient getInstance() {
    try {
        final Path tmpSolrHome = Files.createTempDirectory("solr-home");
        final Path solrHome = FileSystemUtils.toPath(this.getClass().getResource(HOME_PATH));

        Files.walkFileTree(solrHome, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new TreeCopier(solrHome, tmpSolrHome));

        final Path tmpSolrConfig = tmpSolrHome.resolve(SolrXmlConfig.SOLR_XML_FILE);

        // Needed for the tests. For some reason Solr is not replacing the solrConfig.xml ${runtimeLib} property with
        // the system property value but with the defined in the core.properties file.
        //TODO could be nicer ...
        final Properties properties = new Properties();
        final String corePropertiesFile = String.join("/",tmpSolrHome.toAbsolutePath().toString(),CORE_NAME,CORE_PROPERTIES_FILE);
        final FileInputStream iS = new FileInputStream(corePropertiesFile);
        properties.load(iS);
        iS.close();
        properties.setProperty("runtimeLib", "false");

        final FileOutputStream oS = new FileOutputStream(tmpSolrHome.toAbsolutePath().toString()+ CORE_NAME + CORE_PROPERTIES_FILE);
        properties.store(oS, null);
        oS.close();

        final CoreContainer container = CoreContainer.createAndLoad(tmpSolrHome, tmpSolrConfig);
        return new SolrClientWrapper(container, CORE_NAME, tmpSolrHome);

    } catch (URISyntaxException | IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #4
Source File: SolrTestCaseJ4.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Call this from @BeforeClass to set up the test harness and update handler with no cores.
 *
 * @param solrHome The solr home directory.
 * @param xmlStr - the text of an XML file to use. If null, use the what's the absolute minimal file.
 * @throws Exception Lost of file-type things can go wrong.
 */
public static void setupNoCoreTest(Path solrHome, String xmlStr) throws Exception {

  if (xmlStr == null)
    xmlStr = "<solr></solr>";
  Files.write(solrHome.resolve(SolrXmlConfig.SOLR_XML_FILE), xmlStr.getBytes(StandardCharsets.UTF_8));
  h = new TestHarness(SolrXmlConfig.fromSolrHome(solrHome, new Properties()));
  lrf = h.getRequestFactory("/select", 0, 20, CommonParams.VERSION, "2.2");
}
 
Example #5
Source File: SolrGraphiteReporterTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testReporter() throws Exception {
  int jmxReporter = JmxUtil.findFirstMBeanServer() != null ? 1: 0;
  Path home = Paths.get(TEST_HOME());
  // define these properties, they are used in solrconfig.xml
  System.setProperty("solr.test.sys.prop1", "propone");
  System.setProperty("solr.test.sys.prop2", "proptwo");

  MockGraphite mock = new MockGraphite();
  try {
    mock.start();
    Thread.sleep(1000);
    // define the port where MockGraphite is running
    System.setProperty("mock-graphite-port", String.valueOf(mock.port));
    String solrXml = FileUtils.readFileToString(Paths.get(home.toString(), "solr-graphitereporter.xml").toFile(), "UTF-8");
    NodeConfig cfg = SolrXmlConfig.fromString(home, solrXml);
    CoreContainer cc = createCoreContainer(cfg, new TestHarness.TestCoresLocator
                                           (DEFAULT_TEST_CORENAME, initAndGetDataDir().getAbsolutePath(),
                                            "solrconfig.xml", "schema.xml"));
                                           
    h.coreName = DEFAULT_TEST_CORENAME;
    SolrMetricManager metricManager = cc.getMetricManager();
    Map<String, SolrMetricReporter> reporters = metricManager.getReporters("solr.node");
    assertEquals(1 + jmxReporter, reporters.size());
    SolrMetricReporter reporter = reporters.get("test");
    assertNotNull(reporter);
    assertTrue(reporter instanceof SolrGraphiteReporter);
    Thread.sleep(5000);
    assertTrue(mock.lines.size() >= 3);
    String[] frozenLines = mock.lines.toArray(new String[mock.lines.size()]);
    for (String line : frozenLines) {
      assertTrue(line, line.startsWith("test.solr.node.CONTAINER.cores."));
    }
  } finally {
    mock.close();
  }
}
 
Example #6
Source File: Factory.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public SolrClient create(String spec) {
	Path solrHome = SolrResourceLoader.locateSolrHome();
	Path configFile = solrHome.resolve(SolrXmlConfig.SOLR_XML_FILE);
	return new EmbeddedSolrServer(CoreContainer.createAndLoad(solrHome, configFile), "embedded");
}
 
Example #7
Source File: SolrMetricsIntegrationTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Before
public void beforeTest() throws Exception {
  Path home = Paths.get(TEST_HOME());
  // define these properties, they are used in solrconfig.xml
  System.setProperty("solr.test.sys.prop1", "propone");
  System.setProperty("solr.test.sys.prop2", "proptwo");
  String solrXml = FileUtils.readFileToString(Paths.get(home.toString(), "solr-metricreporter.xml").toFile(), "UTF-8");
  NodeConfig cfg = SolrXmlConfig.fromString(home, solrXml);
  cc = createCoreContainer(cfg, new TestHarness.TestCoresLocator
                           (DEFAULT_TEST_CORENAME, initAndGetDataDir().getAbsolutePath(),
                            "solrconfig.xml", "schema.xml"));
                           
  h.coreName = DEFAULT_TEST_CORENAME;
  jmxReporter = JmxUtil.findFirstMBeanServer() != null ? 1 : 0;
  metricManager = cc.getMetricManager();
  tag = h.getCore().getCoreMetricManager().getTag();
  // initially there are more reporters, because two of them are added via a matching collection name
  Map<String, SolrMetricReporter> reporters = metricManager.getReporters("solr.core." + DEFAULT_TEST_CORENAME);
  assertEquals(INITIAL_REPORTERS.length + jmxReporter, reporters.size());
  for (String r : INITIAL_REPORTERS) {
    assertTagged(reporters, r);
  }
  // test rename operation
  cc.rename(DEFAULT_TEST_CORENAME, CORE_NAME);
  h.coreName = CORE_NAME;
  cfg = cc.getConfig();
  PluginInfo[] plugins = cfg.getMetricsConfig().getMetricReporters();
  assertNotNull(plugins);
  assertEquals(10 + jmxReporter, plugins.length);
  reporters = metricManager.getReporters("solr.node");
  assertEquals(4 + jmxReporter, reporters.size());
  assertTrue("Reporter '" + REPORTER_NAMES[0] + "' missing in solr.node", reporters.containsKey(REPORTER_NAMES[0]));
  assertTrue("Reporter '" + UNIVERSAL + "' missing in solr.node", reporters.containsKey(UNIVERSAL));
  assertTrue("Reporter '" + MULTIGROUP + "' missing in solr.node", reporters.containsKey(MULTIGROUP));
  assertTrue("Reporter '" + MULTIREGISTRY + "' missing in solr.node", reporters.containsKey(MULTIREGISTRY));
  SolrMetricReporter reporter = reporters.get(REPORTER_NAMES[0]);
  assertTrue("Reporter " + reporter + " is not an instance of " + MockMetricReporter.class.getName(),
      reporter instanceof  MockMetricReporter);
  reporter = reporters.get(UNIVERSAL);
  assertTrue("Reporter " + reporter + " is not an instance of " + MockMetricReporter.class.getName(),
      reporter instanceof  MockMetricReporter);
}
 
Example #8
Source File: MetricsConfigTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private NodeConfig loadNodeConfig() throws Exception {
  InputStream is = MetricsConfigTest.class.getResourceAsStream("/solr/solr-metricsconfig.xml");
  return SolrXmlConfig.fromInputStream(TEST_PATH(), is, new Properties()); //TODO pass in props
}
 
Example #9
Source File: SolrSlf4jReporterTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testReporter() throws Exception {
  ensureLoggingConfiguredAppropriately();
  Path home = Paths.get(TEST_HOME());
  // define these properties, they are used in solrconfig.xml
  System.setProperty("solr.test.sys.prop1", "propone");
  System.setProperty("solr.test.sys.prop2", "proptwo");

  String solrXml = FileUtils.readFileToString(Paths.get(home.toString(), "solr-slf4jreporter.xml").toFile(), "UTF-8");
  NodeConfig cfg = SolrXmlConfig.fromString(home, solrXml);
  CoreContainer cc = createCoreContainer(cfg, new TestHarness.TestCoresLocator
                                         (DEFAULT_TEST_CORENAME, initAndGetDataDir().getAbsolutePath(),
                                          "solrconfig.xml", "schema.xml"));
                                         
  h.coreName = DEFAULT_TEST_CORENAME;
  SolrMetricManager metricManager = cc.getMetricManager();
  Map<String, SolrMetricReporter> reporters = metricManager.getReporters("solr.node");
  assertTrue(reporters.toString(), reporters.size() >= 2);
  SolrMetricReporter reporter1 = reporters.get("test1");
  assertNotNull(reporter1);
  assertTrue(reporter1 instanceof SolrSlf4jReporter);
  SolrMetricReporter reporter2 = reporters.get("test2");
  assertNotNull(reporter2);
  assertTrue(reporter2 instanceof SolrSlf4jReporter);

  LogWatcherConfig watcherCfg = new LogWatcherConfig(true, null, null, 100);
  @SuppressWarnings({"rawtypes"})
  LogWatcher watcher = LogWatcher.newRegisteredLogWatcher(watcherCfg, null);
  watcher.setThreshold("INFO");

  watcher.reset();
  int cnt = 20;
  boolean active;
  do {
    Thread.sleep(1000);
    cnt--;
    active = ((SolrSlf4jReporter)reporter1).isActive() && ((SolrSlf4jReporter)reporter2).isActive();
  } while (!active && cnt > 0);
  if (!active) {
    fail("One or more reporters didn't become active in 20 seconds");
  }
  Thread.sleep(10000);

  SolrDocumentList history = watcher.getHistory(-1, null);
  // dot-separated names are treated like class names and collapsed
  // in regular log output, but here we get the full name
  if (history.stream().filter(d -> "solr.node".equals(d.getFirstValue("logger"))).count() == 0) {
    fail("No 'solr.node' logs in: " + history.toString());
  }
  if (history.stream().filter(d -> "foobar".equals(d.getFirstValue("logger"))).count() == 0) {
    fail("No 'foobar' logs in: " + history.toString());
  }
  if (history.stream().filter(d -> "x:collection1".equals(d.getFirstValue("core"))).count() == 0) {
    fail("No 'solr.core' or MDC context in logs: " + history.toString());
  }
}
 
Example #10
Source File: TestHarness.java    From lucene-solr with Apache License 2.0 2 votes vote down vote up
/**
 * Create a TestHarness using a specific solr home directory and solr xml
 * @param solrHome the solr home directory
 * @param solrXml the text of a solrxml
 */
public TestHarness(Path solrHome, String solrXml) {
  this(SolrXmlConfig.fromString(solrHome, solrXml));
}