private Object getValue(Monitor<?> monitor, boolean reset) {
   try {
     if (limiter != null) {
       final MonitorValueCallable c = new MonitorValueCallable(monitor);
       return limiter.callWithTimeout(c, 1, TimeUnit.SECONDS, true);
     } else {
       return monitor.getValue();
     }
   } catch (UncheckedTimeoutException e) {
     LOGGER.warn("timeout trying to get value for {}", monitor.getConfig());
   } catch (Exception e) {
     LOGGER.warn("failed to get value for " + monitor.getConfig(), e);
   }
   return null;
 }
 private static <T> T timeLimited(
     T target, Class<T> clazz, Duration timeout, ExecutorService executor) {
   TimeLimiter limiter = new SimpleTimeLimiter(executor);
   return limiter.newProxy(target, clazz, timeout.toMillis(), MILLISECONDS);
 }
Example #3
0
  private QueryResult executeQuery(
      String url,
      String username,
      String password,
      Query query,
      String sql,
      Duration timeout,
      Map<String, String> sessionProperties) {
    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());
      }
      long start = System.nanoTime();

      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;
        }
        try {
          if (limitedStatement.execute(sql)) {
            List<List<Object>> results =
                limiter.callWithTimeout(
                    getResultSetConverter(limitedStatement.getResultSet()),
                    timeout.toMillis() - stopwatch.elapsed(TimeUnit.MILLISECONDS),
                    TimeUnit.MILLISECONDS,
                    true);
            return new QueryResult(State.SUCCESS, null, nanosSince(start), results);
          } else {
            return new QueryResult(State.SUCCESS, null, nanosSince(start), null);
          }
        } catch (AssertionError e) {
          if (e.getMessage().startsWith("unimplemented type:")) {
            return new QueryResult(State.INVALID, null, null, ImmutableList.<List<Object>>of());
          }
          throw e;
        } catch (SQLException | VerifierException e) {
          throw e;
        } catch (UncheckedTimeoutException e) {
          return new QueryResult(State.TIMEOUT, null, null, ImmutableList.<List<Object>>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);
    } catch (VerifierException e) {
      return new QueryResult(State.TOO_MANY_ROWS, e, null, null);
    }
  }