You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ak...@apache.org on 2007/10/26 04:35:28 UTC

svn commit: r588476 - in /geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor: MasterRemoteControl.java MasterRemoteControlRemote.java snapshot/SnapshotDBHelper.java snapshot/SnapshotThread.java

Author: akulshreshtha
Date: Thu Oct 25 19:35:28 2007
New Revision: 588476

URL: http://svn.apache.org/viewvc?rev=588476&view=rev
Log:
GERONIMO-3558 Monitoring Plugin : Move old statistics to archive Database, Patch by Viet H. Nguyen

Modified:
    geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/MasterRemoteControl.java
    geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/MasterRemoteControlRemote.java
    geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/snapshot/SnapshotDBHelper.java
    geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/snapshot/SnapshotThread.java

Modified: geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/MasterRemoteControl.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/MasterRemoteControl.java?rev=588476&r1=588475&r2=588476&view=diff
==============================================================================
--- geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/MasterRemoteControl.java (original)
+++ geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/MasterRemoteControl.java Thu Oct 25 19:35:28 2007
@@ -27,6 +27,7 @@
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.Properties;
 import java.util.Set;
 import java.util.TreeMap;
@@ -130,16 +131,28 @@
         mbeanHelper = new MBeanHelper();
 
         // set up databases
-        setUpDatabase();
+        setUpDatabases();
         
         // set up SnaphotDBHelper with the necessary data sources
         SnapshotDBHelper.setDataSources(activeDS, archiveDS);
     }
 
-    private void setUpDatabase() {
+    /**
+     * Sets up the ActiveDB and ArchiveDB
+     */
+    private void setUpDatabases() {
+        setUpDatabase(activeDS);
+        setUpDatabase(archiveDS);
+    }
+    
+    /**
+     * Sets up a DB using a given DS
+     * @param ds
+     */
+    private void setUpDatabase(DataSource ds) {
         Connection conn = null;
         try {
-            conn = activeDS.getConnection();
+            conn = ds.getConnection();
             Statement stmt = conn.createStatement();
             try {
                 // fetch all tables in the db
@@ -230,8 +243,8 @@
      * @throws Exception
      */
     @RolesAllowed("mejbuser")
-    public static HashMap getStats(String objectName) throws Exception {
-        HashMap statsMap = new HashMap();
+    public static HashMap<String, Long> getStats(String objectName) throws Exception {
+        HashMap<String, Long> statsMap = new HashMap<String, Long>();
         Stats stats = (Stats)mejb.getAttribute(new ObjectName(objectName), "stats");
         String[] sttsName = stats.getStatisticNames();
         Statistic[] stts = stats.getStatistics();
@@ -527,7 +540,40 @@
      */
     @RolesAllowed("mejbuser")
     public HashMap<String, ArrayList<String>> getAllSnapshotStatAttributes() {
-        return org.apache.geronimo.monitor.snapshot.SnapshotDBHelper.getAllSnapshotStatAttributes();
+        HashMap<String, ArrayList<String>> snapshotAttributes = new HashMap<String, ArrayList<String>>();
+        Set<String> mbeans = getTrackedMBeans();
+        // for each mbean name
+        for(Iterator<String> it = mbeans.iterator(); it.hasNext(); ) {
+            ArrayList<String> mbeanStatsList = new ArrayList<String>();
+            String mbeanName = it.next();
+            try {
+                Stats stats = (Stats)mejb.getAttribute(new ObjectName(mbeanName), "stats");
+                String[] sttsName = stats.getStatisticNames();
+                Statistic[] stts = stats.getStatistics();
+                for(int i = 0; i < sttsName.length; i++) {
+                    Statistic aStat = stats.getStatistic(sttsName[i]);
+                    if(aStat instanceof RangeStatistic) {
+                        mbeanStatsList.add(stts[i].getName() + " Current");
+                        mbeanStatsList.add(stts[i].getName() + " Max");
+                        mbeanStatsList.add(stts[i].getName() + " Min");
+                    } else if(aStat instanceof CountStatistic) {
+                        mbeanStatsList.add(stts[i].getName());
+                    } else if(aStat instanceof TimeStatistic) {
+                        mbeanStatsList.add(stts[i].getName() + " CurrentTime");
+                        mbeanStatsList.add(stts[i].getName() + " MaxTime");
+                        mbeanStatsList.add(stts[i].getName() + " MinTime");
+                        mbeanStatsList.add(stts[i].getName() + " TotalTime");
+                    } else {
+                        // for the time being, only numbers should be returned
+                    }
+                }
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+            // save attributes to the returning list
+            snapshotAttributes.put(mbeanName, mbeanStatsList);
+        }
+        return snapshotAttributes;
     }
 
     /**
@@ -571,7 +617,7 @@
      * @return A set of all mbeans being tracked from the db
      */
     @RolesAllowed("mejbuser")
-    public Set getTrackedMBeans() {
+    public Set<String> getTrackedMBeans() {
         ArrayList<String> mbeans = (ArrayList<String>)SnapshotConfigXMLBuilder.getMBeanNames();
         Set<String> set = new HashSet<String>();
         for(int i = 0; i < mbeans.size(); i++) {

Modified: geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/MasterRemoteControlRemote.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/MasterRemoteControlRemote.java?rev=588476&r1=588475&r2=588476&view=diff
==============================================================================
--- geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/MasterRemoteControlRemote.java (original)
+++ geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/MasterRemoteControlRemote.java Thu Oct 25 19:35:28 2007
@@ -71,5 +71,5 @@
     @RolesAllowed("mejbuser")
     public TreeMap<Long, Long> getSpecificStatistics(String mbeanName, String statsName, int numberOfSnapshots, int everyNthSnapshot);
     @RolesAllowed("mejbuser")
-    public Set getTrackedMBeans();
+    public Set<String> getTrackedMBeans();
 }

Modified: geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/snapshot/SnapshotDBHelper.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/snapshot/SnapshotDBHelper.java?rev=588476&r1=588475&r2=588476&view=diff
==============================================================================
--- geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/snapshot/SnapshotDBHelper.java (original)
+++ geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/snapshot/SnapshotDBHelper.java Thu Oct 25 19:35:28 2007
@@ -174,23 +174,21 @@
      * @return Returns a boolean if the snapshot statistics were successfully added
      * to the DB.
      */
-    public static boolean addSnapshotToDB(HashMap<String, HashMap> aggregateStats) {
+    public static boolean addSnapshotToDB(HashMap<String, HashMap<String, Long>> aggregateStats) {
         boolean success = true;
+        // get the current date
+        Calendar cal = Calendar.getInstance();
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        String currDate = sdf.format(cal.getTime());
+        
+        // get the current time from 1970
+        String currTime = "";
+        currTime += (new Date()).getTime();
         try {
-            openActiveConnection();
-            Statement stmt = conn.createStatement();
-            
-            // get the current date
-            Calendar cal = Calendar.getInstance();
-            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-            String currDate = sdf.format(cal.getTime());
-            
-            // get the current time from 1970
-            String currTime = "";
-            currTime += (new Date()).getTime();
-            
             // for each mbean
             for(Iterator itt = aggregateStats.keySet().iterator(); itt.hasNext(); ) {
+                openActiveConnection();
+                Statement stmt = conn.createStatement();
                 String mbean = (String)itt.next();
                 HashMap stats = aggregateStats.get(mbean);
                 System.out.println(mbean);
@@ -221,6 +219,7 @@
                     //--------Ensure Statistics are in place
                     stmt.executeUpdate( prepareInsertStatement(key, value, snapshotId, mbeanId) );
                 }
+                closeConnection();
             }
         } catch(Exception  e){
             e.printStackTrace();
@@ -229,37 +228,194 @@
             closeConnection();
         }
         
-        
+        // go through the archiving process
+        try {
+            int retentionDays = Integer.parseInt(SnapshotConfigXMLBuilder.getAttributeValue("retention"));
+            long retentionMillis = (long)(retentionDays) * 86400000; // convert from days to milliseconds
+            archiveSnapshots( Long.parseLong(currTime) - retentionMillis );
+        } catch(Exception e) {
+            System.out.println("[WARN] Cannot archive snapshots because attribute 'retention' is not present in snapshot-config.xml.");
+        }
         
         return success;
     }
     
     /**
+     * Moves records from the ActiveDB to the ArchiveDB. The records that are moved
+     * are those whose snapshot_times exceed the retention period
+     * @param cutOffTime - in milliseconds
+     */
+    private static void archiveSnapshots(long cutOffTime) {
+        // for each successful update of Snapshots/Statistics table
+        // increment or decrement these counters to ensure that nothing is being 
+        // lost in between. If these counters are non-zero, some records have been
+        // lost.
+        int snapshotsOver = 0;
+        int statisticsOver = 0;
+        try {
+            openActiveConnection();
+            ResultSet overDueSnapshotTimes = getOverDueSnapshotTimes(cutOffTime);
+            ArrayList<Long> overdueTimes = new ArrayList<Long>();
+            // save overdue times into an array list for later usage
+            while(overDueSnapshotTimes.next()) {
+                overdueTimes.add(overDueSnapshotTimes.getLong(SNAPSHOT_TIME));
+            }
+            closeConnection();
+            // for each overdue snapshot time
+            // -transfer all records associated with that snaphot_time to ArchiveDB
+            for(int i = 0; i < overdueTimes.size(); i++) {
+                long snapshotTime = overdueTimes.get(i);
+                openActiveConnection();
+                ResultSet rsSnapshotData = fetchSnapshotDataFromDB(new Long(snapshotTime));
+                HashMap<String, HashMap<String, Long>> snapshotData = new HashMap<String,HashMap<String, Long>>();
+                while(rsSnapshotData.next()) {
+                    // extract values from sql table
+                    String mbeanName = rsSnapshotData.getString(MBEANNAME);
+                    String statsName = rsSnapshotData.getString(STATSNAME);
+                    Long statsValue = rsSnapshotData.getLong(STATSVALUE);
+                    Long snapshot_time = rsSnapshotData.getLong(SNAPSHOT_TIME);
+                    String snapshot_date = rsSnapshotData.getString(SNAPSHOT_DATE);
+                    // get a connection to the archive db too
+                    Connection archiveConn = archiveDS.getConnection();
+                    Statement archiveStmt = archiveConn.createStatement();
+                    //--------Ensure MBeans are in place
+                    int mbeanId = getMBeanIdFromArchive(mbeanName); 
+                    if(mbeanId != -1) {
+                        // mbean already exists in the db
+                    } else {
+                        // doesn't exist in the db so add it
+                        // add mbean record to the db
+                        archiveStmt.executeUpdate("INSERT INTO MBeans (mbeanName) VALUES ("+ surroundWithQuotes(mbeanName) + ")");
+                        mbeanId = getMBeanIdFromArchive(mbeanName);
+                    }
+                    //--------Ensure Snapshots are in place
+                    int snapshotId = getSnapshotIdFromArchive(snapshot_time + "");
+                    if(snapshotId != -1) {
+                        // snapshot already exists in the db
+                    } else {
+                        // doesn't exist in the db
+                        // add snapshot time record to the db
+                        snapshotsOver += archiveStmt.executeUpdate("INSERT INTO Snapshots (snapshot_time, snapshot_date) VALUES (" + snapshot_time + ", " + surroundWithQuotes(snapshot_date) +")");
+                        snapshotId = getSnapshotIdFromArchive(snapshot_time + "");
+                    }
+                    // ensure Statistics table has record of mbeanId, snapshotId, statsValue, statsName
+                    String updateStr = prepareInsertStatement(statsName, statsValue, snapshotId, mbeanId);
+                    statisticsOver += archiveStmt.executeUpdate( updateStr );
+                    // close connection to archiveDB
+                    archiveConn.close();
+                }
+                closeConnection();
+            }
+            // for each snapshot time, remove all instances that is associated with in 
+            // in the active DB
+            for(int i = 0; i < overdueTimes.size(); i++) {
+                long snapshotTime = overdueTimes.get(i);
+                openActiveConnection();
+                Statement stmt = conn.createStatement();
+                // remove from Statistics table
+                String statisticsUpdate = "DELETE FROM Statistics WHERE snapshotId=" + getSnapshotId(snapshotTime + "");
+                statisticsOver -= stmt.executeUpdate(statisticsUpdate);
+                // remove from Snapshots Table
+                String snapshotUpdate = "DELETE FROM Snapshots WHERE snapshot_time=" + snapshotTime;
+                snapshotsOver -= stmt.executeUpdate(snapshotUpdate);
+                closeConnection();
+            }
+        } catch(Exception e) {
+            e.printStackTrace();
+        } finally {
+            closeConnection();
+        }
+        
+        // ensure that the transferring was good
+        if(snapshotsOver != 0) {
+            System.out.println("[WARN] Transferred snapshots was completed, but some things were lost.");
+        }
+        if(statisticsOver != 0) {
+            System.out.println("[WARN] Transferred statistics was completed, but some things were lost.");
+        }
+    }
+    
+    /**
+     * @param cutOffTime
+     * @return An SQL table contain a column of all the times that did not make the cutOffTime.
+     */
+    private static ResultSet getOverDueSnapshotTimes(long cutOffTime) {
+        try {
+            Statement stmt = conn.createStatement();
+            String query = "SELECT snapshot_time FROM Snapshots WHERE snapshot_time < " + cutOffTime;
+            return stmt.executeQuery(query);
+        } catch(Exception e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+    
+    /**
      * @param snapshot_time
-     * @return The snapshot id of the snapshot time from table Snapshots. Returns -1 if record does not exist.
+     * @return The snapshot id of the snapshot time from table ActiveDB.Snapshots. Returns -1 if record does not exist.
      */    
     private static int getSnapshotId(String snapshot_time) throws Exception {
+        int retval = -1;
+        Connection conn = activeDS.getConnection();
         Statement stmt = conn.createStatement();
         ResultSet rs = stmt.executeQuery("SELECT id FROM Snapshots WHERE snapshot_time=" + snapshot_time);
         if(rs.next()) {
-            return rs.getInt("id");
-        } else {
-            return -1;
+            retval = rs.getInt("id");
         }
+        stmt.close();
+        conn.close();
+        return retval;
     }
     
     /**
+     * @param snapshot_time
+     * @return The snapshot id of the snapshot time from table ArchiveDB.Snapshots. Returns -1 if record does not exist.
+     */    
+    private static int getSnapshotIdFromArchive(String snapshot_time) throws Exception {
+        int retval = -1;
+        Connection archiveConn = archiveDS.getConnection();
+        Statement stmt = archiveConn.createStatement();
+        ResultSet rs = stmt.executeQuery("SELECT id FROM Snapshots WHERE snapshot_time=" + snapshot_time);
+        if(rs.next()) {
+            retval = rs.getInt("id");
+        }
+        stmt.close();
+        archiveConn.close();
+        return retval;
+    }
+
+    /**
      * @param mbean
-     * @return The mbean id of the mbean from table MBean. Returns -1 if record does not exist.
+     * @return The mbean id of the mbean from table ArchiveDB.MBean. Returns -1 if record does not exist.
+     */
+    private static int getMBeanIdFromArchive(String mbean) throws Exception {
+        int retval = -1;
+        Connection archiveConn = archiveDS.getConnection();
+        Statement stmt = archiveConn.createStatement();
+        ResultSet rs = stmt.executeQuery("SELECT id FROM MBeans WHERE mbeanName=" + surroundWithQuotes(mbean));
+        if(rs.next()) {
+            retval = rs.getInt("id");
+        }
+        stmt.close();
+        archiveConn.close();
+        return retval;
+    }
+    
+    /**
+     * @param mbean
+     * @return The mbean id of the mbean from table ActiveDB.MBean. Returns -1 if record does not exist.
      */
     private static int getMBeanId(String mbean) throws Exception {
+        int retval = -1;
+        Connection conn = activeDS.getConnection();
         Statement stmt = conn.createStatement();
         ResultSet rs = stmt.executeQuery("SELECT id FROM MBeans WHERE mbeanName=" + surroundWithQuotes(mbean));
         if(rs.next()) {
-            return rs.getInt("id");
-        } else {
-            return -1;
+            retval = rs.getInt("id");
         }
+        stmt.close();
+        conn.close();
+        return retval;
     }
     
     /**
@@ -388,6 +544,9 @@
         query += " AND S.snapshotId=SN.id AND S.mbeanId=M.id";
         ResultSet retval = null;
         try {
+            if(conn.isClosed()) {
+                openActiveConnection();
+            }
             Statement stmt = conn.createStatement();
             retval = stmt.executeQuery(query);
         } catch(Exception e) {
@@ -403,6 +562,9 @@
         String query = "SELECT DISTINCT snapshot_time FROM Snapshots ORDER BY snapshot_time DESC";
         ResultSet retval = null;
         try {
+            if(conn.isClosed()) {
+                openActiveConnection();
+            }
             Statement stmt = conn.createStatement();
             retval = stmt.executeQuery(query);
         } catch(Exception e) {
@@ -416,9 +578,7 @@
      */
     private static void openActiveConnection() {
         try {
-            if(conn == null || conn.isClosed()) {
-                conn = activeDS.getConnection();
-            }
+            conn = activeDS.getConnection();
         } catch(Exception e) {
             e.printStackTrace();
         }
@@ -429,9 +589,7 @@
      */
     private static void openArchiveConnection() {
         try {
-            if(conn == null || conn.isClosed()) {
-                conn = archiveDS.getConnection();
-            }
+            conn = archiveDS.getConnection();
         } catch(Exception e) {
             e.printStackTrace();
         }

Modified: geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/snapshot/SnapshotThread.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/snapshot/SnapshotThread.java?rev=588476&r1=588475&r2=588476&view=diff
==============================================================================
--- geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/snapshot/SnapshotThread.java (original)
+++ geronimo/sandbox/monitoring/mrc-server/mrc-ejb/src/main/java/org/apache/geronimo/monitor/snapshot/SnapshotThread.java Thu Oct 25 19:35:28 2007
@@ -142,12 +142,12 @@
                 // take a snapshot
                 System.out.println("======SNAPSHOT======");
                 // instantiate map <mbean name, stats for mbean>
-                HashMap<String, HashMap> aggregateStats = new HashMap<String, HashMap>();
+                HashMap<String, HashMap<String, Long>> aggregateStats = new HashMap<String, HashMap<String, Long>>();
                 MasterRemoteControlRemote mrc = getMRC();
                 // for each mbean name in the list, get its stats
                 for(int i = 0; i < mbeanNames.size(); i++) {
                     String mbeanName = mbeanNames.get(i);
-                    HashMap stats = (HashMap)mrc.getStats(mbeanName);
+                    HashMap<String, Long> stats = (HashMap<String, Long>)mrc.getStats(mbeanName);
                     aggregateStats.put(mbeanName, stats);
                 }
                 
@@ -156,7 +156,7 @@
                 
                 for(Iterator itt = aggregateStats.keySet().iterator(); itt.hasNext(); ) {
                     String mbean = (String)itt.next();
-                    HashMap stats = aggregateStats.get(mbean);
+                    HashMap<String, Long> stats = aggregateStats.get(mbean);
                     System.out.println(mbean);
                     for(Iterator it = stats.keySet().iterator(); it.hasNext(); ) {
                         String key = (String)it.next();