Skip to content

[Backport 3.0] Create thread id to SourceLookup map #18107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiFunction;
import java.util.function.Supplier;

Expand Down Expand Up @@ -77,10 +78,10 @@ public class SearchLookup {
*/
private final Set<String> fieldChain;
private final DocLookup docMap;
private final SourceLookup sourceLookup;
private final FieldsLookup fieldsLookup;
private final BiFunction<MappedFieldType, Supplier<SearchLookup>, IndexFieldData<?>> fieldDataLookup;
private final int shardId;
private final ConcurrentHashMap<Long, SourceLookup> sourceLookupMap = new ConcurrentHashMap<>();

/**
* Constructor for backwards compatibility. Use the one with explicit shardId argument.
Expand All @@ -101,14 +102,22 @@ public SearchLookup(
MapperService mapperService,
BiFunction<MappedFieldType, Supplier<SearchLookup>, IndexFieldData<?>> fieldDataLookup,
int shardId
) {
this(mapperService, fieldDataLookup, shardId, new FieldsLookup(mapperService));
}

public SearchLookup(
MapperService mapperService,
BiFunction<MappedFieldType, Supplier<SearchLookup>, IndexFieldData<?>> fieldDataLookup,
int shardId,
FieldsLookup fieldsLookup
) {
this.fieldChain = Collections.emptySet();
docMap = new DocLookup(
mapperService,
fieldType -> fieldDataLookup.apply(fieldType, () -> forkAndTrackFieldReferences(fieldType.name()))
);
sourceLookup = new SourceLookup();
fieldsLookup = new FieldsLookup(mapperService);
this.fieldsLookup = fieldsLookup;
this.fieldDataLookup = fieldDataLookup;
this.shardId = shardId;
}
Expand All @@ -126,7 +135,6 @@ private SearchLookup(SearchLookup searchLookup, Set<String> fieldChain) {
searchLookup.docMap.mapperService(),
fieldType -> searchLookup.fieldDataLookup.apply(fieldType, () -> forkAndTrackFieldReferences(fieldType.name()))
);
this.sourceLookup = searchLookup.sourceLookup;
this.fieldsLookup = searchLookup.fieldsLookup;
this.fieldDataLookup = searchLookup.fieldDataLookup;
this.shardId = searchLookup.shardId;
Expand Down Expand Up @@ -160,7 +168,7 @@ public LeafSearchLookup getLeafSearchLookup(LeafReaderContext context) {
return new LeafSearchLookup(
context,
docMap.getLeafDocLookup(context),
new SourceLookup(),
sourceLookupMap.computeIfAbsent(Thread.currentThread().threadId(), K -> new SourceLookup()),
fieldsLookup.getLeafFieldsLookup(context)
);
}
Expand All @@ -173,7 +181,7 @@ public DocLookup doc() {
* Returned SourceLookup will be unrelated to any created LeafSearchLookups. Instead, use {@link LeafSearchLookup#source()} to access the related {@link SearchLookup}.
*/
public SourceLookup source() {
return sourceLookup;
return sourceLookupMap.computeIfAbsent(Thread.currentThread().threadId(), K -> new SourceLookup());
}

public int shardId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.env.Environment;
import org.opensearch.search.lookup.FieldsLookup;
import org.opensearch.search.lookup.SearchLookup;
import org.opensearch.test.OpenSearchTestCase;
import org.junit.Before;

Expand All @@ -63,10 +65,12 @@
import static org.opensearch.script.ScriptService.SCRIPT_GENERAL_CACHE_SIZE_SETTING;
import static org.opensearch.script.ScriptService.SCRIPT_GENERAL_MAX_COMPILATIONS_RATE_SETTING;
import static org.opensearch.script.ScriptService.SCRIPT_MAX_COMPILATIONS_RATE_SETTING;
import static org.opensearch.search.lookup.SearchLookup.UNKNOWN_SHARD_ID;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.sameInstance;
import static org.mockito.Mockito.mock;

public class ScriptServiceTests extends OpenSearchTestCase {

Expand Down Expand Up @@ -180,6 +184,22 @@ public void testInlineScriptCompiledOnceCache() throws IOException {
assertThat(factoryScript1, sameInstance(factoryScript2));
}

public void testScriptsUseCachedSourceLookup() throws IOException {
buildScriptService(Settings.EMPTY);
Script script = new Script(ScriptType.INLINE, "test", "1+1", Collections.emptyMap());
FieldScript.Factory factoryScript = scriptService.compile(script, FieldScript.CONTEXT);

FieldScript.LeafFactory leafFactory = factoryScript.newFactory(
new HashMap<>(),
new SearchLookup(null, null, UNKNOWN_SHARD_ID, mock(FieldsLookup.class))
);

FieldScript script1 = leafFactory.newInstance(null);
FieldScript script2 = leafFactory.newInstance(null);

assertThat(script1.getLeafLookup().source(), sameInstance(script2.getLeafLookup().source()));
}

public void testAllowAllScriptTypeSettings() throws IOException {
buildScriptService(Settings.EMPTY);

Expand Down
Loading