use of com.yahoo.dba.perf.myperf.common.Metric in project mysql_perf_analyzer by yahoo.
the class UdmController method testAndValidateAlert.
private void testAndValidateAlert(HttpServletRequest req, AlertDefinition def, String db) throws Exception {
if (def == null)
throw new Exception("please provide valid alert definition for test");
if (AlertDefinition.SOURCE_SQL.equals(def.getSource())) {
if (def.getSqlText() == null || def.getSqlText().isEmpty())
throw new Exception("please provide valid SQL text for test");
if (db == null || db.isEmpty())
throw new Exception("please provide valid database for test");
String[] dbs = db.split("\\|");
if (dbs == null || dbs.length < 2)
throw new Exception("please provide valid database for test");
DBInstanceInfo dbinfo = this.frameworkContext.getDbInfoManager().findDB(dbs[0], dbs[1]);
if (dbinfo == null)
throw new Exception("please provide valid database for test");
DBConnectionWrapper connWrapper = null;
Statement stmt = null;
ResultSet rs = null;
try {
connWrapper = WebAppUtil.getDBConnection(req, this.frameworkContext, dbinfo);
if (connWrapper == null) {
throw new Exception("failed to connect to target db (" + dbinfo + ")");
}
String sql = def.getSqlText();
if (def.getParams() != null && def.getParams().size() > 0) {
for (Map.Entry<String, String> e : def.getParams().entrySet()) {
sql = sql.replace("&" + e.getKey(), e.getValue());
}
}
stmt = connWrapper.getConnection().createStatement();
rs = stmt.executeQuery(sql);
} finally {
DBUtils.close(rs);
DBUtils.close(stmt);
//close it anyway
WebAppUtil.closeDBConnection(req, connWrapper, true, false);
}
} else if (AlertDefinition.SOURCE_METRICS.equals(def.getSource())) {
String metricsName = def.getMetricName();
if (metricsName == null || metricsName.isEmpty())
throw new Exception("please provide valid metrics name in the format of group[.subgroup].metric_name");
String[] ms = metricsName.split("\\.");
if (ms.length < 2)
throw new Exception("please provide valid metrics name in the format of group[.subgroup].metric_name. " + metricsName + " is not valid.");
MetricsGroup mg = this.frameworkContext.getMetricsDef().getGroupByName(ms[0]);
if (mg == null) {
//try UDM
UserDefinedMetrics udm = this.frameworkContext.getMetricsDef().getUdmManager().getUDMByName(ms[0]);
if (udm == null)
throw new Exception("please provide valid metrics name in the format of group[.subgroup].metric_name. " + ms[0] + " is not valid metrics group name.");
else
mg = udm.getMetricsGroup();
}
if (mg == null)
throw new Exception("please provide valid metrics name in the format of group[.subgroup].metric_name. " + ms[0] + " is not valid metrics group name.");
if (mg.getSubGroups().size() > 0) {
if (ms.length < 3)
throw new Exception("please provide valid metrics name in the format of group[.subgroup].metric_name. " + ms[0] + " has sub groups.");
else
mg = mg.getSubGroupByName(ms[1]);
if (mg == null)
throw new Exception("please provide valid metrics name in the format of group[.subgroup].metric_name. " + ms[1] + " is not a valid sub group in group " + ms[0]);
}
boolean findOne = false;
for (Metric m : mg.getMetrics()) {
if (m.getName().equals(ms[ms.length - 1])) {
findOne = true;
break;
}
}
if (!findOne)
throw new Exception("please provide valid metrics name in the format of group[.subgroup].metric_name. " + metricsName + " is not valid.");
}
}
use of com.yahoo.dba.perf.myperf.common.Metric in project mysql_perf_analyzer by yahoo.
the class UdmController method testUDM.
/**
*
* Test UDM, use exception to send out message
* @param req
* @param udm
* @param db
* @throws SQLException
*/
private void testUDM(HttpServletRequest req, UserDefinedMetrics udm, String db) throws Exception {
if (db == null || db.isEmpty())
throw new Exception("please provide valid database for test");
String[] dbs = db.split("\\|");
if (dbs == null || dbs.length < 2)
throw new Exception("please provide valid database for test");
DBInstanceInfo dbinfo = this.frameworkContext.getDbInfoManager().findDB(dbs[0], dbs[1]);
if (dbinfo == null)
throw new Exception("please provide valid database for test");
HashSet<String> metricsNameSet = new HashSet<String>();
for (Metric m : udm.getMetrics()) {
metricsNameSet.add(m.getSourceName());
}
DBConnectionWrapper connWrapper = null;
Statement stmt = null;
ResultSet rs = null;
try {
String sql = udm.getSql();
MetricsGroup mg = udm.getMetricsGroup();
String udmType = udm.getUdmType();
String nameCol = udm.getNameCol();
String valCol = udm.getValueCol();
String keyCol = udm.getKeyCol();
boolean isBuiltin = false;
if (!"SQL".equals(udm.getSource())) {
sql = this.frameworkContext.getSqlTextForMetricsGroup(udm.getSource());
mg = this.frameworkContext.getMetricsDef().getGroupByName(udm.getSource());
if (mg != null) {
if (mg.getKeyColumn() != null)
udmType = "key";
else if (mg.isMultipleMetricsPerRow())
udmType = "column";
else
udmType = "row";
nameCol = mg.getMetricNameColumn();
valCol = mg.getMetricValueColumn();
keyCol = mg.getKeyColumn();
}
isBuiltin = true;
}
if (sql == null || sql.isEmpty()) {
throw new Exception("please provide valid SQL");
}
connWrapper = WebAppUtil.getDBConnection(req, this.frameworkContext, dbinfo);
if (connWrapper == null) {
throw new Exception("failed to connect to target db (" + dbinfo + ")");
}
stmt = connWrapper.getConnection().createStatement();
rs = stmt.executeQuery(sql);
if (rs != null) {
ResultSetMetaData meta = rs.getMetaData();
//verify columns
int cols = meta.getColumnCount();
Map<String, Integer> colMap = new HashMap<String, Integer>(cols);
for (int i = 1; i <= cols; i++) colMap.put(meta.getColumnName(i).toUpperCase(), meta.getColumnType(i));
if ("row".equals(udmType)) {
if (!colMap.containsKey(udm.getNameCol().toUpperCase()))
throw new Exception("Failed to find name column from SQL result: " + udm.getNameCol() + ", returned: " + colMap);
if (!colMap.containsKey(udm.getValueCol().toUpperCase()))
throw new Exception("Failed to find value column from SQL result: " + udm.getValueCol() + ", returned: " + colMap);
} else //check metrics column
{
if ("key".equals(udmType)) {
if (!colMap.containsKey(keyCol.toUpperCase()))
throw new Exception("Failed to find key column from SQL result: " + udm.getKeyCol());
}
for (Metric m : udm.getMetrics()) {
if (!colMap.containsKey(m.getSourceName().toUpperCase()))
throw new Exception("Failed to find metric column from SQL result: " + m.getSourceName());
}
}
} else {
throw new Exception("Failed to test SQL.");
}
while (rs != null && rs.next()) {
if ("row".equals(udmType)) {
String name = rs.getString(nameCol);
if (!metricsNameSet.contains(name))
continue;
String val = rs.getString(valCol);
try {
BigDecimal d = new BigDecimal(val == null ? "0" : val);
} catch (Exception ex) {
throw new Exception("Expect numeric value for metric from SQL result, got " + val);
}
} else {
for (Metric m : udm.getMetrics()) {
String val = rs.getString(m.getSourceName());
try {
BigDecimal d = new BigDecimal(val == null ? "0" : val);
} catch (Exception ex) {
throw new Exception("Expect numeric value metric value from SQL result for column " + m.getShortName() + ", got " + val);
}
}
}
}
} finally {
DBUtils.close(rs);
DBUtils.close(stmt);
//close it anyway
WebAppUtil.closeDBConnection(req, connWrapper, true, false);
}
}
Aggregations