Пример #1
0
  @Inject
  public SqlQueryManager(
      QueryManagerConfig config,
      QueryMonitor queryMonitor,
      QueryIdGenerator queryIdGenerator,
      LocationFactory locationFactory,
      Map<Class<? extends Statement>, QueryExecutionFactory<?>> executionFactories) {
    checkNotNull(config, "config is null");

    this.executionFactories = checkNotNull(executionFactories, "executionFactories is null");

    this.queryExecutor = Executors.newCachedThreadPool(threadsNamed("query-scheduler-%d"));
    this.queryExecutorMBean = new ThreadPoolExecutorMBean((ThreadPoolExecutor) queryExecutor);

    this.queryMonitor = checkNotNull(queryMonitor, "queryMonitor is null");
    this.locationFactory = checkNotNull(locationFactory, "locationFactory is null");
    this.queryIdGenerator = checkNotNull(queryIdGenerator, "queryIdGenerator is null");

    this.maxQueryAge = config.getMaxQueryAge();
    this.maxQueryHistory = config.getMaxQueryHistory();
    this.clientTimeout = config.getClientTimeout();

    queryManagementExecutor =
        Executors.newScheduledThreadPool(
            config.getQueryManagerExecutorPoolSize(), threadsNamed("query-management-%d"));
    queryManagementExecutorMBean =
        new ThreadPoolExecutorMBean((ThreadPoolExecutor) queryManagementExecutor);
    queryManagementExecutor.scheduleAtFixedRate(
        new Runnable() {
          @Override
          public void run() {
            try {
              removeExpiredQueries();
            } catch (Throwable e) {
              log.warn(e, "Error removing old queries");
            }
            try {
              failAbandonedQueries();
            } catch (Throwable e) {
              log.warn(e, "Error removing old queries");
            }
          }
        },
        200,
        200,
        TimeUnit.MILLISECONDS);
  }
Пример #2
0
  @Inject
  public SqlQueryQueueManager(
      QueryManagerConfig config, ObjectMapper mapper, MBeanExporter mbeanExporter) {
    checkNotNull(config, "config is null");
    this.mbeanExporter = checkNotNull(mbeanExporter, "mbeanExporter is null");

    ImmutableList.Builder<QueryQueueRule> rules = ImmutableList.builder();
    if (config.getQueueConfigFile() == null) {
      QueryQueueDefinition global =
          new QueryQueueDefinition(
              "global", config.getMaxConcurrentQueries(), config.getMaxQueuedQueries());
      QueryQueueDefinition big =
          new QueryQueueDefinition(
              "big", config.getMaxConcurrentBigQueries(), config.getMaxQueuedBigQueries());
      rules.add(
          new QueryQueueRule(
              null,
              null,
              ImmutableMap.of(BIG_QUERY, Pattern.compile("true", Pattern.CASE_INSENSITIVE)),
              ImmutableList.of(big)));
      rules.add(new QueryQueueRule(null, null, ImmutableMap.of(), ImmutableList.of(global)));
    } else {
      File file = new File(config.getQueueConfigFile());
      ManagerSpec managerSpec;
      try {
        managerSpec = mapper.readValue(file, ManagerSpec.class);
      } catch (IOException e) {
        throw Throwables.propagate(e);
      }
      Map<String, QueryQueueDefinition> definitions = new HashMap<>();
      for (Map.Entry<String, QueueSpec> queue : managerSpec.getQueues().entrySet()) {
        definitions.put(
            queue.getKey(),
            new QueryQueueDefinition(
                queue.getKey(),
                queue.getValue().getMaxConcurrent(),
                queue.getValue().getMaxQueued()));
      }

      for (RuleSpec rule : managerSpec.getRules()) {
        rules.add(
            QueryQueueRule.createRule(
                rule.getUserRegex(),
                rule.getSourceRegex(),
                rule.getSessionPropertyRegexes(),
                rule.getQueues(),
                definitions));
      }
    }
    this.rules = rules.build();
    checkIsDAG(this.rules);
  }