Java Code Examples for org.elasticsearch.index.IndexService#shardSafe()

The following examples show how to use org.elasticsearch.index.IndexService#shardSafe() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TransportShardDeleteAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected void processRequestItemsOnReplica(ShardId shardId, ShardDeleteRequest request) {
    IndexService indexService = indicesService.indexServiceSafe(request.index());
    IndexShard indexShard = indexService.shardSafe(shardId.id());
    for (int i = 0; i < request.itemIndices().size(); i++) {
        int location = request.itemIndices().get(i);
        if (request.skipFromLocation() == location) {
            // skipping this and all next items, the primary did not processed them (mostly due to a kill request)
            break;
        }

        ShardDeleteRequest.Item item = request.items().get(i);
        try {
            Engine.Delete delete = indexShard.prepareDeleteOnReplica(request.type(), item.id(), item.version(), item.versionType());
            indexShard.delete(delete);
            logger.trace("{} REPLICA: successfully deleted [{}]/[{}]", request.shardId(), request.type(), item.id());
        } catch (Throwable e) {
            // if its not an ignore replica failure, we need to make sure to bubble up the failure
            // so we will fail the shard
            if (!ignoreReplicaException(e)) {
                throw e;
            }
        }
    }

}
 
Example 2
Source File: TransportShardMultiTermsVectorAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected MultiTermVectorsShardResponse shardOperation(MultiTermVectorsShardRequest request, ShardId shardId) {
    MultiTermVectorsShardResponse response = new MultiTermVectorsShardResponse();
    for (int i = 0; i < request.locations.size(); i++) {
        TermVectorsRequest termVectorsRequest = request.requests.get(i);
        try {
            IndexService indexService = indicesService.indexServiceSafe(request.index());
            IndexShard indexShard = indexService.shardSafe(shardId.id());
            TermVectorsResponse termVectorsResponse = indexShard.termVectorsService().getTermVectors(termVectorsRequest, shardId.getIndex());
            termVectorsResponse.updateTookInMillis(termVectorsRequest.startTime());
            response.add(request.locations.get(i), termVectorsResponse);
        } catch (Throwable t) {
            if (TransportActions.isShardNotAvailableException(t)) {
                throw (ElasticsearchException) t;
            } else {
                logger.debug("{} failed to execute multi term vectors for [{}]/[{}]", t, shardId, termVectorsRequest.type(), termVectorsRequest.id());
                response.add(request.locations.get(i),
                        new MultiTermVectorsResponse.Failure(request.index(), termVectorsRequest.type(), termVectorsRequest.id(), t));
            }
        }
    }

    return response;
}
 
Example 3
Source File: SyncedFlushService.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private ShardSyncedFlushResponse performSyncedFlush(ShardSyncedFlushRequest request) {
    IndexService indexService = indicesService.indexServiceSafe(request.shardId().getIndex());
    IndexShard indexShard = indexService.shardSafe(request.shardId().id());
    logger.trace("{} performing sync flush. sync id [{}], expected commit id {}", request.shardId(), request.syncId(), request.expectedCommitId());
    Engine.SyncedFlushResult result = indexShard.syncFlush(request.syncId(), request.expectedCommitId());
    logger.trace("{} sync flush done. sync id [{}], result [{}]", request.shardId(), request.syncId(), result);
    switch (result) {
        case SUCCESS:
            return new ShardSyncedFlushResponse();
        case COMMIT_MISMATCH:
            return new ShardSyncedFlushResponse("commit has changed");
        case PENDING_OPERATIONS:
            return new ShardSyncedFlushResponse("pending operations");
        default:
            throw new ElasticsearchException("unknown synced flush result [" + result + "]");
    }
}
 
Example 4
Source File: TransportIndexShardStatsAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected ShardStats shardOperation(IndexShardStatsRequest request, ShardRouting shardRouting) {
    IndexService indexService = indicesService.indexServiceSafe(shardRouting.shardId().getIndex());
    IndexShard indexShard = indexService.shardSafe(shardRouting.shardId().id());
    // if we don't have the routing entry yet, we need it stats wise, we treat it as if the shard is not ready yet
    if (indexShard.routingEntry() == null) {
        throw new ShardNotFoundException(indexShard.shardId());
    }

    if (!indexShard.state().equals(IndexShardState.STARTED)) {
        throw new ElasticsearchException(indexShard.shardId().toString() + " state is " + indexShard.state() + ", not started");
    }
    
    CommonStatsFlags flags = new CommonStatsFlags().clear();

    if (request.dl()) {
        flags.set(CommonStatsFlags.Flag.DL);
    }

    return new ShardStats(indexShard.routingEntry(), indexShard.shardPath(), new CommonStats(indexShard, flags), indexShard.commitStats());
}
 
Example 5
Source File: TransportUpgradeStatusAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected ShardUpgradeStatus shardOperation(UpgradeStatusRequest request, ShardRouting shardRouting) {
    IndexService indexService = indicesService.indexServiceSafe(shardRouting.shardId().getIndex());
    IndexShard indexShard = indexService.shardSafe(shardRouting.shardId().id());
    List<Segment> segments = indexShard.engine().segments(false);
    long total_bytes = 0;
    long to_upgrade_bytes = 0;
    long to_upgrade_bytes_ancient = 0;
    for (Segment seg : segments) {
        total_bytes += seg.sizeInBytes;
        if (seg.version.major != Version.CURRENT.luceneVersion.major) {
            to_upgrade_bytes_ancient += seg.sizeInBytes;
            to_upgrade_bytes += seg.sizeInBytes;
        } else if (seg.version.minor != Version.CURRENT.luceneVersion.minor) {
            // TODO: this comparison is bogus! it would cause us to upgrade even with the same format
            // instead, we should check if the codec has changed
            to_upgrade_bytes += seg.sizeInBytes;
        }
    }

    return new ShardUpgradeStatus(indexShard.routingEntry(), total_bytes, to_upgrade_bytes, to_upgrade_bytes_ancient);
}
 
Example 6
Source File: TransportGetAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected GetResponse shardOperation(GetRequest request, ShardId shardId) {
    IndexService indexService = indicesService.indexServiceSafe(shardId.getIndex());
    IndexShard indexShard = indexService.shardSafe(shardId.id());

    if (request.refresh() && !request.realtime()) {
        indexShard.refresh("refresh_flag_get");
    }

    GetResult result = indexShard.getService().get(request.type(), request.id(), request.fields(),
            request.realtime(), request.version(), request.versionType(), request.fetchSourceContext(), request.ignoreErrorsOnGeneratedFields());
    return new GetResponse(result);
}
 
Example 7
Source File: TransportIndexAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected void shardOperationOnReplica(IndexRequest request) {
    final ShardId shardId = request.shardId();
    IndexService indexService = indicesService.indexServiceSafe(shardId.getIndex());
    IndexShard indexShard = indexService.shardSafe(shardId.id());
    indexShard.checkDiskSpace(fsService);
    final Engine.IndexingOperation operation = executeIndexRequestOnReplica(request, indexShard);
    processAfterWrite(request.refresh(), indexShard, operation.getTranslogLocation());
}
 
Example 8
Source File: TransportShardMultiGetAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected MultiGetShardResponse shardOperation(MultiGetShardRequest request, ShardId shardId) {
    IndexService indexService = indicesService.indexServiceSafe(shardId.getIndex());
    IndexShard indexShard = indexService.shardSafe(shardId.id());

    if (request.refresh() && !request.realtime()) {
        indexShard.refresh("refresh_flag_mget");
    }

    MultiGetShardResponse response = new MultiGetShardResponse();
    for (int i = 0; i < request.locations.size(); i++) {
        MultiGetRequest.Item item = request.items.get(i);
        try {
            GetResult getResult = indexShard.getService().get(item.type(), item.id(), item.fields(), request.realtime(), item.version(), item.versionType(), item.fetchSourceContext(), request.ignoreErrorsOnGeneratedFields());
            response.add(request.locations.get(i), new GetResponse(getResult));
        } catch (Throwable t) {
            if (TransportActions.isShardNotAvailableException(t)) {
                throw (ElasticsearchException) t;
            } else {
                logger.debug("{} failed to execute multi_get for [{}]/[{}]", t, shardId, item.type(), item.id());
                response.add(request.locations.get(i), new MultiGetResponse.Failure(request.index(), item.type(), item.id(), t));
            }
        }
    }

    return response;
}
 
Example 9
Source File: TransportShardUpsertAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected void processRequestItemsOnReplica(ShardId shardId, ShardUpsertRequest request) {
    IndexService indexService = indicesService.indexServiceSafe(shardId.getIndex());
    IndexShard indexShard = indexService.shardSafe(shardId.id());
    for (ShardUpsertRequest.Item item : request.items()) {
        if (item.source() == null) {
            if (logger.isTraceEnabled()) {
                logger.trace("[{} (R)] Document with id {}, has no source, primary operation must have failed",
                        indexShard.shardId(), item.id());
            }
            continue;
        }
        shardIndexOperationOnReplica(request, item, indexShard);
    }
}
 
Example 10
Source File: SyncedFlushService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private InFlightOpsResponse performInFlightOps(InFlightOpsRequest request) {
    IndexService indexService = indicesService.indexServiceSafe(request.shardId().getIndex());
    IndexShard indexShard = indexService.shardSafe(request.shardId().id());
    if (indexShard.routingEntry().primary() == false) {
        throw new IllegalStateException("[" + request.shardId() +"] expected a primary shard");
    }
    int opCount = indexShard.getOperationsCount();
    logger.trace("{} in flight operations sampled at [{}]", request.shardId(), opCount);
    return new InFlightOpsResponse(opCount);
}
 
Example 11
Source File: TransportTermVectorsAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected TermVectorsResponse shardOperation(TermVectorsRequest request, ShardId shardId) {
    IndexService indexService = indicesService.indexServiceSafe(shardId.getIndex());
    IndexShard indexShard = indexService.shardSafe(shardId.id());
    TermVectorsResponse response = indexShard.termVectorsService().getTermVectors(request, shardId.getIndex());
    response.updateTookInMillis(request.startTime());
    return response;
}
 
Example 12
Source File: SearchService.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
final SearchContext createContext(ShardSearchRequest request, @Nullable Engine.Searcher searcher) {
    IndexService indexService = indicesService.indexServiceSafe(request.index());
    IndexShard indexShard = indexService.shardSafe(request.shardId());

    SearchShardTarget shardTarget = new SearchShardTarget(clusterService.localNode().id(), request.index(), request.shardId());
    String searchSource = "search";
    if (request.hasHeader("search_source")) {
        searchSource = request.getHeader("search_source");
    }
    Engine.Searcher engineSearcher = searcher == null ? indexShard.acquireSearcher(searchSource) : searcher;

    DefaultSearchContext context = new DefaultSearchContext(idGenerator.incrementAndGet(), request, shardTarget, engineSearcher, indexService, indexShard, scriptService, pageCacheRecycler, bigArrays, threadPool.estimatedTimeInMillisCounter(), parseFieldMatcher, defaultSearchTimeout);
    SearchContext.setCurrent(context);
    try {
        if (request.scroll() != null) {
            context.scrollContext(new ScrollContext());
            context.scrollContext().scroll = request.scroll();
        }

        parseTemplate(request, context);
        parseSource(context, request.source());
        parseSource(context, request.extraSource());

        // if the from and size are still not set, default them
        if (context.from() == -1) {
            context.from(0);
        }
        if (context.searchType() == SearchType.COUNT) {
            // so that the optimizations we apply to size=0 also apply to search_type=COUNT
            // and that we close contexts when done with the query phase
            context.searchType(SearchType.QUERY_THEN_FETCH);
            context.size(0);
        } else if (context.size() == -1) {
            context.size(10);
        }

        if (context.request().isProfile()) {
            context.setProfilers(new Profilers(context.searcher()));
        }

        // pre process
        dfsPhase.preProcess(context);
        queryPhase.preProcess(context);
        fetchPhase.preProcess(context);

        // compute the context keep alive
        long keepAlive = defaultKeepAlive;
        if (request.scroll() != null && request.scroll().keepAlive() != null) {
            keepAlive = request.scroll().keepAlive().millis();
        }
        context.keepAlive(keepAlive);
    } catch (Throwable e) {
        context.close();
        throw ExceptionsHelper.convertToRuntime(e);
    }

    return context;
}
 
Example 13
Source File: TransportRecoveryAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected RecoveryState shardOperation(RecoveryRequest request, ShardRouting shardRouting) {
    IndexService indexService = indicesService.indexServiceSafe(shardRouting.shardId().getIndex());
    IndexShard indexShard = indexService.shardSafe(shardRouting.shardId().id());
    return indexShard.recoveryState();
}
 
Example 14
Source File: TransportIndicesSegmentsAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected ShardSegments shardOperation(IndicesSegmentsRequest request, ShardRouting shardRouting) {
    IndexService indexService = indicesService.indexServiceSafe(shardRouting.getIndex());
    IndexShard indexShard = indexService.shardSafe(shardRouting.id());
    return new ShardSegments(indexShard.routingEntry(), indexShard.engine().segments(request.verbose()));
}
 
Example 15
Source File: TransportIndicesStatsAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected ShardStats shardOperation(IndicesStatsRequest request, ShardRouting shardRouting) {
    IndexService indexService = indicesService.indexServiceSafe(shardRouting.shardId().getIndex());
    IndexShard indexShard = indexService.shardSafe(shardRouting.shardId().id());
    // if we don't have the routing entry yet, we need it stats wise, we treat it as if the shard is not ready yet
    if (indexShard.routingEntry() == null) {
        throw new ShardNotFoundException(indexShard.shardId());
    }

    CommonStatsFlags flags = new CommonStatsFlags().clear();

    if (request.docs()) {
        flags.set(CommonStatsFlags.Flag.Docs);
    }
    if (request.store()) {
        flags.set(CommonStatsFlags.Flag.Store);
    }
    if (request.indexing()) {
        flags.set(CommonStatsFlags.Flag.Indexing);
        flags.types(request.types());
    }
    if (request.get()) {
        flags.set(CommonStatsFlags.Flag.Get);
    }
    if (request.search()) {
        flags.set(CommonStatsFlags.Flag.Search);
        flags.groups(request.groups());
    }
    if (request.merge()) {
        flags.set(CommonStatsFlags.Flag.Merge);
    }
    if (request.refresh()) {
        flags.set(CommonStatsFlags.Flag.Refresh);
    }
    if (request.flush()) {
        flags.set(CommonStatsFlags.Flag.Flush);
    }
    if (request.warmer()) {
        flags.set(CommonStatsFlags.Flag.Warmer);
    }
    if (request.queryCache()) {
        flags.set(CommonStatsFlags.Flag.QueryCache);
    }
    if (request.fieldData()) {
        flags.set(CommonStatsFlags.Flag.FieldData);
        flags.fieldDataFields(request.fieldDataFields());
    }
    if (request.percolate()) {
        flags.set(CommonStatsFlags.Flag.Percolate);
    }
    if (request.segments()) {
        flags.set(CommonStatsFlags.Flag.Segments);
    }
    if (request.completion()) {
        flags.set(CommonStatsFlags.Flag.Completion);
        flags.completionDataFields(request.completionFields());
    }
    if (request.translog()) {
        flags.set(CommonStatsFlags.Flag.Translog);
    }
    if (request.suggest()) {
        flags.set(CommonStatsFlags.Flag.Suggest);
    }
    if (request.requestCache()) {
        flags.set(CommonStatsFlags.Flag.RequestCache);
    }
    if (request.recovery()) {
        flags.set(CommonStatsFlags.Flag.Recovery);
    }
    if (request.dl()) {
        flags.set(CommonStatsFlags.Flag.DL);
    }
    if (request.reindex()) {
        flags.set(CommonStatsFlags.Flag.Reindex);
    }

    return new ShardStats(indexShard.routingEntry(), indexShard.shardPath(), new CommonStats(indexShard, flags), indexShard.commitStats());
}
 
Example 16
Source File: TransportExistsAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected ShardExistsResponse shardOperation(ShardExistsRequest request) {
    IndexService indexService = indicesService.indexServiceSafe(request.shardId().getIndex());
    IndexShard indexShard = indexService.shardSafe(request.shardId().id());

    SearchShardTarget shardTarget = new SearchShardTarget(clusterService.localNode().id(), request.shardId().getIndex(), request.shardId().id());
    SearchContext context = new DefaultSearchContext(0,
            new ShardSearchLocalRequest(request.types(), request.nowInMillis(), request.filteringAliases()),
            shardTarget, indexShard.acquireSearcher("exists"), indexService, indexShard,
            scriptService, pageCacheRecycler, bigArrays, threadPool.estimatedTimeInMillisCounter(), parseFieldMatcher,
            SearchService.NO_TIMEOUT
    );
    SearchContext.setCurrent(context);

    try {
        if (request.minScore() != DEFAULT_MIN_SCORE) {
            context.minimumScore(request.minScore());
        }
        BytesReference source = request.querySource();
        if (source != null && source.length() > 0) {
            try {
                QueryParseContext.setTypes(request.types());
                context.parsedQuery(indexService.queryParserService().parseQuery(source));
            } finally {
                QueryParseContext.removeTypes();
            }
        }
        context.preProcess();
        try {
            boolean exists;
            try {
                exists = Lucene.exists(context.searcher(), context.query());
            } finally {
                context.clearReleasables(SearchContext.Lifetime.COLLECTION);
            }
            return new ShardExistsResponse(request.shardId(), exists);
        } catch (Exception e) {
            throw new QueryPhaseExecutionException(context, "failed to execute exists", e);
        }
    } finally {
        // this will also release the index searcher
        context.close();
        SearchContext.removeCurrent();
    }
}
 
Example 17
Source File: RecoverySource.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private RecoveryResponse recover(final StartRecoveryRequest request) {
    final IndexService indexService = indicesService.indexServiceSafe(request.shardId().index().name());
    final IndexShard shard = indexService.shardSafe(request.shardId().id());

    // starting recovery from that our (the source) shard state is marking the shard to be in recovery mode as well, otherwise
    // the index operations will not be routed to it properly
    RoutingNode node = clusterService.state().getRoutingNodes().node(request.targetNode().id());
    if (node == null) {
        logger.debug("delaying recovery of {} as source node {} is unknown", request.shardId(), request.targetNode());
        throw new DelayRecoveryException("source node does not have the node [" + request.targetNode() + "] in its state yet..");
    }
    ShardRouting targetShardRouting = null;
    for (ShardRouting shardRouting : node) {
        if (shardRouting.shardId().equals(request.shardId())) {
            targetShardRouting = shardRouting;
            break;
        }
    }
    if (targetShardRouting == null) {
        logger.debug("delaying recovery of {} as it is not listed as assigned to target node {}", request.shardId(), request.targetNode());
        throw new DelayRecoveryException("source node does not have the shard listed in its state as allocated on the node");
    }
    if (!targetShardRouting.initializing()) {
        logger.debug("delaying recovery of {} as it is not listed as initializing on the target node {}. known shards state is [{}]",
                request.shardId(), request.targetNode(), targetShardRouting.state());
        throw new DelayRecoveryException("source node has the state of the target shard to be [" + targetShardRouting.state() + "], expecting to be [initializing]");
    }

    logger.trace("[{}][{}] starting recovery to {}, mark_as_relocated {}", request.shardId().index().name(), request.shardId().id(), request.targetNode(), request.markAsRelocated());
    final RecoverySourceHandler handler;
    if (IndexMetaData.isOnSharedFilesystem(shard.indexSettings())) {
        handler = new SharedFSRecoverySourceHandler(shard, request, recoverySettings, transportService, logger);
    } else if (IndexMetaData.isIndexUsingDLEngine(shard.indexSettings())) {
        handler = new DLBasedIndexRecoverySourceHandler(shard, request, recoverySettings, transportService, logger);
    } else {
        handler = new RecoverySourceHandler(shard, request, recoverySettings, transportService, logger);
    }
    ongoingRecoveries.add(shard, handler);
    try {
        return handler.recoverToTarget();
    } finally {
        ongoingRecoveries.remove(shard, handler);
    }
}
 
Example 18
Source File: TransportShardUpsertAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected ShardResponse processRequestItems(ShardId shardId,
                                            ShardUpsertRequest request,
                                            AtomicBoolean killed) throws InterruptedException {
    ShardResponse shardResponse = new ShardResponse();
    DocTableInfo tableInfo = schemas.getWritableTable(TableIdent.fromIndexName(request.index()));
    IndexService indexService = indicesService.indexServiceSafe(shardId.getIndex());
    IndexShard indexShard = indexService.shardSafe(shardId.id());

    Translog.Location translogLocation = null;
    for (int i = 0; i < request.itemIndices().size(); i++) {
        int location = request.itemIndices().get(i);
        ShardUpsertRequest.Item item = request.items().get(i);
        if (killed.get()) {
            // set failure on response and skip all next items.
            // this way replica operation will be executed, but only items with a valid source (= was processed on primary)
            // will be processed on the replica
            shardResponse.failure(new InterruptedException());
            break;
        }
        try {
            translogLocation = indexItem(
                    tableInfo,
                    request,
                    item,
                    indexShard,
                    item.insertValues() != null, // try insert first
                    0);
            shardResponse.add(location);
        } catch (Throwable t) {
            if (retryPrimaryException(t)) {
                Throwables.propagate(t);
            }
            logger.debug("{} failed to execute upsert for [{}]/[{}]",
                    t, request.shardId(), request.type(), item.id());
            if (!request.continueOnError()) {
               shardResponse.failure(t);
               break;
            }
            shardResponse.add(location,
                    new ShardResponse.Failure(
                            item.id(),
                            ExceptionsHelper.detailedMessage(t),
                            (t instanceof VersionConflictEngineException)));
        }
    }
    if (indexShard.getTranslogDurability() == Translog.Durabilty.REQUEST && translogLocation != null) {
        indexShard.sync(translogLocation);
    }
    return shardResponse;
}
 
Example 19
Source File: TransportTermsByQueryAction.java    From siren-join with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * The operation that executes the query and generates a {@link TermsByQueryShardResponse} for each shard.
 */
@Override
protected TermsByQueryShardResponse shardOperation(TermsByQueryShardRequest shardRequest) throws ElasticsearchException {
  IndexService indexService = indicesService.indexServiceSafe(shardRequest.shardId().getIndex());
  IndexShard indexShard = indexService.shardSafe(shardRequest.shardId().id());
  TermsByQueryRequest request = shardRequest.request();
  OrderByShardOperation orderByOperation = OrderByShardOperation.get(request.getOrderBy(), request.maxTermsPerShard());

  SearchShardTarget shardTarget = new SearchShardTarget(clusterService.localNode().id(),
                                                        shardRequest.shardId().getIndex(),
                                                        shardRequest.shardId().id());

  ShardSearchRequest shardSearchRequest = new ShardSearchLocalRequest(request.types(), request.nowInMillis(),
                                                                      shardRequest.filteringAliases());

  SearchContext context = new DefaultSearchContext(0, shardSearchRequest, shardTarget,
    indexShard.acquireSearcher("termsByQuery"), indexService, indexShard, scriptService,
    pageCacheRecycler, bigArrays, threadPool.estimatedTimeInMillisCounter(), parseFieldMatcher,
    SearchService.NO_TIMEOUT);
  SearchContext.setCurrent(context);

  try {
    MappedFieldType fieldType = context.smartNameFieldType(request.field());
    if (fieldType == null) {
      throw new SearchContextException(context, "[termsByQuery] field '" + request.field() +
              "' not found for types " + Arrays.toString(request.types()));
    }

    IndexFieldData indexFieldData = context.fieldData().getForField(fieldType);

    BytesReference querySource = request.querySource();
    if (querySource != null && querySource.length() > 0) {
      XContentParser queryParser = null;
      try {
        queryParser = XContentFactory.xContent(querySource).createParser(querySource);
        QueryParseContext.setTypes(request.types());
        ParsedQuery parsedQuery = orderByOperation.getParsedQuery(queryParser, indexService);
        if (parsedQuery != null) {
          context.parsedQuery(parsedQuery);
        }
      }
      finally {
        QueryParseContext.removeTypes();
        if (queryParser != null) {
          queryParser.close();
        }
      }
    }

    context.preProcess();

    // execute the search only gathering the hit count and bitset for each segment
    logger.debug("{}: Executes search for collecting terms {}", Thread.currentThread().getName(),
      shardRequest.shardId());

    TermsCollector termsCollector = this.getTermsCollector(request.termsEncoding(), indexFieldData, context);
    if (request.expectedTerms() != null) termsCollector.setExpectedTerms(request.expectedTerms());
    if (request.maxTermsPerShard() != null) termsCollector.setMaxTerms(request.maxTermsPerShard());
    HitStream hitStream = orderByOperation.getHitStream(context);
    TermsSet terms = termsCollector.collect(hitStream);

    logger.debug("{}: Returns terms response with {} terms for shard {}", Thread.currentThread().getName(),
      terms.size(), shardRequest.shardId());

    return new TermsByQueryShardResponse(shardRequest.shardId(), terms);
  }
  catch (Throwable e) {
    logger.error("[termsByQuery] Error executing shard operation", e);
    throw new QueryPhaseExecutionException(context, "[termsByQuery] Failed to execute query", e);
  }
  finally {
    // this will also release the index searcher
    context.close();
    SearchContext.removeCurrent();
  }
}
 
Example 20
Source File: BlobRecoverySource.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private RecoveryResponse recover(final StartRecoveryRequest request) {
    final IndexService indexService = indicesService.indexServiceSafe(request.shardId().index().name());
    final IndexShard shard = indexService.shardSafe(request.shardId().id());

    // starting recovery from that our (the source) shard state is marking the shard to be in recovery mode as well, otherwise
    // the index operations will not be routed to it properly
    RoutingNode node = clusterService.state().getRoutingNodes().node(request.targetNode().id());
    if (node == null) {
        logger.debug("delaying recovery of {} as source node {} is unknown", request.shardId(), request.targetNode());
        throw new DelayRecoveryException("source node does not have the node [" + request.targetNode() + "] in its state yet..");
    }
    ShardRouting targetShardRouting = null;
    for (ShardRouting shardRouting : node) {
        if (shardRouting.shardId().equals(request.shardId())) {
            targetShardRouting = shardRouting;
            break;
        }
    }
    if (targetShardRouting == null) {
        logger.debug("delaying recovery of {} as it is not listed as assigned to target node {}", request.shardId(), request.targetNode());
        throw new DelayRecoveryException("source node does not have the shard listed in its state as allocated on the node");
    }
    if (!targetShardRouting.initializing()) {
        logger.debug("delaying recovery of {} as it is not listed as initializing on the target node {}. known shards state is [{}]",
                request.shardId(), request.targetNode(), targetShardRouting.state());
        throw new DelayRecoveryException("source node has the state of the target shard to be [" + targetShardRouting.state() + "], expecting to be [initializing]");
    }

    logger.trace("[{}][{}] starting recovery to {}, mark_as_relocated {}", request.shardId().index().name(), request.shardId().id(), request.targetNode(), request.markAsRelocated());
    final RecoverySourceHandler handler;
    if (IndexMetaData.isOnSharedFilesystem(shard.indexSettings())) {
        handler = new SharedFSRecoverySourceHandler(shard, request, recoverySettings, transportService, logger);
    } else {
        // CRATE CHANGE:
        handler = new BlobRecoverySourceHandler(
                shard, request, recoverySettings, transportService, logger, blobTransferTarget, blobIndices);
    }
    ongoingRecoveries.add(shard, handler);
    try {
        return handler.recoverToTarget();
    } finally {
        ongoingRecoveries.remove(shard, handler);
    }
}