Search in sources :

Example 1 with TimeLimiter

use of com.google.common.util.concurrent.TimeLimiter in project presto by prestodb.

the class Validator method executeQuery.

private QueryResult executeQuery(String url, String username, String password, Query query, String sql, Duration timeout, Map<String, String> sessionProperties) {
    String queryId = null;
    try (Connection connection = DriverManager.getConnection(url, username, password)) {
        trySetConnectionProperties(query, connection);
        for (Map.Entry<String, String> entry : sessionProperties.entrySet()) {
            connection.unwrap(PrestoConnection.class).setSessionProperty(entry.getKey(), entry.getValue());
        }
        try (Statement statement = connection.createStatement()) {
            TimeLimiter limiter = new SimpleTimeLimiter();
            Stopwatch stopwatch = Stopwatch.createStarted();
            Statement limitedStatement = limiter.newProxy(statement, Statement.class, timeout.toMillis(), TimeUnit.MILLISECONDS);
            if (explainOnly) {
                sql = "EXPLAIN " + sql;
            }
            long start = System.nanoTime();
            PrestoStatement prestoStatement = limitedStatement.unwrap(PrestoStatement.class);
            ProgressMonitor progressMonitor = new ProgressMonitor();
            prestoStatement.setProgressMonitor(progressMonitor);
            try {
                boolean isSelectQuery = limitedStatement.execute(sql);
                List<List<Object>> results = null;
                if (isSelectQuery) {
                    results = limiter.callWithTimeout(getResultSetConverter(limitedStatement.getResultSet()), timeout.toMillis() - stopwatch.elapsed(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS, true);
                } else {
                    results = ImmutableList.of(ImmutableList.of(limitedStatement.getLargeUpdateCount()));
                }
                prestoStatement.clearProgressMonitor();
                QueryStats queryStats = progressMonitor.getFinalQueryStats();
                if (queryStats == null) {
                    throw new VerifierException("Cannot fetch query stats");
                }
                Duration queryCpuTime = new Duration(queryStats.getCpuTimeMillis(), TimeUnit.MILLISECONDS);
                queryId = queryStats.getQueryId();
                return new QueryResult(State.SUCCESS, null, nanosSince(start), queryCpuTime, queryId, results);
            } catch (AssertionError e) {
                if (e.getMessage().startsWith("unimplemented type:")) {
                    return new QueryResult(State.INVALID, null, null, null, queryId, ImmutableList.of());
                }
                throw e;
            } catch (SQLException | VerifierException e) {
                throw e;
            } catch (UncheckedTimeoutException e) {
                return new QueryResult(State.TIMEOUT, null, null, null, queryId, ImmutableList.of());
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw Throwables.propagate(e);
            } catch (Exception e) {
                throw Throwables.propagate(e);
            }
        }
    } catch (SQLException e) {
        Exception exception = e;
        if (("Error executing query".equals(e.getMessage()) || "Error fetching results".equals(e.getMessage())) && (e.getCause() instanceof Exception)) {
            exception = (Exception) e.getCause();
        }
        State state = isPrestoQueryInvalid(e) ? State.INVALID : State.FAILED;
        return new QueryResult(state, exception, null, null, null, null);
    } catch (VerifierException e) {
        return new QueryResult(State.TOO_MANY_ROWS, e, null, null, null, null);
    }
}
Also used : SQLException(java.sql.SQLException) Stopwatch(com.google.common.base.Stopwatch) UncheckedTimeoutException(com.google.common.util.concurrent.UncheckedTimeoutException) PrestoStatement(com.facebook.presto.jdbc.PrestoStatement) Collections.unmodifiableList(java.util.Collections.unmodifiableList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter) TimeLimiter(com.google.common.util.concurrent.TimeLimiter) PrestoStatement(com.facebook.presto.jdbc.PrestoStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) PrestoConnection(com.facebook.presto.jdbc.PrestoConnection) Duration(io.airlift.units.Duration) PrestoConnection(com.facebook.presto.jdbc.PrestoConnection) SQLClientInfoException(java.sql.SQLClientInfoException) SQLException(java.sql.SQLException) UncheckedTimeoutException(com.google.common.util.concurrent.UncheckedTimeoutException) QueryStats(com.facebook.presto.jdbc.QueryStats) State(com.facebook.presto.verifier.QueryResult.State) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Map(java.util.Map) SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter)

Example 2 with TimeLimiter

use of com.google.common.util.concurrent.TimeLimiter in project symja_android_library by axkr.

the class ExprEvaluator method evaluateWithTimeout.

/**
	 * <p>
	 * Parse the given <code>expression String</code> and evaluate it to an IExpr value.
	 * </p>
	 * 
	 * @param inputExpression
	 *            the Symja input expression
	 * @param timeoutDuration
	 *            with timeoutUnit, the maximum length of time to wait
	 * @param timeUnit
	 *            with timeoutDuration, the maximum length of time to wait
	 * @param interruptible
	 *            whether to respond to thread interruption by aborting the operation and throwing InterruptedException;
	 *            if false, the operation is allowed to complete or time out, and the current thread's interrupt status
	 *            is re-asserted.
	 * @return
	 * @throws SyntaxError
	 */
public IExpr evaluateWithTimeout(final String inputExpression, long timeoutDuration, TimeUnit timeUnit, boolean interruptible) {
    if (inputExpression != null) {
        EvalEngine.set(engine);
        engine.reset();
        fExpr = engine.parse(inputExpression);
        if (fExpr != null) {
            F.join();
            TimeLimiter timeLimiter = new SimpleTimeLimiter();
            Callable<IExpr> work = new EvalCallable(fExpr, engine);
            try {
                return timeLimiter.callWithTimeout(work, timeoutDuration, timeUnit, interruptible);
            } catch (java.util.concurrent.TimeoutException e) {
                return F.Aborted;
            } catch (com.google.common.util.concurrent.UncheckedTimeoutException e) {
                return F.Aborted;
            } catch (Exception e) {
                if (Config.DEBUG) {
                    e.printStackTrace();
                }
                return F.Null;
            }
        }
    }
    return null;
}
Also used : SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter) TimeLimiter(com.google.common.util.concurrent.TimeLimiter) IExpr(org.matheclipse.core.interfaces.IExpr) SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter) MathException(org.matheclipse.parser.client.math.MathException)

Example 3 with TimeLimiter

use of com.google.common.util.concurrent.TimeLimiter in project symja_android_library by axkr.

the class TimeConstrained method evaluate.

@Override
public IExpr evaluate(final IAST ast, EvalEngine engine) {
    Validate.checkRange(ast, 3, 4);
    IExpr arg2 = engine.evaluate(ast.arg2());
    long seconds = 0L;
    try {
        if (arg2.isSignedNumber()) {
            seconds = ((ISignedNumber) arg2).toLong();
        } else {
            engine.printMessage("TimeConstrained: " + ast.arg2().toString() + " is not a Java long value.");
            return F.NIL;
        }
    } catch (ArithmeticException ae) {
        engine.printMessage("TimeConstrained: " + ast.arg2().toString() + " is not a Java long value.");
        return F.NIL;
    }
    if (Config.JAS_NO_THREADS) {
        // no thread can be spawned
        try {
            return engine.evaluate(ast.arg1());
        } catch (final MathException e) {
            throw e;
        } catch (final Throwable th) {
            if (ast.isAST3()) {
                return ast.arg3();
            }
        }
        return F.Aborted;
    } else {
        TimeLimiter timeLimiter = new SimpleTimeLimiter();
        Callable<IExpr> work = new EvalCallable(ast.arg1(), engine);
        try {
            return timeLimiter.callWithTimeout(work, seconds, TimeUnit.SECONDS, true);
        } catch (java.util.concurrent.TimeoutException e) {
            if (ast.isAST3()) {
                return ast.arg3();
            }
            return F.Aborted;
        } catch (com.google.common.util.concurrent.UncheckedTimeoutException e) {
            if (ast.isAST3()) {
                return ast.arg3();
            }
            return F.Aborted;
        } catch (Exception e) {
            if (Config.DEBUG) {
                e.printStackTrace();
            }
            return F.Null;
        }
    }
}
Also used : SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter) TimeLimiter(com.google.common.util.concurrent.TimeLimiter) MathException(org.matheclipse.parser.client.math.MathException) MathException(org.matheclipse.parser.client.math.MathException) IExpr(org.matheclipse.core.interfaces.IExpr) SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter)

Example 4 with TimeLimiter

use of com.google.common.util.concurrent.TimeLimiter in project presto by prestodb.

the class QueryRewriter method getColumns.

private List<Column> getColumns(Connection connection, CreateTableAsSelect createTableAsSelect) throws SQLException {
    com.facebook.presto.sql.tree.Query createSelectClause = createTableAsSelect.getQuery();
    // Rewrite the query to select zero rows, so that we can get the column names and types
    QueryBody innerQuery = createSelectClause.getQueryBody();
    com.facebook.presto.sql.tree.Query zeroRowsQuery;
    if (innerQuery instanceof QuerySpecification) {
        QuerySpecification querySpecification = (QuerySpecification) innerQuery;
        innerQuery = new QuerySpecification(querySpecification.getSelect(), querySpecification.getFrom(), querySpecification.getWhere(), querySpecification.getGroupBy(), querySpecification.getHaving(), querySpecification.getOrderBy(), Optional.of("0"));
        zeroRowsQuery = new com.facebook.presto.sql.tree.Query(createSelectClause.getWith(), innerQuery, Optional.empty(), Optional.empty());
    } else {
        zeroRowsQuery = new com.facebook.presto.sql.tree.Query(createSelectClause.getWith(), innerQuery, Optional.empty(), Optional.of("0"));
    }
    ImmutableList.Builder<Column> columns = ImmutableList.builder();
    try (java.sql.Statement jdbcStatement = connection.createStatement()) {
        TimeLimiter limiter = new SimpleTimeLimiter();
        java.sql.Statement limitedStatement = limiter.newProxy(jdbcStatement, java.sql.Statement.class, timeout.toMillis(), TimeUnit.MILLISECONDS);
        try (ResultSet resultSet = limitedStatement.executeQuery(formatSql(zeroRowsQuery, Optional.empty()))) {
            ResultSetMetaData metaData = resultSet.getMetaData();
            for (int i = 1; i <= metaData.getColumnCount(); i++) {
                String name = metaData.getColumnName(i);
                int type = metaData.getColumnType(i);
                columns.add(new Column(name, APPROXIMATE_TYPES.contains(type)));
            }
        }
    }
    return columns.build();
}
Also used : SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter) TimeLimiter(com.google.common.util.concurrent.TimeLimiter) ImmutableList(com.google.common.collect.ImmutableList) ResultSetMetaData(java.sql.ResultSetMetaData) QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) SingleColumn(com.facebook.presto.sql.tree.SingleColumn) ResultSet(java.sql.ResultSet) QueryBody(com.facebook.presto.sql.tree.QueryBody) SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter)

Example 5 with TimeLimiter

use of com.google.common.util.concurrent.TimeLimiter in project etcd-java by IBM.

the class EtcdTestSuite method waitForStartup.

static void waitForStartup() throws Exception {
    if (etcdProcess == null)
        return;
    TimeLimiter tl = new SimpleTimeLimiter();
    tl.callWithTimeout(() -> {
        Reader isr = new InputStreamReader(etcdProcess.getErrorStream());
        BufferedReader br = new BufferedReader(isr);
        String line;
        while ((line = br.readLine()) != null && !line.contains("ready to serve client requests")) {
            System.out.println(line);
        }
        return null;
    }, 10L, TimeUnit.SECONDS, true);
}
Also used : SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter) TimeLimiter(com.google.common.util.concurrent.TimeLimiter) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) SimpleTimeLimiter(com.google.common.util.concurrent.SimpleTimeLimiter)

Aggregations

SimpleTimeLimiter (com.google.common.util.concurrent.SimpleTimeLimiter)11 TimeLimiter (com.google.common.util.concurrent.TimeLimiter)11 ImmutableList (com.google.common.collect.ImmutableList)2 Scope (io.opencensus.common.Scope)2 IOException (java.io.IOException)2 ExecutorService (java.util.concurrent.ExecutorService)2 TimeoutException (java.util.concurrent.TimeoutException)2 IExpr (org.matheclipse.core.interfaces.IExpr)2 MathException (org.matheclipse.parser.client.math.MathException)2 BoundedExecutor (com.facebook.airlift.concurrent.BoundedExecutor)1 ExecutorServiceAdapter (com.facebook.airlift.concurrent.ExecutorServiceAdapter)1 PrestoConnection (com.facebook.presto.jdbc.PrestoConnection)1 PrestoStatement (com.facebook.presto.jdbc.PrestoStatement)1 QueryStats (com.facebook.presto.jdbc.QueryStats)1 QueryBody (com.facebook.presto.sql.tree.QueryBody)1 QuerySpecification (com.facebook.presto.sql.tree.QuerySpecification)1 SingleColumn (com.facebook.presto.sql.tree.SingleColumn)1 State (com.facebook.presto.verifier.QueryResult.State)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Stopwatch (com.google.common.base.Stopwatch)1