Skip to content

Commit

Permalink
Enable time series optimization only if it is not IndexSorted index, …
Browse files Browse the repository at this point in the history
…also ASC order reverse should only consider in @timestamp field

Signed-off-by: gashutos <[email protected]>
  • Loading branch information
gashutos committed Jun 8, 2023
1 parent 59285c0 commit d01c3c1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
19 changes: 10 additions & 9 deletions server/src/main/java/org/opensearch/index/shard/IndexShard.java
Original file line number Diff line number Diff line change
Expand Up @@ -3633,7 +3633,8 @@ private EngineConfig newEngineConfig(LongSupplier globalCheckpointSupplier) thro
isReadOnlyReplica,
replicationTracker::isPrimaryMode,
translogFactorySupplier.apply(indexSettings, shardRouting),
isTimeSeriesIndex ? DataStream.TIMESERIES_LEAF_SORTER : null // DESC @timestamp default order for timeseries
isTimeSeriesDescSortOptimizationEnabled() ? DataStream.TIMESERIES_LEAF_SORTER : null // DESC @timestamp default order for
// timeseries
);
}

Expand All @@ -3645,6 +3646,14 @@ public boolean isRemoteTranslogEnabled() {
return indexSettings() != null && indexSettings().isRemoteTranslogStoreEnabled();
}

/**
* @return true if segment reverse search optimization is enabled for time series based workload.
*/
public boolean isTimeSeriesDescSortOptimizationEnabled() {
// Do not change segment order in case of index sort.
return isTimeSeriesIndex && getIndexSort() == null;
}

/**
* @return True if settings indicate this shard is backed by a remote snapshot, false otherwise.
*/
Expand Down Expand Up @@ -4676,12 +4685,4 @@ RetentionLeaseSyncer getRetentionLeaseSyncer() {
public GatedCloseable<SegmentInfos> getSegmentInfosSnapshot() {
return getEngine().getSegmentInfosSnapshot();
}

/**
* If index is time series (if it contains @timestamp field)
* @return true or false based on above condition
*/
public boolean isTimeSeriesIndex() {
return this.isTimeSeriesIndex;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.CombinedBitSet;
import org.apache.lucene.util.SparseFixedBitSet;
import org.opensearch.cluster.metadata.DataStream;
import org.opensearch.common.lucene.search.TopDocsAndMaxScore;
import org.opensearch.core.common.lease.Releasable;
import org.opensearch.search.DocValueFormat;
Expand All @@ -75,7 +76,6 @@
import org.opensearch.search.query.QuerySearchResult;
import org.opensearch.search.sort.FieldSortBuilder;
import org.opensearch.search.sort.MinAndMax;
import org.opensearch.search.sort.SortOrder;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -512,13 +512,15 @@ private boolean shouldReverseLeafReaderContexts() {
// This is actually beneficial for search queries to start search on latest segments first for time series workload.
// That can slow down ASC order queries on timestamp workload. So to avoid that slowdown, we will reverse leaf
// reader order here.
if (searchContext != null && searchContext.indexShard().isTimeSeriesIndex()) {
if (searchContext != null && searchContext.indexShard().isTimeSeriesDescSortOptimizationEnabled()) {
// Only reverse order for asc order sort queries
if (searchContext.request() != null
&& searchContext.request().source() != null
&& searchContext.request().source().sorts() != null
&& searchContext.request().source().sorts().size() > 0
&& searchContext.request().source().sorts().get(0).order() == SortOrder.ASC) {
if (searchContext.sort() != null
&& searchContext.sort().sort != null
&& searchContext.sort().sort.getSort() != null
&& searchContext.sort().sort.getSort().length > 0
&& searchContext.sort().sort.getSort()[0].getReverse() == false
&& searchContext.sort().sort.getSort()[0].getField() != null
&& searchContext.sort().sort.getSort()[0].getField().equals(DataStream.TIMESERIES_FIELDNAME)) {
return true;
}
}
Expand Down

0 comments on commit d01c3c1

Please sign in to comment.