You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by ch...@apache.org on 2014/06/16 14:17:04 UTC

svn commit: r1602853 - in /jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene: IndexTracker.java LuceneIndexMBean.java LuceneIndexMBeanImpl.java LuceneIndexProvider.java LuceneIndexProviderService.java

Author: chetanm
Date: Mon Jun 16 12:17:03 2014
New Revision: 1602853

URL: http://svn.apache.org/r1602853
Log:
OAK-1893 - MBean to dump Lucene Index content and related stats

Added:
    jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexMBean.java   (with props)
    jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexMBeanImpl.java   (with props)
Modified:
    jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexTracker.java
    jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexProvider.java
    jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexProviderService.java

Modified: jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexTracker.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexTracker.java?rev=1602853&r1=1602852&r2=1602853&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexTracker.java (original)
+++ jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexTracker.java Mon Jun 16 12:17:03 2014
@@ -34,6 +34,7 @@ import static org.apache.jackrabbit.oak.
 import java.io.IOException;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.jackrabbit.oak.commons.PathUtils;
 import org.apache.jackrabbit.oak.spi.commit.CompositeEditor;
@@ -128,6 +129,10 @@ class IndexTracker {
         }
     }
 
+    Set<String> getIndexNodePaths(){
+        return indices.keySet();
+    }
+
     private synchronized IndexNode findIndexNode(String path) {
         // Retry the lookup from acquireIndexNode now that we're
         // synchronized. The acquire() call is guaranteed to succeed

Added: jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexMBean.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexMBean.java?rev=1602853&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexMBean.java (added)
+++ jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexMBean.java Mon Jun 16 12:17:03 2014
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.jackrabbit.oak.plugins.index.lucene;
+
+import java.io.IOException;
+
+import javax.management.openmbean.TabularData;
+
+public interface LuceneIndexMBean {
+    String TYPE = "LuceneIndex";
+
+    TabularData getIndexStats() throws IOException;
+
+    /**
+     * Dumps the lucene index files on to the filesystem
+     *
+     * @param sourcePath path in content where the index files are stores
+     * @param destPath path on server where the index content needs to be copied
+     * @throws IOException
+     */
+    void dumpIndexContent(String sourcePath, String destPath) throws IOException;
+}

Propchange: jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexMBean.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexMBeanImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexMBeanImpl.java?rev=1602853&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexMBeanImpl.java (added)
+++ jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexMBeanImpl.java Mon Jun 16 12:17:03 2014
@@ -0,0 +1,236 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.jackrabbit.oak.plugins.index.lucene;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Set;
+
+import javax.management.MBeanOperationInfo;
+import javax.management.MBeanParameterInfo;
+import javax.management.StandardMBean;
+import javax.management.openmbean.CompositeDataSupport;
+import javax.management.openmbean.CompositeType;
+import javax.management.openmbean.OpenDataException;
+import javax.management.openmbean.OpenType;
+import javax.management.openmbean.SimpleType;
+import javax.management.openmbean.TabularData;
+import javax.management.openmbean.TabularDataSupport;
+import javax.management.openmbean.TabularType;
+
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.store.FSDirectory;
+import org.apache.lucene.store.IOContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+public class LuceneIndexMBeanImpl extends StandardMBean implements LuceneIndexMBean {
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private final IndexTracker indexTracker;
+
+    public LuceneIndexMBeanImpl(IndexTracker indexTracker) {
+        super(LuceneIndexMBean.class, false);
+        this.indexTracker = indexTracker;
+    }
+
+    @Override
+    public TabularData getIndexStats() throws IOException {
+        TabularDataSupport tds;
+        try {
+            TabularType tt = new TabularType(LuceneIndexMBeanImpl.class.getName(),
+                    "Lucene Index Stats", IndexStats.TYPE, new String[]{"path"});
+            tds = new TabularDataSupport(tt);
+            Set<String> indexes = indexTracker.getIndexNodePaths();
+            for (String path : indexes) {
+                IndexNode indexNode = null;
+                try {
+                    indexNode = indexTracker.acquireIndexNode(path);
+                    if (indexNode != null) {
+                        IndexStats stats = new IndexStats(path, indexNode.getSearcher().getIndexReader());
+                        tds.put(stats.toCompositeData());
+                    }
+                } finally {
+                    if (indexNode != null) {
+                        indexNode.release();
+                    }
+                }
+            }
+        } catch (OpenDataException e) {
+            throw new IllegalStateException(e);
+        }
+        return tds;
+    }
+
+    @Override
+    public void dumpIndexContent(String sourcePath, String destPath) throws IOException {
+        IndexNode indexNode = null;
+        try {
+            if(sourcePath == null){
+                sourcePath = "/";
+            }
+
+            indexNode = indexTracker.acquireIndexNode(sourcePath);
+            if (indexNode != null) {
+                log.info("Dumping Lucene directory content for [{}] to [{}]", sourcePath, destPath);
+                Directory source = getDirectory(indexNode.getSearcher().getIndexReader());
+                checkNotNull(source, "IndexSearcher not backed by DirectoryReader");
+                Directory dest = FSDirectory.open(new File(destPath));
+                for (String file : source.listAll()) {
+                    source.copy(dest, file, file, IOContext.DEFAULT);
+                }
+            }
+        } finally {
+            if (indexNode != null) {
+                indexNode.release();
+            }
+        }
+    }
+
+    //~------------------------------------------------------< StandardMBean >
+
+    @Override
+    protected String getDescription(MBeanOperationInfo op, MBeanParameterInfo param, int sequence) {
+        if(op.getName().equals("dumpIndexContent")){
+            switch(sequence){
+                case 0 : return "Index path in content. Can be null";
+                case 1 : return "Target directory path on server";
+            }
+        }
+        return super.getDescription(op, param, sequence);
+    }
+
+    private static class IndexStats {
+        static final String[] FIELD_NAMES = new String[]{
+                "path",
+                "indexSizeStr",
+                "indexSize",
+                "numDocs",
+                "maxDoc",
+                "numDeletedDocs",
+        };
+
+        static final String[] FIELD_DESCRIPTIONS = new String[]{
+                "Path",
+                "Index size in human readable format",
+                "Index size in bytes",
+                "Number of documents in this index.",
+                "The time and date for when the longest query took place",
+                "Number of deleted documents",
+        };
+
+        @SuppressWarnings("rawtypes")
+        static final OpenType[] FIELD_TYPES = new OpenType[]{
+                SimpleType.STRING,
+                SimpleType.STRING,
+                SimpleType.LONG,
+                SimpleType.INTEGER,
+                SimpleType.INTEGER,
+                SimpleType.INTEGER,
+        };
+
+        static final CompositeType TYPE = createCompositeType();
+
+        static CompositeType createCompositeType() {
+            try {
+                return new CompositeType(
+                        IndexStats.class.getName(),
+                        "Composite data type for Lucene Index statistics",
+                        IndexStats.FIELD_NAMES,
+                        IndexStats.FIELD_DESCRIPTIONS,
+                        IndexStats.FIELD_TYPES);
+            } catch (OpenDataException e) {
+                throw new IllegalStateException(e);
+            }
+        }
+
+        private final String path;
+        private final long indexSize;
+        private final int numDocs;
+        private final int maxDoc;
+        private final int numDeletedDocs;
+        private final String indexSizeStr;
+
+        public IndexStats(String path, IndexReader indexReader) throws IOException {
+            this.path = path;
+            numDocs = indexReader.numDocs();
+            maxDoc = indexReader.maxDoc();
+            numDeletedDocs = indexReader.numDeletedDocs();
+            indexSize = dirSize(getDirectory(indexReader));
+            indexSizeStr = humanReadableByteCount(indexSize, true);
+        }
+
+        CompositeDataSupport toCompositeData() {
+            Object[] values = new Object[]{
+                    path,
+                    indexSizeStr,
+                    indexSize,
+                    numDocs,
+                    maxDoc,
+                    numDeletedDocs
+            };
+            try {
+                return new CompositeDataSupport(TYPE, FIELD_NAMES, values);
+            } catch (OpenDataException e) {
+                throw new IllegalStateException(e);
+            }
+        }
+    }
+
+    //~---------------------------------------------------------< Internal >
+
+    private static Directory getDirectory(IndexReader reader) {
+        if (reader instanceof DirectoryReader) {
+            return ((DirectoryReader) reader).directory();
+        }
+        return null;
+    }
+
+    private static long dirSize(Directory directory) throws IOException {
+        long totalFileSize = 0L;
+        String[] files = directory.listAll();
+        if (files == null) {
+            return totalFileSize;
+        }
+        for (String file : files) {
+            totalFileSize += directory.fileLength(file);
+        }
+        return totalFileSize;
+    }
+
+    /**
+     * Based on http://stackoverflow.com/a/3758880/1035417
+     */
+    private static String humanReadableByteCount(long bytes, boolean si) {
+        if (bytes < 0) {
+            return "0";
+        }
+        int unit = si ? 1000 : 1024;
+        if (bytes < unit) {
+            return bytes + " B";
+        }
+        int exp = (int) (Math.log(bytes) / Math.log(unit));
+        String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
+        return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
+    }
+}

Propchange: jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexMBeanImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexProvider.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexProvider.java?rev=1602853&r1=1602852&r2=1602853&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexProvider.java (original)
+++ jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexProvider.java Mon Jun 16 12:17:03 2014
@@ -91,4 +91,8 @@ public class LuceneIndexProvider impleme
         this.setAggregator(analyzer);
         return this;
     }
+
+    IndexTracker getTracker() {
+        return tracker;
+    }
 }

Modified: jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexProviderService.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexProviderService.java?rev=1602853&r1=1602852&r2=1602853&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexProviderService.java (original)
+++ jackrabbit/oak/trunk/oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/LuceneIndexProviderService.java Mon Jun 16 12:17:03 2014
@@ -30,16 +30,20 @@ import org.apache.felix.scr.annotations.
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.ReferencePolicy;
 import org.apache.felix.scr.annotations.ReferencePolicyOption;
+import org.apache.jackrabbit.oak.osgi.OsgiWhiteboard;
 import org.apache.jackrabbit.oak.plugins.index.aggregate.AggregateIndexProvider;
 import org.apache.jackrabbit.oak.plugins.index.aggregate.NodeAggregator;
 import org.apache.jackrabbit.oak.spi.commit.Observer;
 import org.apache.jackrabbit.oak.spi.query.QueryIndexProvider;
+import org.apache.jackrabbit.oak.spi.whiteboard.Registration;
 import org.apache.lucene.analysis.Analyzer;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceRegistration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import static org.apache.jackrabbit.oak.spi.whiteboard.WhiteboardUtils.registerMBean;
+
 @SuppressWarnings("UnusedDeclaration")
 @Component(immediate = true)
 public class LuceneIndexProviderService {
@@ -64,6 +68,8 @@ public class LuceneIndexProviderService 
     )
     protected Analyzer analyzer;
 
+    private Registration mbeanReg;
+
     @Activate
     private void activate(BundleContext bundleContext, Map<String, ?> config) {
         indexProvider = new LuceneIndexProvider();
@@ -73,6 +79,12 @@ public class LuceneIndexProviderService 
 
         regs.add(bundleContext.registerService(QueryIndexProvider.class.getName(), aggregate, null));
         regs.add(bundleContext.registerService(Observer.class.getName(), indexProvider, null));
+
+        mbeanReg = registerMBean(new OsgiWhiteboard(bundleContext),
+                LuceneIndexMBean.class,
+                new LuceneIndexMBeanImpl(indexProvider.getTracker()),
+                LuceneIndexMBean.TYPE,
+                "Lucene Index statistics");
     }
 
     @Deactivate
@@ -81,6 +93,10 @@ public class LuceneIndexProviderService 
             reg.unregister();
         }
 
+        if(mbeanReg != null){
+            mbeanReg.unregister();
+        }
+
         if (indexProvider != null) {
             indexProvider.close();
             indexProvider = null;