Skip to content
This repository was archived by the owner on May 12, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -633,14 +633,7 @@ public void validate(AbstractQueryContext pContext) throws LensException {
PreparedStatement stmt;
// Estimate queries need to get connection from estimate pool to make sure
// we are not blocked by data queries.
stmt = prepareInternal(pContext, true, true, "validate-");
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
throw new LensException();
}
}
prepareInternal(pContext, true, true, "validate-");
}
}

Expand Down Expand Up @@ -707,12 +700,12 @@ protected final ConnectionProvider getConnectionProvider() {
* @return prepared statement of the query
* @throws LensException
*/
private PreparedStatement prepareInternal(AbstractQueryContext pContext) throws LensException {
private void prepareInternal(AbstractQueryContext pContext) throws LensException {
if (pContext.getDriverQuery(this) == null) {
throw new NullPointerException("Null driver query for " + pContext.getUserQuery());
}
checkConfigured();
return prepareInternal(pContext, false, false, "prepare-");
prepareInternal(pContext, false, false, "prepare-");
}


Expand All @@ -725,7 +718,7 @@ private PreparedStatement prepareInternal(AbstractQueryContext pContext) throws
* @return prepared statement
* @throws LensException
*/
private PreparedStatement prepareInternal(AbstractQueryContext pContext,
private void prepareInternal(AbstractQueryContext pContext,
boolean calledForEstimate,
boolean checkConfigured,
String metricCallStack) throws LensException {
Expand All @@ -742,33 +735,8 @@ private PreparedStatement prepareInternal(AbstractQueryContext pContext,
MethodMetricsContext sqlRewriteGauge = MethodMetricsFactory.createMethodGauge(pContext.getDriverConf(this), true,
metricCallStack + COLUMNAR_SQL_REWRITE_GAUGE);
String rewrittenQuery = rewriteQuery(pContext);
pContext.setSelectedDriverQuery(rewrittenQuery);
sqlRewriteGauge.markSuccess();
MethodMetricsContext jdbcPrepareGauge = MethodMetricsFactory.createMethodGauge(pContext.getDriverConf(this), true,
metricCallStack + JDBC_PREPARE_GAUGE);

PreparedStatement stmt = null;
Connection conn = null;
try {
conn = calledForEstimate ? getEstimateConnection() : getConnection();
stmt = conn.prepareStatement(rewrittenQuery);
if (!pContext.getDriverConf(this).getBoolean(JDBC_VALIDATE_SKIP_WARNINGS,
DEFAULT_JDBC_VALIDATE_SKIP_WARNINGS) && stmt.getWarnings() != null) {
throw new LensException(stmt.getWarnings());
}
} catch (SQLException sql) {
handleJDBCSQLException(sql);
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
log.error("Error closing connection: {}", rewrittenQuery, e);
}
}
jdbcPrepareGauge.markSuccess();
}
log.info("Prepared: {}", rewrittenQuery);
return stmt;
}

/**
Expand Down Expand Up @@ -807,10 +775,7 @@ public void prepare(PreparedQueryContext pContext) throws LensException {
// already prepared
return;
}
PreparedStatement stmt = prepareInternal(pContext);
if (stmt != null) {
preparedQueries.put(pContext.getPrepareHandle(), stmt);
}
prepareInternal(pContext);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import org.apache.lens.server.api.query.QueryContext;
import org.apache.lens.server.api.query.cost.QueryCost;
import org.apache.lens.server.api.query.cost.StaticQueryCost;
import org.apache.lens.server.api.util.LensUtil;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.conf.HiveConf;
Expand Down Expand Up @@ -294,23 +293,7 @@ public void testEstimate() throws Exception {
}

/**
* Test estimate failing
*
* @throws Exception the exception
*/
@Test
public void testEstimateFailing() throws Exception {
String query2 = "SELECT * FROM estimate_test2"; // Select query against non existing table
try {
driver.estimate(createExplainContext(query2, baseConf));
Assert.fail("Running estimate on a non existing table.");
} catch (LensException ex) {
Assert.assertEquals(LensUtil.getCauseMessage(ex), "user lacks privilege or object not found: ESTIMATE_TEST2");
}
}

/**
* Test estimate failing
* Test estimate guages
*
* @throws Exception the exception
*/
Expand All @@ -326,7 +309,6 @@ public void testEstimateGauges() throws Exception {
String driverQualifiledName = driver.getFullyQualifiedName();
Assert.assertTrue(reg.getGauges().keySet().containsAll(Arrays.asList(
"lens.MethodMetricGauge.TestJdbcDriver-"+driverQualifiledName+"-validate-columnar-sql-rewrite",
"lens.MethodMetricGauge.TestJdbcDriver-"+driverQualifiledName+"-validate-jdbc-prepare-statement",
"lens.MethodMetricGauge.TestJdbcDriver-"+driverQualifiledName+"-validate-thru-prepare",
"lens.MethodMetricGauge.TestJdbcDriver-"+driverQualifiledName+"-jdbc-check-allowed-query")));
}
Expand Down Expand Up @@ -359,7 +341,11 @@ public void testMetricsEnabled() throws Exception {
cost = driver.estimate(pContext2);
Assert.assertEquals(cost, JDBC_COST);
driver.prepare(pContext2);
driver.explainAndPrepare(pContext2);

PreparedQueryContext pContext3 = new PreparedQueryContext(query1, "SA", metricConf, drivers);
cost = driver.estimate(pContext3);
Assert.assertEquals(cost, JDBC_COST);
driver.explainAndPrepare(pContext3);
}

/**
Expand Down Expand Up @@ -653,23 +639,6 @@ public void testPrepare() throws Exception {
driver.prepare(pContext);
}

/**
* Test prepare failing
*
* @throws Exception the exception
*/
@Test
public void testPrepareFailing() throws Exception {
String query = "SELECT * FROM prepare_test2"; // Select query against non existing table
try {
PreparedQueryContext pContext = new PreparedQueryContext(query, "SA", baseConf, drivers);
driver.prepare(pContext);
Assert.fail("Running prepare on a non existing table.");
} catch (LensException ex) {
Assert.assertEquals(LensUtil.getCauseMessage(ex), "user lacks privilege or object not found: PREPARE_TEST2");
}
}

/**
* Test prepare skip warnings
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public class PreparedQueryContext extends AbstractQueryContext implements Delaye
/**
* The millis in week.
*/
private static long millisInWeek = 7 * 24 * 60 * 60 * 1000;
private static long millisInTenMinutes = 10 * 60 * 1000;

/**
* Instantiates a new prepared query context.
Expand Down Expand Up @@ -129,7 +129,7 @@ public long getDelay(TimeUnit units) {
if (this.prepareStartTime != null) {
Date now = new Date();
long elapsedMills = now.getTime() - this.prepareStartTime.getTime();
delayMillis = millisInWeek - elapsedMills;
delayMillis = millisInTenMinutes - elapsedMills;
return units.convert(delayMillis, TimeUnit.MILLISECONDS);
} else {
return Integer.MAX_VALUE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void createPreparedQueriesTable() throws Exception {
log.warn("Unable to create prepared_queries queries table", e);
}
}

public void createFailedAttemptsTable() throws Exception {
String sql = "CREATE TABLE if not exists failed_attempts (handle varchar(255) not null,"
+ "attempt_number int, drivername varchar(10000), progress float, progressmessage varchar(10000), "
Expand Down Expand Up @@ -845,8 +845,9 @@ public boolean deleteActiveSession(LensSessionHandle sessionId) throws LensExcep
* @throws SQLException the exception
*/
public void insertPreparedQuery(PreparedQueryContext preparedQueryContext) throws LensException {
String sql = "insert into prepared_queries (handle, userquery, submitter, timetaken, queryname, drivername, "
+ "driverquery, starttime)" + " values (?,?,?,?,?,?,?,?)";
String sql =
"insert into prepared_queries (handle, userquery, submitter, timetaken, queryname, drivername, "
+ "driverquery, starttime)" + " values (?,?,?,?,?,?,?,?)";
Connection conn = null;
try {
conn = getConnection();
Expand All @@ -864,7 +865,7 @@ public void insertPreparedQuery(PreparedQueryContext preparedQueryContext) throw
} catch (SQLException e) {
log.error("Failed to insert prepared query into database with error, " + e);
throw new LensException(e);
} finally {
} finally {
DbUtils.closeQuietly(conn);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2057,6 +2057,7 @@ public QueryPrepareHandle prepare(LensSessionHandle sessionHandle, String query,
acquire(sessionHandle);
prepared = prepareQuery(sessionHandle, query, lensConf, SubmitOp.PREPARE);
prepared.setQueryName(queryName);
prepared.getSelectedDriver().prepare(prepared);
lensServerDao.insertPreparedQuery(prepared);
return prepared.getPrepareHandle();
} catch (LensException e) {
Expand Down