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.
Method from org.apache.geronimo.monitoring.snapshot.SnapshotDBHelper Detail: |
public boolean addSnapshotToDB(HashMap<String, Long> aggregateStats) {
boolean success = true;
// get the current time from 1970
String currTime = "";
currTime += (new Date()).getTime();
try {
// for each mbean
for(Iterator itt = aggregateStats.keySet().iterator(); itt.hasNext(); ) {
String mbean = (String)itt.next();
// prepare the statsNameList and statsValueList beforehand
String statsNameList = "", statsValueList = "";
for(Iterator< String > it = aggregateStats.get(mbean).keySet().iterator(); it.hasNext(); ) {
String statsName = it.next();
Long statsValue = aggregateStats.get(mbean).get(statsName);
if(statsNameList.length() == 0 || statsValueList.length() == 0) {
// do not add a comma because this is the first occurrence
statsValueList += statsValue.toString();
statsNameList += statsName;
} else {
// add a comma
statsValueList += "," + statsValue.toString();
statsNameList += "," + statsName;
}
}
// start talking to DB
openActiveConnection();
Statement stmt = conn.createStatement();
HashMap stats = aggregateStats.get(mbean);
//--------Ensure MBeans are in place
int mbeanId = getMBeanId(mbean);
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
stmt.executeUpdate("INSERT INTO MBeans (mbeanName, statsNameList) VALUES ("+ surroundWithQuotes(mbean) + "," + surroundWithQuotes(statsNameList) + ")");
mbeanId = getMBeanId(mbean);
}
// insert the statistics into Statistics table
stmt.executeUpdate( prepareInsertSnapshotStatement(currTime, statsValueList, mbeanId) );
closeConnection();
}
} catch(Exception e){
log.error(e.getMessage(), e);
success = false;
} finally {
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) {
log.warn("Cannot archive snapshots because attribute 'retention' is not present in snapshot-config.xml.");
}
return success;
}
|
public ArrayList<String, Object> fetchData(Integer numberOfSnapshot,
Integer everyNthSnapshot) {
ArrayList< HashMap< String, HashMap< String, Object > > > stats = new ArrayList< HashMap< String, HashMap< String, Object > > >();
openActiveConnection();
// get all records in the database grouped and ordered by time
ResultSet table = fetchSnapshotTimesFromDB();
// iterate through the table and finds the times (which uniquely IDs a snapshot)
// that are wanted and queries the rest of the DB using the time as the condition
// (i.e. the ones that are in the c*n-th snapshot where c < = numberOfSnapshot
// and n == everyNthSnapshot)
int nthSnapshot = 0;
try {
while(table.next()) {
Long snapshotTime = table.getLong( MonitorConstants.SNAPSHOT_TIME );
// grab 0*nth, 1*nth, 2*nth snapshot up to min(numberOfSnapshot*everyNthSnapshot, size of the table)
if(nthSnapshot % everyNthSnapshot == 0) {
HashMap< String, HashMap< String, Object > > snapshotData = packageSnapshotData(snapshotTime);
stats.add( 0, snapshotData );
numberOfSnapshot--;
}
nthSnapshot++;
// no more snapshots needs to be looked at, we have successfully acquired our goal
if(numberOfSnapshot == 0) {
break;
}
}
} catch(Exception e) {
log.error(e.getMessage(), e);
} finally {
closeConnection();
}
return stats;
}
Fetches the data stored from the snapshot thread and returns
it in a ArrayList with each element being a HashMap of the attribute
mapping to the statistic. Grabs 'numberOfSnapshots' snapshots. Grabs
one snapshot per 'everyNthsnapshot' |
public HashMap<String, Long> fetchMaxSnapshotData(Integer numberOfSnapshots) {
return fetchMaxOrMinSnapshotData(numberOfSnapshots, true);
}
|
public HashMap<String, Long> fetchMinSnapshotData(Integer numberOfSnapshots) {
return fetchMaxOrMinSnapshotData(numberOfSnapshots, false);
}
|
public HashMap<String> getAllSnapshotStatAttributes() {
openActiveConnection();
HashMap< String, ArrayList< String > > retval = new HashMap< String, ArrayList< String > >();
try {
Statement stmt = conn.createStatement();
String query = "SELECT DISTINCT mbeanName, statsNameList FROM MBeans";
ResultSet rs = stmt.executeQuery(query);
// add each mbean/statsValue combination to retval
while(rs.next()) {
String mbeanName = rs.getString( MonitorConstants.MBEANNAME );
String statsNameStr = rs.getString( MonitorConstants.STATSNAMELIST );
String[] statsNameList = statsNameStr.split(",");
ArrayList< String > mbeanAttributeList = new ArrayList< String >();
// copy from String[] to ArrayList< String >
for(int i = 0; i < statsNameList.length; i++) {
mbeanAttributeList.add(statsNameList[i]);
}
retval.put(mbeanName, mbeanAttributeList);
}
} catch(Exception e) {
log.error(e.getMessage(), e);
} finally {
closeConnection();
}
return retval;
}
|
public Long getSnapshotCount() {
long retval = 0;
try {
openActiveConnection();
Statement stmt = conn.createStatement();
String query = "SELECT COUNT(DISTINCT snapshot_time) FROM Statistics";
ResultSet rs = stmt.executeQuery(query);
rs.next();
retval = rs.getLong(1);
} catch(Exception e) {
log.error(e.getMessage(), e);
} finally {
closeConnection();
}
return new Long(retval);
}
|
public TreeMap<Long, Long> getSpecificStatistics(String mbeanName,
String statsName,
int numberOfSnapshots,
int everyNthSnapshot,
boolean showArchived) {
openActiveConnection();
TreeMap< Long, Long > stats = new TreeMap< Long, Long >();
int nthSnapshot = 0;
// attempt to get as many snapshots from the active db as possible
try {
Statement stmt = conn.createStatement();
int mbeanId = getMBeanId(mbeanName);
if(mbeanId == -1) {
log.error(mbeanName + " does not exist in the database.");
} else {
String query = "SELECT DISTINCT snapshot_time, statsValueList, statsNameList FROM Statistics, MBeans M WHERE mbeanId=" + mbeanId + " AND mbeanId=M.id ORDER BY snapshot_time DESC";
ResultSet rs = stmt.executeQuery(query);
// iterate through the table paying attention to those at everyNthSnapshot-th position
while(rs.next()) {
// every nth snapshot I save the information into my returning hashmap
if(nthSnapshot % everyNthSnapshot == 0) {
String[] statsValueList = rs.getString( MonitorConstants.STATSVALUELIST ).split(",");
String[] statsNameList = rs.getString( MonitorConstants.STATSNAMELIST ).split(",");
assert(statsValueList.length == statsNameList.length);
Long statsValue = null;
for(int i = 0 ; i < statsNameList.length; i++) {
if(statsNameList[i].equals(statsName)) {
long value = Long.parseLong(statsValueList[i]);
statsValue = new Long(value);
}
}
// exit function after error
if(statsValue == null) {
log.warn("Statistics name '" + statsName + "' does not exist");
return stats;
} else {
stats.put(rs.getLong( MonitorConstants.SNAPSHOT_TIME ), statsValue);
numberOfSnapshots--;
}
}
// update counter
nthSnapshot++;
// enough data, end this thing
if(numberOfSnapshots == 0) {
break;
}
}
}
} catch(Exception e) {
log.error(e.getMessage(), e);
} finally {
closeConnection();
}
nthSnapshot = 0;
// attempt to get the remaining snapshots requested from the archive DB
// iff the showArchive flag is set
if(showArchived && numberOfSnapshots != 0) {
try {
openArchiveConnection(); // connection to the Archive DB
Statement stmt = conn.createStatement();
int mbeanId = getMBeanId(mbeanName);
if(mbeanId == -1) {
log.error(mbeanName + " does not exist in the database.");
} else {
String query = "SELECT DISTINCT snapshot_time, statsValueList, statsNameList FROM Statistics, MBeans M WHERE mbeanId=" + mbeanId + " AND mbeanId=M.id ORDER BY snapshot_time DESC";
ResultSet rs = stmt.executeQuery(query);
// iterate through the table paying attention to those at everyNthSnapshot-th position
while(rs.next()) {
// every nth snapshot I save the information into my returning hashmap
if(nthSnapshot % everyNthSnapshot == 0) {
String[] statsValueList = rs.getString( MonitorConstants.STATSVALUELIST ).split(",");
String[] statsNameList = rs.getString( MonitorConstants.STATSNAMELIST ).split(",");
assert(statsValueList.length == statsNameList.length);
Long statsValue = null;
for(int i = 0 ; i < statsNameList.length; i++) {
if(statsNameList[i].equals(statsName)) {
long value = Long.parseLong(statsValueList[i]);
statsValue = new Long(value);
}
}
// exit function after error
if(statsValue == null) {
log.warn("Statistics name '" + statsName + "' does not exist");
return stats;
} else {
stats.put(rs.getLong( MonitorConstants.SNAPSHOT_TIME ), statsValue);
numberOfSnapshots--;
}
}
// update counter
nthSnapshot++;
// enough data, end this thing
if(numberOfSnapshots == 0) {
break;
}
}
}
} catch(Exception e) {
log.error(e.getMessage(), e);
} finally {
closeConnection();
}
}
return stats;
}
|
public String prepareInsertSnapshotStatement(String snapshot_time,
String statsValueList,
int mbeanId) {
String retval = "INSERT INTO Statistics (snapshot_time, statsValueList, mbeanId) VALUES (";
retval += snapshot_time;
retval += ",";
retval += surroundWithQuotes(statsValueList);
retval += ",";
retval += mbeanId;
retval += ")";
return retval;
}
|
public void setDataSources(DataSource active,
DataSource archive) {
activeDS = active;
archiveDS = archive;
}
Sets the necessary data sources for this helper to talk to the db |