-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Add segment sorter for data streams #75195
Add segment sorter for data streams #75195
Conversation
Pinging @elastic/es-search (Team:Search) |
3106e3a
to
4661286
Compare
@elasticmachine run elasticsearch-ci/part-1 |
4661286
to
3b25eed
Compare
It is beneficial to add leaf sorter for data streams by desc order of their max timestamp field, so that the most recent (in terms of timestamp) segments will be first. This allows to speed up sort query on @timestamp desc field, which is the most common type of query for datastreams, as we are mostly concerned with the recent data. This patch addressed for writable indices. This path also adds a new flag isDataStreamIndex to IndexMetadata that shows if this index is a part of datastream or not. TODO: - Should we also to this for frozen or readonly indices?
3b25eed
to
5360987
Compare
@mayya-sharipova I have a question because I am unfamiliar with this part of the code :) Is this leaf sorter the same as regular index sorting, or is this something that only takes effect on query or merging? Does this affect the indexing throughput the way the index-time sorting can? |
@dakrone Thank you for taking a look. Answering your questions:
Segments' sorter is different from index sorting, and is only used during querying. An index sorter by itself is only concerned about the order of docs within an individual segment (and not how the segments are organized), while the segment sorter is used during querying to start docs collection with the "right" segment, so we can terminate the collection faster once we collected to the top docs (for data streams, these would be the most recent docs).
No, it doesn't. Segment's sorter is only used during querying. I've also clarified the PR comment with this information. |
Does sorting get always applied? In the case of rollup and transform a composite agg is used and both read data in ascending order (date histogram), in addition both can not benefit from an early exit, but actually disable this explicitly. That's why I am concerned about this change if it changes the behavior for all cases. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left a comment regarding the index metadata change. It should be possible to detect if an index is part of a datastream dynamically.
@@ -387,6 +389,7 @@ public static APIBlock readFrom(StreamInput input) throws IOException { | |||
private final ActiveShardCount waitForActiveShards; | |||
private final ImmutableOpenMap<String, RolloverInfo> rolloverInfos; | |||
private final boolean isSystem; | |||
private final boolean isDataStreamIndex; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should change the index metadata for this information. That would only work on data streams created after 7.15 unless I missed something ?
Checking if a concrete index is a data stream can be done dynamically when the IndexShard
or IndexService
is created ? You can access the index abstraction lookup with clusterService.state().metadata().getIndicesLookup()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 to what @jimczi said here. Individual indices can be detached from and added to data streams in a variety of scenarios including snapshot restores and the relatively new Migrate to Data Stream API. If such a flag were included in index metadata, code would need to be added in all such places to correct the index metadata and we'd prefer to avoid that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jimczi Thank you for comment. I've started with a dynamic lookup clusterService.state().metadata().getIndicesLookup()
like you suggested but it is not allowed to to access a cluster state while modifying cluster state (e.g. during index creation). There is a function ClusterApplierService::assertNotCalledFromClusterStateApplier
that checks for it, and returns an error: "the applied cluster state is not yet available". I can investigate if it is possible to do this check somewhere earlier.
@danhermann Thanks for checking, and highlighting Migrate Data Stream API. I would think that this API would be quite rare to use, and most datastream indices would be created in a usual way. But indeed if used, we would need to update index metadata.
I will see if it is possible to do this check dynamically, but as we build more code around datastreams it would be nice to have an easy way to check if a shard is a part of datastream, with APIs like: IndexMetadata::isDataStreamIndex
or IndexShard::isDataStreamIndex
.
@hendrikmuhs Thanks for looking and raising your concerns. Answering your questions:
Yes, it will be always applied to datastream indices. But this PR is about sorting of segments, in terms of which segment is better to start when doing document collection. It adds a tiny overhead, and for aggregations that collect all documents from all segments, it doesn't really matter from what segment to start, and I don't think there will be any impact on the performance of date histogram or other aggs (but I will make sure to benchmark for this). I think a bigger concern for your use case could be #71186, which proposes to add sort on @timestamp by default to all searches on datastream indices. Please raise your very valid concerns on that issue. |
This reverts commit 5360987. As we want to dynamically specify in an index belongs to data stream.
Thanks for the response. It would be good to benchmark it. If you need help to set this up let me know. It seems tricky to benchmark, I might be wrong, but a lot of benchmarks force-merge to 1 segment before testing, we obviously don't want that behavior in this case. I have a non-PR'd yet transform track which might be useful, however we don't need transform for this. The aggregation tracks might work as well. |
It is beneficial to sort segments within a datastream's index by desc order of their max timestamp field, so that the most recent (in terms of timestamp) segments will be first. This allows to speed up sort query on @timestamp desc field, which is the most common type of query for datastreams, as we are mostly concerned with the recent data. This patch addressed this for writable indices. Segments' sorter is different from index sorting. An index sorter by itself is only concerned about the order of docs within an individual segment (and not how the segments are organized), while the segment sorter is only used during search and allows to start docs collection with the "right" segment, so we can terminate the collection faster. This PR adds a property to IndexShard `isDataStreamIndex` that shows if a shard is a part of datastream.
@jimczi Thanks for your review so far. In a9d44d9, I've added a data-stream check as you suggested; the check is done in There is an unresolved issue with migrate with data stream API, as there is no closing and reopening of shard during migration, a migration to data stream on already existing migrated indices will not be reflected. We have two options to address it:
@jimczi WDYT? |
@hendrikmuhs Do I understand you correctly that benchmarking date histograms on |
I like the idea. That'll probably require to move the timestamp metadata field in core but that makes sense to me. |
Yes, that sound like a good plan to me. Thanks for looking into it! |
Another use case that might be affected is scrolling through all docs in |
@elasticmachine run elasticsearch-ci/part-1 |
if the segment doesn't contain documents.
@elasticmachine run elasticsearch-ci/part-2 |
server/src/main/java/org/elasticsearch/cluster/metadata/DataStream.java
Outdated
Show resolved
Hide resolved
@elasticmachine run elasticsearch-ci/bwc |
It is beneficial to sort segments within a datastream's index by desc order of their max timestamp field, so that the most recent (in terms of timestamp) segments will be first. This allows to speed up sort query on @timestamp desc field, which is the most common type of query for datastreams, as we are mostly concerned with the recent data. This patch addressed this for writable indices. Segments' sorter is different from index sorting. An index sorter by itself is concerned about the order of docs within an individual segment (and not how the segments are organized), while the segment sorter is only used during search and allows to start docs collection with the "right" segment, so we can terminate the collection faster. This PR adds a property to IndexShard `isDataStreamIndex` that shows if a shard is a part of datastream.
It is beneficial to sort segments within a datastream's index by desc order of their max timestamp field, so that the most recent (in terms of timestamp) segments will be first. This allows to speed up sort query on @timestamp desc field, which is the most common type of query for datastreams, as we are mostly concerned with the recent data. This patch addressed this for writable indices. Segments' sorter is different from index sorting. An index sorter by itself is concerned about the order of docs within an individual segment (and not how the segments are organized), while the segment sorter is only used during search and allows to start docs collection with the "right" segment, so we can terminate the collection faster. This PR adds a property to IndexShard `isDataStreamIndex` that shows if a shard is a part of datastream. Backport for #75195
* master: (128 commits) Mute DieWithDignityIT (elastic#77283) Fix randomization in MlNodeShutdownIT (elastic#77281) Add target_node_name for REPLACE shutdown type (elastic#77151) [DOCS] Adds information about version compatibility headers (elastic#77096) Fix template equals when mappings are wrapped (elastic#77008) Fix TextFieldMapper Retaining a Reference to its Builder (elastic#77251) Move die with dignity to be a test module (elastic#77136) Update task names for rest compatiblity (elastic#75267) [ML] adjusting bwc serialization for elastic#77256 (elastic#77257) Move `index.hidden` from Static to Dynamic settings (elastic#77218) Handle cgroups v2 in `OsProbe` (elastic#77128) Choose postings format from FieldMapper instead of MappedFieldType (elastic#77234) Add segment sorter for data streams (elastic#75195) Update skip after backport (elastic#77212) [ML] adding new defer_definition_decompression parameter to put trained model API (elastic#77189) [ML] Fix bug in inference stats persister for when feature reset is called Only check replicas in cancelling existing recoveries. (elastic#60564) Format `AbstractFilteringTestCase` (elastic#77217) [DOCS] Fixes line breaks. (elastic#77248) Convert 'routing' values in REST API tests to strings ... # Conflicts: # server/src/main/java/org/elasticsearch/cluster/metadata/DataStream.java
PR elastic#75195 added segment sorter on @timestamp desc for datastream indices. This PR applies segment sorter to all indices that have @timestamp field. The presence of @timestamp field can serve as a strong indication that we are dealing with timeseries indices. The most common type of query for timeseries indices is to get the latest data, that is data sorted by @timestamp desc. This PR sorts segments by @timestamp desc which allows to speed up this kind of queries. Relates to elastic#75195
PR #75195 added segment sorter on @timestamp desc for datastream indices. This PR applies segment sorter to all indices that have @timestamp field. The presence of @timestamp field can serve as a strong indication that we are dealing with timeseries indices. The most common type of query for timeseries indices is to get the latest data, that is data sorted by @timestamp desc. This PR sorts segments by @timestamp desc which allows to speed up this kind of queries. Relates to #75195
PR elastic#75195 added segment sorter on @timestamp desc for datastream indices. This PR applies segment sorter to all indices that have @timestamp field. The presence of @timestamp field can serve as a strong indication that we are dealing with timeseries indices. The most common type of query for timeseries indices is to get the latest data, that is data sorted by @timestamp desc. This PR sorts segments by @timestamp desc which allows to speed up this kind of queries. Backport for elastic#78639 Relates to elastic#75195
PR #75195 added segment sorter on @timestamp desc for datastream indices. This PR applies segment sorter to all indices that have @timestamp field. The presence of @timestamp field can serve as a strong indication that we are dealing with timeseries indices. The most common type of query for timeseries indices is to get the latest data, that is data sorted by @timestamp desc. This PR sorts segments by @timestamp desc which allows to speed up this kind of queries. Backport for #78639 Relates to #75195
Ordinary indices support sorting their segments on the timestamp when they have such a field defined in their mappings (see elastic#75195). This was not initially supported as a directory reader could not be created provding a leaf sorter, which is now possible in Lucene and we can make use of.
Ordinary indices support sorting their segments on the timestamp when they have such a field defined in their mappings (see #75195). This was not initially supported as a directory reader could not be created provding a leaf sorter, which is now possible in Lucene and we can make use of.
It is beneficial to sort segments within a datastream's index
by desc order of their max timestamp field, so
that the most recent (in terms of timestamp) segments
will be first.
This allows to speed up sort query on @timestamp desc field,
which is the most common type of query for datastreams,
as we are mostly concerned with the recent data.
This patch addressed this for writable indices.
Segments' sorter is different from index sorting.
An index sorter by itself is only concerned about the order of docs
within an individual segment (and not how the segments are organized),
while the segment sorter is only used during search and allows
to start docs collection with the "right" segment,
so we can terminate the collection faster.
TODO:
Or they always supposed to have 1 segment?