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

Support Time To Live attributes in the DynamoDB Enhanced Client #4295

Open
acouvreur opened this issue Aug 11, 2023 · 7 comments
Open

Support Time To Live attributes in the DynamoDB Enhanced Client #4295

acouvreur opened this issue Aug 11, 2023 · 7 comments
Labels
dynamodb-enhanced feature-request A feature should be added or improved. p2 This is a standard priority issue

Comments

@acouvreur
Copy link
Contributor

acouvreur commented Aug 11, 2023

[Edit by @debora-ito]:

This changed to a feature request to add an annotation to manage DynamoDB TTL attributes in the DDB Enhanced Client.

Community note: add a 👍 to this if you are interested.

While this is not supported out of the box by the SDK, note that you can write a custom extension with the logic you need to create a TTL timestamp attribute - you can find an example in our Developer guide: https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/ddb-en-client-extensions.html#ddb-en-client-extensions-custom

Original issue description below:


Describe the bug

DynamoDbAutoGeneratedTimestampAttribute format in DynamoDb is incompatible with Time To Live.

When using attributes such as createdAt or upatedAt like the following:

@DynamoDbAutoGeneratedTimestampAttribute
@DynamoDbUpdateBehavior(UpdateBehavior.WRITE_IF_NOT_EXISTS)
public Instant getCreatedAt() {
    return createdAt;
}

@DynamoDbAutoGeneratedTimestampAttribute
public Instant getUpdatedAt() {
    return updatedAt;
}

The value in DynamoDb is stored with the following format: 2023-06-06T18:16:40.632760903Z.


This format is not compatible with a Time To Live attribute as documented here: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/time-to-live-ttl-before-you-start.html

The TTL attribute’s value must be a timestamp in Unix epoch time format in seconds. If you use any other format, the TTL processes ignore the item. For example, if you set the value of the attribute to 1645119622, that is Thursday, February 17, 2022 17:40:22 (GMT), the item will be expired after that time. For help formatting your epoch timestamps, you can use third-party tools such as Epoch Converter to get a visual web form.

Expected Behavior

I would expect that properties that specifically use the DynamoDbAutoGeneratedTimestampAttribute annotation would be compatible with TTL format.

Current Behavior

And as per the general usage and documented in the project (see https://github.com/search?q=repo%3Aaws%2Faws-sdk-java-v2%20DynamoDbAutoGeneratedTimestampAttribute&type=code) we are using an Instant.

If this is not compatible with the TTL format on purpose, how can I store these timestamp as TTL compatible attributes?

Reproduction Steps

@DynamoDbAutoGeneratedTimestampAttribute
@DynamoDbUpdateBehavior(UpdateBehavior.WRITE_IF_NOT_EXISTS)
public Instant getCreatedAt() {
    return createdAt;
}

@DynamoDbAutoGeneratedTimestampAttribute
public Instant getUpdatedAt() {
    return updatedAt;
}

And save something to DynamoDb, check the format is not unix timestamp.

Possible Solution

No response

Additional Information/Context

I would like to not change the property type from Instant to int or something else.

AWS Java SDK version used

2.20.30

JDK version used

18.0.2

Operating System and version

MacOS M1 (13.3.1)

@acouvreur acouvreur added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Aug 11, 2023
@debora-ito
Copy link
Member

debora-ito commented Aug 11, 2023

I don't think there was an intention to make @DynamoDbAutoGeneratedTimestampAttribute compatible with DynamoDB TTL to begin with. I'm changing to a feature request because of this. Also, we can't change the current timestamp format without a breaking change, so this will probably be a new, additional setting.

@debora-ito debora-ito added feature-request A feature should be added or improved. dynamodb-enhanced and removed bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Aug 11, 2023
@debora-ito debora-ito self-assigned this Aug 11, 2023
@acouvreur
Copy link
Contributor Author

Makes a lot of sense!
Thank you very much!

@swar8080
Copy link
Contributor

@acouvreur just curious why you want ttl to always be equal to the current timestamp, since the item will always be deleted shortly after?

In general it could be convenient to use Instant instead of Long for TTL attributes though, not just with DynamoDbAutoGeneratedTimestampAttribute. I ended-up implementing a custom AttributeConverter from Instant -> Long but had to search the documentation/sdk to confirm that isn't built-in somehow. Although it's easy enough to use a Long on the bean and use a @DynamoDbIgnore getter that returns an Instant

@acouvreur
Copy link
Contributor Author

Oh, you're completely right @swar8080 !

Actually I got it wrong with TTL in the first place.

I thought I could configure the expiration based on a updatedAt property and somewhere I could configure TTL = 1 day
So everything with now - updatedAt > 1 day would be deleted.

But in fact the TTL property only states the date after which it should be deleted.

So in my case I would have to create a new field which would be the value of updatedAt + 1 day


I understand now that this feature request does not make much sense.

However, I'd still need to "hook" on the updatedAt attribute being updated automatically to update my new expiresAt attribute.

Do you have any recommendations on how to achieve that?

@debora-ito
Copy link
Member

Does it need to be hooked up on the DynamoDbAutoGeneratedTimestampAttribute attribute?

If not, you can define a new class and write a custom AttributeConverter where it gets the current time and adds whatever logic necessary to calculate the TTL date.

@swar8080 good catch, thank you.

Closing this since it's not needed anymore.

@debora-ito debora-ito closed this as not planned Won't fix, can't repro, duplicate, stale Aug 14, 2023
@debora-ito debora-ito reopened this Aug 15, 2023
@aws aws deleted a comment from github-actions bot Aug 15, 2023
@debora-ito
Copy link
Member

Reopening this, as we got the same request in another issue.

@debora-ito debora-ito changed the title DynamoDbAutoGeneratedTimestampAttribute format in DynamoDb is incompatible with Time To Live Support Time To Live attributes in the DynamoDB Enhanced Client Aug 15, 2023
@debora-ito debora-ito added the p2 This is a standard priority issue label Aug 15, 2023
@debora-ito debora-ito removed their assignment Aug 16, 2023
@AnakinPt
Copy link

I'm very interested in this, because I don't find other way to define the TTL of a table item.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dynamodb-enhanced feature-request A feature should be added or improved. p2 This is a standard priority issue
Projects
None yet
Development

No branches or pull requests

4 participants