Author: chetanm
Date: Fri May 12 05:20:24 2017
New Revision: 1794922

URL: http://svn.apache.org/viewvc?rev=1794922&view=rev
Log:
OAK-6215 - Dump index stats and index definitions

Printer to dump index stats

Added:
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/inventory/
    
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/inventory/IndexPrinter.java
   (with props)
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/inventory/
    
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/inventory/IndexPrinterTest.java
   (with props)

Added: 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/inventory/IndexPrinter.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/inventory/IndexPrinter.java?rev=1794922&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/inventory/IndexPrinter.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/index/inventory/IndexPrinter.java
 Fri May 12 05:20:24 2017
@@ -0,0 +1,145 @@
+/*
+ * 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.inventory;
+
+import java.io.PrintWriter;
+import java.util.List;
+
+import com.google.common.base.Strings;
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ListMultimap;
+import org.apache.felix.inventory.Format;
+import org.apache.felix.inventory.InventoryPrinter;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Properties;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.jackrabbit.oak.api.jmx.IndexStatsMBean;
+import org.apache.jackrabbit.oak.commons.IOUtils;
+import org.apache.jackrabbit.oak.plugins.index.AsyncIndexInfo;
+import org.apache.jackrabbit.oak.plugins.index.AsyncIndexInfoService;
+import org.apache.jackrabbit.oak.plugins.index.IndexInfo;
+import org.apache.jackrabbit.oak.plugins.index.IndexInfoService;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+@Component
+@Service
+@Properties({
+        @Property(name = "felix.inventory.printer.name", value = 
"oak-index-stats"),
+        @Property(name = "felix.inventory.printer.title", value = "Oak Index 
Stats"),
+        @Property(name = "felix.inventory.printer.format", value = { "TEXT"})
+})
+public class IndexPrinter implements InventoryPrinter {
+
+    @Reference
+    private IndexInfoService indexInfoService;
+
+    @Reference
+    private AsyncIndexInfoService asyncIndexInfoService;
+
+    public IndexPrinter() {
+    }
+
+    public IndexPrinter(IndexInfoService indexInfoService, 
AsyncIndexInfoService asyncIndexInfoService) {
+        this.indexInfoService = checkNotNull(indexInfoService);
+        this.asyncIndexInfoService = checkNotNull(asyncIndexInfoService);
+    }
+
+    @Override
+    public void print(PrintWriter pw, Format format, boolean isZip) {
+        //TODO Highlight if failing
+        printAsyncIndexInfo(pw);
+        printIndexInfo(pw);
+    }
+
+    private void printAsyncIndexInfo(PrintWriter pw) {
+        List<String> asyncLanes = 
ImmutableList.copyOf(asyncIndexInfoService.getAsyncLanes());
+        String title = "Async Indexers State";
+        printTitle(pw, title);
+        pw.printf("Number of async indexer lanes : %d%n", asyncLanes.size());
+        pw.println();
+        for (String lane : asyncLanes) {
+            pw.println(lane);
+            AsyncIndexInfo info = asyncIndexInfoService.getInfo(lane);
+            if (info != null) {
+                        pw.printf("    Last Indexed To      : %tc%n", 
info.getLastIndexedTo());
+                IndexStatsMBean stats = info.getStatsMBean();
+                if (stats != null) {
+                        pw.printf("    Status              : %s%n", 
stats.getStatus());
+                        pw.printf("    Failing             : %s%n", 
stats.isFailing());
+                        pw.printf("    Paused              : %s%n", 
stats.isPaused());
+                    if (stats.isFailing()) {
+                        pw.printf("    Failing Since       : %s%n", 
stats.getFailingSince());
+                        pw.printf("    Latest Error        : %s%n", 
stats.getLatestError());
+                    }
+                }
+                pw.println();
+            }
+        }
+    }
+
+    private static void printTitle(PrintWriter pw, String title) {
+        pw.println(title);
+        pw.println(Strings.repeat("=", title.length()));
+    }
+
+    private void printIndexInfo(PrintWriter pw) {
+        ListMultimap<String, IndexInfo> infos = ArrayListMultimap.create();
+        for (IndexInfo info : indexInfoService.getAllIndexInfo()) {
+            infos.put(info.getType(), info);
+        }
+
+        pw.printf("Total number of indexes : %d%n", infos.size());
+        for (String type : infos.keySet()){
+            List<IndexInfo> typedInfo = infos.get(type);
+            String title = String.format("%s(%d)", type, typedInfo.size());
+            printTitle(pw, title);
+            pw.println();
+            for (IndexInfo info : typedInfo){
+                printIndexInfo(pw, info);
+            }
+        }
+    }
+
+    private static void printIndexInfo(PrintWriter pw, IndexInfo info) {
+        pw.println(info.getIndexPath());
+        pw.printf("    Type                    : %s%n", info.getType());
+        if (info.getAsyncLaneName() != null) {
+            pw.printf("    async                   : true%n");
+            pw.printf("    async name              : %s%n", 
info.getAsyncLaneName());
+        }
+
+        if (info.getIndexedUpToTime() > 0){
+            pw.printf("    Last Indexed Upto       : %tc%n", 
info.getIndexedUpToTime());
+        }
+
+        if (info.getSizeInBytes() > 0){
+            pw.printf("    Size                    : %s%n", 
IOUtils.humanReadableByteCount(info.getSizeInBytes()));
+        }
+
+        if (info.getEstimatedEntryCount() > 0){
+            pw.printf("    Estimated entry count   : %d%n", 
info.getEstimatedEntryCount());
+        }
+        pw.println();
+    }
+}

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

Added: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/inventory/IndexPrinterTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/inventory/IndexPrinterTest.java?rev=1794922&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/inventory/IndexPrinterTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/inventory/IndexPrinterTest.java
 Fri May 12 05:20:24 2017
@@ -0,0 +1,131 @@
+/*
+ * 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.inventory;
+
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+import org.apache.felix.inventory.Format;
+import org.apache.jackrabbit.oak.plugins.index.AsyncIndexInfo;
+import org.apache.jackrabbit.oak.plugins.index.AsyncIndexInfoService;
+import org.apache.jackrabbit.oak.plugins.index.IndexInfo;
+import org.apache.jackrabbit.oak.plugins.index.IndexInfoService;
+import org.junit.Test;
+
+import static java.util.Arrays.asList;
+import static java.util.Collections.emptyList;
+import static org.hamcrest.Matchers.containsString;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class IndexPrinterTest {
+    private AsyncIndexInfoService asyncInfo = 
mock(AsyncIndexInfoService.class);
+    private IndexInfoService indexInfo = mock(IndexInfoService.class);
+
+    private IndexPrinter printer = new IndexPrinter(indexInfo, asyncInfo);
+
+    @Test
+    public void asyncIndexInfo() throws Exception {
+        when(indexInfo.getAllIndexInfo()).thenReturn(emptyList());
+        when(asyncInfo.getAsyncLanes()).thenReturn(asList("foo-async", 
"bar-async"));
+        when(asyncInfo.getInfo("foo-async"))
+                .thenReturn(new AsyncIndexInfo("foo-async", 0, 0, false, 
null));
+
+        String output = getPrintOutput();
+
+        assertThat(output, containsString("foo-async"));
+    }
+
+    @Test
+    public void indexInfo() throws Exception{
+        when(asyncInfo.getAsyncLanes()).thenReturn(emptyList());
+
+        TestInfo info1 = new TestInfo("/oak:index/fooIndex", "property");
+        TestInfo info2 = new TestInfo("/oak:index/barIndex", "lucene");
+        info2.laneName = "async";
+
+        when(indexInfo.getAllIndexInfo()).thenReturn(asList(info1, info2));
+
+        String output = getPrintOutput();
+        assertThat(output, containsString("/oak:index/fooIndex"));
+        assertThat(output, containsString("/oak:index/barIndex"));
+        assertThat(output, containsString("async"));
+
+    }
+
+    private String getPrintOutput() {
+        StringWriter sw = new StringWriter();
+        printer.print(new PrintWriter(sw), Format.TEXT, false);
+        return sw.toString();
+    }
+
+    private static class TestInfo implements IndexInfo {
+        final String indexPath;
+        final String type;
+        String laneName;
+
+        private TestInfo(String indexPath, String type) {
+            this.indexPath = indexPath;
+            this.type = type;
+        }
+
+        @Override
+        public String getIndexPath() {
+            return indexPath;
+        }
+
+        @Override
+        public String getType() {
+            return type;
+        }
+
+        @Override
+        public String getAsyncLaneName() {
+            return null;
+        }
+
+        @Override
+        public long getLastUpdatedTime() {
+            return 0;
+        }
+
+        @Override
+        public long getIndexedUpToTime() {
+            return 0;
+        }
+
+        @Override
+        public long getEstimatedEntryCount() {
+            return 0;
+        }
+
+        @Override
+        public long getSizeInBytes() {
+            return 0;
+        }
+
+        @Override
+        public boolean hasIndexDefinitionChangedWithoutReindexing() {
+            return false;
+        }
+    }
+
+}
\ No newline at end of file

Propchange: 
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/index/inventory/IndexPrinterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to