Skip to content

Conversation

JeremyDahlgren
Copy link
Contributor

-1 duration for TimeValue has special meaning and is the only negative value allowed in the constructor. When a TimeValue string is parsed, "-1" is converted into TimeValue.MINUS_ONE, which uses TimeUnit.MILLISECONDS. Round trip equality of TimeValue.timeValueSeconds(-1) to string form and back will fail since the time units will be different in compareTo(). This change adds a special check in compareTo() to return zero if both durations are -1. Unit test cases are also added that fail without this change.
I ran into this in a unit test when checking a time setting default value, it boiled down to a check similar to the one below which seemed reasonable, but was failing:

        final var DEFAULT_TIME_VALUE = TimeValue.timeValueSeconds(-1);
        final var timeSetting = timeSetting("key", DEFAULT_TIME_VALUE);
        assertThat(TimeValue.timeValueSeconds(-1), equalTo(timeSetting.getDefault(Settings.EMPTY)));

-1 duration for TimeValue has special meaning and is the
only negative value allowed in the constructor.  When a
TimeValue string is parsed, "-1" is converted into
TimeValue.MINUS_ONE, which uses TimeUnit.MILLISECONDS.
Round trip equality of TimeValue.timeValueSeconds(-1) to
string form and back will fail since the time units will
be different in compareTo().  This change adds a special
check in compareTo() to return zero if both durations
are -1.  Unit test cases are also added that fail without
this change.
@JeremyDahlgren JeremyDahlgren added >non-issue Team:Distributed Coordination Meta label for Distributed Coordination team :Distributed Coordination/Distributed A catch all label for anything in the Distributed Coordination area. Please avoid if you can. v9.2.0 labels Aug 27, 2025
@JeremyDahlgren JeremyDahlgren marked this pull request as ready for review August 27, 2025 19:54
@JeremyDahlgren JeremyDahlgren requested a review from a team as a code owner August 27, 2025 19:54
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-distributed-coordination (Team:Distributed Coordination)

@Override
public int compareTo(TimeValue timeValue) {
if (duration == -1L && timeValue.duration == -1L) {
return 0;
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we also handle one being -1 and the other not?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is ok: if one is negative and the other is non-negative then the negative one will correctly compare as less than the non-negative one already.

Copy link
Contributor

@DaveCTurner DaveCTurner left a comment

Choose a reason for hiding this comment

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

The -1-with-units case for time values is pretty weird and IMO there's a strong argument that permitting them in the first place was a bug. The intention was that the string -1 is a reasonable representation of a missing time value, e.g. an infinite timeout, but it is an implementation detail that this means the same as -1ms and there's no good reason for us to be able to distinguish "a missing number of milliseconds" from "a missing number of seconds".

Given that these cases exist today I'd be inclined to treat them all differently: -1s is a smaller (more negative) time value than -1ms. That would mean that the bug is that getStringRep doesn't faithfully represent these things, and I'd be supportive of fixing that.

Then, separately, I'd like us to work towards dropping support for -1-with-units time values altogether. They aren't documented anywhere AFAIK and I think it's a bug that they're even permitted, so deprecating them now and removing them in the future should have minimal impact.

In the meantime I'd suggest we could add a little bit of lenience here and change the parser to map strings -1s, -1m, -1d etc onto -1, likewise adjusting StreamInput#readTimeValue to overwrite timeUnit as MILLISECONDS if duration is negative, and rejecting all other attempts to create a negative timevalue in code. That way we'd never see any of these odd negative TimeValue instances apart from TimeValue.MINUS_ONE.

Copy link
Contributor

@DaveCTurner DaveCTurner left a comment

Choose a reason for hiding this comment

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

LGTM assuming CI is happy too

@DaveCTurner
Copy link
Contributor

The failing test is bogus:

"Test forecast given expires_in is negative":
- do:
catch: /\[expires_in\] must be non-negative[:] \[-1\]/
ml.forecast:
job_id: "forecast-job"
expires_in: "-1s"

AFAICT there's nothing in any docs or anything to say that -1s is a valid value for this expires_in field. I think we should fix the test to just use -1, in a separate PR backported to all relevant branches, which should then mean that this REST compatibility test will pass.

JeremyDahlgren added a commit that referenced this pull request Sep 3, 2025
Split from #133677.
This PR changes these ML forecast tests to use -1 instead of -1s.
The use of units with a -1 time value is not documented as a valid
value and its use should be eliminated.
Once this change has been backported successfully the
compatibility tests in #133677 should pass.
JeremyDahlgren added a commit to JeremyDahlgren/elasticsearch that referenced this pull request Sep 3, 2025
Split from elastic#133677.
This PR changes these ML forecast tests to use -1 instead of -1s.
The use of units with a -1 time value is not documented as a valid
value and its use should be eliminated.
Once this change has been backported successfully the
compatibility tests in elastic#133677 should pass.
JeremyDahlgren added a commit to JeremyDahlgren/elasticsearch that referenced this pull request Sep 3, 2025
Split from elastic#133677.
This PR changes these ML forecast tests to use -1 instead of -1s.
The use of units with a -1 time value is not documented as a valid
value and its use should be eliminated.
Once this change has been backported successfully the
compatibility tests in elastic#133677 should pass.
JeremyDahlgren added a commit to JeremyDahlgren/elasticsearch that referenced this pull request Sep 3, 2025
Split from elastic#133677.
This PR changes these ML forecast tests to use -1 instead of -1s.
The use of units with a -1 time value is not documented as a valid
value and its use should be eliminated.
Once this change has been backported successfully the
compatibility tests in elastic#133677 should pass.
elasticsearchmachine pushed a commit that referenced this pull request Sep 3, 2025
Split from #133677.
This PR changes these ML forecast tests to use -1 instead of -1s.
The use of units with a -1 time value is not documented as a valid
value and its use should be eliminated.
Once this change has been backported successfully the
compatibility tests in #133677 should pass.
elasticsearchmachine pushed a commit that referenced this pull request Sep 3, 2025
Split from #133677.
This PR changes these ML forecast tests to use -1 instead of -1s.
The use of units with a -1 time value is not documented as a valid
value and its use should be eliminated.
Once this change has been backported successfully the
compatibility tests in #133677 should pass.
elasticsearchmachine pushed a commit that referenced this pull request Sep 3, 2025
Split from #133677.
This PR changes these ML forecast tests to use -1 instead of -1s.
The use of units with a -1 time value is not documented as a valid
value and its use should be eliminated.
Once this change has been backported successfully the
compatibility tests in #133677 should pass.
@JeremyDahlgren JeremyDahlgren merged commit a1f494a into elastic:main Sep 3, 2025
33 checks passed
sarog pushed a commit to portsbuild/elasticsearch that referenced this pull request Sep 11, 2025
…4042)

Split from elastic#133677.
This PR changes these ML forecast tests to use -1 instead of -1s.
The use of units with a -1 time value is not documented as a valid
value and its use should be eliminated.
Once this change has been backported successfully the
compatibility tests in elastic#133677 should pass.
sarog pushed a commit to portsbuild/elasticsearch that referenced this pull request Sep 19, 2025
…4042)

Split from elastic#133677.
This PR changes these ML forecast tests to use -1 instead of -1s.
The use of units with a -1 time value is not documented as a valid
value and its use should be eliminated.
Once this change has been backported successfully the
compatibility tests in elastic#133677 should pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Distributed Coordination/Distributed A catch all label for anything in the Distributed Coordination area. Please avoid if you can. >non-issue Team:Distributed Coordination Meta label for Distributed Coordination team v9.2.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants