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

io.druid.query.spec.SpecificSegmentQueryRunner Maven / Gradle / Ivy

/*
 * 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.spec;

import com.google.common.base.Supplier;
import com.google.common.collect.Lists;
import io.druid.java.util.common.StringUtils;
import io.druid.java.util.common.guava.Accumulator;
import io.druid.java.util.common.guava.Sequence;
import io.druid.java.util.common.guava.SequenceWrapper;
import io.druid.java.util.common.guava.Sequences;
import io.druid.java.util.common.guava.Yielder;
import io.druid.java.util.common.guava.Yielders;
import io.druid.java.util.common.guava.YieldingAccumulator;
import io.druid.query.Query;
import io.druid.query.QueryPlus;
import io.druid.query.QueryRunner;
import io.druid.query.Result;
import io.druid.query.SegmentDescriptor;
import io.druid.segment.SegmentMissingException;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 */
public class SpecificSegmentQueryRunner implements QueryRunner
{
  private final QueryRunner base;
  private final SpecificSegmentSpec specificSpec;

  public SpecificSegmentQueryRunner(
      QueryRunner base,
      SpecificSegmentSpec specificSpec
  )
  {
    this.base = base;
    this.specificSpec = specificSpec;
  }

  @Override
  public Sequence run(final QueryPlus input, final Map responseContext)
  {
    final QueryPlus queryPlus = input.withQuerySegmentSpec(specificSpec);
    final Query query = queryPlus.getQuery();

    final Thread currThread = Thread.currentThread();
    final String currThreadName = currThread.getName();
    final String newName = StringUtils.format("%s_%s_%s", query.getType(), query.getDataSource(), query.getIntervals());

    final Sequence baseSequence = doNamed(
        currThread, currThreadName, newName, new Supplier>()
        {
          @Override
          public Sequence get()
          {
            return base.run(queryPlus, responseContext);
          }
        }
    );

    Sequence segmentMissingCatchingSequence = new Sequence()
    {
      @Override
      public  OutType accumulate(final OutType initValue, final Accumulator accumulator)
      {
        try {
          return baseSequence.accumulate(initValue, accumulator);
        }
        catch (SegmentMissingException e) {
          appendMissingSegment(responseContext);
          return initValue;
        }
      }

      @Override
      public  Yielder toYielder(
          final OutType initValue,
          final YieldingAccumulator accumulator
      )
      {
        try {
          return makeYielder(baseSequence.toYielder(initValue, accumulator));
        }
        catch (SegmentMissingException e) {
          appendMissingSegment(responseContext);
          return Yielders.done(initValue, null);
        }
      }

      private  Yielder makeYielder(final Yielder yielder)
      {
        return new Yielder()
        {
          @Override
          public OutType get()
          {
            return yielder.get();
          }

          @Override
          public Yielder next(final OutType initValue)
          {
            try {
              return yielder.next(initValue);
            }
            catch (SegmentMissingException e) {
              appendMissingSegment(responseContext);
              return Yielders.done(initValue, null);
            }
          }

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

          @Override
          public void close() throws IOException
          {
            yielder.close();
          }
        };
      }
    };
    return Sequences.wrap(
        segmentMissingCatchingSequence,
        new SequenceWrapper()
        {
          @Override
          public  RetType wrap(Supplier sequenceProcessing)
          {
            return doNamed(currThread, currThreadName, newName, sequenceProcessing);
          }
        }
    );
  }

  private void appendMissingSegment(Map responseContext)
  {
    List missingSegments = (List) responseContext.get(Result.MISSING_SEGMENTS_KEY);
    if (missingSegments == null) {
      missingSegments = Lists.newArrayList();
      responseContext.put(Result.MISSING_SEGMENTS_KEY, missingSegments);
    }
    missingSegments.add(specificSpec.getDescriptor());
  }

  private  RetType doNamed(Thread currThread, String currName, String newName, Supplier toRun)
  {
    try {
      currThread.setName(newName);
      return toRun.get();
    }
    finally {
      currThread.setName(currName);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy