Search in sources :

Example 1 with DBCredential

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

the class ReplLagQueryProcessor method querySingle.

@Override
public ResultList querySingle(MyPerfContext context, DBInstanceInfo dbinfo, String appUser, DBConnectionWrapper connWrapper, QueryParameters qps) throws SQLException {
    QueryParameters qps2 = new QueryParameters();
    qps2.setSql("mysql_repl_slave");
    ResultList rList = null;
    rList = context.getQueryEngine().executeQueryGeneric(qps2, connWrapper, qps.getMaxRows());
    String master = null;
    String port = null;
    if (rList != null) {
        java.util.LinkedHashMap<String, String> kvPairs = new java.util.LinkedHashMap<String, String>(rList.getRows().size());
        for (ResultRow row : rList.getRows()) {
            if (row.getColumns() == null || row.getColumns().size() < 2)
                continue;
            kvPairs.put(row.getColumns().get(0), row.getColumns().get(1));
        }
        master = kvPairs.get("Master_Host");
        port = kvPairs.get("Master_Port");
        if (master != null && !master.isEmpty() && port != null && !port.isEmpty()) {
            logger.info("Query master status from (" + master + ":" + port + ")");
            DBInstanceInfo dbinfo2 = new DBInstanceInfo();
            dbinfo2.setHostName(master);
            dbinfo2.setPort(port);
            dbinfo2.setDatabaseName("information_schema");
            dbinfo2.setDbType("mysql");
            String url = dbinfo2.getConnectionString();
            DBCredential cred = null;
            if (appUser != null) {
                try {
                    //first, check if the user has his own credential
                    cred = context.getMetaDb().retrieveDBCredential(appUser, dbinfo.getDbGroupName());
                    if (cred != null) {
                        Connection conn = null;
                        Statement stmt = null;
                        ResultSet rs = null;
                        try {
                            DriverManager.setLoginTimeout(60);
                            conn = DriverManager.getConnection(url, cred.getUsername(), cred.getPassword());
                            if (conn != null) {
                                stmt = conn.createStatement();
                                rs = stmt.executeQuery("show master status");
                                //TODO rearrange order
                                ResultList rList2 = new ResultList();
                                rList2.setColumnDescriptor(rList.getColumnDescriptor());
                                for (String e : SLAVE_LAG_ENTRIES) {
                                    ResultRow row = new ResultRow();
                                    row.setColumnDescriptor(rList.getColumnDescriptor());
                                    row.addColumn(e);
                                    row.addColumn(kvPairs.get(e));
                                    rList2.addRow(row);
                                    kvPairs.remove(e);
                                }
                                while (rs != null && rs.next()) {
                                    ResultRow row = new ResultRow();
                                    row.setColumnDescriptor(rList.getColumnDescriptor());
                                    row.addColumn("Master: File");
                                    row.addColumn(rs.getString("File"));
                                    rList2.addRow(row);
                                    row = new ResultRow();
                                    row.setColumnDescriptor(rList.getColumnDescriptor());
                                    row.addColumn("Master: Position");
                                    row.addColumn(rs.getString("Position"));
                                    rList2.addRow(row);
                                //TODO gtid set
                                }
                                //now add slave position info
                                for (String e : SLAVE_POS_ENTRIES) {
                                    ResultRow row = new ResultRow();
                                    row.setColumnDescriptor(rList.getColumnDescriptor());
                                    row.addColumn(e);
                                    row.addColumn(kvPairs.get(e));
                                    rList2.addRow(row);
                                    kvPairs.remove(e);
                                }
                                //now add remaining entries
                                for (Map.Entry<String, String> e : kvPairs.entrySet()) {
                                    ResultRow row = new ResultRow();
                                    row.setColumnDescriptor(rList.getColumnDescriptor());
                                    row.addColumn(e.getKey());
                                    row.addColumn(e.getValue());
                                    rList2.addRow(row);
                                }
                                return rList2;
                            }
                        } catch (Exception ex) {
                        } finally {
                            DBUtils.close(rs);
                            DBUtils.close(stmt);
                            DBUtils.close(conn);
                        }
                    }
                } catch (Exception ex) {
                }
            }
        }
    }
    return rList;
}
Also used : ResultRow(com.yahoo.dba.perf.myperf.common.ResultRow) ResultList(com.yahoo.dba.perf.myperf.common.ResultList) Statement(java.sql.Statement) Connection(java.sql.Connection) QueryParameters(com.yahoo.dba.perf.myperf.common.QueryParameters) DBCredential(com.yahoo.dba.perf.myperf.common.DBCredential) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Map(java.util.Map) DBInstanceInfo(com.yahoo.dba.perf.myperf.common.DBInstanceInfo)

Example 2 with DBCredential

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

the class ReplShowProcessor method querySingle.

@Override
public ResultList querySingle(MyPerfContext context, DBInstanceInfo dbinfo, String appUser, DBConnectionWrapper connWrapper, QueryParameters qps) throws SQLException {
    Map<String, ReplStatus> statusResults = new java.util.LinkedHashMap<String, ReplStatus>();
    DBCredential cred = context.getMetaDb().retrieveDBCredential(appUser, dbinfo.getDbGroupName());
    ReplServer replServer = new ReplServer(dbinfo.getHostName(), dbinfo.getPort());
    //check cache
    ReplServer startingServer = null;
    synchronized (this.topologyCache) {
        ReplTopologyCacheEntry cache = this.topologyCache.get(replServer.toString());
        if (cache != null && cache.createTime + context.getMyperfConfig().getReplTopologyCacheMaxAge() > System.currentTimeMillis())
            startingServer = cache.rootServer;
    }
    if (cred != null) {
        if (startingServer == null) {
            statusResults.put(replServer.toString(), new ReplStatus(dbinfo.getHostName(), dbinfo.getPort()));
            queryDetail(context, cred, statusResults, replServer, 0);
        } else {
            Map<String, ReplServer> allServers = startingServer.getAllServers();
            for (Map.Entry<String, ReplServer> e : allServers.entrySet()) {
                ReplServer s = e.getValue();
                ReplServer rplServer = new ReplServer(s.host, s.port);
                rplServer.probed = true;
                statusResults.put(rplServer.toString(), new ReplStatus(s.host, s.port));
                queryDetail(context, cred, statusResults, rplServer, 0);
            }
        }
    }
    ReplServer rootServer = this.buildReplTree(statusResults);
    if (rootServer == null) {
        logger.warning("Cannot find replication root for " + replServer);
    } else if (startingServer == null) {
        //update cache
        logger.info("Update cache");
        synchronized (this.topologyCache) {
            for (Map.Entry<String, ReplStatus> e : statusResults.entrySet()) {
                ReplTopologyCacheEntry cache = this.topologyCache.get(e.getKey());
                if (cache == null) {
                    cache = new ReplTopologyCacheEntry(rootServer);
                    this.topologyCache.put(e.getKey(), cache);
                } else
                    cache.update(rootServer);
            }
        }
    }
    ResultList rList = new ResultList();
    ColumnDescriptor desc = new ColumnDescriptor();
    rList.setColumnDescriptor(desc);
    int idx = 0;
    //use host:port
    desc.addColumn("SERVER", false, idx++);
    //use host:port
    desc.addColumn("MASTER SERVER", false, idx++);
    desc.addColumn("LAG", false, idx++);
    //IO/SQL
    desc.addColumn("IO/SQL", false, idx++);
    //Master file: master position
    desc.addColumn("MASTER: FILE", false, idx++);
    //Master file: master position
    desc.addColumn("MASTER LOG FILE", false, idx++);
    desc.addColumn("READ MASTER LOG POS", false, idx++);
    desc.addColumn("RELAY MASTER LOG FILE", false, idx++);
    desc.addColumn("EXEC MASTER LOG POS", false, idx++);
    desc.addColumn("MASTER EXECUTED GTID", false, idx++);
    desc.addColumn("EXECUTED GTID", false, idx++);
    Set<String> probed = new HashSet<String>();
    outputTree(rootServer, statusResults, probed, 0, rList);
    //}
    return rList;
}
Also used : ResultList(com.yahoo.dba.perf.myperf.common.ResultList) ColumnDescriptor(com.yahoo.dba.perf.myperf.common.ColumnDescriptor) DBCredential(com.yahoo.dba.perf.myperf.common.DBCredential) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 3 with DBCredential

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

the class MetaDB method retrieveDBCredential.

private DBCredential retrieveDBCredential(Connection conn, String owner, String dbGroupName) {
    String sql = (owner != null && owner.trim().length() > 0) ? "select * from " + CRED_TABLENAME + " where dbgroupname=?  and owner=?" : "select * from " + CRED_TABLENAME + " where dbgroupname=? and owner is null";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, dbGroupName.toLowerCase());
        if (owner != null && owner.trim().length() > 0)
            pstmt.setString(2, owner);
        rs = pstmt.executeQuery();
        if (rs != null && rs.next()) {
            DBCredential cred = new DBCredential();
            cred.setAppUser(owner);
            cred.setDbGroupName(rs.getString("DBGROUPNAME"));
            cred.setUsername(rs.getString("USERNAME"));
            cred.setAppUser(rs.getString("OWNER"));
            String credStringOrig = rs.getString("CREDENTIAL");
            String credString = keyTool.decrypt(credStringOrig);
            //we will use two :: as separator
            int verified = rs.getInt("VERIFIED");
            if (verified == 1) {
                //credString = credString.substring(0, credString.lastIndexOf("::"));
                //credString = credString.substring(credString.lastIndexOf("::")+2);
                //cred.setPassword(credString);
                cred.setEncrypted(credStringOrig);
                return cred;
            } else {
                cred.setPassword(credString);
                updateDBCredentialInternal(cred);
                return retrieveDBCredential(conn, owner, dbGroupName);
            }
        }
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Exception", ex);
    } finally {
        DBUtils.close(rs);
        DBUtils.close(pstmt);
    }
    return null;
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DBCredential(com.yahoo.dba.perf.myperf.common.DBCredential) SQLException(java.sql.SQLException)

Example 4 with DBCredential

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

the class GlobalVariableChangeScanTask method scanHost.

private ConfigBlock scanHost(DBInstanceInfo dbinfo) {
    boolean status = false;
    DBCredential cred = DBUtils.findDBCredential(context, dbinfo.getDbGroupName(), appUser);
    if (cred == null) {
        logger.info("No credential for cluster " + dbinfo.getDbGroupName() + ", skip it");
        //log the error
        return null;
    }
    logger.info("Scan for host (" + dbinfo + ") as user " + cred.getUsername());
    //find one connection is enough
    DBConnectionWrapper conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    ConfigBlock cb = null;
    try {
        conn = conns.checkoutConnection(dbinfo, cred);
        if (conn == null) {
            logger.info("Failed to access " + dbinfo + ", skip it");
            return null;
        }
        String sqlText = "select variable_name, variable_value from information_schema.global_variables";
        stmt = conn.getConnection().createStatement();
        rs = stmt.executeQuery(sqlText);
        cb = new ConfigBlock();
        while (rs != null && rs.next()) {
            String key = rs.getString("variable_name");
            //exclude a timestamp variable
            if ("TIMESTAMP".equalsIgnoreCase(key))
                continue;
            cb.addVariable(key, rs.getString("variable_value"));
        }
        Calendar c = Calendar.getInstance();
        Date dt = c.getTime();
        cb.setTime(sdf.format(dt));
    } catch (Exception ex) {
        logger.log(Level.WARNING, "exception", ex);
    } finally {
        DBUtils.close(rs);
        DBUtils.close(stmt);
        if (conn != null)
            conns.checkinConnectionAndClose(conn);
    }
    logger.info("Done configuration scan for host (" + dbinfo + ") as user " + cred.getUsername());
    return cb;
}
Also used : ConfigBlock(com.yahoo.dba.perf.myperf.common.ConfigBlock) Statement(java.sql.Statement) Calendar(java.util.Calendar) ResultSet(java.sql.ResultSet) DBConnectionWrapper(com.yahoo.dba.perf.myperf.db.DBConnectionWrapper) DBCredential(com.yahoo.dba.perf.myperf.common.DBCredential) Date(java.util.Date)

Example 5 with DBCredential

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

the class WebAppUtil method getDBConnection.

public static DBConnectionWrapper getDBConnection(HttpServletRequest req, MyPerfContext ctx, DBInstanceInfo dbinfo) {
    try {
        //get session
        HttpSession sess = req.getSession();
        //get connection manager
        UserDBConnections conns = UserDBConnections.class.cast(sess.getAttribute("UserDBConnections"));
        //if none, we need add one
        if (conns == null) {
            conns = new UserDBConnections();
            conns.setFrameworkContext(ctx);
            conns.setConnectionIdleTimeout(ctx.getConnectionIdleTime());
            //we don't have validated user, too
            conns.setAppUser(WebAppUtil.findUserFromRequest(req));
            sess.setAttribute("UserDBConnections", conns);
        }
        conns.closeExpired(ctx.getConnectionIdleTime());
        //first try to get connection without cred, meaning from saved
        DBConnectionWrapper connWrapper = conns.checkoutConnection(dbinfo, null);
        if (connWrapper != null) {
            //we will waste one query to check connectivity to avoid annoying error message
            if (DBUtils.checkConnection(connWrapper.getConnection()))
                return connWrapper;
            closeDBConnection(req, connWrapper, true, false);
            connWrapper = null;
        }
        //now get credential
        AppUser appUser = null;
        DBCredential cred = null;
        appUser = AppUser.class.cast(req.getSession().getAttribute(AppUser.SESSION_ATTRIBUTE));
        if (appUser == null)
            throw new RuntimeException("No user found. Session might not be valid.");
        cred = WebAppUtil.findDBCredential(ctx, dbinfo.getDbGroupName(), appUser);
        if (cred == null || cred.getPassword() == null)
            throw new RuntimeException("No valid credential provided for DB " + dbinfo.getDbGroupName());
        if (dbinfo.isConnectionVerified() || !dbinfo.supportClusterQuery()) {
            connWrapper = conns.checkoutConnection(dbinfo, cred);
            if (!DBUtils.checkConnection(connWrapper.getConnection())) {
                closeDBConnection(req, connWrapper, true, false);
                connWrapper = conns.checkoutConnection(dbinfo, cred);
            }
        }
        if (connWrapper == null && dbinfo.supportClusterQuery())
            connWrapper = conns.checkoutConnection(ctx.getDbInfoManager().findGroup(dbinfo.getDbGroupName()), cred);
        if (connWrapper == null)
            throw new RuntimeException("failed to connect to target db (" + dbinfo + ")");
        return connWrapper;
    } catch (Throwable th) {
        if (th instanceof RuntimeException)
            throw RuntimeException.class.cast(th);
        throw new RuntimeException(th);
    }
}
Also used : HttpSession(javax.servlet.http.HttpSession) AppUser(com.yahoo.dba.perf.myperf.common.AppUser) DBConnectionWrapper(com.yahoo.dba.perf.myperf.db.DBConnectionWrapper) UserDBConnections(com.yahoo.dba.perf.myperf.db.UserDBConnections) DBCredential(com.yahoo.dba.perf.myperf.common.DBCredential)

Aggregations

DBCredential (com.yahoo.dba.perf.myperf.common.DBCredential)6 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 ResultList (com.yahoo.dba.perf.myperf.common.ResultList)2 DBConnectionWrapper (com.yahoo.dba.perf.myperf.db.DBConnectionWrapper)2 Connection (java.sql.Connection)2 Statement (java.sql.Statement)2 Map (java.util.Map)2 AppUser (com.yahoo.dba.perf.myperf.common.AppUser)1 ColumnDescriptor (com.yahoo.dba.perf.myperf.common.ColumnDescriptor)1 ConfigBlock (com.yahoo.dba.perf.myperf.common.ConfigBlock)1 DBInstanceInfo (com.yahoo.dba.perf.myperf.common.DBInstanceInfo)1 QueryParameters (com.yahoo.dba.perf.myperf.common.QueryParameters)1 ResultRow (com.yahoo.dba.perf.myperf.common.ResultRow)1 UserDBConnections (com.yahoo.dba.perf.myperf.db.UserDBConnections)1 PreparedStatement (java.sql.PreparedStatement)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1