@Override
 protected ShardIterator shards(ClusterState state, MultiGetShardRequest request) {
   return clusterService
       .operationRouting()
       .getShards(
           clusterService.state(), request.index(), request.shardId(), request.preference());
 }
 @Override
 protected void resolveRequest(ClusterState state, MultiGetShardRequest request) {
   if (request.realtime == null) {
     request.realtime = this.realtime;
   }
   // no need to set concrete index and routing here, it has already been set by the multi get
   // action on the item
   // request.index(state.metaData().concreteIndex(request.index()));
 }
  @Override
  protected MultiGetShardResponse shardOperation(MultiGetShardRequest request, int shardId)
      throws ElasticSearchException {
    IndexService indexService = indicesService.indexServiceSafe(request.index());
    IndexShard indexShard = indexService.shardSafe(shardId);

    if (request.refresh() && !request.realtime()) {
      indexShard.refresh(
          new Engine.Refresh("refresh_flag_mget").force(TransportGetAction.REFRESH_FORCE));
    }

    MultiGetShardResponse response = new MultiGetShardResponse();
    for (int i = 0; i < request.locations.size(); i++) {
      String type = request.types.get(i);
      String id = request.ids.get(i);
      String[] fields = request.fields.get(i);

      long version = request.versions.get(i);
      VersionType versionType = request.versionTypes.get(i);
      if (versionType == null) {
        versionType = VersionType.INTERNAL;
      }

      FetchSourceContext fetchSourceContext = request.fetchSourceContexts.get(i);
      try {
        GetResult getResult =
            indexShard
                .getService()
                .get(
                    type, id, fields, request.realtime(), version, versionType, fetchSourceContext);
        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,
              request.index(),
              shardId,
              type,
              id);
          response.add(
              request.locations.get(i),
              new MultiGetResponse.Failure(
                  request.index(), type, id, ExceptionsHelper.detailedMessage(t)));
        }
      }
    }

    return response;
  }
 @Override
 protected ClusterBlockException checkRequestBlock(
     ClusterState state, MultiGetShardRequest request) {
   return state.blocks().indexBlockedException(ClusterBlockLevel.READ, request.index());
 }
  public void testSerialization() throws IOException {
    MultiGetRequest multiGetRequest = new MultiGetRequest();
    if (randomBoolean()) {
      multiGetRequest.preference(randomAsciiOfLength(randomIntBetween(1, 10)));
    }
    if (randomBoolean()) {
      multiGetRequest.realtime(false);
    }
    if (randomBoolean()) {
      multiGetRequest.refresh(true);
    }
    multiGetRequest.ignoreErrorsOnGeneratedFields(randomBoolean());

    MultiGetShardRequest multiGetShardRequest =
        new MultiGetShardRequest(multiGetRequest, "index", 0);
    int numItems = iterations(10, 30);
    for (int i = 0; i < numItems; i++) {
      MultiGetRequest.Item item =
          new MultiGetRequest.Item(
              "alias-" + randomAsciiOfLength(randomIntBetween(1, 10)), "type", "id-" + i);
      if (randomBoolean()) {
        int numFields = randomIntBetween(1, 5);
        String[] fields = new String[numFields];
        for (int j = 0; j < fields.length; j++) {
          fields[j] = randomAsciiOfLength(randomIntBetween(1, 10));
        }
        item.fields(fields);
      }
      if (randomBoolean()) {
        item.version(randomIntBetween(1, Integer.MAX_VALUE));
        item.versionType(randomFrom(VersionType.values()));
      }
      if (randomBoolean()) {
        item.fetchSourceContext(new FetchSourceContext(randomBoolean()));
      }
      multiGetShardRequest.add(0, item);
    }

    BytesStreamOutput out = new BytesStreamOutput();
    out.setVersion(randomVersion(random()));
    multiGetShardRequest.writeTo(out);

    StreamInput in = StreamInput.wrap(out.bytes());
    in.setVersion(out.getVersion());
    MultiGetShardRequest multiGetShardRequest2 = new MultiGetShardRequest();
    multiGetShardRequest2.readFrom(in);

    assertThat(multiGetShardRequest2.index(), equalTo(multiGetShardRequest.index()));
    assertThat(multiGetShardRequest2.preference(), equalTo(multiGetShardRequest.preference()));
    assertThat(multiGetShardRequest2.realtime(), equalTo(multiGetShardRequest.realtime()));
    assertThat(multiGetShardRequest2.refresh(), equalTo(multiGetShardRequest.refresh()));
    assertThat(
        multiGetShardRequest2.ignoreErrorsOnGeneratedFields(),
        equalTo(multiGetShardRequest.ignoreErrorsOnGeneratedFields()));
    assertThat(multiGetShardRequest2.items.size(), equalTo(multiGetShardRequest.items.size()));
    for (int i = 0; i < multiGetShardRequest2.items.size(); i++) {
      MultiGetRequest.Item item = multiGetShardRequest.items.get(i);
      MultiGetRequest.Item item2 = multiGetShardRequest2.items.get(i);
      assertThat(item2.index(), equalTo(item.index()));
      assertThat(item2.type(), equalTo(item.type()));
      assertThat(item2.id(), equalTo(item.id()));
      assertThat(item2.fields(), equalTo(item.fields()));
      assertThat(item2.version(), equalTo(item.version()));
      assertThat(item2.versionType(), equalTo(item.versionType()));
      assertThat(item2.fetchSourceContext(), equalTo(item.fetchSourceContext()));
    }
    assertThat(multiGetShardRequest2.indices(), equalTo(multiGetShardRequest.indices()));
    assertThat(
        multiGetShardRequest2.indicesOptions(), equalTo(multiGetShardRequest.indicesOptions()));
  }
Exemplo n.º 6
0
  @Override
  protected void doExecute(
      final MultiGetRequest request, final ActionListener<MultiGetResponse> listener) {
    ClusterState clusterState = clusterService.state();
    Map<ShardId, MultiGetShardRequest> shardRequests = new HashMap<ShardId, MultiGetShardRequest>();
    for (int i = 0; i < request.items.size(); i++) {
      MultiGetRequest.Item item = request.items.get(i);
      ShardId shardId =
          clusterService
              .operationRouting()
              .getShards(clusterState, item.index(), item.type(), item.id(), item.routing(), null)
              .shardId();
      MultiGetShardRequest shardRequest = shardRequests.get(shardId);
      if (shardRequest == null) {
        shardRequest = new MultiGetShardRequest(shardId.index().name(), shardId.id());
        shardRequest.preference(request.preference);
        shardRequest.realtime(request.realtime);
        shardRequest.refresh(request.refresh);

        shardRequests.put(shardId, shardRequest);
      }
      shardRequest.add(i, item.type(), item.id(), item.fields());
    }

    final MultiGetItemResponse[] responses = new MultiGetItemResponse[request.items.size()];
    final AtomicInteger counter = new AtomicInteger(shardRequests.size());

    for (final MultiGetShardRequest shardRequest : shardRequests.values()) {
      shardAction.execute(
          shardRequest,
          new ActionListener<MultiGetShardResponse>() {
            @Override
            public void onResponse(MultiGetShardResponse response) {
              synchronized (responses) {
                for (int i = 0; i < response.locations.size(); i++) {
                  responses[response.locations.get(i)] =
                      new MultiGetItemResponse(response.responses.get(i), response.failures.get(i));
                }
              }
              if (counter.decrementAndGet() == 0) {
                finishHim();
              }
            }

            @Override
            public void onFailure(Throwable e) {
              // create failures for all relevant requests
              String message = ExceptionsHelper.detailedMessage(e);
              synchronized (responses) {
                for (int i = 0; i < shardRequest.locations.size(); i++) {
                  responses[shardRequest.locations.get(i)] =
                      new MultiGetItemResponse(
                          null,
                          new MultiGetResponse.Failure(
                              shardRequest.index(),
                              shardRequest.types.get(i),
                              shardRequest.ids.get(i),
                              message));
                }
              }
              if (counter.decrementAndGet() == 0) {
                finishHim();
              }
            }

            private void finishHim() {
              listener.onResponse(new MultiGetResponse(responses));
            }
          });
    }
  }