Search in sources :

Example 26 with SolrResourceLoader

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

the class TestManagedResource method testCustomStorageFormat.

/**
   * The ManagedResource storage framework allows the end developer to use a different
   * storage format other than JSON, as demonstrated by this test. 
   */
@SuppressWarnings("rawtypes")
@Test
public void testCustomStorageFormat() throws Exception {
    String resourceId = "/schema/test/foo";
    String storedResourceId = "_schema_test_foo.bin";
    MockAnalysisComponent observer = new MockAnalysisComponent();
    List<ManagedResourceObserver> observers = Arrays.asList((ManagedResourceObserver) observer);
    // put some data in the storage impl so that we can test 
    // initialization of managed data from storage
    Map<String, Object> storedData = new HashMap<>();
    Map<String, Object> initArgs = new HashMap<>();
    // {'initArgs':{'someArg':'someVal', 'arg2':true, 'arg3':['one','two','three'],
    //              'arg4':18, 'arg5':0.9, 'arg6':{ 'uno':1, 'dos':2 }},'"
    initArgs.put("someArg", "someVal");
    initArgs.put("arg2", Boolean.TRUE);
    List<String> arg3list = Arrays.asList("one", "two", "three");
    initArgs.put("arg3", arg3list);
    initArgs.put("arg4", 18L);
    initArgs.put("arg5", 0.9);
    Map<String, Long> arg6map = new HashMap<>();
    arg6map.put("uno", 1L);
    arg6map.put("dos", 2L);
    initArgs.put("arg6", arg6map);
    storedData.put("initArgs", initArgs);
    List<String> managedList = new ArrayList<>();
    managedList.add("1");
    managedList.add("2");
    managedList.add("3");
    storedData.put(ManagedResource.MANAGED_JSON_LIST_FIELD, managedList);
    ManagedResourceStorage.InMemoryStorageIO storageIO = new ManagedResourceStorage.InMemoryStorageIO();
    storageIO.storage.put(storedResourceId, ser2bytes((Serializable) storedData));
    CustomStorageFormatResource res = new CustomStorageFormatResource(resourceId, new SolrResourceLoader(Paths.get("./")), storageIO);
    res.loadManagedDataAndNotify(observers);
    assertTrue("Observer was not notified by ManagedResource!", observer.wasNotified);
    // now store some data (as if it came from the REST API)
    List<String> updatedData = new ArrayList<>();
    updatedData.add("1");
    updatedData.add("2");
    updatedData.add("3");
    updatedData.add("4");
    res.storeManagedData(updatedData);
    Object stored = res.storage.load(resourceId);
    assertNotNull(stored);
    assertTrue(stored instanceof Map);
    Map storedMap = (Map) stored;
    assertNotNull(storedMap.get("initArgs"));
    List storedList = (List) storedMap.get(ManagedResource.MANAGED_JSON_LIST_FIELD);
    assertTrue(storedList.contains("4"));
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) SolrResourceLoader(org.apache.solr.core.SolrResourceLoader) NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 27 with SolrResourceLoader

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

the class TestManagedResource method testLoadingAndStoringOfManagedData.

/**
   * Tests managed data storage to and loading from {@link ManagedResourceStorage.InMemoryStorageIO}.
   */
@SuppressWarnings("unchecked")
@Test
public void testLoadingAndStoringOfManagedData() throws Exception {
    String resourceId = "/config/test/foo";
    String storedResourceId = "_config_test_foo.json";
    MockAnalysisComponent observer = new MockAnalysisComponent();
    List<ManagedResourceObserver> observers = Arrays.asList((ManagedResourceObserver) observer);
    // put some data in the storage impl so that we can test 
    // initialization of managed data from storage
    String storedJson = "{'initArgs':{'someArg':'someVal', 'arg2':true, 'arg3':['one','two','three']," + " 'arg4':18, 'arg5':0.9, 'arg6':{ 'uno':1, 'dos':2}},'" + ManagedResource.MANAGED_JSON_LIST_FIELD + "':['1','2','3']}";
    ManagedResourceStorage.InMemoryStorageIO storageIO = new ManagedResourceStorage.InMemoryStorageIO();
    storageIO.storage.put(storedResourceId, new BytesRef(json(storedJson)));
    ManagedTestResource res = new ManagedTestResource(resourceId, new SolrResourceLoader(Paths.get("./")), storageIO);
    res.loadManagedDataAndNotify(observers);
    assertTrue("Observer was not notified by ManagedResource!", observer.wasNotified);
    // now update the managed data (as if it came from the REST API)
    List<String> updatedData = new ArrayList<>();
    updatedData.add("1");
    updatedData.add("2");
    updatedData.add("3");
    updatedData.add("4");
    res.storeManagedData(updatedData);
    StringReader stringReader = new StringReader(storageIO.storage.get(storedResourceId).utf8ToString());
    Map<String, Object> jsonObject = (Map<String, Object>) ObjectBuilder.getVal(new JSONParser(stringReader));
    List<String> jsonList = (List<String>) jsonObject.get(ManagedResource.MANAGED_JSON_LIST_FIELD);
    assertTrue("Managed data was not updated correctly!", jsonList.contains("4"));
}
Also used : ArrayList(java.util.ArrayList) SolrResourceLoader(org.apache.solr.core.SolrResourceLoader) StringReader(java.io.StringReader) JSONParser(org.noggit.JSONParser) NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) BytesRef(org.apache.lucene.util.BytesRef) Test(org.junit.Test)

Example 28 with SolrResourceLoader

use of org.apache.solr.core.SolrResourceLoader 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 29 with SolrResourceLoader

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

the class SolrMetricManagerTest method testReporters.

@Test
public void testReporters() throws Exception {
    Random r = random();
    SolrResourceLoader loader = new SolrResourceLoader();
    SolrMetricManager metricManager = new SolrMetricManager();
    PluginInfo[] plugins = new PluginInfo[] { createPluginInfo("universal_foo", null, null), createPluginInfo("multigroup_foo", "jvm, node, core", null), createPluginInfo("multiregistry_foo", null, "solr.node, solr.core.collection1"), createPluginInfo("specific_foo", null, "solr.core.collection1"), createPluginInfo("node_foo", "node", null), createPluginInfo("core_foo", "core", null) };
    String tag = "xyz";
    metricManager.loadReporters(plugins, loader, tag, SolrInfoBean.Group.node);
    Map<String, SolrMetricReporter> reporters = metricManager.getReporters(SolrMetricManager.getRegistryName(SolrInfoBean.Group.node));
    assertEquals(4, reporters.size());
    assertTrue(reporters.containsKey("universal_foo@" + tag));
    assertTrue(reporters.containsKey("multigroup_foo@" + tag));
    assertTrue(reporters.containsKey("node_foo@" + tag));
    assertTrue(reporters.containsKey("multiregistry_foo@" + tag));
    metricManager.loadReporters(plugins, loader, tag, SolrInfoBean.Group.core, "collection1");
    reporters = metricManager.getReporters(SolrMetricManager.getRegistryName(SolrInfoBean.Group.core, "collection1"));
    assertEquals(5, reporters.size());
    assertTrue(reporters.containsKey("universal_foo@" + tag));
    assertTrue(reporters.containsKey("multigroup_foo@" + tag));
    assertTrue(reporters.containsKey("specific_foo@" + tag));
    assertTrue(reporters.containsKey("core_foo@" + tag));
    assertTrue(reporters.containsKey("multiregistry_foo@" + tag));
    metricManager.loadReporters(plugins, loader, tag, SolrInfoBean.Group.jvm);
    reporters = metricManager.getReporters(SolrMetricManager.getRegistryName(SolrInfoBean.Group.jvm));
    assertEquals(2, reporters.size());
    assertTrue(reporters.containsKey("universal_foo@" + tag));
    assertTrue(reporters.containsKey("multigroup_foo@" + tag));
    metricManager.removeRegistry("solr.jvm");
    reporters = metricManager.getReporters(SolrMetricManager.getRegistryName(SolrInfoBean.Group.jvm));
    assertEquals(0, reporters.size());
    metricManager.removeRegistry("solr.node");
    reporters = metricManager.getReporters(SolrMetricManager.getRegistryName(SolrInfoBean.Group.node));
    assertEquals(0, reporters.size());
    metricManager.removeRegistry("solr.core.collection1");
    reporters = metricManager.getReporters(SolrMetricManager.getRegistryName(SolrInfoBean.Group.core, "collection1"));
    assertEquals(0, reporters.size());
}
Also used : SolrResourceLoader(org.apache.solr.core.SolrResourceLoader) Random(java.util.Random) PluginInfo(org.apache.solr.core.PluginInfo) Test(org.junit.Test)

Example 30 with SolrResourceLoader

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

the class JvmMetricsTest method testHiddenSysProps.

@Test
public void testHiddenSysProps() throws Exception {
    Path home = Paths.get(TEST_HOME());
    SolrResourceLoader loader = new SolrResourceLoader(home);
    // default config
    String solrXml = FileUtils.readFileToString(Paths.get(home.toString(), "solr.xml").toFile(), "UTF-8");
    NodeConfig config = SolrXmlConfig.fromString(loader, solrXml);
    NodeConfig.NodeConfigBuilder.DEFAULT_HIDDEN_SYS_PROPS.forEach(s -> {
        assertTrue(s, config.getMetricsConfig().getHiddenSysProps().contains(s));
    });
    // custom config
    solrXml = FileUtils.readFileToString(Paths.get(home.toString(), "solr-hiddensysprops.xml").toFile(), "UTF-8");
    NodeConfig config2 = SolrXmlConfig.fromString(loader, solrXml);
    Arrays.asList("foo", "bar", "baz").forEach(s -> {
        assertTrue(s, config2.getMetricsConfig().getHiddenSysProps().contains(s));
    });
}
Also used : Path(java.nio.file.Path) SolrResourceLoader(org.apache.solr.core.SolrResourceLoader) NodeConfig(org.apache.solr.core.NodeConfig) Test(org.junit.Test)

Aggregations

SolrResourceLoader (org.apache.solr.core.SolrResourceLoader)54 Test (org.junit.Test)18 File (java.io.File)11 InputStream (java.io.InputStream)11 NamedList (org.apache.solr.common.util.NamedList)10 SolrException (org.apache.solr.common.SolrException)9 NodeConfig (org.apache.solr.core.NodeConfig)9 Path (java.nio.file.Path)8 CoreContainer (org.apache.solr.core.CoreContainer)8 InputStreamReader (java.io.InputStreamReader)7 Reader (java.io.Reader)7 Map (java.util.Map)7 IOException (java.io.IOException)6 HashMap (java.util.HashMap)5 LinkedHashMap (java.util.LinkedHashMap)5 XMLResponseParser (org.apache.solr.client.solrj.impl.XMLResponseParser)5 SolrCore (org.apache.solr.core.SolrCore)5 ArrayList (java.util.ArrayList)4 ZkSolrResourceLoader (org.apache.solr.cloud.ZkSolrResourceLoader)4 SolrConfig (org.apache.solr.core.SolrConfig)4