|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.action.get; |
| 10 | + |
| 11 | +import org.apache.lucene.store.AlreadyClosedException; |
| 12 | +import org.apache.lucene.util.BytesRef; |
| 13 | +import org.elasticsearch.action.ActionListener; |
| 14 | +import org.elasticsearch.action.ActionRequest; |
| 15 | +import org.elasticsearch.action.ActionRequestValidationException; |
| 16 | +import org.elasticsearch.action.ActionResponse; |
| 17 | +import org.elasticsearch.action.support.ActionFilters; |
| 18 | +import org.elasticsearch.action.support.HandledTransportAction; |
| 19 | +import org.elasticsearch.common.inject.Inject; |
| 20 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 21 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 22 | +import org.elasticsearch.core.Nullable; |
| 23 | +import org.elasticsearch.index.IndexService; |
| 24 | +import org.elasticsearch.index.engine.Engine; |
| 25 | +import org.elasticsearch.index.engine.InternalEngine; |
| 26 | +import org.elasticsearch.index.get.GetResult; |
| 27 | +import org.elasticsearch.index.shard.IndexShard; |
| 28 | +import org.elasticsearch.index.shard.ShardId; |
| 29 | +import org.elasticsearch.indices.IndicesService; |
| 30 | +import org.elasticsearch.logging.LogManager; |
| 31 | +import org.elasticsearch.logging.Logger; |
| 32 | +import org.elasticsearch.tasks.Task; |
| 33 | +import org.elasticsearch.threadpool.ThreadPool; |
| 34 | +import org.elasticsearch.transport.TransportService; |
| 35 | + |
| 36 | +import java.io.IOException; |
| 37 | +import java.util.Objects; |
| 38 | + |
| 39 | +// TODO(ES-5727): add a retry mechanism to TransportGetFromTranslogAction |
| 40 | +public class TransportGetFromTranslogAction extends HandledTransportAction< |
| 41 | + TransportGetFromTranslogAction.Request, |
| 42 | + TransportGetFromTranslogAction.Response> { |
| 43 | + |
| 44 | + public static final String NAME = "internal:data/read/get_from_translog"; |
| 45 | + public static final Logger logger = LogManager.getLogger(TransportGetFromTranslogAction.class); |
| 46 | + |
| 47 | + private final IndicesService indicesService; |
| 48 | + |
| 49 | + @Inject |
| 50 | + public TransportGetFromTranslogAction(TransportService transportService, IndicesService indicesService, ActionFilters actionFilters) { |
| 51 | + super(NAME, transportService, actionFilters, Request::new, ThreadPool.Names.GET); |
| 52 | + this.indicesService = indicesService; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + protected void doExecute(Task task, Request request, ActionListener<Response> listener) { |
| 57 | + final GetRequest getRequest = request.getRequest(); |
| 58 | + final ShardId shardId = request.shardId(); |
| 59 | + IndexService indexService = indicesService.indexServiceSafe(shardId.getIndex()); |
| 60 | + IndexShard indexShard = indexService.getShard(shardId.id()); |
| 61 | + assert indexShard.routingEntry().isPromotableToPrimary() : "not an indexing shard" + indexShard.routingEntry(); |
| 62 | + assert getRequest.realtime(); |
| 63 | + ActionListener.completeWith(listener, () -> { |
| 64 | + var result = indexShard.getService() |
| 65 | + .getFromTranslog( |
| 66 | + getRequest.id(), |
| 67 | + getRequest.storedFields(), |
| 68 | + getRequest.realtime(), |
| 69 | + getRequest.version(), |
| 70 | + getRequest.versionType(), |
| 71 | + getRequest.fetchSourceContext(), |
| 72 | + getRequest.isForceSyntheticSource() |
| 73 | + ); |
| 74 | + long segmentGeneration = -1; |
| 75 | + if (result == null) { |
| 76 | + Engine engine = indexShard.getEngineOrNull(); |
| 77 | + if (engine == null) { |
| 78 | + throw new AlreadyClosedException("engine closed"); |
| 79 | + } |
| 80 | + segmentGeneration = ((InternalEngine) engine).getLastUnsafeSegmentGenerationForGets(); |
| 81 | + } |
| 82 | + return new Response(result, segmentGeneration); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + public static class Request extends ActionRequest { |
| 87 | + |
| 88 | + private final GetRequest getRequest; |
| 89 | + private final ShardId shardId; |
| 90 | + |
| 91 | + public Request(GetRequest getRequest, ShardId shardId) { |
| 92 | + this.getRequest = Objects.requireNonNull(getRequest); |
| 93 | + this.shardId = Objects.requireNonNull(shardId); |
| 94 | + } |
| 95 | + |
| 96 | + public Request(StreamInput in) throws IOException { |
| 97 | + super(in); |
| 98 | + getRequest = new GetRequest(in); |
| 99 | + shardId = new ShardId(in); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void writeTo(StreamOutput out) throws IOException { |
| 104 | + super.writeTo(out); |
| 105 | + getRequest.writeTo(out); |
| 106 | + shardId.writeTo(out); |
| 107 | + } |
| 108 | + |
| 109 | + public GetRequest getRequest() { |
| 110 | + return getRequest; |
| 111 | + } |
| 112 | + |
| 113 | + public ShardId shardId() { |
| 114 | + return shardId; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public ActionRequestValidationException validate() { |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public String toString() { |
| 124 | + return "GetFromTranslogRequest{" + "getRequest=" + getRequest + ", shardId=" + shardId + "}"; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + public static class Response extends ActionResponse { |
| 129 | + @Nullable |
| 130 | + private final GetResult getResult; |
| 131 | + private final long segmentGeneration; |
| 132 | + |
| 133 | + public Response(GetResult getResult, long segmentGeneration) { |
| 134 | + this.getResult = getResult; |
| 135 | + this.segmentGeneration = segmentGeneration; |
| 136 | + } |
| 137 | + |
| 138 | + public Response(StreamInput in) throws IOException { |
| 139 | + super(in); |
| 140 | + segmentGeneration = in.readZLong(); |
| 141 | + getResult = in.readOptionalWriteable(GetResult::new); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public void writeTo(StreamOutput out) throws IOException { |
| 146 | + out.writeZLong(segmentGeneration); |
| 147 | + out.writeOptionalWriteable(getResult); |
| 148 | + } |
| 149 | + |
| 150 | + @Nullable |
| 151 | + public GetResult getResult() { |
| 152 | + return getResult; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * The segment generation that the search shard should wait for before handling the real-time GET request locally. |
| 157 | + * -1 if the result is not null (i.e., the result is served from the indexing shard), or there hasn't simply been |
| 158 | + * any switches from unsafe to safe map in the LiveVersionMap (see {@link InternalEngine#getVersionFromMap(BytesRef)}). |
| 159 | + */ |
| 160 | + public long segmentGeneration() { |
| 161 | + return segmentGeneration; |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public boolean equals(Object o) { |
| 166 | + if (this == o) return true; |
| 167 | + if (o instanceof Response == false) return false; |
| 168 | + Response other = (Response) o; |
| 169 | + return segmentGeneration == other.segmentGeneration && Objects.equals(getResult, other.getResult); |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public int hashCode() { |
| 174 | + return Objects.hash(segmentGeneration, getResult); |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public String toString() { |
| 179 | + return "Response{" + "getResult=" + getResult + ", segmentGeneration=" + segmentGeneration + "}"; |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments