jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1181060 - in /jackrabbit/trunk: jackrabbit-api/src/main/java/org/apache/jackrabbit/api/jmx/ jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/ jackrabbit-core/src/mai...
Date Mon, 10 Oct 2011 16:35:30 GMT
Author: jukka
Date: Mon Oct 10 16:35:29 2011
New Revision: 1181060

URL: http://svn.apache.org/viewvc?rev=1181060&view=rev
Log:
JCR-2936: JMX Bindings for Jackrabbit

Merge parts of the JCR-2936 branch back to trunk

Added:
    jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/jmx/
    jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/jmx/CoreStatManagerMBean.java
    jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/jmx/ManagedRepositoryMBean.java
    jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/jmx/QueryStatManagerMBean.java
    jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/
    jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/CoreStat.java
    jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/QueryStat.java
    jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/QueryStatDto.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/CoreStatManager.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/QueryStatManager.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/CoreStatImpl.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoComparator.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoImpl.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatImpl.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/StatManager.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/util/
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/util/CachingOpsPerSecondDto.java

Added: jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/jmx/CoreStatManagerMBean.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/jmx/CoreStatManagerMBean.java?rev=1181060&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/jmx/CoreStatManagerMBean.java
(added)
+++ jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/jmx/CoreStatManagerMBean.java
Mon Oct 10 16:35:29 2011
@@ -0,0 +1,37 @@
+/*
+ * 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.api.jmx;
+
+/**
+ * The CoreStatManagerMBean exposes the CoreStat info via JMX
+ * 
+ */
+public interface CoreStatManagerMBean {
+
+    String NAME = "org.apache.jackrabbit:type=CoreStats";
+
+    long getNumberOfSessions();
+
+    void resetNumberOfSessions();
+
+    double getReadOpsPerSecond();
+
+    double getWriteOpsPerSecond();
+
+    void resetNumberOfOperations();
+
+}

Added: jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/jmx/ManagedRepositoryMBean.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/jmx/ManagedRepositoryMBean.java?rev=1181060&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/jmx/ManagedRepositoryMBean.java
(added)
+++ jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/jmx/ManagedRepositoryMBean.java
Mon Oct 10 16:35:29 2011
@@ -0,0 +1,68 @@
+/*
+ * 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.api.jmx;
+
+import java.util.Map;
+
+import javax.jcr.RepositoryException;
+
+/**
+ * Interface for managing a JCR repository as a JMX MBean.
+ *
+ * @since Apache Jackrabbit 2.3
+ */
+public interface ManagedRepositoryMBean {
+
+    /**
+     * Returns the name of this repository implementation.
+     *
+     * @see javax.jcr.Repository#REP_NAME_DESC
+     * @return name of this repository implementation
+     */
+    String getName();
+
+    /**
+     * Returns the version of this repository implementation.
+     *
+     * @see javax.jcr.Repository#REP_VERSION_DESC
+     * @return version of this repository implementation
+     */
+    String getVersion();
+
+    /**
+     * Returns all the repository descriptors.
+     *
+     * @return repository descriptors
+     */
+    Map<String, String> getDescriptors();
+
+    /**
+     * Returns the names of all the workspaces in this repository.
+     *
+     * @return workspace names
+     */
+    String[] getWorkspaceNames();
+
+    /**
+     * Creates a new workspace with the given name.
+     *
+     * @param name workspace name
+     * @throws RepositoryException if the workspace could not be created
+     */
+    void createWorkspace(String name) throws RepositoryException;
+
+}

Added: jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/jmx/QueryStatManagerMBean.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/jmx/QueryStatManagerMBean.java?rev=1181060&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/jmx/QueryStatManagerMBean.java
(added)
+++ jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/jmx/QueryStatManagerMBean.java
Mon Oct 10 16:35:29 2011
@@ -0,0 +1,50 @@
+/*
+ * 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.api.jmx;
+
+import javax.management.openmbean.TabularData;
+
+import org.apache.jackrabbit.api.stats.QueryStat;
+
+/**
+ * JMX Bindings for {@link QueryStat}.
+ * 
+ */
+public interface QueryStatManagerMBean {
+
+    String NAME = "org.apache.jackrabbit:type=QueryStats";
+
+    TabularData getQueries();
+
+    /**
+     * @return how big the <b>Top X</b> queue is
+     */
+    int getQueueSize();
+
+    /**
+     * Change the <b>Top X</b> queue size
+     * 
+     * @param size
+     *            the new size
+     */
+    void setQueueSize(int size);
+
+    /**
+     * clears the queue
+     */
+    void clearQueue();
+}

Added: jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/CoreStat.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/CoreStat.java?rev=1181060&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/CoreStat.java
(added)
+++ jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/CoreStat.java
Mon Oct 10 16:35:29 2011
@@ -0,0 +1,66 @@
+/*
+ * 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.api.stats;
+
+/**
+ * Statistics on core JCR operations
+ * 
+ */
+public interface CoreStat {
+
+    /** -- SESSION INFO -- **/
+
+    void sessionCreated();
+
+    void sessionLoggedOut();
+
+    long getNumberOfSessions();
+
+    void resetNumberOfSessions();
+
+    /**
+     * @param timeNs
+     *            as given by timeNs = System.nanoTime() - timeNs;
+     */
+    void onSessionOperation(boolean isWrite, long timeNs);
+
+    double getReadOpsPerSecond();
+
+    double getWriteOpsPerSecond();
+
+    void resetNumberOfOperations();
+
+    /**
+     * If this service is currently registering stats
+     * 
+     * @return <code>true</code> if the service is enabled
+     */
+    boolean isEnabled();
+
+    /**
+     * Enables/Disables the service
+     * 
+     * @param enabled
+     */
+    void setEnabled(boolean enabled);
+
+    /**
+     * clears all data
+     */
+    void reset();
+
+}

Added: jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/QueryStat.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/QueryStat.java?rev=1181060&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/QueryStat.java
(added)
+++ jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/QueryStat.java
Mon Oct 10 16:35:29 2011
@@ -0,0 +1,74 @@
+/*
+ * 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.api.stats;
+
+/**
+ * Statistics on query operations
+ * 
+ */
+public interface QueryStat {
+
+    void logQuery(final String language, final String statement, long duration);
+
+    /** Slowest Queries */
+
+    QueryStatDto[] getSlowQueries();
+
+    /**
+     * @return how big the <b>Top X</b> queue is
+     */
+    int getSlowQueriesQueueSize();
+
+    /**
+     * Change the <b>Top X</b> queue size
+     * 
+     * @param size
+     *            the new size
+     */
+    void setSlowQueriesQueueSize(int size);
+
+    /**
+     * clears the queue
+     */
+    void clearSlowQueriesQueue();
+
+    double getQueriesPerSecond();
+
+    double getAvgQueryTime();
+
+    /** Generic Stats Stuff */
+
+    /**
+     * If this service is currently registering stats
+     * 
+     * @return <code>true</code> if the service is enabled
+     */
+    boolean isEnabled();
+
+    /**
+     * Enables/Disables the service
+     * 
+     * @param enabled
+     */
+    void setEnabled(boolean enabled);
+
+    /**
+     * clears all data
+     */
+    void reset();
+
+}

Added: jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/QueryStatDto.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/QueryStatDto.java?rev=1181060&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/QueryStatDto.java
(added)
+++ jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/QueryStatDto.java
Mon Oct 10 16:35:29 2011
@@ -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.api.stats;
+
+import java.io.Serializable;
+
+/**
+ * Object that holds statistical info about a query.
+ * 
+ */
+public interface QueryStatDto extends Serializable {
+
+    long getDuration();
+
+    String getLanguage();
+
+    String getStatement();
+
+    String getCreationTime();
+
+    long getPosition();
+
+    void setPosition(long position);
+
+}

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/CoreStatManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/CoreStatManager.java?rev=1181060&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/CoreStatManager.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/CoreStatManager.java
Mon Oct 10 16:35:29 2011
@@ -0,0 +1,70 @@
+/*
+ * 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.core.jmx;
+
+import org.apache.jackrabbit.api.jmx.CoreStatManagerMBean;
+import org.apache.jackrabbit.api.stats.CoreStat;
+
+/**
+ * The CoreStatManagerMBean default implementation
+ * 
+ */
+public class CoreStatManager implements CoreStatManagerMBean {
+
+    private final CoreStat coreStat;
+
+    public CoreStatManager(final CoreStat coreStat) {
+        this.coreStat = coreStat;
+    }
+
+    public long getNumberOfSessions() {
+        return coreStat.getNumberOfSessions();
+    }
+
+    public void resetNumberOfSessions() {
+        this.coreStat.resetNumberOfSessions();
+    }
+
+    public boolean isEnabled() {
+        return this.coreStat.isEnabled();
+    }
+
+    public void reset() {
+        this.coreStat.reset();
+    }
+
+    public double getReadOpsPerSecond() {
+        return this.coreStat.getReadOpsPerSecond();
+    }
+
+    public double getWriteOpsPerSecond() {
+        return this.coreStat.getWriteOpsPerSecond();
+    }
+
+    public void resetNumberOfOperations() {
+        this.coreStat.resetNumberOfOperations();
+
+    }
+
+    public void enable() {
+        this.coreStat.setEnabled(true);
+    }
+
+    public void disable() {
+        this.coreStat.setEnabled(false);
+    }
+}

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/QueryStatManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/QueryStatManager.java?rev=1181060&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/QueryStatManager.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/QueryStatManager.java
Mon Oct 10 16:35:29 2011
@@ -0,0 +1,134 @@
+/*
+ * 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.core.jmx;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+
+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.jackrabbit.api.jmx.QueryStatManagerMBean;
+import org.apache.jackrabbit.api.stats.QueryStat;
+import org.apache.jackrabbit.api.stats.QueryStatDto;
+import org.apache.jackrabbit.core.stats.QueryStatDtoComparator;
+
+/**
+ * The QueryStatManagerMBean default implementation
+ * 
+ */
+public class QueryStatManager implements QueryStatManagerMBean {
+
+    private final QueryStat queryStat;
+
+    private final static Comparator<QueryStatDto> comparatorRev = Collections
+            .reverseOrder(new QueryStatDtoComparator());
+
+    public QueryStatManager(final QueryStat queryStat) {
+        this.queryStat = queryStat;
+    }
+
+    public boolean isEnabled() {
+        return this.queryStat.isEnabled();
+    }
+
+    public void enable() {
+        this.queryStat.setEnabled(true);
+    }
+
+    public void disable() {
+        this.queryStat.setEnabled(false);
+    }
+
+    public void reset() {
+        this.queryStat.reset();
+    }
+
+    public int getQueueSize() {
+        return queryStat.getSlowQueriesQueueSize();
+    }
+
+    public void setQueueSize(int size) {
+        this.queryStat.setSlowQueriesQueueSize(size);
+    }
+
+    public void clearQueue() {
+        this.queryStat.clearSlowQueriesQueue();
+    }
+
+    public QueryStatDto[] getTopQueries() {
+        QueryStatDto[] top = this.queryStat.getSlowQueries();
+        Arrays.sort(top, comparatorRev);
+        for (int i = 0; i < top.length; i++) {
+            top[i].setPosition(i + 1);
+        }
+        return top;
+    }
+
+    public TabularData getQueries() {
+        TabularDataSupport tds = null;
+        try {
+            CompositeType ct = QueryStatCompositeTypeFactory.getCompositeType();
+
+            TabularType tt = new TabularType(QueryStatDto.class.getName(),
+                    "Query History", ct, QueryStatCompositeTypeFactory.index);
+            tds = new TabularDataSupport(tt);
+
+            for (QueryStatDto q : getTopQueries()) {
+                tds.put(new CompositeDataSupport(ct,
+                        QueryStatCompositeTypeFactory.names,
+                        QueryStatCompositeTypeFactory.getValues(q)));
+            }
+            return tds;
+        } catch (Exception e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+
+    private static class QueryStatCompositeTypeFactory {
+
+        private final static String[] index = { "position" };
+
+        private final static String[] names = { "position", "duration",
+                "language", "statement", "creationTime" };
+
+        private final static String[] descriptions = { "position", "duration",
+                "language", "statement", "creationTime" };
+
+        private final static OpenType[] types = { SimpleType.LONG,
+                SimpleType.LONG, SimpleType.STRING, SimpleType.STRING,
+                SimpleType.STRING };
+
+        public static CompositeType getCompositeType() throws OpenDataException {
+            return new CompositeType(QueryStat.class.getName(),
+                    QueryStat.class.getName(), names, descriptions, types);
+        }
+
+        public static Object[] getValues(QueryStatDto q) {
+            return new Object[] { q.getPosition(), q.getDuration(),
+                    q.getLanguage(), q.getStatement(), q.getCreationTime() };
+        }
+    }
+}

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/CoreStatImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/CoreStatImpl.java?rev=1181060&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/CoreStatImpl.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/CoreStatImpl.java
Mon Oct 10 16:35:29 2011
@@ -0,0 +1,103 @@
+/*
+ * 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.core.stats;
+
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.apache.jackrabbit.api.stats.CoreStat;
+import org.apache.jackrabbit.core.stats.util.CachingOpsPerSecondDto;
+
+/**
+ * Default CoreStat impl
+ * 
+ */
+public class CoreStatImpl implements CoreStat {
+
+    /** -- SESSION INFO -- **/
+
+    private final AtomicLong sessions = new AtomicLong(0);
+
+    private boolean enabled = false;
+
+    private final CachingOpsPerSecondDto reads = new CachingOpsPerSecondDto();
+
+    private final CachingOpsPerSecondDto writes = new CachingOpsPerSecondDto();
+
+    public void sessionCreated() {
+        if (!enabled) {
+            return;
+        }
+        sessions.incrementAndGet();
+    }
+
+    public void sessionLoggedOut() {
+        if (!enabled || sessions.get() == 0) {
+            return;
+        }
+        sessions.decrementAndGet();
+    }
+
+    public long getNumberOfSessions() {
+        return sessions.get();
+    }
+
+    public void resetNumberOfSessions() {
+        sessions.set(0);
+    }
+
+    public void onSessionOperation(boolean isWrite, long timeNs) {
+        if (!enabled) {
+            return;
+        }
+        if (isWrite) {
+            writes.onOp(timeNs);
+        } else {
+            reads.onOp(timeNs);
+        }
+    }
+
+    public double getReadOpsPerSecond() {
+        return reads.getOpsPerSecond();
+    }
+
+    public double getWriteOpsPerSecond() {
+        return writes.getOpsPerSecond();
+    }
+
+    public void resetNumberOfOperations() {
+        reads.reset();
+        writes.reset();
+    }
+
+    /** -- GENERAL INFO -- **/
+
+    public boolean isEnabled() {
+        return enabled;
+    }
+
+    public void setEnabled(boolean enabled) {
+        this.enabled = enabled;
+        if (!this.enabled) {
+            reset();
+        }
+    }
+
+    public void reset() {
+        resetNumberOfSessions();
+        resetNumberOfOperations();
+    }
+}

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoComparator.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoComparator.java?rev=1181060&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoComparator.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoComparator.java
Mon Oct 10 16:35:29 2011
@@ -0,0 +1,31 @@
+/*
+ * 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.core.stats;
+
+import java.util.Comparator;
+
+import org.apache.jackrabbit.api.stats.QueryStatDto;
+
+/**
+ * QueryStatDto comparator by duration
+ * 
+ */
+public class QueryStatDtoComparator implements Comparator<QueryStatDto> {
+    public int compare(QueryStatDto o1, QueryStatDto o2) {
+        return new Long(o1.getDuration()).compareTo(o2.getDuration());
+    }
+}

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoImpl.java?rev=1181060&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoImpl.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoImpl.java
Mon Oct 10 16:35:29 2011
@@ -0,0 +1,98 @@
+/*
+ * 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.core.stats;
+
+import java.util.Calendar;
+import java.util.Date;
+
+import org.apache.jackrabbit.api.stats.QueryStatDto;
+
+/**
+ * Object that holds statistical info about a query.
+ * 
+ */
+public class QueryStatDtoImpl implements QueryStatDto {
+
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * lazy, computed at call time
+     */
+    private long position;
+
+    /**
+     * the time that the query was created
+     */
+    private final Date creationTime;
+
+    /**
+     * run duration
+     */
+    private final long duration;
+
+    /**
+     * query language
+     */
+    private final String language;
+
+    /**
+     * query statement
+     */
+    private final String statement;
+
+    public QueryStatDtoImpl(final String language, final String statement,
+            long duration) {
+        this.duration = duration;
+        this.language = language;
+        this.statement = statement;
+
+        Calendar c = Calendar.getInstance();
+        c.setTimeInMillis(System.currentTimeMillis() - duration);
+        this.creationTime = c.getTime();
+    }
+
+    public long getDuration() {
+        return duration;
+    }
+
+    public String getLanguage() {
+        return language;
+    }
+
+    public String getStatement() {
+        return statement;
+    }
+
+    public String getCreationTime() {
+        return creationTime.toString();
+    }
+
+    public long getPosition() {
+        return position;
+    }
+
+    public void setPosition(long position) {
+        this.position = position;
+    }
+
+    @Override
+    public String toString() {
+        return "QueryStat [creationTime=" + creationTime + ", duration="
+                + duration + ", language=" + language + ", statement="
+                + statement + "]";
+    }
+}

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatImpl.java?rev=1181060&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatImpl.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatImpl.java
Mon Oct 10 16:35:29 2011
@@ -0,0 +1,97 @@
+/*
+ * 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.core.stats;
+
+import java.util.Comparator;
+import java.util.PriorityQueue;
+
+import org.apache.jackrabbit.api.stats.QueryStat;
+import org.apache.jackrabbit.api.stats.QueryStatDto;
+import org.apache.jackrabbit.core.stats.util.CachingOpsPerSecondDto;
+
+/**
+ * Default {@link QueryStat} implementation
+ * 
+ */
+public class QueryStatImpl implements QueryStat {
+
+    private final static Comparator<QueryStatDto> comparator = new QueryStatDtoComparator();
+
+    private int queueSize = 15;
+
+    private PriorityQueue<QueryStatDto> queries = new PriorityQueue<QueryStatDto>(
+            queueSize + 1, comparator);
+
+    private CachingOpsPerSecondDto qps = new CachingOpsPerSecondDto();
+
+    private boolean enabled = false;
+
+    public QueryStatImpl() {
+    }
+
+    public int getSlowQueriesQueueSize() {
+        return queueSize;
+    }
+
+    public synchronized void setSlowQueriesQueueSize(int size) {
+        this.queueSize = size;
+        this.queries = new PriorityQueue<QueryStatDto>(this.queueSize + 1,
+                comparator);
+    }
+
+    public boolean isEnabled() {
+        return enabled;
+    }
+
+    public void setEnabled(boolean enabled) {
+        this.enabled = enabled;
+        this.queries = new PriorityQueue<QueryStatDto>(this.queueSize + 1,
+                comparator);
+    }
+
+    public synchronized void logQuery(final String language,
+            final String statement, long duration) {
+        if (!enabled) {
+            return;
+        }
+        queries.add(new QueryStatDtoImpl(language, statement, duration));
+        if (queries.size() > queueSize) {
+            queries.remove();
+        }
+        qps.onOp(duration * 1000);
+    }
+
+    public void clearSlowQueriesQueue() {
+        this.queries.clear();
+    }
+
+    public void reset() {
+        clearSlowQueriesQueue();
+    }
+
+    public QueryStatDto[] getSlowQueries() {
+        return queries.toArray(new QueryStatDto[queries.size()]);
+    }
+
+    public double getQueriesPerSecond() {
+        return qps.getOpsPerSecond();
+    }
+
+    public double getAvgQueryTime() {
+        return qps.getOpAvgTime();
+    }
+}

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/StatManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/StatManager.java?rev=1181060&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/StatManager.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/StatManager.java
Mon Oct 10 16:35:29 2011
@@ -0,0 +1,58 @@
+/*
+ * 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.core.stats;
+
+import org.apache.jackrabbit.api.stats.CoreStat;
+import org.apache.jackrabbit.api.stats.QueryStat;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * StatManager represents a single entry point to the stats objects available.<br>
+ * 
+ */
+public class StatManager {
+
+    private static final Logger log = LoggerFactory
+            .getLogger(StatManager.class);
+
+    /* STATS */
+    private final QueryStat queryStat = new QueryStatImpl();
+
+    private final CoreStat coreStat = new CoreStatImpl();
+
+    public StatManager() {
+    }
+
+    public void init() {
+        coreStat.setEnabled(false);
+        queryStat.setEnabled(false);
+        log.debug("Started StatManager");
+    }
+
+    public void stop() {
+
+    }
+
+    public CoreStat getCoreStat() {
+        return coreStat;
+    }
+
+    public QueryStat getQueryStat() {
+        return queryStat;
+    }
+}

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/util/CachingOpsPerSecondDto.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/util/CachingOpsPerSecondDto.java?rev=1181060&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/util/CachingOpsPerSecondDto.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/util/CachingOpsPerSecondDto.java
Mon Oct 10 16:35:29 2011
@@ -0,0 +1,152 @@
+/*
+ * 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.core.stats.util;
+
+import java.math.BigDecimal;
+import java.math.MathContext;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+/**
+ * Statistical data structure, use to compute stats on operations
+ * 
+ */
+public class CachingOpsPerSecondDto {
+
+    // @ 1 min
+    public static long DEFAULT_UPDATE_FREQ_MS = 1000 * 60 * 1;
+
+    private final long updateFreqMs;
+
+    private final ReadWriteLock rwl = new ReentrantReadWriteLock();
+    private final Lock r = rwl.readLock();
+    private final Lock w = rwl.writeLock();
+
+    // intermediary values
+
+    private long lastUpdate = System.currentTimeMillis();
+
+    private long startMs = System.currentTimeMillis();
+
+    private long operations = 0;
+
+    private long totalTimeNs = 0;
+
+    // cached stats
+
+    private double opsPerSecond = 0;
+
+    private double opAvgTime = 0;
+
+    public CachingOpsPerSecondDto(long updateFreqMs) {
+        this.updateFreqMs = updateFreqMs;
+    }
+
+    public CachingOpsPerSecondDto() {
+        this(DEFAULT_UPDATE_FREQ_MS);
+    }
+
+    public void onOp(long timeNs) {
+        w.lock();
+        try {
+            final long localStart = System.currentTimeMillis() - timeNs / 1000;
+            if (localStart < startMs) {
+                startMs = localStart;
+            }
+            operations++;
+            totalTimeNs += timeNs;
+        } finally {
+            w.unlock();
+        }
+    }
+
+    public double getOpsPerSecond() {
+        checkUpdate(false);
+        return opsPerSecond;
+    }
+
+    public double getOpAvgTime() {
+        checkUpdate(false);
+        return opAvgTime;
+    }
+
+    private void checkUpdate(boolean forceUpdate) {
+        r.lock();
+        final long now = System.currentTimeMillis();
+        try {
+            if (!forceUpdate && now - lastUpdate < updateFreqMs) {
+                return;
+            }
+        } finally {
+            r.unlock();
+        }
+        w.lock();
+        try {
+            if (!forceUpdate && now - lastUpdate < updateFreqMs) {
+                return;
+            }
+            update(now);
+        } finally {
+            w.unlock();
+        }
+    }
+
+    private final static BigDecimal thousand = BigDecimal.valueOf(1000);
+
+    private final static MathContext DEFAULT_CONTEXT = new MathContext(3);
+
+    private void update(long now) {
+        if (operations == 0) {
+            opsPerSecond = 0;
+            opAvgTime = 0;
+            return;
+        }
+        long durationMs = now - startMs;
+        if (durationMs == 0) {
+            durationMs = 1000;
+        }
+        opsPerSecond = BigDecimal.valueOf(operations).multiply(thousand)
+                .divide(BigDecimal.valueOf(durationMs), DEFAULT_CONTEXT)
+                .doubleValue();
+        opAvgTime = BigDecimal.valueOf(totalTimeNs)
+                .divide(BigDecimal.valueOf(operations), DEFAULT_CONTEXT)
+                .doubleValue();
+        // reset if needed
+        if (operations > Long.MAX_VALUE - 5000) {
+            reset();
+        }
+    }
+
+    public void reset() {
+        w.lock();
+        try {
+            opsPerSecond = 0;
+            opAvgTime = 0;
+            lastUpdate = System.currentTimeMillis();
+            operations = 0;
+            startMs = lastUpdate;
+            totalTimeNs = 0;
+        } finally {
+            w.unlock();
+        }
+    }
+
+    protected void refresh() {
+        checkUpdate(true);
+    }
+}



Mime
View raw message