-
Notifications
You must be signed in to change notification settings - Fork 25k
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
Core: Migrating from joda to java.time. Monitoring plugin #36297
Conversation
Pinging @elastic/es-core-infra |
09de1e0
to
13846f5
Compare
ee5a5d8
to
96109e1
Compare
96109e1
to
b1f2cc6
Compare
b1f2cc6
to
93541a7
Compare
@@ -123,7 +123,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws | |||
* @return a string representing the timestamp | |||
*/ | |||
public static String toUTC(final long timestamp) { | |||
return new DateTime(timestamp, DateTimeZone.UTC).toString(); |
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.
joda toString:
Output the date time in ISO8601 format (yyyy-MM-ddTHH:mm:ss.SSSZZ).
when java-time
{@code 2007-12-03T10:15:30+01:00[Europe/Paris]}.
but it also says: The output is compatible with ISO-8601 if the offset and ID are the same.
The quick test for sample long shows that the format is the same
Instant.ofEpochMilli(123).atZone(ZoneOffset.UTC).toString()
new DateTime(123, DateTimeZone.UTC).toString();
1970-01-01T00:00:00.123Z
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.
can we make this more explicit and not rely on the toString()
method of classes we dont have control over (even though I assume this does not get changed) Using a strict_date_time
formatter might just work as expected?
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.
it should work fine - the pattern is the same
...src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTests.java
Outdated
Show resolved
Hide resolved
x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/RestSqlTranslateAction.java
Outdated
Show resolved
Hide resolved
...src/test/java/org/elasticsearch/xpack/monitoring/exporter/local/LocalExporterIntegTests.java
Show resolved
Hide resolved
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.
Looks fine overall but I think I spotted one unrelate change.
...re/src/main/java/org/elasticsearch/xpack/core/security/action/user/AuthenticateResponse.java
Outdated
Show resolved
Hide resolved
...re/src/main/java/org/elasticsearch/xpack/core/security/action/user/AuthenticateResponse.java
Outdated
Show resolved
Hide resolved
@elasticmachine run elasticsearch-ci/1 |
@elasticmachine run elasticsearch-ci/default-distro |
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.
LGTM.
ok to test |
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.
left a few comments, mostly about week year based format that should be replaced
@@ -123,7 +123,8 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws | |||
* @return a string representing the timestamp | |||
*/ | |||
public static String toUTC(final long timestamp) { | |||
return new DateTime(timestamp, DateTimeZone.UTC).toString(); |
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.
can we make this more explicit and not rely on the toString()
method of classes we dont have control over (even though I assume this does not get changed) Using a strict_date_time
formatter might just work as expected?
@@ -57,7 +57,9 @@ public CleanerService(Settings settings, ClusterSettings clusterSettings, Thread | |||
@Override | |||
protected void doStart() { | |||
logger.debug("starting cleaning service"); | |||
threadPool.schedule(executionScheduler.nextExecutionDelay(new DateTime(ISOChronology.getInstance())), executorName(), runnable); | |||
threadPool.schedule(executionScheduler.nextExecutionDelay(ZonedDateTime.now(Clock.systemDefaultZone())), |
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.
can we use Clock.systemUTC()
here? I see that we did not do this properly before though... see the DateTime() ctor
@@ -191,7 +193,7 @@ protected void doRunInLifecycle() throws Exception { | |||
*/ | |||
@Override | |||
protected void onAfterInLifecycle() { | |||
DateTime start = new DateTime(ISOChronology.getInstance()); | |||
ZonedDateTime start = ZonedDateTime.now(Clock.systemDefaultZone()); |
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.
Clock.systemUTC()
?
final DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY.MM.dd").withZoneUTC(); | ||
final String index = ".watcher-history-" + version + "-" + formatter.print(creationDate.getMillis()); | ||
protected void createWatcherHistoryIndex(final ZonedDateTime creationDate, final String version) { | ||
DateFormatter formatter = DateFormatter.forPattern("YYYY.MM.dd").withZone(ZoneOffset.UTC); |
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.
YYYY
in java time means week based year, where is in joda time it means year of era - this is lower case yyyy
in java time
see https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
see https://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormat.html
final DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY.MM.dd").withZoneUTC(); | ||
final String index = ".watcher-history-" + version + "-" + formatter.print(creationDate.getMillis()); | ||
protected void createWatcherHistoryIndex(final ZonedDateTime creationDate, final String version) { | ||
DateFormatter formatter = DateFormatter.forPattern("YYYY.MM.dd").withZone(ZoneOffset.UTC); |
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.
does it make sense to make the formatter static?
final DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY.MM.dd").withZoneUTC(); | ||
final String index = ".watcher-history-" + version + "-" + formatter.print(creationDate.getMillis()); | ||
protected void createWatcherHistoryIndex(final ZonedDateTime creationDate, final String version) { | ||
DateFormatter formatter = DateFormatter.forPattern("YYYY.MM.dd").withZone(ZoneOffset.UTC); |
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.
does it make sense to add a test for this corner case in order to catch it?
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.
hmm this is a test class, what do you suggest to catch?
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.
indeed, nevermind.
|
||
DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYY.MM.dd").withZoneUTC(); | ||
DateFormatter formatter = DateFormatter.forPattern("YYYY.MM.dd").withZone(ZoneOffset.UTC); |
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.
week based formatter
@@ -105,7 +105,7 @@ public void testIndexName() { | |||
assertThat(indexName(formatter, MonitoredSystem.BEATS, timestamp), | |||
equalTo(".monitoring-beats-" + TEMPLATE_VERSION + "-2017.08.03")); | |||
|
|||
formatter = DateTimeFormat.forPattern("YYYY-dd-MM-HH.mm.ss").withZoneUTC(); | |||
formatter = DateFormatter.forPattern("YYYY-dd-MM-HH.mm.ss").withZone(ZoneOffset.UTC); |
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.
week based formatter
@@ -352,7 +354,8 @@ public void testDynamicIndexFormatChange() throws Exception { | |||
remoteClusterAllowsWatcher, currentLicenseAllowsWatcher, watcherAlreadyExists); | |||
MockRequest recordedRequest = assertBulk(webServer); | |||
|
|||
String indexName = indexName(DateTimeFormat.forPattern("YYYY.MM.dd").withZoneUTC(), doc.getSystem(), doc.getTimestamp()); | |||
DateFormatter formatter = DateFormatter.forPattern("YYYY.MM.dd").withZone(ZoneOffset.UTC); |
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.
week based formatter
assertThat(index, equalTo(MonitoringTemplateUtils.indexName(DateTimeFormat.forPattern("YYYY.MM.dd").withZoneUTC(), | ||
expectedSystem, | ||
ISODateTimeFormat.dateTime().parseMillis(timestamp)))); | ||
DateFormatter formatter = DateFormatter.forPattern("YYYY.MM.dd"); |
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.
week based formatter
I think the most important date was not changed in the PR and this can suffer from the weekyear issue: the formatter string of the exporter that sends the data to the local cluster or a remote one. I put this test in
I think we need to fix |
// if it's not after now, then it needs to be the next day! | ||
if (next.isAfter(now) == false) { | ||
next = next.plusDays(1); | ||
} | ||
return TimeValue.timeValueMillis(next.getMillis() - now.getMillis()); | ||
return TimeValue.timeValueMillis(next.toInstant().toEpochMilli() - now.toInstant().toEpochMilli()); |
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.
minor nit: you could simplify this to Duration.between(now, next).toMillis()
(pay attention that you have to switch the order in that case)
Setting<String> setting = INDEX_NAME_TIME_FORMAT_SETTING.getConcreteSettingForNamespace(config.name); | ||
String format = setting.exists(config.settings()) ? setting.get(config.settings()) : INDEX_FORMAT; | ||
try { | ||
return DateTimeFormat.forPattern(format).withZoneUTC(); | ||
return DateFormatter.forPattern(format).withZone(ZoneOffset.UTC); |
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.
this also requires a change in 6.7, otherwise users using outdated formats will not be warned if they do so, as this uses the joda time API directly
FYI: I created #38309 for the required 6.7 change |
monitoring plugin migration from joda to java.time
refers #27330