Example #1
0
 @Override
 public void close() throws IOException {
   for (QueryRunner runner : runners) {
     try {
       runner.close();
     } catch (Exception ignored) {
     }
   }
 }
Example #2
0
  private static void process(
      QueryRunner queryRunner, String sql, OutputFormat outputFormat, boolean interactive) {
    try (Query query = queryRunner.startQuery(sql)) {
      query.renderOutput(System.out, outputFormat, interactive);

      // update session properties if present
      if (!query.getSetSessionProperties().isEmpty()
          || !query.getResetSessionProperties().isEmpty()) {
        Map<String, String> sessionProperties =
            new HashMap<>(queryRunner.getSession().getProperties());
        sessionProperties.putAll(query.getSetSessionProperties());
        sessionProperties.keySet().removeAll(query.getResetSessionProperties());
        queryRunner.setSession(withProperties(queryRunner.getSession(), sessionProperties));
      }
    } catch (RuntimeException e) {
      System.out.println("Error running command: " + e.getMessage());
      if (queryRunner.getSession().isDebug()) {
        e.printStackTrace();
      }
    }
  }
Example #3
0
    public Duration executeCommands(int parallelism, List<String> queries) throws Exception {
      checkArgument(parallelism >= 0, "parallelism is negative");
      checkArgument(parallelism <= runners.size(), "parallelism is greater than maxParallelism");
      checkNotNull(queries, "queries is null");

      CountDownLatch remainingQueries = new CountDownLatch(queries.size());
      BlockingQueue<String> queue = new ArrayBlockingQueue<>(queries.size(), false, queries);

      List<ListenableFuture<?>> futures = new ArrayList<>(parallelism);
      long start = System.nanoTime();
      for (int i = 0; i < parallelism; i++) {
        QueryRunner runner = runners.get(i);
        futures.add(runner.execute(queue, remainingQueries));
      }

      // kill test if anything fails
      ListenableFuture<List<Object>> allFutures = Futures.allAsList(futures);
      Futures.addCallback(
          allFutures,
          new FutureCallback<List<Object>>() {
            @Override
            public void onSuccess(@Nullable List<Object> result) {}

            @Override
            public void onFailure(Throwable t) {
              System.err.println("Run failed");
              t.printStackTrace(System.err);
              System.exit(1);
            }
          },
          executor);

      remainingQueries.await();
      Duration executionTime = Duration.nanosSince(start);

      // wait for runners to spin-down
      allFutures.get();

      return executionTime;
    }
Example #4
0
  @Override
  public void run() {
    ClientSession session = clientOptions.toClientSession();
    boolean hasQuery = !Strings.isNullOrEmpty(clientOptions.execute);
    boolean isFromFile = !Strings.isNullOrEmpty(clientOptions.file);

    if (!hasQuery || !isFromFile) {
      AnsiConsole.systemInstall();
    }

    initializeLogging(session.isDebug());

    String query = clientOptions.execute;
    if (hasQuery) {
      query += ";";
    }

    if (isFromFile) {
      if (hasQuery) {
        throw new RuntimeException("both --execute and --file specified");
      }
      try {
        query = Files.toString(new File(clientOptions.file), UTF_8);
        hasQuery = true;
      } catch (IOException e) {
        throw new RuntimeException(
            format("Error reading from file %s: %s", clientOptions.file, e.getMessage()));
      }
    }

    try (QueryRunner queryRunner =
        QueryRunner.create(session, Optional.ofNullable(clientOptions.socksProxy))) {
      if (hasQuery) {
        executeCommand(queryRunner, query, clientOptions.outputFormat);
      } else {
        runConsole(queryRunner, session);
      }
    }
  }
Example #5
0
  private static void runConsole(QueryRunner queryRunner, ClientSession session) {
    try (TableNameCompleter tableNameCompleter = new TableNameCompleter(queryRunner);
        LineReader reader = new LineReader(getHistory(), tableNameCompleter)) {
      tableNameCompleter.populateCache();
      StringBuilder buffer = new StringBuilder();
      while (true) {
        // read a line of input from user
        String prompt = PROMPT_NAME + ":" + session.getSchema();
        if (buffer.length() > 0) {
          prompt = Strings.repeat(" ", prompt.length() - 1) + "-";
        }
        String line = reader.readLine(prompt + "> ");

        // add buffer to history and clear on user interrupt
        if (reader.interrupted()) {
          String partial = squeezeStatement(buffer.toString());
          if (!partial.isEmpty()) {
            reader.getHistory().add(partial);
          }
          buffer = new StringBuilder();
          continue;
        }

        // exit on EOF
        if (line == null) {
          System.out.println();
          return;
        }

        // check for special commands if this is the first line
        if (buffer.length() == 0) {
          String command = line.trim();
          if (command.endsWith(";")) {
            command = command.substring(0, command.length() - 1).trim();
          }
          switch (command.toLowerCase(ENGLISH)) {
            case "exit":
            case "quit":
              return;
            case "help":
              System.out.println();
              System.out.println(getHelpText());
              continue;
          }
        }

        // not a command, add line to buffer
        buffer.append(line).append("\n");

        // execute any complete statements
        String sql = buffer.toString();
        StatementSplitter splitter = new StatementSplitter(sql, ImmutableSet.of(";", "\\G"));
        for (Statement split : splitter.getCompleteStatements()) {
          Optional<Object> statement = getParsedStatement(split.statement());
          if (statement.isPresent() && isSessionParameterChange(statement.get())) {
            session = processSessionParameterChange(statement.get(), session);
            queryRunner.setSession(session);
            tableNameCompleter.populateCache();
          } else {
            OutputFormat outputFormat = OutputFormat.ALIGNED;
            if (split.terminator().equals("\\G")) {
              outputFormat = OutputFormat.VERTICAL;
            }

            process(queryRunner, split.statement(), outputFormat, true);
          }
          reader.getHistory().add(squeezeStatement(split.statement()) + split.terminator());
        }

        // replace buffer with trailing partial statement
        buffer = new StringBuilder();
        String partial = splitter.getPartialStatement();
        if (!partial.isEmpty()) {
          buffer.append(partial).append('\n');
        }
      }
    } catch (IOException e) {
      System.err.println("Readline error: " + e.getMessage());
    }
  }
  @Override
  public Sequence<T> run(final Query<T> query, final Map<String, Object> responseContext) {
    final Sequence<T> baseSequence = delegate.run(query, responseContext);
    return Sequences.withEffect(
        new Sequence<T>() {
          @Override
          public <OutType> OutType accumulate(
              OutType initValue, Accumulator<OutType, T> accumulator) {
            final long start = VMUtils.getCurrentThreadCpuTime();
            try {
              return baseSequence.accumulate(initValue, accumulator);
            } finally {
              cpuTimeAccumulator.addAndGet(VMUtils.getCurrentThreadCpuTime() - start);
            }
          }

          @Override
          public <OutType> Yielder<OutType> toYielder(
              OutType initValue, YieldingAccumulator<OutType, T> accumulator) {
            final Yielder<OutType> delegateYielder = baseSequence.toYielder(initValue, accumulator);
            return new Yielder<OutType>() {
              @Override
              public OutType get() {
                final long start = VMUtils.getCurrentThreadCpuTime();
                try {
                  return delegateYielder.get();
                } finally {
                  cpuTimeAccumulator.addAndGet(VMUtils.getCurrentThreadCpuTime() - start);
                }
              }

              @Override
              public Yielder<OutType> next(OutType initValue) {
                final long start = VMUtils.getCurrentThreadCpuTime();
                try {
                  return delegateYielder.next(initValue);
                } finally {
                  cpuTimeAccumulator.addAndGet(VMUtils.getCurrentThreadCpuTime() - start);
                }
              }

              @Override
              public boolean isDone() {
                return delegateYielder.isDone();
              }

              @Override
              public void close() throws IOException {
                delegateYielder.close();
              }
            };
          }
        },
        new Runnable() {
          @Override
          public void run() {
            if (report) {
              final long cpuTime = cpuTimeAccumulator.get();
              if (cpuTime > 0) {
                final ServiceMetricEvent.Builder builder =
                    Preconditions.checkNotNull(builderFn.apply(query));
                builder.setDimension(DruidMetrics.ID, Strings.nullToEmpty(query.getId()));
                emitter.emit(builder.build("query/cpu/time", cpuTimeAccumulator.get() / 1000));
              }
            }
          }
        },
        MoreExecutors.sameThreadExecutor());
  }
 public void show() {
   sDialogShown = true;
   // TODO: use a loader instead
   mRunner.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
 }