Skip to content

Commit

Permalink
TSDB: Tests for nanosecond timeprecision timestamp just beyond the li…
Browse files Browse the repository at this point in the history
…mit (#80932)

add some tests for the nanosecond case
  • Loading branch information
weizijun authored Nov 23, 2021
1 parent d2217eb commit 30ed6b0
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,162 @@ set start_time and end_time without timeseries mode:
index:
time_series:
end_time: 1632625782000

---
check start_time and end_time with data_nano:
- skip:
version: " - 8.0.99"
reason: introduced in 8.1.0
- do:
indices.create:
index: test_index
body:
settings:
index:
mode: time_series
routing_path: [metricset]
time_series:
start_time: 2021-09-26T03:09:42Z
end_time: 2021-09-26T03:09:52Z
mappings:
properties:
"@timestamp":
type: date_nanos
metricset:
type: keyword
time_series_dimension: true

- do:
index:
refresh: true
index: test_index
body: {
"@timestamp": "2021-09-26T03:09:51.123456789Z",
"metricset": "pod"
}

- do:
search:
index: test_index
body:
docvalue_fields: [ '@timestamp' ]
- match: { hits.total.value: 1 }
- match: { "hits.hits.0.fields.@timestamp": [ "2021-09-26T03:09:51.123456789Z" ] }

- do:
catch: /time series index @timestamp value \[2010-09-26T03:09:52.123456789Z\] must be larger than 2021-09-26T03:09:42Z/
index:
index: test_index
body: {
"@timestamp": "2010-09-26T03:09:52.123456789Z",
"metricset": "pod"
}

- do:
catch: /time series index @timestamp value \[2031-09-26T03:09:52.123456789Z\] must be smaller than 2021-09-26T03:09:52Z/
index:
index: test_index
body: {
"@timestamp": "2031-09-26T03:09:52.123456789Z",
"metricset": "pod"
}

---
check start_time boundary with data_nano:
- skip:
version: " - 8.0.99"
reason: introduced in 8.1.0
- do:
indices.create:
index: test_index
body:
settings:
index:
mode: time_series
routing_path: [metricset]
time_series:
start_time: 2021-09-26T03:09:42Z
end_time: 2021-09-26T03:09:52Z
mappings:
properties:
"@timestamp":
type: date_nanos
metricset:
type: keyword
time_series_dimension: true

- do:
index:
refresh: true
index: test_index
body: {
"@timestamp": "2021-09-26T03:09:42.123456789Z",
"metricset": "pod"
}

- do:
search:
index: test_index
body:
docvalue_fields: [ '@timestamp' ]
- match: { hits.total.value: 1 }
- match: { "hits.hits.0.fields.@timestamp": [ "2021-09-26T03:09:42.123456789Z" ] }

- do:
catch: /time series index @timestamp value \[2021-09-26T03:09:41.123456789Z\] must be larger than 2021-09-26T03:09:42Z/
index:
index: test_index
body: {
"@timestamp": "2021-09-26T03:09:41.123456789Z",
"metricset": "pod"
}

---
check end_time boundary with data_nano:
- skip:
version: " - 8.0.99"
reason: introduced in 8.1.0
- do:
indices.create:
index: test_index
body:
settings:
index:
mode: time_series
routing_path: [metricset]
time_series:
start_time: 2021-09-26T03:09:42Z
end_time: 2021-09-26T03:09:52Z
mappings:
properties:
"@timestamp":
type: date_nanos
metricset:
type: keyword
time_series_dimension: true

- do:
index:
refresh: true
index: test_index
body: {
"@timestamp": "2021-09-26T03:09:51.123456789Z",
"metricset": "pod"
}

- do:
search:
index: test_index
body:
docvalue_fields: [ '@timestamp' ]
- match: { hits.total.value: 1 }
- match: { "hits.hits.0.fields.@timestamp": [ "2021-09-26T03:09:51.123456789Z" ] }

- do:
catch: /time series index @timestamp value \[2021-09-26T03:09:52.123456789Z\] must be smaller than 2021-09-26T03:09:52Z/
index:
index: test_index
body: {
"@timestamp": "2021-09-26T03:09:52.123456789Z",
"metricset": "pod"
}
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ public static boolean isTimeSeriesModeEnabled() {
*/
public static final Setting<Instant> TIME_SERIES_END_TIME = Setting.dateSetting(
"index.time_series.end_time",
DateUtils.MAX_NANOSECOND_INSTANT,
Instant.ofEpochMilli(DateUtils.MAX_MILLIS_BEFORE_9999),
new Setting.Validator<>() {
@Override
public void validate(Instant value) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.index.IndexMode;
import org.elasticsearch.index.mapper.DateFieldMapper.Resolution;
import org.elasticsearch.index.query.SearchExecutionContext;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentType;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -215,19 +217,35 @@ private void validateTimestamp(IndexableField field, DocumentParserContext conte
return;
}

long value = field.numericValue().longValue();
long originValue = field.numericValue().longValue();
long value = originValue;

Resolution resolution;
if (context.mappingLookup().getMapper(DEFAULT_PATH).typeName().equals(DateFieldMapper.DATE_NANOS_CONTENT_TYPE)) {
resolution = Resolution.NANOSECONDS;
value /= NSEC_PER_MSEC;
} else {
resolution = Resolution.MILLISECONDS;
}

long startTime = context.indexSettings().getTimeSeriesStartTime();
if (value < startTime) {
throw new IllegalArgumentException("time series index @timestamp value [" + value + "] must be larger than " + startTime);
throw new IllegalArgumentException(
"time series index @timestamp value ["
+ resolution.toInstant(originValue)
+ "] must be larger than "
+ Instant.ofEpochMilli(startTime)
);
}

long endTime = context.indexSettings().getTimeSeriesEndTime();
if (value >= endTime) {
throw new IllegalArgumentException("time series index @timestamp value [" + value + "] must be smaller than " + endTime);
throw new IllegalArgumentException(
"time series index @timestamp value ["
+ resolution.toInstant(originValue)
+ "] must be smaller than "
+ Instant.ofEpochMilli(endTime)
);
}
}

Expand Down

0 comments on commit 30ed6b0

Please sign in to comment.