use of co.cask.cdap.explore.service.HandleNotFoundException in project cdap by caskdata.
the class ExploreStatement method execute.
/**
* Executes a query and wait until it is finished, but does not close the session.
*/
@Override
public boolean execute(String sql) throws SQLException {
if (isClosed) {
throw new SQLException("Can't execute after statement has been closed");
}
if (resultSet != null) {
// As requested by the Statement interface javadoc, "All execution methods in the Statement interface
// implicitly close a statement's current ResultSet object if an open one exists"
resultSet.close();
resultSet = null;
}
futureResults = exploreClient.submit(namespace, sql);
try {
resultSet = new ExploreResultSet(futureResults.get(), this, maxRows);
// Here we have a result, it may contain rows or may be empty, but it exists.
return true;
} catch (InterruptedException e) {
LOG.error("Caught exception", e);
Thread.currentThread().interrupt();
return false;
} catch (ExecutionException e) {
Throwable t = Throwables.getRootCause(e);
if (t instanceof HandleNotFoundException) {
LOG.error("Error executing query", e);
throw new SQLException("Unknown state");
}
LOG.error("Caught exception", e);
throw new SQLException(Throwables.getRootCause(e));
} catch (CancellationException e) {
// If futureResults has been cancelled
return false;
}
}
use of co.cask.cdap.explore.service.HandleNotFoundException in project cdap by caskdata.
the class BaseHiveExploreService method getQueries.
@Override
public List<QueryInfo> getQueries(NamespaceId namespace) throws ExploreException, SQLException {
startAndWait();
List<QueryInfo> result = new ArrayList<>();
String namespaceHiveDb = getHiveDatabase(namespace.getNamespace());
for (Map.Entry<QueryHandle, OperationInfo> entry : activeHandleCache.asMap().entrySet()) {
try {
if (namespaceHiveDb.equals(entry.getValue().getHiveDatabase())) {
// we use empty query statement for get tables, get schemas, we don't need to return it this method call.
if (!entry.getValue().getStatement().isEmpty()) {
QueryStatus status = getStatus(entry.getKey());
result.add(new QueryInfo(entry.getValue().getTimestamp(), entry.getValue().getStatement(), entry.getKey(), status, true));
}
}
} catch (HandleNotFoundException e) {
// ignore the handle not found exception. this method returns all queries and handle, if the
// handle is removed from the internal cache, then there is no point returning them from here.
}
}
for (Map.Entry<QueryHandle, InactiveOperationInfo> entry : inactiveHandleCache.asMap().entrySet()) {
InactiveOperationInfo inactiveOperationInfo = entry.getValue();
if (namespaceHiveDb.equals(inactiveOperationInfo.getHiveDatabase())) {
// we use empty query statement for get tables, get schemas, we don't need to return it this method call.
if (!inactiveOperationInfo.getStatement().isEmpty()) {
if (inactiveOperationInfo.getStatus() == null) {
LOG.error("Null status for query {}, handle {}", inactiveOperationInfo.getStatement(), entry.getKey());
}
result.add(new QueryInfo(inactiveOperationInfo.getTimestamp(), inactiveOperationInfo.getStatement(), entry.getKey(), inactiveOperationInfo.getStatus(), false));
}
}
}
Collections.sort(result);
return result;
}
use of co.cask.cdap.explore.service.HandleNotFoundException in project cdap by caskdata.
the class BaseHiveExploreService method fetchNextResults.
@SuppressWarnings("unchecked")
private List<QueryResult> fetchNextResults(QueryHandle handle, int size) throws HiveSQLException, ExploreException, HandleNotFoundException {
startAndWait();
Lock nextLock = getActiveOperationInfo(handle).getNextLock();
nextLock.lock();
try {
// Fetch results from Hive
LOG.trace("Getting results for handle {}", handle);
OperationHandle operationHandle = getOperationHandle(handle);
if (operationHandle.hasResultSet()) {
return doFetchNextResults(operationHandle, FetchOrientation.FETCH_NEXT, size);
} else {
return Collections.emptyList();
}
} catch (Exception e) {
throw Throwables.propagate(e);
} finally {
nextLock.unlock();
}
}
use of co.cask.cdap.explore.service.HandleNotFoundException in project cdap by caskdata.
the class BaseHiveExploreService method previewResults.
@Override
public List<QueryResult> previewResults(QueryHandle handle) throws ExploreException, HandleNotFoundException, SQLException {
startAndWait();
if (inactiveHandleCache.getIfPresent(handle) != null) {
throw new HandleNotFoundException("Query is inactive.", true);
}
OperationInfo operationInfo = getActiveOperationInfo(handle);
Lock previewLock = operationInfo.getPreviewLock();
previewLock.lock();
try {
File previewFile = operationInfo.getPreviewFile();
if (previewFile != null) {
try {
Reader reader = com.google.common.io.Files.newReader(previewFile, Charsets.UTF_8);
try {
return GSON.fromJson(reader, new TypeToken<List<QueryResult>>() {
}.getType());
} finally {
Closeables.closeQuietly(reader);
}
} catch (FileNotFoundException e) {
LOG.error("Could not retrieve preview result file {}", previewFile, e);
throw new ExploreException(e);
}
}
try {
// Create preview results for query
previewFile = new File(previewsDir, handle.getHandle());
try (FileWriter fileWriter = new FileWriter(previewFile)) {
List<QueryResult> results = fetchNextResults(handle, PREVIEW_COUNT);
GSON.toJson(results, fileWriter);
operationInfo.setPreviewFile(previewFile);
return results;
}
} catch (IOException e) {
LOG.error("Could not write preview results into file", e);
throw new ExploreException(e);
}
} finally {
previewLock.unlock();
}
}
use of co.cask.cdap.explore.service.HandleNotFoundException in project cdap by caskdata.
the class ExploreQueryExecutorHttpHandler method getQueryResultsSchema.
@GET
@Path("data/explore/queries/{id}/schema")
public void getQueryResultsSchema(HttpRequest request, HttpResponder responder, @PathParam("id") String id) throws ExploreException {
try {
final QueryHandle handle = QueryHandle.fromId(id);
List<ColumnDesc> schema;
if (!handle.equals(QueryHandle.NO_OP)) {
schema = doAs(handle, new Callable<List<ColumnDesc>>() {
@Override
public List<ColumnDesc> call() throws Exception {
return exploreService.getResultSchema(handle);
}
});
} else {
schema = Lists.newArrayList();
}
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(schema));
} catch (IllegalArgumentException e) {
LOG.debug("Got exception:", e);
responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
} catch (SQLException e) {
LOG.debug("Got exception:", e);
responder.sendString(HttpResponseStatus.BAD_REQUEST, String.format("[SQLState %s] %s", e.getSQLState(), e.getMessage()));
} catch (HandleNotFoundException e) {
responder.sendStatus(HttpResponseStatus.NOT_FOUND);
}
}
Aggregations