Skip to content

Commit e706193

Browse files
authored
Reduce number of get method variants in ShardGetService (#122175)
1 parent 6e6e42f commit e706193

File tree

3 files changed

+57
-69
lines changed

3 files changed

+57
-69
lines changed

server/src/main/java/org/elasticsearch/action/get/TransportShardMultiGetAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ private void getAndAddToResponse(
312312
MultiGetRequest.Item item = request.items.get(location);
313313
try {
314314
GetResult getResult = indexShard.getService()
315-
.get(
315+
.mget(
316316
item.id(),
317317
item.storedFields(),
318318
request.realtime(),

server/src/main/java/org/elasticsearch/index/get/ShardGetService.java

Lines changed: 43 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.elasticsearch.common.metrics.MeanMetric;
2222
import org.elasticsearch.core.Nullable;
2323
import org.elasticsearch.index.IndexSettings;
24-
import org.elasticsearch.index.IndexVersion;
2524
import org.elasticsearch.index.IndexVersions;
2625
import org.elasticsearch.index.VersionType;
2726
import org.elasticsearch.index.engine.Engine;
@@ -93,7 +92,7 @@ public GetResult get(
9392
FetchSourceContext fetchSourceContext,
9493
boolean forceSyntheticSource
9594
) throws IOException {
96-
return get(
95+
return doGet(
9796
id,
9897
gFields,
9998
realtime,
@@ -107,7 +106,7 @@ public GetResult get(
107106
);
108107
}
109108

110-
public GetResult get(
109+
public GetResult mget(
111110
String id,
112111
String[] gFields,
113112
boolean realtime,
@@ -117,7 +116,7 @@ public GetResult get(
117116
boolean forceSyntheticSource,
118117
MultiEngineGet mget
119118
) throws IOException {
120-
return get(
119+
return doGet(
121120
id,
122121
gFields,
123122
realtime,
@@ -131,7 +130,7 @@ public GetResult get(
131130
);
132131
}
133132

134-
private GetResult get(
133+
private GetResult doGet(
135134
String id,
136135
String[] gFields,
137136
boolean realtime,
@@ -144,21 +143,40 @@ private GetResult get(
144143
Function<Engine.Get, Engine.GetResult> engineGetOperator
145144
) throws IOException {
146145
currentMetric.inc();
146+
final long now = System.nanoTime();
147147
try {
148-
long now = System.nanoTime();
149-
GetResult getResult = innerGet(
150-
id,
151-
gFields,
152-
realtime,
153-
version,
154-
versionType,
155-
ifSeqNo,
156-
ifPrimaryTerm,
157-
fetchSourceContext,
158-
forceSyntheticSource,
159-
engineGetOperator
160-
);
148+
var engineGet = new Engine.Get(realtime, realtime, id).version(version)
149+
.versionType(versionType)
150+
.setIfSeqNo(ifSeqNo)
151+
.setIfPrimaryTerm(ifPrimaryTerm);
161152

153+
final GetResult getResult;
154+
try (Engine.GetResult get = engineGetOperator.apply(engineGet)) {
155+
if (get == null) {
156+
getResult = null;
157+
} else if (get.exists() == false) {
158+
getResult = new GetResult(
159+
shardId.getIndexName(),
160+
id,
161+
UNASSIGNED_SEQ_NO,
162+
UNASSIGNED_PRIMARY_TERM,
163+
-1,
164+
false,
165+
null,
166+
null,
167+
null
168+
);
169+
} else {
170+
// break between having loaded it from translog (so we only have _source), and having a document to load
171+
getResult = innerGetFetch(
172+
id,
173+
gFields,
174+
normalizeFetchSourceContent(fetchSourceContext, gFields),
175+
get,
176+
forceSyntheticSource
177+
);
178+
}
179+
}
162180
if (getResult != null && getResult.isExists()) {
163181
existsMetric.inc(System.nanoTime() - now);
164182
} else {
@@ -179,7 +197,7 @@ public GetResult getFromTranslog(
179197
FetchSourceContext fetchSourceContext,
180198
boolean forceSyntheticSource
181199
) throws IOException {
182-
return get(
200+
return doGet(
183201
id,
184202
gFields,
185203
realtime,
@@ -193,12 +211,8 @@ public GetResult getFromTranslog(
193211
);
194212
}
195213

196-
public GetResult getForUpdate(String id, long ifSeqNo, long ifPrimaryTerm) throws IOException {
197-
return getForUpdate(id, ifSeqNo, ifPrimaryTerm, new String[] { RoutingFieldMapper.NAME });
198-
}
199-
200214
public GetResult getForUpdate(String id, long ifSeqNo, long ifPrimaryTerm, String[] gFields) throws IOException {
201-
return get(
215+
return doGet(
202216
id,
203217
gFields,
204218
true,
@@ -259,35 +273,6 @@ private static FetchSourceContext normalizeFetchSourceContent(@Nullable FetchSou
259273
return FetchSourceContext.DO_NOT_FETCH_SOURCE;
260274
}
261275

262-
private GetResult innerGet(
263-
String id,
264-
String[] gFields,
265-
boolean realtime,
266-
long version,
267-
VersionType versionType,
268-
long ifSeqNo,
269-
long ifPrimaryTerm,
270-
FetchSourceContext fetchSourceContext,
271-
boolean forceSyntheticSource,
272-
Function<Engine.Get, Engine.GetResult> engineGetOperator
273-
) throws IOException {
274-
fetchSourceContext = normalizeFetchSourceContent(fetchSourceContext, gFields);
275-
var engineGet = new Engine.Get(realtime, realtime, id).version(version)
276-
.versionType(versionType)
277-
.setIfSeqNo(ifSeqNo)
278-
.setIfPrimaryTerm(ifPrimaryTerm);
279-
try (Engine.GetResult get = engineGetOperator.apply(engineGet)) {
280-
if (get == null) {
281-
return null;
282-
}
283-
if (get.exists() == false) {
284-
return new GetResult(shardId.getIndexName(), id, UNASSIGNED_SEQ_NO, UNASSIGNED_PRIMARY_TERM, -1, false, null, null, null);
285-
}
286-
// break between having loaded it from translog (so we only have _source), and having a document to load
287-
return innerGetFetch(id, gFields, fetchSourceContext, get, forceSyntheticSource);
288-
}
289-
}
290-
291276
private GetResult innerGetFetch(
292277
String id,
293278
String[] storedFields,
@@ -298,7 +283,6 @@ private GetResult innerGetFetch(
298283
assert get.exists() : "method should only be called if document could be retrieved";
299284
// check first if stored fields to be loaded don't contain an object field
300285
MappingLookup mappingLookup = mapperService.mappingLookup();
301-
final IndexVersion indexVersion = indexSettings.getIndexVersionCreated();
302286
final Set<String> storedFieldSet = new HashSet<>();
303287
boolean hasInferenceMetadataFields = false;
304288
if (storedFields != null) {
@@ -338,6 +322,9 @@ private GetResult innerGetFetch(
338322
throw new ElasticsearchException("Failed to get id [" + id + "]", e);
339323
}
340324

325+
final boolean supportDocValuesForIgnoredMetaField = indexSettings.getIndexVersionCreated()
326+
.onOrAfter(IndexVersions.DOC_VALUES_FOR_IGNORED_META_FIELD);
327+
341328
// put stored fields into result objects
342329
if (leafStoredFieldLoader.storedFields().isEmpty() == false) {
343330
Set<String> needed = new HashSet<>();
@@ -351,8 +338,7 @@ private GetResult innerGetFetch(
351338
if (false == needed.contains(entry.getKey())) {
352339
continue;
353340
}
354-
if (IgnoredFieldMapper.NAME.equals(entry.getKey())
355-
&& indexVersion.onOrAfter(IndexVersions.DOC_VALUES_FOR_IGNORED_META_FIELD)) {
341+
if (IgnoredFieldMapper.NAME.equals(entry.getKey()) && supportDocValuesForIgnoredMetaField) {
356342
continue;
357343
}
358344
MappedFieldType ft = mapperService.fieldType(entry.getKey());
@@ -371,9 +357,7 @@ private GetResult innerGetFetch(
371357
// NOTE: when _ignored is requested via `stored_fields` we need to load it from doc values instead of loading it from stored fields.
372358
// The _ignored field used to be stored, but as a result of supporting aggregations on it, it moved from using a stored field to
373359
// using doc values.
374-
if (indexVersion.onOrAfter(IndexVersions.DOC_VALUES_FOR_IGNORED_META_FIELD)
375-
&& storedFields != null
376-
&& Arrays.asList(storedFields).contains(IgnoredFieldMapper.NAME)) {
360+
if (supportDocValuesForIgnoredMetaField && storedFields != null && Arrays.asList(storedFields).contains(IgnoredFieldMapper.NAME)) {
377361
final DocumentField ignoredDocumentField = loadIgnoredMetadataField(docIdAndVersion);
378362
if (ignoredDocumentField != null) {
379363
if (metadataFields == null) {

server/src/test/java/org/elasticsearch/index/shard/ShardGetServiceTests.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434

3535
public class ShardGetServiceTests extends IndexShardTestCase {
3636

37+
private GetResult getForUpdate(IndexShard indexShard, String id, long ifSeqNo, long ifPrimaryTerm) throws IOException {
38+
return indexShard.getService().getForUpdate(id, ifSeqNo, ifPrimaryTerm, new String[] { RoutingFieldMapper.NAME });
39+
}
40+
3741
public void testGetForUpdate() throws IOException {
3842
Settings settings = indexSettings(IndexVersion.current(), 1, 1).build();
3943
IndexMetadata metadata = IndexMetadata.builder("test").putMapping("""
@@ -44,7 +48,7 @@ public void testGetForUpdate() throws IOException {
4448
long translogInMemorySegmentCountExpected = 0;
4549
Engine.IndexResult test = indexDoc(primary, "test", "0", "{\"foo\" : \"bar\"}");
4650
assertTrue(primary.getEngine().refreshNeeded());
47-
GetResult testGet = primary.getService().getForUpdate("0", UNASSIGNED_SEQ_NO, UNASSIGNED_PRIMARY_TERM);
51+
GetResult testGet = getForUpdate(primary, "0", UNASSIGNED_SEQ_NO, UNASSIGNED_PRIMARY_TERM);
4852
assertFalse(testGet.getFields().containsKey(RoutingFieldMapper.NAME));
4953
assertEquals(testGet.sourceRef().utf8ToString(), "{\"foo\" : \"bar\"}");
5054
assertEquals(translogInMemorySegmentCountExpected, translogInMemorySegmentCount.getAsLong());
@@ -54,7 +58,7 @@ public void testGetForUpdate() throws IOException {
5458

5559
Engine.IndexResult test1 = indexDoc(primary, "1", "{\"foo\" : \"baz\"}", XContentType.JSON, "foobar");
5660
assertTrue(primary.getEngine().refreshNeeded());
57-
GetResult testGet1 = primary.getService().getForUpdate("1", UNASSIGNED_SEQ_NO, UNASSIGNED_PRIMARY_TERM);
61+
GetResult testGet1 = getForUpdate(primary, "1", UNASSIGNED_SEQ_NO, UNASSIGNED_PRIMARY_TERM);
5862
assertEquals(testGet1.sourceRef().utf8ToString(), "{\"foo\" : \"baz\"}");
5963
assertTrue(testGet1.getFields().containsKey(RoutingFieldMapper.NAME));
6064
assertEquals("foobar", testGet1.getFields().get(RoutingFieldMapper.NAME).getValue());
@@ -70,19 +74,19 @@ public void testGetForUpdate() throws IOException {
7074
// now again from the reader
7175
Engine.IndexResult test2 = indexDoc(primary, "1", "{\"foo\" : \"baz\"}", XContentType.JSON, "foobar");
7276
assertTrue(primary.getEngine().refreshNeeded());
73-
testGet1 = primary.getService().getForUpdate("1", UNASSIGNED_SEQ_NO, UNASSIGNED_PRIMARY_TERM);
77+
testGet1 = getForUpdate(primary, "1", UNASSIGNED_SEQ_NO, UNASSIGNED_PRIMARY_TERM);
7478
assertEquals(testGet1.sourceRef().utf8ToString(), "{\"foo\" : \"baz\"}");
7579
assertTrue(testGet1.getFields().containsKey(RoutingFieldMapper.NAME));
7680
assertEquals("foobar", testGet1.getFields().get(RoutingFieldMapper.NAME).getValue());
7781
assertEquals(translogInMemorySegmentCountExpected, translogInMemorySegmentCount.getAsLong());
7882

7983
final long primaryTerm = primary.getOperationPrimaryTerm();
80-
testGet1 = primary.getService().getForUpdate("1", test2.getSeqNo(), primaryTerm);
84+
testGet1 = getForUpdate(primary, "1", test2.getSeqNo(), primaryTerm);
8185
assertEquals(testGet1.sourceRef().utf8ToString(), "{\"foo\" : \"baz\"}");
8286
assertEquals(translogInMemorySegmentCountExpected, translogInMemorySegmentCount.getAsLong());
8387

84-
expectThrows(VersionConflictEngineException.class, () -> primary.getService().getForUpdate("1", test2.getSeqNo() + 1, primaryTerm));
85-
expectThrows(VersionConflictEngineException.class, () -> primary.getService().getForUpdate("1", test2.getSeqNo(), primaryTerm + 1));
88+
expectThrows(VersionConflictEngineException.class, () -> getForUpdate(primary, "1", test2.getSeqNo() + 1, primaryTerm));
89+
expectThrows(VersionConflictEngineException.class, () -> getForUpdate(primary, "1", test2.getSeqNo(), primaryTerm + 1));
8690
closeShards(primary);
8791
}
8892

@@ -183,7 +187,7 @@ private void runGetFromTranslogWithOptions(
183187
Engine.IndexResult res = indexDoc(primary, "test", "0", docToIndex);
184188
assertTrue(res.isCreated());
185189
assertTrue(primary.getEngine().refreshNeeded());
186-
GetResult testGet = primary.getService().getForUpdate("0", UNASSIGNED_SEQ_NO, UNASSIGNED_PRIMARY_TERM);
190+
GetResult testGet = getForUpdate(primary, "0", UNASSIGNED_SEQ_NO, UNASSIGNED_PRIMARY_TERM);
187191
assertFalse(testGet.getFields().containsKey(RoutingFieldMapper.NAME));
188192
assertFalse(testGet.getFields().containsKey("foo"));
189193
assertFalse(testGet.getFields().containsKey("bar"));
@@ -194,7 +198,7 @@ private void runGetFromTranslogWithOptions(
194198

195199
indexDoc(primary, "1", docToIndex, XContentType.JSON, "foobar");
196200
assertTrue(primary.getEngine().refreshNeeded());
197-
GetResult testGet1 = primary.getService().getForUpdate("1", UNASSIGNED_SEQ_NO, UNASSIGNED_PRIMARY_TERM);
201+
GetResult testGet1 = getForUpdate(primary, "1", UNASSIGNED_SEQ_NO, UNASSIGNED_PRIMARY_TERM);
198202
assertEquals(testGet1.sourceRef() == null ? "" : testGet1.sourceRef().utf8ToString(), expectedResult);
199203
assertTrue(testGet1.getFields().containsKey(RoutingFieldMapper.NAME));
200204
assertFalse(testGet.getFields().containsKey("foo"));
@@ -252,7 +256,7 @@ public void testTypelessGetForUpdate() throws IOException {
252256
Engine.IndexResult indexResult = indexDoc(shard, "some_type", "0", "{\"foo\" : \"bar\"}");
253257
assertTrue(indexResult.isCreated());
254258

255-
GetResult getResult = shard.getService().getForUpdate("0", UNASSIGNED_SEQ_NO, UNASSIGNED_PRIMARY_TERM);
259+
GetResult getResult = getForUpdate(shard, "0", UNASSIGNED_SEQ_NO, UNASSIGNED_PRIMARY_TERM);
256260
assertTrue(getResult.isExists());
257261

258262
closeShards(shard);

0 commit comments

Comments
 (0)