Simplify the logic of script sort parallel collection #124639
+427
−199
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pr introduced a simpler way to support parallel collection for script sort.
The concurrency issue of script sort parallel collection was fixed in Fix concurrency issue in ScriptSortBuilder, which introduced a concurrentMap to save the relationship between leafContext and leafScript, so that the correct leafScript can be obtained later. However, we could fix the concurrency issue in another way, which may get rid of the map and simplify the code.
Before Fix concurrency issue in ScriptSortBuilder, the leaf script was a member of anonymous subClass of
BytesRefFieldComparatorSource
orDoubleValuesComparatorSource
, so that it's shared across the shard and is not thread-safe.If we create a named subClass of
BytesRefFieldComparatorSource
orDoubleValuesComparatorSource
, we can put the declaration of leaf script member in the FieldComparator (forBytesRefFieldComparatorSource
) or LeafFieldComparator (forDoubleValuesComparatorSource
). So that different comparator could hold a different reference of leaf script.In this pr, the leaf Comparator isn't wrapped as Fix concurrency issue in ScriptSortBuilder did because in each slice, the leafs are searched sequentially. So that we could set leaf script as a member of the FieldComparator, and the leaf script will be assigned to a new values for each leaf context and consumed later in the
setScorer
method, which works the same way just as before.