Search in sources :

Example 51 with CoreContainer

use of org.apache.solr.core.CoreContainer in project stanbol by apache.

the class SolrServerPublishingComponent method updateFilter.

/**
     * A change was made to the tracked CoreContainer (adding ,removal, ranking change).
     * This removes and re-add the Servlet filter to apply such changes.
     * @param name The name of the filter to be updated
     * @param ref The serviceReference for the new CoreContainer to be added for
     * the parsed name. <code>null</code> if the filter for that name needs only
     * to be removed
     * @param server The {@link CoreContainer} may be parsed in cases a reference
     * is already available. If not the {@link #tracker} is used to look it up
     * based on the parsed reference. This is basically a workaround for the
     * fact that if the call originates form 
     * {@link ServiceTrackerCustomizer#addingService(ServiceReference)} the
     * {@link CoreContainer} is not yet available via the tracker.
     */
protected void updateFilter(String name, ServiceReference ref, CoreContainer server) {
    String serverPrefix = gloablPrefix + name;
    Filter filter = published.remove(name);
    if (filter != null) {
        extHttpService.unregisterFilter(filter);
        filter = null;
        log.info("removed ServletFilter for SolrServer {} and prefix {}", name, serverPrefix);
    }
    // else no current filter for that name
    if (ref != null || server != null) {
        if (server == null) {
            server = (CoreContainer) tracker.getService(ref);
        }
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(CoreContainer.class.getClassLoader());
        try {
            filter = new SolrFilter(server);
        } finally {
            Thread.currentThread().setContextClassLoader(classLoader);
        }
        Dictionary<String, Object> filterPrpoerties = new Hashtable<String, Object>();
        filterPrpoerties.put("path-prefix", serverPrefix);
        String filterPrefix = serverPrefix + "/.*";
        try {
            extHttpService.registerFilter(filter, filterPrefix, filterPrpoerties, 0, null);
        } catch (ServletException e) {
            throw new IllegalStateException("Unable to register SolrDispatchFilter for" + "CoreContainer with name" + name + " (prefix: " + filterPrefix + "| properties: " + filterPrpoerties + ").", e);
        }
        log.info("added ServletFilter for SolrServer {} and prefix {}", name, serverPrefix);
    }
// else no new filter to add
}
Also used : ServletException(javax.servlet.ServletException) CoreContainer(org.apache.solr.core.CoreContainer) SolrDispatchFilter(org.apache.solr.servlet.SolrDispatchFilter) DelegatingSolrDispatchFilter(org.apache.stanbol.commons.solr.web.dispatch.DelegatingSolrDispatchFilter) Filter(javax.servlet.Filter) Hashtable(java.util.Hashtable)

Example 52 with CoreContainer

use of org.apache.solr.core.CoreContainer in project stanbol by apache.

the class EmbeddedSolrHelper method makeEmbeddedSolrServer.

/**
     * Create a single core Solr server with it's own folder hierarchy.
     */
public static EmbeddedSolrServer makeEmbeddedSolrServer(File rootFolder, String solrServerId, String coreId, String configName) throws IOException, ParserConfigurationException, SAXException {
    File solrFolder = new File(rootFolder, solrServerId);
    FileUtils.deleteQuietly(solrFolder);
    solrFolder.mkdir();
    // solr conf file
    File solrFile = new File(solrFolder, "solr.xml");
    InputStream is = EmbeddedSolrHelper.class.getResourceAsStream("/solr.xml");
    if (is == null) {
        throw new IllegalArgumentException("missing test solr.xml file");
    }
    IOUtils.copy(is, new FileOutputStream(solrFile));
    // solr conf folder with schema
    File solrCoreFolder = new File(solrFolder, coreId);
    solrCoreFolder.mkdir();
    File solrConfFolder = new File(solrCoreFolder, "conf");
    solrConfFolder.mkdir();
    File schemaFile = new File(solrConfFolder, "schema.xml");
    is = EmbeddedSolrHelper.class.getResourceAsStream("/" + configName + "/conf/schema.xml");
    if (is == null) {
        throw new IllegalArgumentException("missing test schema.xml file");
    }
    IOUtils.copy(is, new FileOutputStream(schemaFile));
    File solrConfigFile = new File(solrConfFolder, "solrconfig.xml");
    is = EmbeddedSolrHelper.class.getResourceAsStream("/" + configName + "/conf/solrconfig.xml");
    if (is == null) {
        throw new IllegalArgumentException("missing test solrconfig.xml file");
    }
    IOUtils.copy(is, new FileOutputStream(solrConfigFile));
    // create the embedded server
    SolrResourceLoader loader = new SolrResourceLoader(solrFolder.getAbsolutePath());
    CoreContainer coreContainer = new CoreContainer(loader);
    //NOTE: with Solr 4.4 we need to call coreContainer.load() otherwise we
    //would be affected by the issue stated at 
    //http://mail-archives.apache.org/mod_mbox/lucene-solr-user/201301.mbox/%3CB7B8B36F1A0BE24F842758C318E56E925EB334%40EXCHDB2.na1.ad.group%3E
    //while this was introduced with 4.1 this only affects this code with 4.4
    //as with an API change the methods implicitly calling load() where
    //removed.
    coreContainer.load();
    CoreDescriptor coreDescriptor = new CoreDescriptor(coreContainer, coreId, solrCoreFolder.getAbsolutePath());
    SolrCore core = coreContainer.create(coreDescriptor);
    //        coreContainer.createAndLoad(solrHome, configFile)load();
    coreContainer.register(coreId, core, true);
    return new EmbeddedSolrServer(coreContainer, coreId);
}
Also used : SolrResourceLoader(org.apache.solr.core.SolrResourceLoader) CoreContainer(org.apache.solr.core.CoreContainer) InputStream(java.io.InputStream) CoreDescriptor(org.apache.solr.core.CoreDescriptor) SolrCore(org.apache.solr.core.SolrCore) FileOutputStream(java.io.FileOutputStream) File(java.io.File) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer)

Example 53 with CoreContainer

use of org.apache.solr.core.CoreContainer in project Xponents by OpenSextant.

the class SolrProxy method setupCore.

/**
     * Creates an EmbeddedSolrServer given solr home &amp; the core to use.
     * These may be null and you get the default.
     *
     * @param _solrHome  solr home
     * @param _coreName  name of core
     * @return the embedded solr server
     * @throws ConfigException on err
     */
public static EmbeddedSolrServer setupCore(String _solrHome, String _coreName) throws ConfigException {
    try {
        CoreContainer solrContainer;
        if (_solrHome == null) {
            solrContainer = new CoreContainer();
        } else {
            solrContainer = new CoreContainer(_solrHome);
        }
        // since Solr 4.4
        solrContainer.load();
        return new EmbeddedSolrServer(solrContainer, _coreName);
    } catch (Exception err) {
        throw new ConfigException("Failed to set up Embedded Solr at " + _solrHome + " CORE:" + _coreName, err);
    }
}
Also used : CoreContainer(org.apache.solr.core.CoreContainer) ConfigException(org.opensextant.ConfigException) EmbeddedSolrServer(org.apache.solr.client.solrj.embedded.EmbeddedSolrServer) MalformedURLException(java.net.MalformedURLException) ConfigException(org.opensextant.ConfigException) IOException(java.io.IOException) SolrServerException(org.apache.solr.client.solrj.SolrServerException)

Example 54 with CoreContainer

use of org.apache.solr.core.CoreContainer in project lucene-solr by apache.

the class SolrSlf4jReporterTest method testReporter.

@Test
public void testReporter() throws Exception {
    LogWatcherConfig watcherCfg = new LogWatcherConfig(true, null, null, 100);
    LogWatcher watcher = LogWatcher.newRegisteredLogWatcher(watcherCfg, null);
    watcher.setThreshold("INFO");
    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(new SolrResourceLoader(home), solrXml);
    CoreContainer cc = createCoreContainer(cfg, new TestHarness.TestCoresLocator(DEFAULT_TEST_CORENAME, initCoreDataDir.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 reporter = reporters.get("test1");
    assertNotNull(reporter);
    assertTrue(reporter instanceof SolrSlf4jReporter);
    reporter = reporters.get("test2");
    assertNotNull(reporter);
    assertTrue(reporter instanceof SolrSlf4jReporter);
    watcher.reset();
    Thread.sleep(5000);
    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
    assertTrue(history.stream().filter(d -> "solr.node".equals(d.getFirstValue("logger"))).count() > 0);
    assertTrue(history.stream().filter(d -> "foobar".equals(d.getFirstValue("logger"))).count() > 0);
}
Also used : LogWatcherConfig(org.apache.solr.logging.LogWatcherConfig) Path(java.nio.file.Path) LogWatcherConfig(org.apache.solr.logging.LogWatcherConfig) SolrDocumentList(org.apache.solr.common.SolrDocumentList) TestHarness(org.apache.solr.util.TestHarness) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) CoreContainer(org.apache.solr.core.CoreContainer) SolrTestCaseJ4(org.apache.solr.SolrTestCaseJ4) SolrXmlConfig(org.apache.solr.core.SolrXmlConfig) SolrResourceLoader(org.apache.solr.core.SolrResourceLoader) Paths(java.nio.file.Paths) Map(java.util.Map) SolrMetricManager(org.apache.solr.metrics.SolrMetricManager) LogWatcher(org.apache.solr.logging.LogWatcher) NodeConfig(org.apache.solr.core.NodeConfig) Path(java.nio.file.Path) SolrMetricReporter(org.apache.solr.metrics.SolrMetricReporter) SolrDocumentList(org.apache.solr.common.SolrDocumentList) LogWatcher(org.apache.solr.logging.LogWatcher) SolrResourceLoader(org.apache.solr.core.SolrResourceLoader) CoreContainer(org.apache.solr.core.CoreContainer) TestHarness(org.apache.solr.util.TestHarness) SolrMetricManager(org.apache.solr.metrics.SolrMetricManager) NodeConfig(org.apache.solr.core.NodeConfig) SolrMetricReporter(org.apache.solr.metrics.SolrMetricReporter) Test(org.junit.Test)

Example 55 with CoreContainer

use of org.apache.solr.core.CoreContainer in project lucene-solr by apache.

the class SolrCloudReportersTest method testDefaultPlugins.

@Test
public void testDefaultPlugins() throws Exception {
    String solrXml = IOUtils.toString(SolrCloudReportersTest.class.getResourceAsStream("/solr/solr.xml"), "UTF-8");
    configureCluster(2).withSolrXml(solrXml).configure();
    cluster.uploadConfigSet(Paths.get(TEST_PATH().toString(), "configsets", "minimal", "conf"), "test");
    System.out.println("ZK: " + cluster.getZkServer().getZkAddress());
    CollectionAdminRequest.createCollection("test_collection", "test", 2, 2).setMaxShardsPerNode(4).process(cluster.getSolrClient());
    waitForState("Expected test_collection with 2 shards and 2 replicas", "test_collection", clusterShape(2, 2));
    cluster.getJettySolrRunners().forEach(jetty -> {
        CoreContainer cc = jetty.getCoreContainer();
        SolrMetricManager metricManager = cc.getMetricManager();
        Map<String, SolrMetricReporter> reporters = metricManager.getReporters("solr.cluster");
        assertEquals(reporters.toString(), 0, reporters.size());
        for (String registryName : metricManager.registryNames(".*\\.shard[0-9]\\.replica.*")) {
            reporters = metricManager.getReporters(registryName);
            assertEquals(reporters.toString(), 0 + jmxReporter, reporters.size());
        }
    });
}
Also used : CoreContainer(org.apache.solr.core.CoreContainer) SolrMetricManager(org.apache.solr.metrics.SolrMetricManager) SolrMetricReporter(org.apache.solr.metrics.SolrMetricReporter) Test(org.junit.Test)

Aggregations

CoreContainer (org.apache.solr.core.CoreContainer)95 SolrCore (org.apache.solr.core.SolrCore)30 Test (org.junit.Test)23 SolrException (org.apache.solr.common.SolrException)16 JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)14 File (java.io.File)13 HashMap (java.util.HashMap)11 Replica (org.apache.solr.common.cloud.Replica)11 CoreDescriptor (org.apache.solr.core.CoreDescriptor)11 Path (java.nio.file.Path)10 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)8 SolrMetricManager (org.apache.solr.metrics.SolrMetricManager)8 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)8 Map (java.util.Map)7 SolrResourceLoader (org.apache.solr.core.SolrResourceLoader)7 Properties (java.util.Properties)6 DocCollection (org.apache.solr.common.cloud.DocCollection)6 Slice (org.apache.solr.common.cloud.Slice)6 SolrZkClient (org.apache.solr.common.cloud.SolrZkClient)6