// Queues returned have already been created and added queryQueues
 private List<QueryQueue> selectQueues(Session session, Executor executor) {
   for (QueryQueueRule rule : rules) {
     List<QueryQueueDefinition> definitions = rule.match(session);
     if (definitions != null) {
       return getOrCreateQueues(session, executor, definitions);
     }
   }
   throw new PrestoException(USER_ERROR, "Query did not match any queuing rule");
 }
  @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);
  }
  private static void checkIsDAG(List<QueryQueueRule> rules) {
    DirectedPseudograph<String, DefaultEdge> graph = new DirectedPseudograph<>(DefaultEdge.class);
    for (QueryQueueRule rule : rules) {
      String lastQueueName = null;
      for (QueryQueueDefinition queue : rule.getQueues()) {
        String currentQueueName = queue.getTemplate();
        graph.addVertex(currentQueueName);
        if (lastQueueName != null) {
          graph.addEdge(lastQueueName, currentQueueName);
        }
        lastQueueName = currentQueueName;
      }
    }

    List<String> shortestCycle = shortestCycle(graph);

    if (shortestCycle != null) {
      String s = Joiner.on(", ").join(shortestCycle);
      throw new IllegalArgumentException(
          format("Queues must not contain a cycle. The shortest cycle found is [%s]", s));
    }
  }