jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From alexparvule...@apache.org
Subject svn commit: r1153868 - in /jackrabbit/trunk/jackrabbit-core/src: main/java/org/apache/jackrabbit/core/ main/java/org/apache/jackrabbit/core/jmx/ main/java/org/apache/jackrabbit/core/jmx/core/ main/java/org/apache/jackrabbit/core/jmx/query/ main/java/or...
Date Thu, 04 Aug 2011 12:42:01 GMT
Author: alexparvulescu
Date: Thu Aug  4 12:41:59 2011
New Revision: 1153868

URL: http://svn.apache.org/viewvc?rev=1153868&view=rev
Log:
JCR-3040 JMX Stats for the Session

Added:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/core/
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatDto.java
  (with props)
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatDtoComparator.java
  (with props)
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatImpl.java
  (with props)
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatManagerMBean.java
  (with props)
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/util/
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/util/CachingOpsPerSecondDto.java
  (with props)
    jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/AbstractJmxTest.java
  (with props)
    jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/core/
    jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/query/
    jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/util/
Removed:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/JmxRegistryUtils.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatManagerBase.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatManagerImpl.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatManagerImplMBean.java
    jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/QueryStatManagerTest.java
Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryContext.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryImpl.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/JackrabbitBaseMBean.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/JmxRegistry.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/JmxRegistryImpl.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStat.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatManager.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryImpl.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryObjectModelImpl.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/session/SessionState.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryContext.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryContext.java?rev=1153868&r1=1153867&r2=1153868&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryContext.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryContext.java
Thu Aug  4 12:41:59 2011
@@ -21,6 +21,7 @@ import org.apache.jackrabbit.core.data.D
 import org.apache.jackrabbit.core.fs.FileSystem;
 import org.apache.jackrabbit.core.id.NodeId;
 import org.apache.jackrabbit.core.id.NodeIdFactory;
+import org.apache.jackrabbit.core.jmx.JmxRegistry;
 import org.apache.jackrabbit.core.nodetype.NodeTypeRegistry;
 import org.apache.jackrabbit.core.security.JackrabbitSecurityManager;
 import org.apache.jackrabbit.core.security.authorization.PrivilegeRegistry;
@@ -103,6 +104,8 @@ public class RepositoryContext {
      * Repository-wide timer instance.
      */
     private final Timer timer = new Timer(false);
+    
+    private JmxRegistry jmxRegistry;
 
     /**
      * Creates a component context for the given repository.
@@ -359,4 +362,13 @@ public class RepositoryContext {
         return nodeIdFactory;
     }
 
+    public JmxRegistry getJmxRegistry() {
+        assert jmxRegistry != null;
+        return jmxRegistry;
+    }
+
+    public void setJmxRegistry(JmxRegistry jmxRegistry) {
+        assert jmxRegistry != null;
+        this.jmxRegistry = jmxRegistry;
+    }
 }

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryImpl.java?rev=1153868&r1=1153867&r2=1153868&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/RepositoryImpl.java
Thu Aug  4 12:41:59 2011
@@ -59,6 +59,7 @@ import org.apache.jackrabbit.api.Jackrab
 import org.apache.jackrabbit.api.management.RepositoryManager;
 import org.apache.jackrabbit.api.security.authentication.token.TokenCredentials;
 import org.apache.jackrabbit.commons.AbstractRepository;
+import org.apache.jackrabbit.core.cache.CacheManager;
 import org.apache.jackrabbit.core.cluster.ClusterContext;
 import org.apache.jackrabbit.core.cluster.ClusterException;
 import org.apache.jackrabbit.core.cluster.ClusterNode;
@@ -98,10 +99,9 @@ import org.apache.jackrabbit.core.retent
 import org.apache.jackrabbit.core.retention.RetentionRegistryImpl;
 import org.apache.jackrabbit.core.security.JackrabbitSecurityManager;
 import org.apache.jackrabbit.core.security.authentication.AuthContext;
-import org.apache.jackrabbit.core.security.authorization.PrivilegeRegistry;
 import org.apache.jackrabbit.core.security.authentication.token.TokenBasedAuthentication;
+import org.apache.jackrabbit.core.security.authorization.PrivilegeRegistry;
 import org.apache.jackrabbit.core.security.simple.SimpleSecurityManager;
-import org.apache.jackrabbit.core.cache.CacheManager;
 import org.apache.jackrabbit.core.state.ChangeLog;
 import org.apache.jackrabbit.core.state.ISMLocking;
 import org.apache.jackrabbit.core.state.ItemStateException;
@@ -468,7 +468,7 @@ public class RepositoryImpl extends Abst
         return cacheMgr;
     }
 
-    public JmxRegistry getJmxRegistry(){
+    public JmxRegistry getJmxRegistry() {
         return jmxRegistry;
     }
 
@@ -1013,6 +1013,7 @@ public class RepositoryImpl extends Abst
         synchronized (activeSessions) {
             session.addListener(this);
             activeSessions.put(session, session);
+            jmxRegistry.getCoreStat().sessionCreated();
         }
     }
 
@@ -1467,6 +1468,7 @@ public class RepositoryImpl extends Abst
 
     protected void initJmxRegistry(){
         this.jmxRegistry.start();
+        this.context.setJmxRegistry(jmxRegistry);
     }
 
     //-----------------------------------------------------------< Repository >
@@ -1603,6 +1605,7 @@ public class RepositoryImpl extends Abst
         synchronized (activeSessions) {
             // remove session from active sessions
             activeSessions.remove(session);
+            jmxRegistry.getCoreStat().sessionLoggedOut();
         }
     }
 

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/JackrabbitBaseMBean.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/JackrabbitBaseMBean.java?rev=1153868&r1=1153867&r2=1153868&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/JackrabbitBaseMBean.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/JackrabbitBaseMBean.java
Thu Aug  4 12:41:59 2011
@@ -27,8 +27,23 @@ public interface JackrabbitBaseMBean {
 
     String BASE_NAME = "org.apache.jackrabbit";
 
+    /**
+     * 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();
+
 }

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/JmxRegistry.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/JmxRegistry.java?rev=1153868&r1=1153867&r2=1153868&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/JmxRegistry.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/JmxRegistry.java
Thu Aug  4 12:41:59 2011
@@ -18,7 +18,8 @@ package org.apache.jackrabbit.core.jmx;
 
 import javax.management.ObjectName;
 
-import org.apache.jackrabbit.core.jmx.query.QueryStatManager;
+import org.apache.jackrabbit.core.jmx.core.CoreStat;
+import org.apache.jackrabbit.core.jmx.query.QueryStat;
 
 /**
  * JMX Mbean registration service
@@ -54,5 +55,8 @@ public interface JmxRegistry {
      */
     void unregister(ObjectName name) throws Exception;
 
-    QueryStatManager getQueryStatManager();
+    CoreStat getCoreStat();
+
+    QueryStat getQueryStat();
+
 }

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/JmxRegistryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/JmxRegistryImpl.java?rev=1153868&r1=1153867&r2=1153868&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/JmxRegistryImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/JmxRegistryImpl.java
Thu Aug  4 12:41:59 2011
@@ -23,9 +23,14 @@ import java.util.List;
 import javax.management.MBeanServer;
 import javax.management.ObjectName;
 
+import org.apache.jackrabbit.core.jmx.core.CoreStat;
+import org.apache.jackrabbit.core.jmx.core.CoreStatImpl;
+import org.apache.jackrabbit.core.jmx.core.CoreStatManager;
+import org.apache.jackrabbit.core.jmx.core.CoreStatManagerMBean;
+import org.apache.jackrabbit.core.jmx.query.QueryStat;
+import org.apache.jackrabbit.core.jmx.query.QueryStatImpl;
 import org.apache.jackrabbit.core.jmx.query.QueryStatManager;
-import org.apache.jackrabbit.core.jmx.query.QueryStatManagerImpl;
-import org.apache.jackrabbit.core.jmx.query.QueryStatManagerImplMBean;
+import org.apache.jackrabbit.core.jmx.query.QueryStatManagerMBean;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -41,7 +46,9 @@ public class JmxRegistryImpl implements 
 
     private List<ObjectName> registry = new ArrayList<ObjectName>();
 
-    private QueryStatManagerImpl queryStatManager;
+    private QueryStat queryStat;
+
+    private CoreStat coreStat;
 
     public JmxRegistryImpl() {
     }
@@ -53,14 +60,26 @@ public class JmxRegistryImpl implements 
      */
     public void start() {
         server = ManagementFactory.getPlatformMBeanServer();
+        log.debug("Started JMX Registry.");
+        try {
+            coreStat = new CoreStatImpl();
+            register(new CoreStatManager(coreStat), new ObjectName(
+                    CoreStatManagerMBean.NAME));
+            log.debug("JMX Registry - registered CoreStats.");
+        } catch (Exception e) {
+            log.error("JMX Registry - Unable to register CoreStats.", e);
+            coreStat.setEnabled(false);
+        }
+
         try {
-            queryStatManager = new QueryStatManagerImpl();
-            register(queryStatManager, new ObjectName(
-                    QueryStatManagerImplMBean.NAME));
-            log.info("Started JMX Registry");
+            queryStat = new QueryStatImpl();
+            register(new QueryStatManager(queryStat), new ObjectName(
+                    QueryStatManagerMBean.NAME));
+            log.debug("JMX Registry - registered QueryStats.");
+
         } catch (Exception e) {
-            queryStatManager = null;
-            log.error("Unable to start JMX Registry", e);
+            log.error("JMX Registry - Unable to register QueryStats.", e);
+            queryStat.setEnabled(false);
         }
     }
 
@@ -74,9 +93,10 @@ public class JmxRegistryImpl implements 
             return;
         }
 
-        for (ObjectName o : registry) {
+        List<ObjectName> registryCopy = new ArrayList<ObjectName>(registry);
+        for (ObjectName o : registryCopy) {
             try {
-                server.unregisterMBean(o);
+                unregister(o);
             } catch (Exception e) {
                 e.printStackTrace();
             }
@@ -94,6 +114,9 @@ public class JmxRegistryImpl implements 
      */
     public void register(JackrabbitBaseMBean bean, ObjectName name)
             throws Exception {
+        if (server == null || server.isRegistered(name)) {
+            return;
+        }
         this.server.registerMBean(bean, name);
         this.registry.add(name);
     }
@@ -113,7 +136,22 @@ public class JmxRegistryImpl implements 
         server.unregisterMBean(name);
     }
 
-    public QueryStatManager getQueryStatManager() {
-        return queryStatManager;
+    /*
+     * (non-Javadoc)
+     * 
+     * @see org.apache.jackrabbit.core.jmx.JmxRegistry#getCoreStat()
+     */
+    public CoreStat getCoreStat() {
+        return coreStat;
     }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see org.apache.jackrabbit.core.jmx.JmxRegistry#getQueryStatManager()
+     */
+    public QueryStat getQueryStat() {
+        return queryStat;
+    }
+
 }

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStat.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStat.java?rev=1153868&r1=1153867&r2=1153868&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStat.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStat.java
Thu Aug  4 12:41:59 2011
@@ -16,82 +16,55 @@
  */
 package org.apache.jackrabbit.core.jmx.query;
 
-import java.io.Serializable;
-import java.util.Calendar;
-import java.util.Date;
-
-/**
- * Object that holds statistical info about a query.
- * 
- */
-public class QueryStat implements Serializable {
+public interface QueryStat {
 
-    private static final long serialVersionUID = 1L;
+    void logQuery(final String language, final String statement, long duration);
 
-    /**
-     * lazy, computed at call time
-     */
-    private long position;
+    /** Slowest Queries */
 
-    /**
-     * the time that the query was created
-     */
-    private final Date creationTime;
+    QueryStatDto[] getSlowQueries();
 
     /**
-     * run duration
+     * @return how big the <b>Top X</b> queue is
      */
-    private final long duration;
+    int getSlowQueriesQueueSize();
 
     /**
-     * query language
+     * Change the <b>Top X</b> queue size
+     * 
+     * @param size
+     *            the new size
      */
-    private final String language;
+    void setSlowQueriesQueueSize(int size);
 
     /**
-     * query statement
+     * clears the queue
      */
-    private final String statement;
-
-    public QueryStat(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();
-    }
+    void clearSlowQueriesQueue();
 
-    public long getDuration() {
-        return duration;
-    }
+    double getQueriesPerSecond();
 
-    public String getLanguage() {
-        return language;
-    }
+    double getAvgQueryTime();
 
-    public String getStatement() {
-        return statement;
-    }
+    /** Generic Stats Stuff */
 
-    public String getCreationTime() {
-        return creationTime.toString();
-    }
+    /**
+     * If this service is currently registering stats
+     * 
+     * @return <code>true</code> if the service is enabled
+     */
+    boolean isEnabled();
 
-    public long getPosition() {
-        return position;
-    }
+    /**
+     * Enables/Disables the service
+     * 
+     * @param enabled
+     */
+    void setEnabled(boolean enabled);
 
-    public void setPosition(long position) {
-        this.position = position;
-    }
+    /**
+     * clears all data
+     */
+    void reset();
 
-    @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/jmx/query/QueryStatDto.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatDto.java?rev=1153868&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatDto.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatDto.java
Thu Aug  4 12:41:59 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.jmx.query;
+
+import java.io.Serializable;
+import java.util.Calendar;
+import java.util.Date;
+
+/**
+ * Object that holds statistical info about a query.
+ * 
+ */
+public class QueryStatDto implements Serializable {
+
+    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 QueryStatDto(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 + "]";
+    }
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatDto.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatDtoComparator.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatDtoComparator.java?rev=1153868&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatDtoComparator.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatDtoComparator.java
Thu Aug  4 12:41:59 2011
@@ -0,0 +1,25 @@
+/*
+ * 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.query;
+
+import java.util.Comparator;
+
+public class QueryStatDtoComparator implements Comparator<QueryStatDto> {
+    public int compare(QueryStatDto o1, QueryStatDto o2) {
+        return new Long(o1.getDuration()).compareTo(o2.getDuration());
+    }
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatDtoComparator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatImpl.java?rev=1153868&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatImpl.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatImpl.java
Thu Aug  4 12:41:59 2011
@@ -0,0 +1,95 @@
+/*
+ * 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.query;
+
+import java.util.Comparator;
+import java.util.PriorityQueue;
+
+import org.apache.jackrabbit.core.jmx.util.CachingOpsPerSecondDto;
+
+/**
+ * Default {@link QueryStatManager} 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 QueryStatDto(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();
+    }
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatImpl.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatManager.java?rev=1153868&r1=1153867&r2=1153868&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatManager.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatManager.java
Thu Aug  4 12:41:59 2011
@@ -16,20 +16,106 @@
  */
 package org.apache.jackrabbit.core.jmx.query;
 
-/**
- * 
- * Extends QueryStatManagerBase with write access to the query log. <br>
- * 
- */
-public interface QueryStatManager extends QueryStatManagerBase {
+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;
+
+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 queryStat.isEnabled();
+    }
+
+    public void setEnabled(boolean enabled) {
+        queryStat.setEnabled(enabled);
+    }
+
+    public void reset() {
+        queryStat.reset();
+    }
+
+    public int getQueueSize() {
+        return queryStat.getSlowQueriesQueueSize();
+    }
+
+    public void setQueueSize(int size) {
+        queryStat.setSlowQueriesQueueSize(size);
+    }
+
+    public void clearQueue() {
+        queryStat.clearSlowQueriesQueue();
+    }
+
+    public QueryStatDto[] getTopQueries() {
+        QueryStatDto[] top = 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" };
 
-    /**
-     * log a {@link QueryStat} to the service if it is enabled
-     * 
-     * @param query
-     */
-    void logQuery(QueryStat query);
+        private final static OpenType<?>[] types = { SimpleType.LONG,
+                SimpleType.LONG, SimpleType.STRING, SimpleType.STRING,
+                SimpleType.STRING };
 
-    QueryStat[] getTopQueries();
+        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/jmx/query/QueryStatManagerMBean.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatManagerMBean.java?rev=1153868&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatManagerMBean.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatManagerMBean.java
Thu Aug  4 12:41:59 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.core.jmx.query;
+
+import javax.management.openmbean.TabularData;
+
+import org.apache.jackrabbit.core.jmx.JackrabbitBaseMBean;
+
+/**
+ * JMX Binding for the {@link QueryStatManagerImpl}. <br>
+ * 
+ */
+public interface QueryStatManagerMBean extends JackrabbitBaseMBean {
+
+    String NAME = BASE_NAME + ":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();
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/query/QueryStatManagerMBean.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/util/CachingOpsPerSecondDto.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/util/CachingOpsPerSecondDto.java?rev=1153868&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/util/CachingOpsPerSecondDto.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/util/CachingOpsPerSecondDto.java
Thu Aug  4 12:41:59 2011
@@ -0,0 +1,132 @@
+package org.apache.jackrabbit.core.jmx.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;
+
+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);
+    }
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/util/CachingOpsPerSecondDto.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryImpl.java?rev=1153868&r1=1153867&r2=1153868&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryImpl.java
Thu Aug  4 12:41:59 2011
@@ -35,8 +35,6 @@ import javax.jcr.query.InvalidQueryExcep
 import javax.jcr.query.QueryResult;
 import javax.jcr.version.VersionException;
 
-import org.apache.jackrabbit.core.jmx.JmxRegistryUtils;
-import org.apache.jackrabbit.core.jmx.query.QueryStatManager;
 import org.apache.jackrabbit.core.session.SessionContext;
 import org.apache.jackrabbit.core.session.SessionOperation;
 import org.apache.jackrabbit.spi.Path;
@@ -133,16 +131,15 @@ public class QueryImpl extends AbstractQ
                             throws RepositoryException {
                         return query.execute(offset, limit);
                     }
+
                     public String toString() {
                         return "query.execute(" + statement + ")";
                     }
                 });
         time = System.currentTimeMillis() - time;
-        QueryStatManager qsm = sessionContext.getRepository().getJmxRegistry()
-                .getQueryStatManager();
-        if (qsm != null) {
-            qsm.logQuery(JmxRegistryUtils.buildQueryStat(this, time));
-        }
+        sessionContext.getRepositoryContext().getJmxRegistry().getQueryStat()
+                .logQuery(language, statement, time);
+
         if (log.isDebugEnabled()) {
             NumberFormat format = NumberFormat.getNumberInstance();
             format.setMinimumFractionDigits(2);

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryObjectModelImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryObjectModelImpl.java?rev=1153868&r1=1153867&r2=1153868&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryObjectModelImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryObjectModelImpl.java
Thu Aug  4 12:41:59 2011
@@ -32,8 +32,6 @@ import javax.jcr.query.qom.QueryObjectMo
 import javax.jcr.query.qom.Source;
 
 import org.apache.jackrabbit.commons.query.QueryObjectModelBuilderRegistry;
-import org.apache.jackrabbit.core.jmx.JmxRegistryUtils;
-import org.apache.jackrabbit.core.jmx.query.QueryStatManager;
 import org.apache.jackrabbit.core.query.lucene.LuceneQueryFactory;
 import org.apache.jackrabbit.core.query.lucene.SearchIndex;
 import org.apache.jackrabbit.core.query.lucene.join.QueryEngine;
@@ -126,11 +124,9 @@ public class QueryObjectModelImpl extend
                 getConstraint(), getOrderings(), offset, limit);
         time = System.currentTimeMillis() - time;
 
-        QueryStatManager qsm = sessionContext.getRepository().getJmxRegistry()
-        .getQueryStatManager();
-        if (qsm != null) {
-            qsm.logQuery(JmxRegistryUtils.buildQueryStat(this, time));
-        }
+        sessionContext.getRepositoryContext().getJmxRegistry().getQueryStat()
+                .logQuery(language, statement, time);
+        
         if (log.isDebugEnabled()) {
             NumberFormat format = NumberFormat.getNumberInstance();
             format.setMinimumFractionDigits(2);

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/session/SessionState.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/session/SessionState.java?rev=1153868&r1=1153867&r2=1153868&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/session/SessionState.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/session/SessionState.java
Thu Aug  4 12:41:59 2011
@@ -23,6 +23,7 @@ import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
 import org.apache.jackrabbit.core.WorkspaceManager;
+import org.apache.jackrabbit.core.jmx.core.CoreStat;
 import org.apache.jackrabbit.core.observation.ObservationDispatcher;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -180,20 +181,22 @@ public class SessionState {
             }
 
             try {
+                final CoreStat coreStat = context.getRepositoryContext().getJmxRegistry().getCoreStat();
                 // Perform the actual operation, optionally with debug logs
-                if (log.isDebugEnabled()) {
-                    log.debug("Performing {}", operation);
+                if (log.isDebugEnabled() || coreStat.isEnabled()) {
                     long start = System.nanoTime();
+                    log.debug("Performing {}", operation);
                     try {
                         return operation.perform(context);
                     } finally {
                         long time = System.nanoTime() - start;
+                        coreStat.onSessionOperation(isWriteOperation, time);
                         if (time > NS_PER_MS) {
-                            log.debug("Performed {} in {}ms",
-                                    operation, time / NS_PER_MS);
+                            log.debug("Performed {} in {}ms", operation, time
+                                    / NS_PER_MS);
                         } else {
-                            log.debug("Performed {} in {}us",
-                                    operation, time / NS_PER_US);
+                            log.debug("Performed {} in {}us", operation, time
+                                    / NS_PER_US);
                         }
                     }
                 } else {

Added: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/AbstractJmxTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/AbstractJmxTest.java?rev=1153868&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/AbstractJmxTest.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/AbstractJmxTest.java
Thu Aug  4 12:41:59 2011
@@ -0,0 +1,35 @@
+/*
+ * 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.core.RepositoryImpl;
+import org.apache.jackrabbit.test.AbstractJCRTest;
+
+/**
+ * Performs various JMX test cases.
+ */
+public abstract class AbstractJmxTest extends AbstractJCRTest {
+
+    protected JmxRegistry jmxRegistry;
+
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        RepositoryImpl r = (RepositoryImpl) superuser.getRepository();
+        jmxRegistry = r.getJmxRegistry();
+    }
+}
\ No newline at end of file

Propchange: jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/jmx/AbstractJmxTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message