Search in sources :

Example 1 with HandleNotFoundException

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;
    }
}
Also used : HandleNotFoundException(co.cask.cdap.explore.service.HandleNotFoundException) SQLException(java.sql.SQLException) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with HandleNotFoundException

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;
}
Also used : HandleNotFoundException(co.cask.cdap.explore.service.HandleNotFoundException) ArrayList(java.util.ArrayList) QueryInfo(co.cask.cdap.proto.QueryInfo) QueryHandle(co.cask.cdap.proto.QueryHandle) Map(java.util.Map) HashMap(java.util.HashMap) QueryStatus(co.cask.cdap.proto.QueryStatus)

Example 3 with HandleNotFoundException

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();
    }
}
Also used : OperationHandle(org.apache.hive.service.cli.OperationHandle) SQLException(java.sql.SQLException) TableNotFoundException(co.cask.cdap.explore.service.TableNotFoundException) TException(org.apache.thrift.TException) IOException(java.io.IOException) NoSuchObjectException(org.apache.hadoop.hive.metastore.api.NoSuchObjectException) TransactionFailureException(org.apache.tephra.TransactionFailureException) HandleNotFoundException(co.cask.cdap.explore.service.HandleNotFoundException) MetaException(org.apache.hadoop.hive.metastore.api.MetaException) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) FileNotFoundException(java.io.FileNotFoundException) ExploreException(co.cask.cdap.explore.service.ExploreException) HiveSQLException(org.apache.hive.service.cli.HiveSQLException) Lock(java.util.concurrent.locks.Lock)

Example 4 with HandleNotFoundException

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();
    }
}
Also used : HandleNotFoundException(co.cask.cdap.explore.service.HandleNotFoundException) QueryResult(co.cask.cdap.proto.QueryResult) TypeToken(com.google.common.reflect.TypeToken) FileWriter(java.io.FileWriter) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) IOException(java.io.IOException) File(java.io.File) Lock(java.util.concurrent.locks.Lock) ExploreException(co.cask.cdap.explore.service.ExploreException)

Example 5 with HandleNotFoundException

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);
    }
}
Also used : HandleNotFoundException(co.cask.cdap.explore.service.HandleNotFoundException) SQLException(java.sql.SQLException) QueryHandle(co.cask.cdap.proto.QueryHandle) ColumnDesc(co.cask.cdap.proto.ColumnDesc) Callable(java.util.concurrent.Callable) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

HandleNotFoundException (co.cask.cdap.explore.service.HandleNotFoundException)12 SQLException (java.sql.SQLException)10 QueryHandle (co.cask.cdap.proto.QueryHandle)8 Path (javax.ws.rs.Path)7 ExploreException (co.cask.cdap.explore.service.ExploreException)5 QueryResult (co.cask.cdap.proto.QueryResult)5 Callable (java.util.concurrent.Callable)5 IOException (java.io.IOException)4 POST (javax.ws.rs.POST)4 QueryStatus (co.cask.cdap.proto.QueryStatus)3 ColumnDesc (co.cask.cdap.proto.ColumnDesc)2 FileNotFoundException (java.io.FileNotFoundException)2 CancellationException (java.util.concurrent.CancellationException)2 ExecutionException (java.util.concurrent.ExecutionException)2 Lock (java.util.concurrent.locks.Lock)2 GET (javax.ws.rs.GET)2 RowMaker (co.cask.cdap.cli.util.RowMaker)1 Table (co.cask.cdap.cli.util.table.Table)1 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)1 ExploreExecutionResult (co.cask.cdap.explore.client.ExploreExecutionResult)1