Skip to content

Use expiry(duration) with duration. #1223

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

Merged
merged 1 commit into from
Sep 14, 2021

Conversation

mikereiche
Copy link
Collaborator

Also adds test for exceptions thrown during events - with validator.

Closes #1204.

  • You have read the Spring Data contribution guidelines.
  • There is a ticket in the bug tracker for the project in our JIRA.
  • You use the code formatters provided here and have them applied to your changes. Don’t submit any formatting related changes.
  • You submit test cases (unit or integration tests) that back your changes.
  • You added yourself as author in the headers of the classes you touched. Amend the date range in the Apache license header if needed. For new types, add the license header (copy from another file and set the current year only).

@mikereiche mikereiche force-pushed the datacouch_1204_use_duration_for_expiry branch 10 times, most recently from 73f2b46 to 909d8f7 Compare September 14, 2021 06:30
pom.xml Outdated
@@ -118,6 +118,18 @@
<optional>true</optional>
</dependency>

<dependency>
Copy link
Contributor

Choose a reason for hiding this comment

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

are those really needed runtime dependencies? or do you use them for tests only? in which case they need to have test scope

}

private static Duration getExpiryDuration(Expiry annotation, Environment environment) {
if (annotation == null)
Copy link
Contributor

Choose a reason for hiding this comment

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

missing { }

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed in all three places.

@daschl
Copy link
Contributor

daschl commented Sep 14, 2021

@mikereiche does this supersede #1205 ?

@daschl daschl requested a review from dnault September 14, 2021 06:37
@daschl
Copy link
Contributor

daschl commented Sep 14, 2021

@dnault can you take a look at the instant and duration related bits please?

@mikereiche mikereiche force-pushed the datacouch_1204_use_duration_for_expiry branch 3 times, most recently from 6b44c7e to 80e69c0 Compare September 14, 2021 06:44
@@ -503,7 +503,7 @@ protected void writeInternal(final Object source, final CouchbaseDocument target
final TreeMap<Integer, String> suffixes = new TreeMap<>();
final TreeMap<Integer, String> idAttributes = new TreeMap<>();

target.setExpiration(entity.getExpiry());
target.setExpiration((int)(entity.getExpiryDuration().getSeconds()));

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

use explicit getExpiryDuration() as the other getExpiry() is sometimes duration and sometimes instant.

assertThrows(CouchbaseException.class, () -> couchbaseTemplate.findByQuery(UserSubmission.class).project(new String[] { "address.street" })
.withConsistency(QueryScanConsistency.REQUEST_PLUS).all());
assertThrows(CouchbaseException.class, () -> couchbaseTemplate.findByQuery(UserSubmission.class)
.project(new String[] { "address.street" }).withConsistency(QueryScanConsistency.REQUEST_PLUS).all());

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

formatting only

ExecutableFindByQuery q = (ExecutableFindByQuery) couchbaseTemplate
.findByQuery(Airline.class).matching(query);
.findByQuery(Airline.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).matching(query);

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Do this properly with QueryScanConsistency instead of sleep()

Also adds test for exceptions thrown during events - with validator.

Closes #1204.
@mikereiche mikereiche force-pushed the datacouch_1204_use_duration_for_expiry branch from 80e69c0 to a5f643b Compare September 14, 2021 06:55
return Instant.ofEpochSecond(cal.getTimeInMillis() / 1000); // note: Unix UTC time representation in int is okay
// until year 2038
}

private static int getExpiryValue(Expiry annotation, Environment environment) {
Copy link
Collaborator Author

@mikereiche mikereiche Sep 14, 2021

Choose a reason for hiding this comment

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

To avoid the ambiguity with getExpiry() that returns a long that is sometimes a seconds_duration and sometimes an instant_seconds_since_epoch.
getExpiryDuration() returns a Duration
getExpiryInstant() returns an Instant

@@ -84,6 +85,7 @@ public void beforeEach() {
couchbaseTemplate.removeByQuery(UserAnnotated.class).all();
couchbaseTemplate.removeByQuery(UserAnnotated2.class).all();
couchbaseTemplate.removeByQuery(UserAnnotated3.class).all();
couchbaseTemplate.removeByQuery(User.class).withConsistency(QueryScanConsistency.REQUEST_PLUS).all();
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This fixed intermittent failures when my vagrant vm got slow.

@@ -282,15 +284,23 @@ void withExpiryAndExpiryAnnotation()
}
// check that they are gone after a few seconds.
sleepSecs(4);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This gives all the failures instead of the just the first one.

@mikereiche
Copy link
Collaborator Author

@mikereiche does this supersede #1205 ?

Yes. #1205 had a merge from main on top of it. I closed it now.

@mikereiche mikereiche merged commit a879d0b into main Sep 14, 2021
@mikereiche mikereiche deleted the datacouch_1204_use_duration_for_expiry branch September 14, 2021 17:11

private static Duration getExpiryDuration(Expiry annotation, Environment environment) {
if (annotation == null) {
return Duration.ofSeconds(0);
Copy link
Contributor

@dnault dnault Sep 14, 2021

Choose a reason for hiding this comment

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

NIT: Could avoid creating a new Duration by using Duration.ZERO
Mike: Fixed

/**
* Returns the expiration time of the entity.
* <p/>
* The Couchbase format for expiration time is: - for TTL < 31 days (<= 30 * 24 * 60 * 60): expressed as a TTL in
Copy link
Contributor

Choose a reason for hiding this comment

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

NIT: Is this relevant when getting the expiry as a duration?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

copied from getExpiry()

@@ -40,6 +43,26 @@
*/
int getExpiry();
Copy link
Contributor

Choose a reason for hiding this comment

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

Can getExpiry (and the static version) be deprecated?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes. It also needs to be replaced in ReactiveFindByIdOperationSupport where it is used for getAndTouch()

return Instant.ofEpochSecond(0);
}
// we want it to be represented as a UNIX timestamp style, seconds since Epoch in UTC
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
Copy link
Contributor

@dnault dnault Sep 14, 2021

Choose a reason for hiding this comment

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

What happens if the expiry unit is not DAYS or SECONDS? (I think this is also a problem in getExpiry where this code originated.)

Since we're not worried about time zones, and UTC does not have Daylight Savings, using a calendar seems like overkill. Why not just:

long currentEpochSecond = System.curentTimeMillis() / 1000;
return Instant.ofEpochSecond(currentEpochSecond + secondsShift);

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed this method.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If it's not days, then secondsShift is used which was produced from the expiryUnit() and expiryVlaue.
annotation.expiryUnit().toSeconds(expiryValue)
I don't understand the significance of adding in DAYS vs. seconds.

*
* @return the expiration time Instant
*/
Instant getExpiryInstant();
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this method required? It seems like the value it returns is calculated from the duration specified by the user in the annotation.

If it's only used by integration tests, perhaps it could be moved into the test code?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed.

Copy link
Collaborator Author

@mikereiche mikereiche left a comment

Choose a reason for hiding this comment

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

I made a new PR for your comments.
It's getting close to the release so I was hasty getting the previous one merged.

return Instant.ofEpochSecond(0);
}
// we want it to be represented as a UNIX timestamp style, seconds since Epoch in UTC
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed this method.

@@ -40,6 +43,26 @@
*/
int getExpiry();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes. It also needs to be replaced in ReactiveFindByIdOperationSupport where it is used for getAndTouch()

/**
* Returns the expiration time of the entity.
* <p/>
* The Couchbase format for expiration time is: - for TTL < 31 days (<= 30 * 24 * 60 * 60): expressed as a TTL in
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

copied from getExpiry()

*
* @return the expiration time Instant
*/
Instant getExpiryInstant();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed.

return Instant.ofEpochSecond(0);
}
// we want it to be represented as a UNIX timestamp style, seconds since Epoch in UTC
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If it's not days, then secondsShift is used which was produced from the expiryUnit() and expiryVlaue.
annotation.expiryUnit().toSeconds(expiryValue)
I don't understand the significance of adding in DAYS vs. seconds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Warning message while using Expiry with @Document annotation
3 participants