From e8bdf16ea7550ee9db20ac1be9d6cfa0571d6485 Mon Sep 17 00:00:00 2001
From: Michael Peter Christen <mc@yacy.net>
Date: Fri, 7 Feb 2014 00:02:19 +0100
Subject: [PATCH] added statistic information for solr resources in
 PerformanceMemory

---
 htroot/PerformanceMemory_p.html               | 23 +++++++++++++++++
 htroot/PerformanceMemory_p.java               | 25 ++++++++++++++++++-
 .../solr/connector/EmbeddedSolrConnector.java |  7 +++++-
 source/net/yacy/search/index/Fulltext.java    | 13 +++++++---
 4 files changed, 62 insertions(+), 6 deletions(-)

diff --git a/htroot/PerformanceMemory_p.html b/htroot/PerformanceMemory_p.html
index c1ef9995e..200aee8f2 100644
--- a/htroot/PerformanceMemory_p.html
+++ b/htroot/PerformanceMemory_p.html
@@ -74,6 +74,29 @@
       </tr>
     </table>
     
+    <p><strong>Solr Resources:</strong></p>
+    <table border="0" cellpadding="2" cellspacing="1">
+      <tr class="TableHeader" valign="bottom">
+        <td>Class</td>
+        <td>Type</td>
+        <td>Description</td>
+        <td>Statistics</td>
+        <td>Size</td>
+      </tr>
+      #{SolrList}#
+      <tr class="TableCellLight">
+        <td align="left" class="TableCellDark">#[class]#</td>
+        <td align="left" class="TableCellDark">#[type]#</td>
+        <td align="left">#[description]#</td>
+        <td align="left">#[statistics]#</td>
+        <td align="right">#[size]#</td>
+      </tr>
+      #{/SolrList}#
+      <tr class="TableCellDark">
+        <td colspan="5">Total Cache Count = #[SolrCacheCount]#</td>
+      </tr>
+    </table>
+    
     <p><strong>Table RAM Index:</strong></p>
     <table border="0" cellpadding="2" cellspacing="1">
       <tr class="TableHeader" valign="bottom">
diff --git a/htroot/PerformanceMemory_p.java b/htroot/PerformanceMemory_p.java
index 11c1ed259..57bdc0c51 100644
--- a/htroot/PerformanceMemory_p.java
+++ b/htroot/PerformanceMemory_p.java
@@ -26,10 +26,14 @@
 
 //import java.util.Iterator;
 import java.io.File;
+import java.util.Collection;
 import java.util.ConcurrentModificationException;
 import java.util.Iterator;
 import java.util.Map;
 
+import org.apache.solr.core.SolrInfoMBean;
+import org.apache.solr.search.SolrCache;
+
 import net.yacy.cora.protocol.Domains;
 import net.yacy.cora.protocol.RequestHeader;
 import net.yacy.cora.util.ConcurrentLog;
@@ -52,6 +56,8 @@ public class PerformanceMemory_p {
     
     public static serverObjects respond(@SuppressWarnings("unused") final RequestHeader header, final serverObjects post, final serverSwitch env) {
         // return variable that accumulates replacements
+        Switchboard sb = (Switchboard) env;
+        
         final serverObjects prop = new serverObjects();
         if (defaultSettings == null) {
             defaultSettings = FileUtils.loadMap(new File(env.getAppPath(), "defaults/yacy.init"));
@@ -103,11 +109,28 @@ public class PerformanceMemory_p {
         prop.putNum("memoryUsedAfterInitAGC", (memoryTotalAfterInitAGC - memoryFreeAfterInitAGC) / KB);
         prop.putNum("memoryUsedNow", MemoryControl.used() / MB);
 
+        
+        Collection<SolrInfoMBean> solrCaches = sb.index.fulltext().getSolrInfoBeans();
+        int c = 0;
+        int scc = 0;
+        for (SolrInfoMBean sc: solrCaches) {
+            prop.put("SolrList_" + c + "_class", sc.getName());
+            prop.put("SolrList_" + c + "_type", sc instanceof SolrCache ? ((SolrCache<?,?>)sc).name() : "");
+            prop.put("SolrList_" + c + "_description", sc.getDescription());
+            prop.put("SolrList_" + c + "_statistics", sc.getStatistics() == null ? "" : sc.getStatistics().toString().replaceAll(",", ", "));
+            prop.put("SolrList_" + c + "_size", sc instanceof SolrCache ? Integer.toString(((SolrCache<?,?>)sc).size()) : "");
+            if (sc instanceof SolrCache) scc++;
+            c++;
+        }
+        prop.put("SolrList", c);
+        prop.put("SolrCacheCount", scc);
+        
         // write table for Table index sizes
         Iterator<String> i = Table.filenames();
         String filename;
         Map<Table.StatKeys, String> mapx;
-        int p, c = 0;
+        int p;
+        c = 0;
         long mem, totalmem = 0;
         while (i.hasNext()) {
             filename = i.next();
diff --git a/source/net/yacy/cora/federate/solr/connector/EmbeddedSolrConnector.java b/source/net/yacy/cora/federate/solr/connector/EmbeddedSolrConnector.java
index 7039f964e..7a5c20481 100644
--- a/source/net/yacy/cora/federate/solr/connector/EmbeddedSolrConnector.java
+++ b/source/net/yacy/cora/federate/solr/connector/EmbeddedSolrConnector.java
@@ -23,6 +23,7 @@ package net.yacy.cora.federate.solr.connector;
 
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashSet;
 import java.util.Set;
 import java.util.TreeSet;
@@ -82,7 +83,7 @@ public class EmbeddedSolrConnector extends SolrServerConnector implements SolrCo
     
     private final SearchHandler requestHandler;
     private final EmbeddedInstance instance;
-    private SolrCore core;
+    private final SolrCore core;
 
     public EmbeddedSolrConnector(EmbeddedInstance instance) {
         super();
@@ -109,6 +110,10 @@ public class EmbeddedSolrConnector extends SolrServerConnector implements SolrCo
         return 0;
     }
 
+    public Collection<SolrInfoMBean> getSolrInfoBeans() {
+        return this.core.getInfoRegistry().values();
+    }
+    
     @Override
     public void clearCaches() {
         SolrConfig solrConfig = this.core.getSolrConfig();
diff --git a/source/net/yacy/search/index/Fulltext.java b/source/net/yacy/search/index/Fulltext.java
index e9e60b8d8..b9ebb4688 100644
--- a/source/net/yacy/search/index/Fulltext.java
+++ b/source/net/yacy/search/index/Fulltext.java
@@ -75,6 +75,7 @@ import net.yacy.search.schema.WebgraphSchema;
 import org.apache.solr.common.SolrDocument;
 import org.apache.solr.common.SolrException;
 import org.apache.solr.common.SolrInputDocument;
+import org.apache.solr.core.SolrInfoMBean;
 import org.apache.lucene.util.Version;
 
 public final class Fulltext {
@@ -133,14 +134,12 @@ public final class Fulltext {
         }
         
         EmbeddedInstance localCollectionInstance = new EmbeddedInstance(new File(new File(Switchboard.getSwitchboard().appPath, "defaults"), "solr"), solrLocation, CollectionSchema.CORE_NAME, new String[]{CollectionSchema.CORE_NAME, WebgraphSchema.CORE_NAME});
-        EmbeddedSolrConnector localCollectionConnector = new EmbeddedSolrConnector(localCollectionInstance);
-
-        Version luceneVersion = localCollectionConnector.getConfig().getLuceneVersion("luceneMatchVersion");
+        Version luceneVersion = localCollectionInstance.getDefaultCore().getSolrConfig().getLuceneVersion("luceneMatchVersion");
         String lvn = luceneVersion.name();
         ConcurrentLog.info("Fulltext", "using lucene version " + lvn);
         int p = lvn.indexOf('_');
         assert SOLR_PATH.endsWith(lvn.substring(p)) : "luceneVersion = " + lvn + ", solrPath = " + SOLR_PATH + ", p = " + p + ", check defaults/solr/solrconfig.xml";
-        ConcurrentLog.info("Fulltext", "connected solr in " + solrLocation.toString() + ", lucene version " + lvn + ", default core size: " + localCollectionConnector.getSize());
+        ConcurrentLog.info("Fulltext", "connected solr in " + solrLocation.toString() + ", lucene version " + lvn);
         this.solrInstances.connectEmbedded(localCollectionInstance);
     }
 
@@ -197,6 +196,12 @@ public final class Fulltext {
         }
     }
     
+    public Collection<SolrInfoMBean> getSolrInfoBeans() {
+        EmbeddedSolrConnector esc = this.solrInstances.getDefaultEmbeddedConnector();
+        if (esc == null) return new ArrayList<SolrInfoMBean>(0);
+        return esc.getSolrInfoBeans();
+    }
+    
     public int bufferSize() {
         return this.solrInstances.bufferSize();
     }