Skip to content

Commit 0585741

Browse files
committed
Move tsdb sort settings to IndexModeSettingsProvider
1 parent 6d0489d commit 0585741

File tree

4 files changed

+61
-13
lines changed

4 files changed

+61
-13
lines changed

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/tsdb/10_settings.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,3 +576,29 @@ check time_series empty time bound value:
576576
"@timestamp": "2021-09-26T03:09:52.123456789Z",
577577
"metricset": "pod"
578578
}
579+
580+
---
581+
default sort field:
582+
- requires:
583+
cluster_features: ["gte_v8.1.0"]
584+
reason: introduced in 8.1.0
585+
586+
- do:
587+
indices.create:
588+
index: test_index
589+
body:
590+
settings:
591+
index:
592+
mode: time_series
593+
routing_path: foo
594+
time_series:
595+
start_time: 2021-04-28T00:00:00Z
596+
end_time: 2021-04-29T00:00:00Z
597+
598+
- do:
599+
indices.get_settings:
600+
index: test_index
601+
include_defaults: true
602+
- match: { .test_index.settings.index.mode: time_series }
603+
- match: { .test_index.settings.index.sort.field: [ "_tsid", "@timestamp" ] }
604+
- match: { .test_index.settings.index.sort.order: [ "asc", "desc" ] }

server/src/main/java/org/elasticsearch/index/IndexMode.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.elasticsearch.index.mapper.TimeSeriesIdFieldMapper;
3939
import org.elasticsearch.index.mapper.TimeSeriesRoutingHashFieldMapper;
4040
import org.elasticsearch.index.mapper.TsidExtractingIdFieldMapper;
41+
import org.elasticsearch.search.sort.SortOrder;
4142

4243
import java.io.IOException;
4344
import java.time.Instant;
@@ -442,8 +443,6 @@ private static CompressedXContent createDefaultMapping(boolean includeHostName)
442443
}
443444

444445
private static final List<Setting<?>> TIME_SERIES_UNSUPPORTED = List.of(
445-
IndexSortConfig.INDEX_SORT_FIELD_SETTING,
446-
IndexSortConfig.INDEX_SORT_ORDER_SETTING,
447446
IndexSortConfig.INDEX_SORT_MODE_SETTING,
448447
IndexSortConfig.INDEX_SORT_MISSING_SETTING
449448
);
@@ -629,6 +628,29 @@ public void provideAdditionalMetadata(
629628
}
630629
if (indexMode == LOOKUP) {
631630
additionalSettings.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1);
631+
} else if (indexMode == TIME_SERIES) {
632+
var sortFields = List.of(TimeSeriesIdFieldMapper.NAME, DataStreamTimestampFieldMapper.DEFAULT_PATH);
633+
var sortOrder = List.of(SortOrder.ASC, SortOrder.DESC);
634+
635+
if (IndexSortConfig.INDEX_SORT_FIELD_SETTING.exists(indexTemplateAndCreateRequestSettings)) {
636+
if (IndexSortConfig.INDEX_SORT_FIELD_SETTING.get(indexTemplateAndCreateRequestSettings).equals(sortFields) == false) {
637+
throw new IllegalArgumentException(
638+
tsdbMode() + " is incompatible with [" + IndexSortConfig.INDEX_SORT_FIELD_SETTING.getKey() + "]"
639+
);
640+
}
641+
} else {
642+
additionalSettings.putList(IndexSortConfig.INDEX_SORT_FIELD_SETTING.getKey(), sortFields);
643+
}
644+
645+
if (IndexSortConfig.INDEX_SORT_ORDER_SETTING.exists(indexTemplateAndCreateRequestSettings)) {
646+
if (IndexSortConfig.INDEX_SORT_ORDER_SETTING.get(indexTemplateAndCreateRequestSettings).equals(sortOrder) == false) {
647+
throw new IllegalArgumentException(
648+
tsdbMode() + " is incompatible with [" + IndexSortConfig.INDEX_SORT_ORDER_SETTING.getKey() + "]"
649+
);
650+
}
651+
} else {
652+
additionalSettings.putList(IndexSortConfig.INDEX_SORT_ORDER_SETTING.getKey(), List.of("asc", "desc"));
653+
}
632654
}
633655
}
634656
}

server/src/main/java/org/elasticsearch/index/IndexSortConfig.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public static void validateSortSettings(Settings settings) {
152152
}
153153
}
154154

155-
public static class IndexSortConfigDefaults {
155+
public static class LegacyIndexSortConfigDefaults {
156156
public static final FieldSortSpec[] TIME_SERIES_SORT, TIMESTAMP_SORT, HOSTNAME_TIMESTAMP_SORT, HOSTNAME_TIMESTAMP_BWC_SORT;
157157

158158
static {
@@ -173,7 +173,12 @@ public static class IndexSortConfigDefaults {
173173
new FieldSortSpec(DataStreamTimestampFieldMapper.DEFAULT_PATH) };
174174
}
175175

176-
public static FieldSortSpec[] getDefaultSortSpecs(IndexSettings indexSettings) {
176+
public static FieldSortSpec[] getLegacyDefaultSortSpecs(IndexSettings indexSettings) {
177+
var version = indexSettings.getIndexVersionCreated();
178+
if (version.onOrAfter(IndexVersions.LOGSDB_EXPLICIT_INDEX_SORTING_DEFAULTS)) {
179+
return null;
180+
}
181+
177182
final Settings settings = indexSettings.getSettings();
178183

179184
IndexMode indexMode = indexSettings.getMode();
@@ -184,11 +189,6 @@ public static FieldSortSpec[] getDefaultSortSpecs(IndexSettings indexSettings) {
184189
return null;
185190
}
186191

187-
var version = indexSettings.getIndexVersionCreated();
188-
if (version.onOrAfter(IndexVersions.LOGSDB_EXPLICIT_INDEX_SORTING_DEFAULTS)) {
189-
return null;
190-
}
191-
192192
List<String> fields = INDEX_SORT_FIELD_SETTING.get(settings);
193193
if (INDEX_SORT_ORDER_SETTING.exists(settings)) {
194194
var order = INDEX_SORT_ORDER_SETTING.get(settings);
@@ -231,9 +231,9 @@ public IndexSortConfig(IndexSettings indexSettings) {
231231
this.indexName = indexSettings.getIndex().getName();
232232
this.indexMode = indexSettings.getMode();
233233

234-
var defaultSortSpecs = IndexSortConfigDefaults.getDefaultSortSpecs(indexSettings);
235-
if (defaultSortSpecs != null) {
236-
this.sortSpecs = defaultSortSpecs;
234+
var legacyDefaultSortSpecs = LegacyIndexSortConfigDefaults.getLegacyDefaultSortSpecs(indexSettings);
235+
if (legacyDefaultSortSpecs != null) {
236+
this.sortSpecs = legacyDefaultSortSpecs;
237237
return;
238238
}
239239

server/src/main/java/org/elasticsearch/search/aggregations/support/TimeSeriesIndexSearcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
import java.util.List;
4040
import java.util.function.IntSupplier;
4141

42-
import static org.elasticsearch.index.IndexSortConfig.IndexSortConfigDefaults.TIME_SERIES_SORT;
42+
import static org.elasticsearch.index.IndexSortConfig.LegacyIndexSortConfigDefaults.TIME_SERIES_SORT;
4343

4444
/**
4545
* An IndexSearcher wrapper that executes the searches in time-series indices by traversing them by tsid and timestamp

0 commit comments

Comments
 (0)