All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.druid.query.AsyncQueryRunner Maven / Gradle / Ivy

There is a newer version: 0.12.3
Show newest version
/*
 * Licensed to Metamarkets Group Inc. (Metamarkets) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Metamarkets licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package io.druid.query;

import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.metamx.common.guava.LazySequence;
import com.metamx.common.guava.Sequence;

import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class AsyncQueryRunner implements QueryRunner
{

  private final QueryRunner baseRunner;
  private final ListeningExecutorService executor;
  private final QueryWatcher queryWatcher;

  public AsyncQueryRunner(QueryRunner baseRunner, ExecutorService executor, QueryWatcher queryWatcher) {
    this.baseRunner = baseRunner;
    this.executor = MoreExecutors.listeningDecorator(executor);
    this.queryWatcher = queryWatcher;
  }

  @Override
  public Sequence run(final Query query, final Map responseContext)
  {
    final int priority = BaseQuery.getContextPriority(query, 0);
    final ListenableFuture> future = executor.submit(new AbstractPrioritizedCallable>(priority)
        {
          @Override
          public Sequence call() throws Exception
          {
            //Note: this is assumed that baseRunner does most of the work eagerly on call to the
            //run() method and resulting sequence accumulate/yield is fast.
            return baseRunner.run(query, responseContext);
          }
        });
    queryWatcher.registerQuery(query, future);
    
    return new LazySequence<>(new Supplier>()
    {
      @Override
      public Sequence get()
      {
        try {
          Number timeout = query.getContextValue(QueryContextKeys.TIMEOUT);
          if (timeout == null) {
            return future.get();
          } else {
            return future.get(timeout.longValue(), TimeUnit.MILLISECONDS);
          }
        } catch (ExecutionException | InterruptedException | TimeoutException ex) {
          throw Throwables.propagate(ex);
        }
      }
    });
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy