Skip to content
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. ML package #35949

Merged
merged 3 commits into from
Nov 28, 2018

Conversation

pgomulka
Copy link
Contributor

@pgomulka pgomulka commented Nov 27, 2018

merging against java-time branch to be able to test with elasticsearch already using java.time classes.

part of the migrating joda time work. The goal is to find usages of joda
time and refactor to use java.time.

closes: #35490
previous branch against master (on top of java-time)
relates: #35493

@pgomulka pgomulka added the WIP label Nov 27, 2018
@pgomulka pgomulka self-assigned this Nov 27, 2018
@pgomulka pgomulka force-pushed the java-time-ML branch 2 times, most recently from 1dddced to 68faca6 Compare November 27, 2018 15:11
@pgomulka pgomulka added :Core/Infra/Core Core issues without another label :ml Machine learning labels Nov 27, 2018
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra

@elasticmachine
Copy link
Collaborator

Pinging @elastic/ml-core

@pgomulka pgomulka force-pushed the java-time-ML branch 2 times, most recently from 0864968 to fce5c46 Compare November 27, 2018 15:27
@@ -66,7 +68,7 @@
public ExpiredForecastsRemover(Client client, ThreadPool threadPool) {
this.client = Objects.requireNonNull(client);
this.threadPool = Objects.requireNonNull(threadPool);
this.cutoffEpochMs = DateTime.now(ISOChronology.getInstance()).getMillis();
this.cutoffEpochMs = Instant.now().toEpochMilli();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as per discussion in original review, chronology does not matter when calculating milliseconds from epoch

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it is a default timezone..

.atStartOfDay(now.getZone())
.plusMinutes(30)
.plusMinutes(minutesOffset);
return TimeValue.timeValueMillis(next.toInstant().toEpochMilli() - now.toInstant().toEpochMilli());
Copy link
Contributor Author

@pgomulka pgomulka Nov 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we did discuss to use just Instant.now(), but here I think it is better to use ZonedDateTime since it allows to use convenient DSL for calculating next variable

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

part of the migrating joda time work. The goal is to find usages of joda
time and refactor to use java.time.

closes: elastic#35490
Copy link
Contributor

@spinscale spinscale left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one major ask regarding specifying timezones when using ZonedDateTime, otherwise I think we are pretty much there

@@ -266,7 +267,7 @@ public void testHRDSplit() throws Exception {
"\"time\": { \"type\": \"date\" } } }");

// Index some data
DateTime baseTime = new DateTime().minusYears(1);
ZonedDateTime baseTime = ZonedDateTime.now().minusYears(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ZonedDateTime.now(ZoneOffset.UTC) should be used, as IIRC the system timezone will be used otherwise

Copy link
Contributor Author

@pgomulka pgomulka Nov 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right, the system default timezone will be used. But wouldn't this be change of behaviour if we use the UTC here? it was using new DateTime() previously - meaning system default time zone was used as well

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this particular test the timezone shouldn't matter provided the text representation of the timestamps includes the timezone and the test doesn't cross the start/end of daylight saving time. (I think the bit about not spanning daylight saving is a bug in the current version of the test.)

So I don't think it would be a problem to always do the test in UTC. This is not primarily testing date/time functionality so there's no need to get coverage of different timezones. It's testing completely different functionality but just needs to generate incrementing timestamps because anomaly detection deals with time series data.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks a ton for the clarification Dave, much appreciated!

I'd like to add all methods not specifying a timezone to the forbidden apis over time, thus my request.

DateTime next = now.plusDays(1).withTimeAtStartOfDay().plusMinutes(30).plusMinutes(minutesOffset);
return TimeValue.timeValueMillis(next.getMillis() - now.getMillis());

ZonedDateTime now = ZonedDateTime.now();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UTC tz

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here again originally no time zone was specified.
DateTime.now(ISOChronology.getInstance()); is an equivalent of DateTime.now()
ISOChronology is a default and system default time zone will be defaulted too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@droberts195 would you be able to comment on these? Do you think it is safe if we refactor this to use UTC timezones, given it wasn't previously specified?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one really is intended to be in the system timezone. The reason is that the daily maintenance is supposed to happen during the night on the assumption that the night is when other load is lowest. (Of course, it's possible that assumption is wrong for some clusters.)

If the intention is to ban now() with no arguments then I guess this would achieve the same but specifying an argument:

    ZonedDateTime now = ZonedDateTime.now(ZoneId.systemDefault());

.atStartOfDay(now.getZone())
.plusMinutes(30)
.plusMinutes(minutesOffset);
return TimeValue.timeValueMillis(next.toInstant().toEpochMilli() - now.toInstant().toEpochMilli());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@@ -98,13 +96,6 @@ public void testNewTimeFieldGivenSource() {
expectThrows(IllegalArgumentException.class, () -> ExtractedField.newTimeField("time", ExtractedField.ExtractionMethod.SOURCE));
}

public void testValueGivenTimeField() {
final long millis = randomLong();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was this intentionally removed and not updated to ZonedDateTime?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the only values expected to be returned from searchHit are of types Long and String.
there is a separate testValueGivenStringTimeField but I think it wouldn't do any harm to add another testcase for testing Long.
The one for ZonedDateTime/DateTime is not needed

@@ -70,7 +69,7 @@ private void removeData(Iterator<Job> jobIterator, ActionListener<Boolean> liste
}

private long calcCutoffEpochMs(long retentionDays) {
long nowEpochMs = DateTime.now(ISOChronology.getInstance()).getMillis();
long nowEpochMs = Instant.now().toEpochMilli();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This place is worthy to having a look too in regards to our comments about defaulted timezone

@pgomulka pgomulka merged commit 3a27760 into elastic:java-time Nov 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Core/Infra/Core Core issues without another label :ml Machine learning WIP
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants