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

io.druid.query.RetryQueryRunner 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.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.metamx.common.guava.MergeSequence;
import com.metamx.common.guava.Sequence;
import com.metamx.common.guava.Sequences;
import com.metamx.common.guava.Yielder;
import com.metamx.common.guava.YieldingAccumulator;
import com.metamx.common.guava.YieldingSequenceBase;
import com.metamx.emitter.EmittingLogger;
import io.druid.query.spec.MultipleSpecificSegmentSpec;
import io.druid.segment.SegmentMissingException;

import java.util.List;
import java.util.Map;

public class RetryQueryRunner implements QueryRunner
{
  private static final EmittingLogger log = new EmittingLogger(RetryQueryRunner.class);

  private final QueryRunner baseRunner;
  private final QueryToolChest> toolChest;
  private final RetryQueryRunnerConfig config;
  private final ObjectMapper jsonMapper;

  public RetryQueryRunner(
      QueryRunner baseRunner,
      QueryToolChest> toolChest,
      RetryQueryRunnerConfig config,
      ObjectMapper jsonMapper
  )
  {
    this.baseRunner = baseRunner;
    this.toolChest = toolChest;
    this.config = config;
    this.jsonMapper = jsonMapper;
  }

  @Override
  public Sequence run(final Query query, final Map context)
  {
    final List> listOfSequences = Lists.newArrayList();
    listOfSequences.add(baseRunner.run(query, context));

    return new YieldingSequenceBase()
    {
      @Override
      public  Yielder toYielder(
          OutType initValue, YieldingAccumulator accumulator
      )
      {
        final List missingSegments = getMissingSegments(context);

        if (!missingSegments.isEmpty()) {
          for (int i = 0; i < config.getNumTries(); i++) {
            log.info("[%,d] missing segments found. Retry attempt [%,d]", missingSegments.size(), i);

            context.put(Result.MISSING_SEGMENTS_KEY, Lists.newArrayList());
            final Query retryQuery = query.withQuerySegmentSpec(
                new MultipleSpecificSegmentSpec(
                    missingSegments
                )
            );
            Sequence retrySequence = baseRunner.run(retryQuery, context);
            listOfSequences.add(retrySequence);
            if (getMissingSegments(context).isEmpty()) {
              break;
            }
          }

          final List finalMissingSegs = getMissingSegments(context);
          if (!config.isReturnPartialResults() && !finalMissingSegs.isEmpty()) {
            throw new SegmentMissingException("No results found for segments[%s]", finalMissingSegs);
          }

          return new MergeSequence<>(
              query.getResultOrdering(),
              Sequences.simple(listOfSequences)).toYielder(
              initValue, accumulator
          );
        }
        else {
          return Iterables.getOnlyElement(listOfSequences).toYielder(initValue, accumulator);
        }
      }
    };
  }

  private List getMissingSegments(final Map context)
  {
    final Object maybeMissingSegments = context.get(Result.MISSING_SEGMENTS_KEY);
    if (maybeMissingSegments == null) {
      return Lists.newArrayList();
    }

    return jsonMapper.convertValue(
        maybeMissingSegments,
        new TypeReference>()
        {
        }
    );
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy