Search in sources :

Example 6 with MetricsGroup

use of com.yahoo.dba.perf.myperf.common.MetricsGroup in project mysql_perf_analyzer by yahoo.

the class UdmController method handleRequestImpl.

@Override
protected ModelAndView handleRequestImpl(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    String message = "OK";
    String cmd = req.getParameter("cmd");
    boolean isValid = true;
    UserDefinedMetrics udm = null;
    logger.info("receive cmd " + cmd + ", url " + req.getQueryString());
    //it a form request
    if ((cmd == null || cmd.isEmpty()) && !WebAppUtil.hasValidSession(req)) {
        return new ModelAndView(new RedirectView(nosessView));
    }
    //first session check
    boolean isSessionValid = WebAppUtil.hasValidSession(req);
    if (!isSessionValid)
        return this.respondFailure("session timeout. Please logout and re-login.", req);
    AppUser appUser = AppUser.class.cast(req.getSession().getAttribute(AppUser.SESSION_ATTRIBUTE));
    ModelAndView mv = null;
    //By default, just display the page
    if (//or any invalid command
    cmd == null || cmd.isEmpty())
        message = null;
    if ("test".equals(cmd) || "publish".equals(cmd)) {
        if (appUser.isAdminUser())
            return this.processNewUDM(req, resp);
        else
            return this.respondFailure("Non admin user is not allowed to add UDM", req);
    } else if ("test_alert".equals(cmd) || "publish_alert".equals(cmd)) {
        if (appUser.isAdminUser())
            return this.processNewAlert(req, resp);
        else
            return this.respondFailure("Non admin user is not allowed to add UDM", req);
    } else if (//display
    "udm_detail".equalsIgnoreCase(cmd)) {
        String name = req.getParameter("name");
        udm = this.frameworkContext.getMetricsDef().getUdmManager().getUDMByName(name);
        if (udm != null) {
            mv = new ModelAndView(this.jsonView);
            String res = "{\"resp\":{\"status\": 0, \"message\":\"OK\", \"udm\": " + udm.toJSON(true) + "}}";
            //logger.info("UDM detail: "+res);
            mv.addObject("json_result", res);
            return mv;
        } else
            return this.respondFailure("UDM " + name + " not found", req);
    } else if (//display
    "alert_detail".equalsIgnoreCase(cmd)) {
        String name = req.getParameter("name");
        AlertDefinition alert = this.frameworkContext.getMetricsDef().getUdmManager().getAlertByName(name);
        if (alert != null) {
            mv = new ModelAndView(this.jsonView);
            String res = "{\"resp\":{\"status\": 0, \"message\":\"OK\", \"alert\": " + alert.toJSON(true) + "}}";
            //logger.info("UDM detail: "+res);
            mv.addObject("json_result", res);
            return mv;
        } else
            return this.respondFailure("ALERT " + name + " not found", req);
    } else if (//display
    "udmdb_detail".equalsIgnoreCase(cmd))
        return processUDMDBDetail(req, resp);
    else if (//display
    "udmdb_update".equalsIgnoreCase(cmd))
        return processUDMDBUpdate(req, resp, appUser);
    else if (//display
    "alertdb_update".equalsIgnoreCase(cmd))
        return this.processAlertDBUpdate(req, resp, appUser);
    mv = new ModelAndView(this.formView);
    //udm list
    List<String> udms = new ArrayList<String>();
    for (Map.Entry<String, UserDefinedMetrics> e : this.frameworkContext.getMetricsDef().getUdmManager().getUdms().entrySet()) {
        udms.add(e.getKey());
    }
    mv.addObject("udms", udms);
    //alerts
    List<String> alerts = new ArrayList<String>();
    for (Map.Entry<String, AlertDefinition> e : this.frameworkContext.getMetricsDef().getUdmManager().getAlerts().entrySet()) {
        alerts.add(e.getKey());
    }
    mv.addObject("alerts", alerts);
    //predefined
    Map<String, String> predefined = new TreeMap<String, String>();
    String[] predefinedGroups = this.frameworkContext.getMetricsDef().getGroupNames();
    for (String gname : predefinedGroups) {
        MetricsGroup g = this.frameworkContext.getMetricsDef().getGroupByName(gname);
        List<MetricsGroup> subGroups = g.getSubGroups();
        if (subGroups != null && subGroups.size() > 0) {
            for (MetricsGroup subG : subGroups) predefined.put(gname + "." + subG.getGroupName(), subG.isAuto() ? "y" : "n");
        } else
            predefined.put(gname, g.isAuto() ? "y" : "n");
    }
    mv.addObject("predefined", predefined);
    if (WebAppUtil.hasValidSession(req)) {
        mv.addObject("mydbs", this.frameworkContext.getDbInfoManager().listDbsByUserInfo(WebAppUtil.findUserFromRequest(req), retrieveAppUser(req).isRestrictedUser()));
        mv.addObject("mydbSize", this.frameworkContext.getDbInfoManager().getMyDatabases(WebAppUtil.findUserFromRequest(req), retrieveAppUser(req).isRestrictedUser()).size());
    } else {
        mv.addObject("mydbs", this.frameworkContext.getDbInfoManager().getClusters().keySet());
        mv.addObject("mydbSize", 0);
    }
    mv.addObject("dbMap", this.frameworkContext.getDbInfoManager().getClusters());
    mv.addObject("help_key", "udm");
    if (!isValid && message != null)
        mv.addObject("message", message);
    else if (message != null)
        mv.addObject("okmessage", message);
    if (cmd != null)
        mv.addObject("cmd", cmd);
    mv.addObject("u", appUser);
    return mv;
}
Also used : UserDefinedMetrics(com.yahoo.dba.perf.myperf.common.UserDefinedMetrics) ModelAndView(org.springframework.web.servlet.ModelAndView) ArrayList(java.util.ArrayList) AppUser(com.yahoo.dba.perf.myperf.common.AppUser) TreeMap(java.util.TreeMap) MetricsGroup(com.yahoo.dba.perf.myperf.common.MetricsGroup) RedirectView(org.springframework.web.servlet.view.RedirectView) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) AlertDefinition(com.yahoo.dba.perf.myperf.common.AlertDefinition)

Example 7 with MetricsGroup

use of com.yahoo.dba.perf.myperf.common.MetricsGroup 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.");
    }
}
Also used : UserDefinedMetrics(com.yahoo.dba.perf.myperf.common.UserDefinedMetrics) MetricsGroup(com.yahoo.dba.perf.myperf.common.MetricsGroup) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Metric(com.yahoo.dba.perf.myperf.common.Metric) DBConnectionWrapper(com.yahoo.dba.perf.myperf.db.DBConnectionWrapper) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SQLException(java.sql.SQLException) DBInstanceInfo(com.yahoo.dba.perf.myperf.common.DBInstanceInfo)

Example 8 with MetricsGroup

use of com.yahoo.dba.perf.myperf.common.MetricsGroup in project mysql_perf_analyzer by yahoo.

the class UdmController method processUDMDBDetail.

private ModelAndView processUDMDBDetail(HttpServletRequest req, HttpServletResponse resp) {
    ModelAndView mv = null;
    String group = req.getParameter("group");
    String host = req.getParameter("host");
    String typ = req.getParameter("typ");
    ResultList rList = null;
    if (typ == null || typ.isEmpty() || "UDM".equalsIgnoreCase(typ)) {
        List<String> udms = this.frameworkContext.getMetricsDef().getUdmManager().getMetricsSubscriptions().getSubscribedUDMs(group, host);
        if (udms != null && udms.size() > 0) {
            rList = new ResultList();
            ColumnDescriptor desc = new ColumnDescriptor();
            desc.addColumn("UDM", false, 0);
            rList.setColumnDescriptor(desc);
            for (String u : udms) {
                ResultRow row = new ResultRow();
                ArrayList<String> cols = new ArrayList<String>();
                cols.add(u);
                row.setColumns(cols);
                rList.addRow(row);
            }
        }
    } else if ("ALERT".equalsIgnoreCase(typ)) {
        List<AlertSubscribers.Subscription> alerts = this.frameworkContext.getMetricsDef().getUdmManager().getAlertSubscriptions().getSubscriptions(group, host);
        if (alerts != null && alerts.size() > 0) {
            rList = new ResultList();
            ColumnDescriptor desc = new ColumnDescriptor();
            desc.addColumn("UDM", false, 0);
            rList.setColumnDescriptor(desc);
            for (AlertSubscribers.Subscription u : alerts) {
                ResultRow row = new ResultRow();
                ArrayList<String> cols = new ArrayList<String>();
                cols.add(u.alertName);
                row.setColumns(cols);
                rList.addRow(row);
            }
        }
    } else if ("PREDEFINED".equalsIgnoreCase(typ)) {
        rList = new ResultList();
        ColumnDescriptor desc = new ColumnDescriptor();
        desc.addColumn("UDM", false, 0);
        rList.setColumnDescriptor(desc);
        String[] mgNames = this.frameworkContext.getMetricsDef().getGroupNames();
        for (String mgName : mgNames) {
            MetricsGroup mg = this.frameworkContext.getMetricsDef().getGroupByName(mgName);
            if (!mg.isAuto()) {
                //skip groups requiring manual configuration for now 
                if (this.frameworkContext.getMetricsDef().getUdmManager().isMetricsGroupSubscribed(group, host, mg.getGroupName(), null)) {
                    ResultRow row = new ResultRow();
                    ArrayList<String> cols = new ArrayList<String>();
                    cols.add(mg.getGroupName());
                    row.setColumns(cols);
                    rList.addRow(row);
                }
            }
            List<MetricsGroup> subGrps = mg.getSubGroups();
            if (subGrps != null && subGrps.size() > 0) {
                for (MetricsGroup subG : subGrps) {
                    if (!subG.isAuto() && this.frameworkContext.getMetricsDef().getUdmManager().isMetricsGroupSubscribed(group, host, mg.getGroupName(), subG.getGroupName())) {
                        ResultRow row = new ResultRow();
                        ArrayList<String> cols = new ArrayList<String>();
                        cols.add(mg.getGroupName() + "." + subG.getGroupName());
                        row.setColumns(cols);
                        rList.addRow(row);
                    }
                }
            }
        }
    }
    mv = new ModelAndView(this.jsonView);
    mv.addObject("json_result", ResultListUtil.toJSONString(rList, null, 0, "OK"));
    return mv;
}
Also used : ResultRow(com.yahoo.dba.perf.myperf.common.ResultRow) ResultList(com.yahoo.dba.perf.myperf.common.ResultList) AlertSubscribers(com.yahoo.dba.perf.myperf.common.AlertSubscribers) ColumnDescriptor(com.yahoo.dba.perf.myperf.common.ColumnDescriptor) ModelAndView(org.springframework.web.servlet.ModelAndView) ArrayList(java.util.ArrayList) MetricsGroup(com.yahoo.dba.perf.myperf.common.MetricsGroup) ArrayList(java.util.ArrayList) List(java.util.List) ResultList(com.yahoo.dba.perf.myperf.common.ResultList)

Example 9 with MetricsGroup

use of com.yahoo.dba.perf.myperf.common.MetricsGroup 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);
    }
}
Also used : HashMap(java.util.HashMap) Statement(java.sql.Statement) SQLException(java.sql.SQLException) BigDecimal(java.math.BigDecimal) ResultSetMetaData(java.sql.ResultSetMetaData) MetricsGroup(com.yahoo.dba.perf.myperf.common.MetricsGroup) ResultSet(java.sql.ResultSet) Metric(com.yahoo.dba.perf.myperf.common.Metric) DBConnectionWrapper(com.yahoo.dba.perf.myperf.db.DBConnectionWrapper) DBInstanceInfo(com.yahoo.dba.perf.myperf.common.DBInstanceInfo) HashSet(java.util.HashSet)

Example 10 with MetricsGroup

use of com.yahoo.dba.perf.myperf.common.MetricsGroup in project mysql_perf_analyzer by yahoo.

the class MetricsDbBase method removeUDM.

public boolean removeUDM(UserDefinedMetrics udm) {
    if (udm == null)
        return false;
    MetricsGroup mg = udm.getMetricsGroup();
    String tblName = mg.getSinkTableName();
    synchronized (metricsDefLock) {
        if (this.metricsGroups.containsKey(tblName)) {
            this.metricsGroups.remove(tblName);
            this.dataQueues.remove(tblName);
            this.insertSQL.remove(tblName);
        }
    }
    return true;
}
Also used : MetricsGroup(com.yahoo.dba.perf.myperf.common.MetricsGroup)

Aggregations

MetricsGroup (com.yahoo.dba.perf.myperf.common.MetricsGroup)11 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 UserDefinedMetrics (com.yahoo.dba.perf.myperf.common.UserDefinedMetrics)4 SQLException (java.sql.SQLException)4 Statement (java.sql.Statement)4 DBInstanceInfo (com.yahoo.dba.perf.myperf.common.DBInstanceInfo)3 Metric (com.yahoo.dba.perf.myperf.common.Metric)3 DBConnectionWrapper (com.yahoo.dba.perf.myperf.db.DBConnectionWrapper)2 ByteBuffer (java.nio.ByteBuffer)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 TreeMap (java.util.TreeMap)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 AlertDefinition (com.yahoo.dba.perf.myperf.common.AlertDefinition)1 AlertSubscribers (com.yahoo.dba.perf.myperf.common.AlertSubscribers)1 AppUser (com.yahoo.dba.perf.myperf.common.AppUser)1 ColumnDescriptor (com.yahoo.dba.perf.myperf.common.ColumnDescriptor)1 DBGroupInfo (com.yahoo.dba.perf.myperf.common.DBGroupInfo)1