Skip to content

Commit ba5460c

Browse files
committed
Made TimestampBounds immutable and IndexSettings updates TimestampBounds if end_time changes
1 parent c16e752 commit ba5460c

5 files changed

Lines changed: 30 additions & 30 deletions

File tree

server/src/main/java/org/elasticsearch/cluster/metadata/IndexMetadata.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,7 @@ public IndexLongFieldRange getTimestampRange() {
10071007
*/
10081008
@Nullable
10091009
public IndexLongFieldRange getTimeSeriesTimestampRange() {
1010-
var bounds = indexMode != null ? indexMode.getTimestampBound(this, null) : null;
1010+
var bounds = indexMode != null ? indexMode.getTimestampBound(this) : null;
10111011
if (bounds != null) {
10121012
return IndexLongFieldRange.NO_SHARDS.extendWithShardRange(0, 1, ShardLongFieldRange.of(bounds.startTime(), bounds.endTime()));
10131013
} else {

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.elasticsearch.cluster.metadata.IndexMetadata;
1212
import org.elasticsearch.cluster.metadata.MetadataCreateDataStreamService;
1313
import org.elasticsearch.common.compress.CompressedXContent;
14-
import org.elasticsearch.common.settings.IndexScopedSettings;
1514
import org.elasticsearch.common.settings.Setting;
1615
import org.elasticsearch.common.settings.Settings;
1716
import org.elasticsearch.core.Nullable;
@@ -80,7 +79,7 @@ public CompressedXContent getDefaultMapping() {
8079
}
8180

8281
@Override
83-
public TimestampBounds getTimestampBound(IndexMetadata indexMetadata, IndexScopedSettings settings) {
82+
public TimestampBounds getTimestampBound(IndexMetadata indexMetadata) {
8483
return null;
8584
}
8685

@@ -158,8 +157,8 @@ public CompressedXContent getDefaultMapping() {
158157
}
159158

160159
@Override
161-
public TimestampBounds getTimestampBound(IndexMetadata indexMetadata, IndexScopedSettings settings) {
162-
return new TimestampBounds(indexMetadata, settings);
160+
public TimestampBounds getTimestampBound(IndexMetadata indexMetadata) {
161+
return new TimestampBounds(indexMetadata.getTimeSeriesStart(), indexMetadata.getTimeSeriesEnd());
163162
}
164163

165164
private static String routingRequiredBad() {
@@ -275,7 +274,7 @@ public String getName() {
275274
* Otherwise <code>null</code> is returned.
276275
*/
277276
@Nullable
278-
public abstract TimestampBounds getTimestampBound(IndexMetadata indexMetadata, IndexScopedSettings settings);
277+
public abstract TimestampBounds getTimestampBound(IndexMetadata indexMetadata);
279278

280279
/**
281280
* Return an instance of the {@link TimeSeriesIdFieldMapper} that generates

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ public Iterator<Setting<?>> settings() {
552552
* The bounds for {@code @timestamp} on this index or
553553
* {@code null} if there are no bounds.
554554
*/
555-
private final TimestampBounds timestampBounds;
555+
private volatile TimestampBounds timestampBounds;
556556

557557
// volatile fields are updated via #updateIndexMetadata(IndexMetadata) under lock
558558
private volatile Settings settings;
@@ -697,7 +697,12 @@ public IndexSettings(final IndexMetadata indexMetadata, final Settings nodeSetti
697697
this.indexMetadata = indexMetadata;
698698
numberOfShards = settings.getAsInt(IndexMetadata.SETTING_NUMBER_OF_SHARDS, null);
699699
mode = isTimeSeriesModeEnabled() ? scopedSettings.get(MODE) : IndexMode.STANDARD;
700-
this.timestampBounds = mode.getTimestampBound(indexMetadata, scopedSettings);
700+
this.timestampBounds = mode.getTimestampBound(indexMetadata);
701+
if (mode == IndexMode.TIME_SERIES) {
702+
scopedSettings.addSettingsUpdateConsumer(IndexSettings.TIME_SERIES_END_TIME, endTime -> {
703+
this.timestampBounds = new TimestampBounds(this.timestampBounds, endTime);
704+
});
705+
}
701706
this.searchThrottled = INDEX_SEARCH_THROTTLED.get(settings);
702707
this.queryStringLenient = QUERY_STRING_LENIENT_SETTING.get(settings);
703708
this.queryStringAnalyzeWildcard = QUERY_STRING_ANALYZE_WILDCARD.get(nodeSettings);

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

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,29 @@
77
*/
88
package org.elasticsearch.index;
99

10-
import org.elasticsearch.cluster.metadata.IndexMetadata;
11-
import org.elasticsearch.common.settings.IndexScopedSettings;
12-
import org.elasticsearch.core.Nullable;
13-
1410
import java.time.Instant;
1511

1612
/**
1713
* Bounds for the {@code @timestamp} field on this index.
1814
*/
1915
public class TimestampBounds {
2016
private final long startTime;
21-
private volatile long endTime;
17+
private final long endTime;
18+
19+
public TimestampBounds(Instant startTime, Instant endTime) {
20+
this.startTime = startTime.toEpochMilli();
21+
this.endTime = endTime.toEpochMilli();
22+
}
2223

23-
TimestampBounds(IndexMetadata indexMetadata, @Nullable IndexScopedSettings scopedSettings) {
24-
startTime = indexMetadata.getTimeSeriesStart().toEpochMilli();
25-
endTime = indexMetadata.getTimeSeriesEnd().toEpochMilli();
26-
if (scopedSettings != null) {
27-
scopedSettings.addSettingsUpdateConsumer(IndexSettings.TIME_SERIES_END_TIME, this::updateEndTime);
24+
public TimestampBounds(TimestampBounds previous, Instant newEndTime) {
25+
long newEndTimeMillis = newEndTime.toEpochMilli();
26+
if (previous.endTime > newEndTimeMillis) {
27+
throw new IllegalArgumentException(
28+
"index.time_series.end_time must be larger than current value [" + previous.endTime + "] but was [" + newEndTime + "]"
29+
);
2830
}
31+
this.startTime = previous.startTime();
32+
this.endTime = newEndTimeMillis;
2933
}
3034

3135
/**
@@ -42,16 +46,6 @@ public long endTime() {
4246
return endTime;
4347
}
4448

45-
private void updateEndTime(Instant endTimeInstant) {
46-
long newEndTime = endTimeInstant.toEpochMilli();
47-
if (this.endTime > newEndTime) {
48-
throw new IllegalArgumentException(
49-
"index.time_series.end_time must be larger than current value [" + this.endTime + "] but was [" + newEndTime + "]"
50-
);
51-
}
52-
this.endTime = newEndTime;
53-
}
54-
5549
@Override
5650
public String toString() {
5751
return startTime + "-" + endTime;

server/src/main/java/org/elasticsearch/index/mapper/DataStreamTimestampFieldMapper.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.apache.lucene.search.Query;
1414
import org.elasticsearch.common.bytes.BytesReference;
1515
import org.elasticsearch.common.xcontent.XContentHelper;
16+
import org.elasticsearch.index.IndexMode;
1617
import org.elasticsearch.index.TimestampBounds;
1718
import org.elasticsearch.index.mapper.DateFieldMapper.Resolution;
1819
import org.elasticsearch.index.query.SearchExecutionContext;
@@ -217,8 +218,9 @@ public void postParse(DocumentParserContext context) throws IOException {
217218
if (first == null) {
218219
throw new IllegalArgumentException("data stream timestamp field [" + DEFAULT_PATH + "] is missing");
219220
}
220-
TimestampBounds bounds = context.indexSettings().getTimestampBounds();
221-
if (bounds != null) {
221+
var indexMode = context.indexSettings().getMode();
222+
if (indexMode == IndexMode.TIME_SERIES) {
223+
TimestampBounds bounds = context.indexSettings().getTimestampBounds();
222224
validateTimestamp(bounds, first, context);
223225
}
224226
}

0 commit comments

Comments
 (0)