/** * Executes the given Azkaban related SELECT SQL operations. it will call * {@link AzkabanDataSource#getConnection()} inside queryrunner.query. * * @param sqlQuery The SQL query statement to execute. * @param resultHandler The handler used to create the result object * @param params Initialize the PreparedStatement's IN parameters * @param <T> The type of object that the qeury handler returns * @return The object returned by the handler. */ public <T> T query(final String baseQuery, final ResultSetHandler<T> resultHandler, final Object... params) throws SQLException { try { return this.queryRunner.query(baseQuery, resultHandler, params); } catch (final SQLException ex) { // todo kunkun-tang: Retry logics should be implemented here. logger.error("query failed", ex); if (this.dbMetrics != null) { this.dbMetrics.markDBFailQuery(); } throw ex; } }
/** * Executes the given AZ related INSERT, UPDATE, or DELETE SQL statement. * it will call {@link AzkabanDataSource#getConnection()} inside * queryrunner.update. * * @param updateClause sql statements to execute * @param params Initialize the PreparedStatement's IN parameters * @return The number of rows updated. */ public int update(final String updateClause, final Object... params) throws SQLException { try { return this.queryRunner.update(updateClause, params); } catch (final SQLException ex) { // todo kunkun-tang: Retry logics should be implemented here. logger.error("update failed", ex); if (this.dbMetrics != null) { this.dbMetrics.markDBFailUpdate(); } throw ex; } }
@Inject public DBMetrics(final MetricsManager metricsManager) { this.metricsManager = metricsManager; setupAllMetrics(); }
public Connection getConnection() throws SQLException { this.dbMetrics.markDBConnection(); final long startMs = System.currentTimeMillis(); Connection connection = null; this.dbMetrics.setDBConnectionTime(System.currentTimeMillis() - startMs); return connection; this.dbMetrics.markDBFailConnection(); try { invalidateConnection(connection);
@Test public void testDBConnectionTimeMetrics() { this.metrics.setDBConnectionTime(14); assertEquals(14, this.testUtil.getGaugeValue("dbConnectionTime")); } }
/** * Provide a way to allow users define custom SQL operations without relying on fixed SQL * interface. The common use case is to group a sequence of SQL operations without commit every * time. * * @param operations A sequence of DB operations * @param <T> The type of object that the operations returns. Note that T could be null * @return T The object returned by the SQL statement, expected by the caller */ public <T> T transaction(final SQLTransaction<T> operations) throws SQLException { Connection conn = null; try { conn = this.queryRunner.getDataSource().getConnection(); conn.setAutoCommit(false); final DatabaseTransOperator transOperator = new DatabaseTransOperator(this.queryRunner, conn); final T res = operations.execute(transOperator); conn.commit(); return res; } catch (final SQLException ex) { // todo kunkun-tang: Retry logics should be implemented here. logger.error("transaction failed", ex); if (this.dbMetrics != null) { this.dbMetrics.markDBFailTransaction(); } throw ex; } finally { DbUtils.closeQuietly(conn); } }
@Before public void setUp() { final MetricRegistry metricRegistry = new MetricRegistry(); this.testUtil = new MetricsTestUtility(metricRegistry); this.metrics = new DBMetrics(new MetricsManager(metricRegistry)); }